fixed displaying new high score entry if not on first page
[rocksndiamonds.git] / src / screens.c
index eba14f7ad58b447a601b9c97db3728c3f73a31ad..39650b5cf6a4f9aa32acd0fb999518ccbd6815c1 100644 (file)
 
 // other screen text constants
 #define STR_CHOOSE_TREE_EDIT           "Edit"
+#define MENU_CHOOSE_TREE_FONT(x)       (FONT_TEXT_1 + (x))
 
 // for input setup functions
 #define SETUPINPUT_SCREEN_POS_START    0
@@ -1745,7 +1746,7 @@ static void gotoTopLevelDir(void)
     if (leveldir_current->node_parent->node_group->cl_first == -1)
     {
       int num_leveldirs = numTreeInfoInGroup(leveldir_current);
-      int leveldir_pos = posTreeInfo(leveldir_current);
+      int leveldir_pos = getPosFromTreeInfo(leveldir_current);
       int num_page_entries = MIN(num_leveldirs, NUM_MENU_ENTRIES_ON_SCREEN);
       int cl_first, cl_cursor;
 
@@ -2167,10 +2168,10 @@ void HandleMainMenu(int mx, int my, int dx, int dy, int button)
       else if (pos == MAIN_CONTROL_EDITOR)
       {
        if (leveldir_current->readonly &&
-           !strEqual(setup.player_name, "Artsoft"))
-         Request("This level is read only!", REQ_CONFIRM);
+           setup.editor.show_read_only_warning)
+         Request("This level is read-only!", REQ_CONFIRM | REQ_STAY_OPEN);
 
-       CloseDoor(DOOR_CLOSE_2);
+       CloseDoor(DOOR_CLOSE_ALL);
 
        SetGameStatus(GAME_MODE_EDITOR);
 
@@ -4018,6 +4019,32 @@ void HandleInfoScreen(int mx, int my, int dx, int dy, int button)
 
 static TreeInfo *type_name_node = NULL;
 static char type_name_last[MAX_PLAYER_NAME_LEN + 1] = { 0 };
+static int type_name_nr = 0;
+
+static int getPlayerNameColor(char *name)
+{
+  return (strEqual(name, EMPTY_PLAYER_NAME) ? FC_BLUE : FC_RED);
+}
+
+static void drawTypeNameText(char *name, struct TextPosInfo *pos,
+                             boolean active)
+{
+  char text[MAX_PLAYER_NAME_LEN + 2] = { 0 };
+  boolean multiple_users = (game_status == GAME_MODE_PSEUDO_TYPENAMES);
+  int sx = (multiple_users ? amSX + pos->x : mSX + ALIGNED_TEXT_XPOS(pos));
+  int sy = (multiple_users ? amSY + pos->y : mSY + ALIGNED_TEXT_YPOS(pos));
+  int font_nr = (active ? FONT_ACTIVE(pos->font) : pos->font);
+  int font_width = getFontWidth(font_nr);
+
+  DrawBackgroundForFont(sx, sy, pos->width, pos->height, font_nr);
+
+  sprintf(text, "%s%c", name, (active ? '_' : '\0'));
+
+  pos->width = strlen(text) * font_width;
+  sx = (multiple_users ? amSX + pos->x : mSX + ALIGNED_TEXT_XPOS(pos));
+
+  DrawText(sx, sy, text, font_nr);
+}
 
 static void getTypeNameValues(char *name, struct TextPosInfo *pos, int *xpos)
 {
@@ -4025,7 +4052,7 @@ static void getTypeNameValues(char *name, struct TextPosInfo *pos, int *xpos)
 
   *pos = *mci->pos_input;
 
-  if (setup.multiple_users)
+  if (game_status == GAME_MODE_PSEUDO_TYPENAMES)
   {
     TreeInfo *ti = player_name_current;
     int first_entry = ti->cl_first;
@@ -4033,18 +4060,20 @@ static void getTypeNameValues(char *name, struct TextPosInfo *pos, int *xpos)
     TreeInfo *node_first = getTreeInfoFirstGroupEntry(ti);
     int xpos = MENU_SCREEN_START_XPOS;
     int ypos = MENU_SCREEN_START_YPOS + ti->cl_cursor;
-    int font_width = getFontWidth(pos->font);
 
     type_name_node = getTreeInfoFromPos(node_first, entry_pos);
+    type_name_nr = entry_pos;
 
     strcpy(name, type_name_node->name);
 
-    pos->x = xpos * font_width;
-    pos->y = ypos * font_width;
-    pos->width = MAX_PLAYER_NAME_LEN * font_width;
+    pos->x = xpos * 32;
+    pos->y = ypos * 32;
+    pos->width = MAX_PLAYER_NAME_LEN * 32;
   }
   else
   {
+    type_name_nr = user.nr;
+
     strcpy(name, setup.player_name);
   }
 
@@ -4056,69 +4085,144 @@ static void getTypeNameValues(char *name, struct TextPosInfo *pos, int *xpos)
   *xpos = strlen(name);
 }
 
-static void setTypeNameValues(char *name, int *font, boolean success)
+static void setTypeNameValues_Name(char *name, struct TextPosInfo *pos)
 {
-  if (!success)
-    strcpy(name, type_name_last);
-
-  if (strEqual(name, ""))
-    strcpy(name, EMPTY_PLAYER_NAME);
+  // change name of edited user in global list of user names
+  setString(&global.user_names[type_name_nr], name);
 
-  if (setup.multiple_users)
+  if (game_status == GAME_MODE_PSEUDO_TYPENAMES)
   {
-    if (type_name_node == NULL)                // should not happen
-      return;
-
-    if (success)
-    {
-      type_name_node->color = FC_RED;
+    TreeInfo *node = type_name_node;
 
-      if (strEqual(name, EMPTY_PLAYER_NAME))
-       type_name_node->color = FC_BLUE;
-    }
+    // change name of edited user in local menu tree structure
+    setString(&node->name, name);
+    setString(&node->name_sorting, name);
 
-    *font = FONT_TEXT_1 + type_name_node->color;
+    node->color = getPlayerNameColor(name);
+    pos->font = MENU_CHOOSE_TREE_FONT(node->color);
   }
+}
 
-  if (!success)
-    return;
+static void setTypeNameValues(char *name, struct TextPosInfo *pos,
+                             boolean changed)
+{
+  boolean reset_setup = strEqual(name, "");
+  boolean remove_user = strEqual(name, EMPTY_PLAYER_NAME);
+  boolean create_user = strEqual(type_name_last, EMPTY_PLAYER_NAME);
 
-  int last_user_nr = user.nr;
+  if (!changed)
+    strcpy(name, type_name_last);
 
-  if (setup.multiple_users)
-  {
-    int edit_user_nr = posTreeInfo(type_name_node);
+  if (strEqual(name, ""))
+    strcpy(name, EMPTY_PLAYER_NAME);
 
-    // change name of edited user in global list of user names
-    setString(&global.user_names[edit_user_nr], name);
+  setTypeNameValues_Name(name, pos);
 
-    // change name of edited user in local menu tree structure
-    setString(&type_name_node->name, name);
-    setString(&type_name_node->name_sorting, name);
+  // if player name not changed, no further action required
+  if (strEqual(name, type_name_last))
+    return;
+
+  // redraw player name before (possibly) opening request dialogs
+  drawTypeNameText(name, pos, FALSE);
 
+  int last_user_nr = user.nr;
+
+  if (game_status == GAME_MODE_PSEUDO_TYPENAMES)
+  {
     // save setup of currently active user (may differ from edited user)
     SaveSetup();
 
     // temporarily change active user to edited user
-    user.nr = edit_user_nr;
+    user.nr = type_name_nr;
 
-    // load setup of edited user
-    LoadSetup();
+    // load setup of edited user (unless creating user with current setup)
+    if (!create_user ||
+       !Request("Use current setup values for the new player?", REQ_ASK))
+      LoadSetup();
   }
 
+  char *setup_filename = getSetupFilename();
+  boolean setup_exists = fileExists(setup_filename);
+
   // change name of edited user in setup structure
   strcpy(setup.player_name, name);
 
   // save setup of edited user
   SaveSetup();
 
-  if (setup.multiple_users)
+  if (game_status == GAME_MODE_PSEUDO_TYPENAMES || reset_setup)
   {
+    if (reset_setup)
+    {
+      if (Request("Reset setup values for this player?", REQ_ASK))
+      {
+       // remove setup config file
+       unlink(setup_filename);
+
+       // set player name to default player name
+       LoadSetup();
+
+       // update player name used by name typing functions
+       strcpy(name, setup.player_name);
+
+       setTypeNameValues_Name(name, pos);
+      }
+    }
+    else if (remove_user && type_name_nr != 0)
+    {
+      if (Request("Remove settings and tapes for deleted player?", REQ_ASK))
+      {
+       char *user_dir = getUserGameDataDir();
+       char *user_dir_removed =
+         getStringCat3WithSeparator(user_dir, "REMOVED",
+                                    getCurrentTimestamp(), ".");
+
+       if (rename(user_dir, user_dir_removed) != 0)
+         Request("Removing settings and tapes failed!", REQ_CONFIRM);
+
+       checked_free(user_dir_removed);
+      }
+    }
+    else if (create_user && type_name_nr != 0 && !setup_exists)
+    {
+      if (Request("Create empty level set for the new player?", REQ_ASK))
+      {
+       char *levelset_subdir = getNewUserLevelSubdir();
+
+       if (CreateUserLevelSet(levelset_subdir, name, name, 100, FALSE))
+       {
+         AddUserLevelSetToLevelInfo(levelset_subdir);
+
+         LevelDirTree *leveldir_current_last = leveldir_current;
+
+         leveldir_current = getTreeInfoFromIdentifier(leveldir_first,
+                                                      levelset_subdir);
+
+         // set level number of newly created level set to default value
+         LoadLevelSetup_SeriesInfo();
+
+         // set newly created level set as current level set for new user
+         SaveLevelSetup_LastSeries();
+         SaveLevelSetup_SeriesInfo();
+
+         leveldir_current = leveldir_current_last;
+       }
+       else
+       {
+         Request("Creating new level set failed!", REQ_CONFIRM);
+       }
+      }
+    }
+
     // restore currently active user
     user.nr = last_user_nr;
 
     // restore setup of currently active user
     LoadSetup();
+
+    // restore last level set of currently active user
+    LoadLevelSetup_LastSeries();
+    LoadLevelSetup_SeriesInfo();
   }
 }
 
@@ -4127,24 +4231,18 @@ static void HandleTypeNameExt(boolean initialize, Key key)
   static struct TextPosInfo pos_name = { 0 };
   static char name[MAX_PLAYER_NAME_LEN + 1] = { 0 };
   static int xpos = 0;
-
-  if (initialize)
-    getTypeNameValues(name, &pos_name, &xpos);
-
   struct TextPosInfo *pos = &pos_name;
-  int sx = mSX + ALIGNED_TEXT_XPOS(pos);
-  int sy = mSY + ALIGNED_TEXT_YPOS(pos);
-  int font_nr = pos->font;
-  int font_active_nr = FONT_ACTIVE(font_nr);
-  int font_width = getFontWidth(font_active_nr);
   char key_char = getValidConfigValueChar(getCharFromKey(key));
   boolean is_valid_key_char = (key_char != 0 && (key_char != ' ' || xpos > 0));
-  boolean is_active = TRUE;
-
-  DrawBackgroundForFont(sx, sy, pos->width, pos->height, font_active_nr);
+  boolean active = TRUE;
 
   if (initialize)
   {
+    getTypeNameValues(name, pos, &xpos);
+
+    int sx = mSX + ALIGNED_TEXT_XPOS(pos);
+    int sy = mSY + ALIGNED_TEXT_YPOS(pos);
+
     StartTextInput(sx, sy, pos->width, pos->height);
   }
   else if (is_valid_key_char && xpos < MAX_PLAYER_NAME_LEN)
@@ -4160,37 +4258,25 @@ static void HandleTypeNameExt(boolean initialize, Key key)
 
     name[xpos] = 0;
   }
-  else if (key == KSYM_Return)
+  else if (key == KSYM_Return || key == KSYM_Escape)
   {
-    setTypeNameValues(name, &font_nr, TRUE);
+    boolean changed = (key == KSYM_Return);
 
-    is_active = FALSE;
-  }
-  else if (key == KSYM_Escape)
-  {
-    setTypeNameValues(name, &font_nr, FALSE);
+    StopTextInput();
 
-    is_active = FALSE;
+    setTypeNameValues(name, pos, changed);
+
+    active = FALSE;
   }
 
-  if (is_active)
-  {
-    pos->width = (strlen(name) + 1) * font_width;
-    sx = mSX + ALIGNED_TEXT_XPOS(pos);
+  drawTypeNameText(name, pos, active);
 
-    DrawText(sx, sy, name, font_active_nr);
-    DrawText(sx + xpos * font_width, sy, "_", font_active_nr);
-  }
-  else
+  if (!active)
   {
     SetGameStatus(game_status_last_screen);
 
-    pos->width = strlen(name) * font_width;
-    sx = mSX + ALIGNED_TEXT_XPOS(pos);
-
-    DrawText(sx, sy, name, font_nr);
-
-    StopTextInput();
+    if (game_status == GAME_MODE_MAIN)
+      InitializeMainControls();
   }
 }
 
@@ -4223,7 +4309,8 @@ static int getAlignXOffsetFromTreeInfo(TreeInfo *ti)
 
   int num_entries = numTreeInfoInGroup(ti);
   boolean scrollbar_needed = (num_entries > NUM_MENU_ENTRIES_ON_SCREEN);
-  int text_width = max_text_size * getFontWidth(FONT_TEXT_1);
+  int font_nr = MENU_CHOOSE_TREE_FONT(FC_RED);
+  int text_width = max_text_size * getFontWidth(font_nr);
   int button_width = SC_MENUBUTTON_XSIZE;
   int scrollbar_xpos = SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset;
   int screen_width = (scrollbar_needed ? scrollbar_xpos : SXSIZE);
@@ -4243,7 +4330,8 @@ static int getAlignYOffsetFromTreeInfo(TreeInfo *ti)
 
   int num_entries = numTreeInfoInGroup(ti);
   int num_page_entries = MIN(num_entries, NUM_MENU_ENTRIES_ON_SCREEN);
-  int font_height = getFontHeight(FONT_TEXT_1);
+  int font_nr = MENU_CHOOSE_TREE_FONT(FC_RED);
+  int font_height = getFontHeight(font_nr);
   int text_height = font_height * num_page_entries;
   int page_height = font_height * NUM_MENU_ENTRIES_ON_SCREEN;
   int align = menu.list_setup[SETUP_MODE_CHOOSE_OTHER].valign;
@@ -4306,8 +4394,6 @@ static void drawChooseTreeList(int first_entry, int num_page_entries,
   boolean scrollbar_needed = (num_entries > NUM_MENU_ENTRIES_ON_SCREEN);
   int scrollbar_xpos = SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset;
   int screen_width = (scrollbar_needed ? scrollbar_xpos : SXSIZE);
-  int font_nr = FONT_TEXT_1;
-  int font_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
   int i;
   char *title_string = NULL;
   int yoffset_sets = MENU_TITLE1_YPOS;
@@ -4325,6 +4411,12 @@ static void drawChooseTreeList(int first_entry, int num_page_entries,
   {
     TreeInfo *node, *node_first;
     int entry_pos = first_entry + i;
+
+    node_first = getTreeInfoFirstGroupEntry(ti);
+    node = getTreeInfoFromPos(node_first, entry_pos);
+
+    int font_nr = MENU_CHOOSE_TREE_FONT(node->color);
+    int font_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
     int xpos = MENU_SCREEN_START_XPOS;
     int ypos = MENU_SCREEN_START_YPOS + i;
     int startx = amSX + xpos * 32;
@@ -4335,13 +4427,10 @@ static void drawChooseTreeList(int first_entry, int num_page_entries,
     int max_buffer_len = max_text_size / getFontWidth(font_nr);
     char buffer[max_buffer_len + 1];
 
-    node_first = getTreeInfoFirstGroupEntry(ti);
-    node = getTreeInfoFromPos(node_first, entry_pos);
-
     strncpy(buffer, node->name, max_buffer_len);
     buffer[max_buffer_len] = '\0';
 
-    DrawText(startx, starty, buffer, font_nr + node->color);
+    DrawText(startx, starty, buffer, font_nr);
 
     if (node->parent_link)
       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
@@ -4411,7 +4500,7 @@ static void HandleChooseTree(int mx, int my, int dx, int dy, int button,
   if (button == MB_MENU_INITIALIZE)
   {
     int num_entries = numTreeInfoInGroup(ti);
-    int entry_pos = posTreeInfo(ti);
+    int entry_pos = getPosFromTreeInfo(ti);
 
     align_xoffset = getAlignXOffsetFromTreeInfo(ti);
     align_yoffset = getAlignYOffsetFromTreeInfo(ti);
@@ -4732,7 +4821,7 @@ static void HandleChooseTree(int mx, int my, int dx, int dy, int button,
          {
            if (mx >= sx1_edit_name && mx <= sx2_edit_name)
            {
-             SetGameStatus(GAME_MODE_PSEUDO_TYPENAME);
+             SetGameStatus(GAME_MODE_PSEUDO_TYPENAMES);
 
              DrawTypeName();
 
@@ -4786,18 +4875,13 @@ void DrawChoosePlayerName(void)
 
   for (i = 0; i < MAX_PLAYER_NAMES; i++)
   {
-    boolean team_mode = (!network.enabled && setup.team_mode);
-    int tree_type = (team_mode ? TREE_TYPE_PLAYER_TEAM : TREE_TYPE_PLAYER_NAME);
-    TreeInfo *ti = newTreeInfo_setDefaults(tree_type);
+    TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_PLAYER_NAME);
     char identifier[32], name[MAX_PLAYER_NAME_LEN + 1];
     int value = i;
 
     ti->node_top = &player_name;
     ti->sort_priority = 10000 + value;
-    ti->color = FC_RED;
-
-    if (strEqual(global.user_names[i], EMPTY_PLAYER_NAME))
-      ti->color = FC_BLUE;
+    ti->color = getPlayerNameColor(global.user_names[i]);
 
     snprintf(identifier, sizeof(identifier), "%d", value);
     snprintf(name, sizeof(name), "%s", global.user_names[i]);
@@ -4996,9 +5080,15 @@ void HandleHallOfFame(int mx, int my, int dx, int dy, int button)
   if (button == MB_MENU_INITIALIZE)
   {
     level_nr = mx;
-    first_entry = 0;
     highlight_position = my;
 
+    first_entry = highlight_position - (NUM_MENU_ENTRIES_ON_SCREEN + 1) / 2 + 1;
+
+    if (first_entry < 0)
+      first_entry = 0;
+    else if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN > MAX_SCORE_ENTRIES)
+      first_entry = MAX(0, MAX_SCORE_ENTRIES - NUM_MENU_ENTRIES_ON_SCREEN);
+
     drawHallOfFameList(level_nr, first_entry, highlight_position);
 
     return;
@@ -6606,6 +6696,7 @@ static struct TokenInfo setup_info_engines[] =
   { TYPE_SWITCH,       &setup.forced_scroll_delay, "Scroll Delay:"     },
   { TYPE_ECS_AGA,      &setup.prefer_aga_graphics, "Amiga Graphics Chipset:" },
   { TYPE_SWITCH,       &setup.prefer_lowpass_sounds,"Low-Pass Filter Sounds:" },
+  { TYPE_SWITCH,       &setup.prefer_extra_panel_items,"Show Dynamite and Keys:" },
   { TYPE_EMPTY,                NULL,                   ""                      },
   { TYPE_HEADLINE,     NULL,                   "Supaplex"              },
   { TYPE_SWITCH,       &setup.sp_show_border_elements, "Border Elements:" },
@@ -6644,6 +6735,8 @@ static struct TokenInfo setup_info_editor[] =
 #endif
   { TYPE_SWITCH, &setup.editor.show_element_token,     "Show element token:" },
   { TYPE_EMPTY,                NULL,                   ""                      },
+  { TYPE_SWITCH, &setup.editor.show_read_only_warning, "Show read-only warning:" },
+  { TYPE_EMPTY,                NULL,                   ""                      },
   { TYPE_LEAVE_MENU,   execSetupMain,          "Back"                  },
 
   { 0,                 NULL,                   NULL                    }