epervier-old/framework/scenes/gui/textmenu/init.lua

113 lines
3.6 KiB
Lua
Raw Normal View History

2021-01-23 18:30:45 +01:00
-- listbox : a text-based menu.
-- It allow to easily handle a listbox with text-widget and automatically add a widget
-- for submenu.
-- It also make the menu manage the font and the selection color, for a simpler handling
--[[
Copyright © 2020 Kazhnuz
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
2022-08-13 12:43:49 +02:00
local ListBox = require "framework.scenes.gui.menus.listbox"
2021-01-23 18:30:45 +01:00
local TextMenu = ListBox:extend()
2022-08-13 12:43:49 +02:00
TextMenu.baseWidgets = require "framework.scenes.gui.textmenu.widgets"
2021-01-23 18:30:45 +01:00
local BASE_PADDING = 8
2021-12-04 13:18:54 +01:00
function TextMenu:new(name, font, x, y, w, slotNumber, padding, lineSize)
lineSize = lineSize or 1
2021-01-23 18:30:45 +01:00
self:setFont(font)
2021-12-04 13:18:54 +01:00
self.lineHeight = self.font:getHeight() * lineSize
2021-01-23 18:30:45 +01:00
self.name = name
2021-12-04 13:18:54 +01:00
2021-01-23 18:30:45 +01:00
self.padding = padding or BASE_PADDING
2021-12-04 13:18:54 +01:00
TextMenu.super.new(self, name, x, y, w, (self.lineHeight * slotNumber), slotNumber)
end
2022-07-27 21:57:34 +02:00
function TextMenu:addItem(text, position, func, type, additionnalItems, color, additionnalDatas)
2021-12-04 13:18:54 +01:00
local widget = TextMenu.baseWidgets.Base(self.name, text, position)
widget:setFunc(func)
widget.type = type or "select"
if (additionnalItems ~= nil) then
for _, item in ipairs(additionnalItems) do
widget:addLabel(item[1], item[2])
end
end
if (color ~= nil) then
widget:setColor(color[1], color[2], color[3])
end
2022-07-27 21:57:34 +02:00
if (additionnalDatas ~= nil) then
widget.datas = additionnalDatas
end
2021-12-04 13:18:54 +01:00
end
function TextMenu:generateSubmenu(pageName, label, parent, list, func, backWidget)
self:addSubmenu(pageName, label, parent, backWidget)
for _, data in ipairs(list) do
self:addItem(func(data))
end
2021-01-23 18:30:45 +01:00
end
function TextMenu:setFont(fontName)
2022-08-10 19:16:22 +02:00
local scene = core.scenemanager.nextScene or core.scenemanager.currentScene
2021-01-23 18:30:45 +01:00
self.font = scene.assets:getFont(fontName)
end
function TextMenu:getFont()
return self.font
end
function TextMenu:setSelectedColor(r, g, b)
self.selectedColor = {}
self.selectedColor.r = r
self.selectedColor.g = g
self.selectedColor.b = b
end
function TextMenu:getSelectedColor()
2021-12-04 13:18:54 +01:00
return self.selectedColor
2021-01-23 18:30:45 +01:00
end
function TextMenu:getPadding()
return self.padding
end
2021-12-04 13:18:54 +01:00
function TextMenu:getPaddingLeft()
return self.paddingLeft or self:getPadding()
end
function TextMenu:getPaddingRight()
return self.paddingRight or self:getPadding()
end
2021-01-23 18:30:45 +01:00
function TextMenu:addSubmenu(pageName, label, parent, backWidget)
local label = label or pageName
local parent = parent or "main"
self:switch(parent)
TextMenu.baseWidgets.SubMenu(self.name, pageName, label)
TextMenu.super.addSubmenu(self, pageName, parent)
if (backWidget ~= false) then
TextMenu.baseWidgets.Back(self.name)
2021-12-04 13:18:54 +01:00
self:setCancelWidget("last")
2021-01-23 18:30:45 +01:00
end
end
return TextMenu