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: 11527 Script, Game Maker 5 compatible
Filesize: 6338 byte Creative Commons License

Windows menu script

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

A script that generates a simple looking completely GML menu box.


 Windows menu script
//////////////// Windows menu script ////////////////////
//
// Copyright Simon Donkers 27-1-2005
// www.simondonkers.com - gmmentor@simondonkers.com
//
// place in the draw event
// argument0 = x coordinate
// argument1 = y coordinate
// argument2 = button discription
// argument3 = menu options seperate by |
// variable clicked containes the last clicked. item
// If clicked = 0 then no option has been clicked yet
// first option is 1, second 2...
// set 'treat unitialised variables as 0' on
// near the end stands a command named:
// "what to do when clicked"
// you can place an alarm event or so in there
// the alarm will be set when the button is clicked
// or you can use user defined events
//
/////////////////////////////////////////////////////
//if mouse over button
if mouse_x>argument0 and mouse_x<argument0 + 5 + string_width(argument2) and mouse_y>argument1 and mouse_y<argument1 + 5 + string_height(argument2) then
{
        past:= true;
}
else
{
        past:= false;
}
//draw button background
if past = false and global.open!= id then
{
        //unselected
        brush_color:= c_gray;
}
else
{
        //selected
        brush_color:= make_color(50,50,200);
}
//open or close menu
if past = true and mouse_button = mb_left and !pressed then
{
        mouse_clear(mb_left);
        if global.open = id then
        {
                global.open:= false;
        }
        else
        {
                global.open:= id;
        }
}
//draw top button
pen_color:= c_gray;
draw_rectangle(argument0,argument1,argument0 + 5 + string_width(argument2),argument1 + 5 + string_height(argument2));
font_color:= c_black;
draw_text(argument0 + 2,argument1 + 2,argument2);
position:= 0;
number:= 0;
selected:= 0;
//if menu is open
if global.open = id then
{
        //since last option is not read at dummy option
        argument3:= string_insert('|dummy',argument3,string_length(argument3) + 1);
        //add every menu option to the array text
        for(i = 0; i<string_length(argument3); i += 1)
        {
                if string_copy(argument3,i,1) = '|' then
                {
                        text[number]:= string_copy(argument3,position + 1,i-position-1);
                        position:= i;
                        number += 1;
                }
        }
        length:= 0;
        //check which is the length of the longest menu option
        for(i = 0; i<number; i += 1;)
        {
                if string_width(text[i])>length then
                {
                        length:= string_width(text[i]);
                }
        }
        //draw menu background
        brush_color:= c_gray;
        draw_rectangle(argument0,argument1 + 5 + string_height(argument2),argument0 + 5 + length,argument1 + 10 + string_height(argument2)*(number + 1));
        for(i = 0; i<number; i += 1;)
        {
                //if mouse over button
                if mouse_x>argument0 and mouse_x<argument0 + length and mouse_y>argument1 + string_height(argument2) + 7 + string_height(argument2)*i and mouse_y<argument1 + string_height(argument2) + 7 + string_height(argument2)*(i + 1) then
                {
                        selected:= i + 1;
                        brush_color:= make_color(50,50,200);
                        draw_rectangle(argument0,argument1 + string_height(argument2) + 7 + string_height(argument2)*i,3 + argument0 + length,argument1 + string_height(argument2) + 7 + string_height(argument2)*(i + 1) + 2);
                }
                //draw button text
                draw_text(argument0 + 2,argument1 + string_height(argument2) + 7 + string_height(argument2)*i,text[i]);
        }
}
//if option is selected
if mouse_button = mb_left and !pressed and selected!= 0 then
{
        clicked = selected;
        global.open:= 0;

        ///////////////////////////////////
        //what to do when clicked. Perhaps set an alarm or execute a script
        ///////////////////////////////////

}
//if clicked somewhere else in the screen then close
if !(past | (mouse_x>argument0 and mouse_x<argument0 + 5 + length and mouse_y>argument1 + 5 + string_height(argument2) and mouse_y<argument1 + 10 + string_height(argument2)*(number + 1)))and mouse_button = mb_left and !pressed and global.open = id then
{
        global.open:= 0
}
//checks whether mouse is pressed at the end so in the next step it is know
//whether the mouse is pressed or is still pressed
pressed:= (mouse_button = mb_left);

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

User comments

 DSS posted at 2005-02-11 20:55:15Quote
A very good menu script that can be incorparated in my game maker games. Thanks a million for this script!!
 Ken Masters posted at 2006-06-14 21:22:19Quote
hi i\'m looking for a great code(for room shaking) can u please give me a code or make me an example?

thank u so much.!
 _Player_ posted at 2007-05-20 02:37:51Quote
didn't seem to work for me. Something about global.open does not exist.
 kyle posted at 2007-06-12 19:03:30Quote
Game maker says that the draw_rectangle(argument0,argument1,argument0 + 5 + string_width(argument2),argument1 + 5 + string_height(argument2)); is wrong
what do i do to solve this
 Simon Donkers posted at 2007-06-13 19:27:35Quote
You solve that by reading the big red block with warning sign and bold text stating that this resource doesn't work in GM6/7. :)
 Dalortins posted at 2007-07-07 20:42:52Quote
I know how to make "draw_rectangle(argument0,argument1,argument0 + 5 + string_width(argument2),argument1 + 5 + string_height(argument2));" work with gm6/7. At the end of all draw_rectangle you will have a ";);" and in this cases it will be ";));". Well make that look like this ";),1);" or in the other cases make it look like this ",1);"
 Dalortins posted at 2007-07-07 20:43:54Quote
whats with the )
 chris posted at 2010-03-19 07:55:05Quote
___________________________________________
COMPILATION ERROR in Script: menu
Error in code at line 57:
draw_rectangle(;,1);argument0,argument1,argument0 + 5 + string_width(argument2),argument1 + 5 + string_height(argument2));
^
at position 17: Unexpected symbol in expression.

Reply

Name:
Site: