fixed setting global animation sync frame when started with ".init_event"
[rocksndiamonds.git] / src / anim.c
index dec9d18ee61d49cf1667e61298111e2c9f9060d0..d570be06ae39e8adb013f78a3f3439d3a1595d5f 100644 (file)
 #define ANIM_CLASS_BIT_TITLE_INITIAL   0
 #define ANIM_CLASS_BIT_TITLE           1
 #define ANIM_CLASS_BIT_MAIN            2
-#define ANIM_CLASS_BIT_SCORES          3
-#define ANIM_CLASS_BIT_SUBMENU         4
-#define ANIM_CLASS_BIT_MENU            5
-#define ANIM_CLASS_BIT_TOONS           6
-#define ANIM_CLASS_BIT_NO_TITLE                7
+#define ANIM_CLASS_BIT_NAMES           3
+#define ANIM_CLASS_BIT_SCORES          4
+#define ANIM_CLASS_BIT_SCORESONLY      5
+#define ANIM_CLASS_BIT_SUBMENU         6
+#define ANIM_CLASS_BIT_MENU            7
+#define ANIM_CLASS_BIT_TOONS           8
+#define ANIM_CLASS_BIT_NO_TITLE                9
 
-#define NUM_ANIM_CLASSES               8
+#define NUM_ANIM_CLASSES               10
 
 #define ANIM_CLASS_NONE                        0
 #define ANIM_CLASS_TITLE_INITIAL       (1 << ANIM_CLASS_BIT_TITLE_INITIAL)
 #define ANIM_CLASS_TITLE               (1 << ANIM_CLASS_BIT_TITLE)
 #define ANIM_CLASS_MAIN                        (1 << ANIM_CLASS_BIT_MAIN)
+#define ANIM_CLASS_NAMES               (1 << ANIM_CLASS_BIT_NAMES)
 #define ANIM_CLASS_SCORES              (1 << ANIM_CLASS_BIT_SCORES)
+#define ANIM_CLASS_SCORESONLY          (1 << ANIM_CLASS_BIT_SCORESONLY)
 #define ANIM_CLASS_SUBMENU             (1 << ANIM_CLASS_BIT_SUBMENU)
 #define ANIM_CLASS_MENU                        (1 << ANIM_CLASS_BIT_MENU)
 #define ANIM_CLASS_TOONS               (1 << ANIM_CLASS_BIT_TOONS)
                                         ANIM_CLASS_SCORES      |       \
                                         ANIM_CLASS_NO_TITLE)
 
+#define ANIM_CLASS_TOONS_SCORESONLY    (ANIM_CLASS_TOONS       |       \
+                                        ANIM_CLASS_SCORES      |       \
+                                        ANIM_CLASS_SCORESONLY  |       \
+                                        ANIM_CLASS_NO_TITLE)
+
 #define ANIM_CLASS_TOONS_MENU_MAIN     (ANIM_CLASS_TOONS       |       \
                                         ANIM_CLASS_MENU        |       \
                                         ANIM_CLASS_MAIN        |       \
                                         ANIM_CLASS_SUBMENU     |       \
                                         ANIM_CLASS_NO_TITLE)
 
+#define ANIM_CLASS_TOONS_MENU_SUBMENU_2        (ANIM_CLASS_TOONS       |       \
+                                        ANIM_CLASS_MENU        |       \
+                                        ANIM_CLASS_SUBMENU     |       \
+                                        ANIM_CLASS_NAMES       |       \
+                                        ANIM_CLASS_NO_TITLE)
+
 // values for global animation states
 #define ANIM_STATE_INACTIVE            0
 #define ANIM_STATE_RESTART             (1 << 0)
@@ -109,7 +124,8 @@ struct GlobalAnimPartControlInfo
 
   unsigned int initial_anim_sync_frame;
   unsigned int anim_random_frame;
-  unsigned int step_delay, step_delay_value;
+
+  DelayCounter step_delay;
 
   int init_delay_counter;
   int anim_delay_counter;
@@ -178,14 +194,18 @@ struct GameModeAnimClass
   { GAME_MODE_TITLE_3,                 ANIM_CLASS_TITLE                },
   { GAME_MODE_TITLE_4,                 ANIM_CLASS_TITLE                },
   { GAME_MODE_TITLE_5,                 ANIM_CLASS_TITLE                },
+  { GAME_MODE_NAMES,                   ANIM_CLASS_TOONS_MENU_SUBMENU   },
   { GAME_MODE_LEVELS,                  ANIM_CLASS_TOONS_MENU_SUBMENU   },
   { GAME_MODE_LEVELNR,                 ANIM_CLASS_TOONS_MENU_SUBMENU   },
   { GAME_MODE_INFO,                    ANIM_CLASS_TOONS_MENU_SUBMENU   },
   { GAME_MODE_SETUP,                   ANIM_CLASS_TOONS_MENU_SUBMENU   },
+  { GAME_MODE_PSEUDO_NAMESONLY,                ANIM_CLASS_TOONS_MENU_SUBMENU_2 },
+  { GAME_MODE_PSEUDO_TYPENAMES,                ANIM_CLASS_TOONS_MENU_SUBMENU_2 },
   { GAME_MODE_PSEUDO_MAINONLY,         ANIM_CLASS_TOONS_MENU_MAIN      },
   { GAME_MODE_PSEUDO_TYPENAME,         ANIM_CLASS_TOONS_MENU_MAIN      },
-  { GAME_MODE_PSEUDO_SCORESOLD,                ANIM_CLASS_TOONS_SCORES         },
-  { GAME_MODE_PSEUDO_SCORESNEW,                ANIM_CLASS_TOONS_SCORES         },
+  { GAME_MODE_PSEUDO_SCORESOLD,                ANIM_CLASS_TOONS_SCORESONLY     },
+  { GAME_MODE_PSEUDO_SCORESNEW,                ANIM_CLASS_TOONS_SCORESONLY     },
+  { GAME_MODE_SCOREINFO,               ANIM_CLASS_TOONS_SCORES         },
   { GAME_MODE_EDITOR,                  ANIM_CLASS_NO_TITLE             },
   { GAME_MODE_PLAYING,                 ANIM_CLASS_NO_TITLE             },
 
@@ -201,7 +221,9 @@ struct AnimClassGameMode
   { ANIM_CLASS_BIT_TITLE_INITIAL,      GAME_MODE_TITLE_INITIAL         },
   { ANIM_CLASS_BIT_TITLE,              GAME_MODE_TITLE                 },
   { ANIM_CLASS_BIT_MAIN,               GAME_MODE_MAIN                  },
+  { ANIM_CLASS_BIT_NAMES,              GAME_MODE_NAMES                 },
   { ANIM_CLASS_BIT_SCORES,             GAME_MODE_SCORES                },
+  { ANIM_CLASS_BIT_SCORESONLY,         GAME_MODE_PSEUDO_SCORESONLY     },
   { ANIM_CLASS_BIT_SUBMENU,            GAME_MODE_PSEUDO_SUBMENU        },
   { ANIM_CLASS_BIT_MENU,               GAME_MODE_PSEUDO_MENU           },
   { ANIM_CLASS_BIT_TOONS,              GAME_MODE_PSEUDO_TOONS          },
@@ -389,7 +411,8 @@ static void InitToonControls(void)
     int sound = SND_UNDEFINED;
     int music = MUS_UNDEFINED;
     int graphic = IMG_TOON_1 + i;
-    int control = graphic;
+
+    control = graphic;
 
     part->nr = part_nr;
     part->anim_nr = anim_nr;
@@ -415,8 +438,8 @@ static void InitToonControls(void)
     part->initial_anim_sync_frame = 0;
     part->anim_random_frame = -1;
 
-    part->step_delay = 0;
-    part->step_delay_value = graphic_info[control].step_delay;
+    part->step_delay.count = 0;
+    part->step_delay.value = graphic_info[control].step_delay;
 
     part->state = ANIM_STATE_INACTIVE;
     part->last_anim_status = -1;
@@ -494,13 +517,15 @@ static void InitGlobalAnimControls(void)
          continue;
 
 #if 0
-       printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
-              m, a, p, mode_nr, anim_nr, part_nr, control);
+       Debug("anim:InitGlobalAnimControls",
+             "mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]",
+             m, a, p, mode_nr, anim_nr, part_nr, control);
 #endif
 
 #if 0
-       printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
-              m, a, p, mode_nr, anim_nr, part_nr, sound);
+       Debug("anim:InitGlobalAnimControls",
+             "mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]",
+             m, a, p, mode_nr, anim_nr, part_nr, sound);
 #endif
 
        part->old_nr = p;
@@ -520,8 +545,8 @@ static void InitGlobalAnimControls(void)
        part->initial_anim_sync_frame = 0;
        part->anim_random_frame = -1;
 
-       part->step_delay = 0;
-       part->step_delay_value = graphic_info[control].step_delay;
+       part->step_delay.count = 0;
+       part->step_delay.value = graphic_info[control].step_delay;
 
        part->state = ANIM_STATE_INACTIVE;
        part->last_anim_status = -1;
@@ -606,16 +631,135 @@ static void InitGlobalAnimControls(void)
   anim_classes_last = ANIM_CLASS_NONE;
 }
 
+static void SetGlobalAnimEventsForCustomElements(int list_pos)
+{
+  int num_events = GetGlobalAnimEventValueCount(list_pos);
+  int i;
+
+  for (i = 0; i < num_events; i++)
+  {
+    int event = GetGlobalAnimEventValue(list_pos, i);
+
+    if (event & ANIM_EVENT_CE_CHANGE)
+    {
+      int nr = (event >> ANIM_EVENT_CE_BIT) & 0xff;
+
+      if (nr >= 0 && nr < NUM_CUSTOM_ELEMENTS)
+       element_info[EL_CUSTOM_START + nr].has_anim_event = TRUE;
+    }
+  }
+}
+
+void InitGlobalAnimEventsForCustomElements(void)
+{
+  int m, a, p;
+  int control;
+
+  // custom element events for global animations only relevant while playing
+  m = GAME_MODE_PLAYING;
+
+  for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
+  {
+    int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
+
+    control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
+
+    // if no base animation parameters defined, use default values
+    if (control == IMG_UNDEFINED)
+      control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
+
+    SetGlobalAnimEventsForCustomElements(graphic_info[control].init_event);
+    SetGlobalAnimEventsForCustomElements(graphic_info[control].anim_event);
+
+    for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
+    {
+      control = global_anim_info[ctrl_id].graphic[p][m];
+
+      if (control == IMG_UNDEFINED)
+       continue;
+
+      SetGlobalAnimEventsForCustomElements(graphic_info[control].init_event);
+      SetGlobalAnimEventsForCustomElements(graphic_info[control].anim_event);
+    }
+  }
+}
+
 void InitGlobalAnimations(void)
 {
   InitGlobalAnimControls();
 }
 
-static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
+static void BlitGlobalAnimation(struct GlobalAnimPartControlInfo *part,
+                               Bitmap *src_bitmap, int src_x0, int src_y0,
+                               int drawing_target)
 {
+  struct GraphicInfo *g = &part->graphic_info;
+  struct GraphicInfo *c = &part->control_info;
+  void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
+    (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
+  void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
+    (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
   Bitmap *fade_bitmap =
     (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
      drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
+  int x, y;
+
+  for (y = 0; y < c->stacked_yfactor; y++)
+  {
+    for (x = 0; x < c->stacked_xfactor; x++)
+    {
+      int src_x = src_x0;
+      int src_y = src_y0;
+      int dst_x = part->x + x * (g->width  + c->stacked_xoffset);
+      int dst_y = part->y + y * (g->height + c->stacked_yoffset);
+      int cut_x = 0;
+      int cut_y = 0;
+      int width  = g->width;
+      int height = g->height;
+
+      if (dst_x < 0)
+      {
+       width += dst_x;
+       cut_x = -dst_x;
+       dst_x = 0;
+      }
+      else if (dst_x > part->viewport_width - g->width)
+      {
+       width -= (dst_x - (part->viewport_width - g->width));
+      }
+
+      if (dst_y < 0)
+      {
+       height += dst_y;
+       cut_y  = -dst_y;
+       dst_y = 0;
+      }
+      else if (dst_y > part->viewport_height - g->height)
+      {
+       height -= (dst_y - (part->viewport_height - g->height));
+      }
+
+      if (width <= 0 || height <= 0)
+       continue;
+
+      src_x += cut_x;
+      src_y += cut_y;
+
+      dst_x += part->viewport_x;
+      dst_y += part->viewport_y;
+
+      if (drawing_target == DRAW_TO_SCREEN)
+       blit_screen(src_bitmap, src_x, src_y, width, height,
+                   dst_x, dst_y);
+      else
+       blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
+                   dst_x, dst_y);
+    }
+  }
+}
+
+static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
+{
   int game_mode_anim_action[NUM_GAME_MODES];
   int mode_nr;
 
@@ -640,9 +784,11 @@ static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
     if (drawing_target == DRAW_TO_FADE_TARGET)
       after_fading = TRUE;
 
-    // special case: changing from/to this screen is done without fading
-    if (global.anim_status == GAME_MODE_PSEUDO_TYPENAME ||
-       anim_status_last   == GAME_MODE_PSEUDO_TYPENAME)
+    // special case: changing from/to these screens is done without fading
+    if (global.anim_status == GAME_MODE_PSEUDO_TYPENAME  ||
+       global.anim_status == GAME_MODE_PSEUDO_TYPENAMES ||
+       anim_status_last   == GAME_MODE_PSEUDO_TYPENAME  ||
+       anim_status_last   == GAME_MODE_PSEUDO_TYPENAMES)
       after_fading = TRUE;
 
     // ---------- part 1 ------------------------------------------------------
@@ -758,18 +904,8 @@ static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
        struct GraphicInfo *g = &part->graphic_info;
        Bitmap *src_bitmap;
        int src_x, src_y;
-       int width  = g->width;
-       int height = g->height;
-       int dst_x = part->x;
-       int dst_y = part->y;
-       int cut_x = 0;
-       int cut_y = 0;
        int sync_frame;
        int frame;
-       void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
-         (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
-       void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
-         (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
        int last_anim_random_frame = gfx.anim_random_frame;
 
        if (!(part->state & ANIM_STATE_RUNNING))
@@ -778,30 +914,6 @@ static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
        if (part->drawing_stage != drawing_stage)
          continue;
 
-       if (part->x < 0)
-       {
-         dst_x = 0;
-         width += part->x;
-         cut_x = -part->x;
-       }
-       else if (part->x > part->viewport_width - g->width)
-         width -= (part->x - (part->viewport_width - g->width));
-
-       if (part->y < 0)
-       {
-         dst_y = 0;
-         height += part->y;
-         cut_y = -part->y;
-       }
-       else if (part->y > part->viewport_height - g->height)
-         height -= (part->y - (part->viewport_height - g->height));
-
-       if (width <= 0 || height <= 0)
-         continue;
-
-       dst_x += part->viewport_x;
-       dst_y += part->viewport_y;
-
        sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
 
        // re-initialize random animation frame after animation delay
@@ -818,18 +930,10 @@ static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
 
        gfx.anim_random_frame = last_anim_random_frame;
 
-       getFixedGraphicSource(part->graphic, frame, &src_bitmap,
-                             &src_x, &src_y);
-
-       src_x += cut_x;
-       src_y += cut_y;
+       getGlobalAnimGraphicSource(part->graphic, frame, &src_bitmap,
+                                  &src_x, &src_y);
 
-       if (drawing_target == DRAW_TO_SCREEN)
-         blit_screen(src_bitmap, src_x, src_y, width, height,
-                     dst_x, dst_y);
-       else
-         blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
-                     dst_x, dst_y);
+       BlitGlobalAnimation(part, src_bitmap, src_x, src_y, drawing_target);
       }
     }
   }
@@ -863,6 +967,8 @@ void DrawGlobalAnimations(int drawing_target, int drawing_stage)
     ResetGlobalAnim_Clicked();
   }
 
+  DrawEnvelopeRequestToScreen(drawing_target, drawing_stage);
+
   if (gfx.cursor_mode_override != last_cursor_mode_override)
     SetMouseCursor(gfx.cursor_mode);
 }
@@ -908,7 +1014,7 @@ static boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part
     viewport_width  = part->graphic_info.width;
     viewport_height = part->graphic_info.height;
 
-    part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
+    part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_3;
 
     // do not use global animation mouse pointer when reloading artwork
     if (global.anim_status != GAME_MODE_LOADING)
@@ -981,7 +1087,7 @@ static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
     PlaySound(sound);
 
 #if 0
-  printf("::: PLAY SOUND %d.%d.%d: %d\n",
+  Debug("anim:PlayGlobalAnimSound", "PLAY SOUND %d.%d.%d: %d",
         part->anim_nr, part->nr, part->mode_nr, sound);
 #endif
 }
@@ -996,8 +1102,8 @@ static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
   StopSound(sound);
 
 #if 0
-  printf("::: STOP SOUND %d.%d.%d: %d\n",
-        part->anim_nr, part->nr, part->mode_nr, sound);
+  Debug("anim:StopGlobalAnimSound", "STOP SOUND %d.%d.%d: %d",
+       part->anim_nr, part->nr, part->mode_nr, sound);
 #endif
 }
 
@@ -1017,8 +1123,8 @@ static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
     PlayMusic(music);
 
 #if 0
-  printf("::: PLAY MUSIC %d.%d.%d: %d\n",
-        part->anim_nr, part->nr, part->mode_nr, music);
+  Debug("anim:PlayGlobalAnimMusic", "PLAY MUSIC %d.%d.%d: %d",
+       part->anim_nr, part->nr, part->mode_nr, music);
 #endif
 }
 
@@ -1029,11 +1135,18 @@ static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
   if (music == MUS_UNDEFINED)
     return;
 
+  char *anim_music = getMusicInfoEntryFilename(music);
+  char *curr_music = getCurrentlyPlayingMusicFilename();
+
+  // do not stop music if global anim music differs from current music
+  if (!strEqual(curr_music, anim_music))
+    return;
+
   StopMusic();
 
 #if 0
-  printf("::: STOP MUSIC %d.%d.%d: %d\n",
-        part->anim_nr, part->nr, part->mode_nr, music);
+  Debug("anim:StopGlobalAnimMusic", "STOP MUSIC %d.%d.%d: %d",
+       part->anim_nr, part->nr, part->mode_nr, music);
 #endif
 }
 
@@ -1078,6 +1191,7 @@ static void PlayGlobalAnimSoundIfLoop(struct GlobalAnimPartControlInfo *part)
 static boolean checkGlobalAnimEvent(int anim_event, int mask)
 {
   int mask_anim_only = mask & ~ANIM_EVENT_PART_MASK;
+  int mask_ce_only   = mask & ~ANIM_EVENT_PAGE_MASK;
 
   if (mask & ANIM_EVENT_ANY)
     return (anim_event & ANIM_EVENT_ANY);
@@ -1085,6 +1199,9 @@ static boolean checkGlobalAnimEvent(int anim_event, int mask)
     return (anim_event & ANIM_EVENT_SELF);
   else if (mask & ANIM_EVENT_UNCLICK_ANY)
     return (anim_event & ANIM_EVENT_UNCLICK_ANY);
+  else if (mask & ANIM_EVENT_CE_CHANGE)
+    return (anim_event == mask ||
+           anim_event == mask_ce_only);
   else
     return (anim_event == mask ||
            anim_event == mask_anim_only);
@@ -1116,44 +1233,64 @@ static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
   return FALSE;
 }
 
-static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
-                            int mx, int my, boolean clicked)
+static boolean isInsidePartStacked(struct GlobalAnimPartControlInfo *part,
+                                  int mx, int my)
 {
   struct GraphicInfo *g = &part->graphic_info;
+  struct GraphicInfo *c = &part->control_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;
+  int x, y;
+
+  for (y = 0; y < c->stacked_yfactor; y++)
+  {
+    for (x = 0; x < c->stacked_xfactor; x++)
+    {
+      int part_stacked_x = part_x + x * (part_width  + c->stacked_xoffset);
+      int part_stacked_y = part_y + y * (part_height + c->stacked_yoffset);
+
+      if (mx >= part_stacked_x &&
+         mx <  part_stacked_x + part_width &&
+         my >= part_stacked_y &&
+         my <  part_stacked_y + part_height)
+       return TRUE;
+    }
+  }
 
+  return FALSE;
+}
+
+static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
+                            int mx, int my, boolean clicked)
+{
   // check if mouse click was detected at all
   if (!clicked)
     return FALSE;
 
-  // check if mouse click is inside the animation part's viewport
+  // check if mouse click is outside 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;
+  // check if mouse click is inside the animation part's (stacked) graphic
+  if (isInsidePartStacked(part, mx, my))
+    return TRUE;
 
-  return TRUE;
+  return FALSE;
 }
 
 static boolean clickBlocked(struct GlobalAnimPartControlInfo *part)
 {
-  return (part->control_info.style & STYLE_BLOCK ? TRUE : FALSE);
+  return ((part->control_info.style & STYLE_BLOCK) ? TRUE : FALSE);
 }
 
 static boolean clickConsumed(struct GlobalAnimPartControlInfo *part)
 {
-  return (part->control_info.style & STYLE_PASSTHROUGH ? FALSE : TRUE);
+  return ((part->control_info.style & STYLE_PASSTHROUGH) ? FALSE : TRUE);
 }
 
 static void InitGlobalAnim_Triggered(struct GlobalAnimPartControlInfo *part,
@@ -1190,13 +1327,15 @@ static void InitGlobalAnim_Triggered(struct GlobalAnimPartControlInfo *part,
        *click_consumed |= clickConsumed(part);         // click was on "part"!
 
 #if DEBUG_ANIM_EVENTS
-       printf("::: => %d.%d TRIGGERED BY %s OF %d.%d\n",
-              part2->old_anim_nr + 1, part2->old_nr + 1, info_text,
-              part->old_anim_nr + 1, part->old_nr + 1);
+       Debug("anim:InitGlobalAnim_Triggered",
+             "%d.%d TRIGGERED BY %s OF %d.%d",
+             part2->old_anim_nr + 1, part2->old_nr + 1, info_text,
+             part->old_anim_nr + 1, part->old_nr + 1);
 #endif
 #if 0
-       printf("::: %d.%d TRIGGER CLICKED [%d]\n", anim2_nr, part2_nr,
-              part2->control_info.anim_event_action);
+       Debug("anim:InitGlobalAnim_Triggered",
+             "%d.%d TRIGGER CLICKED [%d]", anim2_nr, part2_nr,
+             part2->control_info.anim_event_action);
 #endif
 
        // after executing event action, ignore any further actions
@@ -1207,15 +1346,50 @@ static void InitGlobalAnim_Triggered(struct GlobalAnimPartControlInfo *part,
 #if 0
       struct GraphicInfo *c = &part2->control_info;
 
-      printf("::: - %d.%d: 0x%08x, 0x%08x [0x%08x]",
-            anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
+      if (isClickablePart(part2, mask))
+       Debug("anim:InitGlobalAnim_Triggered",
+             "%d.%d: 0x%08x, 0x%08x [0x%08x] <--- TRIGGERED BY %d.%d",
+             anim2_nr, part2_nr, c->init_event, c->anim_event, mask,
+             anim_nr, part_nr);
+      else
+       Debug("anim:InitGlobalAnim_Triggered",
+             "%d.%d: 0x%08x, 0x%08x [0x%08x]",
+             anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
+#endif
+    }
+  }
+}
+
+static void InitGlobalAnim_Triggered_ByCustomElement(int nr, int page)
+{
+  struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[GAME_MODE_PLAYING];
+
+  int event_value = ANIM_EVENT_CE_CHANGE;
+  int event_bits = (nr << ANIM_EVENT_CE_BIT) | (page << ANIM_EVENT_PAGE_BIT);
+  int mask = event_value | event_bits;
+  int anim2_nr;
+
+  for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
+  {
+    struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
+    int part2_nr;
+
+    for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
+    {
+      struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
+
+      if (!(part2->state & ANIM_STATE_RUNNING))
+       continue;
 
       if (isClickablePart(part2, mask))
-       printf(" <--- TRIGGERED BY %d.%d",
-              anim_nr, part_nr);
+      {
+       part2->triggered = TRUE;
 
-      printf("\n");
+#if 0
+       Debug("anim:InitGlobalAnim_Triggered_ByCustomElement",
+             "%d.%d TRIGGERED BY CE %d", anim2_nr, part2_nr, nr + 1);
 #endif
+      }
     }
   }
 }
@@ -1224,7 +1398,8 @@ static void HandleGlobalAnimDelay(struct GlobalAnimPartControlInfo *part,
                                  int delay_type, char *info_text)
 {
 #if DEBUG_ANIM_DELAY
-  printf("::: %d.%d %s\n", part->old_anim_nr + 1, part->old_nr + 1, info_text);
+  Debug("anim:HandleGlobalAnimDelay", "%d.%d %s",
+       part->old_anim_nr + 1, part->old_nr + 1, info_text);
 #endif
 
   DoGlobalAnim_DelayAction(part, delay_type);
@@ -1234,7 +1409,8 @@ static void HandleGlobalAnimEvent(struct GlobalAnimPartControlInfo *part,
                                  int event_value, char *info_text)
 {
 #if DEBUG_ANIM_EVENTS
-  printf("::: %d.%d %s\n", part->old_anim_nr + 1, part->old_nr + 1, info_text);
+  Debug("anim:HandleGlobalAnimEvent", "%d.%d %s",
+       part->old_anim_nr + 1, part->old_nr + 1, info_text);
 #endif
 
   boolean click_consumed = FALSE;
@@ -1281,7 +1457,8 @@ static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
     part->anim_event_state = (c->anim_event != ANIM_EVENT_UNDEFINED);
 
     part->initial_anim_sync_frame =
-      (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
+      (g->anim_global_sync || g->anim_global_anim_sync ? 0 :
+       anim_sync_frame + part->init_delay_counter);
 
     // do not re-initialize random animation frame after fade-in
     if (part->anim_random_frame == -1)
@@ -1389,7 +1566,12 @@ static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
       part->init_event_state)
   {
     if (part->initial_anim_sync_frame > 0)
-      part->initial_anim_sync_frame -= part->init_delay_counter - 1;
+    {
+      if (part->init_delay_counter > 0)
+       part->initial_anim_sync_frame -= part->init_delay_counter - 1;
+      else
+       part->initial_anim_sync_frame = anim_sync_frame;
+    }
 
     part->init_delay_counter = 1;
     part->init_event_state = FALSE;
@@ -1499,8 +1681,7 @@ static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
   // special case to prevent expiring loop sounds when playing
   PlayGlobalAnimSoundIfLoop(part);
 
-  if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
-                      anim_sync_frame))
+  if (!DelayReachedExt(&part->step_delay, anim_sync_frame))
     return ANIM_STATE_RUNNING;
 
 #if 0
@@ -1508,8 +1689,8 @@ static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
     static unsigned int last_counter = -1;
     unsigned int counter = Counter();
 
-    printf("::: NEXT ANIM PART [%d, %d]\n",
-          anim_sync_frame, counter - last_counter);
+    Debug("anim:HandleGlobalAnim_Part", "NEXT ANIM PART [%d, %d]",
+         anim_sync_frame, counter - last_counter);
 
     last_counter = counter;
   }
@@ -1534,21 +1715,22 @@ static void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim,
   int i;
 
 #if 0
-  printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
+  Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
         anim->mode_nr, anim->nr, anim->num_parts);
-  printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
+  Debug("anim:HandleGlobalAnim_Main",
+       "%d, %d, %d", global.num_toons, setup.toons, num_toons);
 #endif
 
 #if 0
-  printf("::: %s(%d): %d, %d, %d [%d]\n",
-        (action == ANIM_START ? "ANIM_START" :
-         action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
-         action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
-        anim->nr,
-        anim->state & ANIM_STATE_RESTART,
-        anim->state & ANIM_STATE_WAITING,
-        anim->state & ANIM_STATE_RUNNING,
-        anim->num_parts);
+  Debug("anim:HandleGlobalAnim_Main", "%s(%d): %d, %d, %d [%d]",
+       (action == ANIM_START ? "ANIM_START" :
+        action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
+        action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
+       anim->nr,
+       anim->state & ANIM_STATE_RESTART,
+       anim->state & ANIM_STATE_WAITING,
+       anim->state & ANIM_STATE_RUNNING,
+       anim->num_parts);
 #endif
 
   switch (action)
@@ -1584,8 +1766,8 @@ static void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim,
   if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
   {
 #if 0
-    printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
-          anim->mode_nr, anim->nr, num_parts);
+    Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
+         anim->mode_nr, anim->nr, num_parts);
 #endif
 
     for (i = 0; i < num_parts; i++)
@@ -1682,8 +1864,7 @@ static void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action
   int i;
 
 #if 0
-  printf("::: HandleGlobalAnim_Mode: %d => %d\n",
-        ctrl->nr, ctrl->num_anims);
+  Debug("anim:HandleGlobalAnim_Mode", "%d => %d", ctrl->nr, ctrl->num_anims);
 #endif
 
   for (i = 0; i < ctrl->num_anims; i++)
@@ -1693,7 +1874,7 @@ static void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action
 static void HandleGlobalAnim(int action, int game_mode)
 {
 #if 0
-  printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
+  Debug("anim:HandleGlobalAnim", "mode == %d", game_status);
 #endif
 
   HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
@@ -1704,7 +1885,7 @@ static void DoAnimationExt(void)
   int i;
 
 #if 0
-  printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
+  Debug("anim:DoAnimationExt", "%d, %d", anim_sync_frame, Counter());
 #endif
 
   // global animations now synchronized with frame delay of screen update
@@ -1743,7 +1924,10 @@ static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *part)
   if (event_action == ANIM_EVENT_ACTION_NONE)
     return FALSE;
 
-  PushUserEvent(USEREVENT_ANIM_EVENT_ACTION, event_action, 0);
+  if (event_action < MAX_IMAGE_FILES)
+    PushUserEvent(USEREVENT_ANIM_EVENT_ACTION, event_action, 0);
+  else
+    OpenURLFromHash(anim_url_hash, event_action);
 
   // check if further actions are allowed to be executed
   if (part->control_info.style & STYLE_MULTIPLE_ACTIONS)
@@ -1828,8 +2012,8 @@ static boolean InitGlobalAnim_Clicked(int mx, int my, int clicked_event)
            isClickablePart(part, ANIM_EVENT_ANY))
        {
 #if DEBUG_ANIM_EVENTS
-         printf("::: => %d.%d TRIGGERED BY ANY\n",
-                part->old_anim_nr + 1, part->old_nr + 1);
+         Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY ANY",
+               part->old_anim_nr + 1, part->old_nr + 1);
 #endif
 
          anything_clicked = part->clicked = TRUE;
@@ -1841,8 +2025,8 @@ static boolean InitGlobalAnim_Clicked(int mx, int my, int clicked_event)
            isClickablePart(part, ANIM_EVENT_UNCLICK_ANY))
        {
 #if DEBUG_ANIM_EVENTS
-         printf("::: => %d.%d TRIGGERED BY UNCLICK:ANY\n",
-                part->old_anim_nr + 1, part->old_nr + 1);
+         Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY UNCLICK:ANY",
+               part->old_anim_nr + 1, part->old_nr + 1);
 #endif
 
          anything_clicked = part->clicked = TRUE;
@@ -1857,8 +2041,8 @@ static boolean InitGlobalAnim_Clicked(int mx, int my, int clicked_event)
            isClickedPart(part, mx, my, TRUE))
        {
 #if 0
-         printf("::: %d.%d CLICKED [%d]\n", anim_nr, part_nr,
-                part->control_info.anim_event_action);
+         Debug("anim:InitGlobalAnim_Clicked", "%d.%d CLICKED [%d]",
+               anim_nr, part_nr, part->control_info.anim_event_action);
 #endif
 
          // after executing event action, ignore any further actions
@@ -1871,8 +2055,8 @@ static boolean InitGlobalAnim_Clicked(int mx, int my, int clicked_event)
          if (isClickablePart(part, ANIM_EVENT_SELF))
          {
 #if DEBUG_ANIM_EVENTS
-           printf("::: => %d.%d TRIGGERED BY SELF\n",
-                  part->old_anim_nr + 1, part->old_nr + 1);
+           Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY SELF",
+                 part->old_anim_nr + 1, part->old_nr + 1);
 #endif
 
            anything_clicked = part->clicked = TRUE;
@@ -1916,6 +2100,18 @@ static void ResetGlobalAnim_Clicked(void)
   InitGlobalAnim_Clicked(-1, -1, ANIM_CLICKED_RESET);
 }
 
+void RestartGlobalAnimsByStatus(int status)
+{
+  int anim_status_last = global.anim_status;
+
+  global.anim_status = status;
+
+  // force restarting global animations by changed global animation status
+  SDLRedrawWindow();
+
+  global.anim_status = anim_status_last;
+}
+
 boolean HandleGlobalAnimClicks(int mx, int my, int button, boolean force_click)
 {
   static boolean click_consumed = FALSE;
@@ -1946,3 +2142,17 @@ boolean HandleGlobalAnimClicks(int mx, int my, int button, boolean force_click)
 
   return click_consumed_current;
 }
+
+int getGlobalAnimSyncFrame(void)
+{
+  return anim_sync_frame;
+}
+
+void HandleGlobalAnimEventByElementChange(int element, int page)
+{
+  if (!IS_CUSTOM_ELEMENT(element))
+    return;
+
+  // custom element stored as 0 to 255, change page stored as 1 to 32
+  InitGlobalAnim_Triggered_ByCustomElement(element - EL_CUSTOM_START, page + 1);
+}