[nostalgia/core/studio/paletteeditor] Fix Alt shortcuts to respect keyboard focus
All checks were successful
Build / build (push) Successful in 2m30s

This commit is contained in:
Gary Talent 2024-10-02 22:50:55 -05:00
parent cb16687641
commit 135f0e4ce8
2 changed files with 24 additions and 39 deletions

View File

@ -41,31 +41,10 @@ PaletteEditorImGui::PaletteEditorImGui(studio::StudioContext &sctx, ox::StringPa
m_sctx(sctx), m_sctx(sctx),
m_tctx(sctx.tctx), m_tctx(sctx.tctx),
m_pal(*keel::readObj<Palette>(keelCtx(m_tctx), itemPath()).unwrapThrow()) { m_pal(*keel::readObj<Palette>(keelCtx(m_tctx), itemPath()).unwrapThrow()) {
if (keel::ensureValid(m_pal).errCode) {
throw OxException(1, "PaletteEditorImGui: invalid Palette object");
}
undoStack()->changeTriggered.connect(this, &PaletteEditorImGui::handleCommand); undoStack()->changeTriggered.connect(this, &PaletteEditorImGui::handleCommand);
m_pageRename.inputSubmitted.connect(this, &PaletteEditorImGui::renamePage); 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<std::size_t>(
static_cast<uint32_t>(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<std::size_t>(
static_cast<uint32_t>(key - turbine::Key::Num_1 + 9), m_pal.pages.size() - 1);
}
}
}
void PaletteEditorImGui::draw(studio::StudioContext&) noexcept { void PaletteEditorImGui::draw(studio::StudioContext&) noexcept {
auto const paneSize = ImGui::GetContentRegionAvail(); auto const paneSize = ImGui::GetContentRegionAvail();
{ {
@ -98,6 +77,19 @@ void PaletteEditorImGui::drawColumn(ox::CStringView txt) noexcept {
ImGui::Text("%s", txt.c_str()); 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<size_t>(9, lastElem);
} else for (auto i = 9u; i < 10; --i) {
auto const key = static_cast<ImGuiKey>(ImGuiKey_1 + i);
if (ImGui::IsKeyPressed(key)) {
val = ox::min<size_t>(i, lastElem);
break;
}
}
}
void PaletteEditorImGui::colorInput(ox::CStringView label, int &v, bool &inputFocused) noexcept { void PaletteEditorImGui::colorInput(ox::CStringView label, int &v, bool &inputFocused) noexcept {
ImGui::InputInt(label.c_str(), &v, 1, 5); ImGui::InputInt(label.c_str(), &v, 1, 5);
inputFocused = inputFocused || ImGui::IsItemFocused(); inputFocused = inputFocused || ImGui::IsItemFocused();
@ -240,10 +232,8 @@ void PaletteEditorImGui::drawColorEditor() noexcept {
int g = green16(c); int g = green16(c);
int b = blue16(c); int b = blue16(c);
int const a = alpha16(c); int const a = alpha16(c);
auto const&currentName = m_pal.colorNames[m_selectedColorRow]; auto const newName = ig::InputText<50>(
ox::IString<50> name; "Name", m_pal.colorNames[m_selectedColorRow]);
name = currentName;
auto const nameUpdated = ig::InputText("Name", name);
bool inputFocused = ImGui::IsItemFocused(); bool inputFocused = ImGui::IsItemFocused();
ImGui::Separator(); ImGui::Separator();
colorInput("Red", r, inputFocused); colorInput("Red", r, inputFocused);
@ -253,25 +243,20 @@ void PaletteEditorImGui::drawColorEditor() noexcept {
std::ignore = pushCommand<ApplyColorAllPagesCommand>( std::ignore = pushCommand<ApplyColorAllPagesCommand>(
m_pal, m_page, m_selectedColorRow); m_pal, m_page, m_selectedColorRow);
} }
if (!inputFocused && !m_pageRename.isOpen() && !ImGui::IsKeyDown(ImGuiKey_ModAlt)) { if (!inputFocused && !m_pageRename.isOpen()) {
auto const lastColor = largestPage(m_pal) - 1; if (!ImGui::IsKeyDown(ImGuiKey_ModAlt)) {
if (ImGui::IsKeyPressed(ImGuiKey_0)) { numShortcuts(m_selectedColorRow, largestPage(m_pal));
m_selectedColorRow = ox::min<size_t>(9, lastColor); } else {
} else for (auto i = 9u; i < 10; --i) { numShortcuts(m_page, m_pal.pages.size());
auto const key = static_cast<ImGuiKey>(ImGuiKey_1 + i);
if (ImGui::IsKeyPressed(key)) {
m_selectedColorRow = ox::min<size_t>(i, lastColor);
break;
}
} }
} }
auto const newColor = color16(r, g, b, a); auto const newColor = color16(r, g, b, a);
if (c != newColor) { if (c != newColor) {
std::ignore = pushCommand<UpdateColorCommand>(m_pal, m_page, m_selectedColorRow, newColor); std::ignore = pushCommand<UpdateColorCommand>(m_pal, m_page, m_selectedColorRow, newColor);
} }
if (nameUpdated) { if (newName) {
std::ignore = pushCommand<UpdateColorInfoCommand>( std::ignore = pushCommand<UpdateColorInfoCommand>(
m_pal, m_selectedColorRow, ox::String{name}); m_pal, m_selectedColorRow, ox::String{newName});
} }
} }

View File

@ -40,8 +40,6 @@ class PaletteEditorImGui: public studio::Editor {
public: public:
PaletteEditorImGui(studio::StudioContext &sctx, ox::StringParam path); PaletteEditorImGui(studio::StudioContext &sctx, ox::StringParam path);
void keyStateChanged(turbine::Key key, bool down) override;
void draw(studio::StudioContext&) noexcept final; void draw(studio::StudioContext&) noexcept final;
protected: protected:
@ -56,6 +54,8 @@ class PaletteEditorImGui: public studio::Editor {
drawColumn(ox::itoa(i)); 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; static void colorInput(ox::CStringView label, int &v, bool &inputFocused) noexcept;
void drawColorsEditor() noexcept; void drawColorsEditor() noexcept;