LoveFrames/loveframes/objects/internal/menuoption.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

222 lines
5.3 KiB
Lua

--[[------------------------------------------------
-- Love Frames - A GUI library for LOVE --
-- Copyright (c) 2012-2014 Kenny Shields --
--]]------------------------------------------------
return function(loveframes)
---------- module start ----------
-- menuoption object
local newobject = loveframes.NewObject("menuoption", "loveframes_object_menuoption", true)
--[[---------------------------------------------------------
- func: initialize()
- desc: initializes the object
--]]---------------------------------------------------------
function newobject:initialize(parent, option_type, menu)
self.type = "menuoption"
self.text = "Option"
self.width = 100
self.height = 25
self.contentwidth = 0
self.contentheight = 0
self.parent = parent
self.option_type = option_type or "option"
self.menu = menu
self.activated = false
self.internal = true
self.icon = false
self.func = nil
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
local hover = self.hover
self:CheckHover()
local parent = self.parent
local option_type = self.option_type
local activated = self.activated
local base = loveframes.base
local update = self.Update
if option_type == "submenu_activator" then
if hover and not activated then
self.menu:SetVisible(true)
self.menu:MoveToTop()
self.activated = true
elseif not hover and activated then
local hoverobject = loveframes.hoverobject
if hoverobject and hoverobject:GetBaseParent() == self.parent then
self.menu:SetVisible(false)
self.activated = false
end
elseif activated then
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 x1 + self.menu.width <= screen_width then
self.menu.x = x1
else
self.menu.x = sx - self.menu.width
end
if sy + self.menu.height <= screen_height then
self.menu.y = sy
else
self.menu.y = (sy + height) - self.menu.height
end
end
end
-- 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
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
self.down = true
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 hover = self.hover
local option_type = self.option_type
if hover and option_type ~= "divider" and button == 1 and self.down then
local func = self.func
if func then
local text = self.text
func(self, text)
end
local basemenu = self.parent:GetBaseMenu()
basemenu:SetVisible(false)
self.hover = false
end
self.down = false
end
--[[---------------------------------------------------------
- func: SetText(text)
- desc: sets the object's text
--]]---------------------------------------------------------
function newobject:SetText(text)
self.text = text
end
--[[---------------------------------------------------------
- func: GetText()
- desc: gets the object's text
--]]---------------------------------------------------------
function newobject:GetText()
return self.text
end
--[[---------------------------------------------------------
- func: SetIcon(icon)
- desc: sets the object's icon
--]]---------------------------------------------------------
function newobject:SetIcon(icon)
if type(icon) == "string" then
self.icon = love.graphics.newImage(icon)
self.icon:setFilter("nearest", "nearest")
elseif type(icon) == "userdata" then
self.icon = icon
end
end
--[[---------------------------------------------------------
- func: GetIcon()
- desc: gets the object's icon
--]]---------------------------------------------------------
function newobject:GetIcon()
return self.icon
end
--[[---------------------------------------------------------
- func: SetFunction(func)
- desc: sets the object's function
--]]---------------------------------------------------------
function newobject:SetFunction(func)
self.func = func
end
---------- module end ----------
end