· 6 years ago · Dec 12, 2019, 09:16 PM
1#pragma once
2#include "Main.h"
3#include "DrawManager.h"
4#include "Math.h"
5
6#define GUI_API
7
8struct GuiIO; // Main Configuration and I/O between the applicaiton and Gui
9struct GuiContext; // Gui context
10struct GuiWindow;
11struct GuiStyle;
12struct Rect;
13
14typedef unsigned int GuiID;
15typedef unsigned int U32;
16
17static inline float LengthSqr(const Vector2& lhs) { return lhs.x*lhs.x + lhs.y*lhs.y; }
18GUI_API U32 Hash(const void* data, int data_size, U32 seed = 0);
19
20struct GUI_API Rect
21{
22 Vector2 Min;
23 Vector2 Max;
24
25 Rect() : Min(FLT_MAX, FLT_MAX), Max(-FLT_MAX, -FLT_MAX) {}
26 Rect(const Vector2& min, const Vector2& max) : Min(min), Max(max) {}
27 Rect(const Vector4& v) : Min(v.x, v.y), Max(v.z, v.w) {}
28 Rect(float x1, float y1, float x2, float y2) : Min(x1, y1), Max(x2, y2) {}
29
30 Vector2 GetCenter() const { return Vector2((Min.x + Max.x)*0.5f, (Min.y + Max.y)*0.5f); }
31 Vector2 GetSize() const { return Vector2(Max.x - Min.x, Max.y - Min.y); }
32 float GetWidth() const { return Max.x - Min.x; }
33 float GetHeight() const { return Max.y - Min.y; }
34 Vector2 GetTL() const { return Min; } // Top-left
35 Vector2 GetTR() const { return Vector2(Max.x, Min.y); } // Top-right
36 Vector2 GetBL() const { return Vector2(Min.x, Max.y); } // Bottom-left
37 Vector2 GetBR() const { return Max; } // Bottom-right
38 bool Contains(const Vector2& p) const { return p.x >= Min.x && p.y >= Min.y && p.x < Max.x && p.y < Max.y; }
39 bool Contains(const Rect& r) const { return r.Min.x >= Min.x && r.Min.y >= Min.y && r.Max.x < Max.x && r.Max.y < Max.y; }
40 bool Overlaps(const Rect& r) const { return r.Min.y < Max.y && r.Max.y > Min.y && r.Min.x < Max.x && r.Max.x > Min.x; }
41 void Add(const Vector2& rhs) { if (Min.x > rhs.x) Min.x = rhs.x; if (Min.y > rhs.y) Min.y = rhs.y; if (Max.x < rhs.x) Max.x = rhs.x; if (Max.y < rhs.y) Max.y = rhs.y; }
42 void Add(const Rect& rhs) { if (Min.x > rhs.Min.x) Min.x = rhs.Min.x; if (Min.y > rhs.Min.y) Min.y = rhs.Min.y; if (Max.x < rhs.Max.x) Max.x = rhs.Max.x; if (Max.y < rhs.Max.y) Max.y = rhs.Max.y; }
43 void Expand(const float amount) { Min.x -= amount; Min.y -= amount; Max.x += amount; Max.y += amount; }
44 void Expand(const Vector2& amount) { Min.x -= amount.x; Min.y -= amount.y; Max.x += amount.x; Max.y += amount.y; }
45 void Reduce(const Vector2& amount) { Min.x += amount.x; Min.y += amount.y; Max.x -= amount.x; Max.y -= amount.y; }
46 void Clip(const Rect& clip) { if (Min.x < clip.Min.x) Min.x = clip.Min.x; if (Min.y < clip.Min.y) Min.y = clip.Min.y; if (Max.x > clip.Max.x) Max.x = clip.Max.x; if (Max.y > clip.Max.y) Max.y = clip.Max.y; }
47 void Floor() { Min.x = (float)(int)Min.x; Min.y = (float)(int)Min.y; Max.x = (float)(int)Max.x; Max.y = (float)(int)Max.y; }
48 Vector2 GetClosestPoint(Vector2 p, bool on_edge) const
49 {
50 if (!on_edge && Contains(p))
51 return p;
52 if (p.x > Max.x) p.x = Max.x;
53 else if (p.x < Min.x) p.x = Min.x;
54 if (p.y > Max.y) p.y = Max.y;
55 else if (p.y < Min.y) p.y = Min.y;
56 return p;
57 }
58
59};
60
61// Gui API
62namespace Gui
63{
64 // Main
65 GUI_API GuiIO& GetIO();
66 GUI_API GuiWindow& GetWindow();
67 GUI_API GuiStyle& GetStyle();
68 GUI_API void NewFrame(); // start a new Gui frame
69
70 // Window
71 GUI_API bool Begin();
72 GUI_API void End();
73
74 //Tabs
75 GUI_API bool Tab(const char* label, bool* p_state);
76 GUI_API void TabPop();
77
78 //Widget: Main
79 GUI_API bool Button(const char* label);
80 GUI_API bool SwitchCheck(const char* label, bool* p_state);
81 GUI_API bool ExpansionPanel(const char* label, bool* p_state);
82 GUI_API void ExpansionPanelPop();
83 GUI_API void Divider();
84
85 //Widget: Slider
86 GUI_API bool SliderFloat(const char* label, float* v, float v_min, float v_max, int precision = 3, const char* display_format = "");
87 GUI_API bool SliderInt(const char* label, int* v, int v_min, int v_max, const char* display_format = "");
88 GUI_API bool SliderDegree(const char* label, int* v, int v_min, int v_max, const char* display_format = "°");
89
90 // Utilities
91 GUI_API float GetTime();
92 GUI_API int GetFrameCount();
93
94 // Inputs
95 GUI_API int GetKeyIndex(int key);
96 GUI_API bool IsKeyDown(int key_index);
97 GUI_API bool IsKeyPressed(int key_index, bool repeat = true);
98 GUI_API bool IsKeyReleased(int key_index);
99 GUI_API bool IsMouseDown(int button); // is mouse button held
100 GUI_API bool IsMouseClicked(int button, bool repeat = false); // did mouse button clicked (went from !Down to Down)
101 GUI_API bool IsMouseDoubleClicked(int button); // did mouse button double-clicked. a double-click returns false in IsMouseClicked(). uses io.MouseDoubleClickTime.
102 GUI_API bool IsMouseReleased(int button); // did mouse button released (went from Down to !Down)
103 GUI_API bool IsMouseHoveringWindow(); // is mouse hovering current window ("window" in API names always refer to current window). disregarding of any consideration of being blocked by a popup. (unlike IsWindowHovered() this will return true even if the window is blocked because of a popup)
104 GUI_API bool IsMouseHoveringAnyWindow(); // is mouse hovering any visible window
105 GUI_API bool IsMouseHoveringRect(const Vector2& r_min, const Vector2& r_max, bool clip = true); // is mouse hovering given bounding rect (in screen space). clipped by current clipping settings. disregarding of consideration of focus/window ordering/blocked by a popup.
106 GUI_API bool IsMouseDragging(int button = 0, float lock_threshold = -1.0f); // is mouse dragging. if lock_threshold < -1.0f uses io.MouseDraggingThreshold
107 GUI_API Vector2 GetMousePos(); // shortcut to Gui::GetIO().MousePos provided by user, to be consistent with other calls
108
109 // Internal
110 GUI_API int FormatString(char* buf, int buf_size, const char* fmt, ...);
111 GUI_API const char* FindRenderedTextEnd(const char* text, const char* text_end);
112 //GUI_API void SetMaximumScrollForDrawFn();
113};
114
115// Communication with Gui. Access via Gui::GetOI()
116struct GuiIO
117{
118 //------------------------------------------------------------------
119 // Setting
120 //------------------------------------------------------------------
121 Vector2 DisplaySize;
122 float DeltaTime;
123 float MouseDoubleClickTime; // = 0.30f
124 float MouseDoubleClickMaxDist; // = 6.0f
125 float MouseDragThreshold; // = 6.0f
126 float KeyRepeatDelay; // = 0.250f
127 float KeyRepeatRate; // = 0.050f
128
129 //------------------------------------------------------------------
130 // Output - Retrieve after calling NewFrame()
131 //------------------------------------------------------------------
132 float Framerate; // Framerate estimnation, in frame per second.
133
134
135 //------------------------------------------------------------------
136 // Input
137 //------------------------------------------------------------------
138 Vector2 MousePos; // Mouse position, ini pixels
139 bool MouseDown[5]; // Mouse buttons: left, right, middle + extras,
140 float MouseWheel; // Mouse wheel: 1 unit scrolls about 5 line text.
141 bool MouseDrawCursor; // Request Gui to draw a mouse cursor for you
142 bool KeyCtrl; // Keyboard modifier pressed: Control
143 bool KeyShift; // Keyboard modifier pressed: Shift
144 bool KeyAlt; // Keyboard modifier pressed: Alt
145 bool KeysDown[512]; // Keyboard key that are pressed
146
147 //------------------------------------------------------------------
148 // [Internal] Gui
149 //------------------------------------------------------------------
150 Vector2 MousePosPrev; // Previous mouse position
151 Vector2 MouseDelta; // Mouse delat.
152 bool MouseClicked[5]; // Mouse button went from !Down to Down
153 bool MouseDragging;
154 Vector2 MouseDraggingTo;
155 Vector2 MouseClickedPos[5]; // Postion at time of clicking
156 float MouseClickedTime[5]; // Time of last click(used to figure out double-click)
157 bool MouseDoubleClicked[5]; // Has mouse button been double-clicked?
158 bool MouseReleased[5]; // Mouse button went from Down to !Down
159 float MouseDownDuration[5]; // Duration the mouse button has been down (0.0f == just clicked)
160 float MouseDownDurationPrev[5]; // Previous time the mouse button has been down (0.0f == just clicked)
161 float KeysDownDuration[512]; // Duration the keyboard key been down (0.0f == just clicked)
162 float KeysDownDurationPrev[512]; // Previous time the keyboard key has been down (0.0f = just clicked)
163
164 GUI_API GuiIO();
165};
166
167
168struct GuiWindow
169{
170 // HUB
171 Vector2 HubPos; // Hub position rounned-up to nearset pixel
172 Vector2 HubSize; // Hub current size
173 float BarWidth; // width of the titlebar
174 float BarWidthMaximumEverGet; // maximum width ever get
175 bool HubActive; // Set to true on begin();
176 bool HubWasActive; // Last state of the Hub
177
178 // Window
179 Vector2 Pos; // Position rounned-up to nearest pixel
180 Vector2 Size; // Curent size
181 int MinX; // 0
182 int MaxX; // 12
183 int MinY; // 12
184 int MaxY; // 12
185 bool Active; // Set to true if HubActive is false;
186 bool WasActive; // Last state of the window
187 Vector2 ItemsLastPos; // Last position of the item in menu
188 Vector2 ItemsCurrentPos; // Current position where item will be placed in menu
189 Vector2 ItemsLastSize;
190 int ItemsCount; // Count of item in the menu
191 int ItemsCountDisplay; // Count of item can be display in current window
192 int ItemsCreatedCount; // Maximum items can be display on the current window
193 bool ExpansionActive;
194 bool ExpansionWasActive;
195 bool ExpansionPop;
196
197 //Tabs
198 bool TabsActive;
199 int TabsItemsCount; // Current ItemsCount
200 int TabsItemsCountLast; // Last ItemsCount
201 Vector2 TabsLastPosBeforePop; // Last Positon before calling Pop
202 std::vector<bool> TabsArrayState; // Array of all state of the tabs
203 //Vector2 TabsSize; // Size of the tabs
204
205 //Scroll
206 bool ScrollNeeded; // if the scrollbar is needed (depend of item count)
207 bool ScrollWasActive;
208 Vector2 ScrollPos; // Starting position of the scrollbar
209 Vector2 ScrollSize; // Size of the scr ollbar
210 int ScrollState; // Return to value of state of the scrollbar
211 int ScrollStateLast;
212 Vector2 ScrollStateCount; // Count of possible state for the scrollbar
213 bool ScrollIsDragging;
214
215 //Drag
216 float DragWidth; // width of dragging
217
218public:
219 //GuiWindow();
220 //~GuiWindow();
221
222 //GuiID GetID(const void* ptr);
223 GUI_API GuiWindow();
224 //~GuiWindow();
225};
226
227struct GUI_API GuiStyle
228{
229 Vector2 HubPadding;
230 float HubItemsSpacing;
231 float ItemsOffset;
232 int ItemsMaximum;
233
234 //Tabs Style
235 float TabsItemsOffset;
236 float TabsX;
237
238 float OffsetFromScrollbar;
239 GUI_API GuiStyle();
240};
241
242//Main state for Gui
243struct GuiContext
244{
245 bool Initialized;
246 GuiIO IO;
247 GuiWindow Window;
248 GuiStyle Style;
249
250 float Time;
251 int FrameCount;
252 int FrameCountEnded;
253 int FrameCountRendered;
254
255 // Misc
256 float FramerateSecPerFrame[120]; // calculate estimate of framerate for user
257 int FramerateSecPerFrameIdx;
258 float FramerateSecPerFrameAccum;
259
260 GuiContext()
261 {
262 Initialized = false;
263 Time = 0.0f;
264 FrameCount = 0;
265 FrameCountEnded = FrameCountRendered = -1;
266
267 memset(FramerateSecPerFrame, 0, sizeof(FramerateSecPerFrame));
268 FramerateSecPerFrameIdx = 0;
269 FramerateSecPerFrameAccum = 0.0f;
270 }
271};
272
273
274extern GUI_API GuiContext* GGui;