Legacy GM [SOLVED] point_in_rectangle not registering correctly with some GUI buttons

Hola!

Before you roll your eyes, I know there are a bunch of similar posts already but I've given them an eyeball and the suggestions in those (eg. device_mouse_x_to_gui, view_x) seem to have no impact whatsoever. I thought maybe drawing clickable buttons to the GUI when using a view had an effect, but disabling views doesn't make any difference. I also read elsewhere that the x2 and y2 parts of point_in_rectangle shouldn't(?) be higher values than x1 and y1, but that doesn't make any sense to me whatsoever.

So what's my actual problem?

The code for my first button works perfectly, whether I'm using views or not:

Code:
//First button!
if point_in_rectangle(window_mouse_get_x(),window_mouse_get_y(),32,32,64,64) && hovering = 0
{
    draw_set_alpha(0.8);
    hovering = 1;
};
else
{
    draw_set_alpha(0.5);
};

 draw_set_color(c_black);
 draw_rectangle(32,32,64,64,true);
 draw_sprite(spr_tower,image_index,48,49);
but the second button, though it draws the sprite and rectangle correctly (ie they match onscreen), doesn't register any mouse interaction at all:

Code:
//Second button!
        if point_in_rectangle(window_mouse_get_x(),window_mouse_get_y(),70,64,100,32) && hovering = 0
        {
            draw_set_alpha(0.8);
            hovering = 2;
        };
        else
        {
            draw_set_alpha(0.5);
        };
        
        draw_set_color(c_black);
        draw_rectangle(70,64,100,32,true);
        draw_sprite(spr_tower_green,image_index,85,49);
and here's the switch statements at the end of the code:

Code:
//Now we clean up our drawing
        draw_set_alpha(1); //Reset the alpha
        draw_set_color(c_white); //Reset the color
        draw_set_halign(fa_left); //Reset the horizontal align

//We check if the mouse has been pressed on any of the buttons
if mouse_check_button_pressed(mb_left) && hovering > 0
{
    switch hovering
    {
        case 1:
        if (global.resources >= blue_cost)
        {
        instance_create(mouse_x,mouse_y,obj_tower_1d);
        global.resources -= blue_cost;
        }
        break;
        
        case 2:
        if (global.resources >= green_cost)
        {
        instance_create(mouse_x,mouse_y,obj_tower_2d);
        global.resources -= green_cost;
        }
        break;
   }
}

hovering = 0;
I bet the answer's really bloody obvious, isn't it? :rolleyes:
 

rIKmAN

Member
Hola!

Before you roll your eyes, I know there are a bunch of similar posts already but I've given them an eyeball and the suggestions in those (eg. device_mouse_x_to_gui, view_x) seem to have no impact whatsoever. I thought maybe drawing clickable buttons to the GUI when using a view had an effect, but disabling views doesn't make any difference. I also read elsewhere that the x2 and y2 parts of point_in_rectangle shouldn't(?) be higher values than x1 and y1, but that doesn't make any sense to me whatsoever.

So what's my actual problem?

The code for my first button works perfectly, whether I'm using views or not:

Code:
//First button!
if point_in_rectangle(window_mouse_get_x(),window_mouse_get_y(),32,32,64,64) && hovering = 0
{
    draw_set_alpha(0.8);
    hovering = 1;
};
else
{
    draw_set_alpha(0.5);
};

 draw_set_color(c_black);
 draw_rectangle(32,32,64,64,true);
 draw_sprite(spr_tower,image_index,48,49);
but the second button, though it draws the sprite and rectangle correctly (ie they match onscreen), doesn't register any mouse interaction at all:

Code:
//Second button!
        if point_in_rectangle(window_mouse_get_x(),window_mouse_get_y(),70,64,100,32) && hovering = 0
        {
            draw_set_alpha(0.8);
            hovering = 2;
        };
        else
        {
            draw_set_alpha(0.5);
        };
      
        draw_set_color(c_black);
        draw_rectangle(70,64,100,32,true);
        draw_sprite(spr_tower_green,image_index,85,49);
and here's the switch statements at the end of the code:

Code:
//Now we clean up our drawing
        draw_set_alpha(1); //Reset the alpha
        draw_set_color(c_white); //Reset the color
        draw_set_halign(fa_left); //Reset the horizontal align

//We check if the mouse has been pressed on any of the buttons
if mouse_check_button_pressed(mb_left) && hovering > 0
{
    switch hovering
    {
        case 1:
        if (global.resources >= blue_cost)
        {
        instance_create(mouse_x,mouse_y,obj_tower_1d);
        global.resources -= blue_cost;
        }
        break;
      
        case 2:
        if (global.resources >= green_cost)
        {
        instance_create(mouse_x,mouse_y,obj_tower_2d);
        global.resources -= green_cost;
        }
        break;
   }
}

hovering = 0;
I bet the answer's really bloody obvious, isn't it? :rolleyes:
The second button has x1,y1,x2,y2 set as 70,64,100,32.

The manual says x1,y1,x2,y2 should be the left, top, right and bottom of the rectangle, so the bottom of your rectangle is above the top which might be causing the issue.
 
Yeah, that's it. I could have sworn I'd changed the coordinates around to test exactly this, but evidently not. Buuuhhhhhh :eek::rolleyes:

Cheers, rlKmAN!
 
Top