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
use bitflags::bitflags;

/// The layout type determines how nodes will be positioned when directed by the parent
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LayoutType {
    /// Stack child elements horizontally
    Row,
    /// Stack child elements vertically
    Column,
    /// Position child elements into specified rows and columns
    Grid,
}

impl Default for LayoutType {
    fn default() -> Self {
        LayoutType::Column
    }
}

/// The position type determines whether a node will be positioned in-line with its siblings or seperate
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PositionType {
    /// Node is positioned relative to parent but ignores its siblings
    SelfDirected,
    /// Node is positioned relative to parent and in-line with siblings
    ParentDirected,
}

impl Default for PositionType {
    fn default() -> Self {
        PositionType::ParentDirected
    }
}

/// Units which describe spacing and size
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Units {
    Pixels(f32),
    Percentage(f32),
    Stretch(f32),
    Auto,
}

impl Default for Units {
    fn default() -> Self {
        Units::Auto
    }
}

impl Units {
    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,
        }
    }
}

bitflags! {
    #[derive(Default)]
    pub struct GeometryChanged: u8 {
        const CHANGE_POSX    = 0b00000001;
        const CHANGE_POSY    = 0b00000010;
        const CHANGE_WIDTH   = 0b00000100;
        const CHANGE_HEIGHT  = 0b00001000;
        const POSX_CHANGED   = 0b00010000;
        const POSY_CHANGED   = 0b00100000;
        const WIDTH_CHANGED  = 0b01000000;
        const HEIGHT_CHANGED = 0b10000000; 
    }     
}



// 

#[derive(Debug, Clone, Copy)]
pub struct Value {
    min: f32,
    val: f32,
    max: f32,
}

const MIN: f32 = -std::f32::MAX;
const MAX: f32 = std::f32::MAX;


#[derive(Debug, Clone, Copy)]
pub enum Units2 {
    Pixels(Value),
    Percentage(Value),
    Stretch(Value),
    Auto,
}

impl Units2 {
    pub fn pixels(val: f32) -> Self {
        Self::Pixels(Value {min: MIN, val, max: MAX})
    }

    pub fn percentage(val: f32) -> Self {
        Self::Pixels(Value {min: MIN, val, max: MAX})
    }

    pub fn stretch(val: f32) -> Self {
        Self::Pixels(Value {min: MIN, val, max: MAX})
    }

    pub fn auto() -> Self {
        Self::Auto
    }

    pub fn min(self, min: f32) -> Self {
        match self {
            Units2::Pixels(px) => {
                assert!(min < px.max, "min must be less than max");
                Units2::Pixels(Value {
                    min,
                    val: px.val,
                    max: px.max,
                })
            }

            Units2::Percentage(pc) => {
                assert!(min < pc.max, "min must be less than max");
                Units2::Percentage(Value {
                    min,
                    val: pc.val,
                    max: pc.max,
                })
            }

            Units2::Stretch(s) => {
                assert!(min < s.max, "min must be less than max");
                Units2::Stretch(Value {
                    min,
                    val: s.val,
                    max: s.max,
                })
            }

            Units2::Auto => {
                Units2::Auto
            }
        }
    }

    pub fn max(self, max: f32) -> Self {
        match self {
            Units2::Pixels(px) => {
                assert!(max > px.min, "max must be greater than min");
                Units2::Pixels(Value {
                    min: px.min,
                    val: px.val,
                    max,
                })
            }

            Units2::Percentage(pc) => {
                assert!(max > pc.min, "max must be greater than min");
                Units2::Percentage(Value {
                    min: pc.min,
                    val: pc.val,
                    max,
                })
            }

            Units2::Stretch(s) => {
                assert!(max > s.min, "max must be greater than min");
                Units2::Stretch(Value {
                    min: s.min,
                    val: s.val,
                    max,
                })
            }

            Units2::Auto => {
                Units2::Auto
            }
        }
    }

    pub fn clamp(&mut self) {
        match self {
            Units2::Pixels(px) => {
                px.val = px.val.clamp(px.min, px.max);
            }

            Units2::Percentage(pc) => {
                pc.val = pc.val.clamp(pc.min, pc.max);
            }

            Units2::Stretch(s) => {
                s.val = s.val.clamp(s.min, s.max);
            }

            _=> {}
        }
    }
}