Flint Engine
Modular C++20 2D game engine powered by SDL3
Loading...
Searching...
No Matches
slider.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
8#include <functional>
9
10namespace Flint {
11
17 class UISlider : public UINode {
18 public:
23 float min = 0.f;
24 float max = 1.f;
25 float step = 0.1f;
27 };
28
32 struct SliderStyle {
33 float trackHeight = 6.f;
34 float thumbWidth = 8.f;
35 float thumbHeight = 6.f;
36 Color trackColor = {70, 70, 70, 255};
37 Color fillColor = {180, 180, 180, 255};
38 Color thumbColor = {230, 230, 230, 255};
39 };
40
41 UISlider() = default;
42 ~UISlider() noexcept override = default;
44 FLINT_API void setProperties(SliderProperties properties);
45
47 FLINT_API void setRange(float min, float max);
48
50 FLINT_API void setStyle(const SliderStyle &style);
51 FLINT_API void setTrackHeight(float height);
52 FLINT_API void setThumbWidth(float width);
53 FLINT_API void setThumbHeight(float height);
54 FLINT_API void setThumbColor(const Color &color);
55 FLINT_API void setFillColor(const Color &color);
56 FLINT_API void setTrackColor(const Color &color);
57
63 FLINT_API float applyStep(float value) const;
64
66 FLINT_API void setValue(float value);
67
68 [[nodiscard]] const SliderProperties &getProperties() const {
69 return m_properties;
70 }
71
72 [[nodiscard]] const SliderStyle &getStyle() const {
73 return m_sliderStyle;
74 }
75
77 [[nodiscard]] const float &getValue() const {
78 return m_value;
79 }
80
81 FLINT_API void setFocused(bool focused) override;
82 FLINT_API void measure(UIConstraints constraints) override;
83 FLINT_API void arrange(const Rect<float> &rect) override;
84
90 FLINT_API void handleEvent(const UIEvent &event) override;
91
93 FLINT_API void render(UICanvas &canvas) const override;
94
98 bool consumesDirectionalInput() const override {
99 return true;
100 }
101
105 bool isFocusable() const override {
106 return true;
107 }
108
111 FLINT_API void onChange(std::function<void(float)> callback);
112
113 FLINT_API bool hitTest(Vec2f point) const override;
114
115 protected:
118 FLINT_API const Color &currentColor() const override;
119
120 private:
121 void setValueInternal(float value, bool notify);
122 void updateValueFromMouse(Vec2f mousePosition);
123 [[nodiscard]] float getNormalizedValue() const;
124
125 SliderProperties m_properties{};
126 SliderStyle m_sliderStyle{};
127 float m_value = 0.f;
128
129 bool m_dragging = false;
130 std::function<void(float)> m_onChange;
131 };
132}
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
An interactive value-selector bar supporting mouse dragging and gamepad focus adjustments.
Definition slider.h:17
FLINT_API void handleEvent(const UIEvent &event) override
Routes interaction updates, translating drag motions and directional navigations.
FLINT_API void setRange(float min, float max)
Sets the absolute functional minimum and maximum selection boundaries.
FLINT_API const Color & currentColor() const override
Resolves handle colors by evaluating hover, active focus, and dragging parameters.
const float & getValue() const
Gets the current stepped, clamped value of the slider.
Definition slider.h:77
FLINT_API void setProperties(SliderProperties properties)
Assigns minimum, maximum, and quantization stepping properties.
FLINT_API float applyStep(float value) const
Snaps a raw value to the nearest incremental step boundary configuration.
FLINT_API void setStyle(const SliderStyle &style)
Overrides the visual style parameters for the track and slider handles.
FLINT_API bool hitTest(Vec2f point) const override
Computes true if a vector coordinate sits inside the spatial arrangement bounding tracking layout box...
FLINT_API void setValue(float value)
Sets the tracking selection value directly, applying steps and clamps.
FLINT_API void onChange(std::function< void(float)> callback)
Registers a function callback to execute whenever the slider value mutations occur.
bool consumesDirectionalInput() const override
Dictates whether this widget captures directional inputs instead of passing them to focus navigation.
Definition slider.h:98
FLINT_API void render(UICanvas &canvas) const override
Draws the slider track background, active fill bar, and handle quad.
bool isFocusable() const override
Dictates if keyboard selection trackers or gamepad vectors can snap control focuses onto this item.
Definition slider.h:105
FLINT_API void measure(UIConstraints constraints) override
Pass 1 of the layout engine: Crawls branches recursively to compute base display boundaries requested...
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...
FLINT_API void setFocused(bool focused) override
Sets whether this node is currently focused.
RGBA color with 8-bit unsigned components.
Definition color.h:13
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
Architectural constraints governing value clamping and stepping metrics.
Definition slider.h:22
Visual properties defining the slider track, fill area, and thumb indicator.
Definition slider.h:32