summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ui.cpp40
-rw-r--r--ui.hpp12
2 files changed, 51 insertions, 1 deletions
diff --git a/ui.cpp b/ui.cpp
index e20a18c..1e18fc0 100644
--- a/ui.cpp
+++ b/ui.cpp
@@ -895,6 +895,46 @@ void UI::Button::on_render() {
);
}
+UI::Checkbox::Checkbox(
+ UI* ui,
+ Element* parent,
+ const char* text
+): Element(ui, parent) {
+ btn = ui->create_element<Button>(this, " ");
+ btn->handler = [](UI::Element* e, const Message& m) {
+ if (m.type == UI::Message::Type::click) {
+ UI::Checkbox* ch = (UI::Checkbox*)e->parent;
+ ch->set_val(!ch->val);
+ }
+ return 0;
+ };
+ label = ui->create_element<Label>(this, text);
+ val = false;
+}
+
+UI::Rect UI::Checkbox::layout(const Rect& avail) {
+ int w;
+ Rect r = avail;
+ Rect br = btn->layout(avail);
+ w = br.w + ui_padding;
+ r.x += w;
+ r.w -= w;
+ r.clip(avail);
+ label->layout(r);
+ bound = Rect(br.x, br.y, br.w + ui_padding * 2, br.h);
+ clip = bound;
+ clip.clip(ui->area);
+ return bound;
+}
+
+void UI::Checkbox::set_val(bool v) {
+ Message msg{};
+ btn->set_text(v? "x": " ");
+ val = v;
+ msg.type = Message::Type::checkbox_changed;
+ message(msg);
+}
+
UI::Input::Input(UI* ui, Element* parent, Input_Filter f):
Element(ui, parent),
filter(f),
diff --git a/ui.hpp b/ui.hpp
index 576f7a0..dbf6fb6 100644
--- a/ui.hpp
+++ b/ui.hpp
@@ -117,7 +117,8 @@ struct UI {
input_changed,
input_finalised,
text_typed,
- text_backspaced
+ text_backspaced,
+ checkbox_changed
} type;
void* payload;
};
@@ -201,6 +202,15 @@ struct UI {
void on_render() override;
};
+ struct Checkbox : Element {
+ Label* label;
+ Button* btn;
+ bool val;
+ Checkbox(UI* ui, Element* parent, const char* text);
+ Rect layout(const Rect& avail) override;
+ void set_val(bool v);
+ };
+
typedef int (*Input_Filter)(char ch);
struct Input : Element {