ToyMaker Game Engine 0.0.2
ToyMaker is a game engine developed and maintained by Zoheb Shujauddin.
Loading...
Searching...
No Matches
ui_button.hpp
Go to the documentation of this file.
1
11
12#ifndef ZOAPPUIBUTTON_H
13#define ZOAPPUIBUTTON_H
14
15#include <nlohmann/json.hpp>
16
19
22
23namespace ToyMaker {
24
93 class UIButton: public ToyMaker::SimObjectAspect<UIButton>, public IHoverable, public ILeftClickable {
94 public:
99 enum State: uint8_t {
100 ACTIVE, //< This button is active, and ready to be pressed.
101 HOVER, //< This button is being hovered over by a pointer.
102 PRESSED, //< This button has been pressed and held down.
103 INACTIVE, //< This button is disabled and will not respond to pointer events.
104
105 //=============
106 TOTAL, //< The tally of all button states.
107 };
109
115 inline static std::string getSimObjectAspectTypeName() { return "UIButton"; }
116
123 static std::shared_ptr<BaseSimObjectAspect> create(const nlohmann::json& jsonAspectProperties);
124
130 std::shared_ptr<BaseSimObjectAspect> clone() const override;
131
136 void onActivated() override;
137
142 void enableButton();
143
148 void disableButton();
149
155 void updateText(const std::string& newText);
156
162 void updateTextScale(float scale);
163
169 void updateTextFont(const std::string& textResourceName);
170
176 void updateTextColor(glm::u8vec4 textColor);
177
183 void updateButtonAnchor(glm::vec2 newAnchor);
184
190 void updateHighlightColor(glm::vec4 newColor);
191
192 private:
197 State mCurrentState { State::ACTIVE };
198
203 bool mHovered { false };
204
209 std::array<std::shared_ptr<NineSlicePanel>, State::TOTAL> mStatePanels {};
210
217 glm::vec2 mAnchor {.5f, .5f};
218
223 std::string mValue {""};
224
229 std::string mTextOverride {""};
230
236
241 std::string mTextFontOverride {""};
242
247 glm::u8vec4 mTextColorOverride { 0x00, 0x00, 0x00, 0xFF };
248
255 std::shared_ptr<NineSlicePanel> mHighlightPanel {};
256
261 glm::vec4 mHighlightColor {0.f, 0.f, 0.f, 0.f};
262
267 void recomputeTexture();
268
274 void updateButtonState(UIButton::State newState);
275
280 void fireStateEvent();
281
287 std::shared_ptr<ToyMaker::SimObject> getTextObject();
288 public:
289
295
301
307
312 ToyMaker::Signal<> mSigButtonActivated { *this, "ButtonActivated" };
313
318 ToyMaker::Signal<> mSigButtonDeactivated { *this, "ButtonDeactivated" };
319
320 private:
328 bool onPointerEnter(glm::vec4 pointerLocation) override;
329
336 bool onPointerLeave() override;
337
345 bool onPointerLeftClick(glm::vec4 pointerLocation) override;
346
354 bool onPointerLeftRelease(glm::vec4 pointerLocation) override;
355 };
356
357
359 NLOHMANN_JSON_SERIALIZE_ENUM(UIButton::State, {
360 {UIButton::State::ACTIVE, "active"},
361 {UIButton::State::HOVER, "hover"},
362 {UIButton::State::PRESSED, "pressed"},
363 {UIButton::State::INACTIVE, "inactive"},
364 });
365
366}
367
368#endif
The interface implemented by aspects which wish to respond to pointer hover related events.
Definition interface_pointer_callback.hpp:121
The interface used by aspects which wish to respond to mouse left click events (or equivalent).
Definition interface_pointer_callback.hpp:89
A Signal object, designed to emit signals matching some data signature to be received by all the Sign...
Definition signals.hpp:323
An object containing closely related methods and data, and exposing object lifecycle and application ...
Definition sim_system.hpp:956
A UI component class for creating simple buttons comprised of a resizable panel and some text.
Definition ui_button.hpp:93
static std::shared_ptr< BaseSimObjectAspect > create(const nlohmann::json &jsonAspectProperties)
Creates an instance of this aspect based on its description in JSON.
Definition ui_button.cpp:6
void updateTextScale(float scale)
Updates the scale of the text in this button.
Definition ui_button.cpp:146
ToyMaker::Signal< std::string > mSigButtonReleased
The signal fired when this button has just been released after being pressed.
Definition ui_button.hpp:300
void updateButtonAnchor(glm::vec2 newAnchor)
Updates the point on the button StaticModel considered its anchor, where (0, 0) is the top left corne...
Definition ui_button.cpp:135
void enableButton()
Enables the button so that it will respond to pointer events.
Definition ui_button.cpp:119
void updateButtonState(UIButton::State newState)
The method through which all state changes pass, which is responsible for firing events related to th...
Definition ui_button.cpp:128
void updateHighlightColor(glm::vec4 newColor)
The color applied to this object's "highlight" texture.
Definition ui_button.cpp:161
std::string mTextFontOverride
The name of the font resource used to render this button's text.
Definition ui_button.hpp:241
void updateText(const std::string &newText)
Updates the text displayed within the button.
Definition ui_button.cpp:141
void updateTextColor(glm::u8vec4 textColor)
Updates the color of the text rendered by TextFont.
Definition ui_button.cpp:156
void fireStateEvent()
Fires an event based on the button's state. Related closely with updateButtonState().
Definition ui_button.cpp:167
std::shared_ptr< NineSlicePanel > mHighlightPanel
The texture used on this object's child object, which acts as an overlay to this button.
Definition ui_button.hpp:255
State mCurrentState
The current state of this button.
Definition ui_button.hpp:197
ToyMaker::Signal< std::string > mSigButtonHoveredOver
The signal fired when a pointer just enters this button.
Definition ui_button.hpp:306
bool onPointerLeftClick(glm::vec4 pointerLocation) override
Handler for the hover event where a pointer presses on this button.
Definition ui_button.cpp:205
bool onPointerLeftRelease(glm::vec4 pointerLocation) override
Handler for the hover event where a pointer presses and releases this button.
Definition ui_button.cpp:212
void recomputeTexture()
The method responsible for recomputing this button's panel and text textures.
Definition ui_button.cpp:229
State
A list of states this button class supports.
Definition ui_button.hpp:99
ToyMaker::Signal mSigButtonDeactivated
The signal fired when this button is deactivated.
Definition ui_button.hpp:318
static std::string getSimObjectAspectTypeName()
Gets the aspect type string associated with this class.
Definition ui_button.hpp:115
void disableButton()
Disables this button, preventing it from responding to pointer events.
Definition ui_button.cpp:123
std::shared_ptr< ToyMaker::SimObject > getTextObject()
Gets the object which owns the text texture associated with this button.
Definition ui_button.cpp:308
float mTextScaleOverride
The scale of the rendered text used with this button.
Definition ui_button.hpp:235
void onActivated() override
Sets the state of this button and computes its texture on becoming an active part of the scene.
Definition ui_button.cpp:108
bool mHovered
Whether this button is being hovered over presently.
Definition ui_button.hpp:203
std::array< std::shared_ptr< NineSlicePanel >, State::TOTAL > mStatePanels
The panel textures associated with the different states of this button.
Definition ui_button.hpp:209
bool onPointerLeave() override
Handler for the hover event where a pointer just leaves this button's area.
Definition ui_button.cpp:197
glm::u8vec4 mTextColorOverride
The color value with which this button's text is rendered.
Definition ui_button.hpp:247
std::shared_ptr< BaseSimObjectAspect > clone() const override
Creates an instance of the aspect using this this aspect as its blueprint.
Definition ui_button.cpp:93
glm::vec2 mAnchor
The anchor of the button, considered the StaticModel component's "origin".
Definition ui_button.hpp:217
void updateTextFont(const std::string &textResourceName)
Updates the font used to generate the text texture used by this button.
Definition ui_button.cpp:151
std::string mValue
The value emitted by this button when it has been pressed-and-released.
Definition ui_button.hpp:223
ToyMaker::Signal< std::string > mSigButtonPressed
The signal fired when this button has just been pressed (but not released).
Definition ui_button.hpp:294
std::string mTextOverride
The text rendered on this button.
Definition ui_button.hpp:229
ToyMaker::Signal mSigButtonActivated
The signal fired when this button is activated.
Definition ui_button.hpp:312
bool onPointerEnter(glm::vec4 pointerLocation) override
Handler for the hover event where a pointer just enters this button's area.
Definition ui_button.cpp:188
glm::vec4 mHighlightColor
The colour which the highlight object will be rendered with.
Definition ui_button.hpp:261
Contains classes that serve as interfaces for sim objects that wish to respond to click events.
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition application.hpp:24
Contains class defining this project's implementation of nine-slice (or nine-region) resizable panels...
Classes relating to this engine's implementation of signals. Contains template classes used to define...
Classes and structs relating to the SimSystem, the system responsible for providing some level of gen...