Sparkle 0.0.1
Loading...
Searching...
No Matches
spk_icon_button.hpp
1#pragma once
2
3#include "structure/design_pattern/spk_contract_provider.hpp"
4#include "structure/math/spk_vector2.hpp"
5#include "structure/widget/components/spk_icon_region.hpp"
6#include "structure/widget/components/spk_nine_slice_background.hpp"
7#include "structure/widget/spk_widget.hpp"
8
9namespace spk
10{
21 class IconButton : public spk::Widget
22 {
23 public:
32
37 enum class State
38 {
39 Released,
40 Pressed,
41 Hovered,
42 Both
43 };
44
45 private:
46 struct Visual
47 {
48 NineSliceBackground background;
49 IconRegion icon;
50 spk::Vector2UInt iconPadding{0, 0};
51 bool iconVisible = true;
52 };
53
54 Visual _released;
55 Visual _pressed;
56 Visual _hovered;
57
58 bool _isPressed = false;
59 bool _isHovered = false;
60
61 spk::TContractProvider<> _onClick;
62
63 Visual &_visualForState(State p_state);
64 const Visual &_visualForState(State p_state) const;
65 void _applyGeometry();
66
67 protected:
73 void _setIconVisible(bool p_visible, State p_state = State::Both);
79 bool _iconVisible(State p_state = State::Released) const;
80
84 void _onGeometryChange() override;
89 void _onPaintEvent(spk::PaintEvent &p_event) override;
94 void _onMousePressEvent(spk::MousePressEvent &p_event) override;
99 void _onMouseReleaseEvent(spk::MouseReleaseEvent &p_event) override;
104 void _onMouseDoubleClickEvent(spk::MouseDoubleClickEvent &p_event) override;
109 void _onMouseMotionEvent(spk::MouseMotionEvent &p_event) override;
110
111 public:
117 IconButton(const std::wstring &p_name, spk::Widget *p_parent);
118
123 void setVisualState(const State &p_state);
128 void toggle(State p_state = State::Both);
134 bool isIconVisible(State p_state = State::Released) const;
140 Contract subscribe(const Job &p_job);
141
147 void setTexture(const spk::SpriteSheet *p_spriteSheet, State p_state = State::Both);
153 void setCornerSize(const spk::Vector2UInt &p_cornerSize, State p_state = State::Both);
154
160 void setIconTexture(const spk::SpriteSheet &p_spriteSheet, State p_state = State::Both);
166 void setIconTexture(const spk::SpriteSheet *p_spriteSheet, State p_state = State::Both);
172 void setIconSpriteID(const spk::Vector2UInt &p_spriteID, State p_state = State::Both);
178 void setIconPadding(const spk::Vector2UInt &p_padding, State p_state = State::Both);
179
185 const spk::SpriteSheet *texture(State p_state = State::Both) const;
191 const spk::Vector2UInt &cornerSize(State p_state = State::Both) const;
197 const spk::SpriteSheet *iconTexture(State p_state = State::Both) const;
203 const spk::Vector2UInt &iconSpriteID(State p_state = State::Both) const;
209 const spk::Vector2UInt &iconPadding(State p_state = State::Both) const;
210 };
211}
void toggle()
Toggles between activated and deactivated states.
Definition spk_activable_object.hpp:68
const spk::Vector2UInt & iconPadding(State p_state=State::Both) const
Returns the icon padding for a state.
Definition spk_icon_button.cpp:304
State
Visual state of the button.
Definition spk_icon_button.hpp:38
void setIconPadding(const spk::Vector2UInt &p_padding, State p_state=State::Both)
Sets icon padding for a state.
Definition spk_icon_button.cpp:266
void setCornerSize(const spk::Vector2UInt &p_cornerSize, State p_state=State::Both)
Sets the background corner size for a state.
Definition spk_icon_button.cpp:215
void _onPaintEvent(spk::PaintEvent &p_event) override
Paints the button visuals.
Definition spk_icon_button.cpp:143
void _onMouseReleaseEvent(spk::MouseReleaseEvent &p_event) override
Handles mouse release events.
Definition spk_icon_button.cpp:168
spk::TContractProvider<>::Job Job
Job type invoked on clicks.
Definition spk_icon_button.hpp:31
void setTexture(const spk::SpriteSheet *p_spriteSheet, State p_state=State::Both)
Sets the background texture for a state.
Definition spk_icon_button.cpp:201
void _setIconVisible(bool p_visible, State p_state=State::Both)
Sets icon visibility for a state.
Definition spk_icon_button.cpp:117
bool isIconVisible(State p_state=State::Released) const
Returns icon visibility for a state.
Definition spk_icon_button.cpp:72
const spk::Vector2UInt & iconSpriteID(State p_state=State::Both) const
Returns the icon sprite ID for a state.
Definition spk_icon_button.cpp:299
bool _iconVisible(State p_state=State::Released) const
Returns icon visibility for a state.
Definition spk_icon_button.cpp:133
void _onMousePressEvent(spk::MousePressEvent &p_event) override
Handles mouse press events.
Definition spk_icon_button.cpp:155
void _onGeometryChange() override
Updates layout geometry when size changes.
Definition spk_icon_button.cpp:138
void setVisualState(const State &p_state)
Sets the current visual state.
Definition spk_icon_button.cpp:36
const spk::SpriteSheet * texture(State p_state=State::Both) const
Returns the background texture for a state.
Definition spk_icon_button.cpp:284
void _onMouseMotionEvent(spk::MouseMotionEvent &p_event) override
Handles mouse motion events.
Definition spk_icon_button.cpp:191
spk::TContractProvider<>::Contract Contract
Contract type for click subscriptions.
Definition spk_icon_button.hpp:27
void setIconSpriteID(const spk::Vector2UInt &p_spriteID, State p_state=State::Both)
Sets the icon sprite ID for a state.
Definition spk_icon_button.cpp:252
const spk::Vector2UInt & cornerSize(State p_state=State::Both) const
Returns the background corner size for a state.
Definition spk_icon_button.cpp:289
void _onMouseDoubleClickEvent(spk::MouseDoubleClickEvent &p_event) override
Handles mouse double click events.
Definition spk_icon_button.cpp:178
void setIconTexture(const spk::SpriteSheet &p_spriteSheet, State p_state=State::Both)
Sets the icon texture from a sprite sheet reference.
Definition spk_icon_button.cpp:233
IconButton(const std::wstring &p_name, spk::Widget *p_parent)
Builds an icon button widget.
Definition spk_icon_button.cpp:9
const spk::SpriteSheet * iconTexture(State p_state=State::Both) const
Returns the icon texture for a state.
Definition spk_icon_button.cpp:294
Contract subscribe(const Job &p_job)
Subscribes to click events.
Definition spk_icon_button.cpp:196
Renders a textured sprite region within a given geometry.
Definition spk_icon_region.hpp:24
Renders a scalable nine-slice background using a sprite sheet.
Definition spk_nine_slice_background.hpp:25
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
Base class for interactive UI elements handling focus and events.
Definition spk_widget.hpp:52