Game of Ur 0.3.3
This is a computer adaptation of Game of Ur, written in C++ mainly using SDL and OpenGL.
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
17#include "toymaker/sim_system.hpp"
18#include "toymaker/signals.hpp"
19
21#include "nine_slice_panel.hpp"
22
91class UIButton: public ToyMaker::SimObjectAspect<UIButton>, public IHoverable, public ILeftClickable {
92public:
97 enum State: uint8_t {
98 ACTIVE, //< This button is active, and ready to be pressed.
99 HOVER, //< This button is being hovered over by a pointer.
100 PRESSED, //< This button has been pressed and held down.
101 INACTIVE, //< This button is disabled and will not respond to pointer events.
102
103 //=============
104 TOTAL, //< The tally of all button states.
105 };
106 UIButton(): SimObjectAspect<UIButton>{0} {}
107
113 inline static std::string getSimObjectAspectTypeName() { return "UIButton"; }
114
121 static std::shared_ptr<BaseSimObjectAspect> create(const nlohmann::json& jsonAspectProperties);
122
128 std::shared_ptr<BaseSimObjectAspect> clone() const override;
129
134 void onActivated() override;
135
140 void enableButton();
141
146 void disableButton();
147
153 void updateText(const std::string& newText);
154
160 void updateTextScale(float scale);
161
167 void updateTextFont(const std::string& textResourceName);
168
174 void updateTextColor(glm::u8vec4 textColor);
175
181 void updateButtonAnchor(glm::vec2 newAnchor);
182
188 void updateHighlightColor(glm::vec4 newColor);
189
190private:
195 State mCurrentState { State::ACTIVE };
196
201 bool mHovered { false };
202
207 std::array<std::shared_ptr<NineSlicePanel>, State::TOTAL> mStatePanels {};
208
215 glm::vec2 mAnchor {.5f, .5f};
216
221 std::string mValue {""};
222
227 std::string mTextOverride {""};
228
234
239 std::string mTextFontOverride {""};
240
245 glm::u8vec4 mTextColorOverride { 0x00, 0x00, 0x00, 0xFF };
246
253 std::shared_ptr<NineSlicePanel> mHighlightPanel {};
254
259 glm::vec4 mHighlightColor {0.f, 0.f, 0.f, 0.f};
260
265 void recomputeTexture();
266
272 void updateButtonState(UIButton::State newState);
273
278 void fireStateEvent();
279
285 std::shared_ptr<ToyMaker::SimObject> getTextObject();
286public:
287
293
299
305
310 ToyMaker::Signal<> mSigButtonActivated { *this, "ButtonActivated" };
311
316 ToyMaker::Signal<> mSigButtonDeactivated { *this, "ButtonDeactivated" };
317
318private:
326 bool onPointerEnter(glm::vec4 pointerLocation) override;
327
334 bool onPointerLeave() override;
335
343 bool onPointerLeftClick(glm::vec4 pointerLocation) override;
344
352 bool onPointerLeftRelease(glm::vec4 pointerLocation) override;
353};
354
355
357NLOHMANN_JSON_SERIALIZE_ENUM(UIButton::State, {
358 {UIButton::State::ACTIVE, "active"},
359 {UIButton::State::HOVER, "hover"},
360 {UIButton::State::PRESSED, "pressed"},
361 {UIButton::State::INACTIVE, "inactive"},
362});
363
364#endif
The interface implemented by aspects which wish to respond to pointer hover related events.
Definition interface_pointer_callback.hpp:120
The interface used by aspects which wish to respond to mouse left click events (or equivalent).
Definition interface_pointer_callback.hpp:88
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:91
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:4
void updateTextScale(float scale)
Updates the scale of the text in this button.
Definition ui_button.cpp:144
State
A list of states this button class supports.
Definition ui_button.hpp:97
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:133
void enableButton()
Enables the button so that it will respond to pointer events.
Definition ui_button.cpp:117
ToyMaker::Signal< std::string > mSigButtonPressed
The signal fired when this button has just been pressed (but not released).
Definition ui_button.hpp:292
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:126
void updateHighlightColor(glm::vec4 newColor)
The color applied to this object's "highlight" texture.
Definition ui_button.cpp:159
void updateText(const std::string &newText)
Updates the text displayed within the button.
Definition ui_button.cpp:139
static std::string getSimObjectAspectTypeName()
Gets the aspect type string associated with this class.
Definition ui_button.hpp:113
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:253
ToyMaker::Signal mSigButtonActivated
The signal fired when this button is activated.
Definition ui_button.hpp:310
void updateTextColor(glm::u8vec4 textColor)
Updates the color of the text rendered by TextFont.
Definition ui_button.cpp:154
void fireStateEvent()
Fires an event based on the button's state. Related closely with updateButtonState().
Definition ui_button.cpp:165
float mTextScaleOverride
The scale of the rendered text used with this button.
Definition ui_button.hpp:233
ToyMaker::Signal< std::string > mSigButtonHoveredOver
The signal fired when a pointer just enters this button.
Definition ui_button.hpp:304
ToyMaker::Signal< std::string > mSigButtonReleased
The signal fired when this button has just been released after being pressed.
Definition ui_button.hpp:298
std::string mTextFontOverride
The name of the font resource used to render this button's text.
Definition ui_button.hpp:239
bool onPointerLeftClick(glm::vec4 pointerLocation) override
Handler for the hover event where a pointer presses on this button.
Definition ui_button.cpp:203
bool onPointerLeftRelease(glm::vec4 pointerLocation) override
Handler for the hover event where a pointer presses and releases this button.
Definition ui_button.cpp:210
void recomputeTexture()
The method responsible for recomputing this button's panel and text textures.
Definition ui_button.cpp:227
void disableButton()
Disables this button, preventing it from responding to pointer events.
Definition ui_button.cpp:121
std::array< std::shared_ptr< NineSlicePanel >, State::TOTAL > mStatePanels
The panel textures associated with the different states of this button.
Definition ui_button.hpp:207
std::shared_ptr< ToyMaker::SimObject > getTextObject()
Gets the object which owns the text texture associated with this button.
Definition ui_button.cpp:306
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:106
std::string mTextOverride
The text rendered on this button.
Definition ui_button.hpp:227
glm::u8vec4 mTextColorOverride
The color value with which this button's text is rendered.
Definition ui_button.hpp:245
bool onPointerLeave() override
Handler for the hover event where a pointer just leaves this button's area.
Definition ui_button.cpp:195
State mCurrentState
The current state of this button.
Definition ui_button.hpp:195
glm::vec4 mHighlightColor
The colour which the highlight object will be rendered with.
Definition ui_button.hpp:259
std::shared_ptr< BaseSimObjectAspect > clone() const override
Creates an instance of the aspect using this this aspect as its blueprint.
Definition ui_button.cpp:91
glm::vec2 mAnchor
The anchor of the button, considered the StaticModel component's "origin".
Definition ui_button.hpp:215
ToyMaker::Signal mSigButtonDeactivated
The signal fired when this button is deactivated.
Definition ui_button.hpp:316
void updateTextFont(const std::string &textResourceName)
Updates the font used to generate the text texture used by this button.
Definition ui_button.cpp:149
std::string mValue
The value emitted by this button when it has been pressed-and-released.
Definition ui_button.hpp:221
bool onPointerEnter(glm::vec4 pointerLocation) override
Handler for the hover event where a pointer just enters this button's area.
Definition ui_button.cpp:186
bool mHovered
Whether this button is being hovered over presently.
Definition ui_button.hpp:201
Contains classes that serve as interfaces for sim objects that wish to respond to click events.
Contains class defining this project's implementation of nine-slice (or nine-region) resizable panels...