Add MultiMenu positioning and backgrounds
This commit is contained in:
parent
e731fc4c38
commit
e4b65b2fd5
13
main.lua
13
main.lua
|
@ -1,4 +1,3 @@
|
||||||
local Animation = require "animation"
|
|
||||||
local Animator = require "animator"
|
local Animator = require "animator"
|
||||||
local Atlas = require "atlas"
|
local Atlas = require "atlas"
|
||||||
local Font = require "font"
|
local Font = require "font"
|
||||||
|
@ -59,25 +58,25 @@ function love.load()
|
||||||
|
|
||||||
options[#options + 1] = {
|
options[#options + 1] = {
|
||||||
text = unit.name,
|
text = unit.name,
|
||||||
callback = function() StateMenu = MultiMenu(BlockyFont, state_menu) end,
|
callback = function()
|
||||||
|
StateMenu = MultiMenu(BlockyFont, state_menu, 32, 32, Window)
|
||||||
|
end,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
UnitMenu = MultiMenu(BlockyFont, options)
|
UnitMenu = MultiMenu(BlockyFont, options, 16, 16, Window)
|
||||||
ActiveUnit = Animator(Units[1].states, "idle")
|
ActiveUnit = Animator(Units[1].states, "idle")
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.draw()
|
function love.draw()
|
||||||
push:start()
|
push:start()
|
||||||
|
|
||||||
Window:draw(100, 100, 5, 5)
|
|
||||||
BlockyFont:draw("Hello, world!")
|
BlockyFont:draw("Hello, world!")
|
||||||
ActiveUnit:draw()
|
ActiveUnit:draw(100, 100)
|
||||||
|
UnitMenu:draw()
|
||||||
|
|
||||||
if StateMenu then
|
if StateMenu then
|
||||||
StateMenu:draw()
|
StateMenu:draw()
|
||||||
else
|
|
||||||
UnitMenu:draw()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
push:finish()
|
push:finish()
|
||||||
|
|
|
@ -6,34 +6,55 @@ local MultiMenu = Object:extend()
|
||||||
---Creates a new multimenu.
|
---Creates a new multimenu.
|
||||||
---@param font Font
|
---@param font Font
|
||||||
---@param options table
|
---@param options table
|
||||||
function MultiMenu:new(font, options)
|
function MultiMenu:new(font, options, x, y, bg)
|
||||||
self.font = font
|
self.font = font
|
||||||
self.options = options
|
self.options = options
|
||||||
self.selected = 1
|
self.selected = 1
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.bg = bg
|
||||||
|
self.padding = 16
|
||||||
|
|
||||||
|
local inner_width = 0
|
||||||
|
local inner_height = #options * self.font.sh
|
||||||
|
|
||||||
|
for idx, option in ipairs(options) do
|
||||||
|
local text_width = string.len(option.text) * self.font.sw
|
||||||
|
inner_width = math.max(inner_width, text_width)
|
||||||
|
end
|
||||||
|
|
||||||
|
local bg_width = inner_width + self.padding * 2
|
||||||
|
local bg_height = inner_height + self.padding * 2
|
||||||
|
|
||||||
|
self.bg_width = math.ceil(bg_width / bg.atlas.sw)
|
||||||
|
self.bg_height = math.ceil(bg_height / bg.atlas.sh)
|
||||||
end
|
end
|
||||||
|
|
||||||
function MultiMenu:draw()
|
function MultiMenu:draw()
|
||||||
-- option drawing constants
|
-- option drawing constants
|
||||||
local x = 20
|
local x = self.x
|
||||||
local y_anchor = 50
|
local y = self.y
|
||||||
local spacing = 10
|
local spacing = self.font.sh
|
||||||
y_anchor = y_anchor - spacing
|
local padding = self.padding
|
||||||
|
|
||||||
|
--draw the background
|
||||||
|
self.bg:draw(x, y, self.bg_width, self.bg_height)
|
||||||
|
|
||||||
-- draw all the options
|
-- draw all the options
|
||||||
for idx, option in ipairs(self.options) do
|
for idx, option in ipairs(self.options) do
|
||||||
local y = y_anchor + idx * spacing
|
local ty = y + (idx - 1) * spacing
|
||||||
self.font:draw(option.text, x, y)
|
self.font:draw(option.text, x + padding, ty + padding)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- cursor metric constants
|
-- cursor metric constants
|
||||||
local cursor_width = 4
|
local cursor_width = 4
|
||||||
local cursor_depth = 8
|
local cursor_depth = 8
|
||||||
local cursor_spacing = 8
|
local cursor_spacing = 4
|
||||||
local cursor_offset = 4
|
local cursor_offset = 4
|
||||||
|
|
||||||
-- calculate the cursor's position
|
-- calculate the cursor's position
|
||||||
local cursor_x = x - cursor_spacing
|
local cursor_x = x - cursor_spacing + padding
|
||||||
local cursor_y = y_anchor + cursor_offset + spacing * self.selected
|
local cursor_y = y + cursor_offset + (self.selected - 1) * spacing + padding
|
||||||
|
|
||||||
-- draw the cursor
|
-- draw the cursor
|
||||||
love.graphics.polygon("fill",
|
love.graphics.polygon("fill",
|
||||||
|
|
Loading…
Reference in New Issue