forked from Dynamitos/Seele
69 lines
2.2 KiB
C++
69 lines
2.2 KiB
C++
#pragma once
|
|
#include "UI/Element/Div.h"
|
|
#include "UI/Style/Class.h"
|
|
|
|
namespace Seele {
|
|
namespace UI {
|
|
// A slider: a horizontal track with a fill bar that reflects a 0.0-1.0 value.
|
|
// HTML analog: <input type="range">
|
|
//
|
|
// Usage:
|
|
// auto* slider = new Slider<>();
|
|
// // per-frame: slider->setValue(viewState.volume) // 0.0 - 1.0
|
|
//
|
|
// Pure display widget. The slider's background is a lerp between a dimmed
|
|
// "track" color and an accent "fill" color based on the value, giving a
|
|
// visual fill effect. Mouse drag handling is the owning view's job.
|
|
template<StyleClass... classes>
|
|
class Slider : public Div<classes...> {
|
|
public:
|
|
using Div<classes...>::attr;
|
|
using Div<classes...>::style;
|
|
using Div<classes...>::markDirty;
|
|
using Div<classes...>::getTheme;
|
|
|
|
Slider(Attributes attr = Attributes{}, std::initializer_list<OElement> children = {})
|
|
: Div<classes...>(std::move(attr), children) {
|
|
attr.focusable = true;
|
|
style.height = 8;
|
|
}
|
|
virtual ~Slider() override {}
|
|
|
|
void setValue(float value) {
|
|
_value = value < 0.0f ? 0.0f : (value > 1.0f ? 1.0f : value);
|
|
markDirty();
|
|
}
|
|
float getValue() const { return _value; }
|
|
|
|
void setHovered(bool hovered) override { _hovered = hovered; }
|
|
void setFocused(bool focused) override { _focused = focused; }
|
|
bool isFocusable() const override { return attr.focusable; }
|
|
|
|
void applyStyle(Style parentStyle, const Theme& theme) override {
|
|
Div<classes...>::applyStyle(parentStyle, theme);
|
|
}
|
|
|
|
void applyOverrides() override {
|
|
const Theme& th = getTheme();
|
|
if (_focused || _hovered) {
|
|
style.backgroundColor = th.color(ColorToken::Blue);
|
|
} else {
|
|
// Lerp between track (dimmed) and accent (blue) for fill effect.
|
|
Vector track(0.18f, 0.18f, 0.20f);
|
|
Vector accent = th.color(ColorToken::Blue);
|
|
float v = _value;
|
|
style.backgroundColor = Vector(
|
|
track.x + (accent.x - track.x) * v,
|
|
track.y + (accent.y - track.y) * v,
|
|
track.z + (accent.z - track.z) * v);
|
|
}
|
|
}
|
|
|
|
private:
|
|
float _value = 0.0f;
|
|
bool _hovered = false;
|
|
bool _focused = false;
|
|
};
|
|
} // namespace UI
|
|
} // namespace Seele
|