changed click events for global animations to ignore 'consumed' clicks
[rocksndiamonds.git] / src / anim.c
index b9ea8becc2a44d2b84ea4e32e8424a95e96a7e87..2ef7872558b841ff080a4ee6902b6e2b13d54096 100644 (file)
@@ -9,6 +9,8 @@
 // anim.c
 // ============================================================================
 
+#include "libgame/libgame.h"
+
 #include "anim.h"
 #include "main.h"
 #include "tools.h"
@@ -90,6 +92,12 @@ struct GlobalAnimPartControlInfo
   int anim_delay_counter;
   int post_delay_counter;
 
+  boolean init_event_state;
+  boolean anim_event_state;
+
+  boolean clickable;
+  boolean clicked;
+
   int drawing_stage;
 
   int state;
@@ -173,6 +181,8 @@ struct AnimClassGameMode
 /* forward declaration for internal use */
 static void HandleGlobalAnim(int, int);
 static void DoAnimationExt(void);
+static void ResetGlobalAnim_Clickable();
+static void ResetGlobalAnim_Clicked();
 
 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_GAME_MODES];
 
@@ -728,7 +738,13 @@ void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
 
 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
 {
+  if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
+    ResetGlobalAnim_Clickable();
+
   DrawGlobalAnimationsExt(drawing_target, drawing_stage);
+
+  if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
+    ResetGlobalAnim_Clicked();
 }
 
 boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
@@ -881,6 +897,44 @@ static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
   StopGlobalAnimMusic(part);
 }
 
+static boolean isClickablePart(struct GlobalAnimPartControlInfo *part)
+{
+  struct GraphicInfo *c = &part->control_info;
+
+  return (c->init_event & ANIM_EVENT_CLICK ||
+         c->anim_event & ANIM_EVENT_CLICK);
+}
+
+static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
+                            int mx, int my, boolean clicked)
+{
+  struct GraphicInfo *g = &part->graphic_info;
+  int part_x = part->viewport_x + part->x;
+  int part_y = part->viewport_y + part->y;
+  int part_width  = g->width;
+  int part_height = g->height;
+
+  // check if mouse click was detected at all
+  if (!clicked)
+    return FALSE;
+
+  // check if mouse click is inside the animation part's viewport
+  if (mx <  part->viewport_x ||
+      mx >= part->viewport_x + part->viewport_width ||
+      my <  part->viewport_y ||
+      my >= part->viewport_y + part->viewport_height)
+    return FALSE;
+
+  // check if mouse click is inside the animation part's graphic
+  if (mx <  part_x ||
+      mx >= part_x + part_width ||
+      my <  part_y ||
+      my >= part_y + part_height)
+    return FALSE;
+
+  return TRUE;
+}
+
 int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
 {
   struct GraphicInfo *g = &part->graphic_info;
@@ -905,6 +959,9 @@ int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
     part->anim_delay_counter =
       (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
 
+    part->init_event_state = c->init_event;
+    part->anim_event_state = c->anim_event;
+
     part->initial_anim_sync_frame =
       (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
 
@@ -981,26 +1038,60 @@ int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
     if (c->step_yoffset != ARG_UNDEFINED_VALUE)
       part->step_yoffset = c->step_yoffset;
 
-    if (part->init_delay_counter == 0)
+    if (part->init_delay_counter == 0 &&
+       part->init_event_state == ANIM_EVENT_NONE)
       PlayGlobalAnimSoundAndMusic(part);
   }
 
+  part->clickable = isClickablePart(part);
+
+  if (part->clicked &&
+      part->init_event_state != ANIM_EVENT_NONE)
+  {
+    if (part->initial_anim_sync_frame > 0)
+      part->initial_anim_sync_frame -= part->init_delay_counter - 1;
+
+    part->init_delay_counter = 1;
+    part->init_event_state = ANIM_EVENT_NONE;
+
+    part->clicked = FALSE;
+  }
+
+  if (part->clicked &&
+      part->anim_event_state != ANIM_EVENT_NONE)
+  {
+    part->anim_delay_counter = 1;
+    part->anim_event_state = ANIM_EVENT_NONE;
+
+    part->clicked = FALSE;
+  }
+
   if (part->init_delay_counter > 0)
   {
     part->init_delay_counter--;
 
     if (part->init_delay_counter == 0)
+    {
+      part->init_event_state = ANIM_EVENT_NONE;
+
       PlayGlobalAnimSoundAndMusic(part);
+    }
 
     return ANIM_STATE_WAITING;
   }
 
+  if (part->init_event_state != ANIM_EVENT_NONE)
+    return ANIM_STATE_WAITING;
+
   // check if moving animation has left the visible screen area
   if ((part->x <= -g->width              && part->step_xoffset <= 0) ||
       (part->x >=  part->viewport_width  && part->step_xoffset >= 0) ||
       (part->y <= -g->height             && part->step_yoffset <= 0) ||
       (part->y >=  part->viewport_height && part->step_yoffset >= 0))
   {
+    // do not wait for "anim" events for off-screen animations
+    part->anim_event_state = ANIM_EVENT_NONE;
+
     // do not stop animation before "anim" or "post" counter are finished
     if (part->anim_delay_counter == 0 &&
        part->post_delay_counter == 0)
@@ -1024,6 +1115,8 @@ int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
 
     if (part->anim_delay_counter == 0)
     {
+      part->anim_event_state = ANIM_EVENT_NONE;
+
       StopGlobalAnimSoundAndMusic(part);
 
       part->post_delay_counter =
@@ -1263,3 +1356,92 @@ static void DoAnimationExt()
   redraw_mask = REDRAW_ALL;
 #endif
 }
+
+static void InitGlobalAnim_Clickable()
+{
+  int mode_nr;
+
+  for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
+  {
+    struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
+    int anim_nr;
+
+    for (anim_nr = 0; anim_nr < NUM_GLOBAL_ANIMS_AND_TOONS; anim_nr++)
+    {
+      struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
+      int part_nr;
+
+      for (part_nr = 0; part_nr < NUM_GLOBAL_ANIM_PARTS_AND_TOONS; part_nr++)
+      {
+       struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
+
+       part->clickable = FALSE;
+      }
+    }
+  }
+}
+
+static boolean InitGlobalAnim_Clicked(int mx, int my, boolean clicked)
+{
+  boolean any_part_clicked = FALSE;
+  int mode_nr;
+
+  for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
+  {
+    struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
+    int anim_nr;
+
+    for (anim_nr = 0; anim_nr < NUM_GLOBAL_ANIMS_AND_TOONS; anim_nr++)
+    {
+      struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
+      int part_nr;
+
+      for (part_nr = 0; part_nr < NUM_GLOBAL_ANIM_PARTS_AND_TOONS; part_nr++)
+      {
+       struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
+
+       part->clicked = FALSE;
+
+       if (part->clickable && isClickedPart(part, mx, my, clicked))
+         any_part_clicked = part->clicked = TRUE;
+      }
+    }
+  }
+
+  return any_part_clicked;
+}
+
+static void ResetGlobalAnim_Clickable()
+{
+  InitGlobalAnim_Clickable();
+}
+
+static void ResetGlobalAnim_Clicked()
+{
+  InitGlobalAnim_Clicked(-1, -1, FALSE);
+}
+
+boolean HandleGlobalAnimClicks(int mx, int my, int button)
+{
+  static boolean click_consumed = FALSE;
+  static int last_button = 0;
+  boolean press_event;
+  boolean release_event;
+  boolean click_consumed_current = click_consumed;
+
+  /* check if button state has changed since last invocation */
+  press_event   = (button != 0 && last_button == 0);
+  release_event = (button == 0 && last_button != 0);
+  last_button = button;
+
+  if (press_event)
+  {
+    click_consumed = InitGlobalAnim_Clicked(mx, my, TRUE);
+    click_consumed_current = click_consumed;
+  }
+
+  if (release_event)
+    click_consumed = FALSE;
+
+  return click_consumed_current;
+}