• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Windows [SOLVED] Dynamic clickable area

Hello,
I have a button that has variable caption (basically is a text inside a rectangle).
I'll post the code in order to make you understand better than words:

Object Button
Create object
GML:
txt = "Hello there"
c = c_ltgray
Step event
GML:
if point_in_rectangle(mouse_x,mouse_y,x,y,x+string_width(txt)+8,y+24) {
c = c_white;
window_set_cursor(cr_handpoint);
} else { c = c_ltgray; window_set_cursor(cr_default); }
Draw event
GML:
draw_set_font(Font_Button)
draw_rectangle_color(x,y,x+string_width(txt)+8,y+24,c,c,c,c,1)
draw_text_color(x+4,y+2,string(txt),c,c,c,c,1)
The fact is: the rectangle around the text is perfectly drawn, while the clickable area is not working properly!
The last part of the button (on the right side) is not full clickable (and doesn't change color), it's like the text is smaller than how it appears on screen (even if the rectangle is correct).

How do I solve this?

An example screen:
1606995543001.png

PS: the more the text is long, the more the unclickable gap is big
PPS: the font I use is 16pt, if I use a 14pt font the problem doesn't sussist (while if I use a 12pt font I have the opposite problem)
 
Last edited:

SoapSud39

Member
In your step event, before you do point_in_rectangle(), write draw_set_font(Font_Button). The string_width() function is based off the current font. Unless you don't reset your font somewhere, in which case the problem would be something else.
 
In your step event, before you do point_in_rectangle(), write draw_set_font(Font_Button). The string_width() function is based off the current font. Unless you don't reset your font somewhere, in which case the problem would be something else.
Omg, it works :O
I don't get how didn't I manage this haha Well, thank you!

I use some other fonts somewhere else but it seems like it's not causing issues.
I guess it's solved, thank you!
 

FoxyOfJungle

Kazan Games
Maybe you better use point_in_rectangle() in the Draw Event, it won't make much difference in performance and it will be better in general, even to organize the code:

GML:
draw_set_font(Font_Button);

var c = c_white;
if point_in_rectangle(mouse_x,mouse_y,x,y,x+string_width(txt)+8,y+24)
{
    c = c_white;
    window_set_cursor(cr_handpoint);
}
else
{
    c = c_ltgray;
    window_set_cursor(cr_default);
}

draw_rectangle_color(x,y,x+string_width(txt)+8,y+24,c,c,c,c,1);
draw_text_color(x+4,y+2,string(txt),c,c,c,c,1);
 
Last edited:
Maybe you better use point_in_rectangle() in the Draw Event, it won't make much difference in performance and it will be better in general, even to organize the code:

GML:
draw_set_font(Font_Button);
if point_in_rectangle(mouse_x,mouse_y,x,y,x+string_width(txt)+8,y+24){
c = c_white;
window_set_cursor(cr_handpoint);
} else { c = c_ltgray; window_set_cursor(cr_default); }

draw_rectangle_color(x,y,x+string_width(txt)+8,y+24,c,c,c,c,1);
draw_text_color(x+4,y+2,string(txt),c,c,c,c,1);
Thank you for the reply even if it's solved.
This is a good advice, I'll do it!
 
Top