astrelis-ui 0.2.2

UI Framework designed for Astrelis Game Engine
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//! Type-safe length and dimension types for UI styling.
//!
//! Provides enum-based alternatives to raw floats for better type safety
//! and clearer intent in style declarations.

use astrelis_core::math::Vec2;
use std::fmt;

/// Length value for UI dimensions.
///
/// Represents different ways to specify sizes in the UI system.
///
/// # Examples
/// ```
/// use astrelis_ui::Length;
///
/// let fixed = Length::Px(100.0);
/// let relative = Length::Percent(50.0);
/// let auto_size = Length::Auto;
/// let viewport_width = Length::Vw(80.0); // 80% of viewport width
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Length {
    /// Fixed pixel value
    Px(f32),
    /// Percentage of parent size (0.0 - 100.0)
    Percent(f32),
    /// Automatic sizing based on content
    Auto,
    /// Percentage of viewport width (0.0 - 100.0)
    Vw(f32),
    /// Percentage of viewport height (0.0 - 100.0)
    Vh(f32),
    /// Percentage of smaller viewport dimension (0.0 - 100.0)
    Vmin(f32),
    /// Percentage of larger viewport dimension (0.0 - 100.0)
    Vmax(f32),
}

impl Length {
    /// Create a pixel length.
    pub fn px(value: f32) -> Self {
        Self::Px(value)
    }

    /// Create a percentage length.
    pub fn percent(value: f32) -> Self {
        Self::Percent(value)
    }

    /// Create an auto length.
    pub fn auto() -> Self {
        Self::Auto
    }

    /// Create a viewport width length (1vw = 1% of viewport width).
    pub fn vw(value: f32) -> Self {
        Self::Vw(value)
    }

    /// Create a viewport height length (1vh = 1% of viewport height).
    pub fn vh(value: f32) -> Self {
        Self::Vh(value)
    }

    /// Create a viewport minimum length (1vmin = 1% of smaller viewport dimension).
    pub fn vmin(value: f32) -> Self {
        Self::Vmin(value)
    }

    /// Create a viewport maximum length (1vmax = 1% of larger viewport dimension).
    pub fn vmax(value: f32) -> Self {
        Self::Vmax(value)
    }

    /// Resolve viewport-relative units to pixels.
    ///
    /// Converts vw/vh/vmin/vmax units to absolute pixel values based on the viewport size.
    /// Other unit types (Px, Percent, Auto) are returned unchanged.
    ///
    /// # Arguments
    /// * `viewport_size` - The viewport dimensions (width, height) in pixels
    ///
    /// # Examples
    /// ```
    /// use astrelis_ui::Length;
    /// use astrelis_core::math::Vec2;
    ///
    /// let viewport = Vec2::new(1280.0, 720.0);
    ///
    /// let width = Length::Vw(50.0).resolve(viewport);
    /// assert_eq!(width, Length::Px(640.0)); // 50% of 1280px
    ///
    /// let height = Length::Vh(100.0).resolve(viewport);
    /// assert_eq!(height, Length::Px(720.0)); // 100% of 720px
    /// ```
    pub fn resolve(self, viewport_size: Vec2) -> Self {
        match self {
            Length::Vw(v) => Length::Px(v * viewport_size.x / 100.0),
            Length::Vh(v) => Length::Px(v * viewport_size.y / 100.0),
            Length::Vmin(v) => {
                let min = viewport_size.x.min(viewport_size.y);
                Length::Px(v * min / 100.0)
            }
            Length::Vmax(v) => {
                let max = viewport_size.x.max(viewport_size.y);
                Length::Px(v * max / 100.0)
            }
            other => other, // Px/Percent/Auto unchanged
        }
    }

    /// Convert to Taffy Dimension.
    ///
    /// # Panics
    /// Panics if called on viewport-relative units (Vw/Vh/Vmin/Vmax).
    /// These must be resolved to pixels first using [`Length::resolve`].
    pub fn to_dimension(self) -> taffy::Dimension {
        match self {
            Length::Px(v) => taffy::Dimension::Length(v),
            Length::Percent(v) => taffy::Dimension::Percent(v / 100.0),
            Length::Auto => taffy::Dimension::Auto,
            Length::Vw(_) | Length::Vh(_) | Length::Vmin(_) | Length::Vmax(_) => {
                panic!(
                    "Viewport-relative units must be resolved to pixels before converting to Taffy dimension. \
                     Call .resolve(viewport_size) first."
                );
            }
        }
    }

    /// Convert from Taffy Dimension.
    pub fn from_dimension(dim: taffy::Dimension) -> Self {
        match dim {
            taffy::Dimension::Length(v) => Length::Px(v),
            taffy::Dimension::Percent(v) => Length::Percent(v * 100.0),
            taffy::Dimension::Auto => Length::Auto,
        }
    }

    /// Check if this is a fixed pixel value.
    pub fn is_px(&self) -> bool {
        matches!(self, Length::Px(_))
    }

    /// Check if this is a percentage value.
    pub fn is_percent(&self) -> bool {
        matches!(self, Length::Percent(_))
    }

    /// Check if this is auto.
    pub fn is_auto(&self) -> bool {
        matches!(self, Length::Auto)
    }

    /// Check if this is a viewport-relative unit.
    pub fn is_viewport(&self) -> bool {
        matches!(
            self,
            Length::Vw(_) | Length::Vh(_) | Length::Vmin(_) | Length::Vmax(_)
        )
    }
}

impl From<f32> for Length {
    fn from(value: f32) -> Self {
        Length::Px(value)
    }
}

impl From<Length> for taffy::Dimension {
    fn from(length: Length) -> Self {
        length.to_dimension()
    }
}

impl fmt::Display for Length {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Length::Px(v) => write!(f, "{}px", v),
            Length::Percent(v) => write!(f, "{}%", v),
            Length::Auto => write!(f, "auto"),
            Length::Vw(v) => write!(f, "{}vw", v),
            Length::Vh(v) => write!(f, "{}vh", v),
            Length::Vmin(v) => write!(f, "{}vmin", v),
            Length::Vmax(v) => write!(f, "{}vmax", v),
        }
    }
}

/// Length value that can also be Auto (for margins/insets).
///
/// Similar to Length but allows automatic positioning.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LengthAuto {
    /// Fixed pixel value
    Px(f32),
    /// Percentage of parent size (0.0 - 100.0)
    Percent(f32),
    /// Automatic positioning
    Auto,
    /// Percentage of viewport width (0.0 - 100.0)
    Vw(f32),
    /// Percentage of viewport height (0.0 - 100.0)
    Vh(f32),
    /// Percentage of smaller viewport dimension (0.0 - 100.0)
    Vmin(f32),
    /// Percentage of larger viewport dimension (0.0 - 100.0)
    Vmax(f32),
}

impl LengthAuto {
    /// Create a pixel length.
    pub fn px(value: f32) -> Self {
        Self::Px(value)
    }

    /// Create a percentage length.
    pub fn percent(value: f32) -> Self {
        Self::Percent(value)
    }

    /// Create an auto length.
    pub fn auto() -> Self {
        Self::Auto
    }

    /// Create a viewport width length (1vw = 1% of viewport width).
    pub fn vw(value: f32) -> Self {
        Self::Vw(value)
    }

    /// Create a viewport height length (1vh = 1% of viewport height).
    pub fn vh(value: f32) -> Self {
        Self::Vh(value)
    }

    /// Create a viewport minimum length (1vmin = 1% of smaller viewport dimension).
    pub fn vmin(value: f32) -> Self {
        Self::Vmin(value)
    }

    /// Create a viewport maximum length (1vmax = 1% of larger viewport dimension).
    pub fn vmax(value: f32) -> Self {
        Self::Vmax(value)
    }

    /// Resolve viewport-relative units to pixels.
    pub fn resolve(self, viewport_size: Vec2) -> Self {
        match self {
            LengthAuto::Vw(v) => LengthAuto::Px(v * viewport_size.x / 100.0),
            LengthAuto::Vh(v) => LengthAuto::Px(v * viewport_size.y / 100.0),
            LengthAuto::Vmin(v) => {
                let min = viewport_size.x.min(viewport_size.y);
                LengthAuto::Px(v * min / 100.0)
            }
            LengthAuto::Vmax(v) => {
                let max = viewport_size.x.max(viewport_size.y);
                LengthAuto::Px(v * max / 100.0)
            }
            other => other,
        }
    }

    /// Convert to Taffy LengthPercentageAuto.
    ///
    /// # Panics
    /// Panics if called on viewport-relative units (Vw/Vh/Vmin/Vmax).
    /// These must be resolved to pixels first using [`LengthAuto::resolve`].
    pub fn to_length_percentage_auto(self) -> taffy::LengthPercentageAuto {
        match self {
            LengthAuto::Px(v) => taffy::LengthPercentageAuto::Length(v),
            LengthAuto::Percent(v) => taffy::LengthPercentageAuto::Percent(v / 100.0),
            LengthAuto::Auto => taffy::LengthPercentageAuto::Auto,
            LengthAuto::Vw(_) | LengthAuto::Vh(_) | LengthAuto::Vmin(_) | LengthAuto::Vmax(_) => {
                panic!(
                    "Viewport-relative units must be resolved to pixels before converting to Taffy dimension. \
                     Call .resolve(viewport_size) first."
                );
            }
        }
    }

    /// Convert from Taffy LengthPercentageAuto.
    pub fn from_length_percentage_auto(lpa: taffy::LengthPercentageAuto) -> Self {
        match lpa {
            taffy::LengthPercentageAuto::Length(v) => LengthAuto::Px(v),
            taffy::LengthPercentageAuto::Percent(v) => LengthAuto::Percent(v * 100.0),
            taffy::LengthPercentageAuto::Auto => LengthAuto::Auto,
        }
    }
}

impl From<f32> for LengthAuto {
    fn from(value: f32) -> Self {
        LengthAuto::Px(value)
    }
}

impl From<Length> for LengthAuto {
    fn from(length: Length) -> Self {
        match length {
            Length::Px(v) => LengthAuto::Px(v),
            Length::Percent(v) => LengthAuto::Percent(v),
            Length::Auto => LengthAuto::Auto,
            Length::Vw(v) => LengthAuto::Vw(v),
            Length::Vh(v) => LengthAuto::Vh(v),
            Length::Vmin(v) => LengthAuto::Vmin(v),
            Length::Vmax(v) => LengthAuto::Vmax(v),
        }
    }
}

impl From<LengthAuto> for taffy::LengthPercentageAuto {
    fn from(length: LengthAuto) -> Self {
        length.to_length_percentage_auto()
    }
}

impl fmt::Display for LengthAuto {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            LengthAuto::Px(v) => write!(f, "{}px", v),
            LengthAuto::Percent(v) => write!(f, "{}%", v),
            LengthAuto::Auto => write!(f, "auto"),
            LengthAuto::Vw(v) => write!(f, "{}vw", v),
            LengthAuto::Vh(v) => write!(f, "{}vh", v),
            LengthAuto::Vmin(v) => write!(f, "{}vmin", v),
            LengthAuto::Vmax(v) => write!(f, "{}vmax", v),
        }
    }
}

/// Length value without Auto (for padding/border).
///
/// Similar to Length but doesn't allow Auto sizing.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LengthPercentage {
    /// Fixed pixel value
    Px(f32),
    /// Percentage of parent size (0.0 - 100.0)
    Percent(f32),
    /// Percentage of viewport width (0.0 - 100.0)
    Vw(f32),
    /// Percentage of viewport height (0.0 - 100.0)
    Vh(f32),
    /// Percentage of smaller viewport dimension (0.0 - 100.0)
    Vmin(f32),
    /// Percentage of larger viewport dimension (0.0 - 100.0)
    Vmax(f32),
}

impl LengthPercentage {
    /// Create a pixel length.
    pub fn px(value: f32) -> Self {
        Self::Px(value)
    }

    /// Create a percentage length.
    pub fn percent(value: f32) -> Self {
        Self::Percent(value)
    }

    /// Create a viewport width length (1vw = 1% of viewport width).
    pub fn vw(value: f32) -> Self {
        Self::Vw(value)
    }

    /// Create a viewport height length (1vh = 1% of viewport height).
    pub fn vh(value: f32) -> Self {
        Self::Vh(value)
    }

    /// Create a viewport minimum length (1vmin = 1% of smaller viewport dimension).
    pub fn vmin(value: f32) -> Self {
        Self::Vmin(value)
    }

    /// Create a viewport maximum length (1vmax = 1% of larger viewport dimension).
    pub fn vmax(value: f32) -> Self {
        Self::Vmax(value)
    }

    /// Resolve viewport-relative units to pixels.
    pub fn resolve(self, viewport_size: Vec2) -> Self {
        match self {
            LengthPercentage::Vw(v) => LengthPercentage::Px(v * viewport_size.x / 100.0),
            LengthPercentage::Vh(v) => LengthPercentage::Px(v * viewport_size.y / 100.0),
            LengthPercentage::Vmin(v) => {
                let min = viewport_size.x.min(viewport_size.y);
                LengthPercentage::Px(v * min / 100.0)
            }
            LengthPercentage::Vmax(v) => {
                let max = viewport_size.x.max(viewport_size.y);
                LengthPercentage::Px(v * max / 100.0)
            }
            other => other,
        }
    }

    /// Convert to Taffy LengthPercentage.
    ///
    /// # Panics
    /// Panics if called on viewport-relative units (Vw/Vh/Vmin/Vmax).
    /// These must be resolved to pixels first using [`LengthPercentage::resolve`].
    pub fn to_length_percentage(self) -> taffy::LengthPercentage {
        match self {
            LengthPercentage::Px(v) => taffy::LengthPercentage::Length(v),
            LengthPercentage::Percent(v) => taffy::LengthPercentage::Percent(v / 100.0),
            LengthPercentage::Vw(_)
            | LengthPercentage::Vh(_)
            | LengthPercentage::Vmin(_)
            | LengthPercentage::Vmax(_) => {
                panic!(
                    "Viewport-relative units must be resolved to pixels before converting to Taffy dimension. \
                     Call .resolve(viewport_size) first."
                );
            }
        }
    }

    /// Convert from Taffy LengthPercentage.
    pub fn from_length_percentage(lp: taffy::LengthPercentage) -> Self {
        match lp {
            taffy::LengthPercentage::Length(v) => LengthPercentage::Px(v),
            taffy::LengthPercentage::Percent(v) => LengthPercentage::Percent(v * 100.0),
        }
    }
}

impl From<f32> for LengthPercentage {
    fn from(value: f32) -> Self {
        LengthPercentage::Px(value)
    }
}

impl From<LengthPercentage> for taffy::LengthPercentage {
    fn from(length: LengthPercentage) -> Self {
        length.to_length_percentage()
    }
}

impl fmt::Display for LengthPercentage {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            LengthPercentage::Px(v) => write!(f, "{}px", v),
            LengthPercentage::Percent(v) => write!(f, "{}%", v),
            LengthPercentage::Vw(v) => write!(f, "{}vw", v),
            LengthPercentage::Vh(v) => write!(f, "{}vh", v),
            LengthPercentage::Vmin(v) => write!(f, "{}vmin", v),
            LengthPercentage::Vmax(v) => write!(f, "{}vmax", v),
        }
    }
}

/// Helper function to create a Taffy length dimension.
pub fn length(value: f32) -> taffy::Dimension {
    taffy::Dimension::Length(value)
}

/// Helper function to create a Taffy percent dimension.
pub fn percent(value: f32) -> taffy::Dimension {
    taffy::Dimension::Percent(value / 100.0)
}

/// Helper function to create auto dimension.
pub fn auto() -> taffy::Dimension {
    taffy::Dimension::Auto
}

/// Helper function to create a viewport width length.
///
/// # Examples
/// ```
/// use astrelis_ui::vw;
///
/// let width = vw(80.0); // 80% of viewport width
/// ```
pub fn vw(value: f32) -> Length {
    Length::Vw(value)
}

/// Helper function to create a viewport height length.
///
/// # Examples
/// ```
/// use astrelis_ui::vh;
///
/// let height = vh(100.0); // 100% of viewport height
/// ```
pub fn vh(value: f32) -> Length {
    Length::Vh(value)
}

/// Helper function to create a viewport minimum length.
///
/// # Examples
/// ```
/// use astrelis_ui::vmin;
///
/// let size = vmin(50.0); // 50% of smaller viewport dimension
/// ```
pub fn vmin(value: f32) -> Length {
    Length::Vmin(value)
}

/// Helper function to create a viewport maximum length.
///
/// # Examples
/// ```
/// use astrelis_ui::vmax;
///
/// let size = vmax(50.0); // 50% of larger viewport dimension
/// ```
pub fn vmax(value: f32) -> Length {
    Length::Vmax(value)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_length_px() {
        let len = Length::Px(100.0);
        assert!(len.is_px());
        assert!(!len.is_percent());
        assert!(!len.is_auto());
        assert_eq!(len.to_string(), "100px");
    }

    #[test]
    fn test_length_percent() {
        let len = Length::Percent(50.0);
        assert!(!len.is_px());
        assert!(len.is_percent());
        assert!(!len.is_auto());
        assert_eq!(len.to_string(), "50%");
    }

    #[test]
    fn test_length_auto() {
        let len = Length::Auto;
        assert!(!len.is_px());
        assert!(!len.is_percent());
        assert!(len.is_auto());
        assert_eq!(len.to_string(), "auto");
    }

    #[test]
    fn test_length_from_f32() {
        let len: Length = 100.0.into();
        assert_eq!(len, Length::Px(100.0));
    }

    #[test]
    fn test_length_to_dimension() {
        let len = Length::Px(100.0);
        let dim = len.to_dimension();
        assert!(matches!(dim, taffy::Dimension::Length(100.0)));

        let len = Length::Percent(50.0);
        let dim = len.to_dimension();
        assert!(matches!(dim, taffy::Dimension::Percent(0.5)));

        let len = Length::Auto;
        let dim = len.to_dimension();
        assert!(matches!(dim, taffy::Dimension::Auto));
    }

    #[test]
    fn test_length_percentage() {
        let lp = LengthPercentage::Px(100.0);
        assert_eq!(lp.to_string(), "100px");

        let lp = LengthPercentage::Percent(50.0);
        assert_eq!(lp.to_string(), "50%");
    }

    #[test]
    fn test_length_auto_conversion() {
        let la = LengthAuto::Px(100.0);
        assert_eq!(la.to_string(), "100px");

        let len: LengthAuto = Length::Percent(50.0).into();
        assert_eq!(len, LengthAuto::Percent(50.0));
    }

    #[test]
    fn test_helper_functions() {
        let dim = length(100.0);
        assert!(matches!(dim, taffy::Dimension::Length(100.0)));

        let dim = percent(50.0);
        assert!(matches!(dim, taffy::Dimension::Percent(0.5)));

        let dim = auto();
        assert!(matches!(dim, taffy::Dimension::Auto));
    }
}