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

A UI component class for creating simple buttons comprised of a resizable panel and some text. More...

#include <ui_button.hpp>

Inheritance diagram for UIButton:
ToyMaker::SimObjectAspect< UIButton > IHoverable ILeftClickable ToyMaker::BaseSimObjectAspect ToyMaker::SignalTracker ToyMaker::IActionHandler

Public Types

enum  State : uint8_t {
  ACTIVE , HOVER , PRESSED , INACTIVE ,
  TOTAL
}
 A list of states this button class supports.
 

Public Member Functions

std::shared_ptr< BaseSimObjectAspect > clone () const override
 Creates an instance of the aspect using this this aspect as its blueprint.
 
void onActivated () override
 Sets the state of this button and computes its texture on becoming an active part of the scene.
 
void enableButton ()
 Enables the button so that it will respond to pointer events.
 
void disableButton ()
 Disables this button, preventing it from responding to pointer events.
 
void updateText (const std::string &newText)
 Updates the text displayed within the button.
 
void updateTextScale (float scale)
 Updates the scale of the text in this button.
 
void updateTextFont (const std::string &textResourceName)
 Updates the font used to generate the text texture used by this button.
 
void updateTextColor (glm::u8vec4 textColor)
 Updates the color of the text rendered by TextFont.
 
void updateButtonAnchor (glm::vec2 newAnchor)
 Updates the point on the button StaticModel considered its anchor, where (0, 0) is the top left corner, and (1, 1) is the bottom right corner.
 
void updateHighlightColor (glm::vec4 newColor)
 The color applied to this object's "highlight" texture.
 
- Public Member Functions inherited from ToyMaker::BaseSimObjectAspect
virtual ~BaseSimObjectAspect ()=default
 Destroys this object.
 
virtual void simulationUpdate (uint32_t simStepMillis)
 Overriding this allows an aspect to respond to simulation updates.
 
virtual void variableUpdate (uint32_t variableStepMillis)
 Overriding this allows an aspect to respond to variable updates.
 
bool handleAction (const ActionData &actionData, const ActionDefinition &actionDefinition) override final
 Pipes an action received from the InputManager via our SimObject to all that action's handler methods on this aspect.
 
ViewportNodegetLocalViewport ()
 Returns the closest ancestor viewport to this node, if one exists (which it should, since this shouldn't be called until this aspect is attached to an active SimObject).
 
- Public Member Functions inherited from ToyMaker::SignalTracker
 SignalTracker ()
 Constructs a new SignalTracker object.
 
 SignalTracker (const SignalTracker &other)
 Constructs a new SignalTracker.
 
SignalTrackeroperator= (const SignalTracker &other)
 Copy assignment operator.
 
 SignalTracker (SignalTracker &&other)
 Moves resources from another SignalTracker into this one, invalidating them from the other.
 
SignalTrackeroperator= (SignalTracker &&other)
 Moves resources from another SignalTracker into this one, destroying this tracker's resources in the process.
 
void connect (const std::string &theirSignal, const std::string &ourObserver, SignalTracker &other)
 Method that connects one of this objects SignalObservers to another tracker's Signal.
 

Static Public Member Functions

static std::string getSimObjectAspectTypeName ()
 Gets the aspect type string associated with this class.
 
static std::shared_ptr< BaseSimObjectAspect > create (const nlohmann::json &jsonAspectProperties)
 Creates an instance of this aspect based on its description in JSON.
 

Public Attributes

ToyMaker::Signal< std::string > mSigButtonPressed { *this, "ButtonPressed" }
 The signal fired when this button has just been pressed (but not released).
 
ToyMaker::Signal< std::string > mSigButtonReleased { *this, "ButtonReleased" }
 The signal fired when this button has just been released after being pressed.
 
ToyMaker::Signal< std::string > mSigButtonHoveredOver { *this, "ButtonHoveredOver" }
 The signal fired when a pointer just enters this button.
 
ToyMaker::Signal mSigButtonActivated { *this, "ButtonActivated" }
 The signal fired when this button is activated.
 
ToyMaker::Signal mSigButtonDeactivated { *this, "ButtonDeactivated" }
 The signal fired when this button is deactivated.
 

Private Member Functions

void recomputeTexture ()
 The method responsible for recomputing this button's panel and text textures.
 
void updateButtonState (UIButton::State newState)
 The method through which all state changes pass, which is responsible for firing events related to those state changes.
 
void fireStateEvent ()
 Fires an event based on the button's state. Related closely with updateButtonState().
 
std::shared_ptr< ToyMaker::SimObjectgetTextObject ()
 Gets the object which owns the text texture associated with this button.
 
bool onPointerEnter (glm::vec4 pointerLocation) override
 Handler for the hover event where a pointer just enters this button's area.
 
bool onPointerLeave () override
 Handler for the hover event where a pointer just leaves this button's area.
 
bool onPointerLeftClick (glm::vec4 pointerLocation) override
 Handler for the hover event where a pointer presses on this button.
 
bool onPointerLeftRelease (glm::vec4 pointerLocation) override
 Handler for the hover event where a pointer presses and releases this button.
 

Private Attributes

State mCurrentState { State::ACTIVE }
 The current state of this button.
 
bool mHovered { false }
 Whether this button is being hovered over presently.
 
std::array< std::shared_ptr< NineSlicePanel >, State::TOTAL > mStatePanels {}
 The panel textures associated with the different states of this button.
 
glm::vec2 mAnchor {.5f, .5f}
 The anchor of the button, considered the StaticModel component's "origin".
 
std::string mValue {""}
 The value emitted by this button when it has been pressed-and-released.
 
std::string mTextOverride {""}
 The text rendered on this button.
 
float mTextScaleOverride {1.f}
 The scale of the rendered text used with this button.
 
std::string mTextFontOverride {""}
 The name of the font resource used to render this button's text.
 
glm::u8vec4 mTextColorOverride { 0x00, 0x00, 0x00, 0xFF }
 The color value with which this button's text is rendered.
 
std::shared_ptr< NineSlicePanelmHighlightPanel {}
 The texture used on this object's child object, which acts as an overlay to this button.
 
glm::vec4 mHighlightColor {0.f, 0.f, 0.f, 0.f}
 The colour which the highlight object will be rendered with.
 

Additional Inherited Members

- Protected Member Functions inherited from ToyMaker::SimObjectAspect< UIButton >
 SimObjectAspect (int explicitlyInitializeMe)
 
- Protected Member Functions inherited from ToyMaker::BaseSimObjectAspect
 BaseSimObjectAspect (const BaseSimObjectAspect &other)=delete
 
 BaseSimObjectAspect (BaseSimObjectAspect &&other)=delete
 
SimObjectgetSimObject ()
 Returns the sim object that this aspect is attached to.
 
template<typename TComponent>
void addComponent (const TComponent &component)
 Adds a component of some type to the underlying entity.
 
template<typename TComponent>
bool hasComponent ()
 Tests whether a component of some specific type is present on the object.
 
template<typename TComponent>
void updateComponent (const TComponent &component)
 Updates the value of a component belonging to this object to a new one.
 
template<typename TComponent>
TComponent getComponent (const float simulationProgress=1.f) const
 Gets the value of a component belonging to this object.
 
template<typename TComponent>
void removeComponent ()
 Removes a component of some type belonging to the underlying SimObject.
 
void addAspect (const nlohmann::json &jsonAspectProperties)
 Adds a new aspect to the underlying SimObject constructed based on its properties in JSON.
 
void addAspect (const BaseSimObjectAspect &aspect)
 Adds a new aspect to the underlying SimObject copied from an already existing aspect.
 
template<typename TSimObjectAspect>
bool hasAspect () const
 Tests whether an aspect of a particular type is attached to the underlying SimObject.
 
bool hasAspect (const std::string &aspectType) const
 Tests whether an aspect of a particular type is attached to the underlying SimObject.
 
template<typename TSimObjectAspect>
TSimObjectAspect & getAspect ()
 Gets an aspect of a particular type belonging to the underlying SimObject.
 
BaseSimObjectAspectgetAspect (const std::string &aspectType)
 Gets (a base class reference to) an aspect of a particular type belonging to the underlying SimObject.
 
template<typename TSimObjectAspect>
void removeAspect ()
 Removes an aspect from the underlying SimObject.
 
void addOrReplaceAspect (const BaseSimObjectAspect &aspect)
 Adds or replaces an aspect on the underlying SimObject with a new aspect constructed as a copy of another.
 
void addOrReplaceAspect (const nlohmann::json &jsonAspectProperties)
 Adds or replaces an aspect on the underlying SimObject with a new aspect constructed from its JSON description.
 
std::weak_ptr< FixedActionBindingdeclareFixedActionBinding (const std::string &context, const std::string &action, std::function< bool(const ActionData &, const ActionDefinition &)>)
 Binds some method (or any function) present on this object to an action generated by the InputManager.
 
EntityID getEntityID () const
 Gets the ID of the ECSWorld Entity belonging to our SimObject.
 
std::weak_ptr< ECSWorldgetWorld () const
 Gets a weak reference to the ECSWorld to which our SimObject's entity belongs.
 
- Static Protected Member Functions inherited from ToyMaker::BaseSimObjectAspect
template<typename TSimObjectAspectDerived>
static void registerAspect ()
 Registers an implementation of an aspect with the SimSystem.
 

Detailed Description

A UI component class for creating simple buttons comprised of a resizable panel and some text.

May optionally also have a "highlight" texture configured to overlay over a button in order to represent special application defined states.

Upon receiving a pointer left button event from an implementer of ILeftClickable, emits a "ButtonPressed" or "ButtonReleased" event containing the value configured for the button.

Usage

Its appearance in JSON may be as follows:

{
"name": "Ur_Button",
"parent": "/viewport_UI/",
"type": "Scene",
"copy": true,
"overrides": {
"name": "return",
"components": [
{
"type": "Placement",
"orientation": [ 1.0, 0.0, 0.0, 0.0 ],
"position": [ -683.0, -384.0, 0.0, 1.0 ],
"scale": [ 1.0, 1.0, 1.0 ]
}
],
"aspects": [
{
"type": "UIButton",
"text": "<- Main Menu",
"font_resource_name": "Roboto_Mono_Regular_24",
"color": [255, 255, 255, 255],
"scale": 1.0,
"anchor": [0.0, 1.0],
"value": "Game_Of_Ur_Main_Menu",
"panel_active": "Bad_Button_Active_Panel",
"panel_inactive": "Bad_Button_Inactive_Panel",
"panel_hover": "Bad_Button_Hover_Panel",
"panel_pressed": "Bad_Button_Pressed_Panel",
"has_highlight": false
}
]
}
},

To connect with, for example, the "ButtonReleased" event for this button, write this in the connections section of the appropriate scene file.

{
"from": "/viewport_UI/return/@UIButton",
"signal": "ButtonReleased",
"to": "/@UrUINavigation",
"observer": "ButtonClickedObserved"
}

... where "/@UrUINavigation" is the path to the aspect of the SimObject responsible for handling this signal, and "ButtonClickedObserved" is the name of its SignalObserver.

The button may also be enabled or disabled by calling UIButton::enableButton() or UIButton::disableButton().

Member Function Documentation

◆ clone()

std::shared_ptr< ToyMaker::BaseSimObjectAspect > UIButton::clone ( ) const
overridevirtual

Creates an instance of the aspect using this this aspect as its blueprint.

Returns
std::shared_ptr<BaseSimObjectAspect> The newly constructed aspect.

Implements ToyMaker::BaseSimObjectAspect.

◆ create()

std::shared_ptr< ToyMaker::BaseSimObjectAspect > UIButton::create ( const nlohmann::json & jsonAspectProperties)
static

Creates an instance of this aspect based on its description in JSON.

Parameters
jsonAspectPropertiesThis aspect's description in JSON.
Returns
std::shared_ptr<BaseSimObjectAspect> The newly constructed aspect.

◆ getSimObjectAspectTypeName()

static std::string UIButton::getSimObjectAspectTypeName ( )
inlinestatic

Gets the aspect type string associated with this class.

Returns
std::string This class' aspect type string.

◆ getTextObject()

std::shared_ptr< ToyMaker::SimObject > UIButton::getTextObject ( )
private

Gets the object which owns the text texture associated with this button.

Returns
std::shared_ptr<ToyMaker::SimObject> The object with this button's text texture.

◆ onActivated()

void UIButton::onActivated ( )
overridevirtual

Sets the state of this button and computes its texture on becoming an active part of the scene.

Reimplemented from ToyMaker::BaseSimObjectAspect.

◆ onPointerEnter()

bool UIButton::onPointerEnter ( glm::vec4 pointerLocation)
overrideprivatevirtual

Handler for the hover event where a pointer just enters this button's area.

Parameters
pointerLocationThe location of the intersection between the pointer raycast and this object.
Return values
trueThis button handled the pointer enter event.
falseThe button did not handle the pointer enter event.

Implements IHoverable.

◆ onPointerLeave()

bool UIButton::onPointerLeave ( )
overrideprivatevirtual

Handler for the hover event where a pointer just leaves this button's area.

Return values
trueThis button handled the pointer leave event.
falseThis button didn't handle the pointer leave event.

Implements IHoverable.

◆ onPointerLeftClick()

bool UIButton::onPointerLeftClick ( glm::vec4 pointerLocation)
overrideprivatevirtual

Handler for the hover event where a pointer presses on this button.

Parameters
pointerLocationThe location of the intersection of the pointer ray and this object.
Return values
trueThis button handled the pointer button press event.
falseThis button did not handle the pointer button press event.

Implements ILeftClickable.

◆ onPointerLeftRelease()

bool UIButton::onPointerLeftRelease ( glm::vec4 pointerLocation)
overrideprivatevirtual

Handler for the hover event where a pointer presses and releases this button.

Parameters
pointerLocationThe location of the intersection of the pointer ray and this object.
Return values
trueThis button handled the pointer button release event.
falseThis button did not handle the pointer button release event.

Implements ILeftClickable.

◆ updateButtonAnchor()

void UIButton::updateButtonAnchor ( glm::vec2 newAnchor)

Updates the point on the button StaticModel considered its anchor, where (0, 0) is the top left corner, and (1, 1) is the bottom right corner.

Parameters
newAnchorThe new anchor (origin) for this UI element.

◆ updateButtonState()

void UIButton::updateButtonState ( UIButton::State newState)
private

The method through which all state changes pass, which is responsible for firing events related to those state changes.

Parameters
newStateThe new state the button is transitioning to.

◆ updateHighlightColor()

void UIButton::updateHighlightColor ( glm::vec4 newColor)

The color applied to this object's "highlight" texture.

Parameters
newColorThe new color of the highligh sub object of this button.

◆ updateText()

void UIButton::updateText ( const std::string & newText)

Updates the text displayed within the button.

Parameters
newTextThe new text displayed by this button.

◆ updateTextColor()

void UIButton::updateTextColor ( glm::u8vec4 textColor)

Updates the color of the text rendered by TextFont.

Parameters
textColorThe new color with which to render the text, each component specified as a value between 0-255.

◆ updateTextFont()

void UIButton::updateTextFont ( const std::string & textResourceName)

Updates the font used to generate the text texture used by this button.

Parameters
textResourceNameThe resource name of the new TextFont resource this button should use.

◆ updateTextScale()

void UIButton::updateTextScale ( float scale)

Updates the scale of the text in this button.

Parameters
scaleThe multiplier by which this button's text texture is scaled.

Member Data Documentation

◆ mAnchor

glm::vec2 UIButton::mAnchor {.5f, .5f}
private

The anchor of the button, considered the StaticModel component's "origin".

(0, 0) represents the top left corner of the button, and (1, 1) represents the bottom right corner.

◆ mHighlightPanel

std::shared_ptr<NineSlicePanel> UIButton::mHighlightPanel {}
private

The texture used on this object's child object, which acts as an overlay to this button.

This overlay can be used to communicate special information not covered by any currently existing button states.


The documentation for this class was generated from the following files: