added mouse click events to CE change events (experimental)
authorHolger Schemel <info@artsoft.org>
Fri, 17 Apr 2020 22:14:20 +0000 (00:14 +0200)
committerHolger Schemel <info@artsoft.org>
Tue, 19 May 2020 16:21:18 +0000 (18:21 +0200)
This addition is sort of fundamental to the R'n'D game engine in that
it extends the previous limitation of game engine input to only four
direction and two buttons, now also handling mouse clicks to elements
in the playfield as configurable custom element change events.

In contrast to the Mirror Magic game engine (which already handles
mouse input), recording mouse click events to tapes still has to be
implemented.

src/editor.c
src/events.c
src/files.c
src/game.c
src/main.h

index f648853f516b3aa5ce237d349047d8611c6ca543..838a898daaaa6170505afd35893051939941cbf2 100644 (file)
@@ -1965,6 +1965,10 @@ static struct ValueTextInfo options_change_direct_action[] =
 #endif
   { CE_VALUE_GETS_ZERO,                "CE value gets 0"               },
   { CE_SCORE_GETS_ZERO,                "CE score gets 0"               },
+  { CE_UNDEFINED,              " "                             },
+  { CE_HEADLINE_SPECIAL_EVENTS,        "[mouse events]"                },
+  { CE_CLICKED_BY_MOUSE,       "clicked by mouse"              },
+  { CE_PRESSED_BY_MOUSE,       "pressed by mouse"              },
 
   { -1,                                NULL                            }
 };
@@ -1994,6 +1998,10 @@ static struct ValueTextInfo options_change_other_action[] =
   { CE_SCORE_CHANGES_OF_X,     "CE score changes of"           },
   { CE_VALUE_GETS_ZERO_OF_X,   "CE value gets 0 of"            },
   { CE_SCORE_GETS_ZERO_OF_X,   "CE score gets 0 of"            },
+  { CE_UNDEFINED,              " "                             },
+  { CE_HEADLINE_SPECIAL_EVENTS,        "[mouse events]"                },
+  { CE_MOUSE_CLICKED_ON_X,     "mouse clicked on"              },
+  { CE_MOUSE_PRESSED_ON_X,     "mouse pressed on"              },
 
   { -1,                                NULL                            }
 };
@@ -8144,6 +8152,8 @@ static void CopyCustomElementPropertiesToEditor(int element)
      HAS_CHANGE_EVENT(element, CE_SCORE_CHANGES) ? CE_SCORE_CHANGES :
      HAS_CHANGE_EVENT(element, CE_VALUE_GETS_ZERO) ? CE_VALUE_GETS_ZERO :
      HAS_CHANGE_EVENT(element, CE_SCORE_GETS_ZERO) ? CE_SCORE_GETS_ZERO :
+     HAS_CHANGE_EVENT(element, CE_CLICKED_BY_MOUSE) ? CE_CLICKED_BY_MOUSE :
+     HAS_CHANGE_EVENT(element, CE_PRESSED_BY_MOUSE) ? CE_PRESSED_BY_MOUSE :
      custom_element_change.direct_action);
 
   // set "change by other element action" selectbox help value
@@ -8171,6 +8181,8 @@ static void CopyCustomElementPropertiesToEditor(int element)
      HAS_CHANGE_EVENT(element, CE_SCORE_CHANGES_OF_X) ? CE_SCORE_CHANGES_OF_X :
      HAS_CHANGE_EVENT(element, CE_VALUE_GETS_ZERO_OF_X) ? CE_VALUE_GETS_ZERO_OF_X :
      HAS_CHANGE_EVENT(element, CE_SCORE_GETS_ZERO_OF_X) ? CE_SCORE_GETS_ZERO_OF_X :
+     HAS_CHANGE_EVENT(element, CE_MOUSE_CLICKED_ON_X) ? CE_MOUSE_CLICKED_ON_X :
+     HAS_CHANGE_EVENT(element, CE_MOUSE_PRESSED_ON_X) ? CE_MOUSE_PRESSED_ON_X :
      custom_element_change.other_action);
 }
 
@@ -8302,6 +8314,8 @@ static void CopyCustomElementPropertiesToGame(int element)
   custom_element_change_events[CE_SCORE_CHANGES] = FALSE;
   custom_element_change_events[CE_VALUE_GETS_ZERO] = FALSE;
   custom_element_change_events[CE_SCORE_GETS_ZERO] = FALSE;
+  custom_element_change_events[CE_CLICKED_BY_MOUSE] = FALSE;
+  custom_element_change_events[CE_PRESSED_BY_MOUSE] = FALSE;
   custom_element_change_events[custom_element_change.direct_action] =
     custom_element_change_events[CE_BY_DIRECT_ACTION];
 
@@ -8329,6 +8343,8 @@ static void CopyCustomElementPropertiesToGame(int element)
   custom_element_change_events[CE_SCORE_CHANGES_OF_X] = FALSE;
   custom_element_change_events[CE_VALUE_GETS_ZERO_OF_X] = FALSE;
   custom_element_change_events[CE_SCORE_GETS_ZERO_OF_X] = FALSE;
+  custom_element_change_events[CE_MOUSE_CLICKED_ON_X] = FALSE;
+  custom_element_change_events[CE_MOUSE_PRESSED_ON_X] = FALSE;
   custom_element_change_events[custom_element_change.other_action] =
     custom_element_change_events[CE_BY_OTHER_ACTION];
 
index 6def0cb60992c285be6837ad94a877c4a6a0a303..2b55372054f61a8479e66ccb911cbb7ed77dae75 100644 (file)
@@ -94,6 +94,25 @@ static int FilterEvents(const Event *event)
     ((MotionEvent *)event)->y -= video.screen_yoffset;
   }
 
+  if (event->type == EVENT_BUTTONPRESS ||
+      event->type == EVENT_BUTTONRELEASE ||
+      event->type == EVENT_MOTIONNOTIFY)
+  {
+    // do not reset mouse cursor before all pending events have been processed
+    if (gfx.cursor_mode == cursor_mode_last &&
+       ((game_status == GAME_MODE_TITLE &&
+         gfx.cursor_mode == CURSOR_NONE) ||
+        (game_status == GAME_MODE_PLAYING &&
+         gfx.cursor_mode == CURSOR_PLAYFIELD)))
+    {
+      SetMouseCursor(CURSOR_DEFAULT);
+
+      DelayReached(&special_cursor_delay, 0);
+
+      cursor_mode_last = CURSOR_DEFAULT;
+    }
+  }
+
   // non-motion events are directly passed to event handler functions
   if (event->type != EVENT_MOTIONNOTIFY)
     return 1;
@@ -111,20 +130,6 @@ static int FilterEvents(const Event *event)
   gfx.mouse_x = motion->x;
   gfx.mouse_y = motion->y;
 
-  // do no reset mouse cursor before all pending events have been processed
-  if (gfx.cursor_mode == cursor_mode_last &&
-      ((game_status == GAME_MODE_TITLE &&
-       gfx.cursor_mode == CURSOR_NONE) ||
-       (game_status == GAME_MODE_PLAYING &&
-       gfx.cursor_mode == CURSOR_PLAYFIELD)))
-  {
-    SetMouseCursor(CURSOR_DEFAULT);
-
-    DelayReached(&special_cursor_delay, 0);
-
-    cursor_mode_last = CURSOR_DEFAULT;
-  }
-
   // skip mouse motion events without pressed button outside level editor
   if (button_status == MB_RELEASED &&
       game_status != GAME_MODE_EDITOR && game_status != GAME_MODE_PLAYING)
@@ -322,6 +327,10 @@ static void HandleMouseCursor(void)
   {
     // when playing, display a special mouse pointer inside the playfield
 
+    // display normal pointer if mouse pressed
+    if (button_status != MB_RELEASED)
+      DelayReached(&special_cursor_delay, 0);
+
     if (gfx.cursor_mode != CURSOR_PLAYFIELD &&
        cursor_inside_playfield &&
        DelayReached(&special_cursor_delay, special_cursor_delay_value))
@@ -1348,6 +1357,8 @@ static void HandleButtonOrFinger_FollowFinger(int mx, int my, int button)
 
 static void HandleButtonOrFinger(int mx, int my, int button)
 {
+  boolean valid_mouse_event = (mx != -1 && my != -1 && button != -1);
+
   if (game_status != GAME_MODE_PLAYING)
     return;
 
@@ -1364,6 +1375,8 @@ static void HandleButtonOrFinger(int mx, int my, int button)
   {
     if (strEqual(setup.touch.control_type, TOUCH_CONTROL_FOLLOW_FINGER))
       HandleButtonOrFinger_FollowFinger(mx, my, button);
+    else if (level.has_mouse_events && valid_mouse_event)
+      SetPlayerMouseAction(mx, my, button);
   }
 }
 
index 54835e281f2136a93ba98abcf09ad7b074e122c8..fa807e8f323dd0161959467fb6553ca5a703da52 100644 (file)
@@ -1733,6 +1733,9 @@ static void setLevelInfoToDefaults_Level(struct LevelInfo *level)
   // set all bug compatibility flags to "false" => do not emulate this bug
   level->use_action_after_change_bug = FALSE;
 
+  // other flags that may be set due to certain level properties
+  level->has_mouse_events = FALSE;
+
   if (leveldir_current)
   {
     // try to determine better author name than 'anonymous'
@@ -6561,6 +6564,18 @@ static void LoadLevel_InitCustomElements(struct LevelInfo *level)
       element_info[element].ignition_delay = 8;
     }
   }
+
+  // check for custom elements which have mouse click events defined
+  for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
+  {
+    int element = EL_CUSTOM_START + i;
+
+    if (HAS_CHANGE_EVENT(element, CE_CLICKED_BY_MOUSE) ||
+       HAS_CHANGE_EVENT(element, CE_PRESSED_BY_MOUSE) ||
+       HAS_CHANGE_EVENT(element, CE_MOUSE_CLICKED_ON_X) ||
+       HAS_CHANGE_EVENT(element, CE_MOUSE_PRESSED_ON_X))
+      level->has_mouse_events = TRUE;
+  }
 }
 
 static void LoadLevel_InitElements(struct LevelInfo *level)
index a82ca0798b9e1dacd6c368dd896d86220d182413..fd1bdc53d936c6a821c8639b66a331d09ccd6750 100644 (file)
@@ -11687,6 +11687,8 @@ void GameActions_RND_Main(void)
 
 void GameActions_RND(void)
 {
+  static struct MouseActionInfo mouse_action_last = { 0 };
+  struct MouseActionInfo mouse_action = local_player->effective_mouse_action;
   int magic_wall_x = 0, magic_wall_y = 0;
   int i, x, y, element, graphic, last_gfx_frame;
 
@@ -11873,6 +11875,24 @@ void GameActions_RND(void)
 #endif
   }
 
+  if (mouse_action.button)
+  {
+    int new_button = (mouse_action.button && mouse_action_last.button == 0);
+
+    x = local_player->mouse_action.lx;
+    y = local_player->mouse_action.ly;
+    element = Feld[x][y];
+
+    if (new_button)
+    {
+      CheckElementChange(x, y, element, EL_UNDEFINED, CE_CLICKED_BY_MOUSE);
+      CheckTriggeredElementChange(x, y, element, CE_MOUSE_CLICKED_ON_X);
+    }
+
+    CheckElementChange(x, y, element, EL_UNDEFINED, CE_PRESSED_BY_MOUSE);
+    CheckTriggeredElementChange(x, y, element, CE_MOUSE_PRESSED_ON_X);
+  }
+
   SCAN_PLAYFIELD(x, y)
   {
     element = Feld[x][y];
@@ -12214,6 +12234,8 @@ void GameActions_RND(void)
   // use random number generator in every frame to make it less predictable
   if (game.engine_version >= VERSION_IDENT(3,1,1,0))
     RND(1);
+
+  mouse_action_last = mouse_action;
 }
 
 static boolean AllPlayersInSight(struct PlayerInfo *player, int x, int y)
index bca6d626a04333621f401470048cd1088afe9603..41af237ffb501ad04bde6d07b49cfa6ea4ee2a30 100644 (file)
 #define CE_VALUE_CHANGES_OF_X          41
 #define CE_SCORE_CHANGES               42
 #define CE_SCORE_CHANGES_OF_X          43
+#define CE_CLICKED_BY_MOUSE            44
+#define CE_PRESSED_BY_MOUSE            45
+#define CE_MOUSE_CLICKED_ON_X          46
+#define CE_MOUSE_PRESSED_ON_X          47
 
-#define NUM_CHANGE_EVENTS              44
+#define NUM_CHANGE_EVENTS              48
 
 #define NUM_CE_BITFIELDS               ((NUM_CHANGE_EVENTS + 31) / 32)
 
+#define CE_HEADLINE_SPECIAL_EVENTS     250
+#define CE_UNDEFINED                   255
+
 #define CE_BITMASK_DEFAULT             0
 
 #define CH_EVENT_BITFIELD_NR(e)                (e / 32)
@@ -3187,6 +3194,9 @@ struct LevelInfo
 
   // runtime flags to handle bugs in old levels (not stored in level file)
   boolean use_action_after_change_bug;
+
+  // runtime flags to indicate level properties (not stored in level file)
+  boolean has_mouse_events;
 };
 
 struct NetworkLevelInfo