From a844ac2389c747309b8ab91d6203fd66d573670f Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Mon, 4 Mar 2013 18:49:25 -0400 Subject: [PATCH] Added love.joystickaxismoved, love.joystickballmoved, and love.joystickhatmoved callbacks --HG-- branch : joystickevents --- src/modules/event/Event.h | 1 + src/modules/event/sdl/Event.cpp | 58 ++++++++++++++++++++++++++++++++- src/modules/event/sdl/Event.h | 2 ++ src/scripts/boot.lua | 9 +++++ src/scripts/boot.lua.h | 23 +++++++++++++ 5 files changed, 92 insertions(+), 1 deletion(-) diff --git a/src/modules/event/Event.h b/src/modules/event/Event.h index e28f36c8c..bd44a7b23 100644 --- a/src/modules/event/Event.h +++ b/src/modules/event/Event.h @@ -27,6 +27,7 @@ #include "common/Variant.h" #include "keyboard/Keyboard.h" #include "mouse/Mouse.h" +#include "joystick/Joystick.h" #include "thread/threads.h" // STL diff --git a/src/modules/event/sdl/Event.cpp b/src/modules/event/sdl/Event.cpp index 25ce5f1f6..157430099 100644 --- a/src/modules/event/sdl/Event.cpp +++ b/src/modules/event/sdl/Event.cpp @@ -23,6 +23,8 @@ #include "keyboard/Keyboard.h" #include "mouse/Mouse.h" +#include + namespace love { namespace event @@ -86,7 +88,8 @@ Message *Event::convert(SDL_Event &e) Message *msg = NULL; love::keyboard::Keyboard::Key key; love::mouse::Mouse::Button button; - Variant *arg1, *arg2, *arg3; + love::joystick::Joystick::Hat hat; + Variant *arg1, *arg2, *arg3, *arg4; const char *txt; switch (e.type) { @@ -135,6 +138,44 @@ Message *Event::convert(SDL_Event &e) arg1->release(); arg2->release(); break; + case SDL_JOYAXISMOTION: + { + arg1 = new Variant((double)(e.jaxis.which+1)); + arg2 = new Variant((double)(e.jaxis.axis+1)); + float value = e.jaxis.value / 32768.0f; + if (fabsf(value) < 0.001f) value = 0.0f; + if (value < -0.99f) value = -1.0f; + if (value > 0.99f) value = 1.0f; + arg3 = new Variant((double) value); + msg = new Message("joystickaxismoved", arg1, arg2, arg3); + arg1->release(); + arg2->release(); + arg3->release(); + } + break; + case SDL_JOYBALLMOTION: + arg1 = new Variant((double)(e.jball.which+1)); + arg2 = new Variant((double)(e.jball.ball+1)); + arg3 = new Variant((double)e.jball.xrel); + arg4 = new Variant((double)e.jball.yrel); + msg = new Message("joystickballmoved", arg1, arg2, arg3, arg4); + arg1->release(); + arg2->release(); + arg3->release(); + arg4->release(); + break; + case SDL_JOYHATMOTION: + if (hats.find(e.jhat.value, hat) && love::joystick::Joystick::getConstant(hat, txt)) + { + arg1 = new Variant((double)(e.jhat.which+1)); + arg2 = new Variant((double)(e.jhat.hat+1)); + arg3 = new Variant(txt, strlen(txt)); + msg = new Message("joystickhatmoved", arg1, arg2, arg3); + arg1->release(); + arg2->release(); + arg3->release(); + } + break; case SDL_ACTIVEEVENT: arg1 = new Variant(e.active.gain != 0); if (e.active.state & SDL_APPINPUTFOCUS) @@ -318,6 +359,21 @@ EnumMap: EnumMap Event::buttons(Event::buttonEntries, sizeof(Event::buttonEntries)); +EnumMap::Entry Event::hatEntries[] = +{ + {love::joystick::Joystick::HAT_CENTERED, SDL_HAT_CENTERED}, + {love::joystick::Joystick::HAT_UP, SDL_HAT_UP}, + {love::joystick::Joystick::HAT_RIGHT, SDL_HAT_RIGHT}, + {love::joystick::Joystick::HAT_DOWN, SDL_HAT_DOWN}, + {love::joystick::Joystick::HAT_LEFT, SDL_HAT_LEFT}, + {love::joystick::Joystick::HAT_RIGHTUP, SDL_HAT_RIGHTUP}, + {love::joystick::Joystick::HAT_RIGHTDOWN, SDL_HAT_RIGHTDOWN}, + {love::joystick::Joystick::HAT_LEFTUP, SDL_HAT_LEFTUP}, + {love::joystick::Joystick::HAT_LEFTDOWN, SDL_HAT_LEFTDOWN}, +}; + +EnumMap Event::hats(Event::hatEntries, sizeof(Event::hatEntries)); + } // sdl } // event } // love diff --git a/src/modules/event/sdl/Event.h b/src/modules/event/sdl/Event.h index 177ba6271..e3bfb387e 100644 --- a/src/modules/event/sdl/Event.h +++ b/src/modules/event/sdl/Event.h @@ -72,6 +72,8 @@ private: static EnumMap keys; static EnumMap::Entry buttonEntries[]; static EnumMap buttons; + static EnumMap::Entry hatEntries[]; + static EnumMap hats; }; // System diff --git a/src/scripts/boot.lua b/src/scripts/boot.lua index 536ece438..fb299246d 100644 --- a/src/scripts/boot.lua +++ b/src/scripts/boot.lua @@ -173,6 +173,15 @@ function love.createhandlers() joystickreleased = function (j,b) if love.joystickreleased then love.joystickreleased(j,b) end end, + joystickaxismoved = function (j,a,v) + if love.joystickaxismoved then love.joystickaxismoved(j,a,v) end + end, + joystickballmoved = function (j,b,dx,dy) + if love.joystickballmoved then love.joystickballmoved(j,b,dx,dy) end + end, + joystickhatmoved = function(j,h,v) + if love.joystickhatmoved then love.joystickhatmoved(j,h,v) end + end, focus = function (f) if love.focus then love.focus(f) end end, diff --git a/src/scripts/boot.lua.h b/src/scripts/boot.lua.h index d3666cde0..2ee8d01f9 100644 --- a/src/scripts/boot.lua.h +++ b/src/scripts/boot.lua.h @@ -299,6 +299,29 @@ const unsigned char boot_lua[] = 0x65, 0x2e, 0x6a, 0x6f, 0x79, 0x73, 0x74, 0x69, 0x63, 0x6b, 0x72, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x64, 0x28, 0x6a, 0x2c, 0x62, 0x29, 0x20, 0x65, 0x6e, 0x64, 0x0a, 0x09, 0x09, 0x65, 0x6e, 0x64, 0x2c, 0x0a, + 0x09, 0x09, 0x6a, 0x6f, 0x79, 0x73, 0x74, 0x69, 0x63, 0x6b, 0x61, 0x78, 0x69, 0x73, 0x6d, 0x6f, 0x76, 0x65, + 0x64, 0x20, 0x3d, 0x20, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x28, 0x6a, 0x2c, 0x61, 0x2c, + 0x76, 0x29, 0x0a, + 0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x6a, 0x6f, 0x79, 0x73, 0x74, 0x69, 0x63, + 0x6b, 0x61, 0x78, 0x69, 0x73, 0x6d, 0x6f, 0x76, 0x65, 0x64, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x20, 0x6c, 0x6f, + 0x76, 0x65, 0x2e, 0x6a, 0x6f, 0x79, 0x73, 0x74, 0x69, 0x63, 0x6b, 0x61, 0x78, 0x69, 0x73, 0x6d, 0x6f, 0x76, + 0x65, 0x64, 0x28, 0x6a, 0x2c, 0x61, 0x2c, 0x76, 0x29, 0x20, 0x65, 0x6e, 0x64, 0x0a, + 0x09, 0x09, 0x65, 0x6e, 0x64, 0x2c, 0x0a, + 0x09, 0x09, 0x6a, 0x6f, 0x79, 0x73, 0x74, 0x69, 0x63, 0x6b, 0x62, 0x61, 0x6c, 0x6c, 0x6d, 0x6f, 0x76, 0x65, + 0x64, 0x20, 0x3d, 0x20, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x28, 0x6a, 0x2c, 0x62, 0x2c, + 0x64, 0x78, 0x2c, 0x64, 0x79, 0x29, 0x0a, + 0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x6a, 0x6f, 0x79, 0x73, 0x74, 0x69, 0x63, + 0x6b, 0x62, 0x61, 0x6c, 0x6c, 0x6d, 0x6f, 0x76, 0x65, 0x64, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x20, 0x6c, 0x6f, + 0x76, 0x65, 0x2e, 0x6a, 0x6f, 0x79, 0x73, 0x74, 0x69, 0x63, 0x6b, 0x62, 0x61, 0x6c, 0x6c, 0x6d, 0x6f, 0x76, + 0x65, 0x64, 0x28, 0x6a, 0x2c, 0x62, 0x2c, 0x64, 0x78, 0x2c, 0x64, 0x79, 0x29, 0x20, 0x65, 0x6e, 0x64, 0x0a, + 0x09, 0x09, 0x65, 0x6e, 0x64, 0x2c, 0x0a, + 0x09, 0x09, 0x6a, 0x6f, 0x79, 0x73, 0x74, 0x69, 0x63, 0x6b, 0x68, 0x61, 0x74, 0x6d, 0x6f, 0x76, 0x65, 0x64, + 0x20, 0x3d, 0x20, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x28, 0x6a, 0x2c, 0x68, 0x2c, 0x76, 0x29, 0x0a, + 0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x6a, 0x6f, 0x79, 0x73, 0x74, 0x69, 0x63, + 0x6b, 0x68, 0x61, 0x74, 0x6d, 0x6f, 0x76, 0x65, 0x64, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x20, 0x6c, 0x6f, 0x76, + 0x65, 0x2e, 0x6a, 0x6f, 0x79, 0x73, 0x74, 0x69, 0x63, 0x6b, 0x68, 0x61, 0x74, 0x6d, 0x6f, 0x76, 0x65, 0x64, + 0x28, 0x6a, 0x2c, 0x68, 0x2c, 0x76, 0x29, 0x20, 0x65, 0x6e, 0x64, 0x0a, + 0x09, 0x09, 0x65, 0x6e, 0x64, 0x2c, 0x0a, 0x09, 0x09, 0x66, 0x6f, 0x63, 0x75, 0x73, 0x20, 0x3d, 0x20, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x28, 0x66, 0x29, 0x0a, 0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x66, 0x6f, 0x63, 0x75, 0x73, 0x20, 0x74,