00001 #include "GuiWrapper.h"
00002
00003 #include "glwx.h"
00004
00005 #include <string>
00006
00007 GuiWrapper::GuiWrapper(GUIEventListener * eventListener)
00008 {
00009 handler = eventListener;
00010 fpsDisplay = NULL;
00011 dt = 0;
00012 elapsed = 0;
00013 frames = 0;
00014 isActive = true;
00015 guiFrame = new GUIFrame;
00016 }
00017
00018 GuiWrapper::~GuiWrapper()
00019 {
00020 TexturesManager::flushAllTextures();
00021 }
00022
00028 bool GuiWrapper::Init(const char * dataPath)
00029 {
00030 Logger::initialize();
00031
00032 std::string path = dataPath;
00033 std::string texturesPath = path + "/Textures/";
00034 std::string layoutsPath = path + "/Layouts/";
00035 std::string layout = layoutsPath + "Layout.xml";
00036
00037 MediaPathManager::registerPath(texturesPath.c_str());
00038 MediaPathManager::registerPath(layoutsPath.c_str());
00039 guiFrame->GUIPanel::loadXMLSettings(layout.c_str());
00040 guiFrame->setGUIEventListener(handler);
00041
00042 fpsDisplay = (GUILabel*)guiFrame->getWidgetByCallbackString("fpsCounter");
00043
00044 Logger::flush();
00045 return true;
00046 }
00047
00048 bool GuiWrapper::MouseClicked(int button, bool isDown, int x, int y)
00049 {
00050 button = (button == 1 ) ? MB_BUTTON1 :
00051 (button == 2) ? MB_BUTTON2 : MB_BUTTON3;
00052
00053 MouseEvent event = MouseEvent(MB_BUTTON1, x, y, guiFrame->getHeight() - y);
00054 guiFrame->checkMouseEvents(event, (isDown == true) ? ME_CLICKED: ME_RELEASED);
00055 return event.getUsed();
00056
00057 }
00058
00059 bool GuiWrapper::MouseMoved(int x, int y)
00060 {
00061 MouseEvent event = MouseEvent(MB_UNKNOWN_BUTTON, x, y, guiFrame->getHeight() - y);
00062 guiFrame->checkMouseEvents(event, ME_MOVED);
00063 return event.getUsed();
00064 }
00065
00066 bool GuiWrapper::AsciiKeyDown(unsigned char key)
00067 {
00068 if (key == 27) this->SetActive(!this->IsActive());
00069 KeyEvent event(key);
00070 guiFrame->checkKeyboardEvents(event, KE_PRESSED);
00071 return event.getUsed();
00072 }
00073
00074 bool GuiWrapper::SpecialKeyDown(int key) {
00075
00076
00077 return false;
00078 }
00079
00080 void GuiWrapper::SetDimensions(int width, int height)
00081 {
00082 guiFrame->setDimensions(float(width), float(height));
00083 guiFrame->forceUpdate(true);
00084 }
00085
00086 void GuiWrapper::RenderScene()
00087 {
00088
00089 Enter2DMode(guiFrame->getWidth(), guiFrame->getHeight());
00090 guiFrame->render(dt);
00091 Exit2DMode();
00092
00093 char charBuffer[50];
00094
00095 if(fpsDisplay)
00096 {
00097 sprintf(charBuffer, "Current FPS: %d", int(fps) );
00098 fpsDisplay->setLabelString(charBuffer);
00099 }
00100
00101 }
00102
00103 void GuiWrapper::Update(double dt)
00104 {
00105 this->dt = dt;
00106 elapsed += dt;
00107 frames++;
00108
00109 if (elapsed >= 0.1)
00110 {
00111 fps = frames/elapsed;
00112 frames = 0;
00113 elapsed = 0;
00114 }
00115 }
00116
00117 bool GuiWrapper::IsActive()
00118 {
00119 return isActive;
00120 }
00121
00122 void GuiWrapper::SetActive( bool active )
00123 {
00124 isActive = active;
00125 }
00126
00127
00128 void GuiWrapper::Enter2DMode(int width, int height)
00129 {
00130 GLint viewport[4];
00131
00132 glGetIntegerv(GL_VIEWPORT, viewport);
00133 int winWidth = viewport[2];
00134 int winHeight = viewport[3];
00135
00136 glMatrixMode(GL_MODELVIEW);
00137 glPushMatrix();
00138 glLoadIdentity();
00139 glMatrixMode(GL_PROJECTION);
00140 glPushMatrix();
00141 glLoadIdentity();
00142 gluOrtho2D(0, winWidth, winHeight, 0);
00143 glDisable(GL_DEPTH_TEST);
00144 glDisable(GL_LIGHTING);
00145
00146 }
00147
00148 void GuiWrapper::Exit2DMode()
00149 {
00150 glMatrixMode(GL_PROJECTION);
00151 glPopMatrix();
00152 glMatrixMode(GL_MODELVIEW);
00153 glPopMatrix();
00154 glEnable(GL_DEPTH_TEST);
00155 glEnable(GL_LIGHTING);
00156 glEnable(GL_BLEND);
00157
00158 }
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168