diff options
author | dec05eba <dec05eba@protonmail.com> | 2024-08-03 05:21:36 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2024-08-03 05:21:36 +0200 |
commit | c080342fcd358561af7edc64cea2222880923b93 (patch) | |
tree | 3a1ed6c92b52f7da5630ccf8d662f8e015f66ca5 /include/gui | |
parent | 2869ef7cec7de6bc744cdba9e753dbd0df4ab65b (diff) |
Add entry with basic text editing and validation for numbers
Diffstat (limited to 'include/gui')
-rw-r--r-- | include/gui/Entry.hpp | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/include/gui/Entry.hpp b/include/gui/Entry.hpp new file mode 100644 index 0000000..c2d59ac --- /dev/null +++ b/include/gui/Entry.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include "Widget.hpp" +#include <functional> + +#include <mglpp/graphics/Color.hpp> +#include <mglpp/graphics/Text.hpp> + +namespace gsr { + using EntryValidateHandler = std::function<bool(std::string &str)>; + + class Entry : public Widget { + public: + Entry(mgl::Font *font, const char *text, float max_width); + Entry(const Entry&) = delete; + Entry& operator=(const Entry&) = delete; + + bool on_event(mgl::Event &event, mgl::Window &window, mgl::vec2f offset) override; + void draw(mgl::Window &window, mgl::vec2f offset) override; + + mgl::vec2f get_size() override; + + void set_string(std::string str); + + // Return false to specify that the string should not be accepted. This reverts the string back to its previous value. + // The input can be changed by changing the input parameter and returning true. + EntryValidateHandler validate_handler; + private: + mgl::Text text; + float max_width; + bool selected = false; + float caret_offset_x = 0.0f; + }; + + EntryValidateHandler create_entry_validator_integer_in_range(int min, int max); +}
\ No newline at end of file |