Text Box Buttons

Y

Yonokid

Guest
So, I want to be able to hit all these buttons that I have within this one sprite.program.png
Sorry about all the spam in it, but is there a way to be able to hit each one these buttons with a different outcome within the sprite?
 

NightFrost

Member
Clicking them with a mouse, I assume? If you aren't wanting to draw the buttons as separate objects and attach actions to them... You need to know the corner coordinates of each button (take the sprite location and then add offset values) and compare each to mouse_x and mouse_y. If mouse coords are inside the button region, and mouse button is being pressed, then execute an action relevant to that button.
 
Y

Yonokid

Guest
Yes, Clicking them with a mouse. I don't want to draw separate objects because this box can be dragged and dropped.

(And yes I can't do this code on my own)
 

NightFrost

Member
Let's assume the origin point for the sprite is in the upper left corner. You can then use the instance's x and y coords to refer to that location. Next you measure the distance of upper left and lower right corner of each button from the sprite's upper left corner, and set them up as variables in create step. For example, for the first button you might set up something like this (numbers are for reference only):
Code:
// CREATE EVENT
Button1_X1 = 20; // upper left x
Button1_Y1 = 150; // upper left y
Button1_X2 = 60; // lower right x
Button1_Y2 = 170; // lower right y
You can now calculate the current button position by adding x and y, respectively, to those variables. To check whether current mouse position is within the button's rectangle, you can use point_in_rectangle which is a command for that exact purpose:
Code:
// STEP EVENT
if(mouse_check_button_pressed(mb_left){
    if(point_in_rectangle(mouse_x, mouse_y, Button1_X1 + x, Button1_Y1 + y, Button1_X2 + x, Button1_Y1 + y)){
        // Action for the button press
    }
   // Handle the rest of the button in same manner here
}
Alternatively, you could set up the buttons as separate objects and make them follow their parent window, but you don't gain much there expect not having to measure button positions.
 
Top