الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Dragging problem

مغلق
بدأه نحلة نشيطة في 28 نوفمبر 2003 · 9 رد · 1,563 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

Hi everybody ,,

How to let a rectangle to be dragged IF AND ONLY IF the mouse pointer is inside the rectangle ... !!!

Here is my rectangle and its parameters :

// Rectangle para.s
float x1=10.00f ,y1=-40.00f,x2=-60.00,y2=-50.00, D=0.0;

void drawbar (void)
{

glPushMatrix();

glTranslatef(dragx,dragy,0.0f);
glColor3f(1.0, 1.0, 1.0);
glRectf(x1,y1,x2,y2);
glPopMatrix();

}

And here is my GlutMouse Function :

void myGlutMouse(int button, int button_state, int x, int y )
{

switch(button)
{
case GLUT_RIGHT_BUTTON:
if (button_state == GLUT_DOWN)
{ dragging = true; y = 600 - y; // Win(0,0)
// is left top
lastMouseX = x;
lastMouseY = y;}
else
dragging = false;


// default:
// break;
}
}

Thanks in advance ...

كما أريد أن أكون .. بأمر الله أكون

#2

السلام عليكم,

إدا افترضنا أن x1 x2 هما أفصول المستطيل وأن y1 y2 هما رواتب المستطيل وإدا كان x y هما احداثيات الماوس فللتأكد من أن الماوس هي داخل المستطيل يجب على x أن تكون منحصرة بين x1 و x2 وكدلك يجب على y أن تكون منحصرة هي الأخرى بين y1 و y2

if(x>x1 && x<x2 && y>y1 && y<y2)

{

نوجد وسط المستطيل وهنا نفترض أن x1 أقل من x2 ونفس الشيء بالنسبة ل y

}

وهناك طريقة أخرى مثل المستعملة في MFC وهي أن نصنع Class للمستطيل ونضع فيه دالة تراقب الماوس إن كان داخل أو خارج المستطيل وترجع لنا مثلا TRUE أدا كانت الماوس داخل والعكس في الحالة الأخرى.

بالتوفيق وإلى اللقاء.

#3

AA Chick,

I already did your first suggestion as following :

if  ( (button_state == GLUT_DOWN) && ( x1 <= x <=x2) && ( y1<= y<=y2) )

But it didn't work with my .. !!

When I print the values of x & y Vs the values of x1,x2,y1, & y2 I noticed that the value of x, y is much greater than the values of the rectangle coordinators !!! Should I update the coordinates of the rectangle ?? I didn't think so because (gltranslate function) do the job and drag the rectangle .. really I don't know what to do !!!

كما أريد أن أكون .. بأمر الله أكون

#4

السلام عليكم,

أولا أريد أن أعرف إن كنت تبرمج في الوندوز أو في نضام الكنسول.

إدا كنت في الوندوز يجب تحويل أفاصيل النقطة من Screen إلى النافدة أو العكس حسب ما نريد فمثلا الدالة ScreenToClient تحول أفاصيل نقطة نسبيا إلى الشاشة إلى أفاصيل نسبة إلى نافدة ما علما أن في غالب الأحيان نقط الماوس في نافدة تكون نسبة لهده النافدة.

وإدا كنت في الكنسول فهنا يجب مراعاة نقطة البداية .ففي غالب الأحيان فإن النقطة 0 0 تكون هي يسار وأعلى الشاشة وإدا دهبنا يمينا تكبر x وإدا دهبنا أسفل تكبر y ولكن حسب ما رأيت في معطياتك عندك أرقاما سالبة وهنا يجب أن تعرف كيف تسير الأفاصيل عندك وأين توجد نقطة البداية وتعمل حسب هدا وأضن أن هدا قد يكون ربما سبب الخطء.وأقترح عليك أن تجعل برنامجك يعطيك أفاصيل الماوس عند أي نقرة مثلا وتنقر في أماكن مختلفة في الشاشة وترى كيف تتغير هده الأفاصيل ومن هنا يمكن أن تقوم بالتغييرات اللازمة.

أتمنى لك التوفيق وفي انتضار ما ستتوصل إليه لاحقا .

إلى اللقاء.

#5

Hello Chick ,

I really couldn't understand your explanation well, I'm a brand new programmer using VC++ and opengl ,,

Any thank you for your respond ..

if you familiar with opengl here is me whole code , so you can tell me what's wrong with x,y coordinates ..

include <string.h> 
#include <GL/glut.h> 
#include <GL/glaux.h> 
#include <stdio.h> 
//#include "glui.h" 


float xy_aspect; 
int last_x, last_y; 
float lastMouseX, lastMouseY; 
float dragx, dragy; 
bool dragging = false; 
int x, y; 

int main_window; 


// Rectangle para.s 
float x1=10.00f ,y1=-40.00f,x2=-60.00,y2=-50.00, D=0.0; 

/**************************************** Draw user bar **********/ 
void drawbar (void) 
{ 

glPushMatrix(); 

glTranslatef(dragx,dragy,0.0f); 
glColor3f(1.0, 1.0, 1.0); 
glRectf(x1,y1,x2,y2); 
glPopMatrix(); 

} 

/**************************************** myGlutKeyboard() **********/ 

void myGlutKeyboard(unsigned char Key, int x, int y) 
{ 
switch(Key) 
{ 
case 27: 
case 'q': 
case 'Q': 
case 'x': 
case 'X': 
exit(0); 
break; 
}; 

glutPostRedisplay(); 
} 


/***************************************** myGlutMenu() ***********/ 

void myGlutMenu( int value ) 
{ 
myGlutKeyboard( value, 0, 0 ); 
} 


/***************************************** myGlutIdle() ***********/ 

void myGlutIdle( void ) 
{ 
/* According to the GLUT specification, the current window is 
undefined during an idle callback. So we need to explicitly change 
it if necessary */ 
if ( glutGetWindow() != main_window ) 
glutSetWindow(main_window); 



glutPostRedisplay(); 
} 

/***************************************** myGlutMouse() **********/ 

void myGlutMouse(int button, int button_state, int x, int y ) 
{ 

switch(button) 
{ 
case GLUT_RIGHT_BUTTON: 
if (button_state == GLUT_DOWN) 
{ dragging = true; y = 600 - y; // Win(0,0) 
// is left top 
lastMouseX = x; 
lastMouseY = y;} 
else 
dragging = false; 


// default: 
// break; 
} 
} 


/***************************************** myGlutMotion() **********/ 

void myGlutMotion(int x, int y ) 
{ 
if (dragging) {// y = 600 - y; // win(0,0) 
//is left top 
dragx = x - lastMouseX; 
dragy = y - lastMouseY; 
glutPostRedisplay( ); 
lastMouseX = x; 
lastMouseY = y;} 


} 



/**************************************** myGlutReshape() *************/ 

void myGlutReshape( int x, int y ) 
{ 
int tx, ty, tw, th; 
//viewport_area( &tx, &ty, &tw, &th ); 
glViewport( tx, ty, tw, th ); 

xy_aspect = (float)tw / (float)th; 

glutPostRedisplay(); 
} 


/***************************************** myGlutDisplay() *****************/ 


void myGlutDisplay( void ) 
{ 


glViewport(0, 0, 580, 600); 
glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
gluPerspective(45.0f, (GLfloat)580/(GLfloat)600, 1.0f, 1000.0f); 

glMatrixMode(GL_MODELVIEW); // Sets the projection matrix. 
glLoadIdentity(); // Reset the modelview matrix. 

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clears the screen. 
glLoadIdentity(); // Reset modelview matrix for new frame. 
glTranslatef(0.0f,0.0f, -200.0f); //zoom the "camera" out a bit 

drawbar ( ); 


glutSwapBuffers(); 
} 


/**************************************** main() ********************/ 

void main(int argc, char* argv[]) 
{ 
/****************************************/ 
/* Initialize GLUT and create window */ 
/****************************************/ 

glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); 
glutInitWindowPosition( 50, 50 ); 
glutInitWindowSize( 800, 600 ); 

main_window = glutCreateWindow( "My New Game " ); 
glutDisplayFunc( myGlutDisplay ); 
glutReshapeFunc( myGlutReshape ); 
glutKeyboardFunc( myGlutKeyboard ); 
glutSpecialFunc( NULL ); 
glutMouseFunc( myGlutMouse ); 
glutMotionFunc( myGlutMotion ); 



/****************************************/ 
/* Enable z-buferring */ 
/****************************************/ 

glEnable(GL_DEPTH_TEST); 


glutIdleFunc( myGlutIdle ); 



/**** Regular GLUT main loop ****/ 

glutMainLoop(); 

}

and If you couldn't .. I also thankful for you Chick

Cheers ,

كما أريد أن أكون .. بأمر الله أكون

#6

السلام عليكم ,,,

ماشاء الله عليكي أختي الكريمة نحلة نشيطة ,, فعلا نحلة نشيطة ,,

برمجة ال Graphics باستخدام ال OpenGl أمر رئاع فعلا وصعب في نفس الوقت ,,

فقط استمري في هذا المضمار ولاتتوقفي ,, واذا أخبرتك أن لعبة Quake 3 تمت برمجتها باستخدام ال OpenGl فستعلمن كم هو رائع هذا المحرك ,,

وهي التقنية المنافسة لتقنية Microsoft DirectX ,,, Direct3D ,,,

وفقك الله ,, وقريبا باذن الله سندخل في برمجة الجرافيكس باستخدام ال DirectX هنا في المنتدى ,,

وفعلا رغبت في مساعدتك ,, لكن لاأملك نسخة ال OpenGl SDK لأتمكن من تشغيل المثال ,," الملفات الرأسية مع ملفات LIB الخاصة بها وملفات ال DLL ",,

فلو تكرمتي أخبريني من أين حصلتي على الملفات الرأسية ,, هذا اذا كنتي تعملين على Windows وليس Linux ,,,

بارك الله فيكي,,

تم تعديل هذه المشاركة بواسطة HGB في 1 ديسمبر 2003 في 14:54

banner_60_468.gif

NOTHING IS IMPOSSIBLE

#7

السلام عليكم,

سأبحث عن الملفات الرأسية التي تنقصني وسأرى وإن شاء الله أتمكن من تحديدي الشيء الدي لا يشتغل.

أتمنى لك التوفيق وإلى اللقاء.

#8

AA HGB

Thank you very much brother ..

Yes opengl is very intresting library to use graph facilities in programming ..

I will write a learning lesson here as soon as I finish my course ..

Cheers,

تم تعديل هذه المشاركة بواسطة نحلة نشيطة في 11 ديسمبر 2003 في 22:31

كما أريد أن أكون .. بأمر الله أكون

#9

AA chik

Thank you very much brother .. I solved the problem .. :)

Cheers,

كما أريد أن أكون .. بأمر الله أكون

#10

السلام عليكم ,,

بارك الله فيك أختي الكريمة نحلة نشيطة ,, ويارييت نشوف بعض الشروح عن هذه التقنية لنثري بها المنتدى ,, ونحن في الانتظار :)

وبالتوفيق ,,

banner_60_468.gif

NOTHING IS IMPOSSIBLE

هذا الموضوع مغلق.

مواضيع مشابهة