hui 0.1.0-alpha.6

Simple UI library for games and other interactive applications
Documentation
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//! a container element that can hold and layout multiple children elements

use derive_setters::Setters;
use glam::{Vec2, vec2};
use crate::{
  element::{ElementList, MeasureContext, ProcessContext, UiElement},
  frame::{Frame, RectFrame},
  layout::{compute_size, Alignment, Alignment2d, Direction, LayoutInfo, Size, Size2d, WrapBehavior},
  measure::{Hints, Response},
  rect::Sides,
};

//XXX: add Order/Direction::Forward/Reverse or sth?
//TODO: clip children flag
//TODO: borders
//TODO: min/max size

#[derive(Clone, Copy)]
struct CudLine {
  start_idx: usize,
  content_size: Vec2,
  remaining_space: f32,
}

struct ContainerUserData {
  lines: Vec<CudLine>,
}

/// A container element that can hold and layout multiple children elements
#[derive(Setters)]
#[setters(prefix = "with_")]
pub struct Container {
  /// Size of the container
  #[setters(into)]
  pub size: Size2d,

  /// Layout direction (horizontal/vertical)
  pub direction: Direction,

  //XXX: should we have separate gap value for primary and secondary (when wrapped, between lines of elements) axis?

  /// Gap between children elements
  pub gap: f32,

  /// Padding inside the container (distance from the edges to the children elements)
  #[setters(into)]
  pub padding: Sides<f32>,

  /// Alignment of the children elements on X and Y axis
  #[setters(into)]
  pub align: Alignment2d,

  #[setters(skip)]
  pub background_frame: Box<dyn Frame>,

  /// Controls if wrapping is enabled
  #[setters(into)]
  pub wrap: WrapBehavior,

  /// List of children elements
  #[setters(skip)]
  pub children: ElementList,
}

impl Container {
  pub fn with_children(mut self, ui: impl FnOnce(&mut ElementList)) -> Self {
    self.children.0.extend(ElementList::from_callback(ui).0);
    self
  }

  pub fn with_background(mut self, frame: impl Frame + 'static) -> Self {
    self.background_frame = Box::new(frame);
    self
  }
}

impl Default for Container {
  fn default() -> Self {
    Self {
      size: (Size::Auto, Size::Auto).into(),
      direction: Direction::Vertical,
      gap: 0.,
      padding: Sides::all(0.),
      align: Alignment2d::default(),
      background_frame: Box::<RectFrame>::default(),
      wrap: WrapBehavior::Allow,
      children: ElementList(Vec::new()),
    }
  }
}

impl Container {
  pub fn measure_max_inner_size(&self, layout: &LayoutInfo) -> Vec2 {
    // let outer_size_x = match self.size.width {
    //   Size::Auto => layout.max_size.x,
    //   Size::Relative(p) => layout.max_size.x * p,
    //   Size::Absolute(p) => p,
    //   Size::Remaining(p) => match layout.direction {
    //     Direction::Horizontal => layout.remaining_space.unwrap_or(layout.max_size.x) * p,
    //     Direction::Vertical => layout.max_size.x,
    //   }
    // };
    // let outer_size_y = match self.size.height {
    //   Size::Auto => layout.max_size.y,
    //   Size::Relative(p) => layout.max_size.y * p,
    //   Size::Absolute(p) => p,
    // };
    let outer_size = compute_size(layout, self.size, layout.max_size);
    vec2(
      outer_size.x - (self.padding.left + self.padding.right),
      outer_size.y - (self.padding.top + self.padding.bottom),
    )
  }
}

impl UiElement for Container {
  fn name(&self) -> &'static str {
    "container"
  }

  fn size(&self) -> Option<Size2d> {
    Some(self.size)
  }

  fn measure(&self, ctx: MeasureContext) -> Response {
    // XXX: If both axes are NOT set to auto, we should be able quickly return the size
    // ... but we can't, because we need to measure the children to get the inner_content_size and user_data values
    // this is a potential optimization opportunity, maybe we could postpone this to the process call
    // as it's guaranteed to be called only ONCE, while measure is assumed to be cheap and called multiple times
    // ... we could also implement some sort of "global" caching for the measure call (to prevent traversal of the same tree multiple times),
    // but that's a bit more complex and probably impossible with the current design of the measure/process calls

    // In case wrapping is enabled, elements cannot exceed this size on the primary axis
    let max_line_pri = match self.direction {
      Direction::Horizontal => match self.size.width {
        Size::Auto => ctx.layout.max_size.x,
        Size::Relative(p) => ctx.layout.max_size.x * p,
        Size::Absolute(p) => p,
        Size::Remaining(p) => ctx.layout.remaining_space.unwrap_or(ctx.layout.max_size.x) * p,
      },
      Direction::Vertical => match self.size.height {
        Size::Auto => ctx.layout.max_size.y,
        Size::Relative(p) => ctx.layout.max_size.y * p,
        Size::Absolute(p) => p,
        Size::Remaining(p) => ctx.layout.remaining_space.unwrap_or(ctx.layout.max_size.y) * p,
      }
    };

    //size of AABB containing all lines
    let mut total_size = Vec2::ZERO;

    //Size of the current row/column (if wrapping)
    let mut line_size = Vec2::ZERO;

    //Size of previous sec. axes combined
    //(basically, in case of the horizontal layout, this is the height of the tallest element in the line)
    //This is a vec2, but only one axis is used, depending on the layout direction
    let mut line_sec_offset: Vec2 = Vec2::ZERO;

    //Amount of elements in the current line
    let mut line_element_count = 0;

    //Leftover gap from the previous element on the primary axis
    let mut leftover_gap = Vec2::ZERO;

    //line metadata for the user_data
    let mut lines = vec![
      CudLine {
        start_idx: 0,
        content_size: Vec2::ZERO,
        remaining_space: 0.,
      }
    ];

    //set to true if in the current line there is an element with Remaining size (line will have to be wrapped)
    // let mut has_remaining = false;

    for (idx, element) in self.children.0.iter().enumerate() {
      if let Some(esize) = element.size() {
        let pri_size = match self.direction {
          Direction::Horizontal => esize.width,
          Direction::Vertical => esize.height,
        };
        if matches!(pri_size, Size::Remaining(_)) {
          //XXX: kinda a hack?
          continue;
        }
      }

      let measure = element.measure(MeasureContext{
        state: ctx.state,
        layout: &LayoutInfo {
          //XXX: if the element gets wrapped, this will be inaccurate.
          //But, we cant know the size of the line until we measure it, and also
          //We dont make any guarantees about this value being valid during the `measure` call
          //For all intents and purposes, this is just a *hint* for the element to use
          //(and could be just set to 0 for all we care)
          position: ctx.layout.position + line_size + line_sec_offset,
          //TODO: subtract size already taken by previous children
          max_size: self.measure_max_inner_size(ctx.layout),
          direction: self.direction,
          remaining_space: None,
        },
        text_measure: ctx.text_measure,
        current_font: ctx.current_font,
        images: ctx.images,
      });

      //Check the position of the side of element closest to the end on the primary axis
      let end_pos_pri = match self.direction {
        Direction::Horizontal => line_size.x + measure.size.x + self.padding.left + self.padding.right,
        Direction::Vertical => line_size.y + measure.size.y + self.padding.top + self.padding.bottom,
      };

      //Wrap the element if it exceeds container's size and is not the first element in the line
      let should_wrap_overflow = self.wrap.is_enabled() && (end_pos_pri > max_line_pri);
      if self.wrap.is_allowed() && line_element_count > 0 && (measure.should_wrap || should_wrap_overflow) {
        // >>>>>>> WRAP THAT B*TCH!

        //Negate the leftover gap from the previous element
        line_size -= leftover_gap;

        //update the previous line metadata
        {
          let last_line = lines.last_mut().unwrap();
          last_line.content_size = line_size;
          //HACK: why? - self.gap, may be different for the last element or if it's the only element in the line
          let will_produce_gap = if line_element_count > 1 { self.gap } else { 0. };
          last_line.remaining_space = max_line_pri - will_produce_gap - match self.direction {
            Direction::Horizontal => line_size.x + self.padding.left + self.padding.right,
            Direction::Vertical => line_size.y + self.padding.top + self.padding.bottom,
          };
        }

        //push the line metadata
        lines.push(CudLine {
          start_idx: idx,
          content_size: Vec2::ZERO,
          remaining_space: 0.,
        });

        //Update the total size accordingly
        match self.direction {
          Direction::Horizontal => {
            total_size.x = total_size.x.max(line_size.x);
            total_size.y += line_size.y + self.gap;
          },
          Direction::Vertical => {
            total_size.x += line_size.x + self.gap;
            total_size.y = total_size.y.max(line_size.y);
          }
        }

        //Now, update line_sec_offset
        match self.direction {
          Direction::Horizontal => {
            line_sec_offset.y += measure.size.y + self.gap;
          },
          Direction::Vertical => {
            line_sec_offset.x += measure.size.x + self.gap;
          }
        };

        //Reset the line size and element count
        line_size = Vec2::ZERO;
        line_element_count = 0;
      }

      //Increment element count
      line_element_count += 1;

      //Sset the leftover gap in case this is the last element in the line
      match self.direction {
        Direction::Horizontal => {
          line_size.x += measure.size.x + self.gap;
          line_size.y = line_size.y.max(measure.size.y);
          leftover_gap = vec2(self.gap, 0.);
        },
        Direction::Vertical => {
          line_size.x = line_size.x.max(measure.size.x);
          line_size.y += measure.size.y + self.gap;
          leftover_gap = vec2(0., self.gap);
        }
      }
    }

    line_size -= leftover_gap;

    //Update the content size of the last line
    {
      //HACK: why? - self.gap, may be different for the last element or if it's the only element in the line
      let cur_line = lines.last_mut().unwrap();
      cur_line.content_size = line_size;
      let will_produce_gap = if line_element_count > 1 { self.gap } else { 0. };
      cur_line.remaining_space = max_line_pri - will_produce_gap - match self.direction {
        Direction::Horizontal => line_size.x + self.padding.left + self.padding.right,
        Direction::Vertical => line_size.y + self.padding.top + self.padding.bottom,
      };
    }

    //Update the total size according to the size of the last line
    match self.direction {
      Direction::Horizontal => {
        total_size.x = total_size.x.max(line_size.x);
        total_size.y += line_size.y;
      },
      Direction::Vertical => {
        total_size.x += line_size.x;
        total_size.y = total_size.y.max(line_size.y);
      }
    }

    //Now, total_size should hold the size of the AABB containing all lines
    //This is exactly what inner_content_size hint should be set to
    let inner_content_size = Some(total_size);

    //After setting the inner_content_size, we can calculate the size of the container
    //Including padding, and in case the size is set to non-auto, override the size

    total_size += vec2(
      self.padding.left + self.padding.right,
      self.padding.top + self.padding.bottom,
    );

    let computed_size = compute_size(ctx.layout, self.size, total_size);
    match self.size.width {
      Size::Auto => (),
      _ => total_size.x = computed_size.x,
    }
    match self.size.height {
      Size::Auto => (),
      _ => total_size.y = computed_size.y,
    }

    // match self.size.width {
    //   Size::Auto => (),
    //   Size::Relative(percentage) => total_size.x = ctx.layout.max_size.x * percentage,
    //   Size::Absolute(pixels) => total_size.x = pixels,
    // }
    // match self.size.height {
    //   Size::Auto => (),
    //   Size::Relative(percentage) => total_size.y = ctx.layout.max_size.y * percentage,
    //   Size::Absolute(pixels) => total_size.y = pixels,
    // }

    Response {
      size: total_size,
      hints: Hints {
        inner_content_size,
        ..Default::default()
      },
      user_data: Some(Box::new(ContainerUserData { lines })),
      ..Default::default()
    }
  }

  fn process(&self, ctx: ProcessContext) {
    let user_data: &ContainerUserData = ctx.measure.user_data
      .as_ref().expect("no user data attached to container")
      .downcast_ref().expect("invalid user data type");

    let mut position = ctx.layout.position;

    //background
    // if !self.background.is_transparent() {
    //   let corner_colors = self.background.corners();
    //   ctx.draw.add(UiDrawCommand::Rectangle {
    //     position,
    //     size: ctx.measure.size,
    //     color: corner_colors,
    //     texture: self.background_image,
    //     rounded_corners: (self.corner_radius.max_f32() > 0.).then_some({
    //       RoundedCorners::from_radius(self.corner_radius)
    //     }),
    //   });
    // }

    self.background_frame.draw(ctx.draw, (ctx.layout.position, ctx.measure.size).into());

    //padding
    position += vec2(self.padding.left, self.padding.top);

    //convert alignment to pri/sec axis based
    //.0 = primary, .1 = secondary
    let pri_sec_align = match self.direction {
      Direction::Horizontal => (self.align.horizontal, self.align.vertical),
      Direction::Vertical => (self.align.vertical, self.align.horizontal),
    };

    //alignment (on sec. axis)
    // match pri_sec_align.1 {
    //   Alignment::Begin => (),
    //   Alignment::Center => {
    //     position += match self.direction {
    //       UiDirection::Horizontal => vec2(0., (ctx.measure.size.y - self.padding.top - self.padding.bottom - user_data.lines.last().unwrap().content_size.y) / 2.),
    //       UiDirection::Vertical => vec2((ctx.measure.size.x - self.padding.left - self.padding.right - user_data.lines.last().unwrap().content_size.x) / 2., 0.),
    //     };
    //   },
    //   Alignment::End => {
    //     position += match self.direction {
    //       UiDirection::Horizontal => vec2(0., ctx.measure.size.y - user_data.lines.last().unwrap().content_size.y - self.padding.bottom - self.padding.top),
    //       UiDirection::Vertical => vec2(ctx.measure.size.x - user_data.lines.last().unwrap().content_size.x - self.padding.right - self.padding.left, 0.),
    //     };
    //   }
    // }

    for (line_idx, cur_line) in user_data.lines.iter().enumerate() {
      let mut local_position = position;

      //alignment on primary axis
      match (pri_sec_align.0, self.direction) {
        (Alignment::Begin, _) => (),
        (Alignment::Center, Direction::Horizontal) => {
          local_position.x += (ctx.measure.size.x - cur_line.content_size.x) / 2. - self.padding.left;
        },
        (Alignment::Center, Direction::Vertical) => {
          local_position.y += (ctx.measure.size.y - cur_line.content_size.y) / 2. - self.padding.top;
        },
        (Alignment::End, Direction::Horizontal) => {
          local_position.x += ctx.measure.size.x - cur_line.content_size.x - self.padding.right - self.padding.left;
        },
        (Alignment::End, Direction::Vertical) => {
          local_position.y += ctx.measure.size.y - cur_line.content_size.y - self.padding.bottom - self.padding.top;
        }
      }

      let next_line_begin = user_data.lines
        .get(line_idx + 1)
        .map(|l| l.start_idx)
        .unwrap_or(self.children.0.len());

      for element_idx in cur_line.start_idx..next_line_begin {
        let element = &self.children.0[element_idx];

        //(passing max size from layout rather than actual known bounds for the sake of consistency with measure() above)
        //... as this must match!

        let mut el_layout = LayoutInfo {
          position: local_position,
          max_size: self.measure_max_inner_size(ctx.layout),
          direction: self.direction,
          remaining_space: Some(cur_line.remaining_space),
        };

        //measure
        let el_measure = element.measure(MeasureContext {
          layout: &el_layout,
          state: ctx.state,
          text_measure: ctx.text_measure,
          current_font: ctx.current_font,
          images: ctx.images,
        });

        //align (on sec. axis)
        //TODO separate align withing the line and align of the whole line
        let inner_content_size = ctx.measure.hints.inner_content_size.unwrap();
        match (pri_sec_align.1, self.direction) {
          (Alignment::Begin, _) => (),
          (Alignment::Center, Direction::Horizontal) => {
            //Align whole row
            el_layout.position.y += ((ctx.measure.size.y - self.padding.bottom - self.padding.top) - inner_content_size.y) / 2.;
            //Align within row
            el_layout.position.y += (cur_line.content_size.y - el_measure.size.y) / 2.;
          },
          (Alignment::Center, Direction::Vertical) => {
            //Align whole row
            el_layout.position.x += ((ctx.measure.size.x - self.padding.left - self.padding.right) - inner_content_size.x) / 2.;
            //Align within row
            el_layout.position.x += (cur_line.content_size.x - el_measure.size.x) / 2.;
          },
          //TODO update these two cases:
          (Alignment::End, Direction::Horizontal) => {
            //Align whole row
            el_layout.position.y += (ctx.measure.size.y - self.padding.bottom - self.padding.top) - inner_content_size.y;
            //Align within row
            el_layout.position.y += cur_line.content_size.y - el_measure.size.y;
          },
          (Alignment::End, Direction::Vertical) => {
            //Align whole row
            el_layout.position.x += (ctx.measure.size.x - self.padding.right - self.padding.left) - inner_content_size.x;
            //Align within row
            el_layout.position.x += cur_line.content_size.x - el_measure.size.x;
          }
        }

        //process
        element.process(ProcessContext {
          measure: &el_measure,
          layout: &el_layout,
          draw: ctx.draw,
          state: ctx.state,
          text_measure: ctx.text_measure,
          current_font: ctx.current_font,
          images: ctx.images,
          input: ctx.input,
          signal: ctx.signal,
        });

        //layout
        match self.direction {
          Direction::Horizontal => {
            local_position.x += el_measure.size.x + self.gap;
          },
          Direction::Vertical => {
            local_position.y += el_measure.size.y + self.gap;
          }
        }
      }

      //Move to the next line
      match self.direction {
        Direction::Horizontal => {
          position.y += cur_line.content_size.y + self.gap;
          //position.x -= cur_line.content_size.x;
          // leftover_line_gap = vec2(0., self.gap);
        }
        Direction::Vertical => {
          position.x += cur_line.content_size.x + self.gap;
          //position.y -= cur_line.content_size.y;
          // leftover_line_gap = vec2(self.gap, 0.);
        }
      };
    }
  }
}