+ DrawText(sx, sy, text, font_nr);
+}
+
+static void getTypeNameValues(char *name, struct TextPosInfo *pos, int *xpos)
+{
+ struct MainControlInfo *mci = getMainControlInfo(MAIN_CONTROL_NAME);
+
+ *pos = *mci->pos_input;
+
+ if (game_status == GAME_MODE_PSEUDO_TYPENAMES)
+ {
+ TreeInfo *ti = player_name_current;
+ int first_entry = ti->cl_first;
+ int entry_pos = first_entry + ti->cl_cursor;
+ TreeInfo *node_first = getTreeInfoFirstGroupEntry(ti);
+ int xpos = MENU_SCREEN_START_XPOS;
+ int ypos = MENU_SCREEN_START_YPOS + ti->cl_cursor;
+
+ type_name_node = getTreeInfoFromPos(node_first, entry_pos);
+ type_name_nr = entry_pos;
+
+ strcpy(name, type_name_node->name);
+
+ 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);
+ }
+
+ strcpy(type_name_last, name);
+
+ if (strEqual(name, EMPTY_PLAYER_NAME))
+ strcpy(name, "");
+
+ *xpos = strlen(name);
+}
+
+static void setTypeNameValues_Name(char *name, struct TextPosInfo *pos)
+{
+ // change name of edited user in global list of user names
+ setString(&global.user_names[type_name_nr], name);
+
+ if (game_status == GAME_MODE_PSEUDO_TYPENAMES)
+ {
+ TreeInfo *node = type_name_node;
+
+ // change name of edited user in local menu tree structure
+ setString(&node->name, name);
+ setString(&node->name_sorting, name);
+
+ node->color = getPlayerNameColor(name);
+ pos->font = MENU_CHOOSE_TREE_FONT(node->color);
+ }
+}
+
+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);
+
+ if (!changed)
+ strcpy(name, type_name_last);
+
+ if (strEqual(name, ""))
+ strcpy(name, EMPTY_PLAYER_NAME);
+
+ setTypeNameValues_Name(name, pos);
+
+ // 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 = type_name_nr;
+
+ // 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();
+
+ // change name of edited user on score server
+ ApiRenamePlayerAsThread();
+
+ 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();
+ }
+}
+
+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;
+ struct TextPosInfo *pos = &pos_name;
+ char key_char = getValidConfigValueChar(getCharFromKey(key));
+ boolean is_valid_key_char = (key_char != 0 && (key_char != ' ' || xpos > 0));
+ 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)
+ {
+ name[xpos] = key_char;
+ name[xpos + 1] = 0;
+
+ xpos++;
+ }
+ else if ((key == KSYM_Delete || key == KSYM_BackSpace) && xpos > 0)
+ {
+ xpos--;
+
+ name[xpos] = 0;
+ }
+ else if (key == KSYM_Return || key == KSYM_Escape)
+ {
+ boolean changed = (key == KSYM_Return);
+
+ StopTextInput();
+
+ setTypeNameValues(name, pos, changed);
+
+ active = FALSE;
+ }
+
+ drawTypeNameText(name, pos, active);
+
+ if (!active)
+ {
+ SetGameStatus(game_status_last_screen);
+
+ if (game_status == GAME_MODE_MAIN)
+ InitializeMainControls();
+ }
+}
+
+static void DrawTypeName(void)
+{
+ HandleTypeNameExt(TRUE, 0);
+}
+
+void HandleTypeName(Key key)
+{
+ HandleTypeNameExt(FALSE, key);
+}
+
+
+// ============================================================================
+// tree menu functions
+// ============================================================================
+
+static int getAlignXOffsetFromTreeInfo(TreeInfo *ti)
+{
+ if (game_status != GAME_MODE_SETUP ||
+ DRAW_MODE_SETUP(setup_mode) != SETUP_MODE_CHOOSE_OTHER)
+ return 0;
+
+ int max_text_size = 0;
+ TreeInfo *node;
+
+ for (node = getTreeInfoFirstGroupEntry(ti); node != NULL; node = node->next)
+ max_text_size = MAX(max_text_size, strlen(node->name));
+
+ int num_entries = numTreeInfoInGroup(ti);
+ boolean scrollbar_needed = (num_entries > NUM_MENU_ENTRIES_ON_SCREEN);
+ 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);
+ int align = menu.list_setup[SETUP_MODE_CHOOSE_OTHER].align;
+ int x = ALIGNED_XPOS(0, screen_width, align) * -1;
+ int align_xoffset_raw = ALIGNED_XPOS(x, button_width + text_width, align);
+ int align_xoffset = MAX(0, align_xoffset_raw);
+
+ return align_xoffset;
+}
+
+static int getAlignYOffsetFromTreeInfo(TreeInfo *ti)
+{
+ if (game_status != GAME_MODE_SETUP ||
+ DRAW_MODE_SETUP(setup_mode) != SETUP_MODE_CHOOSE_OTHER)
+ return 0;
+
+ int num_entries = numTreeInfoInGroup(ti);
+ int num_page_entries = MIN(num_entries, NUM_MENU_ENTRIES_ON_SCREEN);
+ 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;
+ int y = ALIGNED_YPOS(0, page_height, align) * -1;
+ int align_yoffset_raw = ALIGNED_YPOS(y, text_height, align);
+ int align_yoffset = MAX(0, align_yoffset_raw);
+
+ return align_yoffset;
+}
+
+static void DrawChooseTree(TreeInfo **ti_ptr)
+{
+ int fade_mask = REDRAW_FIELD;
+
+ if (CheckFadeAll())
+ fade_mask = REDRAW_ALL;
+
+ if (strEqual((*ti_ptr)->subdir, STRING_TOP_DIRECTORY))
+ {
+ if (game_status == GAME_MODE_SETUP)
+ {
+ execSetupArtwork();
+ }
+ else // GAME_MODE_LEVELS
+ {
+ SetGameStatus(GAME_MODE_MAIN);
+
+ DrawMainMenu();
+ }
+
+ return;
+ }
+
+ UnmapAllGadgets();
+
+ FreeScreenGadgets();
+ CreateScreenGadgets();
+
+ FadeOut(fade_mask);
+
+ // needed if different viewport properties defined for choosing level (set)
+ ChangeViewportPropertiesIfNeeded();
+
+ if (game_status == GAME_MODE_NAMES)
+ SetMainBackgroundImage(IMG_BACKGROUND_NAMES);
+ else if (game_status == GAME_MODE_LEVELNR)
+ SetMainBackgroundImage(IMG_BACKGROUND_LEVELNR);
+ else if (game_status == GAME_MODE_LEVELS)
+ SetMainBackgroundImage(IMG_BACKGROUND_LEVELS);
+
+ ClearField();
+
+ OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
+
+ MapScreenTreeGadgets(*ti_ptr);
+ HandleChooseTree(0, 0, 0, 0, MB_MENU_INITIALIZE, ti_ptr);
+
+ DrawMaskedBorder(fade_mask);
+
+ FadeIn(fade_mask);
+}
+
+static void drawChooseTreeText(int y, boolean active, TreeInfo *ti)
+{
+ int num_entries = numTreeInfoInGroup(ti);
+ 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 first_entry = ti->cl_first;
+ int entry_pos = first_entry + y;
+ TreeInfo *node_first = getTreeInfoFirstGroupEntry(ti);
+ TreeInfo *node = getTreeInfoFromPos(node_first, entry_pos);
+ int font_color = MENU_CHOOSE_TREE_COLOR(node, active);
+ int font_nr = MENU_CHOOSE_TREE_FONT(font_color);
+ int font_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
+ int xpos = MENU_SCREEN_START_XPOS;
+ int ypos = MENU_SCREEN_START_YPOS + y;
+ int startx = amSX + xpos * 32;
+ int starty = amSY + ypos * 32;
+ int startx_text = startx + font_xoffset;
+ int endx_text = amSX + screen_width;
+ int max_text_size = endx_text - startx_text;
+ int max_buffer_len = max_text_size / getFontWidth(font_nr);
+ char buffer[max_buffer_len + 1];
+
+ strncpy(buffer, node->name, max_buffer_len);
+ buffer[max_buffer_len] = '\0';
+
+ DrawText(startx, starty, buffer, font_nr);
+}
+
+static void drawChooseTreeList(int first_entry, int num_page_entries,
+ TreeInfo *ti)
+{
+ int i;
+ char *title_string = NULL;
+ int yoffset_sets = MENU_TITLE1_YPOS;
+ int yoffset_setup = 16;
+ int yoffset = (ti->type == TREE_TYPE_LEVEL_DIR ||
+ ti->type == TREE_TYPE_LEVEL_NR ? yoffset_sets : yoffset_setup);
+
+ title_string = ti->infotext;
+
+ DrawTextSCentered(mSY - SY + yoffset, FONT_TITLE_1, title_string);
+
+ clearMenuListArea();
+
+ for (i = 0; i < num_page_entries; i++)
+ {
+ TreeInfo *node, *node_first;
+ int entry_pos = first_entry + i;
+
+ node_first = getTreeInfoFirstGroupEntry(ti);
+ node = getTreeInfoFromPos(node_first, entry_pos);
+
+ drawChooseTreeText(i, FALSE, ti);
+
+ if (node->parent_link)
+ initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
+ else if (node->level_group)
+ initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
+ else
+ initCursor(i, IMG_MENU_BUTTON);
+
+ if (game_status == GAME_MODE_NAMES)
+ drawChooseTreeEdit(i, FALSE);
+ }
+
+ redraw_mask |= REDRAW_FIELD;
+}
+
+static void drawChooseTreeInfo(int entry_pos, TreeInfo *ti)
+{
+ TreeInfo *node, *node_first;
+ int x, last_redraw_mask = redraw_mask;
+ int ypos = MENU_TITLE2_YPOS;
+ int font_nr = FONT_TITLE_2;
+
+ if (ti->type == TREE_TYPE_LEVEL_NR)
+ DrawTextFCentered(ypos, font_nr, leveldir_current->name);
+
+ if (ti->type != TREE_TYPE_LEVEL_DIR)
+ return;
+
+ node_first = getTreeInfoFirstGroupEntry(ti);
+ node = getTreeInfoFromPos(node_first, entry_pos);
+
+ DrawBackgroundForFont(SX, SY + ypos, SXSIZE, getFontHeight(font_nr), font_nr);