Simon Donkers's Game Maker site siteSimon Donkers's Game Maker site
This resource is created for Game Maker 5 and isn't compatible with the latest version of Game Maker.
My apologies for the inconvinience.
Version: GM 5 Views: 11395 Script, Game Maker 5 compatible
Filesize: 10432 byte Creative Commons License

Slider bar

1/52/53/54/55/5 (20 votes)

A script for a windows slidebar. Set 'treat uninitialised variables as value 0' to true. Useful to use in for instance a volume bar


 Slider bar
//////////////// Slider bar ////////////////////
//
// Copyright Simon Donkers 27-1-2005
// www.simondonkers.com - gmmentor@simondonkers.com
//
// argument0 = x
// argument1 = y
// argument2 = minimal value
// argument3 = maximal value
// argument4 = 0 = vertical 1 = horizontal
// argument5 = length of bar
// place in the draw event
// value = the value initialise this variable your self!!!
// set 'treat uninitialised variables as value 0' to true
// example: 'name_of_script'(5,5,0,255,1,100);
//
/////////////////////////////////////////////////////
if argument5 = 0 then
{
        argument5 := argument3-argument2;
}
if argument4 = 0 then
{
        //verical scrollbar
        //object is selected and mouse button is pressed
        if global.selected = id and mouse_button = mb_left then
        {
                value := (argument5 + argument1-5-mouse_y)/(argument5-10)*(argument3-argument2) + argument2;
                if value<argument2 then {value := argument2;}
                if value>argument3 then {value := argument3;}
        }
        //object is selected and mouse button is no longer being pressed
        if global.selected = id and mouse_button!= mb_left then
        {
                global.selected := false;
        }
        pen_color := c_black;
        brush_color := c_gray;
        //x_box = the x coordinate of the plus and minus box
        x_box := argument0 + 30 + string_width(string(argument3));
        //width_box = the width and height off the plus and minus box
        width_box := 2.5 + string_height('1')/2;
        //draws the grey box where the scrollbar is in
        draw_rectangle(argument0,argument1,argument0 + 20,argument5 + argument1);
        //draws a line in the middle of the scrollbox
        draw_line(argument0 + 10,argument1 + 5,argument0 + 10,argument5-5 + argument1);
        //draws the actual scrollblock
        draw_rectangle(argument0 + 3,argument1 + 5 + (argument3-(value-argument2)-argument2)/(argument3-argument2)*(argument5-10)-2,argument0 + 17,argument1 + 5 + (argument3-(value-argument2)-argument2)/(argument3-argument2)*(argument5-10) + 2);
        //draws the plus box
        draw_rectangle(x_box,argument1,x_box + width_box,argument1 + width_box);
        //draws the minus block
        draw_rectangle(x_box,argument1 + width_box,x_box + width_box,argument1 + width_box*2);
        brush_color := c_white;
        //draws the box with the number in it.
        draw_rectangle(argument0 + 20,argument1,argument0 + 30 + string_width(string(argument3)),argument1 + string_height('1') + 5);
        font_color := c_black;
        //draws the number
        draw_text(argument0 + 25,argument1 + 2,round(value));
        if !(width_box)mod(2) then {pen_size := 2;}
        //draws the plus
        draw_line(x_box + width_box/2,argument1 + 2,x_box + width_box/2,argument1 + width_box-2);
        draw_line(x_box + 2,argument1 + width_box/2,x_box + width_box-2,argument1 + width_box/2);
        //draws the minus
        draw_line(x_box + 2,width_box + argument1 + width_box/2,x_box + width_box-2,width_box + argument1 + width_box/2);
        //if plus pressed
        if mouse_button = mb_left and mouse_x>x_box and mouse_x<x_box + width_box and mouse_y>argument1 and mouse_y<argument1 + width_box and value<argument3 then
        {
                value += 1;
        }
        //if minus pressed
        if mouse_button = mb_left and mouse_x>x_box and mouse_x<x_box + width_box and mouse_y>argument1 + width_box and mouse_y<argument1 + width_box*2 and value>argument2 then
        {
                value-= 1;
        }
        //if scrollbar pressed
        if mouse_button = mb_left and mouse_x>argument0 and mouse_x<argument0 + 20 and mouse_y>argument1 and mouse_y<argument1 + + 10 + argument3-argument2 then
        {
                global.selected = id;
                value := (argument5 + argument1-5-mouse_y)/(argument5-10)*(argument3-argument2) + argument2;
                if value<argument2 then {value := argument2;}
                if value>argument3 then {value := argument3;}
                value := round(value);
        }
}
else
{
        //horizontal scrollbar
        pen_color := c_black;
        brush_color := c_gray;
        //object is selected and mouse button is pressed
        if global.selected = id and mouse_button = mb_left then
        {
                value := (mouse_x-argument0-5)/(argument5-10)*(argument3-argument2) + argument2;
                value := round(value);
                if value<argument2 then {value := argument2;}
                if value>argument3 then {value := argument3;}
        }
        //object is selected and mouse button is no longer being pressed
        if global.selected = id and mouse_button!= mb_left then
        {
                global.selected := false;
        }
        //x_box = x coordinate plus and minus
        x_box := argument0 + 10 + string_width(string(argument3));
        //width_box = the width and height off the plus and minus box
        width_box := 2.5 + string_height('1')/2;
        //scrollbar background
        draw_rectangle(argument0,argument1,argument0 + argument5,argument1 + 20);
        //scrollbar line
        draw_line(argument0 + 5,argument1 + 10,argument0 + argument5-5,argument1 + 10);
        //draws the extual scrollbox
        draw_rectangle(5 + argument0 + (value-argument2)*(argument5-10)/(argument3-argument2)-2,argument1 + 3,5 + argument0 + (value-argument2)*(argument5-10)/(argument3-argument2) + 2,argument1 + 17);
        //draw plusbox
        draw_rectangle(x_box,argument1 + 20,x_box + width_box,argument1 + 20 + width_box);
        //draw minusbox
        draw_rectangle(x_box,argument1 + 20 + width_box,x_box + width_box,argument1 + 20 + 2*width_box);
        brush_color := c_white;
        draw_rectangle(argument0,argument1 + 20,x_box,argument1 + 20 + 2*width_box);
        font_color := c_black;
        draw_text(argument0 + 5,argument1 + 22,value);
        //draw plus
        draw_line(x_box + width_box/2,argument1 + 22,x_box + width_box/2,argument1 + 20 + width_box-2);
        draw_line(x_box + 2,argument1 + 20 + width_box/2,x_box + width_box-2,argument1 + 20 + width_box/2);
        //draw minus
        draw_line(x_box + 2,argument1 + 20 + width_box*1.5,x_box + width_box-2,argument1 + 20 + width_box*1.5);
        //if plus pressed
        if mouse_button = mb_left and value<argument3 and mouse_x>x_box and mouse_x<x_box + width_box and mouse_y>argument1 + 20 and mouse_y<argument1 + 20 + width_box then
        {
                value += 1;
        }
        //minus pressed
        if mouse_button = mb_left and value>argument2 and mouse_x>x_box and mouse_x<x_box + width_box and mouse_y>argument1 + 20 + width_box and mouse_y<argument1 + 20 + 2*width_box then
        {
                value-= 1;
        }
        //scrollbar pressed
        if mouse_button = mb_left and mouse_x>argument0 and mouse_x<<argument0 + 5 + argument3-argument2 and mouse_y>argument1 and mouse_y<argument1 + 20
        {
                global.selected = id;
                value := (mouse_x-argument0-5)/(argument5-10)*(argument3-argument2) + argument2;
                value := round(value);
                if value<argument2 then {value := argument2;}
                if value>argument3 then {value := argument3;}
        }
}

Tip: Have a look at the tutorial How to execute a script

User comments

 DSS posted at 2005-02-11 20:56:51Quote
Another very good game maker script for incorparating into game maker games. I have adapted it so that it will scroll some text or a page for you.
 JaMiE P posted at 2005-02-20 08:47:29Quote
Great Script! I'm using this for an upcoming music-mixing program.
 THAT'S ME BUSHER! posted at 2007-10-10 12:43:07Quote
hallo r ya der? im kidding nice script man!
 Urablahblah posted at 2007-11-14 18:22:39Quote
Yeah, it keeps telling me that there are not enough variables for the draw_rectangle command for every command. I just added ", 1" after all of them so it will draw the rectangles not filled.
 Simon Donkers posted at 2007-11-18 10:53:45Quote
Do you see the big red bar with the warning sign at the top of the page?

Try reading it :).
 Glen posted at 2008-11-08 19:55:27Quote
For newer versions you are required to add a true or false argument at the end of your draw_rectangle functions. The script doesn't work unless you do that.

Reply

Name:
Site: