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
use super::bounds::*;
use super::control::*;
use super::actions::*;
use super::font_attr::*;
use super::state_attr::*;
use super::popup_attr::*;
use super::scroll_attr::*;
use super::appearance_attr::*;

use super::super::property::*;
use super::super::binding_canvas::*;
use super::super::resource_manager::*;

use modifier::*;
 
///
/// Attribute attached to a control
///
#[derive(Clone, PartialEq, Debug)]
pub enum ControlAttribute {
    // TODO: layout attribute for bounding box, zindex and padding
    /// The bounding box for this control
    BoundingBox(Bounds),

    // The z-index of this item. Items with higher z-indexes are displayed over the top of those with lower z-indexes
    ZIndex(u32),

    // The padding to surround the subcomponents of this control with. Values are 'left top' and 'right bottom'.
    Padding((u32, u32), (u32, u32)),

    /// The text for this control
    Text(Property),

    /// Specifies the font properties of this control
    FontAttr(Font),

    /// Specifies the state of this control
    StateAttr(State),

    /// Specifies the popup state of this control (meaningless if this is not a popup control)
    PopupAttr(Popup),

    /// Specifies the appearance of this control
    AppearanceAttr(Appearance),

    /// Specifies how the contents of this control will scroll
    ScrollAttr(Scroll),

    /// The unique ID for this control
    Id(String),

    /// Subcomponents of this control
    SubComponents(Vec<Control>),

    /// Specifies the controller that manages the subcomponents of this control
    Controller(String),

    /// When the specified action occurs for this item, send the event 
    /// denoted by the string to the controller
    Action(ActionTrigger, String),

    // TODO: content attribute (maybe with text?). Image might be appearance though
    /// Specifies the canvas to use for this control (assuming it's a canvas control)
    Canvas(Resource<BindingCanvas>)
}

impl ControlAttribute {
    ///
    /// The bounding box represented by this attribute
    ///
    pub fn bounding_box<'a>(&'a self) -> Option<&'a Bounds> {
        match self {
            &BoundingBox(ref bounds)    => Some(bounds),
            _                           => None
        }
    }

    ///
    /// The Z-Index represented by this attribute
    /// 
    pub fn z_index(&self) -> Option<u32> {
        match self {
            &ZIndex(zindex) => Some(zindex),
            _               => None
        }
    }

    ///
    /// The padding represented by this attribute
    /// 
    pub fn padding(&self) -> Option<((u32, u32), (u32, u32))> {
        match self {
            &Padding(left_top, right_bottom)    => Some((left_top, right_bottom)),
            _                                   => None
        }
    }

    ///
    /// The text represented by this attribute
    ///
    pub fn text<'a>(&'a self) -> Option<&'a Property> {
        match self {
            &Text(ref text) => Some(text),
            _               => None
        }
    }

    ///
    /// The font atrributes represented by this attribute
    /// 
    pub fn font<'a>(&'a self) -> Option<&'a Font> {
        match self {
            &FontAttr(ref attr) => Some(attr),
            _                   => None
        }
    }

    ///
    /// The ID represented by this attribute
    ///
    pub fn id<'a>(&'a self) -> Option<&'a String> {
        match self {
            &Id(ref id) => Some(id),
            _           => None
        }
    }

    ///
    /// The subcomponent represented by this attribute
    ///
    pub fn subcomponents<'a>(&'a self) -> Option<&'a Vec<Control>> {
        match self {
            &SubComponents(ref components)  => Some(components),
            _                               => None
        }
    }

    ///
    /// The controller represented by this attribute
    ///
    pub fn controller<'a>(&'a self) -> Option<&'a str> {
        match self {
            &Controller(ref controller) => Some(controller),
            _                           => None
        }
    }

    ///
    /// The action represented by this attribute
    ///
    pub fn action<'a>(&'a self) -> Option<(&'a ActionTrigger, &'a String)> {
        match self {
            &Action(ref trigger, ref action)    => Some((trigger, action)),
            _                                   => None
        }
    }

    ///
    /// The control state represented by this attribute
    /// 
    pub fn state<'a>(&'a self) -> Option<&'a State> {
        match self {
            &StateAttr(ref state)   => Some(state),
            _                       => None
        }
    }

    ///
    /// The popup attribute represented by this attribute
    /// 
    pub fn popup<'a>(&'a self) -> Option<&'a Popup> {
        match self {
            &PopupAttr(ref popup)   => Some(popup),
            _                       => None
        }
    }

    ///
    /// The canvas resource for this control, if there is one
    ///
    pub fn canvas<'a>(&'a self) -> Option<&'a Resource<BindingCanvas>> {
        match self {
            &Canvas(ref canvas) => Some(canvas),
            _                   => None
        }
    }
    
    ///
    /// The appearance assigned by this attribute, if there is one
    /// 
    pub fn appearance<'a>(&'a self) -> Option<&'a Appearance> {
        match self {
            &AppearanceAttr(ref appearance) => Some(appearance),
            _                               => None
        }
    }
    
    ///
    /// The appearance assigned by this attribute, if there is one
    /// 
    pub fn scroll<'a>(&'a self) -> Option<&'a Scroll> {
        match self {
            &ScrollAttr(ref scroll) => Some(scroll),
            _                       => None
        }
    }
     
    ///
    /// Returns true if this attribute is different from another one
    /// (non-recursively, so this won't check subcomoponents)
    ///
    pub fn is_different_flat(&self, compare_to: &ControlAttribute) -> bool {
        match self {
            &BoundingBox(ref bounds)            => Some(bounds) != compare_to.bounding_box(),
            &ZIndex(zindex)                     => Some(zindex) != compare_to.z_index(),
            &Padding(lt, rb)                    => Some((lt, rb)) != compare_to.padding(),
            &Text(ref text)                     => Some(text) != compare_to.text(),
            &FontAttr(ref font)                 => Some(font) != compare_to.font(),
            &Id(ref id)                         => Some(id) != compare_to.id(),
            &Controller(ref controller)         => Some(controller.as_ref()) != compare_to.controller(),
            &Action(ref trigger, ref action)    => Some((trigger, action)) != compare_to.action(),
            &StateAttr(ref state)               => Some(state) != compare_to.state(),
            &PopupAttr(ref popup)               => Some(popup) != compare_to.popup(),
            &Canvas(ref canvas_resource)        => Some(canvas_resource) != compare_to.canvas(),
            &AppearanceAttr(ref appearance)     => Some(appearance) != compare_to.appearance(),
            &ScrollAttr(ref scroll)             => Some(scroll) != compare_to.scroll(),

            // For the subcomponents we only care about the number as we don't want to recurse
            &SubComponents(ref components)      => Some(components.len()) != compare_to.subcomponents().map(|components| components.len())
        }
    }
}

use ControlAttribute::*;

impl<'a> Modifier<Control> for &'a str {
    fn modify(self, control: &mut Control) {
        control.add_attribute(Text(self.to_property()))
    }
}

impl Modifier<Control> for String {
    fn modify(self, control: &mut Control) {
        control.add_attribute(Text(self.to_property()))
    }
}

impl<'a> Modifier<Control> for &'a String {
    fn modify(self, control: &mut Control) {
        control.add_attribute(Text(self.to_property()))
    }
}
impl Modifier<Control> for Bounds {
    fn modify(self, control: &mut Control) {
        control.add_attribute(BoundingBox(self))
    }
}

impl Modifier<Control> for Resource<BindingCanvas> {
    fn modify(self, control: &mut Control) {
        control.add_attribute(ControlAttribute::Canvas(self))
    }
}

impl Modifier<Control> for (ActionTrigger, String) {
    fn modify(self, control: &mut Control) {
        control.add_attribute(Action(self.0, self.1))
    }
}

impl<'a> Modifier<Control> for (ActionTrigger, &'a str) {
    fn modify(self, control: &mut Control) {
        control.add_attribute(Action(self.0, String::from(self.1)))
    }
}

impl Modifier<Control> for Vec<ControlAttribute> {
    fn modify(self, control: &mut Control) {
        for attr in self.into_iter() {
            control.add_attribute(attr);
        }
    }
}

impl Modifier<Control> for Vec<Control> {
    fn modify(self, control: &mut Control) {
        control.add_attribute(SubComponents(self))
    }
}