Modifed Volume Slider Control Script [English]

  • #1, by afrlmeSunday, 11. November 2012, 18:15 9 years ago
    Cheers to einzelkaempfer for writing the original version of this script wink

    I have taken his script, translated it into English & have added various modifications & bug fixes to it!

    º active handle image on mouse button hold. º play sound/speech file accordingly depending on volume type when mouse button released (sliders) or on left click (-|+ buttons) º added various print messages to log -- comment them out if you don't want them. º quick fix to prevent slider from moving randomly (occurred when you moved mouse out of action area too fast) º translated the German comments into English as well as the variables etc & added my own comments too.

    the print statements will add messages along the lines of these to the messages.txt log file:

    17:08:08: volume script has been started!17:12:22: speech volume is now set at 90 - button17:12:23: speech volume is now set at 100 - button17:12:25: sound volume is now set at 100 - button17:12:26: sound volume is now set at 90 - button17:12:28: music Volume is now set at 50 - button17:12:29: music Volume is now set at 60 - button17:12:56: music volume is now set at 60 - slider17:12:59: sound volume is now set at 89 - slider17:13:02: speech volume is now set at 100 - slider
    1

    I made it so they added "button" or "slider" after the message so you know which print is from what section of the script. º button = the plus | minus volume buttons º slider = dragging the handle across the volume bar.

    quick note: if you have a config.ini file in the root folder it will automatically read/write the volume level data to & from it without you needing to add any script to do so!

    # MusicVolume|SoundVolume|SpeechVolume = int value {0-100}MusicVolume = 60SoundVolume = 89SpeechVolume = 100
    1

    ok so let's get on with explaining how the script works shall we smile

    -- * the main script * --

    the main script is a definition script meaning it loads the info inside when we launch the game/player [.exe] so all we need to do is call the functions from inside an object action using "execute a script" then calling a function like so: beginVolCon() which looked like "function beginVolCon()" inside of the script.

    you can also write custom scripts inside of the "execute a script" action which we'll show you a bit later.

    right on with deciphering the script:

    VolConMinPos is where you want the slider rail to begin (x pos = left hand side) VolConMaxPos is where you want the slider rail to end (x pos = right hand side) VolConWidth is the width of the image you are using for your slider rail handle/knob (in pixels) VolConYPos is the bottom edge of where each handle should be placed (y pos - the order is music[0], sound[1], speech[2])
    VolConMinPos = 1063VolConMaxPos = 1872VolConWidth = 18VolConYPos = {558; 477; 395} --music(0),sfx(1),voice(2) 
    1

    VolConTotal works out the actual width of the slider rail. Slider1 is the total divided by 100 to work out percentage [%] value! defaultHoldTime is for the mouse hold button actions play_sound gets the location to an action containing a play sound file action play_speech gets the location to an action containing a background speech file action

    VolConTotal = VolConMaxPos - VolConMinPosSlider1 = VolConTotal / 100defaultHoldTime = game:getInt(VGameHoldTime)play_sound = getObject('Scenes[Options_Menu].SceneActions[play_sound]')play_speech = getObject('Scenes[Options_Menu].SceneActions[play_speech]')
    1

    beginVolCon() is the initial loading function for this script & starts the handle animations & calculates the start positions of the handles. i = integer (0,1,2 which defines which volume type we are manipulating - see higher up the post) startAnimation blah blah - we required 2 startAnimations to be able to control an inactive & an active handle. VolVal is the variable we are storing the volume levels in for the current volume type. º the code if else statements are just checking that the volume levels aren't below 0% or over 100% VolConPos is calculating where the handles should be placed. setVolConPos sets the required handles where they need to be based on the calculations done in VolConPos Print adds message to log file declaring the volume script has been initialized!

    function beginVolCon()  for i = 0, 2 do    startAnimation("Animations[AniVolCon_" .. i .. "]")    startAnimation("Animations[AniVolCon_" .. i .. "x]")    local VolVal = getVolume(i)    if VolVal < 0 then      setVolume(i, 0)      VolVal = 0    elseif VolVal > 100 then      setVolume(i, 100)      VolVal = 100    end       local VolConPos = math.floor((VolVal * Slider1 + VolConMinPos) + 0.5) setVolConPos(i, VolConPos)  end print("volume script has been started!")end
    1

    I had to add 2 separate functions for declaring handle positions as it didn't like it when I tried declaring them using an if else statement as it didn't seem to be registering my condition variable for handle_is_active fast enough and thus defaulted to using the else part - so I added an extra function instead with a single animation which is used for the plus minus button part of the script.

    -- Set the positions of the slider handles (slider only)function setVolConPos(typ, xpos)  local VolAnim = getObject("ActiveAnimations[AniVolCon_" .. typ .. "]")  local VolAnim2 = getObject("ActiveAnimations[AniVolCon_" .. typ .. "x]")  VolAnim:setValue(VAnimationCurrentPosition, {x=xpos,y=VolConYPos[typ + 1]})  VolAnim2:setValue(VAnimationCurrentPosition, {x=xpos,y=VolConYPos[typ + 1]})end-- Set the positions of the slider handles (buttons only)function setVolConPos2(typ, xpos)  local VolAnim = getObject("ActiveAnimations[AniVolCon_" .. typ .. "]")  VolAnim:setValue(VAnimationCurrentPosition, {x=xpos,y=VolConYPos[typ + 1]})end
    1

    don't think I need to explain this one!

    -- Checks if mouse button is being held downfunction mousebtnHold(standard)  if standard == false then    game:setValue(VGameHoldTime, 1)  else    game:setValue(VGameHoldTime, defaultHoldTime)  endend
    1

    calculates where the required handle should be while mouse button is held down by using current cursor position. also checks to see if you've reached start or end of the rail to prevent you from dragging any further.

    -- The function which allows you to drag the slider handlefunction dragVolCon(typ)  local CursorPos = getCursorPos()  local VolConPos = math.floor(CursorPos.x - VolConWidth / 2 + 0.5)    if VolConPos < VolConMinPos then    VolConPos = VolConMinPos  elseif VolConPos > VolConMaxPos then    VolConPos = VolConMaxPos  end  setVolConPos(typ, VolConPos)  local VolVal = math.floor(((VolConPos - VolConMinPos) / Slider1) + 0.5)  setVolume(typ, VolVal)end
    1

    controls what clicking on the plus minus buttons will do. checks if current vol is over/under 100/0 % & if not it will add/subtract 10% each time you click a button the plus minus value is controlled by cond (condition: true|false - true = plus, false = minus) like so: "execute a script" > pluminClick(0, true) - this will increase the volume level for music by 10% if it's under 100% it also checks the typ (int value) to determine if it should play a sound/speech file or do nothing (in case of music) & determine what to print to the log file

    -- Plus_Minus Buttons raise or lower vol by 10%function pluminClick(typ, cond)  VolVal = getVolume(typ)  if cond == true then    VolVal = VolVal + 10    if VolVal > 100 then VolVal = 100 end  else    VolVal = VolVal - 10    if VolVal < 0 then VolVal = 0 end        end  setVolume(typ, VolVal)  local VolConPos = math.floor((VolVal * Slider1 + VolConMinPos) + 0.5)  setVolConPos2(typ, VolConPos) if typ == 1 then  startAction(play_sound)  print("sound volume is now set at " .. VolVal .. " - button") else if typ == 2 then  startAction(play_speech)  print("speech volume is now set at " .. VolVal .." - button") else  print("music Volume is now set at " .. VolVal .. " - button") end endend -- pluminClick()
    1

    -- * the editor stuff * --

    first of all we'll start with what's needed ...

    º 4 or 6 objects for minus/plus buttons (depending on whether you are planning on using audible speech) º 3 or 6 objects for inactive/active slider handles (depends on if you plan on adding active handle image)

    & that's it for the objects smile

    http://alternatingfrequencies.com/vs/vol_slider/objects_screenie.png

    quick note: we also have a few actions, conditions & values set on the "Options_Menu" itself which are used to control various things with the volume slider script!

    Actions: º At beginning of scene ** "execute a script" > beginVolCon() -- this loads the volume script (on my game menu I have this on the main_menu > options button instead) ** call action > "ani_reset" -- this is a call by other action where I added the menu reset actions - in this case it turns off the active handle animations - I tend to create global call by other action on my main character which I use to reset/control various in game settings & misc things! º "execute a script" > endVolCon() -- this would be used if we were closing the scene with the volume script! º call by other action > "play_sound" -- reflects play_sound found in main script which calls this action to play a sound. º call by other action > "play_speech" -- reflects play_speech found in main script which calls this action to play a speech file.

    Conditions: º sfx_is_on -- this determines if current vol type is sound & is used in mouse button release section. º voice_is_on -- this determines if current vol type is speech & is used in mouse button released section.

    Values: º which_volcon_is_active -- reflected in mouse button hold section to determine which vol type / slider is currently active

    onto what's inside the objects ...

    inside the plus/minus objects we only need one action which is: "left click" > "execute a script" > pluminClick(int, true|false)

    examples: º pluminClick(0, true) -- adds 10% to current music level if less than 100% º pluminClick(0, false) -- subtracts 10% from current music level if more than 0% º pluminClick(1, true) -- adds 10% to current sound level if less than 100% º pluminClick(1, false) -- subtracts 10% from current sound level if more than 0% º pluminClick(2, true) -- adds 10% to current speech level if less than 100% º pluminClick(2, false) -- subtracts 10% from current speech level if more than 0%

    inside the inactive slider object (in our case "Voice|Sfx|Music_Slider (options)" we have 3 actions. "called by other action" > ActVol_(int) -- called via main script º "execute a script" > dragVolCon(int) -- int determines which which handle is being dragged [int = 0,1 or 2] º misc > pause > 50ms -- this creates a slight pause before ... º Jump 2 action part(s) backwards -- this loops back to dragVolCon(int) - which gets current cursor position & amends handle to it!

    the active slider objects just contain an animation image which is set as same name as the one in the inactive slider object but with an "x" added to the name which is read inside of the main script file!

    quick note: animations need to be set on "infinite" or they will disappear after pause time / loop amount. also it's a good idea to set a high pause time like "20000" so we don't add unnecessary cpu usage by having it looping rapidly for no reason.

    http://alternatingfrequencies.com/vs/vol_slider/ani_settings.png

    the animation & called by other action names inside the objects have to reflect the names in the main script & vice versa!

    "Cursor enters object area" º set value > "which_volcon_is_active" = (int) -- determines which slider is currently active º "execute a script" > mousebtnHold > false -- not entirely sure (think it determines if button hold is allowed)

    "Cursor leaves object area" º set value > "which_volcon_is_active" = -1 -- resets to init value of which slider is active (none) º "execute a script" > mousebtnHold > true -- not entirely sure (still (think it determines if mouse button hold is allowed )) º call other action > "ani_reset" -- global action I created to reset menu items (in this case; hide active handles) º quit action > "ActVol_(int)" -- stops the current action from looping!

    -*-

    ok onto the final part; setting up the mouse button hold & release actions ...

    http://alternatingfrequencies.com/vs/vol_slider/button_hold_screen.png

    1. this section defines what happens on mouse button release (even though it's not that clear?) 2. this section defines what happens during mouse button hold

    just look at images for info!

    -* one *- (button released)

    http://alternatingfrequencies.com/vs/vol_slider/bhm_release.png

    http://alternatingfrequencies.com/vs/vol_slider/bhm_play_speech.png

    http://alternatingfrequencies.com/vs/vol_slider/bhm_play_sound.png

    http://alternatingfrequencies.com/vs/vol_slider/bhm_music.png

    -* two *- (button hold)

    http://alternatingfrequencies.com/vs/vol_slider/bhm_hold.png

    http://alternatingfrequencies.com/vs/vol_slider/bhm_start_action.png

    quick note: I tend to add my global reset & control actions to my main character like so:

    http://alternatingfrequencies.com/vs/vol_slider/char_reset_action.png

    in this case we are hiding the active slider handles!

    -- * links & so on * --

    you can get the ... main script from: here project file from: here (18.7mb approx)

    & here are some refs I used: getVolume, setVolume, startAction, StartAnimation & einzelkaempfer's slider script

    the music file I've used is a drum'n'bass track I made a couple of years back under the alias of "Sentinel" titled "Beau Caüchémar" which roughly translates to "Beautiful Nightmare" in English!

    the only spoken part of the track is taken from "Hard Candy" which is a movie I sampled from many times for a few of the tracks I made!

    Imperator

    6956 Posts


  • #2, by afrlmeSunday, 11. November 2012, 22:12 9 years ago
    ok it's finally done! smile

    Imperator

    6956 Posts

  • #3, by afrlmeSunday, 18. November 2012, 02:11 9 years ago
    ok just noticed a slight bug with my example demo in which I didn't prevent the script from trying to be called while holding down left mouse button anywhere on the screen!

    in other words it started the animations for the active handles on the far left of the sliders!

    to prevent this I have added a new condition; "allow_btn_hold" which is set to true whenever the cursor enters a sliders object area & false on mouse out.

    simple fix smile

    uploading new version now!

    Imperator

    6956 Posts

  • #4, by MilenaTuesday, 27. November 2012, 11:06 9 years ago
    I want to try your modifies slider, but the link to your project-file doesn`t work. Could you have please al look on it? Thank you smile.

    Newbie

    74 Posts

  • #5, by afrlmeTuesday, 27. November 2012, 13:45 9 years ago
    aye yeah ... sorry I copy/pasted the link from my LUA examples thread which also had an "L" attached to the end of the .rar making it a .rarl file whatever one of them may be!

    links are fixed on both here & the LUA thread now.

    safe smile

    Imperator

    6956 Posts

  • #6, by cr34m3Wednesday, 26. March 2014, 00:17 7 years ago
    Playing VS Forums Necromancer tonight... grin

    Is there a version of these images (and the example vis file) available somewhere on the VS site or elsewhere? I'm particularly interested in the images regarding the mouse hold/release but can't seem to find them.

    Newbie

    72 Posts

  • #7, by afrlmeWednesday, 26. March 2014, 01:06 7 years ago
    ignore this... I've written a new script which is a lot smaller. has optional slider & button control & also can display volume percentage on screen.

    I've not added it to the script index yet mind, but keep a look out for it.

    About the links... I was originally using my website server to host the files but I've not renewed it again this year.

    Imperator

    6956 Posts

  • #8, by bananeisafreeFriday, 13. May 2016, 16:58 5 years ago
    Hi ...
    A bit of digging trough an old post.
    I was really hoping to add a set of slider in the menu of our game (being all straship and stuff).
    I am ,of course, not even remotly able to create a script of my own right now (and I doubt I will ever be).
    I searched the web for slider script for visionaire and the three main thing I founded were this page. a link to an empty page on the "main" wiki, and a page on some kind of vestigial (alternate ?) wiki : https://www.visionaire-studio.net/wiki/article/modified-volu...

    I was wondering if the script that you made on that wiki is still valid (I'm deciphering it as of now), if something better that I missed came up with later visionaire Updates or if the slidder world altogether is to be forgotten :p

    Cheers

    Forum Fan

    120 Posts

  • #9, by afrlmeFriday, 13. May 2016, 18:01 5 years ago
    Yeah it should still be valid. I don't think I ever got around to typing up a tutorial for it though did I?

    Basically it's similar to the old script eizenkaempfler (I think that's his name, been a while since I typed it) wrote, except mine used less code & worked via a mouse event handler instead of a loop. Really it was just a tidy up rewrite. I also included the option to display the current percentage value for the active slider. But you could add that as a static thing at the side of a slider if you preferred or not have it display percentage at all.

    Imperator

    6956 Posts

  • #10, by bananeisafreeFriday, 13. May 2016, 21:36 5 years ago
    Great smile
    No there is an entry in the "real" wiki but it does not have anything, not even that script you made on the other one.
    The menu is "integrated" in the backround image as part of a somewhat cockpit so no percentage, just plain old little knobs and stuff!
    again thank you for the quick awnser !

    Forum Fan

    120 Posts

  • #11, by afrlmeFriday, 13. May 2016, 22:00 5 years ago
    Not sure why I never got round to sorting out that wiki page. Think it was late when I created it & I didn't have a ved example ready to attach to the page either, then I probably just forgot about it (I have terrible short term memory).

    Imperator

    6956 Posts

Write post