LoveFrames/loveframes/objects/menu.lua
rozenmad edab2be4c7 Added grid span feature, fixes
- Grid span feature
- Added offset from parent to update each element (for correct positioning relative to each parent)
- OnClick event rework
- Multichoice: new function GetChoiceIndex
- Added positioning of items in the grid using the align parameter in AddItem
- Correct positioning of multichoice-arrow image
- Fixed stencil test for list
- Fixed a bug that can trigger OnClick events again, even if the mouse cursor is not already hovering over the object
- Fixed a bug where "backspace" leads to an incorrect positioning of the entry if the text is overflowed 'textinput' element
2021-09-05 20:46:49 +03:00

291 lines
6.6 KiB
Lua

--[[------------------------------------------------
-- Love Frames - A GUI library for LOVE --
-- Copyright (c) 2012-2014 Kenny Shields --
--]]------------------------------------------------
return function(loveframes)
---------- module start ----------
-- 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 = {}
self:SetDrawFunc()
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 - (parent.offsetx or 0)
self.y = self.parent.y + self.staticy - (parent.offsety or 0)
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
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
local internals = self.internals
for k, v in ipairs(internals) do
v:mousepressed(x, y, button)
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)
return self
end
--[[---------------------------------------------------------
- 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
return self
end
--[[---------------------------------------------------------
- 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)
return self
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)
return self
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
return self
end
---------- module end ----------
end