From 1a00db9d554e7903fb9ef207846e4a4f4fa35026 Mon Sep 17 00:00:00 2001 From: ITotalJustice <47043333+ITotalJustice@users.noreply.github.com> Date: Sat, 2 Aug 2025 19:18:42 +0100 Subject: [PATCH] sidebar: add text input entry --- sphaira/include/ui/sidebar.hpp | 29 +++++++++++++++++++++++++++-- sphaira/source/ui/sidebar.cpp | 21 +++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/sphaira/include/ui/sidebar.hpp b/sphaira/include/ui/sidebar.hpp index 88eeda5..20e2666 100644 --- a/sphaira/include/ui/sidebar.hpp +++ b/sphaira/include/ui/sidebar.hpp @@ -77,10 +77,9 @@ public: explicit SidebarEntryBool(const std::string& title, bool& option, const std::string& info = "", const std::string& true_str = "On", const std::string& false_str = "Off"); explicit SidebarEntryBool(const std::string& title, option::OptionBool& option, const Callback& cb, const std::string& info = "", const std::string& true_str = "On", const std::string& false_str = "Off"); explicit SidebarEntryBool(const std::string& title, option::OptionBool& option, const std::string& info = "", const std::string& true_str = "On", const std::string& false_str = "Off"); - -private: void Draw(NVGcontext* vg, Theme* theme, const Vec4& root_pos, bool left) override; +private: bool m_option; Callback m_callback; std::string m_true_str; @@ -125,6 +124,32 @@ private: float m_text_yoff{}; }; +class SidebarEntryTextInput final : public SidebarEntryBase { +public: + using Callback = std::function; + +public: + explicit SidebarEntryTextInput(const std::string& text, const std::string& guide = {}, const std::string& info = ""); + + void Draw(NVGcontext* vg, Theme* theme, const Vec4& root_pos, bool left) override; + + auto GetText() const { + return m_title; + } + + void SetText(const std::string& text) { + m_title = text; + } + + void SetGuide(const std::string& guide) { + m_guide = guide; + } + +private: + std::string m_guide; + ScrollingText m_scolling_title{}; +}; + class Sidebar final : public Widget { public: enum class Side { LEFT, RIGHT }; diff --git a/sphaira/source/ui/sidebar.cpp b/sphaira/source/ui/sidebar.cpp index ed69e6b..ef8bd65 100644 --- a/sphaira/source/ui/sidebar.cpp +++ b/sphaira/source/ui/sidebar.cpp @@ -3,6 +3,7 @@ #include "ui/popup_list.hpp" #include "ui/nvg_util.hpp" #include "i18n.hpp" +#include "swkbd.hpp" #include namespace sphaira::ui { @@ -284,6 +285,26 @@ auto SidebarEntryArray::OnFocusLost() noexcept -> void { m_text_yoff = 0; } +SidebarEntryTextInput::SidebarEntryTextInput(const std::string& text, const std::string& guide, const std::string& info) +: SidebarEntryBase{text, info}, m_guide{guide} { + SetAction(Button::A, Action{"OK"_i18n, [this](){ + std::string out; + if (R_SUCCEEDED(swkbd::ShowText(out, m_guide.c_str(), m_title.c_str()))) { + m_title = out; + } + }}); +} + +void SidebarEntryTextInput::Draw(NVGcontext* vg, Theme* theme, const Vec4& root_pos, bool left) { + SidebarEntryBase::Draw(vg, theme, root_pos, left); + + const auto colour_id = IsEnabled() ? ThemeEntryID_TEXT : ThemeEntryID_TEXT_INFO; + const auto max_w = m_pos.w - 15.f * 2; + + m_scolling_title.Draw(vg, HasFocus(), m_pos.x + 15.f, m_pos.y + (m_pos.h / 2.f), max_w, 20.f, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE, theme->GetColour(colour_id), m_title); + // gfx::drawText(vg, Vec2{m_pos.x + 15.f, m_pos.y + (m_pos.h / 2.f)}, 20.f, theme->GetColour(colour_id), m_title.c_str(), NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE); +} + Sidebar::Sidebar(const std::string& title, Side side, Items&& items) : Sidebar{title, "", side, std::forward(items)} { }