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/core/api.h"
6#include "flint/ui/ui-node.h"
7#include "flint/ui/widgets/sprite.h"
8
9#include <functional>
10#include <string>
11
12namespace Flint {
13
17 enum class IconPlacement {
18 Left,
19 Right
20 };
21
26 class UIButton : public UINode {
27 public:
28 FLINT_API UIButton();
29 FLINT_API explicit UIButton(std::string text);
30 ~UIButton() override = default;
31
33 FLINT_API void setOnClick(std::function<void()> callback);
34
38 bool isFocusable() const override {
39 return true;
40 }
41
44 FLINT_API void measure(UIConstraints constraints) override;
45 FLINT_API void arrange(const Rect<float> &rect) override;
46
52 FLINT_API void handleEvent(const UIEvent &event) override;
53 FLINT_API void render(UICanvas &canvas) const override;
54
56 void setIcon(std::unique_ptr<UISprite> icon, IconPlacement placement = IconPlacement::Left) {
57 m_icon = std::move(icon);
58
59 m_iconPlacement = placement;
60 }
61
64 void setIconGap(float gap) {
65 m_iconGap = gap;
66 }
67
68 private:
69 std::function<void()> m_onClick;
70
73 void activate() const;
74
75 std::unique_ptr<UISprite> m_icon;
76 IconPlacement m_iconPlacement = IconPlacement::Left;
77 float m_iconGap = 4.f;
78 };
79
80}
A clickable interactive text button widget supporting custom sprite icons.
Definition button.h:26
FLINT_API void arrange(const Rect< float > &rect) override
Pass 2 of the layout engine: Evaluates layout adjustments to pin final screen coordinate values to th...
void setIconGap(float gap)
Adjusts the internal margin gap spacing applied between text layouts and the embedded icon bounds.
Definition button.h:64
FLINT_API void setOnClick(std::function< void()> callback)
Registers a function callback executed upon click confirmation actions.
FLINT_API void measure(UIConstraints constraints) override
Evaluates spatial needs combining base label text grids with inner icon spacing gap parameters.
FLINT_API void handleEvent(const UIEvent &event) override
Intercepts inputs to capture highlight triggers and selection execution operations.
bool isFocusable() const override
Dictates if keyboard selection trackers or gamepad vectors can snap control focuses onto this item.
Definition button.h:38
FLINT_API void render(UICanvas &canvas) const override
Pushes current styling rules and geometries straight down to the active pipeline vertex drawing queue...
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:56
Dynamic 2D batching engine processing shape vector geometry and hardware-accelerated text strings....
Definition ui-canvas.h:107
Abstract base class for all UI widgets, managing hierarchy tracking, layout passes,...
Definition ui-node.h:31
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