Symphony Of Empires
ui.hpp
Go to the documentation of this file.
1 // Symphony of Empires
2 // Copyright (C) 2021, Symphony of Empires contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // eng3d/ui/ui.hpp
20 //
21 // Abstract:
22 // The general UI context.
23 // ----------------------------------------------------------------------------
24 
25 #pragma once
26 
27 #include <cstddef>
28 #include <deque>
29 #include <functional>
30 #include <vector>
31 #include <mutex>
32 #include <string>
33 
34 #include <glm/vec2.hpp>
35 #include <glm/mat4x4.hpp>
36 
37 #include "eng3d/shader.hpp"
38 #include "eng3d/rectangle.hpp"
39 #include "eng3d/color.hpp"
40 
41 struct SDL_Surface;
42 namespace Eng3D {
43  class Texture;
44  class State;
45  struct Font;
46 }
47 
50 namespace UI {
51  enum class ClickState {
52  NOT_CLICKED = 0,
53  NOT_HANDLED = 1,
54  HANDLED = 2,
55  };
56 
57  class Widget;
58  class Tooltip;
59  typedef void (*Callback)(UI::Widget&);
60 
63  class Context {
64  int drag_x, drag_y;
65  bool is_drag = false;
66  UI::Widget* dragged_widget = nullptr;
67  int width, height;
68  glm::ivec2 cursor_pos;
69 
70  glm::ivec2 get_pos(UI::Widget& w, glm::ivec2 offset);
71  void clear_hover_recursive(UI::Widget& w);
72  bool check_hover_recursive(UI::Widget& w, glm::ivec2 mouse_pos, glm::ivec2 offset);
73  UI::ClickState check_click_recursive(UI::Widget& w, glm::ivec2 mouse_pos, glm::ivec2 offset, UI::ClickState click_state, bool clickable, bool mouse_pressed);
74  bool check_wheel_recursive(UI::Widget& w, glm::ivec2 mouse_pos, glm::ivec2 offset, int y);
75  // Render the widget and it's children
76  void render_recursive(UI::Widget& widget, Eng3D::Rect viewport, glm::ivec2 offset);
77  int do_tick_recursive(UI::Widget& w);
78  void clear_dead_recursive(UI::Widget& w);
79 
80  // Is set when mouse is pressed on a widget with a on_drag function
81  // Is set to nullptr when the mouse is released
82  std::function<void(glm::ivec2 start_mouse_pos, glm::ivec2 current_mouse_pos)> on_drag = nullptr;
83  // Indicates which widget have been pressed.
84  // Will call on_click on the widget if the mouse is released on it
85  Widget* mouse_pressed_widget = nullptr;
86  // The mouse position when the mouse is pressed down
87  glm::ivec2 start_drag_mouse_position;
88 
89  std::vector<std::unique_ptr<UI::Widget>> widgets;
90  std::vector<std::unique_ptr<UI::Widget>> no_eval_widgets;
91  UI::Tooltip* tooltip_widget = nullptr;
92  Eng3D::State& s;
93  public:
94  Context() = delete;
96  ~Context();
97  void load_textures();
98  void add_widget(UI::Widget* widget);
99  void remove_widget(UI::Widget* widget);
100  void render_all();
101  void resize(const int width, const int height);
102  void set_cursor_pos(glm::ivec2 pos);
103 
108  bool check_hover(glm::ivec2 mouse_pos);
109  uint32_t hover_update = 1;
110 
116  bool check_click(glm::ivec2 mouse_pos);
117 
120  bool check_mouse_released(glm::ivec2 mouse_pos);
121 
124  void check_drag(glm::ivec2 mouse_pos);
125 
130  bool check_wheel(glm::ivec2 mouse_pos, int y);
131 
135  bool check_text_input(const char* input);
136 
137  void use_tooltip(Tooltip* tooltip, glm::ivec2 pos);
138 
140  void do_tick();
141  void clear();
142  void clear_dead();
143  void set_eval(UI::Widget& widget, bool eval);
144 
145  void prompt(const std::string& title, const std::string& text);
146 
147  std::shared_ptr<Eng3D::Texture> foreground;
148  std::shared_ptr<Eng3D::Texture> background;
149  std::shared_ptr<Eng3D::Texture> window_top;
150  std::shared_ptr<Eng3D::Texture> button;
151  std::shared_ptr<Eng3D::Texture> tooltip_tex;
152  std::shared_ptr<Eng3D::Texture> piechart_overlay;
153  std::shared_ptr<Eng3D::Texture> border_tex;
154  std::shared_ptr<Eng3D::Texture> button_border;
155  std::shared_ptr<Eng3D::Texture> cursor_tex;
156 
157  std::shared_ptr<Eng3D::TrueType::Font> default_font;
158 
159  std::unique_ptr<Eng3D::OpenGL::Program> obj_shader;
160  std::unique_ptr<Eng3D::OpenGL::Program> piechart_shader;
161 
162  std::vector<std::pair<std::string, std::string>> prompt_queue;
163  std::mutex prompt_queue_mutex;
164 
165  glm::mat4 projection; // Projection & view (recalculated on resize)
166  glm::mat4 view;
167  glm::mat4 model; // Base model matrix
168 
169  friend class Widget;
170  };
171  extern Context* g_ui_context;
172 } // namespace UI
The UI context that handles all the ui widgets.
Definition: ui.hpp:63
std::shared_ptr< Eng3D::Texture > cursor_tex
Definition: ui.hpp:155
std::mutex prompt_queue_mutex
Definition: ui.hpp:163
std::shared_ptr< Eng3D::Texture > button_border
Definition: ui.hpp:154
void set_eval(UI::Widget &widget, bool eval)
Moves a widget from evaluable to non-evaluable making a widget non-evaluable has side effects,...
Definition: ui.cpp:165
std::shared_ptr< Eng3D::Texture > foreground
Definition: ui.hpp:147
Context()=delete
std::shared_ptr< Eng3D::TrueType::Font > default_font
Definition: ui.hpp:157
std::vector< std::pair< std::string, std::string > > prompt_queue
Definition: ui.hpp:162
~Context()
Definition: ui.cpp:104
std::shared_ptr< Eng3D::Texture > tooltip_tex
Definition: ui.hpp:151
glm::mat4 view
Definition: ui.hpp:166
void render_all()
Render all widgets.
Definition: ui.cpp:323
void use_tooltip(Tooltip *tooltip, glm::ivec2 pos)
Definition: ui.cpp:580
void do_tick()
Will call on_tick on all widgets.
Definition: ui.cpp:635
std::shared_ptr< Eng3D::Texture > button
Definition: ui.hpp:150
glm::mat4 projection
Definition: ui.hpp:165
bool check_mouse_released(glm::ivec2 mouse_pos)
Release the dragging of the widget.
Definition: ui.cpp:530
std::unique_ptr< Eng3D::OpenGL::Program > obj_shader
Definition: ui.hpp:159
std::shared_ptr< Eng3D::Texture > background
Definition: ui.hpp:148
void remove_widget(UI::Widget *widget)
Definition: ui.cpp:115
void prompt(const std::string &title, const std::string &text)
Definition: ui.cpp:183
std::shared_ptr< Eng3D::Texture > piechart_overlay
Definition: ui.hpp:152
void resize(const int width, const int height)
Definition: ui.cpp:265
bool check_text_input(const char *input)
Will give keyboard input to Input Widget if one is selected.
Definition: ui.cpp:573
glm::mat4 model
Definition: ui.hpp:167
void load_textures()
uint32_t hover_update
Definition: ui.hpp:109
void check_drag(glm::ivec2 mouse_pos)
Check for on_drag events, will move Window widgets with is_pinned = false.
Definition: ui.cpp:555
bool check_wheel(glm::ivec2 mouse_pos, int y)
Check if the mouse is above a widget and scroll widget.
Definition: ui.cpp:625
std::unique_ptr< Eng3D::OpenGL::Program > piechart_shader
Definition: ui.hpp:160
std::shared_ptr< Eng3D::Texture > window_top
Definition: ui.hpp:149
void add_widget(UI::Widget *widget)
Definition: ui.cpp:108
bool check_hover(glm::ivec2 mouse_pos)
Check for on_hover events If the mouse is above a widget call the widgets on_hover or show its toolti...
Definition: ui.cpp:400
bool check_click(glm::ivec2 mouse_pos)
Check for on_click events. Check if the mouse is above a widget and call the widgets on_click if poss...
Definition: ui.cpp:498
void clear()
Removes all widgets.
Definition: ui.cpp:121
void clear_dead()
Removes all widgets that have been killed.
Definition: ui.cpp:146
std::shared_ptr< Eng3D::Texture > border_tex
Definition: ui.hpp:153
void set_cursor_pos(glm::ivec2 pos)
Definition: ui.cpp:275
Tooltip widget, used entirely for hovering purpouses, don't use any other widget for hovering unless ...
Definition: tooltip.hpp:38
The master widget all the other widgets inherit from, do not use directly instead use one of the many...
Definition: widget.hpp:176
void(* Callback)(UI::Widget &)
Definition: ui.hpp:59
Context * g_ui_context
Definition: ui.cpp:63
ClickState
Definition: ui.hpp:51