From 615875bcfdb1de8285ae1df8d58a4a7f04fa29db Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Fri, 19 Apr 2019 23:21:32 +0200 Subject: [PATCH] examples/options: add audio widgets --- examples/menus/options/init.lua | 3 ++ examples/menus/options/widgets.lua | 59 ++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/examples/menus/options/init.lua b/examples/menus/options/init.lua index 2dd6b86..d0c935f 100644 --- a/examples/menus/options/init.lua +++ b/examples/menus/options/init.lua @@ -48,6 +48,9 @@ function OptionsMenu:new() self:setLanguageMenu() + Widgets.Audio(self, "audio", "sfx") + Widgets.Audio(self, "audio", "music") + Widgets.Exit(self, "main") self.menusystem:switchMenu("main") diff --git a/examples/menus/options/widgets.lua b/examples/menus/options/widgets.lua index bacb546..011b5b4 100644 --- a/examples/menus/options/widgets.lua +++ b/examples/menus/options/widgets.lua @@ -34,6 +34,7 @@ widgets.Resolution = DoubleTextWidget:extend() widgets.Lang = Widget.Text:extend() widgets.PlayerSubMenu = DoubleTextWidget:extend() widgets.Key = DoubleTextWidget:extend() +widgets.Audio = DoubleTextWidget:extend() -- BASIC WIDGETS -- Simple and reusables widgets @@ -249,5 +250,63 @@ function widgets.Key:receiveKey( key ) self:invalidateCanvas() end +-- AUDIO FUNCTIONS +-- Sounds/Music functions + +function widgets.Audio:new(scene, menu, audiotype) + self.scene = scene + self.audiotype = key + + + local widgetmenu = self.scene.menusystem.menus[menu] + local font = self.scene.assets.fonts["medium"] + self.audiotype = audiotype + local label = "" + if (self.audiotype == "sfx") then + label = core.lang:translate("options", "sfx") + else + label = core.lang:translate("options", "music") + end + local label2 = self:getLabel() + + widgets.Audio.super.new(self, widgetmenu, font, label, label2) + self.order = 0 +end + +function widgets.Audio:getLabel() + local value = self:getVolume() + string = utils.math.numberToString(value, 3) + local label = string .. "%" + + return label +end + +function widgets.Audio:getVolume() + if (self.audiotype == "sfx") then + return core.options.data.audio.sfx + else + return core.options.data.audio.music + end +end + +function widgets.Audio:setVolume(vol) + if (vol < 0) then vol = 100 end + + if (self.audiotype == "sfx") then + core.options.data.audio.sfx = vol + else + core.options.data.audio.music = vol + end + + self.label2 = self:getLabel() + self:invalidateCanvas() +end + +function widgets.Audio:action() + local value = self:getVolume() + self:setVolume(value - 20) + self.scene.assets.music:setVolume(core.options.data.audio.music / 100) + core.options:write() +end return widgets