GameMaker: Studio
Find, play, and make games easily
Discover, rate, and download the best player-created games made in GameMaker: Studio for free. Or try making your own and share with the community. Click here to learn more.
Scorcher24 5 Nov 10, 2012 @ 10:00am
How to make a tooltip
Wrote this little script today and thought I share:

/*
argument0 = text
argument1 = font
argument2 = text color
argument3 = tooltip color
argument3 = tooltip alpha
*/
var text_width;
var text_height;
var old_alpha;
var old_color;

// Set alpha
old_alpha = draw_get_alpha();
draw_set_alpha(argument4);

// Set font
draw_set_font(argument1);

// Set Aligns
draw_set_valign(fa_bottom);

text_width = 5 + string_width(argument0);
text_height = 5 + string_height(argument0);

if ( ( ( mouse_x - room_width + text_width) <= ( room_width - 5) ) && ( mouse_x - text_width ) < 0 )
{
// Draw rect around the text
draw_roundrect_color( mouse_x, mouse_y - text_height, mouse_x + text_width, mouse_y + 5, argument3, argument3, false);

// Set color
old_color = draw_get_color();
draw_set_color(argument2);

// Draw the text inside the rect
draw_text( mouse_x, mouse_y - 2, argument0);

// Reset old color
draw_set_color(old_color);
}
else
{
// Set alignment to right
draw_set_halign(fa_right);

// Draw rect around the text
draw_roundrect_color( mouse_x, mouse_y - text_height, mouse_x - text_width, mouse_y + 5, argument3, argument3, false);

// Set color
old_color = draw_get_color();
draw_set_color(argument2);

// Draw the text inside the rect
draw_text( mouse_x, mouse_y - 2, argument0);

// Reset old color
draw_set_color(old_color);

// Reset alignment to left
draw_set_halign(fa_left);

}

// Reset alignment
draw_set_valign(fa_middle);


// Reset alpha
draw_set_alpha(old_alpha);

Save it as a script and name it draw_tooltip.
In your object, make a variable called "showToolTip", initialize it in the create event to 0.
Add Mouse Over Event, set the variable to 1.
Add Mouse Leave Event, set the variable to 0.
Add Draw GUI Event, enter a script there:

if ( showToolTip == 1 ){
draw_tooltip("I am a Tooltip.", fntArial, c_white, c_black, 0.8);
}

The code in action in my game:
http://i.imgur.com/fs659.jpg

The tooltipo also shifts to the left or to the right if there is not enough space to display it.
Did not test multiline, but it SHOULD work if you add the "#" in your text.

Thats basically it.
Have fun :)
Last edited by Scorcher24; Nov 10, 2012 @ 10:04am
Showing 1-1 of 1 comments
< >
Scott 1 Nov 10, 2012 @ 10:48am 
Nice work
Showing 1-1 of 1 comments
< >
Per page: 15 30 50