Simulant  21.12-1292
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
ui_config.h
1 #pragma once
2 
3 #include <cstdint>
4 #include "../../color.h"
5 #include "../../font.h"
6 
7 namespace smlt {
8 namespace ui {
9 
10 enum OverflowType : uint8_t {
11  OVERFLOW_TYPE_HIDDEN,
12  OVERFLOW_TYPE_VISIBLE,
13  OVERFLOW_TYPE_AUTO
14 };
15 
16 enum TextAlignment : uint8_t {
17  TEXT_ALIGNMENT_LEFT,
18  TEXT_ALIGNMENT_CENTER,
19  TEXT_ALIGNMENT_RIGHT
20 };
21 
22 enum ResizeMode : uint8_t {
23  RESIZE_MODE_FIXED, // Clips / scrolls text
24  RESIZE_MODE_FIXED_WIDTH, // Will expand vertically with text, text is word-wrapped
25  RESIZE_MODE_FIXED_HEIGHT, // Will expand horizontally with text, text is not wrapped
26  RESIZE_MODE_FIT_CONTENT // Will fit the text, newlines affect the height
27 };
28 
29 enum WrapMode : uint8_t {
30  WRAP_MODE_CHAR,
31  WRAP_MODE_WORD
32 };
33 
34 enum ChangeFocusBehaviour : uint8_t {
35  FOCUS_THIS_IF_NONE_FOCUSED = 0x1,
36  FOCUS_NONE_IF_NONE_FOCUSED = 0x2
37 };
38 
39 struct Rem;
40 
41 /* Absolute pixel value */
42 struct Px {
43  int16_t value = 0;
44 
45  Px() = default;
46 
47  /* Implicitly convert integer types */
48  Px(const int& rhs):
49  value(rhs) {}
50 
51  Px(const unsigned int& rhs):
52  value(rhs) {}
53 
54  Px(const uint16_t& rhs):
55  value(rhs) {}
56 
57  Px(const long unsigned int& rhs):
58  value(rhs) {}
59 
60  /* Don't convert float types implicitly */
61  explicit Px(const double& rhs):
62  value(rhs) {}
63 
64  explicit Px(const float& rhs):
65  value(rhs) {}
66 
67  explicit operator bool() const {
68  return value != 0;
69  }
70 
71  Px& operator=(const int rhs) {
72  value = rhs;
73  return *this;
74  }
75 
76  Px operator-() const {
77  return Px(-value);
78  }
79 
80  bool operator>=(const Px& rhs) const {
81  return value >= rhs.value;
82  }
83 
84  bool operator>(const Px& rhs) const {
85  return value > rhs.value;
86  }
87 
88  bool operator<(const Px& rhs) const {
89  return value < rhs.value;
90  }
91 
92  bool operator>(const int rhs) const {
93  return value > rhs;
94  }
95 
96  bool operator==(const int16_t rhs) const {
97  return value == rhs;
98  }
99 
100  bool operator==(const Px& rhs) const {
101  return value == rhs.value;
102  }
103 
104  bool operator!=(const Px& rhs) const {
105  return !((*this) == rhs);
106  }
107 
108  bool operator<(const int rhs) const {
109  return value < rhs;
110  }
111 
112  Px operator*(const int rhs) const {
113  return Px(value * rhs);
114  }
115 
116  Px operator/(const int rhs) const {
117  return Px(value / rhs);
118  }
119 
120  Px operator+(const int rhs) const {
121  return Px(value + rhs);
122  }
123 
124  Px operator+(const uint16_t rhs) const {
125  return Px(value + rhs);
126  }
127 
128  Px operator-(const uint16_t rhs) const {
129  return Px(value - rhs);
130  }
131 
132  Px& operator+=(const Px& rhs) {
133  value += rhs.value;
134  return *this;
135  }
136 
137  Px& operator-=(const Px& rhs) {
138  value -= rhs.value;
139  return *this;
140  }
141 
142  Px operator+(const Px& rhs) const {
143  return Px(value + rhs.value);
144  }
145 
146  Px operator-(const Px& rhs) const {
147  return Px(value - rhs.value);
148  }
149 
150  Px& operator=(const int& rhs) {
151  value = rhs;
152  return *this;
153  }
154 
155  Px operator*(const Rem& rhs) const;
156 
157  Px operator*(const uint32_t x) const {
158  return Px(value * x);
159  }
160 
161  Px operator/(const uint32_t x) const {
162  return Px(value / x);
163  }
164 };
165 
166 inline bool operator<(const int& lhs, const Px& rhs) {
167  return lhs < rhs.value;
168 }
169 
170 inline Px operator+(const int& lhs, const Px& rhs) {
171  return Px(lhs + rhs.value);
172 }
173 
174 inline Px operator-(const int& lhs, const Px& rhs) {
175  return Px(lhs - rhs.value);
176 }
177 
178 struct UICoord {
179  UICoord():
180  x(0), y(0) {}
181 
182  UICoord(Px x, Px y):
183  x(x), y(y) {}
184 
185  bool operator==(const UICoord& rhs) const {
186  return x == rhs.x && y == rhs.y;
187  }
188 
189  Px x, y;
190 };
191 
192 struct UIDim {
193  Px width = Px(0);
194  Px height = Px(0);
195 
196  UIDim() = default;
197  UIDim(Px width, Px height):
198  width(width), height(height) {}
199 };
200 
201 
202 struct UInt4 {
203  Px left;
204  Px right;
205  Px bottom;
206  Px top;
207 };
208 
209 inline std::ostream& operator<<(std::ostream& stream, const Px& value) {
210  return (stream << value.value);
211 }
212 
213 inline bool operator==(const int16_t& lhs, const Px& rhs) {
214  return lhs == rhs.value;
215 }
216 
217 inline bool operator!=(const int16_t& lhs, const Px& rhs) {
218  return lhs != rhs.value;
219 }
220 
221 /* Relative to the "root" size */
222 struct Rem {
223  float value = 1.0f;
224 
225  Rem() = default;
226  explicit Rem(float r):
227  value(r) {}
228 
229  operator Px() const;
230 };
231 
232 /* 100th of the viewport width */
233 struct Vw {
234  float value;
235 };
236 
237 /* 100th of the viewport height */
238 struct Vh {
239  float value;
240 };
241 
242 
243 extern const char* DEFAULT_FONT_FAMILY;
244 extern const Px DEFAULT_FONT_SIZE;
245 
246 struct UIConfig {
247  static const Color ALICE_BLUE;
248  static const Color LIGHT_GREY;
249  static const Color DODGER_BLUE;
250 
251  std::string font_family_ = ""; /* Use default */
252  Px font_size_ = Px(0); /* Use default */
253 
254  Rem line_height_ = Rem(1.5f);
255 
256  Color foreground_color_ = Color::from_bytes(40, 40, 40, 255);
257  Color background_color_ = Color::from_bytes(53, 53, 53, 255);
258  Color text_color_ = Color::from_bytes(219, 219, 219, 255);
259  Color highlight_color_ = Color::from_bytes(0, 51, 102, 255);
260 
261  ResizeMode label_resize_mode_ = RESIZE_MODE_FIT_CONTENT;
262  ResizeMode button_resize_mode_ = RESIZE_MODE_FIT_CONTENT;
263 
264  uint8_t scrollbar_width_ = 16;
265  Color scrollbar_background_color_ = background_color_;
266  Color scrollbar_foreground_color_ = foreground_color_;
267 
268  UInt4 label_padding_ = { Px(4), Px(4), Px(4), Px(4) };
269  PackedColor4444 label_background_color_ = Color::none();
270  PackedColor4444 label_foreground_color_ = Color::none();
271  PackedColor4444 label_border_color_ = Color::none();
272  PackedColor4444 label_text_color_ = text_color_;
273 
274  UInt4 button_padding_ = { Px(30), Px(30), Px(20), Px(20) };
275  PackedColor4444 button_background_color_ = highlight_color_;
276  PackedColor4444 button_foreground_color_ = Color::none();
277  PackedColor4444 button_text_color_ = text_color_;
278  PackedColor4444 button_border_color_ = Color::none();
279 
280  Px button_border_width_ = Px(0);
281  Px button_border_radius_ = Px(4);
282 
283  UInt4 image_padding_ = {Px(), Px(), Px(), Px()};
284  Px image_border_width_ = Px(0);
285  PackedColor4444 image_background_color_ = smlt::Color::white();
286  PackedColor4444 image_foreground_color_ = smlt::Color::none();
287  PackedColor4444 image_text_color_ = smlt::Color::none();
288 
289  PackedColor4444 progress_bar_foreground_color_ = highlight_color_;
290  PackedColor4444 progress_bar_background_color_ = background_color_;
291  PackedColor4444 progress_bar_border_color_ = foreground_color_;
292  PackedColor4444 progress_bar_text_color_ = text_color_;
293  Px progress_bar_border_width_ = Px(2);
294 
295  PackedColor4444 frame_background_color_ = background_color_;
296  PackedColor4444 frame_titlebar_color_ = foreground_color_;
297  PackedColor4444 frame_text_color_ = text_color_;
298  Px frame_border_width_ = Px(2);
299  PackedColor4444 frame_border_color_ = foreground_color_;
300 
301  OverflowType default_overflow_ = OVERFLOW_TYPE_HIDDEN;
302  ResizeMode default_resize_mode_ = RESIZE_MODE_FIXED;
303 };
304 
305 }
306 }
smlt::ui::Rem
Definition: ui_config.h:222
smlt::ui::UICoord
Definition: ui_config.h:178
smlt
Definition: animation.cpp:25
smlt::ui::Vh
Definition: ui_config.h:238
smlt::Color
Definition: color.h:32
smlt::ui::UIConfig
Definition: ui_config.h:246
smlt::PackedColor4444
Definition: color.h:219
smlt::ui::UInt4
Definition: ui_config.h:202
smlt::ui::Px
Definition: ui_config.h:42
smlt::ui::UIDim
Definition: ui_config.h:192
smlt::ui::Vw
Definition: ui_config.h:233