Skip to content

Commit

Permalink
Adds keymap script/config-file, simplifying hotkey modification and use.
Browse files Browse the repository at this point in the history
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 Revolutionary-Games#239
  • Loading branch information
Moopli committed Feb 21, 2015
1 parent 7952d69 commit eb7a7bb
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 30 deletions.
78 changes: 78 additions & 0 deletions scripts/keymap.lua
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions scripts/manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ constants.lua
quick_save.lua
util.lua

keymap.lua

main_menu
microbe_stage
microbe_editor
Expand Down
36 changes: 16 additions & 20 deletions scripts/microbe_editor/microbe_editor_hud.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,51 +115,47 @@ 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
else
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

Expand Down
18 changes: 9 additions & 9 deletions scripts/microbe_stage/microbe_stage_hud.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion scripts/microbe_stage/switch_game_state_system.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit eb7a7bb

Please sign in to comment.