Newer
Older
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
use bevy::reflect::Reflect;
/// The layout type determines how nodes will be positioned when directed by the parent
#[derive(Default, Debug, Reflect, Clone, Copy, PartialEq)]
pub enum LayoutType {
/// Stack child elements horizontally
Row,
#[default]
/// Stack child elements vertically
Column,
/// Position child elements into specified rows and columns
Grid,
}
impl From<LayoutType> for morphorm::LayoutType {
fn from(val: LayoutType) -> Self {
match val {
LayoutType::Column => morphorm::LayoutType::Column,
LayoutType::Row => morphorm::LayoutType::Row,
LayoutType::Grid => morphorm::LayoutType::Grid,
}
}
}
/// The position type determines whether a node will be positioned in-line with its siblings or seperate
#[derive(Default, Debug, Reflect, Clone, Copy, PartialEq)]
pub enum KPositionType {
/// Node is positioned relative to parent but ignores its siblings
SelfDirected,
#[default]
/// Node is positioned relative to parent and in-line with siblings
ParentDirected,
}
impl From<KPositionType> for morphorm::PositionType {
fn from(val: KPositionType) -> Self {
match val {
KPositionType::ParentDirected => morphorm::PositionType::ParentDirected,
KPositionType::SelfDirected => morphorm::PositionType::SelfDirected,
}
}
}
/// Units which describe spacing and size
#[derive(Default, Debug, Reflect, Clone, Copy, PartialEq)]
pub enum Units {
/// A number of pixels
Pixels(f32),
/// A percentage of the parent dimension
Percentage(f32),
/// A factor of the remaining free space
Stretch(f32),
#[default]
/// Automatically determine the value
Auto,
}
impl From<Units> for morphorm::Units {
fn from(val: Units) -> Self {
match val {
Units::Pixels(value) => morphorm::Units::Pixels(value),
Units::Percentage(value) => morphorm::Units::Percentage(value),
Units::Stretch(value) => morphorm::Units::Stretch(value),
Units::Auto => morphorm::Units::Auto,
}
}
}
impl Units {
/// Converts the units to an f32 value
pub fn value_or(&self, parent_value: f32, auto: f32) -> f32 {
match self {
Units::Pixels(pixels) => *pixels,
Units::Percentage(percentage) => (percentage / 100.0) * parent_value,
Units::Stretch(_) => auto,
Units::Auto => auto,
}
}
/// Returns true if the value is in pixels
pub fn is_pixels(&self) -> bool {
matches!(self, Units::Pixels(_))
}
/// Returns true if the value is a percentage
pub fn is_percentage(&self) -> bool {
matches!(self, Units::Percentage(_))
}
/// Returns true if the value is a stretch factor
pub fn is_stretch(&self) -> bool {
matches!(self, Units::Stretch(_))
}
/// Returns true if the value is auto
pub fn is_auto(&self) -> bool {
matches!(self, Units::Auto)
}
}