Jivvy
Jivvy

Reputation: 41

Limit drag in gamemaker

Hello I am trying to limit how much a user can drag within Gamemaker.

I have created two views within a room. The first view is the full room width and size 1250x768 The second view is the area which i would like the user to be able to zoom into and drag which is 955x465. x and y position is 40 by 170.

Currently i have set up the zoom for the second view which is:

vmx=(mouse_x-X)-omx;
omx=(mouse_x-X);
vmy=(mouse_y-Y)-omy;
omy=(mouse_y-Y);

if mouse_wheel_up() && view_wview[1] > 600 {
    center_of_space_x=view_xview[1]+view_wview[1]/2;
    center_of_space_y=view_yview[1]+view_hview[1]/2;
    view_wview[1]-=view_wview[1]*0.15;
    view_hview[1]-=view_hview[1]*0.15;
    view_xview[1]=center_of_space_x-view_wview[1]/2;
    view_yview[1]=center_of_space_y-view_hview[1]/2;


}
if mouse_wheel_down(){
    view_xview[1] = 40;
    view_yview[1] = 170;
    view_wview[1] = 955;
    view_hview[1] = 465;}

Below is the code for the drag:

if (mouse_check_button_pressed(mb_left)) {
    drag_x = mouse_x
    drag_y = mouse_y
}
// update:
if (mouse_check_button(mb_left)) && view_wview[1] < 700 {
    // actual dragging logic:
    view_xview[1] = drag_x - (mouse_x - view_xview[1])
    view_yview[1] = drag_y - (mouse_y - view_yview[1])
    // make sure view doesn't go outside the room:
    view_xview[1] = max(0, min(view_xview[1], room_width - view_wport[1]))
    view_yview[1] = max(0, min(view_yview[1], room_height - view_hview[1]))

So the limit works for the view to not leave the room but i want it not to leave the specific view which has been set up.

Please help

I have modified my code to use a clamp function which works but it is not a clean solution:

view_xview[1] = clamp(view_xview[1],40,400);
view_yview[1] = clamp(view_yview[1],170,500);

The user has to be fully zoomed in to have the view restricted. If he is not then they can still see other areas of the room :(

Upvotes: 1

Views: 121

Answers (1)

lozzajp
lozzajp

Reputation: 1016

EDIT: I may have misunderstood your question, I will leave the below code for reference if it helps you solve the problem a bit differently.

I would probably check that mouse_x and mouse_y are not outside the view you want.

if (mouse_x > view_limit_x || mouse_y > view_limit_y)
{
  return; // Or continue or break however your language of choice goes.
}

If you cannot do a return statement, wrapping your code in a reverse logic of above should work fine.

Old Post:

I am not 100% familiar with game maker. I would implement something like this by skipping the drag logic every x frames. Say you are updating at 60fps, and you want the drag to be 6 times slower, you just need to only update the logic 10 x per frame.

const int framesToSkip = 6;
int frameCount = 0;

update()
{

  // Will only call when framecount = 0, 6, 12, 18 etc.
  if (frameCount % framesToSkip == 0)
    {
      // Drag Logic.
    }


  frameCount++;
}

This way drag logic only occurs 1/6 the rate as before. You could also change this around to happen the first x amount of frames and then skip for the rest (but this may appear a bit choppy).

Upvotes: 0

Related Questions