GMS 2.3+ The best way to create many button on GUI when you can zoom and move your camera in the room

Hello everyone,
I did a little code to zoom in and out, and move the camera in my room, with the code bellow :

GML:
vx = camera_get_view_x(view_camera[0]);//retrieve the x position of the view for a given camera
vy = camera_get_view_y(view_camera[0]);
vxx = camera_get_view_width(view_camera[0])//retrieve the width (in pixels) of the given camera
vyy = camera_get_view_height(view_camera[0])

//this is change the zoom based on scolling
//if camera_get_view_width<room_width && vyy<room_height {
    zoom_level = clamp(zoom_level + (((mouse_wheel_down() - mouse_wheel_up())) * 0.1), 0.5, 2);
//}


//Set the rate of interpolation
var rate = 0.2;

//Get new sizes by interpolating current and target zoomed size
new_w = lerp(vxx, zoom_level * default_zoom_width, rate);
new_h = lerp(vyy, zoom_level * default_zoom_height, rate);

//Get the shift necessary to re-align the view.
var shift_x = camera_get_view_x(view_camera[0]) - (new_w - vxx) * 0.5;
var shift_y = camera_get_view_y(view_camera[0]) - (new_h - vyy) * 0.5;

///Horizontal scroll/////////////////////////////
if device_mouse_x_to_gui(0) < 100 && device_mouse_x_to_gui(0) > 0 {shift_x -= 10}
if device_mouse_x_to_gui(0) > (default_zoom_width-100) && device_mouse_x_to_gui(0) < room_width {shift_x += 10}

///Vertical scroll///////////////////////////////////////////
if device_mouse_y_to_gui(0) < 100 && device_mouse_y_to_gui(0) > 0 {shift_y -= 10}
if device_mouse_y_to_gui(0) > (default_zoom_height-100) && device_mouse_y_to_gui(0) <= room_height {shift_y += 10}

//Update
camera_set_view_pos(view_camera[0], shift_x, shift_y);//update the position of the camera view within the room
camera_set_view_size(view_camera[0],new_w,new_h)//update the size of the view camera within the room

//Clamp your view in the room
camera_set_view_pos(view_camera[0],
    clamp( camera_get_view_x(view_camera[0]), 0, room_width - camera_get_view_width(view_camera[0]) ),
    clamp( camera_get_view_y(view_camera[0]), 0, room_height - camera_get_view_height(view_camera[0]) )
);

The code works perfectly but now my button don't works (i used a simple object with mouse collision)
For you what is the best way to create a button on the GUI (a button you can click on it, a button which don't move)?
 

TheouAegis

Member
Draw the buttons in the GUI event and use device_mouse_x_to_gui(0) and device_mouse_y_to_gui(0) to see which sprite the mouse is over.
 
Ok i did this :

It works well

Create event
GML:
active = false;

sprite_get_xoffset(Sprite5);
sprite_get_yoffset(Sprite5);

sprite_width_ = sprite_get_width(Sprite5);
sprite_height_ = sprite_get_height(Sprite5);

x_start = 500
y_start = 500

x2 = x_start+sprite_width_;
y2 = y_start+sprite_height_;
Step event
Code:
if (mouse_check_button_pressed(mb_left)) {
    var mx = window_mouse_get_x(), my = window_mouse_get_y();
    if (mx >= x_start && my >= y_start && mx <= x2 && my <= y2){
        active = !active;}}
Draw gui event
Code:
if active {draw_sprite_ext(Sprite5, -1, x_start, y_start, 1, 1, 0, c_white, 1);}
else draw_sprite_ext(Sprite5, -1, x_start, y_start, 1, 1, 0, c_white, 0.25);
 
Top