From a801f0a0b4c021eee2488cee71c3d2cd0037fd5b Mon Sep 17 00:00:00 2001 From: jacob1 Date: Thu, 6 Nov 2014 20:06:45 -0500 Subject: [PATCH] allow lua mousepress event to cancel drawing, fixes #229 --- src/gui/game/GameController.cpp | 5 +++++ src/gui/game/GameController.h | 1 + src/gui/game/GameView.cpp | 13 +++++++++++++ src/gui/game/GameView.h | 1 + src/lua/CommandInterface.h | 3 ++- src/lua/LuaScriptInterface.cpp | 10 ++++++++-- src/lua/LuaScriptInterface.h | 1 + 7 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/gui/game/GameController.cpp b/src/gui/game/GameController.cpp index 42923695a..422ce6458 100644 --- a/src/gui/game/GameController.cpp +++ b/src/gui/game/GameController.cpp @@ -727,6 +727,11 @@ bool GameController::KeyRelease(int key, Uint16 character, bool shift, bool ctrl return ret; } +bool GameController::MouseTick() +{ + return commandInterface->OnMouseTick(); +} + void GameController::Tick() { if(firstTick) diff --git a/src/gui/game/GameController.h b/src/gui/game/GameController.h index c436c9dad..5baf0b0e2 100644 --- a/src/gui/game/GameController.h +++ b/src/gui/game/GameController.h @@ -68,6 +68,7 @@ public: bool MouseWheel(int x, int y, int d); bool KeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); bool KeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt); + bool MouseTick(); void Tick(); void Exit(); diff --git a/src/gui/game/GameView.cpp b/src/gui/game/GameView.cpp index 04e16ab12..344cb400d 100644 --- a/src/gui/game/GameView.cpp +++ b/src/gui/game/GameView.cpp @@ -1729,6 +1729,19 @@ void GameView::DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bo Window::DoKeyRelease(key, character, shift, ctrl, alt); } +void GameView::DoTick(float dt) +{ + //mouse events trigger every frame when mouse is held down, needs to happen here (before things are drawn) so it can clear the point queue if false is returned from a lua mouse event + if (!c->MouseTick()) + { + isMouseDown = false; + drawMode = DrawPoints; + while (!pointQueue.empty()) + pointQueue.pop(); + } + Window::DoTick(dt); +} + void GameView::DoDraw() { Window::DoDraw(); diff --git a/src/gui/game/GameView.h b/src/gui/game/GameView.h index acbe0e03e..8e652495f 100644 --- a/src/gui/game/GameView.h +++ b/src/gui/game/GameView.h @@ -188,6 +188,7 @@ public: virtual void OnBlur(); //Top-level handlers, for Lua interface + virtual void DoTick(float dt); virtual void DoDraw(); virtual void DoMouseMove(int x, int y, int dx, int dy); virtual void DoMouseDown(int x, int y, unsigned button); diff --git a/src/lua/CommandInterface.h b/src/lua/CommandInterface.h index 2e1a1dfae..f4caf4f6e 100644 --- a/src/lua/CommandInterface.h +++ b/src/lua/CommandInterface.h @@ -29,7 +29,8 @@ public: virtual bool OnMouseWheel(int x, int y, int d) {return true;} virtual bool OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) {return true;} virtual bool OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) {return true;} - virtual void OnTick() {} + virtual bool OnMouseTick() { return true; } + virtual void OnTick() { } virtual int Command(std::string command); virtual std::string FormatCommand(std::string command); std::string GetLastError(); diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index 26faae028..f5f923e67 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -3004,14 +3004,20 @@ bool LuaScriptInterface::OnKeyRelease(int key, Uint16 character, bool shift, boo return luacon_keyevent(key, modifiers, LUACON_KUP); } +bool LuaScriptInterface::OnMouseTick() +{ + ui::Engine::Ref().LastTick(gettime()); + if (luacon_mousedown) + return luacon_mouseevent(luacon_mousex, luacon_mousey, luacon_mousebutton, LUACON_MPRESS, 0); + return true; +} + void LuaScriptInterface::OnTick() { lua_getglobal(l, "simulation"); lua_pushinteger(l, luacon_sim->NUM_PARTS); lua_setfield(l, -2, "NUM_PARTS"); lua_pop(l, 1); ui::Engine::Ref().LastTick(gettime()); - if(luacon_mousedown) - luacon_mouseevent(luacon_mousex, luacon_mousey, luacon_mousebutton, LUACON_MPRESS, 0); luacon_step(luacon_mousex, luacon_mousey); } diff --git a/src/lua/LuaScriptInterface.h b/src/lua/LuaScriptInterface.h index de3f0ec99..debaa8b08 100644 --- a/src/lua/LuaScriptInterface.h +++ b/src/lua/LuaScriptInterface.h @@ -161,6 +161,7 @@ public: virtual bool OnMouseWheel(int x, int y, int d); virtual bool OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); virtual bool OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt); + virtual bool OnMouseTick(); virtual void OnTick(); virtual void Init(); virtual void SetWindow(ui::Window * window);