From 135f0e4ce8f2b65c49e862c54c419da955034719 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 2 Oct 2024 22:50:55 -0500 Subject: [PATCH] [nostalgia/core/studio/paletteeditor] Fix Alt shortcuts to respect keyboard focus --- .../paletteeditor/paletteeditor-imgui.cpp | 59 +++++++------------ .../paletteeditor/paletteeditor-imgui.hpp | 4 +- 2 files changed, 24 insertions(+), 39 deletions(-) diff --git a/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp b/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp index ac179f65..b782709f 100644 --- a/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp +++ b/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp @@ -41,31 +41,10 @@ PaletteEditorImGui::PaletteEditorImGui(studio::StudioContext &sctx, ox::StringPa m_sctx(sctx), m_tctx(sctx.tctx), m_pal(*keel::readObj(keelCtx(m_tctx), itemPath()).unwrapThrow()) { - if (keel::ensureValid(m_pal).errCode) { - throw OxException(1, "PaletteEditorImGui: invalid Palette object"); - } undoStack()->changeTriggered.connect(this, &PaletteEditorImGui::handleCommand); m_pageRename.inputSubmitted.connect(this, &PaletteEditorImGui::renamePage); } -void PaletteEditorImGui::keyStateChanged(turbine::Key key, bool down) { - if (!down || m_pageRename.isOpen()) { - return; - } - if (key >= turbine::Key::Num_1 && key <= turbine::Key::Num_9) { - if (turbine::buttonDown(m_tctx, turbine::Key::Mod_Alt)) { - m_page = ox::min( - static_cast(key - turbine::Key::Num_1), m_pal.pages.size() - 1); - } - } else if (key == turbine::Key::Num_0) { - if (turbine::buttonDown(m_tctx, turbine::Key::Mod_Alt)) { - m_selectedColorRow = - ox::min( - static_cast(key - turbine::Key::Num_1 + 9), m_pal.pages.size() - 1); - } - } -} - void PaletteEditorImGui::draw(studio::StudioContext&) noexcept { auto const paneSize = ImGui::GetContentRegionAvail(); { @@ -98,6 +77,19 @@ void PaletteEditorImGui::drawColumn(ox::CStringView txt) noexcept { ImGui::Text("%s", txt.c_str()); } +void PaletteEditorImGui::numShortcuts(size_t &val, size_t const sizeRange) noexcept { + auto const lastElem = sizeRange - 1; + if (ImGui::IsKeyPressed(ImGuiKey_0)) { + val = ox::min(9, lastElem); + } else for (auto i = 9u; i < 10; --i) { + auto const key = static_cast(ImGuiKey_1 + i); + if (ImGui::IsKeyPressed(key)) { + val = ox::min(i, lastElem); + break; + } + } +} + void PaletteEditorImGui::colorInput(ox::CStringView label, int &v, bool &inputFocused) noexcept { ImGui::InputInt(label.c_str(), &v, 1, 5); inputFocused = inputFocused || ImGui::IsItemFocused(); @@ -240,10 +232,8 @@ void PaletteEditorImGui::drawColorEditor() noexcept { int g = green16(c); int b = blue16(c); int const a = alpha16(c); - auto const¤tName = m_pal.colorNames[m_selectedColorRow]; - ox::IString<50> name; - name = currentName; - auto const nameUpdated = ig::InputText("Name", name); + auto const newName = ig::InputText<50>( + "Name", m_pal.colorNames[m_selectedColorRow]); bool inputFocused = ImGui::IsItemFocused(); ImGui::Separator(); colorInput("Red", r, inputFocused); @@ -253,25 +243,20 @@ void PaletteEditorImGui::drawColorEditor() noexcept { std::ignore = pushCommand( m_pal, m_page, m_selectedColorRow); } - if (!inputFocused && !m_pageRename.isOpen() && !ImGui::IsKeyDown(ImGuiKey_ModAlt)) { - auto const lastColor = largestPage(m_pal) - 1; - if (ImGui::IsKeyPressed(ImGuiKey_0)) { - m_selectedColorRow = ox::min(9, lastColor); - } else for (auto i = 9u; i < 10; --i) { - auto const key = static_cast(ImGuiKey_1 + i); - if (ImGui::IsKeyPressed(key)) { - m_selectedColorRow = ox::min(i, lastColor); - break; - } + if (!inputFocused && !m_pageRename.isOpen()) { + if (!ImGui::IsKeyDown(ImGuiKey_ModAlt)) { + numShortcuts(m_selectedColorRow, largestPage(m_pal)); + } else { + numShortcuts(m_page, m_pal.pages.size()); } } auto const newColor = color16(r, g, b, a); if (c != newColor) { std::ignore = pushCommand(m_pal, m_page, m_selectedColorRow, newColor); } - if (nameUpdated) { + if (newName) { std::ignore = pushCommand( - m_pal, m_selectedColorRow, ox::String{name}); + m_pal, m_selectedColorRow, ox::String{newName}); } } diff --git a/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.hpp b/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.hpp index d11d614f..458dc640 100644 --- a/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.hpp +++ b/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.hpp @@ -40,8 +40,6 @@ class PaletteEditorImGui: public studio::Editor { public: PaletteEditorImGui(studio::StudioContext &sctx, ox::StringParam path); - void keyStateChanged(turbine::Key key, bool down) override; - void draw(studio::StudioContext&) noexcept final; protected: @@ -56,6 +54,8 @@ class PaletteEditorImGui: public studio::Editor { drawColumn(ox::itoa(i)); } + static void numShortcuts(size_t &val, size_t sizeRange) noexcept; + static void colorInput(ox::CStringView label, int &v, bool &inputFocused) noexcept; void drawColorsEditor() noexcept;