Label can now use a function for retrieving the label
This allows to remove the duplicate label setting code from menu_state
This commit is contained in:
parent
ef6c6ea8c2
commit
7e454f328d
2 changed files with 21 additions and 24 deletions
|
@ -25,7 +25,11 @@ Label = Class {
|
||||||
love.graphics.setColor(self.color)
|
love.graphics.setColor(self.color)
|
||||||
love.graphics.translate(self.x, self.y)
|
love.graphics.translate(self.x, self.y)
|
||||||
love.graphics.setFont(self.font)
|
love.graphics.setFont(self.font)
|
||||||
love.graphics.printf(self.label, 0, 0, self.xsize, self.align)
|
local label = self.label
|
||||||
|
if type(label) == "function" then
|
||||||
|
label = label()
|
||||||
|
end
|
||||||
|
love.graphics.printf(label, 0, 0, self.xsize, self.align)
|
||||||
--love.graphics.rectangle("fill", 0, 0, self.xsize, self.ysize)
|
--love.graphics.rectangle("fill", 0, 0, self.xsize, self.ysize)
|
||||||
love.graphics.pop()
|
love.graphics.pop()
|
||||||
end;
|
end;
|
||||||
|
|
|
@ -47,7 +47,8 @@ function menu_state:enter()
|
||||||
for idx, key in ipairs(bindingOrder) do
|
for idx, key in ipairs(bindingOrder) do
|
||||||
Label(bindingInfo[key], font, { 255, 255, 255, 255 }, "left", 190, 25, x, y, menu_state)
|
Label(bindingInfo[key], font, { 255, 255, 255, 255 }, "left", 190, 25, x, y, menu_state)
|
||||||
for bindingIdx, binding in ipairs(KEYMAP[player][key]) do
|
for bindingIdx, binding in ipairs(KEYMAP[player][key]) do
|
||||||
local label = Label(binding, font, { 255, 255, 255, 255 }, "center", 100, 25, x + 200 + 100 * (bindingIdx - 1), y, menu_state)
|
local label = Label(function() return KEYMAP[player][key][bindingIdx] end,
|
||||||
|
font, { 255, 255, 255, 255 }, "center", 100, 25, x + 200 + 100 * (bindingIdx - 1), y, menu_state)
|
||||||
label:onClick(function()
|
label:onClick(function()
|
||||||
menu_state:adjustBindings(label, bindingIdx, player, key)
|
menu_state:adjustBindings(label, bindingIdx, player, key)
|
||||||
end)
|
end)
|
||||||
|
@ -58,24 +59,26 @@ function menu_state:enter()
|
||||||
local axisInfo = AXISMAP[player][axis]
|
local axisInfo = AXISMAP[player][axis]
|
||||||
Label(joyBindingInfo[axis], font, { 255, 255, 255, 255 }, "left", 190, 25, x, y, menu_state)
|
Label(joyBindingInfo[axis], font, { 255, 255, 255, 255 }, "left", 190, 25, x, y, menu_state)
|
||||||
for bindingIdx, info in ipairs(axisInfo) do
|
for bindingIdx, info in ipairs(axisInfo) do
|
||||||
local prefix
|
|
||||||
if info.flipped then
|
|
||||||
prefix = "-"
|
|
||||||
else
|
|
||||||
prefix = "+"
|
|
||||||
end
|
|
||||||
local visAxisBaseX = (100 + 30) / 2 + x + 200 + 100 * (bindingIdx - 1)
|
local visAxisBaseX = (100 + 30) / 2 + x + 200 + 100 * (bindingIdx - 1)
|
||||||
|
|
||||||
local signLabel = Label(prefix, font, { 255, 255, 255, 255 }, "center", 20, 25, x + 200 + 100 * (bindingIdx - 1), y, menu_state)
|
local signLabel = Label(function()
|
||||||
|
if AXISMAP[player][axis][bindingIdx].flipped then
|
||||||
|
return "-"
|
||||||
|
else
|
||||||
|
return "+"
|
||||||
|
end
|
||||||
|
end, font, { 255, 255, 255, 255 }, "center", 20, 25, x + 200 + 100 * (bindingIdx - 1), y, menu_state)
|
||||||
|
|
||||||
signLabel:onClick(function()
|
signLabel:onClick(function()
|
||||||
menu_state:switchSign(player, axis, bindingIdx, signLabel)
|
menu_state:switchSign(player, axis, bindingIdx)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local axisLabel = Label(info.axis, font, { 255, 255, 255, 255 }, "center", 70, 25, x + 30 + 200 + 100 * (bindingIdx - 1), y, menu_state)
|
local axisLabel = Label(function()
|
||||||
|
return AXISMAP[player][axis][bindingIdx].axis
|
||||||
|
end, font, { 255, 255, 255, 255 }, "center", 70, 25, x + 30 + 200 + 100 * (bindingIdx - 1), y, menu_state)
|
||||||
|
|
||||||
axisLabel:onClick(function()
|
axisLabel:onClick(function()
|
||||||
menu_state:switchAxis(player, axis, bindingIdx, axisLabel)
|
menu_state:switchAxis(player, axis, bindingIdx)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local visAxis = VisRectangle({ 40, 40, 0, 128 }, 10, 10, nil, visAxisBaseX, y + 11, menu_state)
|
local visAxis = VisRectangle({ 40, 40, 0, 128 }, 10, 10, nil, visAxisBaseX, y + 11, menu_state)
|
||||||
|
@ -95,19 +98,12 @@ function menu_state:enter()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function menu_state:switchSign(player, axis, bindingIdx, signLabel)
|
function menu_state:switchSign(player, axis, bindingIdx)
|
||||||
AXISMAP[player][axis][bindingIdx].flipped = not AXISMAP[player][axis][bindingIdx].flipped
|
AXISMAP[player][axis][bindingIdx].flipped = not AXISMAP[player][axis][bindingIdx].flipped
|
||||||
local prefix;
|
|
||||||
if AXISMAP[player][axis][bindingIdx].flipped then
|
|
||||||
prefix = "-"
|
|
||||||
else
|
|
||||||
prefix = "+"
|
|
||||||
end
|
|
||||||
signLabel.label = prefix
|
|
||||||
save_settings()
|
save_settings()
|
||||||
end
|
end
|
||||||
|
|
||||||
function menu_state:switchAxis(player, axis, bindingIdx, axisLabel)
|
function menu_state:switchAxis(player, axis, bindingIdx)
|
||||||
local joysticks = love.joystick.getJoysticks()
|
local joysticks = love.joystick.getJoysticks()
|
||||||
|
|
||||||
if #joysticks >= player then
|
if #joysticks >= player then
|
||||||
|
@ -115,13 +111,11 @@ function menu_state:switchAxis(player, axis, bindingIdx, axisLabel)
|
||||||
local axisIdx = AXISMAP[player][axis][bindingIdx].axis
|
local axisIdx = AXISMAP[player][axis][bindingIdx].axis
|
||||||
|
|
||||||
axisIdx = axisIdx + 1
|
axisIdx = axisIdx + 1
|
||||||
|
|
||||||
if axisIdx > joystick:getAxisCount() then
|
if axisIdx > joystick:getAxisCount() then
|
||||||
axisIdx = 1
|
axisIdx = 1
|
||||||
end
|
end
|
||||||
|
|
||||||
AXISMAP[player][axis][bindingIdx].axis = axisIdx
|
AXISMAP[player][axis][bindingIdx].axis = axisIdx
|
||||||
axisLabel.label = AXISMAP[player][axis][bindingIdx].axis
|
|
||||||
save_settings()
|
save_settings()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -227,7 +221,6 @@ function menu_state:keypressed(key, unicode)
|
||||||
key = "none"
|
key = "none"
|
||||||
end
|
end
|
||||||
KEYMAP[currentlyChosen.data.player][currentlyChosen.data.key][currentlyChosen.data.bindingIdx] = key
|
KEYMAP[currentlyChosen.data.player][currentlyChosen.data.key][currentlyChosen.data.bindingIdx] = key
|
||||||
currentlyChosen.data.label.label = key
|
|
||||||
currentlyChosen:delete()
|
currentlyChosen:delete()
|
||||||
currentlyChosen = nil
|
currentlyChosen = nil
|
||||||
save_settings()
|
save_settings()
|
||||||
|
|
Loading…
Reference in a new issue