Simulant  21.12-574
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
ui_config.h
1 #pragma once
2 
3 #include <cstdint>
4 #include "../../colour.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 
230 /* 100th of the viewport width */
231 struct Vw {
232  float value;
233 };
234 
235 /* 100th of the viewport height */
236 struct Vh {
237  float value;
238 };
239 
240 
241 extern const char* DEFAULT_FONT_FAMILY;
242 extern const Px DEFAULT_FONT_SIZE;
243 
244 struct UIConfig {
245  static const Colour ALICE_BLUE;
246  static const Colour LIGHT_GREY;
247  static const Colour DODGER_BLUE;
248 
249  std::string font_family_ = ""; /* Use default */
250  Px font_size_ = Px(0); /* Use default */
251 
252  Rem line_height_ = Rem(1.5f);
253 
254  Colour foreground_colour_ = Colour::from_bytes(40, 40, 40, 255);
255  Colour background_colour_ = Colour::from_bytes(53, 53, 53, 255);
256  Colour text_colour_ = Colour::from_bytes(219, 219, 219, 255);
257  Colour highlight_colour_ = Colour::from_bytes(0, 51, 102, 255);
258 
259  ResizeMode label_resize_mode_ = RESIZE_MODE_FIT_CONTENT;
260  ResizeMode button_resize_mode_ = RESIZE_MODE_FIT_CONTENT;
261  ResizeMode progress_bar_resize_mode_ = RESIZE_MODE_FIXED;
262 
263  uint8_t scrollbar_width_ = 16;
264  Colour scrollbar_background_colour_ = background_colour_;
265  Colour scrollbar_foreground_colour_ = foreground_colour_;
266 
267  uint16_t button_height_ = 36;
268  uint16_t button_width_ = 0; // Fit content
269 
270  UInt4 label_padding_ = { Px(4), Px(4), Px(4), Px(4) };
271  PackedColour4444 label_background_colour_ = Colour::NONE;
272  PackedColour4444 label_foreground_colour_ = Colour::NONE;
273  PackedColour4444 label_border_colour_ = Colour::NONE;
274  PackedColour4444 label_text_colour_ = text_colour_;
275 
276  UInt4 button_padding_ = { Px(30), Px(30), Px(20), Px(20) };
277  PackedColour4444 button_background_colour_ = highlight_colour_;
278  PackedColour4444 button_foreground_colour_ = Colour::NONE;
279  PackedColour4444 button_text_colour_ = text_colour_;
280  PackedColour4444 button_border_colour_ = Colour::NONE;
281 
282  Px button_border_width_ = Px(0);
283  Px button_border_radius_ = Px(4);
284 
285  UInt4 image_padding_ = {Px(), Px(), Px(), Px()};
286  Px image_border_width_ = Px(0);
287  PackedColour4444 image_background_colour_ = smlt::Colour::WHITE;
288  PackedColour4444 image_foreground_colour_ = smlt::Colour::NONE;
289  PackedColour4444 image_text_colour_ = smlt::Colour::NONE;
290 
291  PackedColour4444 progress_bar_foreground_colour_ = highlight_colour_;
292  PackedColour4444 progress_bar_background_colour_ = background_colour_;
293  PackedColour4444 progress_bar_border_colour_ = foreground_colour_;
294  PackedColour4444 progress_bar_text_colour_ = text_colour_;
295  Px progress_bar_border_width_ = Px(2);
296  Px progress_bar_width_ = Px(100);
297  Rem progress_bar_height_ = Rem(1.5f);
298 
299  PackedColour4444 frame_background_colour_ = background_colour_;
300  PackedColour4444 frame_titlebar_colour_ = foreground_colour_;
301  PackedColour4444 frame_text_colour_ = text_colour_;
302  Px frame_border_width_ = Px(2);
303  PackedColour4444 frame_border_colour_ = foreground_colour_;
304 
305  OverflowType default_overflow_ = OVERFLOW_TYPE_HIDDEN;
306  ResizeMode default_resize_mode_ = RESIZE_MODE_FIXED;
307 };
308 
309 }
310 }
smlt::PackedColour4444
Definition: colour.h:136
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:236
smlt::ui::UIConfig
Definition: ui_config.h:244
smlt::ui::UInt4
Definition: ui_config.h:202
smlt::ui::Px
Definition: ui_config.h:42
smlt::Colour
Definition: colour.h:29
smlt::ui::UIDim
Definition: ui_config.h:192
smlt::ui::Vw
Definition: ui_config.h:231