1 #pragma once
2 #include <SFML/Audio.hpp>
3 #include <SFML/Graphics.hpp>
4 #include <SFML/OpenGL.hpp>
5 #include <Localization.h>
6 #include <map>
7 #include <stack>
8 #include <algorithm>
9 #include <iterator>
10 #include <functional>
11 #include <stack>
12
13 #ifdef _WIN32
14 #define ERROR_MSG(x) MessageBox(nullptr, TEXT(x), TEXT("ERROR"), MB_OK);
15 #else
16 #define ERROR_MSG(x) std::cerr << x << std::endl;
17 #endif
18
19 //default interface parameters
20 extern sf::Color default_main_color;
21 extern sf::Color default_hover_main_color;
22 extern sf::Color default_active_main_color;
23 extern float default_margin;
24 extern sf::Vector2f default_size;
25 extern sf::View default_view;
26
27 extern float animation_sharpness;
28 extern float action_dt;
29
30 static const std::string close_png = "images/clear.png";
31 static const std::string delete_png = "images/delete.png";
32 static const std::string edit_png = "images/edit.png";
33 static const std::string priority_png = "images/priority.png";
34 static const std::string done_png = "images/done.png";
35 extern int focused;
36
num2str(const T & n)37 template < typename T > std::string num2str(const T& n)
38 {
39 std::ostringstream stm;
40 if (n < 10) stm << "0";
41 stm << n;
42 return stm.str();
43 }
44
45 struct ColorFloat
46 {
47 float r, g, b, a;
48 ColorFloat(float red = 0.f, float green = 0.f, float blue = 0.f, float alpha = 255.f);
49
50 void operator=(sf::Color a);
51 };
52
53 ColorFloat operator+(ColorFloat a, ColorFloat b);
54
55 ColorFloat operator-(ColorFloat a, ColorFloat b);
56
57 ColorFloat operator*(ColorFloat a, float b);
58
59 sf::Color ToColor(ColorFloat a);
60 ColorFloat ToColorF(sf::Color a);
61
62 struct InputState
63 {
64 bool isKeyPressed;
65 bool keys[sf::Keyboard::KeyCount] = { false };
66 bool key_press[sf::Keyboard::KeyCount] = { false };
67 bool mouse[3] = { false };
68 bool mouse_press[3] = { false };
69 float wheel = 0.f;
70 sf::Vector2f mouse_pos = sf::Vector2f(0,0);
71 sf::Vector2f mouse_prev = sf::Vector2f(0, 0);
72 sf::Vector2f mouse_speed = sf::Vector2f(0, 0);
73 sf::Vector2f window_size = sf::Vector2f(0, 0);
74 float time = 0, dt = 0;
75 bool axis_moved[sf::Joystick::AxisCount] = { false };
76 float axis_value[sf::Joystick::AxisCount] = { 0.f };
77 bool buttons[sf::Joystick::ButtonCount] = { false };
78 bool button_pressed[sf::Joystick::ButtonCount] = { false };
79
80 InputState();
81 InputState(bool keys[sf::Keyboard::KeyCount], bool mouse[3], sf::Vector2f mouse_pos, sf::Vector2f mouse_speed);
82 };
83
84 typedef std::function<void(sf::RenderWindow * window, InputState & state)> call_func;
85
86 //the object parameters
87 struct State
88 {
89 sf::Vector2f position = sf::Vector2f(0,0);
90 sf::Vector2f size = sf::Vector2f(0.1f, 0.1f);
91 float border_thickness = 0.f;
92 float margin = default_margin;
93 float font_size = 1.f;
94 float scroll = 0.f;
95 float inside_size = 0.f;
96 ColorFloat color_main = ToColorF(sf::Color::Black);
97 ColorFloat color_second = ToColorF(sf::Color::Transparent);
98 ColorFloat color_border = ColorFloat(200,0,0);
99 };
100
101 //interpolate between states for animation effects
102 State interpolate(State a, State b, float t);
103
104 void UpdateAllObjects(sf::RenderWindow * window, InputState& state);
105
106
107 //the object base class
108 class Object
109 {
110 public:
111 enum States
112 {
113 DEFAULT, ONHOVER, ACTIVE
114 };
115
116 enum Allign
117 {
118 LEFT, CENTER, RIGHT
119 };
120
121 void ApplyScroll(float x);
122 void SetPosition(float x, float y);
123 void SetSize(float x, float y);
124 void SetHeigth(float x);
125 void SetWidth(float x);
126 void SetBackgroundColor(sf::Color color);
127 void SetBorderColor(sf::Color color);
128 void SetBorderWidth(float S);
129 void SetMargin(float x);
130 void SetInsideSize(float x);
131 void SetScroll(float x);
132 void Move(sf::Vector2f dx);
133
134 void SetDefaultFunction(call_func fun);
135 void SetCallbackFunction(call_func fun, bool limit_repeat = true);
136 void SetHoverFunction(call_func fun);
137
138 void SetMainDefaultFunction(call_func fun);
139 void SetMainCallbackFunction(call_func fun, bool limit_repeat = true);
140 void SetMainHoverFunction(call_func fun);
141
142 void ClearDefaultFunctions();
143 void ClearCallbackFunctions();
144 void ClearHoverFunctions();
145
146 bool RunCallback(sf::RenderWindow * window, InputState& state);
147 void clone_states();
148
149 virtual void Draw(sf::RenderWindow * window, InputState& state);
150 virtual void AddObject(Object* a, Allign b);
151 void Update(sf::RenderWindow * window, InputState& state);
152 void UpdateAction(sf::RenderWindow * window, InputState& state);
153
154 Object();
155
156 Object(Object& A);
157 Object(Object&& A);
158 Object(Object* A);
159
160 virtual void operator=(Object& A);
161 virtual void operator=(Object&& A);
162
163 void copy(Object& A);
164 void copy(Object&& A);
165
166 virtual Object* GetCopy();
167
168 State curstate;
169 State activestate;
170 State hoverstate;
171 State defaultstate;
172 States curmode;
173
174 Allign obj_allign;
175
176 sf::View used_view;
177
178 //multiple callbacks
179 std::vector<std::function<void(sf::RenderWindow * window, InputState & state)>> callback, hoverfn, defaultfn;
180
181 //objects inside this object
182 std::vector<std::unique_ptr<Object>> objects;
183 std::stack<int> z_buffer;
184
185 //operation time limiter
186 float action_time;
187 bool static_object;
188 bool limiter;
189
190 sf::Vector2f worldPos;
191 sf::FloatRect obj;
192
193 int id;
194 };
195
196 void UpdateAspectRatio(float width, float heigth);
197 int AddGlobalObject(Object & a);
198 Object& get_glob_obj(int id);
199 bool NoObjects();
200 int NumberOfObjects();
201 void RemoveGlobalObject(int id);
202 void RemoveAllObjects();
203 void Add2DeleteQueue(int id);
204
205 //a box to add stuff in
206 class Box: public Object
207 {
208 public:
209 void SetBackground(const sf::Texture & texture);
210 void Draw(sf::RenderWindow *window, InputState& state);
211
212 Box(float x, float y, float dx, float dy, sf::Color color_main = default_main_color);
213 Box(float dx, float dy, sf::Color color_main = default_main_color);
214 Box();
215
216 Box(Box& A);
217 Box(Box&& A);
218
219 void SetAutoSize(bool b);
220
221 void operator=(Box& A);
222 void operator=(Box&& A);
223
224 virtual Object* GetCopy();
225
226 ~Box();
227
228 protected:
229 bool auto_size;
230
231 private:
232 sf::Texture image;
233 sf::RectangleShape rect;
234 sf::View boxView;
235 };
236
237
238
239 class MenuBox : public Box
240 {
241 public:
242 int cursor_id;
243
244 sf::Vector2f dmouse;
245
246 virtual void AddObject(Object * something, Allign a);
247
248 void Cursor(int d);
249
250 void ScrollBy(float dx);
251 void ScrollTo(float scroll);
252
253 MenuBox(float dx, float dy, bool auto_y = false, float x = 0, float y = 0, sf::Color color_main = sf::Color(0, 0, 0, 0));
254
255 MenuBox(MenuBox& A);
256 MenuBox(MenuBox&& A);
257
258 void CreateCallbacks();
259
260 void operator=(MenuBox& A);
261 void operator=(MenuBox&& A);
262
263 virtual Object* GetCopy();
264 };
265
266
267 class Window: public Box
268 {
269 public:
270 sf::Vector2f dmouse;
271
272 void Add(Object * something, Allign a = LEFT);
273
274 template<class T>
275 Window(float x, float y, float dx, float dy, sf::Color color_main = default_main_color, T title = LOCAL["Window"], sf::Font & font = LOCAL("default"));
276
277 Window(Window& A);
278 Window(Window&& A);
279
280 void CreateCallbacks();
281
282 void operator=(Window& A);
283 void operator=(Window&& A);
284
285 virtual Object* GetCopy();
286 };
287
288
289 class Text : public Object
290 {
291 public:
292 std::unique_ptr<sf::Text> text;
293
294 void Draw(sf::RenderWindow *window, InputState& state);
295
296 template<class T>
297 Text(T str, sf::Font &f, float size, sf::Color col = sf::Color::White);
298 Text(sf::Text t);
299
300 Text(Text& A);
301 Text(Text&& A);
302
303 template<class T>
304 void SetString(T str);
305
306 void operator=(Text& A);
307 void operator=(Text&& A);
308
309 virtual Object* GetCopy();
310 };
311
312
313 //a button
314 class Button: public Box
315 {
316 public:
317 template<class T>
318 Button(T text, float w, float h, std::function<void(sf::RenderWindow * window, InputState & state)> fun, sf::Color color_hover = default_hover_main_color, sf::Color color_main = default_main_color);
319
320 Button(Button& A);
321 Button(Button&& A);
322
323 void operator=(Button& A);
324 void operator=(Button&& A);
325
326 virtual Object* GetCopy();
327
328 ~Button();
329 };
330
331 class Image : public Box
332 {
333 public:
334 Image(sf::Texture image, float w, float h, sf::Color color_hover);
335 Image(std::string image, float w = 0, float h = 0, sf::Color color_hover = sf::Color::White);
336
337 Image(Image& A);
338 Image(Image&& A);
339
340 void operator=(Image& A);
341 void operator=(Image&& A);
342
343 virtual Object* GetCopy();
344
345 ~Image();
346 };
347
348
349 std::string key_name(sf::Keyboard::Key& key);
350
351 //basic object for control mapping
352 class KeyMapper : public Box
353 {
354 public:
355 enum MapperType {
356 KEYBOARD, JOYSTICK_AXIS, JOYSTICK_KEYS
357 };
358
359 template<class T>
360 KeyMapper(T label, T act_label, int* key, float w, float h, MapperType type, sf::Color color_active = sf::Color::Blue, sf::Color color_hover = default_hover_main_color, sf::Color color_main = default_main_color);
361
362 KeyMapper(KeyMapper& A);
363 KeyMapper(KeyMapper&& A);
364
365 void operator=(KeyMapper& A);
366 void operator=(KeyMapper&& A);
367
368 void CreateCallbacks();
369 void SetKeyString();
370
371 virtual Object* GetCopy();
372
373 MapperType this_type;
374 int *key_ptr;
375 bool waiting;
376 std::wstring wait_text;
377 };
378
379 template<class T>
KeyMapper(T label,T act_label,int * key,float w,float h,MapperType type,sf::Color color_active,sf::Color color_hover,sf::Color color_main)380 inline KeyMapper::KeyMapper(T label, T act_label, int * key, float w, float h, MapperType type, sf::Color color_active, sf::Color color_hover, sf::Color color_main): waiting(false), this_type(type), wait_text(act_label)
381 {
382 SetSize(w, h);
383 SetBackgroundColor(color_main);
384 Box LeftBox(0, 0, float(w * 0.5f), h-defaultstate.margin*2.f, sf::Color(0, 100, 200, 0)),
385 RightBox(0, 0, float(w * 0.33f), h - defaultstate.margin * 2.f, sf::Color(0, 100, 200, 128));
386 LeftBox.AddObject(new Text(label, LOCAL("default"), h*0.7f, sf::Color::White), Allign::LEFT);
387 RightBox.AddObject(new Text(act_label, LOCAL("default"), h*0.7f, sf::Color::White), Allign::CENTER);
388 RightBox.activestate.color_main = color_active;
389 hoverstate.color_main = color_hover;
390 this->AddObject(&LeftBox,Allign::LEFT);
391 this->AddObject(&RightBox, Allign::RIGHT);
392 key_ptr = key;
393 SetKeyString();
394 }
395
396 template<class T>
Window(float x,float y,float dx,float dy,sf::Color color_main,T title,sf::Font & font)397 inline Window::Window(float x, float y, float dx, float dy, sf::Color color_main, T title, sf::Font & font)
398 {
399 defaultstate.position.x = x;
400 defaultstate.position.y = y;
401 defaultstate.size.x = dx;
402 defaultstate.size.y = dy;
403
404 this->SetAutoSize(true);
405 defaultstate.color_main = ToColorF(color_main);
406 clone_states();
407 SetMargin(0);
408
409 Box Bar(0, 0, dx, 30, sf::Color(0, 100, 200, 128)),
410 CloseBx(0, 0, 30, 30, sf::Color(255, 255, 255, 255));
411 Text Title(title, font, 25, sf::Color::White);
412 Bar.SetMargin(0);
413 CloseBx.hoverstate.color_main = sf::Color(255, 0, 0, 255);
414
415 sf::Image close; close.loadFromFile(close_png);
416 sf::Texture closetxt; closetxt.loadFromImage(close);
417 closetxt.setSmooth(true);
418 CloseBx.SetBackground(closetxt);
419
420 Bar.AddObject(&Title, Box::LEFT);
421 Bar.AddObject(&CloseBx, Box::RIGHT);
422
423 MenuBox Inside(dx, dy - 30, true);
424
425
426 this->AddObject(&Bar, Box::CENTER);
427 this->AddObject(&Inside, Box::LEFT);
428
429 CreateCallbacks();
430 }
431
432 template<class T>
Button(T text,float w,float h,std::function<void (sf::RenderWindow * window,InputState & state)> fun,sf::Color color_hover,sf::Color color_main)433 inline Button::Button(T text, float w, float h, std::function<void(sf::RenderWindow*window, InputState&state)> fun, sf::Color color_hover, sf::Color color_main)
434 {
435 SetSize(w, h);
436 SetBackgroundColor(color_main);
437 Text button_text(text, LOCAL("default"), h*0.8f, sf::Color::White);
438 button_text.SetBorderColor(sf::Color::Black);
439 hoverstate.color_main = color_hover;
440 SetCallbackFunction(fun, true);
441 this->AddObject(&button_text,Object::Allign::CENTER);
442 }
443
444
445
446 template<class T>
Text(T str,sf::Font & f,float size,sf::Color col)447 inline Text::Text(T str, sf::Font & f, float size, sf::Color col)
448 {
449 text.reset(new sf::Text(str, f, size));
450 defaultstate.font_size = size;
451 defaultstate.color_main = col;
452 SetBorderColor(sf::Color::Black);
453 SetBorderWidth(2);
454 clone_states();
455 }
456
457 template<class T>
SetString(T str)458 inline void Text::SetString(T str)
459 {
460 text.get()->setString(str);
461 }
462