Skip to content

Commit

Permalink
add escape and restart shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
Bauumm committed Mar 30, 2024
1 parent 68835d9 commit 6d75e2f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
2 changes: 2 additions & 0 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ add_input("right", { 192, 20, 21 })
add_input("left", { 192, 20, 21 })
add_input("focus", { 192, 20, 21 })
add_input("swap", { 20, 21 })
add_input("exit", { 192, 20, 21, 3 })
add_input("restart", { 192, 20, 21, 3 })
add_input("ui_up")
add_input("ui_down")
add_input("ui_right")
Expand Down
29 changes: 29 additions & 0 deletions game_handler/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,35 @@ function game_handler.process_event(name, ...)
msaa = 4,
})
end
if name == "customkeydown" and game_handler.is_running() and not input.is_replaying() then
local key = ...
if not current_game.preview_mode then
if key == "exit" then
local death_overlay = require("ui.overlay.death")
if death_overlay.is_open then
death_overlay.layout.elements[2]:click()
else
if current_game.death_callback then
current_game.death_callback()
end
game_handler.stop()
require("ui.screens.levelselect.score").refresh()
game_handler.onupdate()
end
elseif key == "restart" then
local death_overlay = require("ui.overlay.death")
if death_overlay.is_open then
death_overlay.layout.elements[1]:click()
else
if current_game.death_callback then
current_game.death_callback()
end
game_handler.stop()
game_handler.retry()
end
end
end
end
if current_game then
-- allow game modules to have their own event handlers
if current_game.running and current_game[name] ~= nil then
Expand Down
2 changes: 1 addition & 1 deletion input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ local mapping = {
---records changes if recording
---gets input state from replay if replaying
---@param input_name string
---@param add_ui_button boolean
---@param add_ui_button boolean?
---@return boolean
function input.get(input_name, add_ui_button)
input_name = mapping[input_name] or input_name
Expand Down
2 changes: 2 additions & 0 deletions input_schemes/keyboard.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ local keyboard = {
right = { "right", "d" },
left = { "left", "a" },
swap = { "space" },
exit = { "escape" },
restart = { "up" },
ui_backspace = { "backspace" },
ui_delete = { "delete" },
ui_up = { "up" },
Expand Down
14 changes: 9 additions & 5 deletions ui/key_repeat.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local input = require("input")
local key_repeat = {}
local keys = { "ui_up", "ui_down", "ui_left", "ui_right", "ui_click", "ui_backspace", "ui_delete" }
local keys = { "ui_up", "ui_down", "ui_left", "ui_right", "ui_click", "ui_backspace", "ui_delete", "exit", "restart" }
local modules = { "ui", "game_handler" }
local states = {}
local press_timers = {}
local press_timer_repeat = {}
Expand All @@ -20,10 +21,13 @@ function key_repeat.update(dt)
press_timer_repeat[i] = 0.4
end
if state ~= states[i] then
if state then
require("ui").process_event("customkeydown", keys[i])
else
require("ui").process_event("customkeyup", keys[i])
for j = 1, #modules do
local module = require(modules[j])
if state then
module.process_event("customkeydown", keys[i])
else
module.process_event("customkeyup", keys[i])
end
end
end
states[i] = state
Expand Down

0 comments on commit 6d75e2f

Please sign in to comment.