-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtemp.cpp
387 lines (341 loc) · 10.6 KB
/
temp.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <fmt/format.h>
#include <nodegraph/canvas.h>
#include <nodegraph/logger/logger.h>
#include <nodegraph/theme.h>
#include <nodegraph/widgets/layout.h>
namespace NodeGraph {
Layout::Layout(LayoutType type)
{
m_layoutType = type;
}
// Useful for indexing into glm::vec2
int Layout::GetAxisIndex(Axis axis) const
{
switch (m_layoutType)
{
case LayoutType::Vertical:
if (axis == Axis::Major)
{
return 1;
}
else
{
return 0;
}
break;
case LayoutType::Horizontal:
if (axis == Axis::Major)
{
return 0;
}
else
{
return 1;
}
break;
}
assert(!"Invalid");
return 0;
}
float Layout::SpaceForWidgets(size_t count) const
{
// Add space between all widgets
float spacing = 0.0f;
if (count > 0)
{
spacing += ((count - 1) * m_spacing);
}
return spacing;
}
tWidgets Layout::GetNonFixedWidgets() const
{
tWidgets ret;
std::copy_if(m_children.begin(), m_children.end(), std::back_inserter(ret), [](auto pWidget) {
return !(pWidget->GetFlags() & WidgetFlags::DoNotLayout);
});
return ret;
}
void Layout::Update()
{
tWidgets layoutWidgets = GetNonFixedWidgets();
if (layoutWidgets.empty())
{
return;
}
// Find out the total max width/height the child widgets would like to be
SizeHint hint;
GetChildrenSizeHint(hint);
float totalFixedSize = 0;
float variableCount = 0;
for (auto& pWidget : layoutWidgets)
{
glm::uvec2 constraints = pWidget->GetConstraints();
NRectf widgetRect = pWidget->GetRectWithPad();
switch (m_layoutType)
{
case LayoutType::Vertical:
if (constraints.y == LayoutConstraint::Fixed)
{
totalFixedSize += widgetRect.Height();
}
else
{
variableCount++;
}
break;
case LayoutType::Horizontal:
if (constraints.x == LayoutConstraint::Fixed)
{
totalFixedSize += widgetRect.Width();
}
else
{
variableCount++;
}
break;
}
}
auto spacingSize = SpaceForWidgets(layoutWidgets.size());
// The rectangle for our layout contains all widgets plus our own padding
// The layout rectangle grows in the minor axis but not the major one; it is fixed at whatever size it was when it was stacked
auto contentMargins = GetContentsMargins();
float availableSize = 0;
NRectf layoutRect = GetRect();
switch (m_layoutType)
{
case LayoutType::Vertical:
// Set size of owner
// The 'upper' layout will have sorted us into even rects, but here is our chance to constrain around the found widgets.
if (m_constraints.y == LayoutConstraint::Fixed) // Expand to fill space vertically, or constrain?
{
layoutRect.SetSize(m_constraints.x == LayoutConstraint::Expanding ? hint.hint.x + contentMargins.x + contentMargins.z : layoutRect.Width(), layoutRect.Height());
}
availableSize = layoutRect.Height() - totalFixedSize - contentMargins.y - contentMargins.w;
break;
case LayoutType::Horizontal:
if (m_constraints.x == LayoutConstraint::Fixed)
{
layoutRect.SetSize(layoutRect.Width(), m_constraints.y == LayoutConstraint::Expanding ? hint.hint.y + contentMargins.y + contentMargins.w : layoutRect.Height());
}
availableSize = layoutRect.Width() - totalFixedSize - contentMargins.x - contentMargins.z;
break;
}
// Rect is the max minor axis size + content margins
// Major axis is whatever it was before
m_rect = layoutRect;
// Layout rect is now the inner rect; in child rect coordinates
layoutRect = NRectf(0.0f, 0.0f, layoutRect.Width(), layoutRect.Height());
layoutRect.Adjust(contentMargins.x, contentMargins.y, -contentMargins.z, -contentMargins.w);
//layoutRect.Validate();
// Local World space
m_innerRect = layoutRect.Adjusted(m_rect.TopLeft());
// Resize all the widgets to fit
float expandingWidgetSize = (availableSize - totalFixedSize - spacingSize) / variableCount;
for (int i = 0; i < layoutWidgets.size(); i++)
{
auto& pWidget = layoutWidgets[i];
NRectf widgetRect = pWidget->GetRectWithPad();
// if this is a layout, make it fill the parent's minor axis
// Effectively it is expanding in this direction
if (auto pLayout = dynamic_cast<Layout*>(pWidget.get()))
{
switch (m_layoutType)
{
case LayoutType::Vertical:
widgetRect.SetSize(glm::vec2(layoutRect.Width(), pLayout->GetRect().Height()));
break;
case LayoutType::Horizontal:
widgetRect.SetSize(glm::vec2(pLayout->GetRect().Width(), layoutRect.Height()));
break;
}
}
glm::uvec2 constraints = pWidget->GetConstraints();
auto space = ((i == (layoutWidgets.size() - 1)) ? 0.0f : m_spacing);
// Move and resize the rectangle to the correct location
switch (m_layoutType)
{
case LayoutType::Vertical:
widgetRect.Move(glm::vec2(layoutRect.Left(), layoutRect.Top()));
widgetRect.SetSize(constraints.x == LayoutConstraint::Expanding ? layoutRect.Width() : widgetRect.Width(), constraints.y == LayoutConstraint::Expanding ? expandingWidgetSize : widgetRect.Height());
layoutRect.SetTop(widgetRect.Bottom() + space);
break;
case LayoutType::Horizontal:
widgetRect.Move(glm::vec2(layoutRect.Left(), layoutRect.Top()));
widgetRect.SetSize(constraints.x == LayoutConstraint::Expanding ? expandingWidgetSize : widgetRect.Width(), constraints.y == LayoutConstraint::Expanding ? layoutRect.Height() : widgetRect.Height());
layoutRect.SetLeft(widgetRect.Right() + space);
break;
}
pWidget->SetRectWithPad(widgetRect);
}
// Now the widgets might be too far apart; if we are stacking top->bottom
/*
float lastEdge = 0;
for (auto& pWidget : layoutWidgets)
{
auto rc = pWidget->GetRectWithPad();
if (m_layoutType == LayoutType::Vertical)
{
if (rc.Top() > (lastEdge + m_spacing))
{
pWidget->SetRectWithPad(rc.Moved(glm::vec2(rc.Left(), lastEdge + m_spacing)));
}
lastEdge = rc.Bottom();
}
else
{
if (rc.Left() > (lastEdge + m_spacing))
{
pWidget->SetRectWithPad(rc.Moved(glm::vec2(lastEdge + m_spacing, rc.Top())));
}
lastEdge = rc.Right();
}
}
*/
}
void Layout::SetRect(const NRectf& sz)
{
Widget::SetRect(sz);
Update();
}
void Layout::SetRectWithPad(const NRectf& rc)
{
auto cm = GetContentsMargins();
auto newRc = rc.Adjusted(glm::vec4(cm.x, cm.y, -cm.z, -cm.w));
Widget::SetRect(newRc);
Update();
}
NRectf Layout::GetRectWithPad() const
{
auto cm = GetContentsMargins();
return m_rect.Adjusted(glm::vec4(-cm.x, -cm.y, cm.z, cm.w));
}
void Layout::AddChild(std::shared_ptr<Widget> spWidget)
{
m_children.push_back(spWidget);
spWidget->SetParent(this);
SortWidgets();
}
void Layout::MoveChildToBack(std::shared_ptr<Widget> pWidget)
{
auto itr = std::find_if(m_children.begin(),
m_children.end(),
[&](const auto& pFound) -> bool {
return pFound.get() == pWidget.get();
});
if (itr != m_children.end())
{
m_children.erase(itr);
m_children.insert(m_children.end(), pWidget);
}
SortWidgets();
}
void Layout::MoveChildToFront(std::shared_ptr<Widget> pWidget)
{
auto itr = std::find_if(m_children.begin(),
m_children.end(),
[&](const auto& pFound) -> bool {
return pFound.get() == pWidget.get();
});
if (itr != m_children.end())
{
m_children.erase(itr);
m_children.insert(m_children.begin(), pWidget);
}
SortWidgets();
}
const WidgetList& Layout::GetFrontToBack() const
{
return m_frontToBack;
}
const WidgetList& Layout::GetBackToFront() const
{
return m_children;
}
void Layout::SortWidgets()
{
m_frontToBack = m_children;
std::reverse(m_frontToBack.begin(), m_frontToBack.end());
}
const WidgetList& Layout::GetChildren() const
{
return m_children;
}
void Layout::Draw(Canvas& canvas)
{
auto& theme = ThemeManager::Instance();
if (theme.GetBool(b_debugShowLayout))
{
if (m_layoutType == LayoutType::Horizontal)
{
canvas.FillRect(ToWorldRect(GetRectWithPad()), glm::vec4(1.0f, 0.2f, 0.2f, 1.0f));
canvas.FillRect(ToWorldRect(m_rect), glm::vec4(0.2f, 1.0f, 0.2f, 1.0f));
}
else
{
canvas.FillRect(ToWorldRect(GetRectWithPad()), glm::vec4(0.2f, 0.2f, 1.0f, 1.0f));
canvas.FillRect(ToWorldRect(m_rect), glm::vec4(0.5f, 0.2f, 0.5f, 1.0f));
}
}
for (auto& child : GetLayout()->GetBackToFront())
{
child->Draw(canvas);
}
}
Layout* Layout::GetLayout()
{
return this;
}
void Layout::SetContentsMargins(const glm::vec4& contentsMargins)
{
m_contentsMargins = contentsMargins;
}
const glm::vec4& Layout::GetContentsMargins() const
{
return m_contentsMargins;
}
LayoutType Layout::GetLayoutType() const
{
return m_layoutType;
}
void Layout::SetSpacing(float val)
{
m_spacing = val;
}
// For the direct children of this layout, get the size hint.
// This is the required size for the widgets
void Layout::GetChildrenSizeHint(SizeHint& hint) const
{
for (auto& spChild : m_children)
{
if (auto pLayout = dynamic_cast<Layout*>(spChild.get()))
{
pLayout->GetChildrenSizeHint(hint);
}
else
{
auto pad = spChild->GetPadding();
auto padSize = glm::vec2(pad.x + pad.z, pad.y + pad.w);
hint.hint = glm::max(spChild->GetSizeHint() + padSize, hint.hint);
}
}
}
// Find the range of sizes for the child layout
glm::vec4 Layout::GetChildrenMinMaxSize() const
{
auto minMax = glm::vec4(0.0f, 0.0f, 0.0f, 0.0f);
for (auto& spChild : m_children)
{
glm::vec4 childMinMax;
if (auto pLayout = dynamic_cast<Layout*>(spChild.get()))
{
childMinMax = pLayout->GetChildrenMinMaxSize();
}
else
{
childMinMax = spChild->GetMinMaxSize();
}
minMax += childMinMax;
}
return minMax;
}
}