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
use crate::cx::*;
use std::any::TypeId;

impl Cx{
    pub fn begin_style(&mut self, style_id:StyleId){
        // lets fetch the style, if it doesnt exist allocate it
        if let Some(index) = self.style_map.get(&style_id){
            self.style_stack.push(*index);
        }
        else{
            let index = self.styles.len();
            self.style_map.insert(style_id, index);
            self.styles.push(CxStyle::default());
            self.style_stack.push(index);
        }
    }
    
    pub fn end_style(&mut self){
        self.style_stack.pop().expect("end_style pop failed");
    }
    
    pub fn get_mut_style_top(&mut self)->&mut CxStyle{
        if let Some(last) = self.style_stack.last(){
            &mut self.styles[*last]
        }
        else{
            &mut self.style_base
        }
    }

}

// floats


#[derive(PartialEq, Copy, Clone, Hash, Eq)]
pub struct FloatId(pub TypeId);

impl FloatId {
    pub fn set(&self, cx: &mut Cx, value: f32) {
        cx.get_mut_style_top().floats.insert(*self, value);
    }
    
    pub fn get(&self, cx: &Cx) -> f32 {
        for style_id in &cx.style_stack{
            if let Some(value) = cx.styles[*style_id].floats.get(self){
                return *value
            }
        }
        *cx.style_base.floats.get(&*self).expect("Cannot find FloatId")
    }
}

impl Into<FloatId> for UniqueId {
    fn into(self) -> FloatId {FloatId(self.0)}
}



// Colors


#[derive(PartialEq, Copy, Clone, Hash, Eq)]
pub struct ColorId(pub TypeId);

impl ColorId {
    pub fn set(&self, cx: &mut Cx, value: Color) {
        cx.get_mut_style_top().colors.insert(*self, value);
    }
    
    pub fn get(&self, cx: &Cx) -> Color {
        for style_id in &cx.style_stack{
            if let Some(value) = cx.styles[*style_id].colors.get(self){
                return *value
            }
        }
        *cx.style_base.colors.get(&*self).expect("Cannot find ColorId")
    }
}

impl Into<ColorId> for UniqueId {
    fn into(self) -> ColorId {ColorId(self.0)}
}


// Text



#[derive(PartialEq, Copy, Clone, Hash, Eq)]
pub struct TextStyleId(pub TypeId);

impl TextStyleId {
    pub fn set(&self, cx: &mut Cx, value: TextStyle) {
        cx.get_mut_style_top().text_styles.insert(*self, value);
    }
    
    pub fn get(&self, cx: &Cx) -> TextStyle {
        for style_id in &cx.style_stack{
            if let Some(value) = cx.styles[*style_id].text_styles.get(self){
                return *value
            }
        }
        *cx.style_base.text_styles.get(&*self).expect("Cannot find TextStyleId")
    }
}

impl Into<TextStyleId> for UniqueId {
    fn into(self) -> TextStyleId {TextStyleId(self.0)}
}


// Layout

#[derive(PartialEq, Copy, Clone, Hash, Eq)]
pub struct LayoutId(pub TypeId);

impl LayoutId {
    pub fn set(&self, cx: &mut Cx, value: Layout) {
        cx.get_mut_style_top().layouts.insert(*self, value);
    }
    
    pub fn get(&self, cx: &Cx) -> Layout {
        for style_id in &cx.style_stack{
            if let Some(value) = cx.styles[*style_id].layouts.get(self){
                return *value
            }
        }
        *cx.style_base.layouts.get(&*self).expect("Cannot find LayoutId")
    }
}

impl Into<LayoutId> for UniqueId {
    fn into(self) -> LayoutId {LayoutId(self.0)}
}


// Walks


#[derive(PartialEq, Copy, Clone, Hash, Eq)]
pub struct WalkId(pub TypeId);

impl WalkId {
    pub fn set(&self, cx: &mut Cx, value: Walk) {
        cx.get_mut_style_top().walks.insert(*self, value);
    }
    
    pub fn get(&self, cx: &Cx) -> Walk {
        for style_id in &cx.style_stack{
            if let Some(value) = cx.styles[*style_id].walks.get(self){
                return *value
            }
        }
        *cx.style_base.walks.get(&*self).expect("Cannot find WalkId")
    }
}


impl Into<WalkId> for UniqueId {
    fn into(self) -> WalkId {WalkId(self.0)}
}



// Animations


#[derive(PartialEq, Copy, Clone, Hash, Eq)]
pub struct AnimId(pub TypeId);

impl AnimId {
    pub fn set(&self, cx: &mut Cx, value: Anim) {
        cx.get_mut_style_top().anims.insert(*self, value);
    }
    
    pub fn get(&self, cx: &Cx) -> Anim {
        for style_id in &cx.style_stack{
            if let Some(value) = cx.styles[*style_id].anims.get(self){
                return value.clone()
            }
        }
        cx.style_base.anims.get(&*self).expect("Cannot find AnimId").clone()
    }
}

impl Into<AnimId> for UniqueId {
    fn into(self) -> AnimId {AnimId(self.0)}
}


// Shaders

#[derive(PartialEq, Copy, Clone, Hash, Eq)]
pub struct ShaderId(pub TypeId);

impl ShaderId {
    pub fn set(&self, cx: &mut Cx, sg: ShaderGen) {
        let shader = cx.add_shader(sg, &format!("{:?}", self.0));
        cx.get_mut_style_top().shaders.insert(*self, shader);
    }
    
    pub fn get(&self, cx: &Cx) -> Shader {
        for style_id in &cx.style_stack{
            if let Some(value) = cx.styles[*style_id].shaders.get(self){
                return *value
            }
        }
        *cx.style_base.shaders.get(&*self).expect("Cannot find AnimId")
    }
}


impl Into<ShaderId> for UniqueId {
    fn into(self) -> ShaderId {ShaderId(self.0)}
}


// Classes


#[derive(PartialEq, Copy, Clone, Hash, Eq)]
pub struct StyleId(pub TypeId);

impl Into<StyleId> for UniqueId {
    fn into(self) -> StyleId {StyleId(self.0)}
}


impl StyleId {
    pub fn base() -> StyleId {uid!()}
}