我一直在寻找一个简单程序的教程或示例代码 - 单击对象(例如2d矩形)然后当你按住并移动鼠标时,对象跟随鼠标,然后在鼠标释放时对象保持在新的位置.换句话说,我想了解如何使用鼠标事件拖放对象.
任何人都可以帮助我指出与此问题有关的任何有用信息来源的正确方向吗?
感谢到目前为止的所有回复.
我已经知道如何做到这一点,所以我将继续回答我自己的问题.
我使用GLUT作为鼠标处理程序:
单击鼠标并移动(glutMotionFunc)时,将调用拖动功能.
在拖动功能中,鼠标坐标(x,y)在转换为窗口坐标时转换为Points结构.
如果鼠标位于正方形内,则通过更改其坐标并重新显示来拖动方块.
我仍然是OpenGL和C++的新手,所以我为凌乱的编码道歉.我这样做有点沮丧,因为重绘的方块使得它看起来像是光标卡在中心.出于学习目的,我欢迎这个问题的替代解决方案和对我的代码的批评.
CODE(包含过剩和使用命名空间std):
// points structure made of two coordinates; x and y struct Points { float x,y; // initializor Points() { x = 0.0; y = 0.0; } // constructor Points(float _x, float _y) : x(_x), y(_y) {} }; // square made of 4 points class Square { public: Points pts[4]; // square structure Square(); // initialize constructor void draw(Square *sqr); // draw square Points mouse(int x, int y); // get mouse coordintaes Square* drag(Square *sqr, Points *mouse); // change points of sqr }; // square constructor Square::Square() { pts[0] = Points(0.2,0.2); pts[1] = Points(0.4,0.2); pts[2] = Points(0.4,0.4); pts[3] = Points(0.2,0.4); }; // draw function void Square::draw(Square *sqr) { // draw square fill int i; glColor3f(0.2, 0.2, 0.2); glBegin(GL_QUADS); for (i = 0; i < 4; ++i) { glVertex2f(sqr->pts[i].x, sqr->pts[i].y); } glEnd(); // draw square points i = 0; glColor3f(1.0, 1.0, 1.0); glBegin(GL_POINTS); for (i = 0; i < 4; ++i) { glVertex2f(sqr->pts[i].x, sqr->pts[i].y); } glEnd(); } // mouse function Points Square::mouse(int x, int y) { int windowWidth = 400, windowHeight = 400; return Points(float(x)/windowWidth, 1.0 - float(y)/windowHeight); } // drag function Square* Square::drag(Square *sqr, Points *mouse) { sqr->pts[0].x = mouse->x - 0.1; sqr->pts[0].y = mouse->y - 0.1; sqr->pts[1].x = mouse->x + 0.1; sqr->pts[1].y = mouse->y - 0.1; sqr->pts[3].x = mouse->x - 0.1; sqr->pts[3].y = mouse->y + 0.1; sqr->pts[2].x = mouse->x + 0.1; sqr->pts[2].y = mouse->y + 0.1; return sqr; } // GLOBAL // create square object Square* sqr = new Square; // display at start void display() { glClear(GL_COLOR_BUFFER_BIT); sqr->draw(sqr); glFlush(); } // drag function void drag (int x, int y) { // int x and y of mouse converts to screen coordinates // returns the point as mousePt Points mousePt = sqr->mouse(x,y); //create pointer to window point coordinates Points* mouse = &mousePt; // if the mouse is within the square if (mouse->x > sqr->pts[0].x && mouse->y > sqr->pts[0].y) { if (mouse->x < sqr->pts[2].x && mouse->y < sqr->pts[2].y) { // then drag by chaning square coordinates relative to mouse sqr->drag(sqr,mouse); glutPostRedisplay(); } } } void Initialize() { glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); } int main(int iArgc, char** cppArgv) { glutInit(&iArgc, cppArgv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(400, 400); glutInitWindowPosition(200, 200); glutCreateWindow("Move Box"); glutMotionFunc(drag); Initialize(); glutDisplayFunc(display); glutMainLoop(); return 0; }