guit  0.1
 All Classes Functions Variables Typedefs Enumerations Friends
gtypes.hpp
1 //
2 // Main types and type conversions.
3 // Guit GUI Toolkit
4 // Copyright © 2020 Eric Lecolinet. All rights reserved.
5 // http://www.telecom-paris.fr/~elc
6 //
7 
8 #ifndef Guit_Types_hpp
9 #define Guit_Types_hpp
10 #include <cstdint>
11 #include <memory>
12 #include <iostream>
13 #include <functional>
14 #include <string>
15 #include <vector>
16 
17 #define GUIT_VERSION_MAJOR 0
18 #define GUIT_VERSION_MINOR 1
19 #define GUIT_VERSION_PATCH 1
20 
21 #if defined(_WIN32)
22 # undef min
23 # undef max
24 # undef small
25 using ssize_t = signed long;
26 #endif
27 
28 namespace guit {
29 
31 using GString = std::string;
32 
34 using GStrings = std::vector<GString>;
35 
37 using GId = uint16_t;
38 
40 using GTexture = uint32_t;
41 
43 using GTime = uint32_t;
44 
46 using GKeyCode = uint32_t;
47 
48 // GEvent types (see gevent.hpp).
49 enum struct GEventType : uint32_t;
50 
51 // GEvent Button and Modifier states (see gevent.hpp).
52 enum struct GEventState : uint32_t;
53 
55 enum struct GHandle : uint8_t;
56 
59 using GFunction = std::function<void()>;
60 using GEventFunction = std::function<void(class GEvent&)>;
61 using GPredicate = std::function<bool(class Gadget&)>;
63 
64 
67 void gerror(GString const& where, GString const& message, class GObject const* = {});
68 
72 void gexec(GFunction const& fun);
73 
75 void gsleep(GTime milliseconds);
76 
77 
80 struct GPoint {
81  float x, y;
82 
83  GPoint() : x{}, y{} {}
84  GPoint(float x, float y) : x(x), y(y) {}
85 
86  void set(float x_, float y_) {x = x_; y = y_;}
87  GPoint operator+(GPoint const&) const;
88  GPoint operator-(GPoint const&) const;
89  bool operator==(GPoint const&) const;
90  bool operator!=(GPoint const&) const;
91  friend std::istream& operator>>(std::istream& in, GPoint&);
92  friend std::ostream& operator<<(std::ostream& out, GPoint const&);
93 };
94 
95 
98 struct GDim {
99  float w, h;
100 
101  GDim() : w{}, h{} {}
102  GDim(float w, float h) : w(w), h(h) {}
103 
104  void set(float width, float height) {w = width; h = height;}
105  bool operator==(const GDim&) const;
106  bool operator!=(const GDim&) const;
107  friend std::ostream& operator<<(std::ostream& out, GDim const&);
108  friend std::istream& operator>>(std::istream& in, GDim&);
109 };
110 
111 
113 struct GRect {
114  float x, y, w, h;
115 
116  GRect() : x{}, y{}, w{}, h{} {}
117  GRect(float x, float y, float w, float h) : x(x), y(y), w(w), h(h) {}
118  GRect(GPoint const& p, GDim const& d) : x(p.x), y(p.y), w(d.w), h(d.h) {}
119 
122  GRect(GPoint const& p1, GPoint const& p2);
123 
124  GRect frame() const {return *this;}
125  void set(float x, float y, float width, float height);
126  void translate(float dx, float dy);
127  bool contain(GPoint const& point) const {
128  return point.x >= x && point.x <= x+w && point.y >= y && point.y <= y+h;
129  }
130  bool contain(GPoint const& point, float tolerance) const;
131  bool intersect(GPoint const& line_point1, GPoint const& line_point2) const;
132  bool operator==(const GRect&) const;
133  bool operator!=(const GRect&) const;
134  friend std::ostream& operator<<(std::ostream&, GRect const&);
135  friend std::istream& operator>>(std::istream&, GRect&);
136 };
137 
138 
140 struct GPoints : public std::vector<GPoint> {
141  GRect frame() const;
142  void translate(float dx, float dy);
143  bool contain(GPoint const& point) const;
144  bool contain(GRect const& rect) const;
145  bool intersect(GRect const& rect) const;
146  friend std::ostream& operator<<(std::ostream&, GPoints const&);
147  friend std::istream& operator>>(std::istream&, GPoints&);
148 };
149 
150 
153 inline bool gconvert(GString const& from, GString& to) {to = from; return true;}
154 bool gconvert(GString const& from, bool& to);
155 bool gconvert(GString const& from, float& to);
156 bool gconvert(GString const& from, int& to);
157 bool gconvert(GString const& from, int8_t& to);
158 bool gconvert(GString const& from, int16_t& to);
159 
160 bool gconvert(const bool& from, GString& to);
161 bool gconvert(const float& from, GString& to);
162 inline bool gconvert(const int& from, GString& to) {to = std::to_string(from); return true;}
163 inline bool gconvert(const int8_t& from, GString& to) {to = std::to_string(from); return true;}
164 inline bool gconvert(const int16_t& from, GString& to) {to = std::to_string(from); return true;}
165 
166 bool gconvert(const float& from, int& to);
167 inline bool gconvert(const int& from, float& to) {to = float(from); return true;}
168 
169 inline bool gconvert(const float& from, float& to) {to = from; return true;}
170 inline bool gconvert(const int& from, int& to) {to = from; return true;}
171 inline bool gconvert(const bool& from, bool& to) {to = from; return true;}
172 
173 bool gconvert(const double& from, float& to);
174 bool gconvert(const double& from, int& to);
175 
176 template <typename T>
177 inline GString toString(T const& val) {GString s; gconvert(val,s); return s;}
178 
179 template <typename T>
180 inline int toInt(T const& val) {int num{}; gconvert(val,num); return num;}
181 
182 template <typename T>
183 inline float toFloat(T const& val) {float num{}; gconvert(val,num); return num;}
185 
186 
187 // Gadget and prop metaclasses
188 class GadgetType;
189 class GPropType;
190 
191 // Base class, superclass of Gadget and GProp
192 class GObject;
193 
194 // Main Gadgets
195 class Gadget;
196 class GBox;
197 class GWindow;
198 class GShape;
199 class GItem;
200 class GTextItem;
201 class GValueItem;
202 class GChartBox;
203 class GChoiceBox;
204 class GScrollBox;
205 class GTable;
206 class GTRow;
207 class GTHead;
208 class GMenu;
209 class GMenuBar;
210 
211 // Main Props
212 class GProp;
213 class GVarProp;
214 class GBool;
215 class GNumber;
216 class GInt;
217 class GFloat;
218 class GText;
219 class GIcon;
220 class GFont;
221 class GFormat;
222 class GColor;
223 class GBackground;
224 class GFlow;
225 class GFlex;
226 class GAlign;
227 class GGap;
228 class GPos;
229 class GSize;
230 class GMargin;
231 class GBorder;
232 class GChoice;
233 class GTextData;
234 
235 // Timers and animations
236 class GTimer;
237 class GAnim;
238 
239 // Conditional props
240 class GCond;
241 class GGuard;
242 class GOtherwise;
243 
244 // Active Expressions
245 class GExpr;
246 class GTextExpr;
247 class GNumExpr;
248 class GBoolExpr;
249 class GExprBool;
250 
251 // Statecharts
252 class GStateChart;
253 class GState;
254 class GHState;
255 
256 // Callbacks and Events
257 class GTrigger;
258 class GEvent;
259 class GMouseEvent;
260 class GKeyEvent;
261 class GTouchEvent;
262 class GGestureEvent;
263 class GCaller;
264 
265 // Styles, Themes and Graphics
266 class GTheme;
267 class GStyle;
268 class GRgba;
269 class GGraphics;
270 class GRender;
271 class GRenderGraphics;
272 class GTextAttributes;
273 class GTextAttr;
274 
275 }
276 #endif