Zappy GUI
C++ graphic client: renderer, camera, network
Loading...
Searching...
No Matches
ButtonWidget Class Reference

A configurable, stateful clickable button. More...

#include <ButtonWidget.hpp>

+ Inheritance diagram for ButtonWidget:
+ Collaboration diagram for ButtonWidget:

Public Types

enum class  Anchor {
  TopLeft , TopCenter , TopRight , MiddleLeft ,
  Center , MiddleRight , BottomLeft , BottomCenter ,
  BottomRight , None
}
 Positioning modes for automatic layout. More...
 

Public Member Functions

 ButtonWidget ()=default
 
 ~ButtonWidget () override=default
 
ButtonWidgetsetLabel (const std::string &label)
 
ButtonWidgetsetPosition (float x, float y)
 Sets the button position directly (requires Anchor::None).
 
ButtonWidgetsetSize (float width, float height)
 
ButtonWidgetsetAnchor (Anchor anchor, float margin=10.0f)
 Sets anchor: button is auto-positioned relative to screen edges.
 
ButtonWidgetsetNormalColor (Color color)
 
ButtonWidgetsetHoverColor (Color color)
 
ButtonWidgetsetTextColor (Color color)
 
ButtonWidgetsetBorderColor (Color color)
 
ButtonWidgetsetBorderThickness (float thickness)
 
ButtonWidgetsetRoundness (float roundness)
 
ButtonWidgetsetOnClick (std::function< void()> callback)
 Registers a callback invoked when the button is clicked.
 
void draw (int scaledFontSize) const override
 Draws the widget.
 
bool handleInput () override
 
bool wasClicked ()
 Returns true once per click, then resets.
 
Rectangle getBounds () const
 Returns the bounding rectangle (after anchor resolution).
 
- Public Member Functions inherited from IWidget
virtual ~IWidget ()=default
 

Private Member Functions

Rectangle _resolvedBounds () const
 

Private Attributes

std::string _label
 
float _x = 0.0f
 
float _y = 0.0f
 
float _width = 120.0f
 
float _height = 36.0f
 
Anchor _anchor = Anchor::None
 
float _margin = 10.0f
 
Color _normalColor = {60, 70, 90, 220}
 
Color _hoverColor = {100, 120, 160, 240}
 
Color _textColor = {210, 220, 240, 255}
 
Color _borderColor = {60, 70, 90, 200}
 
float _borderThickness = 2.0f
 
float _roundness = 0.3f
 
bool _hovered = false
 
bool _clicked = false
 
std::function< void()> _onClick
 

Detailed Description

A configurable, stateful clickable button.

Supports custom label, position, size, colors, corner roundness, and an optional on-click callback. Hover state is tracked internally each frame. Use wasClicked() to poll the result, or setOnClick() for a callback.

Definition at line 17 of file ButtonWidget.hpp.

Member Enumeration Documentation

◆ Anchor

enum class ButtonWidget::Anchor
strong

Positioning modes for automatic layout.

Enumerator
TopLeft 
TopCenter 
TopRight 
MiddleLeft 
Center 
MiddleRight 
BottomLeft 
BottomCenter 
BottomRight 
None 

No anchor: position set directly via setPosition().

Definition at line 20 of file ButtonWidget.hpp.

Constructor & Destructor Documentation

◆ ButtonWidget()

ButtonWidget::ButtonWidget ( )
default

◆ ~ButtonWidget()

ButtonWidget::~ButtonWidget ( )
overridedefault

Member Function Documentation

◆ _resolvedBounds()

Rectangle ButtonWidget::_resolvedBounds ( ) const
private

Definition at line 77 of file ButtonWidget.cpp.

References _anchor, _height, _margin, _width, _x, _y, BottomCenter, BottomLeft, BottomRight, Center, MiddleLeft, MiddleRight, None, TopCenter, TopLeft, and TopRight.

Referenced by draw(), getBounds(), and handleInput().

+ Here is the caller graph for this function:

◆ draw()

void ButtonWidget::draw ( int  scaledFontSize) const
overridevirtual

Draws the widget.

Parameters
scaledFontSizeBase font size for UI scaling.

Implements IWidget.

Definition at line 153 of file ButtonWidget.cpp.

References _borderColor, _borderThickness, _hoverColor, _hovered, _label, _normalColor, _resolvedBounds(), _roundness, _textColor, TextRenderer::draw(), and TextRenderer::measure().

Referenced by EntityTooltipWidget::draw(), and WinScreen::draw().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getBounds()

Rectangle ButtonWidget::getBounds ( ) const

Returns the bounding rectangle (after anchor resolution).

Definition at line 130 of file ButtonWidget.cpp.

References _resolvedBounds().

Referenced by EntityTooltipWidget::handleInput().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handleInput()

bool ButtonWidget::handleInput ( )
overridevirtual
Returns
true if the button is hovered (consumes input while hovered).

Implements IWidget.

Definition at line 132 of file ButtonWidget.cpp.

References _clicked, _hovered, _onClick, and _resolvedBounds().

Referenced by EntityTooltipWidget::handleInput(), and WinScreen::handleInput().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setAnchor()

ButtonWidget & ButtonWidget::setAnchor ( Anchor  anchor,
float  margin = 10.0f 
)

Sets anchor: button is auto-positioned relative to screen edges.

Parameters
marginPixel offset from the anchored edge.

Definition at line 28 of file ButtonWidget.cpp.

References _anchor, and _margin.

Referenced by WinScreen::WinScreen().

+ Here is the caller graph for this function:

◆ setBorderColor()

ButtonWidget & ButtonWidget::setBorderColor ( Color  color)

Definition at line 53 of file ButtonWidget.cpp.

References _borderColor.

Referenced by EntityTooltipWidget::EntityTooltipWidget(), and WinScreen::WinScreen().

+ Here is the caller graph for this function:

◆ setBorderThickness()

ButtonWidget & ButtonWidget::setBorderThickness ( float  thickness)

Definition at line 59 of file ButtonWidget.cpp.

References _borderThickness.

Referenced by EntityTooltipWidget::EntityTooltipWidget(), PlayerPanel::handleInput(), and WinScreen::WinScreen().

+ Here is the caller graph for this function:

◆ setHoverColor()

ButtonWidget & ButtonWidget::setHoverColor ( Color  color)

Definition at line 41 of file ButtonWidget.cpp.

References _hoverColor.

◆ setLabel()

ButtonWidget & ButtonWidget::setLabel ( const std::string &  label)

Definition at line 7 of file ButtonWidget.cpp.

References _label.

Referenced by WinScreen::draw(), EntityTooltipWidget::handleInput(), and PlayerPanel::handleInput().

+ Here is the caller graph for this function:

◆ setNormalColor()

ButtonWidget & ButtonWidget::setNormalColor ( Color  color)

Definition at line 35 of file ButtonWidget.cpp.

References _normalColor.

Referenced by EntityTooltipWidget::handleInput().

+ Here is the caller graph for this function:

◆ setOnClick()

ButtonWidget & ButtonWidget::setOnClick ( std::function< void()>  callback)

Registers a callback invoked when the button is clicked.

Definition at line 71 of file ButtonWidget.cpp.

References _onClick.

Referenced by PlayerPanel::handleInput().

+ Here is the caller graph for this function:

◆ setPosition()

ButtonWidget & ButtonWidget::setPosition ( float  x,
float  y 
)

Sets the button position directly (requires Anchor::None).

Definition at line 13 of file ButtonWidget.cpp.

References _anchor, _x, _y, and None.

Referenced by EntityTooltipWidget::_positionButton(), WinScreen::draw(), and PlayerPanel::handleInput().

+ Here is the caller graph for this function:

◆ setRoundness()

ButtonWidget & ButtonWidget::setRoundness ( float  roundness)

Definition at line 65 of file ButtonWidget.cpp.

References _roundness.

Referenced by EntityTooltipWidget::EntityTooltipWidget(), PlayerPanel::handleInput(), and WinScreen::WinScreen().

+ Here is the caller graph for this function:

◆ setSize()

ButtonWidget & ButtonWidget::setSize ( float  width,
float  height 
)

Definition at line 21 of file ButtonWidget.cpp.

References _height, and _width.

Referenced by EntityTooltipWidget::_positionButton(), WinScreen::draw(), EntityTooltipWidget::EntityTooltipWidget(), PlayerPanel::handleInput(), and WinScreen::WinScreen().

+ Here is the caller graph for this function:

◆ setTextColor()

ButtonWidget & ButtonWidget::setTextColor ( Color  color)

Definition at line 47 of file ButtonWidget.cpp.

References _textColor.

Referenced by EntityTooltipWidget::EntityTooltipWidget().

+ Here is the caller graph for this function:

◆ wasClicked()

bool ButtonWidget::wasClicked ( )

Returns true once per click, then resets.

Definition at line 146 of file ButtonWidget.cpp.

References _clicked.

Referenced by EntityTooltipWidget::handleInput(), and WinScreen::handleInput().

+ Here is the caller graph for this function:

Member Data Documentation

◆ _anchor

Anchor ButtonWidget::_anchor = Anchor::None
private

Definition at line 80 of file ButtonWidget.hpp.

Referenced by _resolvedBounds(), setAnchor(), and setPosition().

◆ _borderColor

Color ButtonWidget::_borderColor = {60, 70, 90, 200}
private

Definition at line 86 of file ButtonWidget.hpp.

Referenced by draw(), and setBorderColor().

◆ _borderThickness

float ButtonWidget::_borderThickness = 2.0f
private

Definition at line 87 of file ButtonWidget.hpp.

Referenced by draw(), and setBorderThickness().

◆ _clicked

bool ButtonWidget::_clicked = false
private

Definition at line 91 of file ButtonWidget.hpp.

Referenced by handleInput(), and wasClicked().

◆ _height

float ButtonWidget::_height = 36.0f
private

Definition at line 78 of file ButtonWidget.hpp.

Referenced by _resolvedBounds(), and setSize().

◆ _hoverColor

Color ButtonWidget::_hoverColor = {100, 120, 160, 240}
private

Definition at line 84 of file ButtonWidget.hpp.

Referenced by draw(), and setHoverColor().

◆ _hovered

bool ButtonWidget::_hovered = false
private

Definition at line 90 of file ButtonWidget.hpp.

Referenced by draw(), and handleInput().

◆ _label

std::string ButtonWidget::_label
private

Definition at line 73 of file ButtonWidget.hpp.

Referenced by draw(), and setLabel().

◆ _margin

float ButtonWidget::_margin = 10.0f
private

Definition at line 81 of file ButtonWidget.hpp.

Referenced by _resolvedBounds(), and setAnchor().

◆ _normalColor

Color ButtonWidget::_normalColor = {60, 70, 90, 220}
private

Definition at line 83 of file ButtonWidget.hpp.

Referenced by draw(), and setNormalColor().

◆ _onClick

std::function<void()> ButtonWidget::_onClick
private

Definition at line 93 of file ButtonWidget.hpp.

Referenced by handleInput(), and setOnClick().

◆ _roundness

float ButtonWidget::_roundness = 0.3f
private

Definition at line 88 of file ButtonWidget.hpp.

Referenced by draw(), and setRoundness().

◆ _textColor

Color ButtonWidget::_textColor = {210, 220, 240, 255}
private

Definition at line 85 of file ButtonWidget.hpp.

Referenced by draw(), and setTextColor().

◆ _width

float ButtonWidget::_width = 120.0f
private

Definition at line 77 of file ButtonWidget.hpp.

Referenced by _resolvedBounds(), and setSize().

◆ _x

float ButtonWidget::_x = 0.0f
private

Definition at line 75 of file ButtonWidget.hpp.

Referenced by _resolvedBounds(), and setPosition().

◆ _y

float ButtonWidget::_y = 0.0f
private

Definition at line 76 of file ButtonWidget.hpp.

Referenced by _resolvedBounds(), and setPosition().


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