Skip to content

Commit

Permalink
add text scrolling to sidebar array
Browse files Browse the repository at this point in the history
see #87
  • Loading branch information
ITotalJustice committed Jan 12, 2025
1 parent fd1d461 commit c762daf
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 4 deletions.
4 changes: 4 additions & 0 deletions sphaira/include/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class App {
static auto GetThemeShuffleEnable() -> bool;
static auto GetThemeMusicEnable() -> bool;
static auto GetLanguage() -> long;
static auto GetTextScrollSpeed() -> long;

static void SetMtpEnable(bool enable);
static void SetFtpEnable(bool enable);
Expand All @@ -92,6 +93,7 @@ class App {
static void SetThemeShuffleEnable(bool enable);
static void SetThemeMusicEnable(bool enable);
static void SetLanguage(long index);
static void SetTextScrollSpeed(long index);

static auto Install(OwoConfig& config) -> Result;
static auto Install(ui::ProgressBox* pbox, OwoConfig& config) -> Result;
Expand Down Expand Up @@ -161,6 +163,8 @@ class App {
option::OptionBool m_theme_shuffle{INI_SECTION, "theme_shuffle", false};
option::OptionBool m_theme_music{INI_SECTION, "theme_music", true};
option::OptionLong m_language{INI_SECTION, "language", 0}; // auto
// todo: move this into it's own menu
option::OptionLong m_text_scroll_speed{"accessibility", "text_scroll_speed", 1}; // normal

PLSR_BFSAR m_qlaunch_bfsar{};
PLSR_PlayerSoundId m_sound_ids[SoundEffect_MAX]{};
Expand Down
4 changes: 4 additions & 0 deletions sphaira/include/ui/sidebar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ class SidebarEntryArray final : public SidebarEntryBase {
SidebarEntryArray(std::string title, Items items, std::string& index);

auto Draw(NVGcontext* vg, Theme* theme) -> void override;
auto OnFocusGained() noexcept -> void override;
auto OnFocusLost() noexcept -> void override;

private:
Items m_items;
ListCallback m_list_callback;
Callback m_callback;
s64 m_index;
s64 m_tick{};
float m_text_yoff{};
};

template <typename T>
Expand Down
8 changes: 8 additions & 0 deletions sphaira/source/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,10 @@ auto App::GetLanguage() -> long {
return g_app->m_language.Get();
}

auto App::GetTextScrollSpeed() -> long {
return g_app->m_text_scroll_speed.Get();
}

void App::SetNxlinkEnable(bool enable) {
if (App::GetNxlinkEnable() != enable) {
g_app->m_nxlink_enabled.Set(enable);
Expand Down Expand Up @@ -767,6 +771,10 @@ void App::SetLanguage(long index) {
}
}

void App::SetTextScrollSpeed(long index) {
g_app->m_text_scroll_speed.Set(index);
}

auto App::Install(OwoConfig& config) -> Result {
R_TRY(romfsInit());
ON_SCOPE_EXIT(romfsExit());
Expand Down
9 changes: 9 additions & 0 deletions sphaira/source/ui/menus/main_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ MainMenu::MainMenu() {
install_items.push_back("System memory"_i18n);
install_items.push_back("microSD card"_i18n);

SidebarEntryArray::Items text_scroll_speed_items;
text_scroll_speed_items.push_back("Slow"_i18n);
text_scroll_speed_items.push_back("Normal"_i18n);
text_scroll_speed_items.push_back("Fast"_i18n);

options->Add(std::make_shared<SidebarEntryBool>("Logging"_i18n, App::GetLogEnable(), [this](bool& enable){
App::SetLogEnable(enable);
}, "Enabled"_i18n, "Disabled"_i18n));
Expand All @@ -348,6 +353,10 @@ MainMenu::MainMenu() {
options->Add(std::make_shared<SidebarEntryBool>("Show install warning"_i18n, App::GetInstallPrompt(), [this](bool& enable){
App::SetInstallPrompt(enable);
}, "Enabled"_i18n, "Disabled"_i18n));

options->Add(std::make_shared<SidebarEntryArray>("Text scroll speed"_i18n, text_scroll_speed_items, [this](s64& index_out){
App::SetTextScrollSpeed(index_out);
}, (s64)App::GetTextScrollSpeed()));
}));
}})
);
Expand Down
64 changes: 60 additions & 4 deletions sphaira/source/ui/sidebar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
namespace sphaira::ui {
namespace {

auto GetTextScrollSpeed() -> float {
switch (App::GetTextScrollSpeed()) {
case 0: return 0.5;
default: case 1: return 1.0;
case 2: return 1.5;
}
}

auto DistanceBetweenY(Vec4 va, Vec4 vb) -> Vec4 {
return Vec4{
va.x, va.y,
Expand Down Expand Up @@ -147,11 +155,59 @@ auto SidebarEntryArray::Draw(NVGcontext* vg, Theme* theme) -> void {
SidebarEntryBase::Draw(vg, theme);

const auto& text_entry = m_items[m_index];
// const auto& colour = HasFocus() ? theme->GetColour(ThemeEntryID_TEXT_SELECTED) : theme->GetColour(ThemeEntryID_TEXT);
const auto& colour = theme->GetColour(ThemeEntryID_TEXT);

gfx::drawText(vg, Vec2{m_pos.x + 15.f, m_pos.y + (m_pos.h / 2.f)}, 20.f, colour, m_title.c_str(), NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
gfx::drawText(vg, Vec2{m_pos.x + m_pos.w - 15.f, m_pos.y + (m_pos.h / 2.f)}, 20.f, theme->GetColour(ThemeEntryID_TEXT_SELECTED), text_entry.c_str(), NVG_ALIGN_RIGHT | NVG_ALIGN_MIDDLE);
// scrolling text
// todo: move below in a flexible class and use it for all text drawing.
float bounds[4];
nvgFontSize(vg, 20);
nvgTextAlign(vg, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
nvgTextBounds(vg, 0, 0, m_title.c_str(), nullptr, bounds);
const float start_x = bounds[2] + 50;
const float max_off = m_pos.w - start_x - 15.f;

auto value_str = m_items[m_index];
nvgTextBounds(vg, 0, 0, value_str.c_str(), nullptr, bounds);

if (HasFocus()) {
const auto scroll_amount = GetTextScrollSpeed();
if (bounds[2] > max_off) {
value_str += " ";
nvgTextBounds(vg, 0, 0, value_str.c_str(), nullptr, bounds);

if (!m_text_yoff) {
m_tick++;
if (m_tick >= 90) {
m_tick = 0;
m_text_yoff += scroll_amount;
}
} else if (bounds[2] > m_text_yoff) {
m_text_yoff += std::min(scroll_amount, bounds[2] - m_text_yoff);
} else {
m_text_yoff = 0;
}

value_str += text_entry;
}
}

const Vec2 key_text_pos{m_pos.x + 15.f, m_pos.y + (m_pos.h / 2.f)};
gfx::drawText(vg, key_text_pos, 20.f, theme->GetColour(ThemeEntryID_TEXT), m_title.c_str(), NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);

nvgSave(vg);
const float xpos = m_pos.x + m_pos.w - 15.f - std::min(max_off, bounds[2]);
nvgIntersectScissor(vg, xpos, GetY(), max_off, GetH());
const Vec2 value_text_pos{xpos - m_text_yoff, m_pos.y + (m_pos.h / 2.f)};
gfx::drawText(vg, value_text_pos, 20.f, theme->GetColour(ThemeEntryID_TEXT_SELECTED), value_str.c_str(), NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
nvgRestore(vg);
}

auto SidebarEntryArray::OnFocusGained() noexcept -> void {
Widget::OnFocusGained();
}

auto SidebarEntryArray::OnFocusLost() noexcept -> void {
Widget::OnFocusLost();
m_text_yoff = 0;
}

Sidebar::Sidebar(std::string title, Side side, Items&& items)
Expand Down

0 comments on commit c762daf

Please sign in to comment.