Support multiple bindings per an action
This commit is contained in:
parent
2cda8e6801
commit
6f7e0777b1
3 changed files with 38 additions and 44 deletions
|
@ -36,14 +36,13 @@ function menu_state:enter()
|
|||
Label(string.format("Player %d", player), midfont, { 255, 255, 255, 255 }, "left", 500, 0, x, y, menu_state)
|
||||
y = y + 60
|
||||
for idx, key in ipairs(bindingOrder) do
|
||||
local binding = KEYMAP[player][key]
|
||||
local l1 = Label(bindingName[key], font, { 255, 255, 255, 255 }, "left", 190, 25, x, y, menu_state)
|
||||
local l2 = Label(binding, font, { 255, 255, 255, 255 }, "left", 50, 25, x + 200, y, menu_state)
|
||||
local adjust = function()
|
||||
menu_state:adjustBindings(l1, l2, player, key)
|
||||
Label(bindingName[key], font, { 255, 255, 255, 255 }, "left", 190, 25, x, y, menu_state)
|
||||
for bindingIdx, binding in ipairs(KEYMAP[player][key]) do
|
||||
local label = Label(binding, font, { 255, 255, 255, 255 }, "left", 50, 25, x + 200 + 20 * (bindingIdx - 1), y, menu_state)
|
||||
label:onClick(function()
|
||||
menu_state:adjustBindings(label, bindingIdx, player, key)
|
||||
end)
|
||||
end
|
||||
l1:onClick(adjust)
|
||||
l2:onClick(adjust)
|
||||
y = y + 30
|
||||
end
|
||||
x = x + 400
|
||||
|
@ -55,19 +54,17 @@ local blinkColor = function(t)
|
|||
return 255 * ((math.sin(math.pow((math.log(subseconds + 1) / math.log(2)), 2) * math.pi * 2) * 0.5 + 0.5));
|
||||
end
|
||||
|
||||
function menu_state:adjustBindings(name, binding, player, key)
|
||||
function menu_state:adjustBindings(label, bindingIdx, player, key)
|
||||
if currentlyChosen ~= nil then
|
||||
currentlyChosen:delete()
|
||||
end
|
||||
|
||||
currentlyChosen = ValueController(function ()
|
||||
name.color[4] = blinkColor();
|
||||
binding.color[4] = blinkColor();
|
||||
label.color[4] = blinkColor();
|
||||
end, menu_state)
|
||||
currentlyChosen.data = { name = name, binding = binding, player = player, key = key, label = binding }
|
||||
currentlyChosen.data = { bindingIdx = bindingIdx, player = player, key = key, label = label }
|
||||
currentlyChosen.onDelete = function()
|
||||
name.color[4] = 255;
|
||||
binding.color[4] = 255;
|
||||
label.color[4] = 255;
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -125,7 +122,7 @@ function menu_state:keypressed(key, unicode)
|
|||
currentlyChosen:delete()
|
||||
currentlyChosen = nil
|
||||
else
|
||||
KEYMAP[currentlyChosen.data.player][currentlyChosen.data.key] = key
|
||||
KEYMAP[currentlyChosen.data.player][currentlyChosen.data.key][currentlyChosen.data.bindingIdx] = key
|
||||
currentlyChosen.data.label.label = key
|
||||
currentlyChosen:delete()
|
||||
currentlyChosen = nil
|
||||
|
|
33
player.lua
33
player.lua
|
@ -51,30 +51,27 @@ Player = Class{
|
|||
update = function(self, dt)
|
||||
end;
|
||||
|
||||
press = function(self, key)
|
||||
pressDown = function(self, key, down)
|
||||
local found = false
|
||||
for action, keycode in pairs(self.keys) do
|
||||
if key == keycode then
|
||||
found = true
|
||||
if self.actions[action] and self.plane then
|
||||
self.actions[action](true)
|
||||
end
|
||||
end
|
||||
for action, bindings in pairs(self.keys) do
|
||||
for idx, keycode in pairs(bindings) do
|
||||
if key == keycode then
|
||||
found = true
|
||||
if self.actions[action] and self.plane then
|
||||
self.actions[action](down)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return found
|
||||
end;
|
||||
|
||||
press = function(self, key)
|
||||
return self:pressDown(key, true)
|
||||
end;
|
||||
|
||||
release = function(self, key)
|
||||
local found = false
|
||||
for action, keycode in pairs(self.keys) do
|
||||
if key == keycode then
|
||||
found = true
|
||||
if self.actions[action] and self.plane then
|
||||
self.actions[action](false)
|
||||
end
|
||||
end
|
||||
end
|
||||
return found
|
||||
return self:pressDown(key, false)
|
||||
end;
|
||||
|
||||
joystick = function(self, ...)
|
||||
|
|
24
settings.lua
24
settings.lua
|
@ -13,21 +13,21 @@ HEIGHT = 768
|
|||
|
||||
KEYMAP = {
|
||||
[1] = {
|
||||
ccw = 'k',
|
||||
cw = 'l',
|
||||
flip = ',',
|
||||
shoot = 'o',
|
||||
accelerate = 'j',
|
||||
decelerate = 'm'
|
||||
ccw = { 'k' },
|
||||
cw = { 'l' },
|
||||
flip = { ',' },
|
||||
shoot = { 'o' },
|
||||
accelerate = { 'j' },
|
||||
decelerate = { 'm' }
|
||||
},
|
||||
|
||||
[2] = {
|
||||
ccw = 'a',
|
||||
cw = 's',
|
||||
flip = 'a',
|
||||
shoot = 'w',
|
||||
accelerate = 'x',
|
||||
decelerate = 'z'
|
||||
ccw = { 'a' },
|
||||
cw = { 's' },
|
||||
flip = { 'a' },
|
||||
shoot = { 'w' },
|
||||
accelerate = { 'x' },
|
||||
decelerate = { 'z' }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue