File: vertical-layout.cpp

package info (click to toggle)
ares 126-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,600 kB
  • sloc: cpp: 356,508; ansic: 20,394; makefile: 16; sh: 2
file content (318 lines) | stat: -rw-r--r-- 8,867 bytes parent folder | download | duplicates (2)
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
#if defined(Hiro_VerticalLayout)

auto mVerticalLayout::alignment() const -> maybe<f32> {
  return state.alignment;
}

auto mVerticalLayout::append(sSizable sizable, Size size, f32 spacing) -> type& {
  for(auto& cell : state.cells) {
    if(cell->state.sizable == sizable) return *this;
  }

  VerticalLayoutCell cell;
  cell->setSizable(sizable);
  cell->setSize(size);
  cell->setSpacing(spacing);
  cell->setParent(this, cellCount());
  state.cells.append(cell);
  return synchronize();
}

auto mVerticalLayout::cell(u32 position) const -> VerticalLayoutCell {
  return state.cells(position, {});
}

auto mVerticalLayout::cell(sSizable sizable) const -> VerticalLayoutCell {
  for(auto& cell : state.cells) {
    if(cell->state.sizable == sizable) return cell;
  }
  return {};
}

auto mVerticalLayout::cells() const -> vector<VerticalLayoutCell> {
  return state.cells;
}

auto mVerticalLayout::cellCount() const -> u32 {
  return state.cells.size();
}

auto mVerticalLayout::destruct() -> void {
  for(auto& cell : state.cells) cell->destruct();
  mSizable::destruct();
}

auto mVerticalLayout::minimumSize() const -> Size {
  f32 width = 0;
  for(auto index : range(cellCount())) {
    auto cell = this->cell(index);
    if(cell.collapsible()) continue;
    if(cell.size().width() == Size::Minimum || cell.size().width() == Size::Maximum) {
      width = max(width, cell.sizable().minimumSize().width());
      continue;
    }
    width = max(width, cell.size().width());
  }

  f32 height = 0;
  f32 spacing = 0;
  for(auto index : range(cellCount())) {
    auto cell = this->cell(index);
    if(cell.collapsible()) continue;
    if(cell.size().height() == Size::Minimum || cell.size().height() == Size::Maximum) {
      height += cell.sizable().minimumSize().height();
    } else {
      height += cell.size().height();
    }
    height += spacing;
    spacing = cell.spacing();
  }

  return {
    padding().x() + width  + padding().width(),
    padding().y() + height + padding().height()
  };
}

auto mVerticalLayout::padding() const -> Geometry {
  return state.padding;
}

auto mVerticalLayout::remove(sSizable sizable) -> type& {
  for(auto& cell : state.cells) {
    if(cell->state.sizable == sizable) return remove(cell);
  }
  return *this;
}

auto mVerticalLayout::remove(sVerticalLayoutCell cell) -> type& {
  if(cell->parent() != this) return *this;
  auto offset = cell->offset();
  cell->setParent();
  state.cells.remove(offset);
  for(u32 n : range(offset, cellCount())) state.cells[n]->adjustOffset(-1);
  return synchronize();
}

auto mVerticalLayout::reset() -> type& {
  while(state.cells) remove(state.cells.right());
  return synchronize();
}

auto mVerticalLayout::resize() -> type& {
  setGeometry(geometry());
  return *this;
}

auto mVerticalLayout::setAlignment(maybe<f32> alignment) -> type& {
  state.alignment = alignment;
  return synchronize();
}

auto mVerticalLayout::setEnabled(bool enabled) -> type& {
  mSizable::setEnabled(enabled);
  for(auto& cell : state.cells) cell.sizable().setEnabled(cell.sizable().enabled());
  return *this;
}

auto mVerticalLayout::setFont(const Font& font) -> type& {
  mSizable::setFont(font);
  for(auto& cell : state.cells) cell.sizable().setFont(cell.sizable().font());
  return *this;
}

auto mVerticalLayout::setGeometry(Geometry requestedGeometry) -> type& {
//if(!visible(true)) return mSizable::setGeometry(requestedGeometry), *this;

  auto geometry = requestedGeometry;
  geometry.setX(geometry.x() + padding().x());
  geometry.setY(geometry.y() + padding().y());
  geometry.setWidth (geometry.width()  - padding().x() - padding().width());
  geometry.setHeight(geometry.height() - padding().y() - padding().height());

  f32 width = 0;
  for(u32 index : range(cellCount())) {
    auto cell = this->cell(index);
    if(cell.collapsible()) continue;
    if(cell.size().width() == Size::Maximum) {
      width = geometry.width();
      break;
    } else if(cell.size().width() == Size::Minimum) {
      width = max(width, cell.sizable().minimumSize().width());
    } else {
      width = max(width, cell.size().width());
    }
  }

  vector<f32> heights;
  heights.resize(cellCount());
  u32 maximumHeights = 0;
  for(u32 index : range(cellCount())) {
    auto cell = this->cell(index);
    if(cell.collapsible()) continue;
    f32 height = 0;
    if(cell.size().height() == Size::Maximum) {
      height = Size::Maximum;
      maximumHeights++;
    } else if(cell.size().height() == Size::Minimum) {
      height = cell.sizable().minimumSize().height();
    } else {
      height = cell.size().height();
    }
    heights[index] = height;
  }

  f32 fixedHeight = 0;
  f32 spacing = 0;
  for(u32 index : range(cellCount())) {
    auto cell = this->cell(index);
    if(cell.collapsible()) continue;
    if(heights[index] != Size::Maximum) fixedHeight += heights[index];
    fixedHeight += spacing;
    spacing = cell.spacing();
  }

  f32 maximumHeight = (geometry.height() - fixedHeight) / maximumHeights;
  for(auto& height : heights) {
    if(height == Size::Maximum) height = maximumHeight;
  }

  f32 geometryX = geometry.x();
  f32 geometryY = geometry.y();
  for(u32 index : range(cellCount())) {
    auto cell = this->cell(index);
    if(cell.collapsible()) continue;
    f32 geometryWidth  = width;
    f32 geometryHeight = heights[index];
    auto alignment = cell.alignment();
    if(!alignment) alignment = this->alignment();
    if(!alignment) alignment = 0.0;
    f32 cellWidth  = cell.size().width();
    f32 cellHeight = geometryHeight;
    if(cellWidth == Size::Minimum) cellWidth = cell.sizable()->minimumSize().width();
    if(cellWidth == Size::Maximum) cellWidth = geometryWidth;
    f32 cellX = geometryX + alignment() * (geometryWidth - cellWidth);
    f32 cellY = geometryY;
    cell.sizable().setGeometry({cellX, cellY, cellWidth, cellHeight});
    geometryY += geometryHeight + cell.spacing();
  }

  mSizable::setGeometry(requestedGeometry);
  return *this;
}

auto mVerticalLayout::setPadding(Geometry padding) -> type& {
  state.padding = padding;
  return synchronize();
}

auto mVerticalLayout::setParent(mObject* parent, s32 offset) -> type& {
  for(auto& cell : reverse(state.cells)) cell->destruct();
  mSizable::setParent(parent, offset);
  for(auto& cell : state.cells) cell->setParent(this, cell->offset());
  return *this;
}

auto mVerticalLayout::setSpacing(f32 spacing) -> type& {
  state.spacing = spacing;
  return synchronize();
}

auto mVerticalLayout::setVisible(bool visible) -> type& {
  mSizable::setVisible(visible);
  for(auto& cell : state.cells) cell.sizable().setVisible(cell.sizable().visible());
  return synchronize();
}

auto mVerticalLayout::spacing() const -> f32 {
  return state.spacing;
}

auto mVerticalLayout::synchronize() -> type& {
  setGeometry(geometry());
  return *this;
}

//

auto mVerticalLayoutCell::alignment() const -> maybe<f32> {
  return state.alignment;
}

auto mVerticalLayoutCell::collapsible() const -> bool {
  if(state.sizable) return state.sizable->collapsible() && !state.sizable->visible();
  return false;
}

auto mVerticalLayoutCell::destruct() -> void {
  if(auto& sizable = state.sizable) sizable->destruct();
  mObject::destruct();
}

auto mVerticalLayoutCell::setAlignment(maybe<f32> alignment) -> type& {
  state.alignment = alignment;
  return synchronize();
}

auto mVerticalLayoutCell::setEnabled(bool enabled) -> type& {
  mObject::setEnabled(enabled);
  state.sizable->setEnabled(state.sizable->enabled());
  return *this;
}

auto mVerticalLayoutCell::setFont(const Font& font) -> type& {
  mObject::setFont(font);
  state.sizable->setFont(state.sizable->font());
  return *this;
}

auto mVerticalLayoutCell::setParent(mObject* parent, s32 offset) -> type& {
  state.sizable->destruct();
  mObject::setParent(parent, offset);
  state.sizable->setParent(this, 0);
  return *this;
}

auto mVerticalLayoutCell::setSizable(sSizable sizable) -> type& {
  state.sizable = sizable;
  return synchronize();
}

auto mVerticalLayoutCell::setSize(Size size) -> type& {
  state.size = size;
  return synchronize();
}

auto mVerticalLayoutCell::setSpacing(f32 spacing) -> type& {
  state.spacing = spacing;
  return synchronize();
}

auto mVerticalLayoutCell::setVisible(bool visible) -> type& {
  mObject::setVisible(visible);
  state.sizable->setVisible(state.sizable->visible());
  return *this;
}

auto mVerticalLayoutCell::sizable() const -> Sizable {
  return state.sizable ? state.sizable : Sizable();
}

auto mVerticalLayoutCell::size() const -> Size {
  return state.size;
}

auto mVerticalLayoutCell::spacing() const -> f32 {
  return state.spacing;
}

auto mVerticalLayoutCell::synchronize() -> type& {
  if(auto parent = this->parent()) {
    if(auto verticalLayout = dynamic_cast<mVerticalLayout*>(parent)) {
      verticalLayout->synchronize();
    }
  }
  return *this;
}

#endif