src/settings_gui.cpp
changeset 8323 d5531c7645a7
parent 8321 96c331ee5ac3
child 8336 432b85820441
equal deleted inserted replaced
8322:916ef2361569 8323:d5531c7645a7
    70 	*p = INVALID_STRING_ID;
    70 	*p = INVALID_STRING_ID;
    71 	return buf;
    71 	return buf;
    72 }
    72 }
    73 
    73 
    74 int _nb_orig_names = SPECSTR_TOWNNAME_LAST - SPECSTR_TOWNNAME_START + 1;
    74 int _nb_orig_names = SPECSTR_TOWNNAME_LAST - SPECSTR_TOWNNAME_START + 1;
    75 static StringID *_town_names = NULL;
       
    76 static StringID *_grf_names = NULL;
    75 static StringID *_grf_names = NULL;
    77 static int _nb_grf_names = 0;
    76 static int _nb_grf_names = 0;
    78 
    77 
    79 void SortTownGeneratorNames()
    78 void InitGRFTownGeneratorNames()
    80 {
    79 {
    81 	int n = 0;
       
    82 
       
    83 	/* Get Newgrf generators' names */
       
    84 	free(_grf_names);
    80 	free(_grf_names);
    85 	_grf_names = GetGRFTownNameList();
    81 	_grf_names = GetGRFTownNameList();
    86 	_nb_grf_names = 0;
    82 	_nb_grf_names = 0;
    87 	for (StringID *s = _grf_names; *s != INVALID_STRING_ID; s++) _nb_grf_names++;
    83 	for (StringID *s = _grf_names; *s != INVALID_STRING_ID; s++) _nb_grf_names++;
    88 
       
    89 	/* Prepare the list */
       
    90 	free(_town_names);
       
    91 	_town_names = MallocT<StringID>(_nb_orig_names + _nb_grf_names + 1);
       
    92 
       
    93 	/* Put the original strings */
       
    94 	for (int i = 0; i < _nb_orig_names; i++) _town_names[n++] = STR_TOWNNAME_ORIGINAL_ENGLISH + i;
       
    95 
       
    96 	/* Put the grf strings */
       
    97 	for (int i = 0; i < _nb_grf_names; i++) _town_names[n++] = _grf_names[i];
       
    98 
       
    99 	/* Put the terminator */
       
   100 	_town_names[n] = INVALID_STRING_ID;
       
   101 
       
   102 	/* Sort the strings */
       
   103 	qsort(&_town_names[0], _nb_orig_names + _nb_grf_names, sizeof(StringID), &StringIDSorter);
       
   104 }
    84 }
   105 
    85 
   106 static inline StringID TownName(int town_name)
    86 static inline StringID TownName(int town_name)
   107 {
    87 {
   108 	if (town_name < _nb_orig_names) return STR_TOWNNAME_ORIGINAL_ENGLISH + town_name;
    88 	if (town_name < _nb_orig_names) return STR_TOWNNAME_ORIGINAL_ENGLISH + town_name;
   157 	GAMEOPT_SCREENSHOT_TXT  = 30,
   137 	GAMEOPT_SCREENSHOT_TXT  = 30,
   158 	GAMEOPT_SCREENSHOT_BTN,
   138 	GAMEOPT_SCREENSHOT_BTN,
   159 };
   139 };
   160 
   140 
   161 /**
   141 /**
       
   142  * Update/redraw the townnames dropdown
       
   143  * @param w   the window the dropdown belongs to
       
   144  * @param sel the currently selected townname generator
       
   145  */
       
   146 static void ShowTownnameDropdown(Window *w, int sel)
       
   147 {
       
   148 	typedef std::map<StringID, int, StringIDCompare> TownList;
       
   149 	TownList townnames;
       
   150 
       
   151 	/* Add and sort original townnames generators */
       
   152 	for (int i = 0; i < _nb_orig_names; i++) townnames[STR_TOWNNAME_ORIGINAL_ENGLISH + i] = i;
       
   153 
       
   154 	/* Add and sort newgrf townnames generators */
       
   155 	for (int i = 0; i < _nb_grf_names; i++) townnames[_grf_names[i]] = _nb_orig_names + i;
       
   156 
       
   157 	DropDownList *list = new DropDownList();
       
   158 	for (TownList::iterator it = townnames.begin(); it != townnames.end(); it++) {
       
   159 		list->push_back(new DropDownListStringItem((*it).first, (*it).second, !(_game_mode == GM_MENU || (*it).second == sel)));
       
   160 	}
       
   161 
       
   162 	ShowDropDownList(w, list, sel, GAMEOPT_TOWNNAME_BTN);
       
   163 }
       
   164 
       
   165 /**
   162  * Update/redraw the languages dropdown
   166  * Update/redraw the languages dropdown
   163  * @param w   the window the dropdown belongs to
   167  * @param w   the window the dropdown belongs to
   164  */
   168  */
   165 static void ShowLangDropdown(Window *w)
   169 static void ShowLangDropdown(Window *w)
   166 {
   170 {
   224 						i = (-1) ^ (1 << _opt_ptr->road_side); // disable the other value
   228 						i = (-1) ^ (1 << _opt_ptr->road_side); // disable the other value
   225 
   229 
   226 					ShowDropDownMenu(w, _driveside_dropdown, _opt_ptr->road_side, GAMEOPT_ROADSIDE_BTN, i, 0);
   230 					ShowDropDownMenu(w, _driveside_dropdown, _opt_ptr->road_side, GAMEOPT_ROADSIDE_BTN, i, 0);
   227 				} break;
   231 				} break;
   228 
   232 
   229 				case GAMEOPT_TOWNNAME_TXT: case GAMEOPT_TOWNNAME_BTN: { /* Setup townname dropdown */
   233 				case GAMEOPT_TOWNNAME_TXT: case GAMEOPT_TOWNNAME_BTN: /* Setup townname dropdown */
   230 					uint sel = 0;
   234 					ShowTownnameDropdown(w, _opt_ptr->town_name);
   231 					for (uint i = 0; _town_names[i] != INVALID_STRING_ID; i++) {
   235 					break;
   232 						if (_town_names[i] == TownName(_opt_ptr->town_name)) {
       
   233 							sel = i;
       
   234 							break;
       
   235 						}
       
   236 					}
       
   237 					ShowDropDownMenu(w, _town_names, sel, GAMEOPT_TOWNNAME_BTN, (_game_mode == GM_MENU) ? 0 : (-1) ^ (1 << sel), 0);
       
   238 				} break;
       
   239 
   236 
   240 				case GAMEOPT_AUTOSAVE_TXT: case GAMEOPT_AUTOSAVE_BTN: /* Setup autosave dropdown */
   237 				case GAMEOPT_AUTOSAVE_TXT: case GAMEOPT_AUTOSAVE_BTN: /* Setup autosave dropdown */
   241 					ShowDropDownMenu(w, _autosave_dropdown, _opt_ptr->autosave, GAMEOPT_AUTOSAVE_BTN, 0, 0);
   238 					ShowDropDownMenu(w, _autosave_dropdown, _opt_ptr->autosave, GAMEOPT_AUTOSAVE_BTN, 0, 0);
   242 					break;
   239 					break;
   243 
   240 
   301 					}
   298 					}
   302 					break;
   299 					break;
   303 
   300 
   304 				case GAMEOPT_TOWNNAME_BTN: /* Town names */
   301 				case GAMEOPT_TOWNNAME_BTN: /* Town names */
   305 					if (_game_mode == GM_MENU) {
   302 					if (_game_mode == GM_MENU) {
   306 						for (uint i = 0; _town_names[i] != INVALID_STRING_ID; i++) {
   303 						_opt_ptr->town_name = e->we.dropdown.index;
   307 							if (_town_names[e->we.dropdown.index] == TownName(i)) {
       
   308 								_opt_ptr->town_name = i;
       
   309 								break;
       
   310 							}
       
   311 						}
       
   312 						InvalidateWindow(WC_GAME_OPTIONS, 0);
   304 						InvalidateWindow(WC_GAME_OPTIONS, 0);
   313 					}
   305 					}
   314 					break;
   306 					break;
   315 
   307 
   316 				case GAMEOPT_AUTOSAVE_BTN: /* Autosave options */
   308 				case GAMEOPT_AUTOSAVE_BTN: /* Autosave options */