67 lines
2.0 KiB
C++
67 lines
2.0 KiB
C++
#pragma once
|
|
#include "UI/Element/Div.h"
|
|
#include "UI/Style/Class.h"
|
|
|
|
namespace Seele {
|
|
namespace UI {
|
|
// A radio button: a circular indicator + optional label.
|
|
// HTML analog: <input type="radio">
|
|
//
|
|
// Usage:
|
|
// auto* r1 = new Radio<>();
|
|
// auto* r2 = new Radio<>();
|
|
// // per-frame: r1->setSelected(viewState.choice == 0)
|
|
// // r2->setSelected(viewState.choice == 1)
|
|
//
|
|
// Pure display widget. The view owns the selection state and drives
|
|
// setSelected() on each radio in the group.
|
|
template<StyleClass... classes>
|
|
class Radio : public Div<classes...> {
|
|
public:
|
|
using Div<classes...>::attr;
|
|
using Div<classes...>::style;
|
|
using Div<classes...>::markDirty;
|
|
using Div<classes...>::getTheme;
|
|
|
|
Radio(Attributes attr = Attributes{}, std::initializer_list<OElement> children = {})
|
|
: Div<classes...>(std::move(attr), children) {
|
|
style.height = 20;
|
|
style.width = 20;
|
|
}
|
|
virtual ~Radio() override {}
|
|
|
|
void setSelected(bool selected) {
|
|
_selected = selected;
|
|
markDirty();
|
|
}
|
|
bool isSelected() const { return _selected; }
|
|
|
|
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 (_selected) {
|
|
style.backgroundColor = th.color(ColorToken::Blue);
|
|
} else if (_hovered) {
|
|
style.backgroundColor = Vector(0.22f, 0.24f, 0.28f);
|
|
} else if (_focused) {
|
|
style.backgroundColor = Vector(0.20f, 0.22f, 0.26f);
|
|
} else {
|
|
style.backgroundColor = Vector(0.14f, 0.14f, 0.16f);
|
|
}
|
|
}
|
|
|
|
private:
|
|
bool _selected = false;
|
|
bool _hovered = false;
|
|
bool _focused = false;
|
|
};
|
|
} // namespace UI
|
|
} // namespace Seele
|