Codechange: Make DropDownListStringItem preformat and remove other implementations. (#11063)
Having to choose between DropDownListStringItem, DropDownListCharStringItem, and DropDownListParamStringItem depending on whether to draw a StringID, a raw string, or a StringID with extra parameters was needlessly complex. Instead, allow passing a StringID or raw string to DropDownListStringItem. This will preformat the StringID into a raw string, and can therefore accept parameters via the normal SetDParam mechanism. This also means that strings no longer need to be formatted on every draw.
This commit is contained in:
@@ -82,7 +82,7 @@ static DropDownList BuildSetDropDownList(int *selected_index, bool allow_selecti
|
||||
|
||||
DropDownList list;
|
||||
for (int i = 0; i < n; i++) {
|
||||
list.emplace_back(new DropDownListCharStringItem(T::GetSet(i)->name, i, !allow_selection && (*selected_index != i)));
|
||||
list.emplace_back(new DropDownListStringItem(T::GetSet(i)->name, i, !allow_selection && (*selected_index != i)));
|
||||
}
|
||||
|
||||
return list;
|
||||
@@ -246,20 +246,19 @@ struct GameOptionsWindow : Window {
|
||||
bool hide_language = IsReleasedVersion() && !_languages[i].IsReasonablyFinished();
|
||||
if (hide_language) continue;
|
||||
bool hide_percentage = IsReleasedVersion() || _languages[i].missing < _settings_client.gui.missing_strings_threshold;
|
||||
auto item = new DropDownListParamStringItem(hide_percentage ? STR_JUST_RAW_STRING : STR_GAME_OPTIONS_LANGUAGE_PERCENTAGE, i, false);
|
||||
if (&_languages[i] == _current_language) {
|
||||
*selected_index = i;
|
||||
item->SetParamStr(0, _languages[i].own_name);
|
||||
SetDParamStr(0, _languages[i].own_name);
|
||||
} else {
|
||||
/* Especially with sprite-fonts, not all localized
|
||||
* names can be rendered. So instead, we use the
|
||||
* international names for anything but the current
|
||||
* selected language. This avoids showing a few ????
|
||||
* entries in the dropdown list. */
|
||||
item->SetParamStr(0, _languages[i].name);
|
||||
SetDParamStr(0, _languages[i].name);
|
||||
}
|
||||
item->SetParam(1, (LANGUAGE_TOTAL_STRINGS - _languages[i].missing) * 100 / LANGUAGE_TOTAL_STRINGS);
|
||||
list.emplace_back(item);
|
||||
SetDParam(1, (LANGUAGE_TOTAL_STRINGS - _languages[i].missing) * 100 / LANGUAGE_TOTAL_STRINGS);
|
||||
list.emplace_back(new DropDownListStringItem(hide_percentage ? STR_JUST_RAW_STRING : STR_GAME_OPTIONS_LANGUAGE_PERCENTAGE, i, false));
|
||||
}
|
||||
std::sort(list.begin(), list.end(), DropDownListStringItem::NatSortFunc);
|
||||
break;
|
||||
@@ -270,10 +269,9 @@ struct GameOptionsWindow : Window {
|
||||
|
||||
*selected_index = GetCurrentResolutionIndex();
|
||||
for (uint i = 0; i < _resolutions.size(); i++) {
|
||||
auto item = new DropDownListParamStringItem(STR_GAME_OPTIONS_RESOLUTION_ITEM, i, false);
|
||||
item->SetParam(0, _resolutions[i].width);
|
||||
item->SetParam(1, _resolutions[i].height);
|
||||
list.emplace_back(item);
|
||||
SetDParam(0, _resolutions[i].width);
|
||||
SetDParam(1, _resolutions[i].height);
|
||||
list.emplace_back(new DropDownListStringItem(STR_GAME_OPTIONS_RESOLUTION_ITEM, i, false));
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -281,9 +279,8 @@ struct GameOptionsWindow : Window {
|
||||
for (auto it = _refresh_rates.begin(); it != _refresh_rates.end(); it++) {
|
||||
auto i = std::distance(_refresh_rates.begin(), it);
|
||||
if (*it == _settings_client.gui.refresh_rate) *selected_index = i;
|
||||
auto item = new DropDownListParamStringItem(STR_GAME_OPTIONS_REFRESH_RATE_ITEM, i, false);
|
||||
item->SetParam(0, *it);
|
||||
list.emplace_back(item);
|
||||
SetDParam(0, *it);
|
||||
list.emplace_back(new DropDownListStringItem(STR_GAME_OPTIONS_REFRESH_RATE_ITEM, i, false));
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user