Ejemplo n.º 1
0
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	//printf("msg = %d\n", message);
	switch (message)
	{
		
	case WM_PAINT:
		{
			PAINTSTRUCT ps;
			BeginPaint(hWnd, &ps);
			EndPaint(hWnd, &ps);
		}
		return 1;

	case WM_ERASEBKGND:
		return 1;
	
	case WM_CLOSE:
		if (sData)
			sData->m_quit = true;
		//PostQuitMessage(0);
		return 1;

	case WM_DESTROY:
		if (sData)
			sData->m_quit = true;
		//PostQuitMessage(0);
		return 1;

	case WM_SYSKEYUP:
	case WM_KEYUP:
		{

			int keycode = getAsciiCodeFromVirtualKeycode(wParam);
			

			if (keycode>=0 && sData && sData->m_keyboardCallback )
			{
				int state=0;
				(*sData->m_keyboardCallback)(keycode,state);
			}
			return 0;
		}
	case WM_CHAR:
		{
			int keycode = wParam;

			if (sData && sData->m_keyboardCallback && ((HIWORD(lParam) & KF_REPEAT) == 0))
			{
				int state = 1;
				(*sData->m_keyboardCallback)(keycode,state);
			}
			return 0;
		}
	case WM_SYSKEYDOWN:
	case WM_KEYDOWN:
		{
			int keycode = getAsciiCodeFromVirtualKeycode(wParam);

			if (keycode>=0 && sData && sData->m_keyboardCallback && ((HIWORD(lParam) & KF_REPEAT) == 0))
			{
				int state = 1;
				(*sData->m_keyboardCallback)(keycode,state);
				return 1;
			}
			return 0;
		}

		case WM_MBUTTONUP:
	{
			int xPos = LOWORD(lParam); 
			int yPos = HIWORD(lParam); 
			if (sData)
			{
				sData->m_mouseMButton=0;
				sData->m_mouseXpos = xPos;
				sData->m_mouseYpos = yPos;
				if (sData && sData->m_mouseButtonCallback)
					(*sData->m_mouseButtonCallback)(1,0,xPos,yPos);
			}
		break;
	}
	case WM_MBUTTONDOWN:
	{
			int xPos = LOWORD(lParam); 
			int yPos = HIWORD(lParam); 
			if (sData)
			{
				sData->m_mouseMButton=1;
				sData->m_mouseXpos = xPos;
				sData->m_mouseYpos = yPos;
				if (sData && sData->m_mouseButtonCallback)
					(*sData->m_mouseButtonCallback)(1,1,xPos,yPos);
			}
		break;
	}

	case WM_LBUTTONUP:
	{
			int xPos = LOWORD(lParam); 
			int yPos = HIWORD(lParam); 
			if (sData)
			{
				sData->m_mouseLButton=0;
				sData->m_mouseXpos = xPos;
				sData->m_mouseYpos = yPos;
				
				if (sData && sData->m_mouseButtonCallback)
					(*sData->m_mouseButtonCallback)(0,0,xPos,yPos);

			}
		//	gDemoApplication->mouseFunc(0,1,xPos,yPos);
		break;
	}
	case WM_LBUTTONDOWN:
		{
				int xPos = LOWORD(lParam); 
				int yPos = HIWORD(lParam); 
			if (sData)
			{
				sData->m_mouseLButton=1;
				sData->m_mouseXpos = xPos;
				sData->m_mouseYpos = yPos;

				if (sData && sData->m_mouseButtonCallback)
					(*sData->m_mouseButtonCallback)(0,1,xPos,yPos);
			}
			break;
		}

	case 0x020e://WM_MOUSEWHEEL_LEFT_RIGHT
	{

		int  zDelta = (short)HIWORD(wParam);
		int xPos = LOWORD(lParam); 
		int yPos = HIWORD(lParam); 
		//m_cameraDistance -= zDelta*0.01;
		if (sData && sData->m_wheelCallback)
			(*sData->m_wheelCallback)(-float(zDelta)*0.05f,0);
		return 1;
		break;
	}
	case 0x020A://WM_MOUSEWHEEL:
	{
		
		int  zDelta = (short)HIWORD(wParam);
		int xPos = LOWORD(lParam); 
		int yPos = HIWORD(lParam); 
		//m_cameraDistance -= zDelta*0.01;
		if (sData && sData->m_wheelCallback)
			(*sData->m_wheelCallback)(0,float(zDelta)*0.05f);
		return 1;
		break;
	}

	case WM_MOUSEMOVE:
		{
				int xPos = LOWORD(lParam); 
				int yPos = HIWORD(lParam); 
				sData->m_mouseXpos = xPos;
				sData->m_mouseYpos = yPos;

				if (sData && sData->m_mouseMoveCallback)
					(*sData->m_mouseMoveCallback)(xPos,yPos);

			break;
		}
	case WM_RBUTTONUP:
	{
			int xPos = LOWORD(lParam); 
			int yPos = HIWORD(lParam); 
			sData->m_mouseRButton = 1;

			if (sData && sData->m_mouseButtonCallback)
				(*sData->m_mouseButtonCallback)(2,0,sData->m_mouseXpos,sData->m_mouseYpos);

			//gDemoApplication->mouseFunc(2,1,xPos,yPos);
		break;
	}
	case WM_RBUTTONDOWN:
	{
			int xPos = LOWORD(lParam); 
			int yPos = HIWORD(lParam); 
			sData->m_mouseRButton = 0;
			if (sData && sData->m_mouseButtonCallback)
				(*sData->m_mouseButtonCallback)(2,1,sData->m_mouseXpos,sData->m_mouseYpos);

		break;
	}
	case WM_QUIT:
		{
			return 0;
			break;
		}
	case WM_SIZE:													// Size Action Has Taken Place

			RECT clientRect;
			GetClientRect(hWnd,&clientRect);

			switch (wParam)												// Evaluate Size Action
			{

				case SIZE_MINIMIZED:									// Was Window Minimized?
				return 0;												// Return

				case SIZE_MAXIMIZED:									// Was Window Maximized?
				case SIZE_RESTORED:										// Was Window Restored?
					RECT wr;
					GetWindowRect(hWnd,&wr);
					
					sData->m_fullWindowWidth = wr.right-wr.left;
					sData->m_fullWindowHeight = wr.bottom-wr.top;//LOWORD (lParam) HIWORD (lParam);
					sData->m_openglViewportWidth = clientRect.right;
					sData->m_openglViewportHeight = clientRect.bottom;
					glViewport(0, 0, sData->m_openglViewportWidth, sData->m_openglViewportHeight);

					if (sData->m_resizeCallback)
						(*sData->m_resizeCallback)(sData->m_openglViewportWidth,sData->m_openglViewportHeight);
					//if (sOpenGLInitialized)
					//{
					//	//gDemoApplication->reshape(sWidth,sHeight);
					//}
				return 0;												// Return
			}
		break;

	default:{

			}
	};

	return DefWindowProc(hWnd, message, wParam, lParam);
}
Ejemplo n.º 2
0
void X11OpenGLWindow::pumpMessage()
{

    int buttonState = 1;

     // Process all pending events
    while( MyXPending( m_data->m_dpy ) )
    {
        MyXNextEvent(m_data->m_dpy, &m_data->m_xev);
  //      printf("#");
  //      fflush(stdout);
        switch( m_data->m_xev.type )
        {
            case KeyPress:
            {
                    int keycode = getAsciiCodeFromVirtualKeycode(m_data->m_xev.xkey.keycode);
		    switch (keycode)
			{
			case B3G_ALT:
			m_data->m_modifierFlags |= MY_X11_ALT_KEY;
			break;
			case B3G_SHIFT:
			m_data->m_modifierFlags |= MY_X11_SHIFT_KEY;
			break;
			case B3G_CONTROL:
			m_data->m_modifierFlags |= MY_X11_CONTROL_KEY;
			break;
			default:
			{}
			};
		if (m_data->m_keyboardCallback)
                {

                    int state = 1;
                    (*m_data->m_keyboardCallback)(keycode,state);
                //    printf("keycode %d",keycode);
                  //  fflush(stdout);

                }
                break;
            }

            case KeyRelease:
            {
   //           fflush(stdout);
 int keycode = getAsciiCodeFromVirtualKeycode( m_data->m_xev.xkey.keycode);
		  switch (keycode)
                        {
                        case B3G_ALT:
                        m_data->m_modifierFlags &= ~MY_X11_ALT_KEY;
                        break;
                        case B3G_SHIFT:
                        m_data->m_modifierFlags &= ~MY_X11_SHIFT_KEY;
                        break;
                        case B3G_CONTROL:
                        m_data->m_modifierFlags &= ~MY_X11_CONTROL_KEY;
                        break;
                        default:
                        {}
                        };

                if (m_data->m_keyboardCallback)
                {
#if 1
                     unsigned short is_retriggered = 0;
///filter out keyboard repeat
//see http://stackoverflow.com/questions/2100654/ignore-auto-repeat-in-x11-applications
                     if (MyXEventsQueued(m_data->m_dpy, QueuedAfterReading))
                       {
                         XEvent nev;
                         MyXPeekEvent(m_data->m_dpy, &nev);

                         if (nev.type == KeyPress && nev.xkey.time ==  m_data->m_xev.xkey.time &&
                             nev.xkey.keycode ==  m_data->m_xev.xkey.keycode)
                           {
                             //fprintf (stdout, "key #%ld was retriggered.\n",
                              // (long) MyXLookupKeysym(&nev.xkey, 0));

                             // delete retriggered KeyPress event
                             MyXNextEvent(m_data->m_dpy, & m_data->m_xev);
                             is_retriggered = 1;
                           }
                       }
#endif
                    int state = 0;
		    if (!is_retriggered)
                    (*m_data->m_keyboardCallback)(keycode,state);
                    }

                break;
            }

            case ButtonRelease:
                buttonState = 0;
                //continue with ButtonPress code
            case ButtonPress:
            {
//                printf("!");
//                fflush(stdout);

                int button=-1;

                switch (m_data->m_xev.xbutton.button)
                {
                    case Button1:
                    {
                    button=0;
                    break;
                    }
                    case Button2:
                    {
                        button=1;
                        break;
                    }
                    case Button3:
                    {
                        button=2;
                        break;
                    }
                    case Button4:
                    {
                        if (m_data->m_wheelCallback)
                        {
                            (*m_data->m_wheelCallback)(0,10);
                        }
                        break;
                    }
                    case Button5:
                    {
                        if (m_data->m_wheelCallback)
                        {
                            (*m_data->m_wheelCallback)(0,-10);
                        }
                        break;
                    }
                }
                int xpos = m_data->m_xev.xmotion.x;
                int ypos = m_data->m_xev.xmotion.y;

                if (button>=0 && m_data->m_mouseButtonCallback)
                {
//                      printf("xpos = %d, ypos = %d\n",xpos,ypos);

                    (*m_data->m_mouseButtonCallback)(button,buttonState,xpos,ypos);
                }
                break;
            }
            case MotionNotify:
            {
//                printf("!");
//                fflush(0);
                if (m_data->m_mouseMoveCallback)
                {
                    int xpos = m_data->m_xev.xmotion.x;
                    int ypos = m_data->m_xev.xmotion.y;
                    (*m_data->m_mouseMoveCallback)(xpos,ypos);
                }
                break;
            }
            case ConfigureNotify:
            {
  //              printf("@");
  //              fflush(0);
		m_data->m_glWidth = m_data->m_xev.xconfigure.width;
		m_data->m_glHeight = m_data->m_xev.xconfigure.height;

                if (m_data->m_resizeCallback)
                {
                    (*m_data->m_resizeCallback)(m_data->m_xev.xconfigure.width,m_data->m_xev.xconfigure.height);
                }
                break;
            }
            case ClientMessage:
            {
  //              printf("?");
  //              fflush(stdout);
                break;
            }
            case Expose:
            {
                    break;
            }
            case DestroyNotify:
            {
                break;
            }
            default:
            {
                //XRRUpdateConfiguration( &event );
            }
        };
    }
}