Mothback/code/keybind.lua

150 lines
3.8 KiB
Lua

Keybind = {}
Keybind.move = {}
Keybind.move.left = { demo = "move_left"}
Keybind.move.right = { demo = "move_right"}
Keybind.move.up = { demo = "move_up"}
Keybind.move.down = { demo = "move_down"}
Keybind.move.jump = { demo = "move_jump"}
Keybind.move.hook = { demo = "move_hook"}
Keybind.move.dash = { demo = "move_dash"}
Keybind.menu = {}
Keybind.menu.pause = { demo = "menu_pause"}
Keybind.menu.confirm = { demo = "menu_confirm"}
Keybind.debug = {}
Keybind.editor = {}
Keybind.generic = {}
function Keybind:isAvailable(action)
return not action.occupied
end
function Keybind:checkDown(action)
if DemoPlayback then
for _, demo_action in pairs(DemoAction[CurrentDemoFrame]) do
if demo_action == action.demo then
return true
end
end
return false
else
for _, keyname in pairs(action.keys) do
local check = false
if type(keyname) == "string" then
check = love.keyboard.isDown(keyname)
else
check = love.mouse.isDown(keyname)
end
if check then
if action.demo ~= nil then
Demo:recordAction(action.demo)
end
action.occupied = true
return true
end
end
action.occupied = false
return false
end
end
-- relies on being called exactly once per frame to be accurate.
function Keybind:checkPressed(action)
if Keybind:checkDown(action) then
if not action.pressed then
action.pressed = true
return true
end
else
action.pressed = nil
end
return false
end
function Keybind:checkCollision(cat, key)
for _, action in pairs(cat) do
for _, keyname in pairs(action.keys) do
if key == keyname then return true end
end
end
return false
end
function Keybind:addKey(action, key)
table.insert(action.keys, key)
end
function Keybind:changeKey(action, position, key)
action.keys[position] = key
end
function Keybind:removeKeys(action)
action.keys = {}
end
-- this prolly should be used by Prompt:keypressed()
function Keybind:hasKey(action, key)
for _, v in pairs(action.keys) do
if v == key then
return true
end
end
return false
end
function Keybind:default()
--Menu
Keybind.menu.pause.keys = {"escape"}
Keybind.menu.confirm.keys = {"z", "space", 1}
--Move
Keybind.move.left.keys = {"left", "a"}
Keybind.move.right.keys = {"right", "d"}
Keybind.move.up.keys = {"up", "w"}
Keybind.move.down.keys = {"down", "s"}
Keybind.move.jump.keys = {"z", "space"}
Keybind.move.hook.keys = {"x", 1}
Keybind.move.dash.keys = {"c", 2}
--Debug
Keybind.debug.debug = { keys = {"f1"}}
Keybind.debug.reposition = { keys = {"f2"}}
Keybind.debug.reload = { keys = {"f3"}}
Keybind.debug.editor = { keys = {"f4"}}
Keybind.debug.recording = { keys = {"f5"}}
Keybind.debug.playback = { keys = {"f6"}}
Keybind.debug.respawn = { keys = {"f8"}}
-- Editor
Keybind.editor.palette_mode = { keys = {"tab"}}
Keybind.editor.room_mode = { keys = {"r"}}
Keybind.editor.entity_mode = { keys = {"e"}}
Keybind.editor.properties_mode = { keys = {"p"}}
Keybind.editor.left = { keys = {"left", "a"}}
Keybind.editor.right = { keys = {"right", "d"}}
Keybind.editor.up = { keys = {"up", "w"}}
Keybind.editor.down = { keys = {"down", "s"}}
Keybind.editor.palette_change = { keys = {"f1"}}
Keybind.editor.save = { keys = {"f3"}}
Keybind.editor.tile_set = { keys = {1}}
Keybind.editor.tile_remove = { keys = {2}}
Keybind.editor.entity_select = { keys = {1}}
Keybind.editor.entity_move = { keys = {2}}
Keybind.editor.entity_modify_archetype = { keys = {"t"}}
Keybind.editor.entity_modify_data = { keys = {"g"}}
Keybind.editor.entity_remove = { keys = {"delete"}}
Keybind.editor.entity_new = { keys = {"n"}}
-- Generic
Keybind.generic.lclick = { keys = {1}}
Keybind.generic.rclick = { keys = {2}}
Keybind.generic.lshift = { keys = {"lshift"}}
Keybind.generic.alt = { keys = {"alt"}}
Keybind.generic.lctrl = { keys = {"lctrl"}}
end
-- Set default values at start
Keybind:default()