rnd-20131212-2-src
[rocksndiamonds.git] / src / events.c
index 8f595ecef284cc199912d6f8023f015912a1fc13..d1880faee1297faa523d874dd3fda6c4b9ed28f4 100644 (file)
@@ -152,13 +152,6 @@ void EventLoop(void)
       {
        switch (event.type)
        {
-#if defined(PLATFORM_ANDROID)
-         // !!! TEST ONLY !!!
-         case SDL_QUIT:
-         case SDL_FINGERDOWN:
-           CloseAllAndExit(0);
-#endif
-
          case EVENT_BUTTONPRESS:
          case EVENT_BUTTONRELEASE:
            HandleButtonEvent((ButtonEvent *) &event);
@@ -167,7 +160,15 @@ void EventLoop(void)
          case EVENT_MOTIONNOTIFY:
            HandleMotionEvent((MotionEvent *) &event);
            break;
-  
+
+#if defined(TARGET_SDL2)
+         case EVENT_FINGERPRESS:
+         case EVENT_FINGERRELEASE:
+         case EVENT_FINGERMOTION:
+           HandleFingerEvent((FingerEvent *) &event);
+           break;
+#endif
+
          case EVENT_KEYPRESS:
          case EVENT_KEYRELEASE:
            HandleKeyEvent((KeyEvent *) &event);
@@ -384,6 +385,66 @@ void HandleMotionEvent(MotionEvent *event)
   HandleButton(event->x, event->y, button_status, button_status);
 }
 
+#if defined(TARGET_SDL2)
+void HandleFingerEvent(FingerEvent *event)
+{
+  static int num_events = 0;
+  int max_events = 10;
+
+  // #if DEBUG_EVENTS
+  Error(ERR_DEBUG, "FINGER EVENT: finger was %s, touch ID %lld, finger ID %lld, x/y %f/%f, dx/dy %f/%f, pressure %f",
+       (event->type == EVENT_FINGERPRESS ? "pressed" :
+        event->type == EVENT_FINGERRELEASE ? "released" : "moved"),
+       event->touchId,
+       event->fingerId,
+       event->x, event->y,
+       event->dx, event->dy,
+       event->pressure);
+  // #endif
+
+  int x = (int)(event->x * video.width);
+  int y = (int)(event->y * video.height);
+  int button = MB_LEFTBUTTON;
+
+  Error(ERR_DEBUG, "=> screen x/y %d/%d", x, y);
+
+#if 0
+  if (++num_events >= max_events)
+    CloseAllAndExit(0);
+#endif
+
+#if 1
+  if (event->type == EVENT_FINGERPRESS)
+    button_status = button;
+  else
+    button_status = MB_RELEASED;
+
+  int max_x = SX + SXSIZE;
+  int max_y = SY + SYSIZE;
+
+  if (game_status == GAME_MODE_PLAYING &&
+      x < max_x)
+  {
+    int key_status = (event->type == EVENT_FINGERRELEASE ? KEY_RELEASED :
+                     KEY_PRESSED);
+    Key key = (y <     max_y / 3 ? setup.input[0].key.up :
+              y > 2 * max_y / 3 ? setup.input[0].key.down :
+              x <     max_x / 3 ? setup.input[0].key.left :
+              x > 2 * max_x / 3 ? setup.input[0].key.right :
+              setup.input[0].key.drop);
+
+    Error(ERR_DEBUG, "=> key == %d, key_status == %d", key, key_status);
+
+    HandleKey(key, key_status);
+  }
+  else
+  {
+    HandleButton(x, y, button_status, button);
+  }
+#endif
+}
+#endif
+
 void HandleKeyEvent(KeyEvent *event)
 {
   int key_status = (event->type==EVENT_KEYPRESS ? KEY_PRESSED : KEY_RELEASED);