sidebar: add text input entry

This commit is contained in:
ITotalJustice
2025-08-02 19:18:42 +01:00
parent fb3ad260da
commit 1a00db9d55
2 changed files with 48 additions and 2 deletions

View File

@@ -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<void(bool&)>;
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 };

View File

@@ -3,6 +3,7 @@
#include "ui/popup_list.hpp"
#include "ui/nvg_util.hpp"
#include "i18n.hpp"
#include "swkbd.hpp"
#include <algorithm>
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<decltype(items)>(items)} {
}