Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Sliding option handler [v1] (13/08/2018)
- Written by AFRLme [Lee Clarke]
- -- + --
- afrlme@outlook.com | https://de.tail.studio
- -- + --
- This script was created as freelance work for (game name) [developer name]. In game credit is non-negotiable.
- You are free to: ¹ use it in your game(s). ² modify the script.
- Do not remove - or edit - this comment block.
- --]]
- -- * local vars for storing data * --
- local rail, anim, width, perc, pos, loop
- -- * table for storing objects, interaction polygons, begin & end slider rail positions * --
- sliders = {
- { obj = Objects["slider_music"].ObjectPolygon, b = 64, e = 864 },
- { obj = Objects["slider_sound"].ObjectPolygon, b = 64, e = 864 }
- }
- -- * sound tables for volume slider * --
- vol_sndz = { sfx = "vispath:Sounds/Character/Yeah.ogg" }
- function slider_sound()
- if rail == 2 then startSound(vol_sndz["sfx"], {flags = 1, volume = getVolume(eSoundVolume)}) end -- play sfx on sound volume change or lose focus
- loop = nil; anim = nil; rail = nil -- reset some variables
- Conditions["options_modified"].ConditionValue = true -- set options_modified condition to true to signify an option has been changed (will be used to determine if config.ini should be updated)
- end
- -- * function that handles mouse events * --
- function onMouseEvent(typ, pos)
- if game.CurrentScene:getName() == "options_menu" then -- force it to only register events for settings scene
- if typ == eEvtMouseLeftButtonHolding then
- for i = 1, #sliders do
- if isPointInsidePolygon(getCursorPos(), sliders[i].obj) then rail = i; if loop == nil then registerEventHandler("mainLoop", "updateSlider"); loop = true end end
- end
- elseif typ == eEvtMouseLeftButtonHold then
- slider_sound(); unregisterEventHandler("mainLoop", "updateSlider")
- end
- end
- end
- function updateSlider()
- if rail ~= nil then
- pos = getCursorPos().x; if pos sliders[rail].e then pos = sliders[rail].e end
- perc = math.floor( (getCursorPos().x - sliders[rail].b) / (sliders[rail].e - sliders[rail].b) * 100 ) -- convert mouse position on rail to percentage value
- if perc 100 then perc = 100 end -- fallback in case percentage lower than 0 or higher than 100
- if not isPointInsidePolygon(getCursorPos(), sliders[rail].obj) then slider_sound(); unregisterEventHandler("mainLoop", "updateSlider") end
- if rail == 1 then
- setVolume(eMusicVolume, perc) -- update music volume
- elseif rail == 2 then
- setVolume(eSoundVolume, perc) -- update sound volume
- end
- anim = ActiveAnimations["vol_"..rail] -- get animation current position
- anim.AnimationCurrentPosition = {x = (pos - 23) , y = anim.AnimationCurrentPosition.y} -- update rail handle position
- if anim.AnimationCurrentPosition.x sliders[rail].e then anim.AnimationCurrentPosition = {x = (sliders[rail].e - 23), y = anim.AnimationCurrentPosition.y} end
- end
- end
- registerEventHandler("mouseEvent", "onMouseEvent") -- creates an event listener & assigns it to a function "onMouseEvent" to handle mouse events
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement