From bd4eab495cd9150ff6480b205e7bb7a64f6291b7 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 19 Oct 2009 21:44:08 +0000 Subject: [PATCH] added mouse button down/up and mouse motion events git-svn-id: svn://anubis/anaglym/trunk@111 99a6e188-d820-4881-8870-2d33a10e2619 --- Engine.cc | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- Engine.h | 5 ++++- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/Engine.cc b/Engine.cc index 7c2fd4f..0cd2fca 100644 --- a/Engine.cc +++ b/Engine.cc @@ -321,13 +321,25 @@ void Engine::run() case SDL_KEYUP: key_released_event(event.key.keysym.sym); break; + case SDL_MOUSEBUTTONDOWN: + mousebutton_pressed_event(event.button.button, + event.button.x, event.button.y); + break; + case SDL_MOUSEBUTTONUP: + mousebutton_released_event(event.button.button, + event.button.x, event.button.y); + break; + case SDL_MOUSEMOTION: + mouse_motion_event(event.motion.x, event.motion.y, + event.motion.xrel, event.motion.yrel); + break; case SDL_QUIT: goto RET; break; case SDL_USEREVENT: if (event.user.code == 0) { - update(); + update_event(); } break; } @@ -336,7 +348,7 @@ RET: ; } -void Engine::update() +void Engine::update_event() { m_drawing = true; if (m_autoPhysics) @@ -388,6 +400,55 @@ void Engine::key_released_event(int keysym) } } +void Engine::mousebutton_pressed_event(int button, int x, int y) +{ + if (m_event_mousebutton_pressed_present) + { + lua_getfield(m_luaState, LUA_GLOBALSINDEX, + AG_EVENT_PREFIX "mousebutton_pressed"); + lua_pushinteger(m_luaState, button); + lua_pushnumber(m_luaState, x); + lua_pushnumber(m_luaState, y); + /* call the mouse button pressed event function + * This pops the function ref and arguments from the stack */ + int s = lua_pcall(m_luaState, 3, LUA_MULTRET, 0); + reportErrors(s); + } +} + +void Engine::mousebutton_released_event(int button, int x, int y) +{ + if (m_event_mousebutton_released_present) + { + lua_getfield(m_luaState, LUA_GLOBALSINDEX, + AG_EVENT_PREFIX "mousebutton_released"); + lua_pushinteger(m_luaState, button); + lua_pushnumber(m_luaState, x); + lua_pushnumber(m_luaState, y); + /* call the mouse button released event function + * This pops the function ref and arguments from the stack */ + int s = lua_pcall(m_luaState, 3, LUA_MULTRET, 0); + reportErrors(s); + } +} + +void Engine::mouse_motion_event(int x, int y, int xrel, int yrel) +{ + if (m_event_mouse_motion_present) + { + lua_getfield(m_luaState, LUA_GLOBALSINDEX, + AG_EVENT_PREFIX "mouse_motion"); + lua_pushnumber(m_luaState, x); + lua_pushnumber(m_luaState, y); + lua_pushnumber(m_luaState, xrel); + lua_pushnumber(m_luaState, yrel); + /* call the mouse motion event function + * This pops the function ref and arguments from the stack */ + int s = lua_pcall(m_luaState, 4, LUA_MULTRET, 0); + reportErrors(s); + } +} + void Engine::checkForFunctionFull(const std::string & lua_fn_name, const std::string & event_name, bool & presentFlag) { diff --git a/Engine.h b/Engine.h index a25a45b..4138041 100644 --- a/Engine.h +++ b/Engine.h @@ -116,9 +116,12 @@ class Engine Uint32 updateCallback(Uint32 interval); void registerLibraries(); bool fileExists(const std::string & path); - void update(); + void update_event(); void key_pressed_event(int keysym); void key_released_event(int keysym); + void mousebutton_pressed_event(int button, int x, int y); + void mousebutton_released_event(int button, int x, int y); + void mouse_motion_event(int x, int y, int xrel, int yrel); Object * createObject(bool is_static, GLuint display_list, float scale = 1.0f) {