LoveFrames/loveframes/objects/menu.lua

288 lines
6.4 KiB
Lua
Raw Normal View History

2013-12-26 18:30:35 +00:00
--[[------------------------------------------------
-- Love Frames - A GUI library for LOVE --
2014-01-14 11:29:11 +00:00
-- Copyright (c) 2012-2014 Kenny Shields --
2013-12-26 18:30:35 +00:00
--]]------------------------------------------------
2019-03-06 23:19:58 +00:00
return function(loveframes)
---------- module start ----------
2013-12-26 18:30:35 +00:00
-- menu object
local newobject = loveframes.NewObject("menu", "loveframes_object_menu", true)
--[[---------------------------------------------------------
- func: initialize()
- desc: initializes the object
--]]---------------------------------------------------------
function newobject:initialize(menu)
self.type = "menu"
self.width = 80
self.height = 25
self.largest_item_width = 0
self.largest_item_height = 0
self.is_sub_menu = false
self.internal = false
self.parentmenu = nil
self.options = {}
self.internals = {}
2019-03-06 23:19:58 +00:00
self:SetDrawFunc()
2013-12-26 18:30:35 +00:00
end
--[[---------------------------------------------------------
- func: update(deltatime)
- desc: updates the object
--]]---------------------------------------------------------
function newobject:update(dt)
local state = loveframes.state
local selfstate = self.state
if state ~= selfstate then
return
end
local visible = self.visible
local alwaysupdate = self.alwaysupdate
if not visible then
if not alwaysupdate then
return
end
end
self:CheckHover()
local hover = self.hover
local parent = self.parent
local base = loveframes.base
local update = self.Update
-- move to parent if there is a parent
if parent ~= base then
self.x = self.parent.x + self.staticx
self.y = self.parent.y + self.staticy
end
for k, v in ipairs(self.internals) do
local width = v.contentwidth
local height = v.contentheight
if width > self.largest_item_width then
self.largest_item_width = width
end
if height > self.largest_item_height then
self.largest_item_height = height
end
end
local y = 0
self.height = 0
for k, v in ipairs(self.internals) do
v:SetWidth(self.largest_item_width)
if v.option_type ~= "divider" then
v:SetHeight(self.largest_item_height)
else
v:SetHeight(5)
end
v:SetY(y)
self.height = self.height + v.height
y = y + v.height
v:update(dt)
end
self.width = self.largest_item_width
self.largest_item_width = 0
self.largest_item_height = 0
local screen_width = love.graphics.getWidth()
local screen_height = love.graphics.getHeight()
local sx = self.x
local sy = self.y
local width = self.width
local height = self.height
local x1 = sx + width
if sx + width > screen_width then
self.x = screen_width - width
end
if sy + self.height > screen_height then
self.y = screen_height - self.height
end
2013-12-26 18:30:35 +00:00
if update then
update(self, dt)
end
end
--[[---------------------------------------------------------
- func: mousepressed(x, y, button)
- desc: called when the player presses a mouse button
--]]---------------------------------------------------------
function newobject:mousepressed(x, y, button)
local state = loveframes.state
local selfstate = self.state
if state ~= selfstate then
return
end
local visible = self.visible
if not visible then
return
end
end
--[[---------------------------------------------------------
- func: mousereleased(x, y, button)
- desc: called when the player releases a mouse button
--]]---------------------------------------------------------
function newobject:mousereleased(x, y, button)
local state = loveframes.state
local selfstate = self.state
if state ~= selfstate then
return
end
local visible = self.visible
if not visible then
return
end
local internals = self.internals
for k, v in ipairs(internals) do
v:mousereleased(x, y, button)
end
end
--[[---------------------------------------------------------
- func: AddOption(text, icon, func)
- desc: adds an option to the object
--]]---------------------------------------------------------
function newobject:AddOption(text, icon, func)
local menuoption = loveframes.objects["menuoption"]:new(self)
menuoption:SetText(text)
menuoption:SetIcon(icon)
menuoption:SetFunction(func)
table.insert(self.internals, menuoption)
2014-02-11 12:34:07 +00:00
return self
2013-12-26 18:30:35 +00:00
end
2014-01-07 03:50:52 +00:00
--[[---------------------------------------------------------
- func: RemoveOption(id)
- desc: removes an option
--]]---------------------------------------------------------
function newobject:RemoveOption(id)
for k, v in ipairs(self.internals) do
if k == id then
table.remove(self.internals, k)
return
end
end
2014-02-11 12:34:07 +00:00
return self
2014-01-07 03:50:52 +00:00
end
2013-12-26 18:30:35 +00:00
--[[---------------------------------------------------------
- func: AddSubMenu(text, icon, menu)
- desc: adds a submenu to the object
--]]---------------------------------------------------------
function newobject:AddSubMenu(text, icon, menu)
local function activatorFunc(object)
if menu:GetVisible() then
local hoverobject = loveframes.hoverobject
if hoverobject ~= object and hoverobject:GetBaseParent() ~= menu then
menu:SetVisible(false)
end
else
menu:SetVisible(true)
menu:SetPos(object:GetX() + object:GetWidth(), object:GetY())
end
end
menu:SetVisible(false)
local menuoption = loveframes.objects["menuoption"]:new(self, "submenu_activator", menu)
menuoption:SetText(text)
menuoption:SetIcon(icon)
if menu then
menu.is_sub_menu = true
menu.parentmenu = self
end
table.insert(self.internals, menuoption)
2014-02-11 12:34:07 +00:00
return self
2013-12-26 18:30:35 +00:00
end
--[[---------------------------------------------------------
- func: AddDivider()
- desc: adds a divider to the object
--]]---------------------------------------------------------
function newobject:AddDivider()
local menuoption = loveframes.objects["menuoption"]:new(self, "divider")
table.insert(self.internals, menuoption)
2014-02-11 12:34:07 +00:00
return self
2013-12-26 18:30:35 +00:00
end
--[[---------------------------------------------------------
- func: GetBaseMenu(t)
- desc: gets the object's base menu
--]]---------------------------------------------------------
function newobject:GetBaseMenu(t)
local t = t or {}
if self.parentmenu then
table.insert(t, self.parentmenu)
self.parentmenu:GetBaseMenu(t)
else
return self
end
return t[#t]
end
--[[---------------------------------------------------------
- func: SetVisible(bool)
- desc: sets the object's visibility
--]]---------------------------------------------------------
function newobject:SetVisible(bool)
self.visible = bool
if not bool then
local internals = self.internals
for k, v in ipairs(internals) do
if v.menu then
v.activated = false
v.menu:SetVisible(bool)
end
end
end
2014-02-11 12:34:07 +00:00
return self
2014-11-09 21:24:10 +00:00
end
2019-03-06 23:19:58 +00:00
---------- module end ----------
end