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;
}
and
int main(int argc, char** argv){
glutInit(&argc, argv);
....
//glutMouseFunc(processMouse);
glutMotionFunc(mousemotion);
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
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;
}
pressumably it would be used inside once of the mouse functions, using the x and y mouse coordinates as parameters.
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