From 497217a3e09b577f650aaf503356588515067ca8 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 25 Sep 2022 17:29:17 +0200 Subject: widgets --- include/gui/Button.hpp | 7 ++++++- include/gui/ComboBox.hpp | 36 ++++++++++++++++++++++++++++++++++++ include/gui/Widget.hpp | 12 ++++++++++-- include/gui/WidgetContainer.hpp | 30 ++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 include/gui/ComboBox.hpp create mode 100644 include/gui/WidgetContainer.hpp (limited to 'include/gui') diff --git a/include/gui/Button.hpp b/include/gui/Button.hpp index 84098b2..972e5c9 100644 --- a/include/gui/Button.hpp +++ b/include/gui/Button.hpp @@ -8,9 +8,14 @@ namespace gsr { class Button : public Widget { public: Button(mgl::vec2f size); - void on_event(mgl::Event &event, mgl::Window &window) override; + Button(const Button&) = delete; + Button& operator=(const Button&) = delete; + + bool on_event(mgl::Event &event, mgl::Window &window) override; void draw(mgl::Window &window) override; + mgl::vec2f get_size() const { return size; } + std::function on_click; private: mgl::vec2f size; diff --git a/include/gui/ComboBox.hpp b/include/gui/ComboBox.hpp new file mode 100644 index 0000000..5c899a7 --- /dev/null +++ b/include/gui/ComboBox.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include "Widget.hpp" +#include +#include +#include + +namespace gsr { + class ComboBox : public Widget { + public: + ComboBox(mgl::Font *font); + ComboBox(const ComboBox&) = delete; + ComboBox& operator=(const ComboBox&) = delete; + + bool on_event(mgl::Event &event, mgl::Window &window) override; + void draw(mgl::Window &window) override; + + void add_item(const std::string &text, const std::string &id); + + mgl::vec2f get_size(); + private: + void update_if_dirty(); + private: + struct Item { + mgl::Text text; + std::string id; + }; + + mgl::vec2f max_size; + mgl::Font *font; + std::vector items; + bool dirty = true; + bool show_dropdown = false; + size_t selected_item = 0; + }; +} \ No newline at end of file diff --git a/include/gui/Widget.hpp b/include/gui/Widget.hpp index cf81d69..81f47b5 100644 --- a/include/gui/Widget.hpp +++ b/include/gui/Widget.hpp @@ -9,13 +9,21 @@ namespace mgl { namespace gsr { class Widget { + friend class WidgetContainer; public: - virtual ~Widget() = default; + Widget(); + Widget(const Widget&) = delete; + Widget& operator=(const Widget&) = delete; + virtual ~Widget(); - virtual void on_event(mgl::Event &event, mgl::Window &window) = 0; + // Return true to allow other widgets to also process the event + virtual bool on_event(mgl::Event &event, mgl::Window &window) = 0; virtual void draw(mgl::Window &window) = 0; virtual void set_position(mgl::vec2f position); + + virtual mgl::vec2f get_position() const; protected: mgl::vec2f position; + bool move_to_top = false; }; } \ No newline at end of file diff --git a/include/gui/WidgetContainer.hpp b/include/gui/WidgetContainer.hpp new file mode 100644 index 0000000..e40ffb9 --- /dev/null +++ b/include/gui/WidgetContainer.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include +#include + +namespace mgl { + class Event; + class Window; +} + +namespace gsr { + class Widget; + + class WidgetContainer { + public: + static WidgetContainer& get_instance(); + + void add_widget(Widget *widget); + void remove_widget(Widget *widget); + + void on_event(mgl::Event &event, mgl::Window &window); + void draw(mgl::Window &window); + private: + WidgetContainer() = default; + WidgetContainer& operator=(const WidgetContainer&) = delete; + WidgetContainer(const WidgetContainer&) = delete; + private: + std::vector widgets; + }; +} \ No newline at end of file -- cgit v1.2.3