SUNENGINE 0.0.2
A simple and bright C++ game engine.
 
Loading...
Searching...
No Matches
Button Class Reference

A clickable UI button component. More...

#include <Button.h>

Inheritance diagram for Button:
Inheritance graph

Public Member Functions

 Button ()=default
 Default constructor. Initializes the button with default values.
 
 Button (const Button &)=delete
 
Buttonoperator= (const Button &)=delete
 
 Button (std::string key)
 Constructor that initializes the button with a texture key.
 
void SetOrigin (float x, float y)
 Sets the origin of the button.
 
void SetPosition (float x, float y)
 Sets the position of the button.
 
void SetAngleRadians (float rad)
 Sets the angle of the button in radians.
 
void SetAngleDegrees (float deg)
 Sets the angle of the button in degrees.
 
void SetScaleFactor (float x, float y)
 Sets the scale of the button.
 
void SetScalePixel (float x, float y)
 Sets the scale of the button in pixels.
 
float GetWidth () const
 Gets the width of the button.
 
float GetHeight () const
 Gets the height of the button.
 
float GetAngle () const
 Gets the angle of the button in radians.
 
float GetScaleX () const
 Gets the x-scale factor of the button.
 
float GetScaleY () const
 Gets the y-scale factor of the button.
 
float GetPosX () const
 Gets the x-coordinate of the button's position.
 
float GetPosY () const
 Gets the y-coordinate of the button's position.
 
void SetOnPressCommand (UICommand *cmd)
 Sets the command to be executed when the button is pressed.
 
void SetOnHoverCommand (UICommand *cmd)
 Sets the command to be executed when the button is hovered.
 
void SetOnHoldCommand (UICommand *cmd)
 Sets the command to be executed when the button is held.
 
void SetOnReleaseCommand (UICommand *cmd)
 Sets the command to be executed when the button is released.
 
void SetDefaultSprite (const std::string &key)
 Sets the default sprite for the button.
 
void SetOnPressSprite (const std::string &key)
 Sets the sprite for the button when pressed.
 
void SetOnHeldSprite (const std::string &key)
 Sets the sprite for the button when held.
 
void SetOnHoverSprite (const std::string &key)
 Sets the sprite for the button when hovered.
 
void SetText (const std::string &text, std::string fontKey)
 Sets the text label for the button.
 
std::string GetText () const
 Gets the text label of the button.
 
void SetTextScale (float x, float y)
 Sets the text scale for the button.
 
void SetTextScalePixel (float x, float y)
 Sets the text scale in pixels for the button.
 
- Public Member Functions inherited from UIComponent
virtual ~UIComponent ()=default
 Virtual destructor.
 
int GetLayer () const
 Gets the rendering layer of the component.
 
virtual void SetLayer (int layer)
 Sets the rendering layer of the component.
 
UIComponentType GetType () const
 Gets the type of the UI component.
 
bool IsEnabled () const
 Returns whether the component is enabled or not.
 
void SetEnabled (bool enabled)
 Sets the enabled state of the component.
 
void SetOwningCanvas (Canvas *canvas)
 Gets the owning canvas of the component.
 

Private Member Functions

void OnPress () override
 Called when the button is pressed.
 
void OnHover ()
 Called when the button is hovered.
 
void OnHold ()
 Called when the button is held.
 
void OnRelease ()
 Called when the button is released.
 
void Render () override
 Updates the world matrix of the button.
 
bool IsMouseOver (float xPos, float yPos) override
 Checks if the mouse is over the button.
 
void UpdateWorld ()
 Updates the world matrix of the button.
 

Private Attributes

GraphicsObject_Sprite * pSprite
 The current sprite for the button.
 
GraphicsObject_Sprite * pDefaultSprite
 The default sprite.
 
GraphicsObject_Sprite * pOnPressSprite
 Sprite when pressed.
 
GraphicsObject_Sprite * pOnHeldSprite
 Sprite when held.
 
GraphicsObject_Sprite * pOnHoverSprite
 Sprite when hovered.
 
Matrix trans
 Translation matrix.
 
Matrix rot
 Rotation matrix.
 
Matrix scale
 Scale matrix.
 
float posX
 X position of the button.
 
float posY
 Y position of the button.
 
float angle
 Angle in radians.
 
float scaleX
 X scale factor.
 
float scaleY
 Y scale factor.
 
UICommandpOnPressCommand
 Command for press event.
 
UICommandpOnHoverCommand
 Command for hover event.
 
UICommandpOnHoldCommand
 Command for hold event.
 
UICommandpOnReleaseCommand
 Command for release event.
 
SpriteStringpTextLabel
 Text label for the button.
 
bool textScaleSet
 Whether text scale is set manually.
 
float textScaleX
 X scale for text.
 
float textScaleY
 Y scale for text.
 
std::string font
 Font key for the text.
 

Friends

class UIAttorney
 

Additional Inherited Members

- Protected Attributes inherited from UIComponent
bool Enabled = true
 Indicates if the component is enabled or not.
 
int Layer = 0
 Rendering layer in.
 
UIComponentType Type = UIComponentType::None
 Type of the UI component.
 
CanvaspOwningCanvas = nullptr
 Pointer to the owning canvas, if any.
 

Detailed Description

A clickable UI button component.

The Button class represents a UI button that can display text, respond to mouse events, and execute commands when pressed, hovered, held, or released. It supports custom sprites for different button states and can be added to a Canvas or other UI containers.

Example usage:

Button* btn = new Button("ButtonTextureKey");
btn->SetText("Click Me", "FontKey");
btn->SetOnPressCommand(new MyButtonCommand());
canvas->AddComponent(btn);
Button()=default
Default constructor. Initializes the button with default values.
void SetOnPressCommand(UICommand *cmd)
Sets the command to be executed when the button is pressed.
Definition Button.cpp:218
void SetText(const std::string &text, std::string fontKey)
Sets the text label for the button.
Definition Button.cpp:254
See also
Canvas, UICommand, SpriteString

Constructor & Destructor Documentation

◆ Button()

Button::Button ( std::string key)

Constructor that initializes the button with a texture key.

Parameters
keyThe texture key for the button's default sprite.
Here is the call graph for this function:

Member Function Documentation

◆ GetAngle()

float Button::GetAngle ( ) const

Gets the angle of the button in radians.

Returns
The angle in radians.

◆ GetHeight()

float Button::GetHeight ( ) const

Gets the height of the button.

Returns
The height of the button.
Here is the caller graph for this function:

◆ GetPosX()

float Button::GetPosX ( ) const

Gets the x-coordinate of the button's position.

Returns
The x-coordinate of the button's position.

◆ GetPosY()

float Button::GetPosY ( ) const

Gets the y-coordinate of the button's position.

Returns
The y-coordinate of the button's position.

◆ GetScaleX()

float Button::GetScaleX ( ) const

Gets the x-scale factor of the button.

Returns
The x-scale factor.

◆ GetScaleY()

float Button::GetScaleY ( ) const

Gets the y-scale factor of the button.

Returns
The y-scale factor.

◆ GetText()

std::string Button::GetText ( ) const

Gets the text label of the button.

Returns
The text displayed on the button.

◆ GetWidth()

float Button::GetWidth ( ) const

Gets the width of the button.

Returns
The width of the button.
Here is the caller graph for this function:

◆ IsMouseOver()

bool Button::IsMouseOver ( float xPos,
float yPos )
overrideprivatevirtual

Checks if the mouse is over the button.

Parameters
xPosThe x-coordinate of the mouse position.
yPosThe y-coordinate of the mouse position.
Returns
True if the mouse is over the button, false otherwise.

Implements UIComponent.

◆ OnPress()

void Button::OnPress ( )
overrideprivatevirtual

Called when the button is pressed.

Implements UIComponent.

◆ Render()

void Button::Render ( )
overrideprivatevirtual

Updates the world matrix of the button.

Implements UIComponent.

Here is the call graph for this function:

◆ SetAngleDegrees()

void Button::SetAngleDegrees ( float deg)

Sets the angle of the button in degrees.

Parameters
degThe angle in degrees.

◆ SetAngleRadians()

void Button::SetAngleRadians ( float rad)

Sets the angle of the button in radians.

Parameters
radThe angle in radians.

◆ SetDefaultSprite()

void Button::SetDefaultSprite ( const std::string & key)

Sets the default sprite for the button.

Parameters
keyThe texture key for the default sprite.
Here is the call graph for this function:

◆ SetOnHeldSprite()

void Button::SetOnHeldSprite ( const std::string & key)

Sets the sprite for the button when held.

Parameters
keyThe texture key for the held sprite.
Here is the call graph for this function:

◆ SetOnHoldCommand()

void Button::SetOnHoldCommand ( UICommand * cmd)

Sets the command to be executed when the button is held.

Parameters
cmdThe command to be executed.

◆ SetOnHoverCommand()

void Button::SetOnHoverCommand ( UICommand * cmd)

Sets the command to be executed when the button is hovered.

Parameters
cmdThe command to be executed.

◆ SetOnHoverSprite()

void Button::SetOnHoverSprite ( const std::string & key)

Sets the sprite for the button when hovered.

Parameters
keyThe texture key for the hovered sprite.
Here is the call graph for this function:

◆ SetOnPressCommand()

void Button::SetOnPressCommand ( UICommand * cmd)

Sets the command to be executed when the button is pressed.

Parameters
cmdThe command to be executed.
Here is the caller graph for this function:

◆ SetOnPressSprite()

void Button::SetOnPressSprite ( const std::string & key)

Sets the sprite for the button when pressed.

Parameters
keyThe texture key for the pressed sprite.
Here is the call graph for this function:

◆ SetOnReleaseCommand()

void Button::SetOnReleaseCommand ( UICommand * cmd)

Sets the command to be executed when the button is released.

Parameters
cmdThe command to be executed.

◆ SetOrigin()

void Button::SetOrigin ( float x,
float y )

Sets the origin of the button.

Parameters
xThe x-coordinate of the origin (0.0 to 1.0).
yThe y-coordinate of the origin (0.0 to 1.0).

◆ SetPosition()

void Button::SetPosition ( float x,
float y )

Sets the position of the button.

Parameters
xThe x-coordinate of the button's position.
yThe y-coordinate of the button's position.
Here is the caller graph for this function:

◆ SetScaleFactor()

void Button::SetScaleFactor ( float x,
float y )

Sets the scale of the button.

Parameters
xThe x-scale factor.
yThe y-scale factor.

◆ SetScalePixel()

void Button::SetScalePixel ( float x,
float y )

Sets the scale of the button in pixels.

Parameters
xThe width in pixels.
yThe height in pixels.
Here is the caller graph for this function:

◆ SetText()

void Button::SetText ( const std::string & text,
std::string fontKey )

Sets the text label for the button.

Parameters
textThe text to display on the button.
fontKeyThe texture key for the font to use.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetTextScale()

void Button::SetTextScale ( float x,
float y )

Sets the text scale for the button.

Parameters
xThe x-scale factor for the text.
yThe y-scale factor for the text.
Here is the call graph for this function:

◆ SetTextScalePixel()

void Button::SetTextScalePixel ( float x,
float y )

Sets the text scale in pixels for the button.

Parameters
xThe width in pixels for the text.
yThe height in pixels for the text.
Here is the call graph for this function:

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