summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2025-02-07 00:27:35 +1100
committerquou <quou@disroot.org>2025-02-07 00:27:35 +1100
commit2dfa9cfaec5e6e96c8193af5504ffd0b6a8e9a69 (patch)
tree2020116df1f519b46ad2d8fc9b1f2b754af9be1a
parent7686aae2667fffd798ee2f0dc7ca1252a36612c5 (diff)
ui int input
-rw-r--r--ui.cpp27
-rw-r--r--ui.hpp7
2 files changed, 33 insertions, 1 deletions
diff --git a/ui.cpp b/ui.cpp
index 8ef6c10..2e21d54 100644
--- a/ui.cpp
+++ b/ui.cpp
@@ -1054,8 +1054,12 @@ void UI::Input::add_text(const char* s) {
}
}
+static int int_filter(char ch) {
+ return (ch >= '0' && ch <= '9');
+}
+
static int float_filter(char ch) {
- return (ch >= '0' && ch <= '9') || ch == '.' || ch == '-';
+ return int_filter(ch) || ch == '.' || ch == '-';
}
UI::Float_Input::Float_Input(UI* ui, Element* parent):
@@ -1079,6 +1083,27 @@ void UI::Float_Input::set_val(float v) {
set_text(b);
}
+UI::Int_Input::Int_Input(UI* ui, Element* parent):
+ Input(ui, parent, int_filter),
+ val(0.0f) {
+ set_text("0.0");
+}
+
+void UI::Int_Input::on_message(const Message& m) {
+ Input::on_message(m);
+ if (m.type == UI::Message::Type::input_finalised) {
+ int v = (int)strtol(buf, 0, 10);
+ set_val(v);
+ }
+}
+
+void UI::Int_Input::set_val(int v) {
+ char b[64];
+ val = v;
+ sprintf(b, "%d", v);
+ set_text(b);
+}
+
UI::Slider::Slider(
UI* ui,
Element* parent,
diff --git a/ui.hpp b/ui.hpp
index 0ee811e..056e69a 100644
--- a/ui.hpp
+++ b/ui.hpp
@@ -234,6 +234,13 @@ struct UI {
void set_val(float v);
};
+ struct Int_Input : Input {
+ int val;
+ Int_Input(UI* ui, Element* parent);
+ void on_message(const Message& m) override;
+ void set_val(int v);
+ };
+
struct Slider : Element {
Float_Input* input;
float minval, maxval;