如何在C ++ glut中创建橡皮筋般的2D对象的拖动和移动 [英] How to create a rubber-esque drag and move of 2D object in c++ glut

查看:90
本文介绍了如何在C ++ glut中创建橡皮筋般的2D对象的拖动和移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个想要移动的物体.我希望它仅在我单击时才移动,然后将鼠标拖到某个位置,然后在释放时开始向它移动.因此,如果我在其中单击鼠标,则可以在按住鼠标键的同时将鼠标移动到任意位置,但是只有释放后它才能开始移动.

I have an object that I want to be able to move. I want it to move only if i click within in, then drag my mouse to a place, then upon release, it starts moving towards it. so if i click inside it, I can move my mouse anywhere as much as I want while holding mouse button, but only when I release does it start to move.

目前,最好的方法是让物体跟随(在顺时针方向后几秒钟,而不是在鼠标位置),只要按住鼠标按钮并移动它即可.只要我单击并移动,从哪里开始单击都没有关系,只要按住鼠标按钮,对象就会向其移动.任何其他尝试都会使对象保持静止/完全不移动.

Currently, the best ive been able to do is make the object follow (a couple seconds behind btw, NOT on the mouse position) as long as im holding the mouse button and moving it. it doesnt matter where I start the click from, as long as I click and move, the object moves towards it as long as im holding the mouse button. any other attempts leave the object staying still/not moving at all.

void mousemotion(int x, int yc){
globals.mouse_x = x;
globals.mouse_y = HEIGHT - yc;
}

int main(int argc, char** argv){
glutInit(&argc, argv);
....
//glutMouseFunc(processMouse);
glutMotionFunc(mousemotion);

是当前用于允许上述结果的仅有的鼠标功能/回调.我已经尝试过添加glutMouseFunc回调之类的事情,但是通过更改其中的state参数会产生不好的结果.例如:

are the only mouse functions/callbacks that are currently being used to allow the result above. I have tried things like adding a glutMouseFunc callback but from changing the state parameter in it produces bad results. E.G:

//glutMouseFunc callback 
void processMouse(int button, int state, int x, int yc){
    if ( state == GLUT_UP){
    globals.centre_x = globals.mouse_x;
    globals.centre_y = globals.mouse_y;
}

GLUT_DOWN不会更改主要行为,但是当对象运动时,只要单击一下,对象就会捕捉到要移动的位置. GLUT_UP就是这样,所以一旦我释放鼠标,该对象将立即捕捉到它要移动的位置.这些行为对于它们的行为方式是有意义的,但是我无法操纵它以我想要的方式工作.我还做了一个检查对象内部是否有点的函数,但我不知道它在哪里适用

GLUT_DOWN doesnt change the main behaviour, but when the object is in motion, and I just click once, the object will snap to the position it was going to. GLUT_UP just makes it so once I release the mouse, the object will immediately snap to the position it was going. These behaviours make sense as to they are behaving the way they are, but I cant manipulate it to work the way I want to. I also made a function to check if a point is inside the object but I dont know where its applicable

bool inside(int x, int y){
if(x >= globals.centre_x - 20
    && x <= globals.centre_x +20
    && y >= globals.centre_y - 20
    && y <= globals.centre_y+ 20){
    return true;
}
else
    return false;
}

使用x和y鼠标坐标作为参数,可以在一次鼠标功能中使用它.

pressumably it would be used inside once of the mouse functions, using the x and y mouse coordinates as parameters.

我在网上看到的所有拖放示例都涉及立即拖动对象,即,单击对象,并且在移动对象时,对象将遵循确切的x,y鼠标坐标,但是我只想让它移动鼠标移至该对象将开始移动.

all the drag and drop examples Ive seen online involve immediate dragging of the object, i.e. click on object and object follows the exact x,y mouse coordinates as you move it around, but I want to make it so only when I let go of the mouse will the object start to move.

任何帮助表示赞赏.让我知道我是否可以澄清任何事情.谢谢

Any Help appreciated. let me know if i can clarify anything. thanks

推荐答案

我不使用GLUT,但基于以下内容:

I do not use GLUT but Based on these:

  • GLUT docs
  • GLUT mouse button down

要检测鼠标事件类型,您需要执行以下操作:

To detect mouse event type You need to do something like this:

//glutMouseFunc callback 
int state0l=GLUT_UP; // last left mouse buttons state
int state0r=GLUT_UP; // last right mouse buttons state
void processMouse(int button, int state1, int x, int y)
    {

    if (button == GLUT_LEFT_BUTTON)
     {
     // decode and handle the mouse events by type
     if ((state0l == GLUT_UP  )&&(state1 == GLUT_DOWN)) // mouse down (click)
      {
      // here do your stuff
      }
     if ((state0l == GLUT_DOWN)&&(state1 == GLUT_DOWN)) // mouse move while clicked
      {
      // here do your stuff
      }
     if ((state0l == GLUT_DOWN)&&(state1 == GLUT_UP  )) // mouse up (release)
      {
      // here do your stuff
      }
     if ((state0l == GLUT_UP  )&&(state1 == GLUT_UP  )) // mouse move without buttons
      {
      // here do your stuff
      }
     // store actual buttons state for next time
     state0l = state1;
     }

    if (button == GLUT_RIGHT_BUTTON)
     {
     // decode and handle the mouse events by type
     if ((state0r == GLUT_UP  )&&(state1 == GLUT_DOWN)) // mouse down (click)
      {
      // here do your stuff
      }
     if ((state0r == GLUT_DOWN)&&(state1 == GLUT_DOWN)) // mouse move while clicked
      {
      // here do your stuff
      }
     if ((state0r == GLUT_DOWN)&&(state1 == GLUT_UP  )) // mouse up (release)
      {
      // here do your stuff
      }
     if ((state0r == GLUT_UP  )&&(state1 == GLUT_UP  )) // mouse move without buttons
      {
      // here do your stuff
      }
     // store actual buttons state for next time
     state0r = state1;
     }
    }

正如您所看到的,我只是检查按钮的最后状态和实际状态,以检测4种可能性,+/-与我之前给您的链接相同:

As you can see I just inspect last and actual state of the buttons to detect 4 possibilities +/- the same as in the link I gave you before:

当您检查以下方法时,我只有q0,q1而不是state0,state1:editor::mov,add_kruh,add_stvorec,...它们都使用相同的技术(但只有那些使用粗略的事件).

I just have q0,q1 instead of the state0,state1 when you inspect the methods like: editor::mov,add_kruh,add_stvorec,... they all use this same technique (but only those events they use of coarse).

这篇关于如何在C ++ glut中创建橡皮筋般的2D对象的拖动和移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆