added selecting new level set if dropped as zip file into window
[rocksndiamonds.git] / src / events.c
index 73624accacbaebe9c87d0aac6c6a090d2bf97af3..05d4b351e8b868b9307917dc4fcba7483fd0b3eb 100644 (file)
@@ -39,6 +39,8 @@ static int cursor_mode_last = CURSOR_DEFAULT;
 static unsigned int special_cursor_delay = 0;
 static unsigned int special_cursor_delay_value = 1000;
 
+static boolean virtual_button_pressed = FALSE;
+
 
 // forward declarations for internal use
 static void HandleNoEvent(void);
@@ -56,13 +58,11 @@ static int FilterEvents(const Event *event)
 {
   MotionEvent *motion;
 
-#if defined(TARGET_SDL2)
   // skip repeated key press events if keyboard auto-repeat is disabled
   if (event->type == EVENT_KEYPRESS &&
       event->key.repeat &&
       !keyrepeat_status)
     return 0;
-#endif
 
   if (event->type == EVENT_BUTTONPRESS ||
       event->type == EVENT_BUTTONRELEASE)
@@ -185,7 +185,6 @@ static void HandleEvents(void)
        HandleMotionEvent((MotionEvent *) &event);
        break;
 
-#if defined(TARGET_SDL2)
       case EVENT_WHEELMOTION:
        HandleWheelEvent((WheelEvent *) &event);
        break;
@@ -210,7 +209,6 @@ static void HandleEvents(void)
       case SDL_APP_DIDENTERFOREGROUND:
        HandlePauseResumeEvent((PauseResumeEvent *) &event);
        break;
-#endif
 
       case EVENT_KEYPRESS:
       case EVENT_KEYRELEASE:
@@ -253,8 +251,6 @@ void HandleOtherEvents(Event *event)
       HandleClientMessageEvent((ClientMessageEvent *) event);
       break;
 
-#if defined(TARGET_SDL)
-#if defined(TARGET_SDL2)
     case SDL_CONTROLLERBUTTONDOWN:
     case SDL_CONTROLLERBUTTONUP:
       // for any game controller button event, disable overlay buttons
@@ -266,17 +262,19 @@ void HandleOtherEvents(Event *event)
     case SDL_CONTROLLERDEVICEADDED:
     case SDL_CONTROLLERDEVICEREMOVED:
     case SDL_CONTROLLERAXISMOTION:
-#endif
     case SDL_JOYAXISMOTION:
     case SDL_JOYBUTTONDOWN:
     case SDL_JOYBUTTONUP:
       HandleJoystickEvent(event);
       break;
 
-    case SDL_SYSWMEVENT:
-      HandleWindowManagerEvent(event);
+    case SDL_DROPFILE:
+      HandleDropFileEvent(event);
+      break;
+
+    case SDL_DROPTEXT:
+      HandleDropTextEvent(event);
       break;
-#endif
 
     default:
       break;
@@ -349,7 +347,6 @@ void EventLoop(void)
 
 void ClearAutoRepeatKeyEvents(void)
 {
-#if defined(TARGET_SDL2)
   while (PendingEvent())
   {
     Event next_event;
@@ -363,7 +360,6 @@ void ClearAutoRepeatKeyEvents(void)
     else
       break;
   }
-#endif
 }
 
 void ClearEventQueue(void)
@@ -382,12 +378,10 @@ void ClearEventQueue(void)
        ClearPlayerAction();
        break;
 
-#if defined(TARGET_SDL2)
       case SDL_CONTROLLERBUTTONUP:
        HandleJoystickEvent(&event);
        ClearPlayerAction();
        break;
-#endif
 
       default:
        HandleOtherEvents(&event);
@@ -467,12 +461,10 @@ void SleepWhileUnmapped(void)
        key_joystick_mapping = 0;
        break;
 
-#if defined(TARGET_SDL2)
       case SDL_CONTROLLERBUTTONUP:
        HandleJoystickEvent(&event);
        key_joystick_mapping = 0;
        break;
-#endif
 
       case EVENT_MAPNOTIFY:
        window_unmapped = FALSE;
@@ -540,8 +532,6 @@ void HandleMotionEvent(MotionEvent *event)
   HandleButton(event->x, event->y, button_status, button_status);
 }
 
-#if defined(TARGET_SDL2)
-
 void HandleWheelEvent(WheelEvent *event)
 {
   int button_nr;
@@ -738,6 +728,8 @@ static void HandleFingerEvent_VirtualButtons(FingerEvent *event)
                           "KEY_PRESSED");
   int i;
 
+  virtual_button_pressed = (key_status == KEY_PRESSED && key != KSYM_UNDEFINED);
+
   // for any touch input event, enable overlay buttons (if activated)
   SetOverlayEnabled(TRUE);
 
@@ -1037,8 +1029,6 @@ void HandleFingerEvent(FingerEvent *event)
     HandleFingerEvent_WipeGestures(event);
 }
 
-#endif
-
 static void HandleButtonOrFinger_WipeGestures_MM(int mx, int my, int button)
 {
   static int old_mx = 0, old_my = 0;
@@ -1398,8 +1388,6 @@ static void HandleButtonOrFinger(int mx, int my, int button)
   }
 }
 
-#if defined(TARGET_SDL2)
-
 static boolean checkTextInputKeyModState(void)
 {
   // when playing, only handle raw key events and ignore text input
@@ -1448,8 +1436,6 @@ void HandlePauseResumeEvent(PauseResumeEvent *event)
   }
 }
 
-#endif
-
 void HandleKeyEvent(KeyEvent *event)
 {
   int key_status = (event->type == EVENT_KEYPRESS ? KEY_PRESSED : KEY_RELEASED);
@@ -1474,22 +1460,24 @@ void HandleKeyEvent(KeyEvent *event)
     // always map the "back" button to the "escape" key on Android devices
     key = KSYM_Escape;
   }
+  else if (key == KSYM_Menu)
+  {
+    // the "menu" button can be used to toggle displaying virtual buttons
+    if (key_status == KEY_PRESSED)
+      SetOverlayEnabled(!GetOverlayEnabled());
+  }
   else
   {
-    // for any key event other than "back" button, disable overlay buttons
+    // for any other "real" key event, disable virtual buttons
     SetOverlayEnabled(FALSE);
   }
 #endif
 
   HandleKeyModState(keymod, key_status);
 
-#if defined(TARGET_SDL2)
   // only handle raw key input without text modifier keys pressed
   if (!checkTextInputKeyModState())
     HandleKey(key, key_status);
-#else
-  HandleKey(key, key_status);
-#endif
 }
 
 void HandleFocusEvent(FocusChangeEvent *event)
@@ -1538,11 +1526,63 @@ void HandleClientMessageEvent(ClientMessageEvent *event)
     CloseAllAndExit(0);
 }
 
-void HandleWindowManagerEvent(Event *event)
+static void HandleDropFileEventExt(char *filename)
 {
-#if defined(TARGET_SDL)
-  SDLHandleWindowManagerEvent(event);
-#endif
+  Error(ERR_DEBUG, "DROP FILE EVENT: '%s'", filename);
+
+  // check and extract dropped zip files into correct user data directory
+  if (strSuffixLower(filename, ".zip"))
+  {
+    int tree_type = GetZipFileTreeType(filename);
+    char *directory = TREE_USERDIR(tree_type);
+
+    if (directory == NULL)
+    {
+      Error(ERR_WARN, "zip file '%s' has invalid content!", filename);
+
+      return;
+    }
+
+    char *top_dir = ExtractZipFileIntoDirectory(filename, directory, tree_type);
+
+    if (top_dir != NULL)
+    {
+      AddUserTreeSetToTreeInfo(top_dir, tree_type);
+
+      // when adding new level set, select it as the new current level set
+      if (tree_type == TREE_TYPE_LEVEL_DIR)
+      {
+       // change current level set to newly added level set from zip file
+       leveldir_current = getTreeInfoFromIdentifier(leveldir_first, top_dir);
+
+       // change current level number to first level of newly added level set
+       level_nr = leveldir_current->first_level;
+
+       // when in main menu, redraw screen to reflect changed level set
+       if (game_status == GAME_MODE_MAIN)
+         DrawMainMenu();
+      }
+    }
+  }
+}
+
+static void HandleDropTextEventExt(char *text)
+{
+  Error(ERR_DEBUG, "DROP TEXT EVENT: '%s'", text);
+}
+
+void HandleDropFileEvent(Event *event)
+{
+  HandleDropFileEventExt(event->drop.file);
+
+  SDL_free(event->drop.file);
+}
+
+void HandleDropTextEvent(Event *event)
+{
+  HandleDropTextEventExt(event->drop.file);
+
+  SDL_free(event->drop.file);
 }
 
 void HandleButton(int mx, int my, int button, int button_nr)
@@ -1567,10 +1607,13 @@ void HandleButton(int mx, int my, int button, int button_nr)
 #if defined(PLATFORM_ANDROID)
   // when playing, only handle gadgets when using "follow finger" controls
   // or when using touch controls in combination with the MM game engine
+  // or when using gadgets that do not overlap with virtual buttons
   handle_gadgets =
     (game_status != GAME_MODE_PLAYING ||
      level.game_engine_type == GAME_ENGINE_TYPE_MM ||
-     strEqual(setup.touch.control_type, TOUCH_CONTROL_FOLLOW_FINGER));
+     strEqual(setup.touch.control_type, TOUCH_CONTROL_FOLLOW_FINGER) ||
+     (strEqual(setup.touch.control_type, TOUCH_CONTROL_VIRTUAL_BUTTONS) &&
+      !virtual_button_pressed));
 #endif
 
   if (HandleGlobalAnimClicks(mx, my, button))
@@ -1778,6 +1821,22 @@ static void HandleKeysSpecial(Key key)
     {
       DumpBrush_Small();
     }
+
+    if (GetKeyModState() & (KMOD_Control | KMOD_Meta))
+    {
+      if (letter == 'x')       // copy brush to clipboard (small size)
+      {
+       CopyBrushToClipboard_Small();
+      }
+      else if (letter == 'c')  // copy brush to clipboard (normal size)
+      {
+       CopyBrushToClipboard();
+      }
+      else if (letter == 'v')  // paste brush from Clipboard
+      {
+       CopyClipboardToBrush();
+      }
+    }
   }
 
   // special key shortcuts for all game modes
@@ -1790,11 +1849,14 @@ static void HandleKeysSpecial(Key key)
   }
 }
 
-void HandleKeysDebug(Key key)
+boolean HandleKeysDebug(Key key, int key_status)
 {
 #ifdef DEBUG
   int i;
 
+  if (key_status != KEY_PRESSED)
+    return FALSE;
+
   if (game_status == GAME_MODE_PLAYING || !setup.debug.frame_delay_game_only)
   {
     boolean mod_key_pressed = ((GetKeyModState() & KMOD_Valid) != KMOD_None);
@@ -1821,7 +1883,7 @@ void HandleKeysDebug(Key key)
        else
          Error(ERR_DEBUG, "frame delay == 0 ms (maximum speed)");
 
-       break;
+       return TRUE;
       }
     }
   }
@@ -1834,14 +1896,20 @@ void HandleKeysDebug(Key key)
 
       Error(ERR_DEBUG, "debug mode %s",
            (options.debug ? "enabled" : "disabled"));
+
+      return TRUE;
     }
     else if (key == KSYM_v)
     {
       Error(ERR_DEBUG, "currently using game engine version %d",
            game.engine_version);
+
+      return TRUE;
     }
   }
 #endif
+
+  return FALSE;
 }
 
 void HandleKey(Key key, int key_status)
@@ -1868,13 +1936,14 @@ void HandleKey(Key key, int key_status)
   int joy = 0;
   int i;
 
-#if defined(TARGET_SDL2)
+  if (HandleKeysDebug(key, key_status))
+    return;            // do not handle already processed keys again
+
   // map special keys (media keys / remote control buttons) to default keys
   if (key == KSYM_PlayPause)
     key = KSYM_space;
   else if (key == KSYM_Select)
     key = KSYM_Return;
-#endif
 
   HandleSpecialGameControllerKeys(key, key_status);
 
@@ -2039,7 +2108,7 @@ void HandleKey(Key key, int key_status)
       return;
   }
 
-  if (game_status == GAME_MODE_PLAYING && AllPlayersGone &&
+  if (game_status == GAME_MODE_PLAYING && game.all_players_gone &&
       (key == KSYM_Return || key == setup.shortcut.toggle_pause))
   {
     GameEnd();
@@ -2095,10 +2164,7 @@ void HandleKey(Key key, int key_status)
   HandleKeysSpecial(key);
 
   if (HandleGadgetsKeyInput(key))
-  {
-    if (key != KSYM_Escape)    // always allow ESC key to be handled
-      key = KSYM_UNDEFINED;
-  }
+    return;            // do not handle already processed keys again
 
   switch (game_status)
   {
@@ -2215,8 +2281,6 @@ void HandleKey(Key key, int key_status)
        return;
       }
   }
-
-  HandleKeysDebug(key);
 }
 
 void HandleNoEvent(void)
@@ -2354,6 +2418,16 @@ void HandleJoystick(void)
     return;
   }
 
+  if (newbutton && (game_status == GAME_MODE_PSEUDO_TYPENAME ||
+                   anyTextGadgetActive()))
+  {
+    // leave name input in main menu or text input gadget
+    HandleKey(KSYM_Escape, KEY_PRESSED);
+    HandleKey(KSYM_Escape, KEY_RELEASED);
+
+    return;
+  }
+
   if (level.game_engine_type == GAME_ENGINE_TYPE_MM)
   {
     if (game_status == GAME_MODE_PLAYING)
@@ -2425,7 +2499,7 @@ void HandleJoystick(void)
        newbutton = ((joy & JOY_BUTTON) != 0);
 #endif
 
-      if (newbutton && AllPlayersGone)
+      if (newbutton && game.all_players_gone)
       {
        GameEnd();
 
@@ -2455,7 +2529,6 @@ void HandleJoystick(void)
 
 void HandleSpecialGameControllerButtons(Event *event)
 {
-#if defined(TARGET_SDL2)
   int key_status;
   Key key;
 
@@ -2488,12 +2561,10 @@ void HandleSpecialGameControllerButtons(Event *event)
   }
 
   HandleKey(key, key_status);
-#endif
 }
 
 void HandleSpecialGameControllerKeys(Key key, int key_status)
 {
-#if defined(TARGET_SDL2)
 #if defined(KSYM_Rewind) && defined(KSYM_FastForward)
   int button = SDL_CONTROLLER_BUTTON_INVALID;
 
@@ -2518,7 +2589,6 @@ void HandleSpecialGameControllerKeys(Key key, int key_status)
     HandleJoystickEvent(&event);
   }
 #endif
-#endif
 }
 
 boolean DoKeysymAction(int keysym)