99 lines
3.1 KiB
C++
99 lines
3.1 KiB
C++
#pragma once
|
|
#include "UI/Element/Div.h"
|
|
#include "UI/Element/Text.h"
|
|
#include "UI/Style/Class.h"
|
|
|
|
namespace Seele {
|
|
namespace UI {
|
|
// A text input field: a box that displays a placeholder (when empty) or a
|
|
// text value (when non-empty). The actual text editing (key handling, cursor,
|
|
// IME) is the owning view's responsibility — the view intercepts key events
|
|
// and calls setValue() with the updated text.
|
|
//
|
|
// HTML analog: <input type="text">
|
|
//
|
|
// Usage:
|
|
// auto* field = new InputField<Font_Arial, Text_SM>(UI::Attributes{}, "Enter name...");
|
|
// // per-frame: field->setValue(viewState.inputText);
|
|
template<StyleClass... classes>
|
|
class InputField : public Div<classes...> {
|
|
public:
|
|
using Div<classes...>::attr;
|
|
using Div<classes...>::style;
|
|
using Div<classes...>::markDirty;
|
|
using Div<classes...>::getTheme;
|
|
using Div<classes...>::children;
|
|
|
|
InputField(Attributes attr, std::string placeholder = "")
|
|
: Div<classes...>(std::move(attr), std::initializer_list<OElement>{}) {
|
|
attr.focusable = true;
|
|
_placeholder = std::move(placeholder);
|
|
|
|
auto* t = new Text<classes...>(_placeholder);
|
|
children.add(OElement(t));
|
|
children.back()->setParent(this);
|
|
_text = PElement(t);
|
|
|
|
style.paddingLeft = 12;
|
|
style.paddingRight = 12;
|
|
style.paddingTop = 8;
|
|
style.paddingBottom = 8;
|
|
style.backgroundColor = Vector(0.10f, 0.10f, 0.12f);
|
|
}
|
|
virtual ~InputField() override {}
|
|
|
|
void setValue(std::string value) {
|
|
_value = std::move(value);
|
|
markDirty();
|
|
}
|
|
std::string getValue() const { return _value; }
|
|
std::string getPlaceholder() const { return _placeholder; }
|
|
|
|
void setFocused(bool focused) override {
|
|
_focused = focused;
|
|
}
|
|
|
|
void setHovered(bool hovered) override { _hovered = hovered; }
|
|
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) {
|
|
style.backgroundColor = Vector(0.14f, 0.15f, 0.18f);
|
|
} else if (_hovered) {
|
|
style.backgroundColor = Vector(0.12f, 0.12f, 0.14f);
|
|
} else {
|
|
style.backgroundColor = Vector(0.10f, 0.10f, 0.12f);
|
|
}
|
|
// Switch between placeholder (dimmed) and value (full color) display.
|
|
if (_text == nullptr) {
|
|
return;
|
|
}
|
|
auto* t = dynamic_cast<Text<classes...>*>(_text.getHandle());
|
|
if (t == nullptr) {
|
|
return;
|
|
}
|
|
if (_value.empty()) {
|
|
t->setText(_placeholder);
|
|
style.textColor = th.color(ColorToken::Gray600);
|
|
} else {
|
|
t->setText(_value);
|
|
style.textColor = th.color(ColorToken::White);
|
|
}
|
|
markDirty();
|
|
}
|
|
|
|
private:
|
|
std::string _placeholder;
|
|
std::string _value;
|
|
bool _focused = false;
|
|
bool _hovered = false;
|
|
PElement _text;
|
|
};
|
|
} // namespace UI
|
|
} // namespace Seele
|