Flint Engine
Modular C++20 2D game engine powered by SDL3
Loading...
Searching...
No Matches
button.h
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2025-2026 Manuel Osorio
3#pragma once
4
5#include "flint/ui/ui-node.h"
6#include "flint/ui/widgets/sprite.h"
7
8#include <functional>
9#include <string>
10
11namespace Flint {
12
16 enum class IconPlacement {
17 Left,
18 Right
19 };
20
25 class UIButton : public UINode {
26 public:
27 UIButton();
28 explicit UIButton(std::string text);
29 ~UIButton() override = default;
30
32 void setOnClick(std::function<void()> callback);
33
37 bool isFocusable() const override {
38 return true;
39 }
40
43 void measure(UIConstraints constraints) override;
44 void arrange(const Rect<float> &rect) override;
45
51 void handleEvent(const UIEvent &event) override;
52 void render(UICanvas &canvas) const override;
53
55 void setIcon(std::unique_ptr<UISprite> icon, IconPlacement placement = IconPlacement::Left) {
56 m_icon = std::move(icon);
57
58 m_iconPlacement = placement;
59 }
60
63 void setIconGap(float gap) {
64 m_iconGap = gap;
65 }
66
67 private:
68 std::function<void()> m_onClick;
69
72 void activate() const;
73
74 std::unique_ptr<UISprite> m_icon;
75 IconPlacement m_iconPlacement = IconPlacement::Left;
76 float m_iconGap = 4.f;
77 };
78
79}
void render(UICanvas &canvas) const override
Pushes current styling rules and geometries straight down to the active pipeline vertex drawing queue...
void measure(UIConstraints constraints) override
Evaluates spatial needs combining base label text grids with inner icon spacing gap parameters.
void setIconGap(float gap)
Adjusts the internal margin gap spacing applied between text layouts and the embedded icon bounds.
Definition button.h:63
void handleEvent(const UIEvent &event) override
Intercepts inputs to capture highlight triggers and selection execution operations.
void arrange(const Rect< float > &rect) override
Pass 2 of the layout engine: Evaluates layout adjustments to pin final screen coordinate values to th...
bool isFocusable() const override
Dictates if keyboard selection trackers or gamepad vectors can snap control focuses onto this item.
Definition button.h:37
void setIcon(std::unique_ptr< UISprite > icon, IconPlacement placement=IconPlacement::Left)
Embeds an asset image graphic item inside the structural margins of the widget.
Definition button.h:55
A clickable interactive text button widget supporting custom sprite icons.
Definition button.h:25
void setOnClick(std::function< void()> callback)
Registers a function callback executed upon click confirmation actions.
Dynamic 2D batching engine processing shape vector geometry and hardware-accelerated text strings....
Definition ui-canvas.h:106
Abstract base class for all UI widgets, managing hierarchy tracking, layout passes,...
Definition ui-node.h:30
Generic axis-aligned rectangle defined by a position and size.
Definition types.h:87
Represents the boundary budget passed down the node tree during layout allocation.
Definition ui-types.h:76
A normalized data packet containing translated input data routed down the UI node tree.
Definition ui-event.h:51