Jivvy
Jivvy

Reputation: 41

Gamemaker Zooming and Dragging

I am currently using gamemaker to create a floorplan system. I am able to zoom into a room, drag and zoom out but how can i limit how much i can zoom in and out? The size of the room is 1024 by 768 px. I want to be able to zoom out to the way it originally looks when you first enter the room.

This is my code currently which i have placed in the script:

X=view_xview[0];
Y=view_yview[0];
if mouse_check_button(mb_left){
global.DRAG=true;
window_set_cursor(cr_drag);
view_xview-=vmx;
view_yview-=vmy;
}

/*else{
if !keyboard_check(vk_space){
    global.DRAG=false
}
window_set_cursor(cr_default);
}
*/
vmx=(mouse_x-X)-omx;
omx=(mouse_x-X);
vmy=(mouse_y-Y)-omy;
omy=(mouse_y-Y);

if mouse_wheel_up(){
center_of_space_x=view_xview+view_wview/2;
center_of_space_y=view_yview+view_hview/2;
view_wview-=view_wview*0.15;
view_hview-=view_hview*0.15;
view_xview=center_of_space_x-view_wview/2;
view_yview=center_of_space_y-view_hview/2;

}
if mouse_wheel_down(){
center_of_space_x=view_xview+view_wview/2;
center_of_space_y=view_yview+view_hview/2;
view_wview+=view_wview*0.15;
view_hview+=view_hview*0.15;
view_xview=center_of_space_x-view_wview/2;
view_yview=center_of_space_y-view_hview/2;
}

Upvotes: 0

Views: 812

Answers (2)

Jamie Ford
Jamie Ford

Reputation: 1

simply use the clamp function to limit the view_wview and view_hview

var maxZoomIn = 0.2; //500% zoom in limit
var maxZoomOut = 1; //100% zoom out limit
if mouse_wheel_up(){
    center_of_space_x=view_xview+view_wview/2;
    center_of_space_y=view_yview+view_hview/2;
    view_wview = clamp(view_wview - view_wview * 0.15, maxZoomIn*room_width, maxZoomOut*room_width)
    view_hview = clamp(view_hview - view_hview * 0.15, maxZoomIn*room_height, maxZoomOut*room_height)
    view_xview=center_of_space_x-view_wview/2;
    view_yview=center_of_space_y-view_hview/2;
}

and similar for mouse_wheel_down.

Upvotes: 0

Duphus
Duphus

Reputation: 201

To do this is fairly simple, using the clamp function. A modified version of your code would look like this:

view_wview = clamp(view_wview * 0.15, min_size, 1024)
view_hview = clamp(view_hview * 0.15, min_size, 768)

Upvotes: 0

Related Questions