GML [solved] -1,0,+1 value slider bar

jf_knight

Member
I'm following "SlasherXGames's" tutorial where his example shows the bar with values 0 to 100.


I would like to make a similar slider bar for my projects' global brightness setting that registers the values -1 on the far left, 0 at the very center, and +1 at the far right. (note: this bar is incremental, as there are more that just the three variables stated. It slides -1, -.99, -.98...etc)

I've reduced that there should probably be some editing in either the slide handle's step event:

Code:
//if the mouse button is not clicked
if (!mouse_check_button(mb_left))
{
    grab = false;//slider is not clicked therefore not grabbed
}

if (grab == false) && (clicked == false)//if this object is not being grabbed
{
    exit;
}
//otherwise change its coordinates
else
{
    //if we slide within the constraints, update the sliders coordinate
    if ((mouse_x + xx) > leftLimit) && ((mouse_x + xx < rightLimit))
    {
       
        x = mouse_x + xx;
    }
    else if ((mouse_x + xx) < leftLimit)//set at left limit if user wants to slide it too far left
    {
        x = leftLimit;
    }
    else if ((mouse_x + xx) > rightLimit)//set at right limit if user wants to slide it too far right
    {
        x = rightLimit;
    }
}

//work out the percentage value
percentage = round(((x-leftLimit)/(rightLimit-leftLimit))*100);
clicked = false;

//calculate global.brightness

global.changeBrightness = percentage/100;

Or the bar itself's create event:
Code:
can_click = true;

rightLimit = x+(sprite_get_width(spr_slider_bar)/2)-30;
leftLimit = x-(sprite_get_width(spr_slider_bar)/2)+30;

//create the slider for this bar and assign it some values
a = instance_create(x, y, obj_brightness_slider_handle);
a.image_xscale = 1;
a.image_yscale = 1;
a.barLength = sprite_get_width(spr_slider_handle);
a.rightLimit = rightLimit;
a.leftLimit = leftLimit;
a.percentage = global.changeBrightness * 100;
//its current x value using the above percentage value
a.x = a.leftLimit + ((a.percentage/100) * (a.rightLimit-a.leftLimit));
a.clicked = false;

What variables can I change to achieve this?
 

obscene

Member
Easiest way might be to just take this line and extract your value...

percentage = round(((x-leftLimit)/(rightLimit-leftLimit))*100);

That returns 0 to 100.

Divide it by 50 and it will give you 0 to 2.

Subtract 1 and it will give you -1 to 1.
 

jf_knight

Member
Easiest way might be to just take this line and extract your value...

percentage = round(((x-leftLimit)/(rightLimit-leftLimit))*100);

That returns 0 to 100.

Divide it by 50 and it will give you 0 to 2.

Subtract 1 and it will give you -1 to 1.

So I now have percentage = ((((x-leftLimit)/(rightLimit-leftLimit))*100)/50)-1;

The correct values are showing up, but When I move the bar to -1, I have the code...

Code:
ini_open("game_settings.ini");
ini_write_real("settings","brightness",obj_brightness_slider_handle.percentage/100);
ini_close();
...to run so it saves as soon as I leave the room with the sliders.
When I return, the bar is no longer where I left it.
I leave it at -1, and return to it being -100 with the bar completely outside the slider (though still saved at the -1 global brightness level.
Finally, I click on the bar and it returns to -1.
 

obscene

Member
No no. don't edit that line, you want percentage to keep it's value from 0 to 100 so it will behave normally. Just calculate your secondary value from while leaving the original.

When you load in your value you'll also have to reverse the math in order to calculate the slider's position to 0 to 100 from -1 to 1.

The alternative of course is to hardcode that slider to work from -1 to 1 but if you use that slider for other settings with varying ranges you might find it easier to just find the conversion for each setting.
 

jf_knight

Member
Ok. I think I have the working solution.
first I accessed the function for the brightness shader and included all the arithmetic in there

Code:
position_bri = (   ((((global.changeBrightness*100)- 100)/50))   *  .1 )

finally, I changed the text output to

Code:
//draw_text(1290, y, string((((percentage) - 100)/50)+1));
to display the correct parameters.

It was that simple :p
 
Top