examples/options: separate widgets from main file

This commit is contained in:
Kazhnuz 2019-04-13 10:10:35 +02:00
parent 2b45098bcf
commit 8a50e77796
2 changed files with 98 additions and 73 deletions

View file

@ -25,13 +25,7 @@ local Scene = require "gamecore.modules.scenes"
local OptionsMenu = Scene:extend() local OptionsMenu = Scene:extend()
local ListBox = require "gamecore.modules.menusystem.listbox" local ListBox = require "gamecore.modules.menusystem.listbox"
local Widget = require "gamecore.modules.menusystem.widgets" local Widgets = require "examples.menus.options.widgets"
local DoubleTextWidget = Widget.Text:extend()
local SubMenuWidget = DoubleTextWidget:extend()
local SceneWidget = Widget.Text:extend()
local ExitWidget = Widget.Text:extend()
function OptionsMenu:new() function OptionsMenu:new()
OptionsMenu.super.new(self) OptionsMenu.super.new(self)
@ -47,7 +41,7 @@ function OptionsMenu:new()
self:addSubMenu("gameplay", "Games Examples") self:addSubMenu("gameplay", "Games Examples")
self:addScene("gameplay", examples.MovePlayer, "Movable Player") self:addScene("gameplay", examples.MovePlayer, "Movable Player")
ExitWidget(self, "main") Widgets.Exit(self, "main")
self.menusystem:switchMenu("main") self.menusystem:switchMenu("main")
end end
@ -63,12 +57,12 @@ end
function OptionsMenu:addSubMenu(name, fullname) function OptionsMenu:addSubMenu(name, fullname)
self:addMenu(name) self:addMenu(name)
SubMenuWidget(self, "main", name, fullname) Widgets.SubMenu(self, "main", name, fullname)
SubMenuWidget(self, name, "main", "Back", 1, "<") Widgets.SubMenu(self, name, "main", "Back", 1, "<")
end end
function OptionsMenu:addScene(submenu, scene, fullname) function OptionsMenu:addScene(submenu, scene, fullname)
SceneWidget(self, submenu, scene, fullname) Widgets.Dummy(self, submenu, fullname)
end end
function OptionsMenu:draw() function OptionsMenu:draw()
@ -76,66 +70,4 @@ function OptionsMenu:draw()
love.graphics.rectangle("fill", 0, 0, 424, 240) love.graphics.rectangle("fill", 0, 0, 424, 240)
end end
-- WIDGETS
-- Widgets used by menus
-- DoubleText widget : a two-side text widget
function DoubleTextWidget:new(menu, font, label1, label2)
DoubleTextWidget.super.new(self, menu, font, label1)
self.label2 = label2 or ""
end
function DoubleTextWidget:drawCanvas()
local w, h
w = math.floor(self.width)
h = math.floor(self.height / 2) - (self.font:getHeight() / 2)
self.font:draw(self.label, 4, h, -1, "left")
self.font:draw(self.label2, w-4, h, -1, "right")
end
-- Submenu widget :: go to a submenu
function SubMenuWidget:new(scene, menu, newmenu, fullname, order, label2)
self.scene = scene
local widgetmenu = self.scene.menusystem.menus[menu]
local font = self.scene.assets.fonts["medium"]
local label2 = label2 or ">"
self.newmenu = newmenu
SubMenuWidget.super.new(self, widgetmenu, font, fullname, label2)
self.order = order or 0
end
function SubMenuWidget:action()
self.scene.menusystem:switchMenu(self.newmenu)
end
-- Scene widget :: switch scene
function SceneWidget:new(scene, menu, newscene, fullname)
self.scene = scene
local widgetmenu = self.scene.menusystem.menus[menu]
local font = self.scene.assets.fonts["medium"]
self.newscene = newscene
SceneWidget.super.new(self, widgetmenu, font, fullname)
end
function SceneWidget:action()
core.scenemanager:storeCurrentScene("mainmenu")
self.newscene()
end
-- Exit Widget : exit the examples
function ExitWidget:new(scene, menu)
self.scene = scene
local widgetmenu = self.scene.menusystem.menus[menu]
local font = self.scene.assets.fonts["medium"]
SceneWidget.super.new(self, widgetmenu, font, "Exit")
end
function ExitWidget:action()
core.scenemanager:setStoredScene("mainmenu")
end
return OptionsMenu return OptionsMenu

View file

@ -0,0 +1,93 @@
-- options/widgets :: options menu widgets
--[[
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 widgets = {}
local Widget = require "gamecore.modules.menusystem.widgets"
local DoubleTextWidget = Widget.Text:extend()
widgets.SubMenu = DoubleTextWidget:extend()
widgets.Dummy = Widget.Text:extend()
widgets.Exit = Widget.Text:extend()
-- BASIC WIDGETS
-- Simple and reusables widgets
-- DoubleText widget : a two-side text widget
function DoubleTextWidget:new(menu, font, label1, label2)
DoubleTextWidget.super.new(self, menu, font, label1)
self.label2 = label2 or ""
end
function DoubleTextWidget:drawCanvas()
local w, h
w = math.floor(self.width)
h = math.floor(self.height / 2) - (self.font:getHeight() / 2)
self.font:draw(self.label, 4, h, -1, "left")
self.font:draw(self.label2, w-4, h, -1, "right")
end
-- Submenu widget :: go to a submenu
function widgets.SubMenu:new(scene, menu, newmenu, fullname, order, label2)
self.scene = scene
local widgetmenu = self.scene.menusystem.menus[menu]
local font = self.scene.assets.fonts["medium"]
local label2 = label2 or ">"
self.newmenu = newmenu
widgets.SubMenu.super.new(self, widgetmenu, font, fullname, label2)
self.order = order or 0
end
function widgets.SubMenu:action()
self.scene.menusystem:switchMenu(self.newmenu)
end
-- Dummy widget :: An empty widget to serve as a base for others
function widgets.Dummy:new(scene, menu, fullname)
self.scene = scene
local widgetmenu = self.scene.menusystem.menus[menu]
local font = self.scene.assets.fonts["medium"]
widgets.Dummy.super.new(self, widgetmenu, font, fullname)
end
function widgets.Dummy:action()
-- shoosh
end
-- Exit Widget : exit the examples
function widgets.Exit:new(scene, menu)
self.scene = scene
local widgetmenu = self.scene.menusystem.menus[menu]
local font = self.scene.assets.fonts["medium"]
widgets.Exit.super.new(self, widgetmenu, font, "Exit")
end
function widgets.Exit:action()
core.scenemanager:setStoredScene("mainmenu")
end
return widgets