feat: add a basic text menu

This commit is contained in:
Kazhnuz 2021-01-23 18:30:45 +01:00
parent 772becc1a8
commit 870a4f7118
5 changed files with 232 additions and 0 deletions

View file

@ -0,0 +1,78 @@
-- 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.
]]
local ListBox = require "birb.modules.menusystem.menus.listbox"
local TextMenu = ListBox:extend()
TextMenu.baseWidgets = require "birb.modules.menusystem.textmenu.widgets"
local BASE_PADDING = 8
function TextMenu:new(name, font, x, y, w, slotNumber, padding)
self:setFont(font)
self.name = name
local h = self.font:getHeight() * slotNumber
self.padding = padding or BASE_PADDING
self:setSelectedColor(1,1,1)
TextMenu.super.new(self, nil, name, x, y, w, h, slotNumber)
end
function TextMenu:setFont(fontName)
local scene = core.scenemanager.currentScene
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()
return self.selectedColor.r, self.selectedColor.g, self.selectedColor.b
end
function TextMenu:getPadding()
return self.padding
end
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)
end
end
return TextMenu

View file

@ -0,0 +1,37 @@
-- backwidget : the base widget to go to parent submenu
--[[
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.
]]
local TextMenuWidget = require "birb.modules.menusystem.textmenu.widgets.basic"
local BackWidget = TextMenuWidget:extend()
function BackWidget:new(menuName)
BackWidget.super.new(self, menuName, "Back", "left")
self.order = 1000
end
function BackWidget:action()
self.menu:back()
end
return BackWidget

View file

@ -0,0 +1,47 @@
-- widget : the base widget for a textmenu
-- It replace the functions to get the font and the selectionColor by wrapper to
-- TextMenu.
--[[
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.
]]
local TextWidget = require "birb.modules.menusystem.menus.widgets.text"
local TextMenuWidget = TextWidget:extend()
function TextMenuWidget:new(menuName, label, position)
local position = position or "left"
TextMenuWidget.super.new(self, menuName, "", label, position, 0)
end
function TextMenuWidget:getFont()
return self.menu:getFont()
end
function TextMenuWidget:getSelectedColor()
return self.menu:getSelectedColor()
end
function TextMenuWidget:getPadding()
return self.menu:getPadding()
end
return TextMenuWidget

View file

@ -0,0 +1,31 @@
-- widgets :: basic widget object
--[[
Copyright © 2019 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.
]]
local Widget = {}
-- Add the widget as subvariable to the returned table
Widget.Base = require "birb.modules.menusystem.textmenu.widgets.basic"
Widget.SubMenu= require "birb.modules.menusystem.textmenu.widgets.submenu"
Widget.Back = require "birb.modules.menusystem.textmenu.widgets.back"
return Widget

View file

@ -0,0 +1,39 @@
-- widget : the base widget to go to a submenu
-- Make go to a submenu
--[[
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.
]]
local TextMenuWidget = require "birb.modules.menusystem.textmenu.widgets.basic"
local SubmenuWidget = TextMenuWidget:extend()
function SubmenuWidget:new(menuName, submenu, label, position)
self.submenu = submenu
SubmenuWidget.super.new(self, menuName, label, position)
self:addLabel(">", "right")
end
function SubmenuWidget:action()
self.menu:switch(self.submenu)
end
return SubmenuWidget