examples/options: add audio widgets

This commit is contained in:
Kazhnuz 2019-04-19 23:21:32 +02:00
parent dd0234a624
commit 615875bcfd
2 changed files with 62 additions and 0 deletions

View file

@ -48,6 +48,9 @@ function OptionsMenu:new()
self:setLanguageMenu() self:setLanguageMenu()
Widgets.Audio(self, "audio", "sfx")
Widgets.Audio(self, "audio", "music")
Widgets.Exit(self, "main") Widgets.Exit(self, "main")
self.menusystem:switchMenu("main") self.menusystem:switchMenu("main")

View file

@ -34,6 +34,7 @@ widgets.Resolution = DoubleTextWidget:extend()
widgets.Lang = Widget.Text:extend() widgets.Lang = Widget.Text:extend()
widgets.PlayerSubMenu = DoubleTextWidget:extend() widgets.PlayerSubMenu = DoubleTextWidget:extend()
widgets.Key = DoubleTextWidget:extend() widgets.Key = DoubleTextWidget:extend()
widgets.Audio = DoubleTextWidget:extend()
-- BASIC WIDGETS -- BASIC WIDGETS
-- Simple and reusables widgets -- Simple and reusables widgets
@ -249,5 +250,63 @@ function widgets.Key:receiveKey( key )
self:invalidateCanvas() self:invalidateCanvas()
end 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 return widgets