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
pub struct Width {
   pub scalar: f64,
   pub unit: String,
}
impl Width {
   pub fn new(scalar: f64, unit: String) -> Modifier {
      Modifier::Width(Width { scalar:scalar, unit:unit })
   }
}

pub struct Height {
   pub scalar: f64,
   pub unit: String,
}
impl Height {
   pub fn new(scalar: f64, unit: String) -> Modifier {
      Modifier::Height(Height { scalar:scalar, unit:unit })
   }
}

pub struct TranslateX {
   pub scalar: f64,
   pub unit: String,
}
impl TranslateX {
   pub fn new(scalar: f64, unit: String) -> Modifier {
      Modifier::TranslateX(TranslateX { scalar:scalar, unit:unit })
   }
}

pub struct TranslateY {
   pub scalar: f64,
   pub unit: String,
}
impl TranslateY {
   pub fn new(scalar: f64, unit: String) -> Modifier {
      Modifier::TranslateY(TranslateY { scalar:scalar, unit:unit })
   }
}

pub struct Scale {
   pub scalar: f64,
   pub unit: String
}
impl Scale {
   pub fn new(scalar: f64, unit: &str) -> Modifier {
      Modifier::Scale(Scale { scalar:scalar, unit:unit.to_owned() })
   }
}

pub struct Color {
   pub rgba: [f64; 4],
}
impl Color {
   pub fn new(rgba: [f64; 4]) -> Modifier {
      Modifier::Color(Color { rgba:rgba })
   }
}

pub struct Image {
   pub name: String,
   pub modifiers: Vec<Modifier>,
}
impl Image {
   pub fn new(name: &str) -> Component {
      Component::Image(Image { name: name.to_owned(), modifiers:Vec::new() })
   }
   pub fn modifiers(&self) -> &Vec<Modifier> {
      &self.modifiers
   }
   pub fn translate_x(&mut self, scalar: f64, unit: &str) {
      self.modifiers.push(TranslateX::new(scalar, unit.to_owned()));
   }
   pub fn translate_y(&mut self, scalar: f64, unit: &str) {
      self.modifiers.push(TranslateY::new(scalar, unit.to_owned()));
   }
   pub fn width(&mut self, scalar: f64, unit: &str) {
      self.modifiers.push(Width::new(scalar, unit.to_owned()));
   }
   pub fn height(&mut self, scalar: f64, unit: &str) {
      self.modifiers.push(Height::new(scalar, unit.to_owned()));
   }
}

pub struct Text {
   pub content: String,
   pub font: String,
   pub align: String,
   pub modifiers: Vec<Modifier>,
}
impl Text {
   pub fn new(font: &str, cs: &str) -> Component {
      Component::Text(Text { font:font.to_owned(), content: cs.to_owned(),
                             align: "left".to_owned(), modifiers:Vec::new() })
   }
   pub fn translate_x(&mut self, scalar: f64, unit: &str) {
      self.modifiers.push(TranslateX::new(scalar, unit.to_owned()));
   }
   pub fn translate_y(&mut self, scalar: f64, unit: &str) {
      self.modifiers.push(TranslateY::new(scalar, unit.to_owned()));
   }
   pub fn width(&mut self, scalar: f64, unit: &str) {
      self.modifiers.push(Width::new(scalar, unit.to_owned()));
   }
   pub fn height(&mut self, scalar: f64, unit: &str) {
      self.modifiers.push(Height::new(scalar, unit.to_owned()));
   }
   pub fn color(&mut self, rgba: [f64; 4]) {
      self.modifiers.push(Color::new(rgba));
   }
   pub fn scale(&mut self, scale: f64, unit: &str) {
      self.modifiers.push(Scale::new(scale, unit));
   }
   pub fn align(&mut self, align: &str) {
      self.align = align.to_owned()
   }
}

pub struct Rectangle {
   pub height: f64,
   pub hunit: String,
   pub width: f64,
   pub wunit: String,
   pub modifiers: Vec<Modifier>,
}
impl Rectangle {
   pub fn new(w: f64, wunit: &str, h: f64, hunit: &str) -> Component {
      Component::Rectangle(Rectangle { width:w, wunit:wunit.to_owned(), height:h, hunit:hunit.to_owned(), modifiers:Vec::new() })
   }
   pub fn translate_x(mut self, scalar: f64, unit: String) -> Rectangle {
      self.modifiers.push(TranslateX::new(scalar, unit.to_owned()));
      self
   }
   pub fn translate_y(mut self, scalar: f64, unit: &str) -> Rectangle {
      self.modifiers.push(TranslateY::new(scalar, unit.to_owned()));
      self
   }
}

pub enum Component {
   Modifier(Modifier),
   Image(Image),
   Text(Text),
   Rectangle(Rectangle),
}
impl Component {
   pub fn width(mut self, scalar: f64, unit: &str) -> Component {
      match self {
         Component::Image(ref mut m) => { m.width(scalar,unit); }
         Component::Text(ref mut m) => { m.width(scalar,unit); }
         _ => {}
      }; self
   }
   pub fn height(mut self, scalar: f64, unit: &str) -> Component {
      match self {
         Component::Image(ref mut m) => { m.height(scalar,unit); }
         Component::Text(ref mut m) => { m.height(scalar,unit); }
         _ => {}
      }; self
   }
   pub fn translate_x(mut self, scalar: f64, unit: &str) -> Component {
      match self {
         Component::Image(ref mut m) => { m.translate_x(scalar,unit); }
         Component::Text(ref mut m) => { m.translate_x(scalar,unit); }
         _ => {}
      }; self
   }
   pub fn translate_y(mut self, scalar: f64, unit: &str) -> Component {
      match self {
         Component::Image(ref mut m) => { m.translate_y(scalar,unit); }
         Component::Text(ref mut m) => { m.translate_y(scalar,unit); }
         _ => {}
      }; self
   }
   pub fn color(mut self, rgba: [f64; 4]) -> Component {
      match self {
         Component::Text(ref mut m) => { m.color(rgba); }
         _ => {}
      }; self
   }
   pub fn scale(mut self, scale: f64, unit: &str) -> Component {
      match self {
         Component::Text(ref mut m) => { m.scale(scale, unit); }
         _ => {}
      }; self
   }
   pub fn align(mut self, align: &str) -> Component {
      match self {
         Component::Text(ref mut m) => { m.align(align); }
         _ => {}
      }; self
   }
}

pub enum Modifier {
   Color(Color),
   Scale(Scale),
   TranslateX(TranslateX),
   TranslateY(TranslateY),
   Width(Width),
   Height(Height),
}

pub struct View {
   pub components: Vec<Component>,
}
impl View {
   pub fn new() -> View {
      View {
         components: Vec::new()
      }
   }
   pub fn append(&mut self, c: Component) -> &mut View {
      self.components.push( c );
      self
   }
}