diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index f14f1e4c5..e6ef4294e 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -39,7 +39,7 @@ namespace Glest{ namespace Game{ // ===================== PUBLIC ======================== Game::Game(Program *program, const GameSettings *gameSettings): - ProgramState(program) + ProgramState(program), lastMousePos(0) { this->gameSettings= *gameSettings; scrollSpeed = Config::getInstance().getFloat("UiScrollSpeed","1.5"); @@ -421,14 +421,14 @@ void Game::mouseMove(int x, int y, const MouseState *ms){ mouseX = x; mouseY = y; - /* - if (ms.get(mbCenter)) { - if (input.isCtrlDown()) { + if (ms->get(mbCenter)) { + /*if (input.isCtrlDown()) { float speed = input.isShiftDown() ? 1.f : 0.125f; float response = input.isShiftDown() ? 0.1875f : 0.0625f; gameCamera.moveForwardH((y - lastMousePos.y) * speed, response); gameCamera.moveSideH((x - lastMousePos.x) * speed, response); - } else { + } else*/ + { //float ymult = Config::getInstance().getCameraInvertYAxis() ? -0.2f : 0.2f; //float xmult = Config::getInstance().getCameraInvertXAxis() ? -0.2f : 0.2f; float ymult = 0.2f; @@ -436,8 +436,8 @@ void Game::mouseMove(int x, int y, const MouseState *ms){ gameCamera.transitionVH(-(y - lastMousePos.y) * ymult, (lastMousePos.x - x) * xmult); } - } else */ - { + } + else { //main window if (y < 10) { gameCamera.setMoveZ(-scrollSpeed); @@ -473,6 +473,9 @@ void Game::mouseMove(int x, int y, const MouseState *ms){ gui.mouseMoveDisplay(x - metrics.getDisplayX(), y - metrics.getDisplayY()); } } + + lastMousePos.x = x; + lastMousePos.y = y; } void Game::eventMouseWheel(int x, int y, int zDelta) { diff --git a/source/glest_game/game/game.h b/source/glest_game/game/game.h index 00a68fded..5439d46c5 100644 --- a/source/glest_game/game/game.h +++ b/source/glest_game/game/game.h @@ -75,6 +75,7 @@ private: //misc ptr ParticleSystem *weatherParticleSystem; GameSettings gameSettings; + Vec2i lastMousePos; public: Game(Program *program, const GameSettings *gameSettings); diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index 40b1c8bec..70539f121 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -102,13 +102,29 @@ MainWindow::~MainWindow(){ delete program; } -void MainWindow::eventMouseWheel(int x, int y, int zDelta) { - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - program->eventMouseWheel(x, y, zDelta); - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); -} - void MainWindow::eventMouseDown(int x, int y, MouseButton mouseButton){ + + const Metrics &metrics = Metrics::getInstance(); + int vx = metrics.toVirtualX(x); + int vy = metrics.toVirtualY(getH() - y); + + ProgramState *programState = program->getState(); + + switch(mouseButton) { + case mbLeft: + programState->mouseDownLeft(vx, vy); + break; + case mbRight: + programState->mouseDownRight(vx, vy); + break; + case mbCenter: + programState->mouseDownCenter(vx, vy); + break; + default: + break; + } + + /* switch(mouseButton){ case mbLeft: program->mouseDownLeft(x, getH() - y); @@ -119,22 +135,95 @@ void MainWindow::eventMouseDown(int x, int y, MouseButton mouseButton){ default: break; } + */ } void MainWindow::eventMouseUp(int x, int y, MouseButton mouseButton){ + + const Metrics &metrics = Metrics::getInstance(); + int vx = metrics.toVirtualX(x); + int vy = metrics.toVirtualY(getH() - y); + + ProgramState *programState = program->getState(); + + switch(mouseButton) { + case mbLeft: + programState->mouseUpLeft(vx, vy); + break; + case mbRight: + programState->mouseUpRight(vx, vy); + break; + case mbCenter: + programState->mouseUpCenter(vx, vy); + break; + default: + break; + } + + /* if(mouseButton==mbLeft){ program->mouseUpLeft(x, getH() - y); } + */ } void MainWindow::eventMouseDoubleClick(int x, int y, MouseButton mouseButton){ + + const Metrics &metrics= Metrics::getInstance(); + int vx = metrics.toVirtualX(x); + int vy = metrics.toVirtualY(getH() - y); + + ProgramState *programState = program->getState(); + + switch(mouseButton){ + case mbLeft: + programState->mouseDoubleClickLeft(vx, vy); + break; + case mbRight: + programState->mouseDoubleClickRight(vx, vy); + break; + case mbCenter: + programState->mouseDoubleClickCenter(vx, vy); + break; + default: + break; + } + +/* if(mouseButton == mbLeft){ program->mouseDoubleClickLeft(x, getH() - y); } +*/ } void MainWindow::eventMouseMove(int x, int y, const MouseState *ms){ - program->mouseMove(x, getH() - y, ms); + + const Metrics &metrics= Metrics::getInstance(); + int vx = metrics.toVirtualX(x); + int vy = metrics.toVirtualY(getH() - y); + + ProgramState *programState = program->getState(); + programState->mouseMove(vx, vy, ms); + + //program->mouseMove(x, getH() - y, ms); +} + +void MainWindow::eventMouseWheel(int x, int y, int zDelta) { + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + const Metrics &metrics= Metrics::getInstance(); + int vx = metrics.toVirtualX(x); + int vy = metrics.toVirtualY(getH() - y); + + ProgramState *programState = program->getState(); + programState->eventMouseWheel(vx, vy, zDelta); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + //program->eventMouseWheel(x, y, zDelta); + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } void MainWindow::eventKeyDown(char key){ diff --git a/source/glest_game/main/program.cpp b/source/glest_game/main/program.cpp index 5356e605b..da946f9ad 100644 --- a/source/glest_game/main/program.cpp +++ b/source/glest_game/main/program.cpp @@ -155,6 +155,26 @@ Program::~Program(){ singleton = NULL; } +void Program::eventMouseDown(int x, int y, MouseButton mouseButton) { + const Metrics &metrics = Metrics::getInstance(); + int vx = metrics.toVirtualX(x); + int vy = metrics.toVirtualY(window->getH() - y); + + switch(mouseButton) { + case mbLeft: + programState->mouseDownLeft(vx, vy); + break; + case mbRight: + programState->mouseDownRight(vx, vy); + break; + case mbCenter: + programState->mouseDownCenter(vx, vy); + break; + default: + break; + } +} + void Program::mouseDownLeft(int x, int y){ const Metrics &metrics= Metrics::getInstance(); programState->mouseDownLeft(metrics.toVirtualX(x), metrics.toVirtualY(y)); diff --git a/source/glest_game/main/program.h b/source/glest_game/main/program.h index 9437746af..ac6caa1f3 100644 --- a/source/glest_game/main/program.h +++ b/source/glest_game/main/program.h @@ -16,8 +16,10 @@ #include "platform_util.h" #include "window_gl.h" #include "socket.h" -#include "components.h" - +#include "components.h" +#include "window.h" + +using Shared::Platform::MouseButton; using Shared::Graphics::Context; using Shared::Platform::WindowGl; using Shared::Platform::SizeState; @@ -53,9 +55,14 @@ public: virtual void load(){}; virtual void end(){}; virtual void mouseDownLeft(int x, int y){}; - virtual void mouseUpLeft(int x, int y){}; - virtual void mouseDownRight(int x, int y){}; + virtual void mouseUpLeft(int x, int y){}; + virtual void mouseUpRight(int x, int y){} + virtual void mouseUpCenter(int x, int y){} + virtual void mouseDownRight(int x, int y){}; + virtual void mouseDownCenter(int x, int y){} virtual void mouseDoubleClickLeft(int x, int y){}; + virtual void mouseDoubleClickRight(int x, int y){} + virtual void mouseDoubleClickCenter(int x, int y){} virtual void eventMouseWheel(int x, int y, int zDelta){} virtual void mouseMove(int x, int y, const MouseState *mouseState) {}; virtual void keyDown(char key){}; @@ -117,6 +124,8 @@ public: void mouseDownRight(int x, int y); void mouseDoubleClickLeft(int x, int y); void eventMouseWheel(int x, int y, int zDelta); + + void eventMouseDown(int x, int y, MouseButton mouseButton); void mouseMove(int x, int y, const MouseState *mouseState); void keyDown(char key); @@ -127,7 +136,8 @@ public: void showMessage(const char *msg); //misc - void setState(ProgramState *programState); + void setState(ProgramState *programState); + ProgramState * getState() { return programState;} void exit(); private: diff --git a/source/shared_lib/include/platform/sdl/platform_util.h b/source/shared_lib/include/platform/sdl/platform_util.h index d85b44c4a..6adfaf54f 100644 --- a/source/shared_lib/include/platform/sdl/platform_util.h +++ b/source/shared_lib/include/platform/sdl/platform_util.h @@ -69,6 +69,9 @@ public: int64 getMillis() const; int64 getSeconds() const; + static int64 getCurTicks(); + static int64 getCurMillis(); + private: int64 queryCounter(int multiplier) const; }; diff --git a/source/shared_lib/include/platform/sdl/window.h b/source/shared_lib/include/platform/sdl/window.h index d6b90b43f..730703166 100644 --- a/source/shared_lib/include/platform/sdl/window.h +++ b/source/shared_lib/include/platform/sdl/window.h @@ -1,4 +1,5 @@ // ============================================================== +// ============================================================== // This file is part of Glest Shared Library (www.glest.org) // // Copyright (C) 2005 Matthias Braun @@ -15,24 +16,31 @@ #include #include #include +#include #include "types.h" +#include "vec.h" using std::map; using std::string; +using Shared::Graphics::Vec2i; namespace Shared{ namespace Platform{ class Timer; class PlatformContextGl; -enum MouseButton{ +enum MouseButton { + mbUnknown, mbLeft, - mbRight, mbCenter, + mbRight, mbWheelUp, - mbWheelDown + mbWheelDown, + mbButtonX1, + mbButtonX2, + mbCount }; enum SizeState{ @@ -41,6 +49,31 @@ enum SizeState{ ssRestored }; +class MouseState { +private: + bool states[mbCount]; + + +public: + MouseState() { + clear(); + } + //MouseState(const MouseState &); + //MouseState &operator=(const MouseState &); + void clear() { memset(this, 0, sizeof(MouseState)); } + + bool get(MouseButton b) const { + assert(b > 0 && b < mbCount); + return states[b]; + } + + void set(MouseButton b, bool state) { + assert(b > 0 && b < mbCount); + states[b] = state; + } +}; + + // keycode constants (unfortunately designed after DirectInput and therefore not // very specific) // They also have to fit into a char. The positive numbers seem to be equal @@ -59,11 +92,13 @@ const char vkDown = -10; const char vkReturn = -11; const char vkBack = -12; +/* struct MouseState{ bool leftMouse; bool rightMouse; bool centerMouse; }; +*/ enum WindowStyle{ wsFullscreen, @@ -81,6 +116,19 @@ private: int lastMouseX[3]; int lastMouseY[3]; + static unsigned int lastMouseEvent; /** for use in mouse hover calculations */ + static MouseState mouseState; + static Vec2i mousePos; + + static void setLastMouseEvent(unsigned int lastMouseEvent) {Window::lastMouseEvent = lastMouseEvent;} + static unsigned int getLastMouseEvent() {return Window::lastMouseEvent;} + + static const MouseState &getMouseState() {return Window::mouseState;} + static void setMouseState(MouseButton b, bool state) {Window::mouseState.set(b, state);} + + static const Vec2i &getMousePos() {return Window::mousePos;} + static void setMousePos(const Vec2i &mousePos) {Window::mousePos = mousePos;} + protected: int w, h; diff --git a/source/shared_lib/include/platform/win32/platform_util.h b/source/shared_lib/include/platform/win32/platform_util.h index a3c004108..bb6340186 100755 --- a/source/shared_lib/include/platform/win32/platform_util.h +++ b/source/shared_lib/include/platform/win32/platform_util.h @@ -70,6 +70,9 @@ public: int64 getMillis() const; int64 getSeconds() const; + static int64 getCurTicks(); + static int64 getCurMillis(); + private: int64 queryCounter(int multiplier) const; }; diff --git a/source/shared_lib/sources/platform/sdl/platform_util.cpp b/source/shared_lib/sources/platform/sdl/platform_util.cpp index 3eaa8b314..59325ada7 100644 --- a/source/shared_lib/sources/platform/sdl/platform_util.cpp +++ b/source/shared_lib/sources/platform/sdl/platform_util.cpp @@ -115,6 +115,13 @@ int64 Chrono::queryCounter(int multiplier) const { } } +int64 Chrono::getCurMillis() { + return SDL_GetTicks(); +} +int64 Chrono::getCurTicks() { + return SDL_GetTicks(); +} + // ===================================== // Misc // ===================================== diff --git a/source/shared_lib/sources/platform/sdl/window.cpp b/source/shared_lib/sources/platform/sdl/window.cpp index abf95dfdf..67d973b73 100644 --- a/source/shared_lib/sources/platform/sdl/window.cpp +++ b/source/shared_lib/sources/platform/sdl/window.cpp @@ -32,14 +32,22 @@ namespace Shared{ namespace Platform{ // Matze: hack for now... static Window* global_window = 0; +unsigned int Window::lastMouseEvent = 0; /** for use in mouse hover calculations */ +Vec2i Window::mousePos; +MouseState Window::mouseState; + // ========== PUBLIC ========== -Window::Window() { +Window::Window() { memset(lastMouseDown, 0, sizeof(lastMouseDown)); assert(global_window == 0); global_window = this; + + lastMouseEvent = 0; + mousePos = Vec2i(0); + mouseState.clear(); } Window::~Window() { @@ -53,33 +61,55 @@ bool Window::handleEvent() { try { //printf("START [%d]\n",event.type); + switch(event.type) { + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + case SDL_MOUSEMOTION: + + printf("In [%s::%s] Line :%d\n",__FILE__,__FUNCTION__,__LINE__); + setLastMouseEvent(Chrono::getCurMillis()); + setMousePos(Vec2i(event.button.x, event.button.y)); + break; + } + switch(event.type) { case SDL_QUIT: + printf("In [%s::%s] Line :%d\n",__FILE__,__FUNCTION__,__LINE__); return false; case SDL_MOUSEBUTTONDOWN: + printf("In [%s::%s] Line :%d\n",__FILE__,__FUNCTION__,__LINE__); if(global_window) { global_window->handleMouseDown(event); } break; case SDL_MOUSEBUTTONUP: { + printf("In [%s::%s] Line :%d\n",__FILE__,__FUNCTION__,__LINE__); if(global_window) { + MouseButton b = getMouseButton(event.button.button); + setMouseState(b, false); + global_window->eventMouseUp(event.button.x, event.button.y,getMouseButton(event.button.button)); } break; } case SDL_MOUSEMOTION: { - MouseState ms; - ms.leftMouse = (event.motion.state & SDL_BUTTON_LMASK) != 0; - ms.rightMouse = (event.motion.state & SDL_BUTTON_RMASK) != 0; - ms.centerMouse = (event.motion.state & SDL_BUTTON_MMASK) != 0; + printf("In [%s::%s] Line :%d\n",__FILE__,__FUNCTION__,__LINE__); + //MouseState ms; + //ms.leftMouse = (event.motion.state & SDL_BUTTON_LMASK) != 0; + //ms.rightMouse = (event.motion.state & SDL_BUTTON_RMASK) != 0; + //ms.centerMouse = (event.motion.state & SDL_BUTTON_MMASK) != 0; + setMouseState(mbLeft, event.motion.state & SDL_BUTTON_LMASK); + setMouseState(mbRight, event.motion.state & SDL_BUTTON_RMASK); + setMouseState(mbCenter, event.motion.state & SDL_BUTTON_MMASK); if(global_window) { - global_window->eventMouseMove(event.motion.x, event.motion.y, &ms); + global_window->eventMouseMove(event.motion.x, event.motion.y, &getMouseState()); //&ms); } break; } case SDL_KEYDOWN: + printf("In [%s::%s] Line :%d\n",__FILE__,__FUNCTION__,__LINE__); /* handle ALT+Return */ if(event.key.keysym.sym == SDLK_RETURN && (event.key.keysym.mod & (KMOD_LALT | KMOD_RALT))) { @@ -91,6 +121,7 @@ bool Window::handleEvent() { } break; case SDL_KEYUP: + printf("In [%s::%s] Line :%d\n",__FILE__,__FUNCTION__,__LINE__); if(global_window) { global_window->eventKeyUp(getKey(event.key.keysym)); } diff --git a/source/shared_lib/sources/platform/win32/platform_util.cpp b/source/shared_lib/sources/platform/win32/platform_util.cpp index 4ffc15d25..48906ca7c 100644 --- a/source/shared_lib/sources/platform/win32/platform_util.cpp +++ b/source/shared_lib/sources/platform/win32/platform_util.cpp @@ -115,6 +115,15 @@ int64 Chrono::queryCounter(int multiplier) const{ return multiplier*(accumCount+endCount-startCount)/freq; } } + +int64 Chrono::getCurMillis() { + return getCurTicks() * 1000 / freq; +} +int64 Chrono::getCurTicks() { + int64 now; + QueryPerformanceCounter((LARGE_INTEGER*) &now); + return now; +} // ===================================================== // class PlatformExceptionHandler