Mercurial > hg > openttd
changeset 8323:803ed468fc77 draft
(svn r11888) -Codechange: simplify sorting of the strings in town names dropdown
author | glx <glx@openttd.org> |
---|---|
date | Thu, 17 Jan 2008 02:10:26 +0000 |
parents | 04ad512648d9 |
children | df59905bdc93 |
files | src/newgrf.cpp src/settings_gui.cpp src/strings.cpp |
diffstat | 3 files changed, 31 insertions(+), 41 deletions(-) [+] |
line wrap: on
line diff
--- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -5555,7 +5555,7 @@ void InitDepotWindowBlockSizes(); -extern void SortTownGeneratorNames(); +extern void InitGRFTownGeneratorNames(); static void AfterLoadGRFs() { @@ -5586,7 +5586,7 @@ MapNewCargoStrings(); /* Update the townname generators list */ - SortTownGeneratorNames(); + InitGRFTownGeneratorNames(); } void LoadNewGRF(uint load_index, uint file_index)
--- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -72,35 +72,15 @@ } int _nb_orig_names = SPECSTR_TOWNNAME_LAST - SPECSTR_TOWNNAME_START + 1; -static StringID *_town_names = NULL; static StringID *_grf_names = NULL; static int _nb_grf_names = 0; -void SortTownGeneratorNames() +void InitGRFTownGeneratorNames() { - int n = 0; - - /* Get Newgrf generators' names */ free(_grf_names); _grf_names = GetGRFTownNameList(); _nb_grf_names = 0; for (StringID *s = _grf_names; *s != INVALID_STRING_ID; s++) _nb_grf_names++; - - /* Prepare the list */ - free(_town_names); - _town_names = MallocT<StringID>(_nb_orig_names + _nb_grf_names + 1); - - /* Put the original strings */ - for (int i = 0; i < _nb_orig_names; i++) _town_names[n++] = STR_TOWNNAME_ORIGINAL_ENGLISH + i; - - /* Put the grf strings */ - for (int i = 0; i < _nb_grf_names; i++) _town_names[n++] = _grf_names[i]; - - /* Put the terminator */ - _town_names[n] = INVALID_STRING_ID; - - /* Sort the strings */ - qsort(&_town_names[0], _nb_orig_names + _nb_grf_names, sizeof(StringID), &StringIDSorter); } static inline StringID TownName(int town_name) @@ -159,6 +139,30 @@ }; /** + * Update/redraw the townnames dropdown + * @param w the window the dropdown belongs to + * @param sel the currently selected townname generator + */ +static void ShowTownnameDropdown(Window *w, int sel) +{ + typedef std::map<StringID, int, StringIDCompare> TownList; + TownList townnames; + + /* Add and sort original townnames generators */ + for (int i = 0; i < _nb_orig_names; i++) townnames[STR_TOWNNAME_ORIGINAL_ENGLISH + i] = i; + + /* Add and sort newgrf townnames generators */ + for (int i = 0; i < _nb_grf_names; i++) townnames[_grf_names[i]] = _nb_orig_names + i; + + DropDownList *list = new DropDownList(); + for (TownList::iterator it = townnames.begin(); it != townnames.end(); it++) { + list->push_back(new DropDownListStringItem((*it).first, (*it).second, !(_game_mode == GM_MENU || (*it).second == sel))); + } + + ShowDropDownList(w, list, sel, GAMEOPT_TOWNNAME_BTN); +} + +/** * Update/redraw the languages dropdown * @param w the window the dropdown belongs to */ @@ -226,16 +230,9 @@ ShowDropDownMenu(w, _driveside_dropdown, _opt_ptr->road_side, GAMEOPT_ROADSIDE_BTN, i, 0); } break; - case GAMEOPT_TOWNNAME_TXT: case GAMEOPT_TOWNNAME_BTN: { /* Setup townname dropdown */ - uint sel = 0; - for (uint i = 0; _town_names[i] != INVALID_STRING_ID; i++) { - if (_town_names[i] == TownName(_opt_ptr->town_name)) { - sel = i; - break; - } - } - ShowDropDownMenu(w, _town_names, sel, GAMEOPT_TOWNNAME_BTN, (_game_mode == GM_MENU) ? 0 : (-1) ^ (1 << sel), 0); - } break; + case GAMEOPT_TOWNNAME_TXT: case GAMEOPT_TOWNNAME_BTN: /* Setup townname dropdown */ + ShowTownnameDropdown(w, _opt_ptr->town_name); + break; case GAMEOPT_AUTOSAVE_TXT: case GAMEOPT_AUTOSAVE_BTN: /* Setup autosave dropdown */ ShowDropDownMenu(w, _autosave_dropdown, _opt_ptr->autosave, GAMEOPT_AUTOSAVE_BTN, 0, 0); @@ -303,12 +300,7 @@ case GAMEOPT_TOWNNAME_BTN: /* Town names */ if (_game_mode == GM_MENU) { - for (uint i = 0; _town_names[i] != INVALID_STRING_ID; i++) { - if (_town_names[e->we.dropdown.index] == TownName(i)) { - _opt_ptr->town_name = i; - break; - } - } + _opt_ptr->town_name = e->we.dropdown.index; InvalidateWindow(WC_GAME_OPTIONS, 0); } break;
--- a/src/strings.cpp +++ b/src/strings.cpp @@ -1228,7 +1228,6 @@ #else /* ENABLE_NETWORK */ static inline void SortNetworkLanguages() {} #endif /* ENABLE_NETWORK */ -extern void SortTownGeneratorNames(); bool ReadLanguagePack(int lang_index) { @@ -1286,7 +1285,6 @@ _dynlang.curr = lang_index; SetCurrentGrfLangID(_langpack->isocode); SortNetworkLanguages(); - SortTownGeneratorNames(); return true; }