From 625123813c37fdf68a337d690e3fc389597807ae Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Wed, 19 Aug 2020 20:44:08 +0200 Subject: [PATCH] feat: initial work on a common menu framework --- .../game/modules/menus/list.lua | 166 ++++++++++++++++++ .../game/modules/menus/parents/menu.lua | 36 ++++ 2 files changed, 202 insertions(+) create mode 100644 sonic-radiance.love/game/modules/menus/list.lua create mode 100644 sonic-radiance.love/game/modules/menus/parents/menu.lua diff --git a/sonic-radiance.love/game/modules/menus/list.lua b/sonic-radiance.love/game/modules/menus/list.lua new file mode 100644 index 0000000..02274a4 --- /dev/null +++ b/sonic-radiance.love/game/modules/menus/list.lua @@ -0,0 +1,166 @@ +local ParentMenu = require "game.modules.menus.parents.menu" +local Widget = require "core.modules.menusystem.widgets" + +local list = {} +list.ListMenu = ParentMenu:extend() +list.CenteredWidget = Widget.Text:extend() +list.DualTextWidget = list.CenteredWidget:extend() +list.SubMenuWidget = list.DualTextWidget:extend() + +local MENU_ITEM_HEIGHT = 17 + +-- ListMenu +function list.ListMenu:new(scene, name, x, y, w, itemNumber, isBoxed) + self.scene = scene + self.name = name + local h = itemNumber * MENU_ITEM_HEIGHT + list.ListMenu.super.new(self, scene, name, x, y, w, h, itemNumber) + self.cursorTexture = love.graphics.newImage("assets/gui/cursor-menulist.png") + self.cursorTransition = 0 + self.parent = "" + self.submenus = {} + self.itemNumber = itemNumber + + self.isBoxed = isBoxed + if (self.isBoxed) then + self.box = gui.newTextBox("assets/gui/dialogbox.png", w+16, h+16) + end +end + +function list.ListMenu:addSubMenu(name, nicerName) + print("adding submenu", name, "to", self.name) + local submenu = self:clone(name) + submenu:setAsSubmenu(self.name) + self:addSubMenuWidget(name, nicerName) + table.insert(self.submenus, name) +end + +function list.ListMenu:addSubMenuWidget(newmenu, name) + list.SubMenuWidget(self.scene, self.name, newmenu, name) +end + +function list.ListMenu:finalize() + print("finalizing", self.name) + if (self.parent ~= "") then + self:addSubMenuWidget(self.parent, "Back") + self:setCancelWidget() + end + + for i,name in ipairs(self.submenus) do + if (self.menusystem.menus[name] ~= nil) then + self.menusystem.menus[name]:finalize() + end + end +end + +function list.ListMenu:clone(name) + return list.ListMenu(self.scene, name, self.x, self.y, self.w, self.itemNumber) +end + +function list.ListMenu:getSubmenuWidget() + return nil +end + +function list.ListMenu:setAsSubmenu(parent) + self.parent = parent +end + +function list.ListMenu:update(dt) + list.ListMenu.super.update(self, dt) + self:updateCursorPosition(dt) +end + +function list.ListMenu:updateCursorPosition(dt) + local relativecursor = self.widget.selected - self.view.firstSlot + + local transition = self.cursorTransition - relativecursor + if math.abs(transition) < 0.1 then + self.cursorTransition = relativecursor + else + local speed = dt*45 + local movement = ((relativecursor) - (self.cursorTransition)) + self.cursorTransition = (self.cursorTransition) + movement * speed + end +end + +function list.ListMenu:drawCursor() + local x, y = self:getCursorPosition() + love.graphics.draw(self.cursorTexture, x, y) +end + +function list.ListMenu:getCursorPosition() + local addition = MENU_ITEM_HEIGHT + local x = self.x + 4 + local y = self.y + ((self.cursorTransition) * addition) + return x, y +end + +function list.ListMenu:draw() + if (self.isBoxed) then + + end + self:updateView() + local widgety = self.y + local widgetx = self.x + for i,v in ipairs(self.widget.list) do + if (i >= self.view.firstSlot) and (i < self.view.firstSlot + self.view.slotNumber) then + v:draw(widgetx, widgety, self.w, self.widget.h) + if self.widget.selected == i and self:haveFocus() == true then + v:drawSelected(widgetx, widgety, self.w, self.widget.h) + else + v:draw(widgetx, widgety, self.w, self.widget.h) + end + widgetx, widgety = self:getNextPosition(widgetx, widgety, self.widget.h) + end + end +end + +function list.ListMenu:getNextPosition(x, y, h) + return x, y+h +end + +-- Widgets +-- Tout les widgets pour la liste de base + +-- Widget centré +function list.CenteredWidget:new(scene, menu_name, label) + local font = scene.assets.fonts["small"] + font:setFilter("shadow") + self.scene = scene + local widgetMenu = scene.menusystem.menus[menu_name] + list.CenteredWidget.super.new(self, widgetMenu, font, label) +end + +function list.CenteredWidget:drawCanvas() + local h = math.floor(self.height / 2) - (self.font:getHeight() / 2) + self.font:draw(self.label, self.width / 2, h, -1, "center") +end + +-- Widget avec deux bouts de textes +function list.DualTextWidget:new(scene, menu_name, label, label2) + self.label2 = label2 + list.DualTextWidget.super.new(self, scene, menu_name, label) +end + +function list.DualTextWidget:drawCanvas() + local h = math.floor(self.height / 2) - (self.font:getHeight() / 2) + self.font:draw(self.label, 0, h, -1, "left") + self.font:draw(self.label2, self.width, h, -1, "right") +end + +-- Widget de sous-menu +function list.SubMenuWidget:new(scene, menu_name, newmenu, label) + local label2 = "" + if (label ~= "Back") then + label2 = ">" + end + list.SubMenuWidget.super.new(self, scene, menu_name, label, label2) + self.newmenu = newmenu +end + +function list.SubMenuWidget:action() + self.scene.menusystem:switchMenu(self.newmenu) + self.scene.menusystem.menus[self.newmenu]:activationAction() +end + +return list diff --git a/sonic-radiance.love/game/modules/menus/parents/menu.lua b/sonic-radiance.love/game/modules/menus/parents/menu.lua new file mode 100644 index 0000000..f534cef --- /dev/null +++ b/sonic-radiance.love/game/modules/menus/parents/menu.lua @@ -0,0 +1,36 @@ +local ListBox = require "core.modules.menusystem.listbox" +local TweenManager = require "game.modules.tweenmanager" + +local RadianceMenu = ListBox:extend() + +function RadianceMenu:new(scene, name, x, y, w, h, itemNumber) + self.scene = scene + self.tweens = TweenManager(self) + RadianceMenu.super.new(self, self.scene.menusystem, name, x, y, w, h, itemNumber) +end + +function RadianceMenu:update(dt) + self.tweens:update(dt) + RadianceMenu.super.update(self, dt) +end + +function RadianceMenu:moveFrom(x, y) + local newX, newY = self.x, self.y + self.x, self.y = x, y + + self.tweens:addTween(0, 0.3, {x = newX, y = newY}, "inOutQuad") +end + +function RadianceMenu:moveTo(newX, newY) + self.tweens:addTween(0, 0.3, {x = newX, y = newY}, "inOutQuad") +end + +function RadianceMenu:getCenter() + return self.x + self.width, self.y + self.height +end + +function RadianceMenu:activationAction() + -- Do nothing +end + +return RadianceMenu