fshake3d  0.0.1
FreeformDensity3DSurfaceEditor
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines
ui_Window_glut.cpp
Go to the documentation of this file.
00001 #include "ui_Window.hpp"
00002 #include "opengl.hpp"
00003 // #include <windows.h>
00004 // #include <GL/glut.h>
00005 #include <map>
00006 
00007 static std::map<int,Window*> gWindowMap;
00008 
00009 static Window* get_window()
00010 {
00011   return gWindowMap[ glutGetWindow() ];
00012 }
00013 
00014 void dispatch_event(Event& e)
00015 {
00016   EventSource* pSource = get_window();
00017   e.setEventSource(pSource);
00018   EventTarget* pTarget = get_window()->getEventTarget();
00019   e.setEventTarget(pTarget);
00020   if (pTarget) pTarget->onEvent(e);
00021 }
00022 
00023 
00024 static void display()
00025 {
00026   Event e;
00027   e.setType(EVENT_WINDOWDISPLAY);
00028   dispatch_event(e);
00029 }
00030 
00031 static void reshape(int w, int h)
00032 {
00033   Event e;
00034   e.setType(EVENT_WINDOWRESHAPE);
00035   dispatch_event(e);
00036 }
00037 
00038 static void mouse(int button, int state, int x, int y)
00039 {
00040   Event e;
00041   e.setType( (state == GLUT_DOWN) ? EVENT_BUTTONDOWN : EVENT_BUTTONUP );
00042   switch(button)
00043   {
00044   case GLUT_LEFT_BUTTON: e.setButton(BUTTON_LEFT); break;
00045   case GLUT_RIGHT_BUTTON: e.setButton(BUTTON_RIGHT); break;
00046   case GLUT_MIDDLE_BUTTON: e.setButton(BUTTON_MIDDLE); break;
00047   default: return;
00048   }
00049   e.setMouseX(x);
00050   e.setMouseY(y);
00051   dispatch_event(e);
00052 }
00053 
00054 static void motion(int x, int y)
00055 {
00056   Event e;
00057   e.setType(EVENT_MOUSEMOTION);
00058   e.setMouseX(x);
00059   e.setMouseY(y);
00060   dispatch_event(e);
00061 }
00062 
00063 static void keyboard(unsigned char ch, int x, int y)
00064 {
00065   Event e;
00066   e.setType(EVENT_KEYDOWN);
00067   e.setKeyCode(ch);
00068   e.setMouseX(x);
00069   e.setMouseY(y);
00070   dispatch_event(e);
00071 }
00072 
00073 Window::Window()
00074 : mWindowId(0)
00075 , mpCaptureMouseMotionTarget(0)
00076 {
00077   mWindowId = glutCreateWindow("");
00078   gWindowMap[mWindowId] = this;
00079   glutDisplayFunc(&display);
00080   glutReshapeFunc(&reshape);
00081   glutMouseFunc(&mouse);
00082   glutMotionFunc(&motion);
00083   glutKeyboardFunc(&keyboard);
00084 }
00085 
00086 Window::~Window()
00087 {
00088   destroyWindow();
00089   gWindowMap[mWindowId] = 0;
00090 }
00091 
00092 int Window::getX() const
00093 {
00094   glutSetWindow(mWindowId);
00095   return glutGet(GLUT_WINDOW_X);
00096 }
00097 
00098 int Window::getY() const
00099 {
00100   glutSetWindow(mWindowId);
00101   return glutGet(GLUT_WINDOW_Y);
00102 }
00103 
00104 int Window::getWidth() const
00105 {
00106   glutSetWindow(mWindowId);
00107   return glutGet(GLUT_WINDOW_WIDTH);
00108 }
00109 
00110 int Window::getHeight() const
00111 {
00112   glutSetWindow(mWindowId);
00113   return glutGet(GLUT_WINDOW_HEIGHT);
00114 }
00115 
00116 void Window::setTitle(const char* title)
00117 {
00118   glutSetWindow(mWindowId);
00119   glutSetWindowTitle(title);
00120 }
00121 void Window::setLocation(int x, int y)
00122 {
00123   glutSetWindow(mWindowId);
00124   glutPositionWindow(x,y);
00125 }
00126 void Window::setSize(int w, int h)
00127 {
00128   glutSetWindow(mWindowId);
00129   glutReshapeWindow(w,h);
00130 }
00131 void Window::setViewport(int x, int y, int w, int h)
00132 {
00133   glutSetWindow(mWindowId);
00134   glutPositionWindow(x,y);
00135   glutReshapeWindow(w,h);
00136 }
00137 void Window::setVisible(bool visible)
00138 {
00139   glutSetWindow(mWindowId);
00140   if (visible)
00141     glutShowWindow();
00142   else
00143     glutHideWindow();
00144 }
00145 
00146 void Window::destroyWindow()
00147 {
00148   if (mWindowId) {
00149     glutDestroyWindow(mWindowId);
00150     mWindowId = 0;
00151   }
00152 }
00153 
00154 void Window::captureMouseMotion(EventTarget* target)
00155 {
00156   mpCaptureMouseMotionTarget = target;
00157 }
00158 
00159 void Window::releaseMouseMotion()
00160 {
00161   mpCaptureMouseMotionTarget = 0;
00162 }
00163 
00164 void InitToolkit(int& argc, char* argv[])
00165 {
00166   glutInit(&argc, argv);
00167   glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
00168 }
00169 
00170 void RunMainloop()
00171 {
00172   glutMainLoop();
00173 }