10.05.2015 gamemaker gamedev

GameMaker: Tooltips with GML

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 /*
    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);

Here is what you have to do to use this script:

  • Create a script in GameMakerStudio and name it whatever you want, I used draw_tooltip().
  • In your object, make a variable called “showToolTip”, initialize it in the create event to 0.
  • Add a Mouse Over Event, set “showToolTip” to 1.
  • Add a Mouse Leave Event, set “showToolTip” to 0.

Add a Draw GUI Event, create a new script action in this event and paste the following lines:

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

Modify the code to your liking/style and you are good to go.

The tooltip also shifts to the left or to the right if there is not enough space to display it on either side. I did not test multiple lines, but it should work if you add “#” as a line break in your text, which is a standard function of GameMaker Studio.
Enjoy the script!

Link to the author's twitter Link to the authors ko-fi page

comments

Characters: 0/1000

gravatar portrait

 Pinned by contact@tuxstash.de

Come join the discussion and write something nice. You will have to confirm your comment by mail, so make sure it is legit and not a throwaway. Only the name part of it will be displayed, so don't worry about spam. If it does not show up after confirming it, it may be considered spam, but I curate them manually, so don't worry. Please read the privacy statement for more.