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 checkbox: a square indicator + optional label.
|
|
// HTML analog: <input type="checkbox">
|
|
//
|
|
// Usage:
|
|
// auto* cb = new Checkbox<>();
|
|
// // per-frame: cb->setChecked(viewState.flag)
|
|
//
|
|
// Pure display widget. The view drives checked state via setChecked().
|
|
// The checkbox does NOT handle its own onClick — the view wires the
|
|
// toggle logic via attr.onClick on the owning container.
|
|
template<StyleClass... classes>
|
|
class Checkbox : public Div<classes...> {
|
|
public:
|
|
using Div<classes...>::attr;
|
|
using Div<classes...>::style;
|
|
using Div<classes...>::markDirty;
|
|
using Div<classes...>::getTheme;
|
|
using Div<classes...>::children;
|
|
|
|
Checkbox(Attributes attr = Attributes{}, std::initializer_list<OElement> children = {})
|
|
: Div<classes...>(std::move(attr), children) {
|
|
style.height = 20;
|
|
style.width = 20;
|
|
}
|
|
virtual ~Checkbox() override {}
|
|
|
|
void setChecked(bool checked) {
|
|
_checked = checked;
|
|
markDirty();
|
|
}
|
|
bool isChecked() const { return _checked; }
|
|
|
|
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);
|
|
}
|
|
|
|
// The System calls applyOverrides() during style() after applyStyle.
|
|
// We use it to reflect checked/hover/focus in the background color.
|
|
void applyOverrides() override {
|
|
const Theme& th = getTheme();
|
|
if (_checked) {
|
|
style.backgroundColor = th.color(ColorToken::Green);
|
|
} 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 _checked = false;
|
|
bool _hovered = false;
|
|
bool _focused = false;
|
|
};
|
|
} // namespace UI
|
|
} // namespace Seele
|