GameMaker: Studio

GameMaker: Studio

Оцінок: 52
Tooltips in GML
Автор: Scorcher24
This Guide shows you how to make a tooltip in GML, which can be displayed when you move your mouse over an object.
   
Нагородити
До улюбленого
В улюблених
Прибрати
Content
/* argument0 = text argument1 = font argument2 = text color argument3 = tooltip color argument3 = 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 "showToolTip" to 1.
Add Mouse Leave Event, set "showToolTip" to 0.
Add a Draw GUI Event, create a new script and paste this:

if ( showToolTip == 1 ){ draw_tooltip("I am a Tooltip.", fntArial, c_white, c_black, 0.8); }
You need to modify it to your liking though.

The code in action in a game:



The tooltip 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 :)
Коментарів: 17
Scorcher24  [автор] 7 трав. 2016 о 4:26 
Just switch the condition around, Banana Snow. That is all you need to do. But you won't learn anything if I just tell you everything. So try it out yourself and learn programming.

What you need to do is: Ask yourself why it defaults to the left and then change the condition of the if statement accordingly. It is really easy to do.
Scorcher24  [автор] 17 берез. 2015 о 16:48 
Hey Darzall, here is the license:
http://www.wtfpl.net/about/
Darzall 17 берез. 2015 о 16:35 
Any rules for using this code?
Black Blade 6 трав. 2014 о 13:06 
Nice guide

And man it takes time for Valve to do any thing.. you will have to wait for them to hopefully fix it :/
Engi 5 трав. 2014 о 12:52 
Thank you :D
Scorcher24  [автор] 5 трав. 2014 о 12:51 
Scorcher24  [автор] 5 трав. 2014 о 12:49 
@TheEngiGuy Yes, Steam is swallowing some of the symbols in the code. I reported this multiple times to Steam, but no change so far. Anyway, it is not really meant for copy pasta anyway :D. But here is a working version of it: http://www.reddit.com/r/gamemaker/comments/2002d4/what_are_some_scripts_you_try_to_keep_in_your/cfyqowh
Engi 5 трав. 2014 о 12:30 
I got this compile error:
in script draw_tooltip line 26 pos 34: symbol ) expected
99 Beers 5 січ. 2014 о 20:08 
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);

----
Getting a couple of errors here. First error line 26. If I fex that with a '+' next error goes to the draw rectangle line. Messed around for 10-15m but still too new to GML to succeed.
vgreeson 21 груд. 2013 о 18:56 
Thanks! Very useful. Keep them coming?