Checkpoint on getting touch events to come through SDL. This (deliberately) breaks the mouse emulation & cripples input on Android, as those calls are being replaced with real finger down/up/motion commands instead. More to come...

This commit is contained in:
wrenczes
2011-06-07 00:32:26 +00:00
parent 92dd1f2d40
commit f7cad41013
2 changed files with 448 additions and 358 deletions
@@ -23,6 +23,7 @@
#include <android/log.h> #include <android/log.h>
#include "SDL_events.h" #include "SDL_events.h"
#include "SDL_touch.h"
#include "../../events/SDL_mouse_c.h" #include "../../events/SDL_mouse_c.h"
#include "SDL_androidtouch.h" #include "SDL_androidtouch.h"
@@ -34,25 +35,65 @@
#define ACTION_CANCEL 3 #define ACTION_CANCEL 3
#define ACTION_OUTSIDE 4 #define ACTION_OUTSIDE 4
static SDL_TouchID gTouchID = 0;
SDL_Touch* CreateTouchInstance()
{
SDL_Touch touch;
touch.id = gTouchID;
touch.x_min = 0;
touch.x_max = 1;
touch.native_xres = touch.x_max - touch.x_min;
touch.y_min = 0;
touch.y_max = 1;
touch.native_yres = touch.y_max - touch.y_min;
touch.pressure_min = 0;
touch.pressure_max = 1;
touch.native_pressureres = touch.pressure_max - touch.pressure_min;
SDL_AddTouch(&touch, "");
return SDL_GetTouch(touch.id);
}
void Android_OnTouch(int action, float x, float y, float p) void Android_OnTouch(int action, float x, float y, float p)
{ {
if (!Android_Window) { if (!Android_Window)
{
return; return;
} }
if ((action != ACTION_CANCEL) && (action != ACTION_OUTSIDE)) { if ((action != ACTION_CANCEL) && (action != ACTION_OUTSIDE))
SDL_SetMouseFocus(Android_Window); {
SDL_SendMouseMotion(Android_Window, 0, (int)x, (int)y); //SDL_SetMouseFocus(Android_Window);
switch(action) { //SDL_SendMouseMotion(Android_Window, 0, (int)x, (int)y);
SDL_Touch* touch = SDL_GetTouch(gTouchID);
if (touch == NULL)
{
touch = CreateTouchInstance();
}
switch(action)
{
case ACTION_DOWN: case ACTION_DOWN:
SDL_SendMouseButton(Android_Window, SDL_PRESSED, SDL_BUTTON_LEFT); SDL_SendFingerDown(touch->id, 1, 1, x, y, p);
//SDL_SendMouseButton(Android_Window, SDL_PRESSED, SDL_BUTTON_LEFT);
break; break;
case ACTION_MOVE:
SDL_SendTouchMotion(touch->id, 1, 0, x, y, 1);
break;
case ACTION_UP: case ACTION_UP:
SDL_SendMouseButton(Android_Window, SDL_RELEASED, SDL_BUTTON_LEFT); SDL_SendFingerDown(touch->id, 1, 0, x, y, p);
//SDL_SendMouseButton(Android_Window, SDL_RELEASED, SDL_BUTTON_LEFT);
break; break;
} }
} else { }
SDL_SetMouseFocus(NULL); else
{
//SDL_SetMouseFocus(NULL);
} }
} }
+103 -54
View File
@@ -40,8 +40,9 @@ enum eDisplayMode
unsigned int gDisplayMode = DisplayMode_lowRes; unsigned int gDisplayMode = DisplayMode_lowRes;
class SdlApp { class SdlApp
public: /* For easy interfacing with JGE static functions */ {
public: /* For easy interfacing with JGE static functions */
bool Running; bool Running;
SDL_Window* window; SDL_Window* window;
SDL_Surface* Surf_Display; SDL_Surface* Surf_Display;
@@ -52,23 +53,24 @@ class SdlApp {
int windowed_pos_x; int windowed_pos_x;
int windowed_pos_y; int windowed_pos_y;
public: public:
SdlApp() { SdlApp() : Surf_Display(NULL), window(NULL), lastMouseUpTime(0), Running(true)
Surf_Display = NULL; {
window = NULL; }
lastMouseUpTime = 0;
Running = true;
};
int OnExecute() { int OnExecute()
if(OnInit() == false) { {
if (OnInit() == false)
{
return -1; return -1;
} }
SDL_Event Event; SDL_Event Event;
while(Running) { while(Running)
while(SDL_WaitEventTimeout(&Event, 0)) { {
while(SDL_WaitEventTimeout(&Event, 0))
{
OnEvent(&Event); OnEvent(&Event);
} }
OnUpdate(); OnUpdate();
@@ -79,10 +81,11 @@ class SdlApp {
return 0; return 0;
}; };
public: public:
bool OnInit(); bool OnInit();
void OnResize(int width, int height) { void OnResize(int width, int height)
{
DebugTrace("OnResize Width " << width << " height " << height); DebugTrace("OnResize Width " << width << " height " << height);
if ((GLfloat)width / (GLfloat)height <= ACTUAL_RATIO) if ((GLfloat)width / (GLfloat)height <= ACTUAL_RATIO)
@@ -106,7 +109,7 @@ class SdlApp {
JRenderer::GetInstance()->SetActualHeight(static_cast<float>(viewPort.h)); JRenderer::GetInstance()->SetActualHeight(static_cast<float>(viewPort.h));
glScissor(0, 0, width, height); glScissor(0, 0, width, height);
#if (!defined GL_ES_VERSION_2_0) && (!defined GL_VERSION_2_0) #if (!defined GL_ES_VERSION_2_0) && (!defined GL_VERSION_2_0)
glMatrixMode (GL_PROJECTION); // Select The Projection Matrix glMatrixMode (GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity (); // Reset The Projection Matrix glLoadIdentity (); // Reset The Projection Matrix
@@ -121,53 +124,62 @@ class SdlApp {
glDisable (GL_DEPTH_TEST); glDisable (GL_DEPTH_TEST);
#endif #endif
}; }
void OnKeyPressed(const SDL_KeyboardEvent& event); void OnKeyPressed(const SDL_KeyboardEvent& event);
void OnMouseDoubleClicked(const SDL_MouseButtonEvent& event); void OnMouseDoubleClicked(const SDL_MouseButtonEvent& event);
void OnMouseClicked(const SDL_MouseButtonEvent& event); void OnMouseClicked(const SDL_MouseButtonEvent& event);
void OnMouseMoved(const SDL_MouseMotionEvent& event); void OnMouseMoved(const SDL_MouseMotionEvent& event);
void OnEvent(SDL_Event* Event) {
/* if(Event->type < SDL_USEREVENT) void OnEvent(SDL_Event* Event)
DebugTrace("Event received" << Event->type);*/
switch(Event->type){
case SDL_QUIT:
{ {
/* if(Event->type < SDL_USEREVENT)
DebugTrace("Event received" << Event->type);*/
switch(Event->type)
{
case SDL_QUIT:
Running = false; Running = false;
break; break;
}
/* On Android, this is triggered when the device orientation changed */
case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:
{ /* On Android, this is triggered when the device orientation changed */
window = SDL_GetWindowFromID(Event->window.windowID); window = SDL_GetWindowFromID(Event->window.windowID);
int h,w; int h,w;
SDL_GetWindowSize(window, &w, &h); SDL_GetWindowSize(window, &w, &h);
OnResize(w, h); OnResize(w, h);
break; break;
}
case SDL_KEYDOWN: case SDL_KEYDOWN:
case SDL_KEYUP: case SDL_KEYUP:
OnKeyPressed(Event->key); OnKeyPressed(Event->key);
break; break;
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
OnMouseMoved(Event->motion); OnMouseMoved(Event->motion);
break; break;
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
OnMouseClicked(Event->button); OnMouseClicked(Event->button);
break; break;
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
{ {
Uint32 eventTime = SDL_GetTicks(); Uint32 eventTime = SDL_GetTicks();
if(eventTime - lastMouseUpTime <= 500) { if (eventTime - lastMouseUpTime <= 500)
{
OnMouseDoubleClicked(Event->button); OnMouseDoubleClicked(Event->button);
} else { }
else
{
OnMouseClicked(Event->button); OnMouseClicked(Event->button);
} }
lastMouseUpTime = eventTime; lastMouseUpTime = eventTime;
break;
} }
break;
case SDL_FINGERMOTION: case SDL_FINGERMOTION:
{
DebugTrace("FingerMotion : touchId " << Event->tfinger.touchId DebugTrace("FingerMotion : touchId " << Event->tfinger.touchId
<< ", fingerId " << Event->tfinger.fingerId << ", fingerId " << Event->tfinger.fingerId
<< ", state " << Event->tfinger.state << ", state " << Event->tfinger.state
@@ -177,11 +189,23 @@ class SdlApp {
<< ", dy " << Event->tfinger.dy << ", dy " << Event->tfinger.dy
<< ", pressure " << Event->tfinger.pressure << ", pressure " << Event->tfinger.pressure
); );
OnMouseMoved(Event->motion);
break; break;
}
case SDL_FINGERDOWN:
DebugTrace("FingerDown msg");
OnMouseClicked(Event->button);
break;
case SDL_FINGERUP:
DebugTrace("FingerUp msg");
DebugTrace("X: " << Event->button.x << ", Y: " << Event->button.y);
OnMouseDoubleClicked(Event->button);
break;
case SDL_MULTIGESTURE: case SDL_MULTIGESTURE:
{ DebugTrace("Multigesture : touchId " << Event->mgesture.touchId
DebugTrace("Multigesure : touchId " << Event->mgesture.touchId
<< ", x " << Event->mgesture.x << ", x " << Event->mgesture.x
<< ", y " << Event->mgesture.y << ", y " << Event->mgesture.y
<< ", dTheta " << Event->mgesture.dTheta << ", dTheta " << Event->mgesture.dTheta
@@ -190,9 +214,11 @@ class SdlApp {
break; break;
} }
} }
}
void OnUpdate(); void OnUpdate();
void OnCleanup() {
void OnCleanup()
{
SDL_FreeSurface(Surf_Display); SDL_FreeSurface(Surf_Display);
SDL_Quit(); SDL_Quit();
} }
@@ -338,16 +364,20 @@ void SdlApp::OnUpdate()
int64_t dt = (tickCount - lastTickCount); int64_t dt = (tickCount - lastTickCount);
lastTickCount = tickCount; lastTickCount = tickCount;
if(g_engine->IsDone()) { if(g_engine->IsDone())
{
SDL_Event event; SDL_Event event;
event.user.type = SDL_QUIT; event.user.type = SDL_QUIT;
SDL_PushEvent(&event); SDL_PushEvent(&event);
} }
try { try
{
g_engine->SetDelta((float)dt / 1000.0f); g_engine->SetDelta((float)dt / 1000.0f);
g_engine->Update((float)dt / 1000.0f); g_engine->Update((float)dt / 1000.0f);
} catch(out_of_range& oor) { }
catch(out_of_range& oor)
{
cerr << oor.what(); cerr << oor.what();
} }
@@ -359,9 +389,12 @@ void SdlApp::OnUpdate()
void SdlApp::OnKeyPressed(const SDL_KeyboardEvent& event) void SdlApp::OnKeyPressed(const SDL_KeyboardEvent& event)
{ {
if(event.type == SDL_KEYDOWN) { if (event.type == SDL_KEYDOWN)
{
g_engine->HoldKey_NoRepeat((LocalKeySym)event.keysym.sym); g_engine->HoldKey_NoRepeat((LocalKeySym)event.keysym.sym);
} else if(event.type == SDL_KEYUP) { }
else if(event.type == SDL_KEYUP)
{
g_engine->ReleaseKey((LocalKeySym)event.keysym.sym); g_engine->ReleaseKey((LocalKeySym)event.keysym.sym);
} }
} }
@@ -374,7 +407,8 @@ void SdlApp::OnMouseMoved(const SDL_MouseMotionEvent& event)
if (event.y >= viewPort.y && if (event.y >= viewPort.y &&
event.y <= viewPort.y + viewPort.h && event.y <= viewPort.y + viewPort.h &&
event.x >= viewPort.x && event.x >= viewPort.x &&
event.x <= viewPort.x + viewPort.w) { event.x <= viewPort.x + viewPort.w)
{
g_engine->LeftClicked( g_engine->LeftClicked(
((event.x-viewPort.x)*SCREEN_WIDTH)/actualWidth, ((event.x-viewPort.x)*SCREEN_WIDTH)/actualWidth,
((event.y-viewPort.y)*SCREEN_HEIGHT)/actualHeight); ((event.y-viewPort.y)*SCREEN_HEIGHT)/actualHeight);
@@ -393,9 +427,9 @@ void SdlApp::OnMouseDoubleClicked(const SDL_MouseButtonEvent& event)
void SdlApp::OnMouseClicked(const SDL_MouseButtonEvent& event) void SdlApp::OnMouseClicked(const SDL_MouseButtonEvent& event)
{ {
if(event.type == SDL_MOUSEBUTTONDOWN) if (event.type == SDL_MOUSEBUTTONDOWN)
{ {
if(event.button == SDL_BUTTON_LEFT) /* Left button */ if (event.button == SDL_BUTTON_LEFT) /* Left button */
{ {
// this is intended to convert window coordinate into game coordinate. // this is intended to convert window coordinate into game coordinate.
// this is correct only if the game and window have the same aspect ratio, otherwise, it's just wrong // this is correct only if the game and window have the same aspect ratio, otherwise, it's just wrong
@@ -405,16 +439,21 @@ void SdlApp::OnMouseClicked(const SDL_MouseButtonEvent& event)
if (event.y >= viewPort.y && if (event.y >= viewPort.y &&
event.y <= viewPort.y + viewPort.h && event.y <= viewPort.y + viewPort.h &&
event.x >= viewPort.x && event.x >= viewPort.x &&
event.x <= viewPort.x + viewPort.w) { event.x <= viewPort.x + viewPort.w)
{
g_engine->LeftClicked( g_engine->LeftClicked(
((event.x-viewPort.x)*SCREEN_WIDTH)/actualWidth, ((event.x-viewPort.x)*SCREEN_WIDTH)/actualWidth,
((event.y-viewPort.y)*SCREEN_HEIGHT)/actualHeight); ((event.y-viewPort.y)*SCREEN_HEIGHT)/actualHeight);
#if (!defined ANDROID) && (!defined IOS) #if (!defined ANDROID) && (!defined IOS)
g_engine->HoldKey_NoRepeat(JGE_BTN_OK); g_engine->HoldKey_NoRepeat(JGE_BTN_OK);
#endif #endif
} else if(event.y < viewPort.y) { }
else if(event.y < viewPort.y)
{
g_engine->HoldKey_NoRepeat(JGE_BTN_MENU); g_engine->HoldKey_NoRepeat(JGE_BTN_MENU);
} else if(event.y > viewPort.y + viewPort.h) { }
else if(event.y > viewPort.y + viewPort.h)
{
g_engine->HoldKey_NoRepeat(JGE_BTN_NEXT); g_engine->HoldKey_NoRepeat(JGE_BTN_NEXT);
} }
} }
@@ -426,20 +465,26 @@ void SdlApp::OnMouseClicked(const SDL_MouseButtonEvent& event)
{ /* interrupt please */ { /* interrupt please */
g_engine->HoldKey_NoRepeat(JGE_BTN_SEC); g_engine->HoldKey_NoRepeat(JGE_BTN_SEC);
} }
} else if (event.type == SDL_MOUSEBUTTONUP) }
else if (event.type == SDL_MOUSEBUTTONUP)
{ {
if(event.button == SDL_BUTTON_LEFT) if(event.button == SDL_BUTTON_LEFT)
{ {
if (event.y >= viewPort.y && if (event.y >= viewPort.y &&
event.y <= viewPort.y + viewPort.h && event.y <= viewPort.y + viewPort.h &&
event.x >= viewPort.x && event.x >= viewPort.x &&
event.x <= viewPort.x + viewPort.w) { event.x <= viewPort.x + viewPort.w)
{
#if (!defined ANDROID) && (!defined IOS) #if (!defined ANDROID) && (!defined IOS)
g_engine->ReleaseKey(JGE_BTN_OK); g_engine->ReleaseKey(JGE_BTN_OK);
#endif #endif
} else if(event.y < viewPort.y) { }
else if(event.y < viewPort.y)
{
g_engine->ReleaseKey(JGE_BTN_MENU); g_engine->ReleaseKey(JGE_BTN_MENU);
} else if(event.y > viewPort.y + viewPort.h) { }
else if(event.y > viewPort.y + viewPort.h)
{
g_engine->ReleaseKey(JGE_BTN_NEXT); g_engine->ReleaseKey(JGE_BTN_NEXT);
} }
} }
@@ -454,10 +499,12 @@ void SdlApp::OnMouseClicked(const SDL_MouseButtonEvent& event)
} }
} }
bool SdlApp::OnInit() { bool SdlApp::OnInit()
{
int window_w, window_h; int window_w, window_h;
if(SDL_Init(SDL_INIT_EVERYTHING) < 0) { if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
return false; return false;
} }
@@ -493,9 +540,11 @@ bool SdlApp::OnInit() {
if((Surf_Display = SDL_SetVideoMode(window_w, window_h, 32, if((Surf_Display = SDL_SetVideoMode(window_w, window_h, 32,
#ifdef ANDROID #ifdef ANDROID
SDL_OPENGL | SDL_FULLSCREEN | SDL_WINDOW_BORDERLESS)) == NULL) { SDL_OPENGL | SDL_FULLSCREEN | SDL_WINDOW_BORDERLESS)) == NULL)
{
#else #else
SDL_OPENGL | SDL_RESIZABLE )) == NULL) { SDL_OPENGL | SDL_RESIZABLE )) == NULL)
{
#endif #endif
return false; return false;
} }