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 TOYMAKERBUILTINS_UIBUTTON_H
13#define TOYMAKERBUILTINS_UIBUTTON_H
14
15#include <nlohmann/json.hpp>
16
19
21#include "nine_slice_panel.hpp"
22#include "ui_container.hpp"
23
24namespace ToyMaker {
25
96 class UIButton: public ToyMaker::SimObjectAspect<UIButton>, public IHoverable, public ILeftClickable, public IUIElement {
97 public:
102 enum State: uint8_t {
103 ACTIVE, //< This button is active, and ready to be pressed.
104 HOVER, //< This button is being hovered over by a pointer.
105 PRESSED, //< This button has been pressed and held down.
106 INACTIVE, //< This button is disabled and will not respond to pointer events.
107
108 //=============
109 TOTAL, //< The tally of all button states.
110 };
112
118 inline static std::string getSimObjectAspectTypeName() { return "UIButton"; }
119
126 static std::shared_ptr<BaseSimObjectAspect> create(const nlohmann::json& jsonAspectProperties);
127
133 std::shared_ptr<BaseSimObjectAspect> clone() const override;
134
139 void onActivated() override;
140
145 void enableButton();
146
151 void disableButton();
152
158 void updateText(const std::string& newText);
159
165 void updateTextScale(float scale);
166
172 void updateTextFont(const std::string& textResourceName);
173
179 void updateTextColor(glm::u8vec4 textColor);
180
186 void updateButtonAnchor(glm::vec2 newAnchor);
187
193 void updateHighlightColor(glm::vec4 newColor);
194
195 inline glm::vec2 getReferenceCoordinate() const override {
197 }
198
199 inline glm::vec3 getOffsets() const override {
200 return mOffsets;
201 }
202
203 inline bool allowsDescendantControl() const override {
204 return false;
205 }
206
207 private:
212 State mCurrentState { State::ACTIVE };
213
218 bool mHovered { false };
219
224 std::array<std::shared_ptr<NineSlicePanel>, State::TOTAL> mStatePanels {};
225
232 glm::vec2 mAnchor {.5f, .5f};
233
238 std::string mValue {""};
239
244 std::string mTextOverride {""};
245
251
256 std::string mTextFontOverride {""};
257
262 glm::u8vec4 mTextColorOverride { 0x00, 0x00, 0x00, 0xFF };
263
270 std::shared_ptr<NineSlicePanel> mHighlightPanel {};
271
276 glm::vec4 mHighlightColor {0.f, 0.f, 0.f, 0.f};
277
282 glm::vec2 mReferenceCoordinate { 0.f, 0.f };
283
289 glm::vec3 mOffsets { 0.f, 0.f, 0.f };
290
295 void recomputeTexture();
296
302 void updateButtonState(UIButton::State newState);
303
308 void fireStateEvent();
309
315 std::shared_ptr<ToyMaker::SimObject> getTextObject();
316 public:
317
323
329
335
340 ToyMaker::Signal<> mSigButtonActivated { *this, "ButtonActivated" };
341
346 ToyMaker::Signal<> mSigButtonDeactivated { *this, "ButtonDeactivated" };
347
348 private:
356 bool onPointerEnter(glm::vec4 pointerLocation) override;
357
365 inline bool onPointerHover(glm::vec4 pointerLocation) override { return false; }
366
373 bool onPointerLeave() override;
374
382 bool onPointerLeftClick(glm::vec4 pointerLocation) override;
383
391 bool onPointerLeftRelease(glm::vec4 pointerLocation) override;
392 };
393
394
396 NLOHMANN_JSON_SERIALIZE_ENUM(UIButton::State, {
397 {UIButton::State::ACTIVE, "active"},
398 {UIButton::State::HOVER, "hover"},
399 {UIButton::State::PRESSED, "pressed"},
400 {UIButton::State::INACTIVE, "inactive"},
401 });
402
403}
404
405#endif
The interface implemented by aspects which wish to respond to pointer hover related events.
Definition interface_pointer_callback.hpp:132
The interface used by aspects which wish to respond to mouse left click events (or equivalent).
Definition interface_pointer_callback.hpp:100
Interface for a single UI element.
Definition ui_container.hpp:18
A Signal object, designed to emit signals matching some data signature to be received by all the Sign...
Definition signals.hpp:326
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:96
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
glm::vec3 getOffsets() const override
Returns the offsets relative to the container reference coordinates used to position this element's a...
Definition ui_button.hpp:199
void updateTextScale(float scale)
Updates the scale of the text in this button.
Definition ui_button.cpp:159
ToyMaker::Signal< std::string > mSigButtonReleased
The signal fired when this button has just been released after being pressed.
Definition ui_button.hpp:328
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:148
void enableButton()
Enables the button so that it will respond to pointer events.
Definition ui_button.cpp:132
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:141
void updateHighlightColor(glm::vec4 newColor)
The color applied to this object's "highlight" texture.
Definition ui_button.cpp:174
std::string mTextFontOverride
The name of the font resource used to render this button's text.
Definition ui_button.hpp:256
glm::vec2 getReferenceCoordinate() const override
Returns the reference coordinate on the container relative to which this element's offsets are define...
Definition ui_button.hpp:195
void updateText(const std::string &newText)
Updates the text displayed within the button.
Definition ui_button.cpp:154
void updateTextColor(glm::u8vec4 textColor)
Updates the color of the text rendered by TextFont.
Definition ui_button.cpp:169
void fireStateEvent()
Fires an event based on the button's state. Related closely with updateButtonState().
Definition ui_button.cpp:180
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:270
State mCurrentState
The current state of this button.
Definition ui_button.hpp:212
ToyMaker::Signal< std::string > mSigButtonHoveredOver
The signal fired when a pointer just enters this button.
Definition ui_button.hpp:334
bool onPointerHover(glm::vec4 pointerLocation) override
Handler for the hover event where a pointer lingers in this button's area.
Definition ui_button.hpp:365
glm::vec2 mReferenceCoordinate
The reference coordinate on the owning container relative to which this element is positioned.
Definition ui_button.hpp:282
bool onPointerLeftClick(glm::vec4 pointerLocation) override
Handler for the hover event where a pointer presses on this button.
Definition ui_button.cpp:218
bool onPointerLeftRelease(glm::vec4 pointerLocation) override
Handler for the hover event where a pointer presses and releases this button.
Definition ui_button.cpp:225
void recomputeTexture()
The method responsible for recomputing this button's panel and text textures.
Definition ui_button.cpp:242
State
A list of states this button class supports.
Definition ui_button.hpp:102
ToyMaker::Signal mSigButtonDeactivated
The signal fired when this button is deactivated.
Definition ui_button.hpp:346
static std::string getSimObjectAspectTypeName()
Gets the aspect type string associated with this class.
Definition ui_button.hpp:118
void disableButton()
Disables this button, preventing it from responding to pointer events.
Definition ui_button.cpp:136
std::shared_ptr< ToyMaker::SimObject > getTextObject()
Gets the object which owns the text texture associated with this button.
Definition ui_button.cpp:322
float mTextScaleOverride
The scale of the rendered text used with this button.
Definition ui_button.hpp:250
bool allowsDescendantControl() const override
Whether the container is permitted to reposition this element's children.
Definition ui_button.hpp:203
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:121
bool mHovered
Whether this button is being hovered over presently.
Definition ui_button.hpp:218
std::array< std::shared_ptr< NineSlicePanel >, State::TOTAL > mStatePanels
The panel textures associated with the different states of this button.
Definition ui_button.hpp:224
bool onPointerLeave() override
Handler for the hover event where a pointer just leaves this button's area.
Definition ui_button.cpp:210
glm::u8vec4 mTextColorOverride
The color value with which this button's text is rendered.
Definition ui_button.hpp:262
std::shared_ptr< BaseSimObjectAspect > clone() const override
Creates an instance of the aspect using this this aspect as its blueprint.
Definition ui_button.cpp:104
glm::vec2 mAnchor
The anchor of the button, considered the StaticModel component's "origin".
Definition ui_button.hpp:232
void updateTextFont(const std::string &textResourceName)
Updates the font used to generate the text texture used by this button.
Definition ui_button.cpp:164
std::string mValue
The value emitted by this button when it has been pressed-and-released.
Definition ui_button.hpp:238
ToyMaker::Signal< std::string > mSigButtonPressed
The signal fired when this button has just been pressed (but not released).
Definition ui_button.hpp:322
glm::vec3 mOffsets
The offsets relative to the parent container's reference coordinate determining this element's positi...
Definition ui_button.hpp:289
std::string mTextOverride
The text rendered on this button.
Definition ui_button.hpp:244
ToyMaker::Signal mSigButtonActivated
The signal fired when this button is activated.
Definition ui_button.hpp:340
bool onPointerEnter(glm::vec4 pointerLocation) override
Handler for the hover event where a pointer just enters this button's area.
Definition ui_button.cpp:201
glm::vec4 mHighlightColor
The colour which the highlight object will be rendered with.
Definition ui_button.hpp:276
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:26
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...