evil-jrpg/multimenu.lua

67 lines
1.4 KiB
Lua

local Object = require "lib/classic"
---@class MultiMenu
local MultiMenu = Object:extend()
---Creates a new multimenu.
---@param font Font
---@param options table
function MultiMenu:new(font, options)
self.font = font
self.options = options
self.selected = 1
end
function MultiMenu:draw()
-- option drawing constants
local x = 20
local y_anchor = 50
local spacing = 10
y_anchor = y_anchor - spacing
-- draw all the options
for idx, option in ipairs(self.options) do
local y = y_anchor + idx * spacing
self.font:draw(option.text, x, y)
end
-- cursor metric constants
local cursor_width = 4
local cursor_depth = 8
local cursor_spacing = 8
local cursor_offset = 4
-- calculate the cursor's position
local cursor_x = x - cursor_spacing
local cursor_y = y_anchor + cursor_offset + spacing * self.selected
-- draw the cursor
love.graphics.polygon("fill",
cursor_x, cursor_y,
cursor_x - cursor_depth, cursor_y - cursor_width,
cursor_x - cursor_depth, cursor_y + cursor_width
)
end
function MultiMenu:up()
if self.selected == 1 then
self.selected = #self.options
else
self.selected = self.selected - 1
end
end
function MultiMenu:down()
if self.selected == #self.options then
self.selected = 1
else
self.selected = self.selected + 1
end
end
function MultiMenu:select()
self.options[self.selected].callback()
end
return MultiMenu