feat: ajout support du son au menu GUI

This commit is contained in:
Kazhnuz 2021-08-28 18:08:02 +02:00
parent cf6cd3c7a3
commit ae13c1d5ae
8 changed files with 48 additions and 27 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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()