forked from hugoam/two
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solver.h
177 lines (135 loc) · 5.4 KB
/
Solver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (c) 2019 Hugo Amiard [email protected]
// This software is provided 'as-is' under the zlib License, see the LICENSE.txt file.
// This notice and the license may not be removed or altered from any source distribution.
#pragma once
#ifndef TWO_MODULES
#include <stl/vector.h>
#include <stl/table.h>
#endif
#include <ui/Frame/Frame.h>
#include <ui/Style/Layout.h>
namespace two
{
using SolverVector = vector<FrameSolver*>;
void collect_solvers(Frame& frame, SolverVector& solvers, DirtyLayout dirtyTop);
void relayout(SolverVector& solvers);
export_ class refl_ TWO_UI_EXPORT FrameSolver : public UiRect
{
public:
FrameSolver();
FrameSolver(FrameSolver* solver, Layout* layout, Frame* frame = nullptr);
virtual ~FrameSolver() {}
inline bool flow() { return d_layout->m_flow == LayoutFlow::Flow; }
inline bool posflow() { return d_layout->m_flow <= LayoutFlow::Align; }
inline bool sizeflow() { return d_layout->m_flow <= LayoutFlow::Overlay; }
inline float dpadding(Axis dim) { return d_layout->m_padding[size_t(dim)]; }
inline float dbackpadding(Axis dim) { return d_layout->m_padding[size_t(dim) + 2]; }
inline float dmargin(Axis dim) { return d_layout->m_margin[dim]; }
inline Align dalign(Axis dim) { return d_layout->m_align[dim]; }
inline float dcontent(Axis dim) { return d_content[dim] + dpadding(dim) + dbackpadding(dim); }
inline float dbounds(Axis dim) { return dcontent(dim) + dmargin(dim) * 2.f; }
inline float dextent(Axis dim) { return m_size[dim] + dmargin(dim) * 2.f; }
inline float doffset(Axis dim) { return m_position[dim] + m_size[dim] + dmargin(dim); }
inline float dspace(Axis dim) { return m_size[dim] - dpadding(dim) - dbackpadding(dim); }
//inline float spacing(FrameSolver& frame) { return d_prev ? d_layout->m_spacing[d_length] : 0.f; }
inline float spacing() { return d_layout->m_spacing[d_length]; }
void setup(const vec2& position, const vec2& size, const vec2& span, const vec2* content)
{
m_position = position;
m_span = span;
m_size = size;
if(d_sizing.x == Sizing::Fixed) d_content.x = (content ? content->x : m_size.x - dpadding(Axis::X) - dbackpadding(Axis::X));
if(d_sizing.y == Sizing::Fixed) d_content.y = (content ? content->y : m_size.y - dpadding(Axis::Y) - dbackpadding(Axis::Y));
if(d_sizing.x == Sizing::Fixed) m_size.x = d_content.x + dpadding(Axis::X) + dbackpadding(Axis::X);
if(d_sizing.y == Sizing::Fixed) m_size.y = d_content.y + dpadding(Axis::Y) + dbackpadding(Axis::Y);
}
void reset(bool partial = false)
{
m_size = { 0.f, 0.f };
if(!partial)
d_content = { 0.f, 0.f };
d_spacing = { 0.f, 0.f };
m_space_content = { 0.f, 0.f };
d_content_expand = false;
d_total_span = 0.f;
d_prev = nullptr;
d_count = 0;
}
void applySpace(Axis length = Axis::None);
virtual void collect(SolverVector& solvers);
virtual FrameSolver& solver(FrameSolver& frame, Axis dim);
virtual FrameSolver* grid() { return nullptr; }
void sync();
void compute();
void layout();
void read();
virtual void compute(FrameSolver& frame, Axis dim);
virtual void layout(FrameSolver& frame, Axis dim);
public:
Frame* d_frame;
FrameSolver* d_parent;
Layout* d_layout;
table<Axis, FrameSolver*> m_solvers;
//FrameSolver* m_solvers[2];
FrameSolver* d_grid;
Axis d_length = Axis::None;
Axis d_depth = Axis::None;
v2<Sizing> d_sizing = { Sizing::Shrink, Sizing::Shrink };
vec2 d_content = { 0.f, 0.f };
vec2 d_spacing = { 0.f, 0.f };
vec2 m_space_content = { 0.f, 0.f };
bool d_content_expand = false;
float d_total_span;
v2<uint> d_index;
FrameSolver* d_prev = nullptr;
size_t d_count = 0;
};
export_ class refl_ TWO_UI_EXPORT RowSolver : public FrameSolver
{
public:
RowSolver();
RowSolver(FrameSolver* solver, Layout* layout, Frame* frame = nullptr);
virtual void compute(FrameSolver& frame, Axis dim);
virtual void layout(FrameSolver& frame, Axis dim);
protected:
void measure(FrameSolver& frame, Axis dim);
void resize(FrameSolver& frame, Axis dim);
void position(FrameSolver& frame, Axis dim);
float positionFree(FrameSolver& frame, Axis dim, float space);
float positionSequence(FrameSolver& frame, float space);
};
export_ class TWO_UI_EXPORT CustomSolver : public RowSolver
{
public:
CustomSolver(FrameSolver* solver, Layout* layout, Frame* frame = nullptr);
CustomSolver(const CustomSolver& other) = delete;
CustomSolver& operator=(const CustomSolver& other) = delete;
virtual void collect(SolverVector& solvers);
protected:
vector<unique<FrameSolver>> m_solvers;
};
export_ class refl_ TWO_UI_EXPORT TableSolver : public CustomSolver
{
public:
TableSolver(FrameSolver* solver, Layout* layout, Frame* frame = nullptr);
void divide(span<float> spans);
void update(span<float> spans);
virtual FrameSolver& solver(FrameSolver& frame, Axis dim);
virtual FrameSolver* grid() { return this; }
};
export_ class refl_ TWO_UI_EXPORT LineSolver : public RowSolver
{
public:
LineSolver(FrameSolver* solver, Space space);
LineSolver(const LineSolver& other) = delete; // @kludge for clang modules bug
LineSolver& operator=(const LineSolver& other) = delete;
Layout d_layout;
};
export_ class refl_ TWO_UI_EXPORT GridSolver : public CustomSolver
{
public:
GridSolver(FrameSolver* solver, Layout* layout, Frame* frame = nullptr);
void divide(span<Space> spaces);
virtual FrameSolver& solver(FrameSolver& frame, Axis dim);
};
}