Sparkle 0.0.1
Loading...
Searching...
No Matches
spk_push_button.hpp
1#pragma once
2
3#include "structure/design_pattern/spk_contract_provider.hpp"
4#include "structure/widget/components/spk_nine_slice_background.hpp"
5#include "structure/widget/components/spk_text_region.hpp"
6#include "structure/widget/spk_widget.hpp"
7
8namespace spk
9{
20 class PushButton : public spk::Widget
21 {
22 public:
31
36 enum class State
37 {
38 Released,
39 Pressed,
40 Hovered,
41 Both
42 };
43
44 private:
45 struct Visual
46 {
47 NineSliceBackground background;
48 TextRegion text;
49 };
50
51 Visual _released;
52 Visual _pressed;
53 Visual _hovered;
54
55 bool _isPressed = false;
56 bool _isHovered = false;
57 bool _forceHovered = false;
58
60
61 Visual &_visualForState(State p_state);
62 const Visual &_visualForState(State p_state) const;
63 void _applyGeometry();
64
65 protected:
69 void _onGeometryChange() override;
74 void _onPaintEvent(spk::PaintEvent &p_event) override;
79 void _onMousePressEvent(spk::MousePressEvent &p_event) override;
84 void _onMouseReleaseEvent(spk::MouseReleaseEvent &p_event) override;
94 void _onMouseMotionEvent(spk::MouseMotionEvent &p_event) override;
95
96 public:
102 PushButton(const std::wstring &p_name, spk::Widget *p_parent);
103
108 void setVisualState(const State &p_state);
113 void setForceHovered(bool p_force);
114
120 Contract subscribe(const Job &p_job);
121
127 void setTexture(const spk::SpriteSheet *p_spriteSheet, State p_state = State::Both);
133 void setCornerSize(const spk::Vector2UInt &p_cornerSize, State p_state = State::Both);
134
140 void setFont(spk::Font *p_font, State p_state = State::Both);
146 void setFontSize(const spk::Font::Size &p_size, State p_state = State::Both);
153 void setColors(const spk::Color &p_glyphColor, const spk::Color &p_outlineColor, State p_state = State::Both);
159 void setText(const std::wstring &p_text, State p_state = State::Both);
166 void setAlignment(HorizontalAlignment p_horizontalAlignment, VerticalAlignment p_verticalAlignment, State p_state = State::Both);
167
173 const spk::SpriteSheet *texture(State p_state) const;
179 const spk::Vector2UInt &cornerSize(State p_state) const;
185 spk::Font *font(State p_state) const;
191 const spk::Font::Size &fontSize(State p_state) const;
197 const spk::Color &glyphColor(State p_state) const;
203 const spk::Color &outlineColor(State p_state) const;
209 const std::wstring &text(State p_state) const;
215 HorizontalAlignment horizontalAlignment(State p_state) const;
221 VerticalAlignment verticalAlignment(State p_state) const;
222 };
223}
Loads font data and provides cached bitmap atlases per size.
Definition spk_font.hpp:33
Renders a scalable nine-slice background using a sprite sheet.
Definition spk_nine_slice_background.hpp:25
const spk::Color & outlineColor(State p_state) const
Returns the outline color for a state.
Definition spk_push_button.cpp:297
HorizontalAlignment horizontalAlignment(State p_state) const
Returns the horizontal alignment for a state.
Definition spk_push_button.cpp:307
void setForceHovered(bool p_force)
Forces hovered state visually.
Definition spk_push_button.cpp:55
void _onMouseMotionEvent(spk::MouseMotionEvent &p_event) override
Handles mouse motion events.
Definition spk_push_button.cpp:159
Contract subscribe(const Job &p_job)
Subscribes to click events.
Definition spk_push_button.cpp:164
spk::Font * font(State p_state) const
Returns the font for a state.
Definition spk_push_button.cpp:282
void setTexture(const spk::SpriteSheet *p_spriteSheet, State p_state=State::Both)
Sets the background texture for a state.
Definition spk_push_button.cpp:169
void _onMouseDoubleClickEvent(spk::MouseDoubleClickEvent &p_event) override
Handles mouse double click events.
Definition spk_push_button.cpp:146
const spk::Vector2UInt & cornerSize(State p_state) const
Returns the background corner size for a state.
Definition spk_push_button.cpp:277
void setAlignment(HorizontalAlignment p_horizontalAlignment, VerticalAlignment p_verticalAlignment, State p_state=State::Both)
Sets text alignment for a state.
Definition spk_push_button.cpp:258
void setFont(spk::Font *p_font, State p_state=State::Both)
Sets the font for a state.
Definition spk_push_button.cpp:199
spk::TContractProvider<>::Job Job
Job type invoked on clicks.
Definition spk_push_button.hpp:30
void _onMousePressEvent(spk::MousePressEvent &p_event) override
Handles mouse press events.
Definition spk_push_button.cpp:123
void setCornerSize(const spk::Vector2UInt &p_cornerSize, State p_state=State::Both)
Sets the background corner size for a state.
Definition spk_push_button.cpp:184
const spk::Font::Size & fontSize(State p_state) const
Returns the font size for a state.
Definition spk_push_button.cpp:287
const spk::SpriteSheet * texture(State p_state) const
Returns the background texture for a state.
Definition spk_push_button.cpp:272
spk::TContractProvider<>::Contract Contract
Contract type for click subscriptions.
Definition spk_push_button.hpp:26
void setText(const std::wstring &p_text, State p_state=State::Both)
Sets the text for a state.
Definition spk_push_button.cpp:243
void setFontSize(const spk::Font::Size &p_size, State p_state=State::Both)
Sets the font size for a state.
Definition spk_push_button.cpp:214
const spk::Color & glyphColor(State p_state) const
Returns the glyph color for a state.
Definition spk_push_button.cpp:292
VerticalAlignment verticalAlignment(State p_state) const
Returns the vertical alignment for a state.
Definition spk_push_button.cpp:312
void setVisualState(const State &p_state)
Sets the current visual state.
Definition spk_push_button.cpp:36
PushButton(const std::wstring &p_name, spk::Widget *p_parent)
Builds a push button widget.
Definition spk_push_button.cpp:9
void _onPaintEvent(spk::PaintEvent &p_event) override
Paints the button visuals.
Definition spk_push_button.cpp:116
void _onMouseReleaseEvent(spk::MouseReleaseEvent &p_event) override
Handles mouse release events.
Definition spk_push_button.cpp:136
const std::wstring & text(State p_state) const
Returns the text for a state.
Definition spk_push_button.cpp:302
State
Visual state of the button.
Definition spk_push_button.hpp:37
void _onGeometryChange() override
Updates layout geometry when size changes.
Definition spk_push_button.cpp:111
void setColors(const spk::Color &p_glyphColor, const spk::Color &p_outlineColor, State p_state=State::Both)
Sets glyph and outline colors for a state.
Definition spk_push_button.cpp:229
Loads an image and exposes individual sprite regions.
Definition spk_sprite_sheet.hpp:21
Handle to a subscribed job that can be resigned or triggered.
Definition spk_contract_provider.hpp:42
typename Contract::Job Job
Alias to the subscribed callback signature.
Definition spk_contract_provider.hpp:267
Renders formatted text within a geometry region.
Definition spk_text_region.hpp:24
Base class for interactive UI elements handling focus and events.
Definition spk_widget.hpp:52
RGBA color utility storing normalized floating components.
Definition spk_color.hpp:18
Defines glyph and outline sizes for a font bitmap.
Definition spk_font.hpp:40
Mouse button double-click event.
Definition spk_mouse_event.hpp:38
Mouse move event carrying current position.
Definition spk_mouse_event.hpp:50
Mouse button press event.
Definition spk_mouse_event.hpp:14
Mouse button release event.
Definition spk_mouse_event.hpp:26
Event emitted when a drawable region needs repainting.
Definition spk_paint_event.hpp:19