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#include "flint/ui/ui-node.h"
5
6#include <functional>
7
8namespace Flint {
9
15 class UISlider : public UINode {
16 public:
21 float min = 0.f;
22 float max = 1.f;
23 float step = 0.1f;
25 };
26
30 struct SliderStyle {
31 float trackHeight = 6.f;
32 float thumbWidth = 8.f;
33 float thumbHeight = 6.f;
34 Color trackColor = {70, 70, 70, 255};
35 Color fillColor = {180, 180, 180, 255};
36 Color thumbColor = {230, 230, 230, 255};
37 };
38
39 UISlider() = default;
40 ~UISlider() noexcept override = default;
43
45 void setRange(float min, float max);
46
48 void setStyle(const SliderStyle &style);
49 void setTrackHeight(float height);
50 void setThumbWidth(float width);
51 void setThumbHeight(float height);
52 void setThumbColor(const Color &color);
53 void setFillColor(const Color &color);
54 void setTrackColor(const Color &color);
55
61 float applyStep(float value) const;
62
64 void setValue(float value);
65
66 [[nodiscard]] const SliderProperties &getProperties() const {
67 return m_properties;
68 }
69
70 [[nodiscard]] const SliderStyle &getStyle() const {
71 return m_sliderStyle;
72 }
73
75 [[nodiscard]] const float &getValue() const {
76 return m_value;
77 }
78
79 void setFocused(bool focused) override;
80 void measure(UIConstraints constraints) override;
81 void arrange(const Rect<float> &rect) override;
82
88 void handleEvent(const UIEvent &event) override;
89
91 void render(UICanvas &canvas) const override;
92
96 bool consumesDirectionalInput() const override {
97 return true;
98 }
99
103 bool isFocusable() const override {
104 return true;
105 }
106
109 void onChange(std::function<void(float)> callback);
110
111 bool hitTest(Vec2f point) const override;
112
113 protected:
116 const Color &currentColor() const override;
117
118 private:
119 void setValueInternal(float value, bool notify);
120 void updateValueFromMouse(Vec2f mousePosition);
121 [[nodiscard]] float getNormalizedValue() const;
122
123 SliderProperties m_properties{};
124 SliderStyle m_sliderStyle{};
125 float m_value = 0.f;
126
127 bool m_dragging = false;
128 std::function<void(float)> m_onChange;
129 };
130}
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
void render(UICanvas &canvas) const override
Draws the slider track background, active fill bar, and handle quad.
const Color & currentColor() const override
Resolves handle colors by evaluating hover, active focus, and dragging parameters.
void measure(UIConstraints constraints) override
Pass 1 of the layout engine: Crawls branches recursively to compute base display boundaries requested...
bool hitTest(Vec2f point) const override
Computes true if a vector coordinate sits inside the spatial arrangement bounding tracking layout box...
float applyStep(float value) const
Snaps a raw value to the nearest incremental step boundary configuration.
const float & getValue() const
Gets the current stepped, clamped value of the slider.
Definition slider.h:75
void setFocused(bool focused) override
Sets whether this node is currently focused.
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 consumesDirectionalInput() const override
Dictates whether this widget captures directional inputs instead of passing them to focus navigation.
Definition slider.h:96
bool isFocusable() const override
Dictates if keyboard selection trackers or gamepad vectors can snap control focuses onto this item.
Definition slider.h:103
void handleEvent(const UIEvent &event) override
Routes interaction updates, translating drag motions and directional navigations.
An interactive value-selector bar supporting mouse dragging and gamepad focus adjustments.
Definition slider.h:15
void setProperties(SliderProperties properties)
Assigns minimum, maximum, and quantization stepping properties.
void onChange(std::function< void(float)> callback)
Registers a function callback to execute whenever the slider value mutations occur.
void setStyle(const SliderStyle &style)
Overrides the visual style parameters for the track and slider handles.
void setRange(float min, float max)
Sets the absolute functional minimum and maximum selection boundaries.
void setValue(float value)
Sets the tracking selection value directly, applying steps and clamps.
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:20
Visual properties defining the slider track, fill area, and thumb indicator.
Definition slider.h:30