diff options
author | glx <glx@openttd.org> | 2008-01-17 02:10:26 +0000 |
---|---|---|
committer | glx <glx@openttd.org> | 2008-01-17 02:10:26 +0000 |
commit | 3d61a1adc51000d7769de19bf2fc142b0d014f93 (patch) | |
tree | 8767cdd3d80024fd8cb04f353b19f9ad335c06a0 /src | |
parent | f658b79e152a178a1a6b8c5d585a523de15f1830 (diff) | |
download | openttd-3d61a1adc51000d7769de19bf2fc142b0d014f93.tar.xz |
(svn r11888) -Codechange: simplify sorting of the strings in town names dropdown
Diffstat (limited to 'src')
-rw-r--r-- | src/newgrf.cpp | 4 | ||||
-rw-r--r-- | src/settings_gui.cpp | 66 | ||||
-rw-r--r-- | src/strings.cpp | 2 |
3 files changed, 31 insertions, 41 deletions
diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 399964b42..af6570d98 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -5555,7 +5555,7 @@ void LoadNewGRFFile(GRFConfig *config, uint file_index, GrfLoadingStage stage) void InitDepotWindowBlockSizes(); -extern void SortTownGeneratorNames(); +extern void InitGRFTownGeneratorNames(); static void AfterLoadGRFs() { @@ -5586,7 +5586,7 @@ static void AfterLoadGRFs() MapNewCargoStrings(); /* Update the townname generators list */ - SortTownGeneratorNames(); + InitGRFTownGeneratorNames(); } void LoadNewGRF(uint load_index, uint file_index) diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index c007a6d37..69c75ec2c 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -72,35 +72,15 @@ static StringID *BuildDynamicDropdown(StringID base, int num) } 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 @@ enum GameOptionsWidgets { }; /** + * 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 @@ static void GameOptionsWndProc(Window *w, WindowEvent *e) 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 @@ static void GameOptionsWndProc(Window *w, WindowEvent *e) 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; diff --git a/src/strings.cpp b/src/strings.cpp index 645cb9615..4eb19d77d 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -1228,7 +1228,6 @@ extern void SortNetworkLanguages(); #else /* ENABLE_NETWORK */ static inline void SortNetworkLanguages() {} #endif /* ENABLE_NETWORK */ -extern void SortTownGeneratorNames(); bool ReadLanguagePack(int lang_index) { @@ -1286,7 +1285,6 @@ bool ReadLanguagePack(int lang_index) _dynlang.curr = lang_index; SetCurrentGrfLangID(_langpack->isocode); SortNetworkLanguages(); - SortTownGeneratorNames(); return true; } |