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  [작성자] 2016년 5월 7일 오전 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  [작성자] 2015년 3월 17일 오후 4시 48분 
Hey Darzall, here is the license:
http://www.wtfpl.net/about/
Darzall 2015년 3월 17일 오후 4시 35분 
Any rules for using this code?
Black Blade 2014년 5월 6일 오후 1시 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 2014년 5월 5일 오후 12시 52분 
Thank you :D
Scorcher24  [작성자] 2014년 5월 5일 오후 12시 51분 
Scorcher24  [작성자] 2014년 5월 5일 오후 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 2014년 5월 5일 오후 12시 30분 
I got this compile error:
in script draw_tooltip line 26 pos 34: symbol ) expected
99 Beers 2014년 1월 5일 오후 8시 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 2013년 12월 21일 오후 6시 56분 
Thanks! Very useful. Keep them coming?