From 430cae1622d807a6a04925183646d3ee7401b76a Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 31 May 2024 22:04:31 -0500 Subject: [PATCH] [nostalgia,studio] Update for new ImGui's Mac awareness --- .../tilesheeteditor/tilesheeteditor-imgui.cpp | 10 +-- src/olympic/studio/applib/src/studioapp.cpp | 90 +++++++++---------- src/olympic/studio/applib/src/studioapp.hpp | 2 + 3 files changed, 46 insertions(+), 56 deletions(-) diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp index 534cc784..c5d16c1f 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp @@ -202,7 +202,7 @@ void TileSheetEditorImGui::keyStateChanged(turbine::Key key, bool down) { void TileSheetEditorImGui::draw(studio::StudioContext&) noexcept { auto const popupOpen = m_subsheetEditor.isOpen() || m_exportMenu.isOpen(); if (!popupOpen && m_tool == TileSheetTool::Select) { - if (ImGui::IsKeyPressed(ImGuiKey_ModCtrl)) { + if (ImGui::IsKeyDown(ImGuiKey_ModCtrl)) { if (ImGui::IsKeyPressed(ImGuiKey_A)) { auto const&img = m_model.activeSubSheet(); m_model.setSelection({{}, {img.columns * TileWidth, img.rows * TileHeight}}); @@ -408,19 +408,17 @@ void TileSheetEditorImGui::drawTileSheet(ox::Vec2 const&fbSize) noexcept { ImVec2(1, 0)); // handle input, this must come after drawing auto const&io = ImGui::GetIO(); - auto const mousePos = ox::Vec2(io.MousePos); + auto const mousePos = ox::Vec2(ImGui::GetMousePos()); if (ImGui::IsItemHovered()) { auto const wheel = io.MouseWheel; auto const wheelh = io.MouseWheelH; if (wheel != 0) { - const auto zoomMod = ox::defines::OS == ox::OS::Darwin ? - io.KeySuper : turbine::buttonDown(m_tctx, turbine::Key::Mod_Ctrl); - m_view.scrollV(fbSize, wheel, zoomMod); + m_view.scrollV(fbSize, wheel, ImGui::IsKeyDown(ImGuiKey_ModCtrl)); } if (wheelh != 0) { m_view.scrollH(fbSize, wheelh); } - if (io.MouseDown[0] && m_prevMouseDownPos != mousePos) { + if (ImGui::IsMouseDown(0) && m_prevMouseDownPos != mousePos) { m_prevMouseDownPos = mousePos; switch (m_tool) { case TileSheetTool::Draw: diff --git a/src/olympic/studio/applib/src/studioapp.cpp b/src/olympic/studio/applib/src/studioapp.cpp index 0cf5617b..4fc92069 100644 --- a/src/olympic/studio/applib/src/studioapp.cpp +++ b/src/olympic/studio/applib/src/studioapp.cpp @@ -83,7 +83,6 @@ void StudioUI::update() noexcept { } void StudioUI::handleKeyEvent(turbine::Key key, bool down) noexcept { - auto const ctrlDown = turbine::buttonDown(m_ctx, turbine::Key::Mod_Ctrl); for (auto p : m_popups) { if (p->isOpen()) { if (key == turbine::Key::Escape) { @@ -92,55 +91,7 @@ void StudioUI::handleKeyEvent(turbine::Key key, bool down) noexcept { return; } } - if (down && ctrlDown) { - switch (key) { - case turbine::Key::Num_1: - toggleProjectExplorer(); - break; - case turbine::Key::Alpha_C: - if (m_activeEditor && m_activeEditor->copyEnabled()) { - m_activeEditor->copy(); - } - break; - case turbine::Key::Alpha_N: - if (turbine::buttonDown(m_ctx, turbine::Key::Mod_Shift)) { - m_newProject.open(); - } else { - m_newMenu.open(); - } - break; - case turbine::Key::Alpha_O: - m_taskRunner.add(*ox::make(this, &StudioUI::openProjectPath)); - break; - case turbine::Key::Alpha_Q: - turbine::requestShutdown(m_ctx); - break; - case turbine::Key::Alpha_S: - save(); - break; - case turbine::Key::Alpha_V: - if (m_activeEditor && m_activeEditor->pasteEnabled()) { - m_activeEditor->paste(); - } - break; - case turbine::Key::Alpha_X: - if (m_activeEditor && m_activeEditor->cutEnabled()) { - m_activeEditor->cut(); - } - break; - case turbine::Key::Alpha_Y: - redo(); - break; - case turbine::Key::Alpha_Z: - undo(); - break; - default: - if (m_activeEditor) { - m_activeEditor->keyStateChanged(key, down); - } - break; - } - } else if (m_activeEditor && !ctrlDown) { + if (m_activeEditor && !ImGui::IsKeyDown(ImGuiKey_ModCtrl)) { m_activeEditor->keyStateChanged(key, down); } } @@ -172,6 +123,7 @@ void StudioUI::draw() noexcept { } } ImGui::End(); + handleKeyInput(); } void StudioUI::drawMenu() noexcept { @@ -332,6 +284,44 @@ void StudioUI::save() noexcept { } } +void StudioUI::handleKeyInput() noexcept { + if (ImGui::IsKeyDown(ImGuiKey_ModCtrl)) { + if (ImGui::IsKeyPressed(ImGuiKey_C)) { + if (m_activeEditor && m_activeEditor->copyEnabled()) { + m_activeEditor->copy(); + } + } else if (ImGui::IsKeyPressed(ImGuiKey_N)) { + if (turbine::buttonDown(m_ctx, turbine::Key::Mod_Shift)) { + m_newProject.open(); + } else { + m_newMenu.open(); + } + } else if (ImGui::IsKeyPressed(ImGuiKey_O)) { + m_taskRunner.add(*ox::make(this, &StudioUI::openProjectPath)); + } else if (ImGui::IsKeyPressed(ImGuiKey_S)) { + save(); + } else if (ImGui::IsKeyPressed(ImGuiKey_Q)) { + turbine::requestShutdown(m_ctx); + } else if (ImGui::IsKeyPressed(ImGuiKey_V)) { + if (m_activeEditor && m_activeEditor->pasteEnabled()) { + m_activeEditor->paste(); + } + } else if (ImGui::IsKeyPressed(ImGuiKey_X)) { + if (m_activeEditor && m_activeEditor->cutEnabled()) { + m_activeEditor->cut(); + } + } else if (ImGui::IsKeyPressed(ImGuiKey_Y)) { + auto const undoStack = m_activeEditor ? m_activeEditor->undoStack() : nullptr; + if (undoStack) { oxLogError(undoStack->redo()); } + } else if (ImGui::IsKeyPressed(ImGuiKey_Z)) { + auto const undoStack = m_activeEditor ? m_activeEditor->undoStack() : nullptr; + if (undoStack) { oxLogError(undoStack->undo()); } + } else if (ImGui::IsKeyPressed(ImGuiKey_1)) { + toggleProjectExplorer(); + } + } +} + ox::Error StudioUI::createOpenProject(ox::CRStringView path) noexcept { std::error_code ec; std::filesystem::create_directories(toStdStringView(path), ec); diff --git a/src/olympic/studio/applib/src/studioapp.hpp b/src/olympic/studio/applib/src/studioapp.hpp index 74e24a7c..5a32c7b9 100644 --- a/src/olympic/studio/applib/src/studioapp.hpp +++ b/src/olympic/studio/applib/src/studioapp.hpp @@ -83,6 +83,8 @@ class StudioUI: public ox::SignalHandler { void save() noexcept; + void handleKeyInput() noexcept; + ox::Error createOpenProject(ox::CRStringView path) noexcept; ox::Error openProjectPath(ox::CRStringView path) noexcept;