From ae13c1d5ae4444142894961473c9b3d87438fa28 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sat, 28 Aug 2021 18:08:02 +0200 Subject: [PATCH] feat: ajout support du son au menu GUI --- sonic-radiance.love/birb/modules/gui/init.lua | 18 ++++++++++++ .../birb/modules/gui/menus/flowbox.lua | 4 +++ .../birb/modules/gui/menus/grid.lua | 4 +++ .../birb/modules/gui/menus/hlistbox.lua | 2 ++ .../birb/modules/gui/menus/listbox.lua | 5 ++-- .../birb/modules/gui/menus/model/page.lua | 1 + .../birb/modules/gui/menus/parent.lua | 28 +++++-------------- .../birb/modules/gui/menus/widgets/base.lua | 13 ++++++--- 8 files changed, 48 insertions(+), 27 deletions(-) diff --git a/sonic-radiance.love/birb/modules/gui/init.lua b/sonic-radiance.love/birb/modules/gui/init.lua index 01c2f0f..7f6fbe5 100644 --- a/sonic-radiance.love/birb/modules/gui/init.lua +++ b/sonic-radiance.love/birb/modules/gui/init.lua @@ -33,6 +33,7 @@ local TransformDataStruct = require "birb.structures.tween" function Gui:new(scene) self.scene = scene self:reset() + self.sfx = {} end function Gui:reset() @@ -125,6 +126,23 @@ function Gui:showScreen(screenname) self.screens[screenname]:show() end +-- SOUND FUNCTIONS +-- Handle SFX + +function Gui:addSFX(guiName, sfxName) + self.sfx[guiName] = sfxName +end + +function Gui:playSFX(type) + local sfxName = self.sfx[type] + if (sfxName ~= nil) then + local sfx = self.scene.assets.sfx[sfxName] + if (sfx ~= nil) then + sfx:play() + end + end +end + -- KEYBOARD FUNCTIONS -- Handle keyboard diff --git a/sonic-radiance.love/birb/modules/gui/menus/flowbox.lua b/sonic-radiance.love/birb/modules/gui/menus/flowbox.lua index 52a8870..328bb7a 100644 --- a/sonic-radiance.love/birb/modules/gui/menus/flowbox.lua +++ b/sonic-radiance.love/birb/modules/gui/menus/flowbox.lua @@ -85,18 +85,22 @@ function FlowBox:moveByKeys(key) local col, line = self.view:getCoord(self.widget:getSelected()) if key == 'left' then self:moveCursor2D(col - 1, line) + self:playNavigationSound() end if key == 'right' then self:moveCursor2D(col + 1, line) + self:playNavigationSound() end if key == 'up' then self:moveCursor2D(col, line - 1) + self:playNavigationSound() end if key == 'down' then self:moveCursor2D(col, line + 1) + self:playNavigationSound() end end diff --git a/sonic-radiance.love/birb/modules/gui/menus/grid.lua b/sonic-radiance.love/birb/modules/gui/menus/grid.lua index 1bd0bf1..3c98a1c 100644 --- a/sonic-radiance.love/birb/modules/gui/menus/grid.lua +++ b/sonic-radiance.love/birb/modules/gui/menus/grid.lua @@ -150,18 +150,22 @@ function GridBox:keyreleased(key, code) local col, line = self.cursor.x, self.cursor.y if key == 'left' then self:moveCol(-1) + self:playNavigationSound() end if key == 'right' then self:moveCol(1) + self:playNavigationSound() end if key == 'up' then self:moveLine(-1) + self:playNavigationSound() end if key == 'down' then self:moveLine(1) + self:playNavigationSound() end if key == "A" and self.widget:getSelected() <= self.widget:lenght() then diff --git a/sonic-radiance.love/birb/modules/gui/menus/hlistbox.lua b/sonic-radiance.love/birb/modules/gui/menus/hlistbox.lua index c10d772..7c51643 100644 --- a/sonic-radiance.love/birb/modules/gui/menus/hlistbox.lua +++ b/sonic-radiance.love/birb/modules/gui/menus/hlistbox.lua @@ -60,10 +60,12 @@ end function HListBox:moveByKeys(key, code) if key == 'left' then self.widget:moveCursor(-1) + self:playNavigationSound() end if key == 'right' then self.widget:moveCursor(1) + self:playNavigationSound() end end diff --git a/sonic-radiance.love/birb/modules/gui/menus/listbox.lua b/sonic-radiance.love/birb/modules/gui/menus/listbox.lua index 2a6f30c..05a3095 100644 --- a/sonic-radiance.love/birb/modules/gui/menus/listbox.lua +++ b/sonic-radiance.love/birb/modules/gui/menus/listbox.lua @@ -60,10 +60,12 @@ end function ListBox:moveByKeys(key) if key == 'up' then self.widget:moveCursor(-1) + self:playNavigationSound() end if key == 'down' then self.widget:moveCursor(1) + self:playNavigationSound() end end @@ -84,7 +86,6 @@ function ListBox:drawTexture() local listWidget = self.widget:getList(self.view.firstSlot, self.view.slotNumber) for _, widget in ipairs(listWidget) do - print(self.canvas.padding, widgety) widget:drawWidget(self.canvas.padding, widgety, self.w, self.widgetSize.h) widgety = widgety + self.widgetSize.h end @@ -95,7 +96,7 @@ function ListBox:getGraphicalCursorPosition() local w, h = self:getWidgetSize() local y = (self.widget:getSelected() - self.view.firstSlot) * h - return self.x,self.y + y, w, h + return self.x - self.ox,self.y + y - self.oy, w, h end return ListBox diff --git a/sonic-radiance.love/birb/modules/gui/menus/model/page.lua b/sonic-radiance.love/birb/modules/gui/menus/model/page.lua index e6a0496..e2c13e0 100644 --- a/sonic-radiance.love/birb/modules/gui/menus/model/page.lua +++ b/sonic-radiance.love/birb/modules/gui/menus/model/page.lua @@ -74,6 +74,7 @@ end function Page:action(id, type) if (self:widgetExist(id)) then self.widgets[id]:action(type) + self.widgets[id]:playSFX() end end diff --git a/sonic-radiance.love/birb/modules/gui/menus/parent.lua b/sonic-radiance.love/birb/modules/gui/menus/parent.lua index dba9084..c3dce9e 100644 --- a/sonic-radiance.love/birb/modules/gui/menus/parent.lua +++ b/sonic-radiance.love/birb/modules/gui/menus/parent.lua @@ -39,7 +39,6 @@ function Menu:new(name, x, y, w, h) self.widgetSize = {} self:updateWidgetSize() - self:resetSound() self:initCanvas() end @@ -141,7 +140,7 @@ end -- UPDATE FUNCTIONS function Menu:updateElement(dt) - self:update(dt) + Menu.super.updateElement(self, dt) self.widget:update(dt) if (self.canvas.needRedraw or self.canvas.isAnimated) then self:redraw() @@ -204,7 +203,6 @@ function Menu:setCursor(cursorid) end function Menu:moveCursor(new_selected) - self:playNavigationSound() self.widget:moveCursorAbsolute(new_selected) self.canvas.needRedraw = true end @@ -212,25 +210,13 @@ end -- SOUND FUNCTION -- Handle SFX -function Menu:resetSound() - self.sound = {} - self.sound.active = false - self.sound.asset = nil -end - -function Menu:setSoundFromSceneAssets(name) - self:setSound(self.menusystem.scene.assets:getWithType(name, "sfx")) -end - -function Menu:setSound(soundasset) - self.sound.active = true - self.sound.asset = soundasset -end - function Menu:playNavigationSound() - if self.sound.active == true then - self.sound.asset:play() - end + self:playSFX("navigate") +end + +function Menu:playSFX(name) + local gui = self:getGui() + gui:playSFX(name) end -- VIEW FUNCTIONS diff --git a/sonic-radiance.love/birb/modules/gui/menus/widgets/base.lua b/sonic-radiance.love/birb/modules/gui/menus/widgets/base.lua index e6b594e..e295bb8 100644 --- a/sonic-radiance.love/birb/modules/gui/menus/widgets/base.lua +++ b/sonic-radiance.love/birb/modules/gui/menus/widgets/base.lua @@ -42,6 +42,7 @@ function BaseWidget:new(menuName) self.order = 0 self:register() self.func = nil + self.type = "select" end function BaseWidget:setFunc(func) @@ -106,9 +107,7 @@ function BaseWidget:drawCanvas() end function BaseWidget:selectAction() - if (self.func ~= nil) then - self.func(self) - end + -- Do nothing end -- DRAW WIDGETS @@ -152,8 +151,14 @@ end -- ACTION FUNCTION -- Functions to handle actions and selection. +function BaseWidget:playSFX() + self.menu:playSFX(self.type) +end + function BaseWidget:action(source) - --self:destroy() + if (self.func ~= nil) then + self.func(self) + end end function BaseWidget:destroy()