rnd-20031205-B-src
[rocksndiamonds.git] / src / events.c
index bb6628964d216db9fb65949b80c36fe00bfb4c4a..5af02c558fccfb9dd6d5a522534e75cdeff60656 100644 (file)
 #include "tape.h"
 #include "network.h"
 
-/* values for key_status */
-#define KEY_NOT_PRESSED                FALSE
-#define KEY_RELEASED           FALSE
-#define KEY_PRESSED            TRUE
-
 
 static boolean cursor_inside_playfield = FALSE;
 static boolean playfield_cursor_set = FALSE;
@@ -58,7 +53,8 @@ int FilterMouseMotionEvents(const Event *event)
   }
 
   /* skip mouse motion events without pressed button outside level editor */
-  if (button_status == MB_RELEASED && game_status != GAME_MODE_EDITOR)
+  if (button_status == MB_RELEASED && game_status != GAME_MODE_EDITOR &&
+      game_status != GAME_MODE_PLAYING)
     return 0;
   else
     return 1;
@@ -83,7 +79,7 @@ static boolean NextValidEvent(Event *event)
 
 void EventLoop(void)
 {
-  while(1)
+  while (1)
   {
     if (PendingEvent())                /* got event */
     {
@@ -223,7 +219,7 @@ void ClearPlayerAction()
 
   /* simulate key release events for still pressed keys */
   key_joystick_mapping = 0;
-  for (i=0; i<MAX_PLAYERS; i++)
+  for (i = 0; i < MAX_PLAYERS; i++)
     stored_player[i].action = 0;
 }
 
@@ -233,7 +229,7 @@ void SleepWhileUnmapped()
 
   KeyboardAutoRepeatOn();
 
-  while(window_unmapped)
+  while (window_unmapped)
   {
     Event event;
 
@@ -310,7 +306,9 @@ void HandleKeyEvent(KeyEvent *event)
   int key_status = (event->type==EVENT_KEYPRESS ? KEY_PRESSED : KEY_RELEASED);
   boolean with_modifiers = (game_status == GAME_MODE_PLAYING ? FALSE : TRUE);
   Key key = GetEventKey(event, with_modifiers);
+  Key keymod = (with_modifiers ? GetEventKey(event, FALSE) : key);
 
+  HandleKeyModState(keymod, key_status);
   HandleKey(key, key_status);
 }
 
@@ -376,7 +374,11 @@ void HandleButton(int mx, int my, int button)
     old_my = my;
   }
 
-  HandleGadgets(mx, my, button);
+  if (HandleGadgets(mx, my, button))
+  {
+    /* do not handle this button event anymore */
+    mx = my = -32;     /* force mouse event to be outside screen tiles */
+  }
 
   switch(game_status)
   {
@@ -400,7 +402,7 @@ void HandleButton(int mx, int my, int button)
       break;
 
     case GAME_MODE_INFO:
-      HandleHelpScreen(button);
+      HandleInfoScreen(mx,my, 0,0, button);
       break;
 
     case GAME_MODE_SETUP:
@@ -411,11 +413,10 @@ void HandleButton(int mx, int my, int button)
 #ifdef DEBUG
       if (button == MB_RELEASED)
       {
-       int sx = (mx - SX) / TILEX;
-       int sy = (my - SY) / TILEY;
-
-       if (IN_VIS_FIELD(sx,sy))
+       if (IN_GFX_SCREEN(mx, my))
        {
+         int sx = (mx - SX) / TILEX;
+         int sy = (my - SY) / TILEY;
          int x = LEVELX(sx);
          int y = LEVELY(sy);
 
@@ -424,14 +425,21 @@ void HandleButton(int mx, int my, int button)
          if (!IN_LEV_FIELD(x, y))
            break;
 
-         printf("      Feld[%d][%d] == %d\n", x,y, Feld[x][y]);
+         printf("      Feld[%d][%d] == %d ('%s')\n", x,y, Feld[x][y],
+                element_info[Feld[x][y]].token_name);
+         printf("      Back[%d][%d] == %d\n", x,y, Back[x][y]);
          printf("      Store[%d][%d] == %d\n", x,y, Store[x][y]);
          printf("      Store2[%d][%d] == %d\n", x,y, Store2[x][y]);
          printf("      StorePlayer[%d][%d] == %d\n", x,y, StorePlayer[x][y]);
          printf("      MovPos[%d][%d] == %d\n", x,y, MovPos[x][y]);
          printf("      MovDir[%d][%d] == %d\n", x,y, MovDir[x][y]);
          printf("      MovDelay[%d][%d] == %d\n", x,y, MovDelay[x][y]);
+         printf("      ChangeDelay[%d][%d] == %d\n", x,y, ChangeDelay[x][y]);
          printf("      GfxElement[%d][%d] == %d\n", x,y, GfxElement[x][y]);
+         printf("      GfxAction[%d][%d] == %d\n", x,y, GfxAction[x][y]);
+         printf("      GfxFrame[%d][%d] == %d\n", x,y, GfxFrame[x][y]);
+         printf("      RunnerVisit[%d][%d] == %d\n", x,y, RunnerVisit[x][y]);
+         printf("      PlayerVisit[%d][%d] == %d\n", x,y, PlayerVisit[x][y]);
          printf("\n");
        }
       }
@@ -470,7 +478,7 @@ void HandleKey(Key key, int key_status)
     static boolean bomb_placed[MAX_PLAYERS] = { FALSE,FALSE,FALSE,FALSE };
     int pnr;
 
-    for (pnr=0; pnr<MAX_PLAYERS; pnr++)
+    for (pnr = 0; pnr < MAX_PLAYERS; pnr++)
     {
       int i;
       byte key_action = 0;
@@ -480,7 +488,7 @@ void HandleKey(Key key, int key_status)
 
       custom_key = setup.input[pnr].key;
 
-      for (i=0; i<6; i++)
+      for (i = 0; i < 6; i++)
        if (key == *key_info[i].key_custom)
          key_action |= key_info[i].action;
 
@@ -530,7 +538,7 @@ void HandleKey(Key key, int key_status)
   {
     int i;
 
-    for (i=0; i<6; i++)
+    for (i = 0; i < 6; i++)
       if (key == key_info[i].key_default)
        joy |= key_info[i].action;
   }
@@ -560,19 +568,6 @@ void HandleKey(Key key, int key_status)
     return;
   }
 
-  /* allow quick escape to the main menu with the Escape key */
-  if (key == KSYM_Escape &&
-      game_status != GAME_MODE_MAIN &&
-      game_status != GAME_MODE_PLAYING &&
-      game_status != GAME_MODE_EDITOR &&
-      game_status != GAME_MODE_LEVELS &&
-      game_status != GAME_MODE_SETUP)
-  {
-    game_status = GAME_MODE_MAIN;
-    DrawMainMenu();
-    return;
-  }
-
   /* special key shortcuts */
   if (game_status == GAME_MODE_MAIN || game_status == GAME_MODE_PLAYING)
   {
@@ -584,17 +579,11 @@ void HandleKey(Key key, int key_status)
       TapeTogglePause(TAPE_TOGGLE_MANUAL);
   }
 
-#if 0
-#ifndef DEBUG
-
-  if (game_status == GAME_MODE_PLAYING && (tape.playing || tape.pausing))
-    return;
-
-#endif
-#endif
-
-
-  HandleGadgetsKeyInput(key);
+  if (HandleGadgetsKeyInput(key))
+  {
+    if (key != KSYM_Escape)    /* always allow ESC key to be handled */
+      key = KSYM_UNDEFINED;
+  }
 
   switch(game_status)
   {
@@ -605,6 +594,7 @@ void HandleKey(Key key, int key_status)
     case GAME_MODE_MAIN:
     case GAME_MODE_LEVELS:
     case GAME_MODE_SETUP:
+    case GAME_MODE_INFO:
       switch(key)
       {
        case KSYM_Return:
@@ -614,6 +604,8 @@ void HandleKey(Key key, int key_status)
             HandleChooseLevel(0,0, 0,0, MB_MENU_CHOICE);
          else if (game_status == GAME_MODE_SETUP)
            HandleSetupScreen(0,0, 0,0, MB_MENU_CHOICE);
+         else if (game_status == GAME_MODE_INFO)
+           HandleInfoScreen(0,0, 0,0, MB_MENU_CHOICE);
          break;
 
        case KSYM_Escape:
@@ -621,20 +613,26 @@ void HandleKey(Key key, int key_status)
             HandleChooseLevel(0,0, 0,0, MB_MENU_LEAVE);
          else if (game_status == GAME_MODE_SETUP)
            HandleSetupScreen(0,0, 0,0, MB_MENU_LEAVE);
+         else if (game_status == GAME_MODE_INFO)
+           HandleInfoScreen(0,0, 0,0, MB_MENU_LEAVE);
          break;
 
         case KSYM_Page_Up:
           if (game_status == GAME_MODE_LEVELS)
-            HandleChooseLevel(0,0, 0,-SCR_FIELDY, MB_MENU_MARK);
+            HandleChooseLevel(0,0, 0, -1 * SCROLL_PAGE, MB_MENU_MARK);
          else if (game_status == GAME_MODE_SETUP)
-           HandleSetupScreen(0,0, 0,-SCR_FIELDY, MB_MENU_MARK);
+           HandleSetupScreen(0,0, 0, -1 * SCROLL_PAGE, MB_MENU_MARK);
+         else if (game_status == GAME_MODE_INFO)
+           HandleInfoScreen(0,0, 0, -1 * SCROLL_PAGE, MB_MENU_MARK);
          break;
 
         case KSYM_Page_Down:
           if (game_status == GAME_MODE_LEVELS)
-            HandleChooseLevel(0,0, 0,SCR_FIELDY, MB_MENU_MARK);
+            HandleChooseLevel(0,0, 0, +1 * SCROLL_PAGE, MB_MENU_MARK);
          else if (game_status == GAME_MODE_SETUP)
-           HandleSetupScreen(0,0, 0,SCR_FIELDY, MB_MENU_MARK);
+           HandleSetupScreen(0,0, 0, +1 * SCROLL_PAGE, MB_MENU_MARK);
+         else if (game_status == GAME_MODE_INFO)
+           HandleInfoScreen(0,0, 0, +1 * SCROLL_PAGE, MB_MENU_MARK);
          break;
 
 #ifdef DEBUG
@@ -648,25 +646,21 @@ void HandleKey(Key key, int key_status)
       }
       break;
 
-    case GAME_MODE_INFO:
-      HandleHelpScreen(MB_RELEASED);
-      break;
-
     case GAME_MODE_SCORES:
       switch(key)
       {
        case KSYM_Return:
+       case KSYM_Escape:
          game_status = GAME_MODE_MAIN;
          DrawMainMenu();
-         BackToFront();
          break;
 
         case KSYM_Page_Up:
-         HandleHallOfFame(0,0, 0,-SCR_FIELDY, MB_MENU_MARK);
+         HandleHallOfFame(0,0, 0, -1 * SCROLL_PAGE, MB_MENU_MARK);
          break;
 
         case KSYM_Page_Down:
-         HandleHallOfFame(0,0, 0,SCR_FIELDY, MB_MENU_MARK);
+         HandleHallOfFame(0,0, 0, +1 * SCROLL_PAGE, MB_MENU_MARK);
          break;
 
        default:
@@ -792,9 +786,16 @@ void HandleKey(Key key, int key_status)
 
        case KSYM_Q:
        case KSYM_q:
-         local_player->dynamite = 1000;
-         break;
+         {
+           int i;
 
+           for (i = 0; i < MAX_INVENTORY_SIZE; i++)
+             if (local_player->inventory_size < MAX_INVENTORY_SIZE)
+               local_player->inventory_element[local_player->inventory_size++] =
+                 EL_DYNAMITE;
+         }
+
+         break;
 
 
 #if 0
@@ -803,7 +804,7 @@ void HandleKey(Key key, int key_status)
          {
            int i;
 
-           for(i=0; i<MAX_PLAYERS; i++)
+           for (i = 0; i < MAX_PLAYERS; i++)
            {
              printf("Player %d:\n", i);
              printf("  jx == %d, jy == %d\n",
@@ -824,7 +825,13 @@ void HandleKey(Key key, int key_status)
       break;
     }
     default:
-      break;
+      if (key == KSYM_Escape)
+      {
+       game_status = GAME_MODE_MAIN;
+       DrawMainMenu();
+
+       return;
+      }
   }
 }
 
@@ -849,7 +856,7 @@ static int HandleJoystickForAllPlayers()
   int i;
   int result = 0;
 
-  for (i=0; i<MAX_PLAYERS; i++)
+  for (i = 0; i < MAX_PLAYERS; i++)
   {
     byte joy_action = 0;
 
@@ -889,6 +896,7 @@ void HandleJoystick()
     case GAME_MODE_MAIN:
     case GAME_MODE_LEVELS:
     case GAME_MODE_SETUP:
+    case GAME_MODE_INFO:
     {
       static unsigned long joystickmove_delay = 0;
 
@@ -902,6 +910,8 @@ void HandleJoystick()
         HandleChooseLevel(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
       else if (game_status == GAME_MODE_SETUP)
        HandleSetupScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
+      else if (game_status == GAME_MODE_INFO)
+       HandleInfoScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
       break;
     }
 
@@ -909,10 +919,6 @@ void HandleJoystick()
       HandleHallOfFame(0,0, dx,dy, !newbutton);
       break;
 
-    case GAME_MODE_INFO:
-      HandleHelpScreen(!newbutton);
-      break;
-
     case GAME_MODE_EDITOR:
       HandleLevelEditorIdle();
       break;