From d76d525054659b22ca60c05f1ebe1f7ad2ebe378 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 20 Mar 2022 02:08:54 -0500 Subject: [PATCH] [nostalgia/studio] Add keyboard shortcuts --- src/nostalgia/studio/main.cpp | 11 ++++- src/nostalgia/studio/studioapp.cpp | 69 ++++++++++++++++++++++++++++-- src/nostalgia/studio/studioapp.hpp | 10 +++++ 3 files changed, 84 insertions(+), 6 deletions(-) diff --git a/src/nostalgia/studio/main.cpp b/src/nostalgia/studio/main.cpp index 90e06a79..287c71a1 100644 --- a/src/nostalgia/studio/main.cpp +++ b/src/nostalgia/studio/main.cpp @@ -23,17 +23,24 @@ class StudioUIDrawer: public core::Drawer { } }; -static int eventHandler(core::Context *ctx) noexcept { +static int updateHandler(core::Context *ctx) noexcept { auto sctx = core::applicationData(ctx); auto ui = dynamic_cast(sctx->ui); ui->update(); return 16; } +static void keyEventHandler(core::Context *ctx, core::Key key, bool down) noexcept { + auto sctx = core::applicationData(ctx); + auto ui = dynamic_cast(sctx->ui); + ui->handleKeyEvent(key, down); +} + static ox::Error run(ox::UniquePtr fs) noexcept { oxRequireM(ctx, core::init(std::move(fs), "NostalgiaStudio")); core::setWindowTitle(ctx.get(), "Nostalgia Studio"); - core::setUpdateHandler(ctx.get(), eventHandler); + core::setUpdateHandler(ctx.get(), updateHandler); + core::setKeyEventHandler(ctx.get(), keyEventHandler); core::setConstantRefresh(ctx.get(), false); studio::StudioContext studioCtx; core::setApplicationData(ctx.get(), &studioCtx); diff --git a/src/nostalgia/studio/studioapp.cpp b/src/nostalgia/studio/studioapp.cpp index c21c31b3..f42a1eb5 100644 --- a/src/nostalgia/studio/studioapp.cpp +++ b/src/nostalgia/studio/studioapp.cpp @@ -9,6 +9,7 @@ #include "lib/configio.hpp" #include "builtinmodules.hpp" #include "filedialogmanager.hpp" +#include "nostalgia/core/input.hpp" #include "studioapp.hpp" namespace nostalgia { @@ -56,6 +57,42 @@ void StudioUI::update() noexcept { m_taskRunner.update(m_ctx); } +void StudioUI::handleKeyEvent(core::Key key, bool down) noexcept { + if (down && core::buttonDown(m_ctx, core::Key::Mod_Ctrl)) { + switch (key) { + case core::Key::Num_1: + toggleProjectExplorer(); + break; + case core::Key::Alpha_C: + m_acitveEditor->copy(); + break; + case core::Key::Alpha_O: + m_taskRunner.add(new FileDialogManager(this, &StudioUI::openProject)); + break; + case core::Key::Alpha_Q: + oxIgnoreError(core::shutdown(m_ctx)); + break; + case core::Key::Alpha_S: + save(); + break; + case core::Key::Alpha_V: + m_acitveEditor->paste(); + break; + case core::Key::Alpha_X: + m_acitveEditor->cut(); + break; + case core::Key::Alpha_Y: + redo(); + break; + case core::Key::Alpha_Z: + undo(); + break; + default: + break; + } + } +} + void StudioUI::draw() noexcept { drawMenu(); drawToolbar(); @@ -110,10 +147,7 @@ void StudioUI::drawMenu() noexcept { } if (ImGui::BeginMenu("View")) { if (ImGui::MenuItem("Project Explorer", "Ctrl+1", m_showProjectExplorer)) { - m_showProjectExplorer = !m_showProjectExplorer; - studio::editConfig(m_ctx, [&](StudioConfig *config) { - config->showProjectExplorer = m_showProjectExplorer; - }); + toggleProjectExplorer(); } ImGui::EndMenu(); } @@ -230,6 +264,33 @@ void StudioUI::loadModules() noexcept { } } +void StudioUI::toggleProjectExplorer() noexcept { + m_showProjectExplorer = !m_showProjectExplorer; + studio::editConfig(m_ctx, [&](StudioConfig *config) { + config->showProjectExplorer = m_showProjectExplorer; + }); +} + +void StudioUI::redo() noexcept { + auto undoStack = m_acitveEditor ? m_acitveEditor->undoStack() : nullptr; + if (undoStack && undoStack->canRedo()) { + m_acitveEditor->undoStack()->redo(); + } +} + +void StudioUI::undo() noexcept { + auto undoStack = m_acitveEditor ? m_acitveEditor->undoStack() : nullptr; + if (undoStack && undoStack->canUndo()) { + m_acitveEditor->undoStack()->undo(); + } +} + +void StudioUI::save() noexcept { + if (m_acitveEditor && m_acitveEditor->unsavedChanges()) { + m_acitveEditor->save(); + } +} + ox::Error StudioUI::openProject(const ox::String &path) noexcept { oxRequireM(fs, core::loadRomFs(path.c_str())); m_ctx->rom = std::move(fs); diff --git a/src/nostalgia/studio/studioapp.hpp b/src/nostalgia/studio/studioapp.hpp index a755211d..d268ef39 100644 --- a/src/nostalgia/studio/studioapp.hpp +++ b/src/nostalgia/studio/studioapp.hpp @@ -39,6 +39,8 @@ class StudioUI: public ox::SignalHandler { void update() noexcept; + void handleKeyEvent(core::Key, bool down) noexcept; + constexpr auto project() noexcept { return m_project.get(); } @@ -63,6 +65,14 @@ class StudioUI: public ox::SignalHandler { void loadModules() noexcept; + void toggleProjectExplorer() noexcept; + + void redo() noexcept; + + void undo() noexcept; + + void save() noexcept; + ox::Error openProject(const ox::String &path) noexcept; ox::Error openFile(const ox::String &path) noexcept;