From 3d5e8baa5f66547f1250950b10bd4108e30af423 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 25 Aug 2024 13:39:33 +0200 Subject: File chooser navigation --- include/gui/Button.hpp | 1 + include/gui/FileChooser.hpp | 46 +++++++++++++++++++++++++++++++++++++----- include/gui/GsrPage.hpp | 13 ++++++++++-- include/gui/ScrollablePage.hpp | 4 ++++ include/gui/Utils.hpp | 2 ++ 5 files changed, 59 insertions(+), 7 deletions(-) (limited to 'include/gui') diff --git a/include/gui/Button.hpp b/include/gui/Button.hpp index a4dcca1..bc1dd94 100644 --- a/include/gui/Button.hpp +++ b/include/gui/Button.hpp @@ -22,6 +22,7 @@ namespace gsr { void set_border_scale(float scale); const std::string& get_text() const; + void set_text(std::string str); std::function on_click; private: diff --git a/include/gui/FileChooser.hpp b/include/gui/FileChooser.hpp index 309ff56..36dac2d 100644 --- a/include/gui/FileChooser.hpp +++ b/include/gui/FileChooser.hpp @@ -1,16 +1,51 @@ #pragma once #include "Widget.hpp" +#include "ScrollablePage.hpp" #include #include #include +#include #include // This currently only supports displaying folders // TODO: Support files as well namespace gsr { + class FileChooser; + + class FileChooserBody : public Widget { + friend class FileChooser; + public: + FileChooserBody(FileChooser *file_chooser, mgl::vec2f size); + FileChooserBody(const FileChooserBody&) = delete; + FileChooserBody& operator=(const FileChooserBody&) = 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; + mgl::vec2f get_inner_size() override; + void set_size(mgl::vec2f size); + private: + void set_current_directory(const char *directory); + private: + struct Folder { + mgl::Text text; + time_t last_modified_seconds = 0; + }; + + FileChooser *file_chooser = nullptr; + mgl::vec2f size; + mgl::vec2f inner_size; + std::vector folders; + int mouse_over_item = -1; + int selected_item = -1; + mgl::Clock double_click_timer; + int times_clicked_within_timer = 0; + }; + class FileChooser : public Widget { public: FileChooser(const char *start_directory, mgl::vec2f size); @@ -23,6 +58,9 @@ namespace gsr { mgl::vec2f get_size() override; void set_current_directory(const char *directory); + void open_subdirectory(const char *name); + void open_parent_directory(); + const std::string& get_current_directory() const; private: struct Folder { mgl::Text text; @@ -31,10 +69,8 @@ namespace gsr { mgl::vec2f size; mgl::Text current_directory_text; - int mouse_over_item = -1; - int selected_item = -1; - std::vector folders; - mgl::Clock double_click_timer; - int times_clicked_within_timer = 0; + mgl::Sprite up_arrow_sprite; + ScrollablePage scrollable_page; + FileChooserBody *file_chooser_body_ptr = nullptr; }; } \ No newline at end of file diff --git a/include/gui/GsrPage.hpp b/include/gui/GsrPage.hpp index 916055a..1d298f4 100644 --- a/include/gui/GsrPage.hpp +++ b/include/gui/GsrPage.hpp @@ -3,6 +3,7 @@ #include "Page.hpp" #include "Button.hpp" +#include #include namespace gsr { @@ -19,9 +20,12 @@ namespace gsr { mgl::vec2f get_inner_size() override; void set_margins(float top, float bottom, float left, float right); - void set_on_back_button_click(std::function on_click_handler); + void add_button(const std::string &text, const std::string &id, mgl::Color color); + + std::function on_click; private: void draw_page_label(mgl::Window &window, mgl::vec2f body_pos); + void draw_buttons(mgl::Window &window, mgl::vec2f body_pos, mgl::vec2f body_size); void draw_children(mgl::Window &window, mgl::vec2f position); float get_border_size() const; @@ -29,11 +33,16 @@ namespace gsr { mgl::vec2f get_content_position(); mgl::vec2f get_content_position_with_margin(); private: + struct ButtonItem { + std::unique_ptr