diff --git a/examples/init.lua b/examples/init.lua index 19a25db..56b1195 100644 --- a/examples/init.lua +++ b/examples/init.lua @@ -1,5 +1,6 @@ return { Test = require "examples.test_scene", Test2 = require "examples.test_scene2", - TestMenu = require "examples.test_menus" + TestMenu = require "examples.test_menus", + MainMenu = require "examples.mainmenu", } diff --git a/examples/mainmenu/assets/medium.lua b/examples/mainmenu/assets/medium.lua new file mode 100644 index 0000000..eed9950 --- /dev/null +++ b/examples/mainmenu/assets/medium.lua @@ -0,0 +1,5 @@ +return { + filename = "medium.png", + glyphs = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|} ", + extraspacing = 1, +} diff --git a/examples/mainmenu/assets/medium.png b/examples/mainmenu/assets/medium.png new file mode 100644 index 0000000..271fe5f Binary files /dev/null and b/examples/mainmenu/assets/medium.png differ diff --git a/examples/mainmenu/init.lua b/examples/mainmenu/init.lua new file mode 100644 index 0000000..b8eb7cd --- /dev/null +++ b/examples/mainmenu/init.lua @@ -0,0 +1,103 @@ +-- scenes/mainmenu :: the main menu of the different gamecore examples + +--[[ + 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 Scene = require "gamecore.modules.scenes" +local MainMenu = Scene:extend() + +local ListBox = require "gamecore.modules.menusystem.listbox" +local Widget = require "gamecore.modules.menusystem.widgets" + +local SubMenuWidget = Widget.Text:extend() +local SceneWidget = Widget.Text:extend() + +function MainMenu:new() + MainMenu.super.new(self) + self.assets:addImageFont("medium", "examples/test_menus/assets/medium") + + self:addMenu("main", true) + self:addSubMenu("basic", "Basic Tests") + self:addScene("basic", examples.Test, "Basic Test 1") + self:addScene("basic", examples.Test2, "Basic Test 2") + self:addScene("basic", examples.TestMenu, "Basic Test Menu") + + self.menusystem:switchMenu("main") +end + +-- MENU FUNCTION +-- Functions that serve the handling of menus + +function MainMenu:addMenu(name, nobackbutton) + local screenHeight, screenWidth = core.screen:getDimensions() + local w, h = 424/2, 240 - 48 + local x, y = w / 2, 24 + ListBox(self.menusystem, name, x, y, w, h, 8) +end + +function MainMenu:addSubMenu(name, fullname) + self:addMenu(name) + SubMenuWidget(self, "main", name, fullname) +end + +function MainMenu:addScene(submenu, scene, fullname) + SceneWidget(self, submenu, scene, fullname) +end + +function MainMenu:draw() + love.graphics.setColor(.3, .1, .4, 1) + love.graphics.rectangle("fill", 0, 0, 424, 240) +end + +-- WIDGETS +-- Widgets used by menus + +-- Submenu widget :: go to a submenu + +function SubMenuWidget:new(scene, menu, newmenu, fullname) + self.scene = scene + local widgetmenu = self.scene.menusystem.menus[menu] + local font = self.scene.assets.fonts["medium"] + self.newmenu = newmenu + SubMenuWidget.super.new(self, widgetmenu, font, fullname) +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 + + +return MainMenu diff --git a/main.lua b/main.lua index aaf5913..d29f16d 100644 --- a/main.lua +++ b/main.lua @@ -27,7 +27,7 @@ examples = require "examples" function love.load() core = Core() - examples.Test() + examples.MainMenu() end function love.update(dt)