From a3e479d5b289166ff651fac4ff046656cf78cda0 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 7 Aug 2024 00:21:32 +0200 Subject: Add radio button with simple/advanced view, add widget visibility --- include/gui/RadioButton.hpp | 39 +++++++++++++++++++++++++++++++++++++++ include/gui/ScrollablePage.hpp | 10 ++++++++-- include/gui/StaticPage.hpp | 2 +- include/gui/Widget.hpp | 20 ++++++++++++++++++++ 4 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 include/gui/RadioButton.hpp (limited to 'include/gui') diff --git a/include/gui/RadioButton.hpp b/include/gui/RadioButton.hpp new file mode 100644 index 0000000..d4b1103 --- /dev/null +++ b/include/gui/RadioButton.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include "Widget.hpp" + +#include +#include +#include + +namespace gsr { + class RadioButton : public Widget { + public: + RadioButton(mgl::Font *font); + RadioButton(const RadioButton&) = delete; + RadioButton& operator=(const RadioButton&) = delete; + + bool on_event(mgl::Event &event, mgl::Window &window, mgl::vec2f offset) override; + void draw(mgl::Window &window, mgl::vec2f offset) override; + + void add_item(const std::string &text, const std::string &id); + void set_selected_item(const std::string &id); + + mgl::vec2f get_size() override; + + std::function on_selection_changed; + private: + void update_if_dirty(); + private: + struct Item { + mgl::Text text; + std::string id; + }; + + mgl::Font *font; + std::vector items; + size_t selected_item = 0; + bool dirty = true; + mgl::vec2f size; + }; +} \ No newline at end of file diff --git a/include/gui/ScrollablePage.hpp b/include/gui/ScrollablePage.hpp index 2ad3c92..9d4623b 100644 --- a/include/gui/ScrollablePage.hpp +++ b/include/gui/ScrollablePage.hpp @@ -12,10 +12,16 @@ namespace gsr { 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 { return size; } + mgl::vec2f get_size() override; + + void set_margins(float top, float bottom, float left, float right); private: - float get_border_size(mgl::Window &window) const; + float get_border_size() const; private: mgl::vec2f size; + float margin_top_scale = 0.0f; + float margin_bottom_scale = 0.0f; + float margin_left_scale = 0.0f; + float margin_right_scale = 0.0f; }; } \ No newline at end of file diff --git a/include/gui/StaticPage.hpp b/include/gui/StaticPage.hpp index 8b64ba6..e73083c 100644 --- a/include/gui/StaticPage.hpp +++ b/include/gui/StaticPage.hpp @@ -12,7 +12,7 @@ namespace gsr { 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 { return size; } + mgl::vec2f get_size() override; private: mgl::vec2f size; }; diff --git a/include/gui/Widget.hpp b/include/gui/Widget.hpp index 6c4a1cb..7a04338 100644 --- a/include/gui/Widget.hpp +++ b/include/gui/Widget.hpp @@ -14,6 +14,12 @@ namespace gsr { friend class List; friend class Page; public: + enum class Alignment { + START, + CENTER, + END + }; + Widget(); Widget(const Widget&) = delete; Widget& operator=(const Widget&) = delete; @@ -28,12 +34,26 @@ namespace gsr { virtual mgl::vec2f get_position() const; virtual mgl::vec2f get_size() = 0; + + void set_horizontal_alignment(Alignment alignment); + void set_vertical_alignment(Alignment alignment); + + Alignment get_horizontal_alignment() const; + Alignment get_vertical_alignment() const; + + void set_visible(bool visible); protected: void set_widget_as_selected_in_parent(); void remove_widget_as_selected_in_parent(); + bool has_parent_with_selected_child_widget() const; protected: mgl::vec2f position; Widget *parent_widget = nullptr; Widget *selected_child_widget = nullptr; + + Alignment horizontal_aligment; + Alignment vertical_aligment; + + bool visible = true; }; } \ No newline at end of file -- cgit v1.2.3