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

@@ -15,7 +15,6 @@ local function mDrawQ(obj,quad,x,y,a,k)
love.graphics.draw(obj,quad,x,y,a,k,nil,w*.5,h*.5)
end
ShowLoadingText('virtual key skin')
local empty_quad
-- A table containing quads used to draw icons for virtual control system.
-- local virtual_quad=setmetatable((function()
@@ -42,7 +41,7 @@ local virtual_quad=setmetatable((function()
empty_quad=gc_newQuad(0,0,1,1,5*w,2*w)
for i,name in next,{
'left','right','up','down','restart',
'rotate_right','rotate_left','rotate_right2','rotate_left2','',
'rotate_right','rotate_left','rotate_right2','rotate_left2'
} do if #name>0 then t[name]=gc_newQuad((i-1)%5*w,math.floor((i-1)/5)*w,w,w,5*w,2*w) end end
return t
end)(),{

View File

@@ -151,13 +151,13 @@ function button:press(x, y, touchID)
end
---Trigger release action, don't need ``self._hovering`` to ``true``
function button:release(x, y, touchID)
local valid = true
local valid
if touchID then
valid = touchID == self._touchID
else
valid = true
end
if valid then
self._pressed = false
self._touchID = false
@@ -187,7 +187,7 @@ function BUTTON.checkDataValidation(D, safe)
elseif type(D.text) ~= 'string' then
error("[text] must be a string or a function returns string, got "..type(D.text))
end
assert(type(D.x) == "number" , "[x] must be a integer")
assert(type(D.y) == "number" , "[y] must be a integer")
assert(type(D.w) == "number" and D.w > 0, "[w] must be a positive integer")

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