From 78bda75985659ba4150577c06a629a01a7d14c25 Mon Sep 17 00:00:00 2001 From: ITotalJustice <47043333+ITotalJustice@users.noreply.github.com> Date: Mon, 6 Jan 2025 22:37:38 +0000 Subject: [PATCH] add touch support (#77) * initial work on touch support * add touch support to all objects * add touch scrolling, fix scrollbar, fix appstore search - when fireing an action, the action array may change. so the loop should break early as soon as an action is handled. this fixes the appstore search when pressing B. - scrollbar no longer goes oob. fixes #76 currently, scrolling has no acceleration. --- sphaira/CMakeLists.txt | 3 +- sphaira/include/app.hpp | 8 +- sphaira/include/ui/error_box.hpp | 1 - sphaira/include/ui/list.hpp | 57 +++++ sphaira/include/ui/menus/appstore.hpp | 22 +- sphaira/include/ui/menus/file_viewer.hpp | 4 +- sphaira/include/ui/menus/filebrowser.hpp | 17 +- sphaira/include/ui/menus/ghdl.hpp | 8 +- sphaira/include/ui/menus/homebrew.hpp | 7 +- sphaira/include/ui/menus/irs_menu.hpp | 2 +- sphaira/include/ui/menus/main_menu.hpp | 3 +- sphaira/include/ui/menus/menu_base.hpp | 3 - sphaira/include/ui/menus/themezer.hpp | 14 +- sphaira/include/ui/notification.hpp | 2 - sphaira/include/ui/nvg_util.hpp | 9 +- sphaira/include/ui/object.hpp | 2 - sphaira/include/ui/option_box.hpp | 13 +- sphaira/include/ui/option_list.hpp | 27 --- sphaira/include/ui/popup_list.hpp | 23 +- sphaira/include/ui/progress_box.hpp | 6 +- sphaira/include/ui/scrollbar.hpp | 34 --- sphaira/include/ui/sidebar.hpp | 24 +- sphaira/include/ui/types.hpp | 40 ++-- sphaira/include/ui/widget.hpp | 24 +- sphaira/source/app.cpp | 61 ++--- sphaira/source/download.cpp | 2 +- sphaira/source/ui/error_box.cpp | 4 - sphaira/source/ui/list.cpp | 189 ++++++++++++++++ sphaira/source/ui/menus/appstore.cpp | 277 +++++++++++------------ sphaira/source/ui/menus/filebrowser.cpp | 90 +++----- sphaira/source/ui/menus/ghdl.cpp | 68 ++---- sphaira/source/ui/menus/homebrew.cpp | 136 ++++++----- sphaira/source/ui/menus/irs_menu.cpp | 16 +- sphaira/source/ui/menus/main_menu.cpp | 51 ++--- sphaira/source/ui/menus/menu_base.cpp | 57 ----- sphaira/source/ui/menus/themezer.cpp | 192 ++++++++-------- sphaira/source/ui/notification.cpp | 9 - sphaira/source/ui/nvg_util.cpp | 107 ++++----- sphaira/source/ui/option_box.cpp | 56 ++--- sphaira/source/ui/option_list.cpp | 33 --- sphaira/source/ui/popup_list.cpp | 122 +++++----- sphaira/source/ui/progress_box.cpp | 2 +- sphaira/source/ui/scrollbar.cpp | 68 ------ sphaira/source/ui/sidebar.cpp | 158 ++++++------- sphaira/source/ui/widget.cpp | 98 +++++++- 45 files changed, 1080 insertions(+), 1069 deletions(-) create mode 100644 sphaira/include/ui/list.hpp delete mode 100644 sphaira/include/ui/option_list.hpp delete mode 100644 sphaira/include/ui/scrollbar.hpp create mode 100644 sphaira/source/ui/list.cpp delete mode 100644 sphaira/source/ui/option_list.cpp delete mode 100644 sphaira/source/ui/scrollbar.cpp diff --git a/sphaira/CMakeLists.txt b/sphaira/CMakeLists.txt index dc031dd..2c244bc 100644 --- a/sphaira/CMakeLists.txt +++ b/sphaira/CMakeLists.txt @@ -51,13 +51,12 @@ add_executable(sphaira source/ui/notification.cpp source/ui/nvg_util.cpp source/ui/option_box.cpp - source/ui/option_list.cpp source/ui/popup_list.cpp source/ui/progress_box.cpp source/ui/scrollable_text.cpp - source/ui/scrollbar.cpp source/ui/sidebar.cpp source/ui/widget.cpp + source/ui/list.cpp source/app.cpp source/download.cpp diff --git a/sphaira/include/app.hpp b/sphaira/include/app.hpp index cebb2c6..1edd6a3 100644 --- a/sphaira/include/app.hpp +++ b/sphaira/include/app.hpp @@ -33,7 +33,9 @@ enum class LaunchType { Forwader_Sphaira, }; +// todo: why is this global??? void DrawElement(float x, float y, float w, float h, ThemeEntryID id); +void DrawElement(const Vec4&, ThemeEntryID id); class App { public: @@ -56,8 +58,8 @@ public: static void NotifyFlashLed(); static auto GetThemeMetaList() -> std::span; - static void SetTheme(u64 theme_index); - static auto GetThemeIndex() -> u64; + static void SetTheme(s64 theme_index); + static auto GetThemeIndex() -> s64; static auto GetDefaultImage(int* w = nullptr, int* h = nullptr) -> int; @@ -143,7 +145,7 @@ public: Theme m_theme{}; fs::FsPath theme_path{}; - std::size_t m_theme_index{}; + s64 m_theme_index{}; bool m_quit{}; diff --git a/sphaira/include/ui/error_box.hpp b/sphaira/include/ui/error_box.hpp index e6e58f8..a2327e5 100644 --- a/sphaira/include/ui/error_box.hpp +++ b/sphaira/include/ui/error_box.hpp @@ -10,7 +10,6 @@ public: ErrorBox(Result code, const std::string& message); auto Update(Controller* controller, TouchInfo* touch) -> void override; - auto OnLayoutChange() -> void override; auto Draw(NVGcontext* vg, Theme* theme) -> void override; private: diff --git a/sphaira/include/ui/list.hpp b/sphaira/include/ui/list.hpp new file mode 100644 index 0000000..a30514d --- /dev/null +++ b/sphaira/include/ui/list.hpp @@ -0,0 +1,57 @@ +#pragma once + +#include "ui/object.hpp" + +namespace sphaira::ui { + +struct List final : Object { + using Callback = std::function; + using TouchCallback = std::function; + + List(s64 row, s64 page, const Vec4& pos, const Vec4& v, const Vec2& pad = {}); + + void OnUpdate(Controller* controller, TouchInfo* touch, s64 count, TouchCallback callback); + + void Draw(NVGcontext* vg, Theme* theme, s64 count, Callback callback) const; + + auto SetScrollBarPos(float x, float y, float h) { + m_scrollbar.x = x; + m_scrollbar.y = y; + m_scrollbar.h = h; + } + + auto ScrollDown(s64& index, s64 step, s64 count) -> bool; + auto ScrollUp(s64& index, s64 step, s64 count) -> bool; + + auto GetYoff() const { + return m_yoff; + } + + void SetYoff(float y = 0) { + m_yoff = y; + } + + auto GetMaxY() const { + return m_v.h + m_pad.y; + } + +private: + auto Draw(NVGcontext* vg, Theme* theme) -> void override {} + auto ClampY(float y, s64 count) const -> float; + +private: + const s64 m_row; + const s64 m_page; + + Vec4 m_v; + Vec2 m_pad; + + Vec4 m_scrollbar{}; + + // current y offset. + float m_yoff{}; + // in progress y offset, used when scrolling. + float m_y_prog{}; +}; + +} // namespace sphaira::ui diff --git a/sphaira/include/ui/menus/appstore.hpp b/sphaira/include/ui/menus/appstore.hpp index d31c48c..9e179c1 100644 --- a/sphaira/include/ui/menus/appstore.hpp +++ b/sphaira/include/ui/menus/appstore.hpp @@ -2,6 +2,7 @@ #include "ui/menus/menu_base.hpp" #include "ui/scrollable_text.hpp" +#include "ui/list.hpp" #include "nro.hpp" #include "fs.hpp" #include @@ -77,7 +78,7 @@ struct EntryMenu final : MenuBase { // void OnFocusGained() override; void ShowChangelogAction(); - void SetIndex(std::size_t index); + void SetIndex(s64 index); void UpdateOptions(); @@ -97,10 +98,10 @@ private: const LazyImage& m_default_icon; Menu& m_menu; - std::size_t m_index{}; // where i am in the array + s64 m_index{}; // where i am in the array std::vector