Wrath of Zeus
Made by Torchlight Games for CSE 125 SP24
Loading...
Searching...
No Matches
widget.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "client/core.hpp"
6
7#include <memory>
8#include <unordered_set>
9#include <string>
10#include <functional>
11#include <unordered_map>
12
13namespace gui::widget {
14
16using Handle = std::size_t;
18using CallbackHandle = std::size_t;
22using Callback = std::function<void(Handle)>;
23
41class Widget {
42public:
45 using Ptr = std::unique_ptr<Widget>;
46
51
58 explicit Widget(Type type, glm::vec2 origin);
59 virtual ~Widget() = default;
69 void setOrigin(glm::vec2 origin);
76 virtual void lock() {}
78
83
128 virtual void doClick(float x, float y);
135 virtual void doHover(float x, float y);
137
139
144 virtual void render() = 0;
146
148
152 [[nodiscard]] Type getType() const;
157 [[nodiscard]] const glm::vec2& getOrigin() const;
162 [[nodiscard]] std::pair<std::size_t, std::size_t> getSize() const;
167 [[nodiscard]] Handle getHandle() const;
169
184
193 [[nodiscard]] virtual bool hasHandle(Handle handle) const;
202 [[nodiscard]] virtual Widget* borrow(Handle handle);
204
205protected:
208
211 static std::size_t num_widgets;
212
215
217 glm::vec2 origin;
218
222
224 std::size_t width {0};
226 std::size_t height {0};
227
229 std::unordered_map<CallbackHandle, Callback> on_clicks;
231 std::unordered_map<CallbackHandle, Callback> on_hovers;
232
233private:
235 CallbackHandle next_click_handle {0};
237 CallbackHandle next_hover_handle {0};
238
240 bool _doesIntersect(float x, float y) const;
241};
242
243
244}
Definition: widget.hpp:41
void setOrigin(glm::vec2 origin)
Overrides the current origin position with the newly specified one.
Definition: widget.cpp:15
void removeOnHover(CallbackHandle handle)
Removes the onHover callback function associated with the given handle.
Definition: widget.cpp:40
static std::size_t num_widgets
Static counter for the number of widgets that have been created, so a new identifier can be assigned ...
Definition: widget.hpp:211
std::unique_ptr< Widget > Ptr
Definition: widget.hpp:45
virtual void render()=0
======================================================================================
std::unordered_map< CallbackHandle, Callback > on_hovers
all of the onHover handlers, indexable by handle
Definition: widget.hpp:231
Type type
Type of the widget.
Definition: widget.hpp:214
virtual void doHover(float x, float y)
Performs a hover action at the specified (x,y) position in the GUI coordinate frame.
Definition: widget.cpp:52
glm::vec2 origin
Origin position (bottom left) of the widget in GUI coordinates.
Definition: widget.hpp:217
virtual bool hasHandle(Handle handle) const
======================================================================================
Definition: widget.cpp:81
Type getType() const
======================================================================================
Definition: widget.cpp:60
virtual void doClick(float x, float y)
Performs a click at the specified (x,y) position in the GUI coordinate frame.
Definition: widget.cpp:44
CallbackHandle addOnHover(Callback callback)
Register a new onHover callback function for this widget.
Definition: widget.cpp:30
std::pair< std::size_t, std::size_t > getSize() const
Queries the size of the widget in pixels.
Definition: widget.cpp:64
Handle getHandle() const
Queries the handle for this specific widget.
Definition: widget.cpp:77
virtual ~Widget()=default
Handle handle
======================================================================================
Definition: widget.hpp:207
virtual Widget * borrow(Handle handle)
Gives a pointer to the subwidget specified by the handle.
Definition: widget.cpp:85
void removeOnClick(CallbackHandle handle)
Removes the onClick callback function associated with the given handle.
Definition: widget.cpp:36
CallbackHandle addOnClick(Callback callback)
======================================================================================
Definition: widget.cpp:23
const glm::vec2 & getOrigin() const
Queries the origin position of the widget.
Definition: widget.cpp:19
virtual void lock()
Function that is run after all of the widgets have been added to the screee but before any input hand...
Definition: widget.hpp:76
std::unordered_map< CallbackHandle, Callback > on_clicks
all of the onClick handlers, indexable by handle
Definition: widget.hpp:229
GLint GLsizei width
Definition: glad.h:1516
GLint GLenum GLint x
Definition: glad.h:1655
GLint GLsizei GLsizei height
Definition: glad.h:1516
GLint y
Definition: glad.h:1516
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glad.h:1531
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
Definition: centertext.hpp:6
std::function< void(Handle)> Callback
Type for a onClick or onHover callback function. The Handle parameter is the handle to the widget tha...
Definition: widget.hpp:22
std::size_t Handle
Type for a Widget Handle.
Definition: widget.hpp:16
Type
Definition: type.hpp:5
std::size_t CallbackHandle
Type for the handle for a specific callback function.
Definition: widget.hpp:18