From eb7a7bbb52e3ecb243eb39ee38d3cfa28f35a980 Mon Sep 17 00:00:00 2001 From: Filip Burlacu Date: Mon, 16 Feb 2015 21:45:54 -0500 Subject: [PATCH] Adds keymap script/config-file, simplifying hotkey modification and use. Usage: - Define hotkey combo in scripts/keymap.lua, as examples do - call keyCombo(kmp.yournewcombo) where you need a key-press-check. Examples of behaviour: - Ctrl-C does not activate C - Ctrl-Alt-Q does not activate Q or Ctrl-Q or Alt-Q or Ctrl-Alt-Shift-Q - F does not activate Shift-F - Z does activate Z Fixes #239 --- scripts/keymap.lua | 78 +++++++++++++++++++ scripts/manifest.txt | 2 + scripts/microbe_editor/microbe_editor_hud.lua | 36 ++++----- scripts/microbe_stage/microbe_stage_hud.lua | 18 ++--- .../switch_game_state_system.lua | 2 +- 5 files changed, 106 insertions(+), 30 deletions(-) create mode 100644 scripts/keymap.lua diff --git a/scripts/keymap.lua b/scripts/keymap.lua new file mode 100644 index 00000000000..725f0ebc25c --- /dev/null +++ b/scripts/keymap.lua @@ -0,0 +1,78 @@ +-- Holds the keymap + +kmp = {} + +-- Microbe Editor -- + +kmp.undo = {"ctrl", "U"} +kmp.redo = {"ctrl", "R"} + +kmp.remove = {"R"} +kmp.newmicrobe = {"C"} + +kmp.vacuole = {"S"} +kmp.mitochondrion = {"M"} +kmp.oxytoxyvacuole = {"T"} +kmp.flagellum = {"F"} +kmp.chloroplast = {"P"} + +kmp.togglegrid = {"G"} + +kmp.rename = {"F12"} +kmp.gotostage = {"F2"} + +-- Microbe Stage -- + +kmp.forward = {"W"} +kmp.backward = {"S"} +kmp.leftward = {"A"} +kmp.rightward = {"D"} + +kmp.shootoxytoxy = {"E"} +kmp.reproduce = {"P"} + +kmp.togglemenu = {"ESCAPE"} +kmp.gotoeditor = {"F2"} +kmp.altuniverse = {"F1"} + +-- this is the perfect kind of thing to move into C++ +-- it shouldn't require anything in Lua, it'll just get a table of strings, and do comparisons +function keyCombo(combo) + -- Boolean function, used to check if key combo is pressed + -- doesn't handle modifier keys properly yet (eg, ctrl+R will activate R) + + mods = {} -- holds whether each particular modifier key (left-right-agnostic) is pressed + mods.ctrl = false + mods.alt = false + mods.shift = false + + for _, key in ipairs(combo) do + if key == "ctrl" then + mods.ctrl = true + elseif key == "shift" then + mods.shift = true + elseif key == "alt" then + mods.alt = true + elseif not Engine.keyboard:wasKeyPressed(Keyboard["KC_"..key]) then + return false + end + end + -- fail if any modkey pressed unmatches required mods + + if (Engine.keyboard:isKeyDown(Keyboard.KC_LCONTROL) + or Engine.keyboard:isKeyDown(Keyboard.KC_RCONTROL) + ) ~= mods.ctrl then + return false + end + if (Engine.keyboard:isKeyDown(Keyboard.KC_LSHIFT) + or Engine.keyboard:isKeyDown(Keyboard.KC_RSHIFT) + ) ~= mods.shift then + return false + end + if (Engine.keyboard:isKeyDown(Keyboard.KC_LMENU) + or Engine.keyboard:isKeyDown(Keyboard.KC_RMENU) + ) ~= mods.alt then + return false + end + return true +end diff --git a/scripts/manifest.txt b/scripts/manifest.txt index f434562e5de..177f5c6c6a4 100644 --- a/scripts/manifest.txt +++ b/scripts/manifest.txt @@ -3,6 +3,8 @@ constants.lua quick_save.lua util.lua +keymap.lua + main_menu microbe_stage microbe_editor diff --git a/scripts/microbe_editor/microbe_editor_hud.lua b/scripts/microbe_editor/microbe_editor_hud.lua index 3a2d9429423..129cc8061f9 100644 --- a/scripts/microbe_editor/microbe_editor_hud.lua +++ b/scripts/microbe_editor/microbe_editor_hud.lua @@ -115,41 +115,37 @@ function MicrobeEditorHudSystem:update(renderTime, logicTime) self:removeClicked() self.editor:performLocationAction() end - if Engine.keyboard:wasKeyPressed(Keyboard.KC_C) then + if keyCombo(kmp.newmicrobe) then -- These global event handlers are defined in microbe_editor_hud.lua self:nucleusClicked() - elseif Engine.keyboard:wasKeyPressed(Keyboard.KC_R) then - if Engine.keyboard:isKeyDown(Keyboard.KC_LCONTROL) then - self.editor:redo() - else - self:removeClicked() - self.editor:performLocationAction() - end - elseif Engine.keyboard:wasKeyPressed(Keyboard.KC_U) then - if Engine.keyboard:isKeyDown(Keyboard.KC_LCONTROL) then - self.editor:undo() - end - elseif Engine.keyboard:wasKeyPressed(Keyboard.KC_S) then + elseif keyCombo(kmp.redo) then + self.editor:redo() + elseif keyCombo(kmp.remove) then + self:removeClicked() + self.editor:performLocationAction() + elseif keyCombo(kmp.undo) then + self.editor:undo() + elseif keyCombo(kmp.vacuole) then self:vacuoleClicked() self.editor:performLocationAction() - elseif Engine.keyboard:wasKeyPressed(Keyboard.KC_T) then + elseif keyCombo(kmp.oxytoxyvacuole) then if not Engine:playerData():lockedMap():isLocked("Toxin") then self:toxinClicked() self.editor:performLocationAction() end - elseif Engine.keyboard:wasKeyPressed(Keyboard.KC_F) then + elseif keyCombo(kmp.flagellum) then self:flageliumClicked() self.editor:performLocationAction() - elseif Engine.keyboard:wasKeyPressed(Keyboard.KC_M) then + elseif keyCombo(kmp.mitochondrion) then self:mitochondriaClicked() self.editor:performLocationAction() --elseif Engine.keyboard:wasKeyPressed(Keyboard.KC_A) and self.editor.currentMicrobe ~= nil then -- self:aminoSynthesizerClicked() -- self.editor:performLocationAction() - elseif Engine.keyboard:wasKeyPressed(Keyboard.KC_P) then + elseif keyCombo(kmp.chloroplast) then self:chloroplastClicked() self.editor:performLocationAction() - elseif Engine.keyboard:wasKeyPressed(Keyboard.KC_G) then + elseif keyCombo(kmp.togglegrid) then if self.editor.gridVisible then self.editor.gridSceneNode.visible = false; self.editor.gridVisible = false @@ -157,9 +153,9 @@ function MicrobeEditorHudSystem:update(renderTime, logicTime) self.editor.gridSceneNode.visible = true; self.editor.gridVisible = true end - elseif Engine.keyboard:wasKeyPressed(Keyboard.KC_F2) then + elseif keyCombo(kmp.gotostage) then playClicked() - elseif Engine.keyboard:wasKeyPressed(Keyboard.KC_F12) then + elseif keyCombo(kmp.rename) then self:updateMicrobeName() end diff --git a/scripts/microbe_stage/microbe_stage_hud.lua b/scripts/microbe_stage/microbe_stage_hud.lua index f623b6e32af..5adce778f77 100644 --- a/scripts/microbe_stage/microbe_stage_hud.lua +++ b/scripts/microbe_stage/microbe_stage_hud.lua @@ -74,28 +74,28 @@ function HudSystem:update(renderTime) self.compoundListItems[compoundID]:setText(compoundsString) end end - self.compoundListBox:listboxHandleUpdatedItemData() --]] + self.compoundListBox:listboxHandleUpdatedItemData() - if Engine.keyboard:wasKeyPressed(Keyboard.KC_ESCAPE) then + if keyCombo(kmp.togglemenu) then self:menuButtonClicked() - elseif Engine.keyboard:wasKeyPressed(Keyboard.KC_F2) then + elseif keyCombo(kmp.gotoeditor) then self:editorButtonClicked() - elseif Engine.keyboard:wasKeyPressed(Keyboard.KC_E) then + elseif keyCombo(kmp.shootoxytoxy) then playerMicrobe:emitAgent(CompoundRegistry.getCompoundId("oxytoxy"), 3) - elseif Engine.keyboard:wasKeyPressed(Keyboard.KC_P) then + elseif keyCombo(kmp.reproduce) then playerMicrobe:reproduce() end local direction = Vector3(0, 0, 0) - if (Engine.keyboard:wasKeyPressed(Keyboard.KC_W)) then + if keyCombo(kmp.forward) then playerMicrobe.soundSource:playSound("microbe-movement-2") end - if (Engine.keyboard:wasKeyPressed(Keyboard.KC_S)) then + if keyCombo(kmp.backward) then playerMicrobe.soundSource:playSound("microbe-movement-2") end - if (Engine.keyboard:wasKeyPressed(Keyboard.KC_A)) then + if keyCombo(kmp.leftward) then playerMicrobe.soundSource:playSound("microbe-movement-1") end - if (Engine.keyboard:wasKeyPressed(Keyboard.KC_D)) then + if keyCombo(kmp.rightward) then playerMicrobe.soundSource:playSound("microbe-movement-1") end diff --git a/scripts/microbe_stage/switch_game_state_system.lua b/scripts/microbe_stage/switch_game_state_system.lua index 26addc6feaa..edb72b67346 100644 --- a/scripts/microbe_stage/switch_game_state_system.lua +++ b/scripts/microbe_stage/switch_game_state_system.lua @@ -6,7 +6,7 @@ end function SwitchGameStateSystem:update(renderTime, logicTime) - if Engine.keyboard:wasKeyPressed(Keyboard.KC_F1) then + if keyCombo(kmp.altuniverse) then local currentState = Engine:currentGameState() local nextGameState if Engine:currentGameState():name() == GameState.MICROBE:name() then