Add buttons to prevent soft-lock in keybind conf.

This commit is contained in:
Nguyễn Quốc Hưng
2024-08-09 23:40:07 +07:00
parent 1a99de7435
commit 91100883fe
5 changed files with 116 additions and 23 deletions

View File

@@ -1,5 +1,6 @@
local KeyConfigScene = SCENE:extend()
KeyConfigScene.title = "Key Config"
local buttonList = {}
local configurable_inputs = {
"menu_decide",
@@ -13,7 +14,6 @@ local configurable_inputs = {
"rotate_right",
"rotate_right2",
}
local input_names = {
menu_decide='Confirm Selection',
menu_back = 'Go Back',
@@ -40,13 +40,49 @@ function KeyConfigScene:new()
self.input_state = 1
self.set_inputs = newSetInputs()
self.new_input = {}
BUTTON.reset(buttonList)
buttonList = { -- Configuring
BUTTON.new{
text = CHAR.icon.fastForward.." SKIP",
x = 40, y = 300, w = 100, h = 30,
codeWhenPressed = function() self:onInputPress{type = "key", scancode = "tab"} end
},
BUTTON.new{
text = CHAR.icon.cross_thick.." Cancel",
x = 150, y = 300, w = 100, h = 30,
codeWhenPressed = function() self:onInputPress{type = "key", scancode = "escape"} end
},
}
end
function KeyConfigScene:update()
if self.input_state > #configurable_inputs then
BUTTON.reset(buttonList)
buttonList = { -- Confirming
BUTTON.new{
text = CHAR.icon.checkMark.." CONFIRM",
x = 40, y = 300, w = 100, h = 30,
codeWhenPressed = function() self:onInputPress{type = "key", scancode = "return"} end
},
BUTTON.new{
text = CHAR.icon.retry_spin.." Restart",
x = 150, y = 300, w = 100, h = 30,
codeWhenPressed = function() self:onInputPress{type = "key", scancode = "delete"} end
},
BUTTON.new{
text = CHAR.icon.cross_thick.." Cancel",
x = 260, y = 300, w = 100, h = 30,
codeWhenPressed = function() self:onInputPress{type = "key", scancode = "escape"} end
}
}
end
end
function KeyConfigScene:render()
MainBackground()
BUTTON.draw(buttonList)
for i, input in ipairs(configurable_inputs) do
drawText(input_names[input], 40, 60 + i * 20, 200, "left")
if self.set_inputs[input] then
@@ -62,7 +98,9 @@ function KeyConfigScene:render()
end
function KeyConfigScene:onInputPress(e)
if e.type == "key" then
if e.type == "mouse" or e.type == "touch" then
BUTTON.press(buttonList, e.x, e.y, e.id)
elseif e.type == "key" then
-- function keys, escape, and tab are reserved and can't be remapped
if e.scancode == "escape" or (self.input_state > #configurable_inputs and e.input == "menu_back") then
SCENE = InputConfigScene(SETTINGS.firstTime)
@@ -72,10 +110,7 @@ function KeyConfigScene:onInputPress(e)
SCENE = SETTINGS.firstTime and TitleScene() or InputConfigScene()
SETTINGS.firstTime = false
elseif e.scancode == "delete" or e.scancode == "backspace" then
-- retry
self.input_state = 1
self.set_inputs = newSetInputs()
self.new_input = {}
self:new() -- retry
end
elseif e.scancode == "tab" then
self.set_inputs[configurable_inputs[self.input_state]] = "skipped"
@@ -92,4 +127,16 @@ function KeyConfigScene:onInputPress(e)
end
end
function KeyConfigScene:onInputRelease(e)
if e.type == "mouse" or e.type == "touch" then
BUTTON.release(buttonList, e.x, e.y, e.id)
end
end
function KeyConfigScene:onInputMove(e)
if e.type == "mouse" then
BUTTON.checkHovering(buttonList, e.x, e.y)
end
end
return KeyConfigScene

View File

@@ -42,7 +42,7 @@ function NameEntryScene:new()
self.char_pos = 1
self.name_entry = {'A','A','A'}
self.entry_pos = 1
self.entry_chars = self.name_entry[1]..self.name_entry[2]..self.name_entry[3]
self.entry_chars = table.concat(self.name_entry, '', 1, 3)
self.grid = Grid(10, 20)
self.repeat_limit = 10
self.repeat_counter = self.repeat_limit-1
@@ -130,7 +130,7 @@ function NameEntryScene:update()
end
self.repeat_counter = self.repeat_counter + 1
end
self.entry_chars = self.name_entry[1]..self.name_entry[2]..self.name_entry[3]
self.entry_chars = table.concat(self.name_entry, '', 1, 3)
end
function NameEntryScene:onInputMove(e)
@@ -146,16 +146,15 @@ function NameEntryScene:getPlayInfo(player_name)
self.wins = grade_history[2]
self.plays = grade_history[4]
else
self.grade, self.win, self.plays = 0, 0, 0
self.grade, self.wins, self.plays = 0, 0, 0
end
return self.grade, self.win, self.plays
end
function NameEntryScene:onInputPress(e)
local name = string.lower(self.name_entry[1]..self.name_entry[2]..self.name_entry[3])
local name = string.lower(table.concat(self.name_entry, '', 1, 3))
if e.type == "mouse" or e.type == "touch" then
BUTTON.press(buttonList, e.x, e.y, e.id)
elseif e.key and #e.key == 1 then
local pos = string.find("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.", string.upper(e.key), 1, true)
local pos = string.find(self.chars, string.upper(e.key), 1, true)
if pos then
if self.entry_pos <= 3 then
self.char_pos = pos

View File

@@ -1,5 +1,6 @@
local StickConfigScene = SCENE:extend()
StickConfigScene.title = "Controller Config"
local buttonList = {}
local configurable_inputs = {
"menu_decide",
@@ -13,7 +14,6 @@ local configurable_inputs = {
"rotate_right",
"rotate_right2",
}
local input_names = {
menu_decide='Confirm Selection',
menu_back = 'Go Back',
@@ -40,13 +40,49 @@ function StickConfigScene:new()
self.set_inputs = newSetInputs()
self.new_input = {}
self.axis_timer = 0
BUTTON.reset(buttonList)
buttonList = { -- Configuring
BUTTON.new{
text = CHAR.icon.fastForward.." SKIP",
x = 40, y = 300, w = 100, h = 30,
codeWhenPressed = function() self:onInputPress{type = "key", scancode = "tab"} end
},
BUTTON.new{
text = CHAR.icon.cross_thick.." Cancel",
x = 150, y = 300, w = 100, h = 30,
codeWhenPressed = function() self:onInputPress{type = "key", scancode = "escape"} end
},
}
end
function StickConfigScene:update()
if self.input_state > #configurable_inputs then
BUTTON.reset(buttonList)
buttonList = { -- Confirming
BUTTON.new{
text = CHAR.icon.checkMark.." CONFIRM",
x = 40, y = 300, w = 100, h = 30,
codeWhenPressed = function() self:onInputPress{type = "key", scancode = "return"} end
},
BUTTON.new{
text = CHAR.icon.retry_spin.." Restart",
x = 150, y = 300, w = 100, h = 30,
codeWhenPressed = function() self:onInputPress{type = "key", scancode = "delete"} end
},
BUTTON.new{
text = CHAR.icon.cross_thick.." Cancel",
x = 260, y = 300, w = 100, h = 30,
codeWhenPressed = function() self:onInputPress{type = "key", scancode = "escape"} end
}
}
end
end
function StickConfigScene:render()
MainBackground()
BUTTON.draw(buttonList)
for i, input in ipairs(configurable_inputs) do
drawText(input_names[input], 40, 60 + i * 20, 200, "left")
if self.set_inputs[input] then
@@ -68,8 +104,11 @@ local function addJoystick(input, name)
end
end
---@param e SCENE_onInput
function StickConfigScene:onInputPress(e)
if e.type == "key" then
if e.type == "mouse" or e.type == "touch" then
BUTTON.press(buttonList, e.x, e.y, e.id)
elseif e.type == "key" then
-- function keys, escape, and tab are reserved and can't be remapped
if e.scancode == "escape" or (self.input_state > #configurable_inputs and e.input == "menu_back") then
SCENE = InputConfigScene(SETTINGS.firstTime)
@@ -79,10 +118,7 @@ function StickConfigScene:onInputPress(e)
SCENE = SETTINGS.firstTime and TitleScene() or InputConfigScene()
SETTINGS.firstTime = false
elseif e.scancode == "delete" or e.scancode == "backspace" then
-- retry
self.input_state = 1
self.set_inputs = newSetInputs()
self.new_input = {}
self:new() -- retry
end
else -- Other keys - skip
self.set_inputs[configurable_inputs[self.input_state]] = "skipped"
@@ -147,4 +183,16 @@ function StickConfigScene:onInputPress(e)
end
end
function StickConfigScene:onInputRelease(e)
if e.type == "mouse" or e.type == "touch" then
BUTTON.release(buttonList, e.x, e.y, e.id)
end
end
function StickConfigScene:onInputMove(e)
if e.type == "mouse" then
BUTTON.checkHovering(buttonList, e.x, e.y)
end
end
return StickConfigScene