Skip to content

Commit

Permalink
Codechange: Use functions to create common drop down list items.
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterN committed Apr 1, 2024
1 parent 11aa369 commit 56cac21
Show file tree
Hide file tree
Showing 22 changed files with 191 additions and 137 deletions.
3 changes: 2 additions & 1 deletion src/airport_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "newgrf_airport.h"
#include "newgrf_callbacks.h"
#include "dropdown_type.h"
#include "dropdown_func.h"
#include "core/geometry_func.hpp"
#include "hotkeys.h"
#include "vehicle_func.h"
Expand Down Expand Up @@ -241,7 +242,7 @@ class BuildAirportWindow : public PickerWindowBase {
DropDownList list;

for (uint i = 0; i < AirportClass::GetClassCount(); i++) {
list.push_back(std::make_unique<DropDownListStringItem>(AirportClass::Get((AirportClassID)i)->name, i, false));
list.push_back(MakeDropDownListStringItem(AirportClass::Get((AirportClassID)i)->name, i));
}

return list;
Expand Down
5 changes: 3 additions & 2 deletions src/autoreplace_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "core/geometry_func.hpp"
#include "rail_gui.h"
#include "road_gui.h"
#include "dropdown_type.h"
#include "dropdown_func.h"
#include "autoreplace_cmd.h"
#include "group_cmd.h"
Expand Down Expand Up @@ -563,8 +564,8 @@ class ReplaceVehicleWindow : public Window {

case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN: {
DropDownList list;
list.push_back(std::make_unique<DropDownListStringItem>(STR_REPLACE_ENGINES, 1, false));
list.push_back(std::make_unique<DropDownListStringItem>(STR_REPLACE_WAGONS, 0, false));
list.push_back(MakeDropDownListStringItem(STR_REPLACE_ENGINES, 1));
list.push_back(MakeDropDownListStringItem(STR_REPLACE_WAGONS, 0));
ShowDropDownList(this, std::move(list), this->replace_engines ? 1 : 0, WID_RV_TRAIN_ENGINEWAGON_DROPDOWN);
break;
}
Expand Down
9 changes: 5 additions & 4 deletions src/build_vehicle_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "window_func.h"
#include "timer/timer_game_calendar.h"
#include "vehicle_func.h"
#include "dropdown_type.h"
#include "dropdown_func.h"
#include "engine_gui.h"
#include "cargotype.h"
Expand Down Expand Up @@ -1565,20 +1566,20 @@ struct BuildVehicleWindow : Window {
DropDownList list;

/* Add item for disabling filtering. */
list.push_back(std::make_unique<DropDownListStringItem>(this->GetCargoFilterLabel(CargoFilterCriteria::CF_ANY), CargoFilterCriteria::CF_ANY, false));
list.push_back(MakeDropDownListStringItem(this->GetCargoFilterLabel(CargoFilterCriteria::CF_ANY), CargoFilterCriteria::CF_ANY));
/* Specific filters for trains. */
if (this->vehicle_type == VEH_TRAIN) {
/* Add item for locomotives only in case of trains. */
list.push_back(std::make_unique<DropDownListStringItem>(this->GetCargoFilterLabel(CargoFilterCriteria::CF_ENGINES), CargoFilterCriteria::CF_ENGINES, false));
list.push_back(MakeDropDownListStringItem(this->GetCargoFilterLabel(CargoFilterCriteria::CF_ENGINES), CargoFilterCriteria::CF_ENGINES));
/* Add item for vehicles not carrying anything, e.g. train engines.
* This could also be useful for eyecandy vehicles of other types, but is likely too confusing for joe, */
list.push_back(std::make_unique<DropDownListStringItem>(this->GetCargoFilterLabel(CargoFilterCriteria::CF_NONE), CargoFilterCriteria::CF_NONE, false));
list.push_back(MakeDropDownListStringItem(this->GetCargoFilterLabel(CargoFilterCriteria::CF_NONE), CargoFilterCriteria::CF_NONE));
}

/* Add cargos */
Dimension d = GetLargestCargoIconSize();
for (const CargoSpec *cs : _sorted_standard_cargo_specs) {
list.push_back(std::make_unique<DropDownListIconItem>(d, cs->GetCargoIcon(), PAL_NONE, cs->name, cs->Index(), false));
list.push_back(MakeDropDownListIconItem(d, cs->GetCargoIcon(), PAL_NONE, cs->name, cs->Index()));
}

return list;
Expand Down
7 changes: 4 additions & 3 deletions src/date_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "date_gui.h"
#include "core/geometry_func.hpp"
#include "dropdown_type.h"
#include "dropdown_func.h"

#include "widgets/date_widget.h"

Expand Down Expand Up @@ -75,22 +76,22 @@ struct SetDateWindow : Window {

case WID_SD_DAY:
for (uint i = 0; i < 31; i++) {
list.push_back(std::make_unique<DropDownListStringItem>(STR_DAY_NUMBER_1ST + i, i + 1, false));
list.push_back(MakeDropDownListStringItem(STR_DAY_NUMBER_1ST + i, i + 1));
}
selected = this->date.day;
break;

case WID_SD_MONTH:
for (uint i = 0; i < 12; i++) {
list.push_back(std::make_unique<DropDownListStringItem>(STR_MONTH_JAN + i, i, false));
list.push_back(MakeDropDownListStringItem(STR_MONTH_JAN + i, i));
}
selected = this->date.month;
break;

case WID_SD_YEAR:
for (TimerGameEconomy::Year i = this->min_year; i <= this->max_year; i++) {
SetDParam(0, i);
list.push_back(std::make_unique<DropDownListStringItem>(STR_JUST_INT, i.base(), false));
list.push_back(MakeDropDownListStringItem(STR_JUST_INT, i.base()));
}
selected = this->date.year.base();
break;
Expand Down
32 changes: 31 additions & 1 deletion src/dropdown.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "stdafx.h"
#include "dropdown_type.h"
#include "dropdown_func.h"
#include "strings_func.h"
#include "timer/timer.h"
#include "timer/timer_window.h"
Expand All @@ -20,6 +21,35 @@

#include "safeguards.h"

std::unique_ptr<DropDownListItem> MakeDropDownListDividerItem()
{
return std::make_unique<DropDownListDividerItem>(-1);
}

std::unique_ptr<DropDownListItem> MakeDropDownListStringItem(StringID str, int value, bool masked, bool shaded)
{
return std::make_unique<DropDownListStringItem>(str, value, masked, shaded);
}

std::unique_ptr<DropDownListItem> MakeDropDownListStringItem(const std::string &str, int value, bool masked, bool shaded)
{
return std::make_unique<DropDownListStringItem>(str, value, masked, shaded);
}

std::unique_ptr<DropDownListItem> MakeDropDownListIconItem(SpriteID sprite, PaletteID palette, StringID str, int value, bool masked, bool shaded)
{
return std::make_unique<DropDownListIconItem>(sprite, palette, str, value, masked, shaded);
}

std::unique_ptr<DropDownListItem> MakeDropDownListIconItem(const Dimension &dim, SpriteID sprite, PaletteID palette, StringID str, int value, bool masked, bool shaded)
{
return std::make_unique<DropDownListIconItem>(dim, sprite, palette, str, value, masked, shaded);
}

std::unique_ptr<DropDownListItem> MakeDropDownListCheckedItem(bool checked, StringID str, int value, bool masked, bool shaded)
{
return std::make_unique<DropDownListCheckedItem>(checked, str, value, masked, shaded);
}

static constexpr NWidgetPart _nested_dropdown_menu_widgets[] = {
NWidget(NWID_HORIZONTAL),
Expand Down Expand Up @@ -408,7 +438,7 @@ void ShowDropDownMenu(Window *w, const StringID *strings, int selected, WidgetID

for (uint i = 0; strings[i] != INVALID_STRING_ID; i++) {
if (!HasBit(hidden_mask, i)) {
list.push_back(std::make_unique<DropDownListStringItem>(strings[i], i, HasBit(disabled_mask, i)));
list.push_back(MakeDropDownListStringItem(strings[i], i, HasBit(disabled_mask, i)));
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/dropdown_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@
/* Show drop down menu containing a fixed list of strings */
void ShowDropDownMenu(Window *w, const StringID *strings, int selected, WidgetID button, uint32_t disabled_mask, uint32_t hidden_mask, uint width = 0);

/* Helper functions for commonly used drop down list items. */
std::unique_ptr<DropDownListItem> MakeDropDownListDividerItem();
std::unique_ptr<DropDownListItem> MakeDropDownListStringItem(StringID str, int value, bool masked = false, bool shaded = false);
std::unique_ptr<DropDownListItem> MakeDropDownListStringItem(const std::string &str, int value, bool masked = false, bool shaded = false);
std::unique_ptr<DropDownListItem> MakeDropDownListIconItem(SpriteID sprite, PaletteID palette, StringID str, int value, bool masked = false, bool shaded = false);
std::unique_ptr<DropDownListItem> MakeDropDownListIconItem(const Dimension &dim, SpriteID sprite, PaletteID palette, StringID str, int value, bool masked = false, bool shaded = false);
std::unique_ptr<DropDownListItem> MakeDropDownListCheckedItem(bool checked, StringID str, int value, bool masked = false, bool shaded = false);

#endif /* DROPDOWN_FUNC_H */
3 changes: 2 additions & 1 deletion src/game/game_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "../window_func.h"
#include "../network/network.h"
#include "../network/network_content.h"
#include "../dropdown_type.h"
#include "../dropdown_func.h"
#include "../timer/timer.h"
#include "../timer/timer_window.h"
Expand Down Expand Up @@ -317,7 +318,7 @@ struct GSConfigWindow : public Window {

DropDownList list;
for (int i = config_item.min_value; i <= config_item.max_value; i++) {
list.push_back(std::make_unique<DropDownListStringItem>(config_item.labels.find(i)->second, i, false));
list.push_back(MakeDropDownListStringItem(config_item.labels.find(i)->second, i));
}

ShowDropDownListAt(this, std::move(list), old_val, WID_GSC_SETTING_DROPDOWN, wi_rect, COLOUR_ORANGE);
Expand Down
8 changes: 4 additions & 4 deletions src/genworld_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ static DropDownList BuildMapsizeDropDown()

for (uint i = MIN_MAP_SIZE_BITS; i <= MAX_MAP_SIZE_BITS; i++) {
SetDParam(0, 1LL << i);
list.push_back(std::make_unique<DropDownListStringItem>(STR_JUST_INT, i, false));
list.push_back(MakeDropDownListStringItem(STR_JUST_INT, i));
}

return list;
Expand All @@ -351,20 +351,20 @@ static DropDownList BuildTownNameDropDown()
/* Add and sort newgrf townnames generators */
const auto &grf_names = GetGRFTownNameList();
for (uint i = 0; i < grf_names.size(); i++) {
list.push_back(std::make_unique<DropDownListStringItem>(grf_names[i], BUILTIN_TOWNNAME_GENERATOR_COUNT + i, false));
list.push_back(MakeDropDownListStringItem(grf_names[i], BUILTIN_TOWNNAME_GENERATOR_COUNT + i));
}
std::sort(list.begin(), list.end(), DropDownListStringItem::NatSortFunc);

size_t newgrf_size = list.size();
/* Insert newgrf_names at the top of the list */
if (newgrf_size > 0) {
list.push_back(std::make_unique<DropDownListDividerItem>(-1, false)); // separator line
list.push_back(MakeDropDownListDividerItem()); // separator line
newgrf_size++;
}

/* Add and sort original townnames generators */
for (uint i = 0; i < BUILTIN_TOWNNAME_GENERATOR_COUNT; i++) {
list.push_back(std::make_unique<DropDownListStringItem>(STR_MAPGEN_TOWN_NAME_ORIGINAL_ENGLISH + i, i, false));
list.push_back(MakeDropDownListStringItem(STR_MAPGEN_TOWN_NAME_ORIGINAL_ENGLISH + i, i));
}
std::sort(list.begin() + newgrf_size, list.end(), DropDownListStringItem::NatSortFunc);

Expand Down
10 changes: 5 additions & 5 deletions src/industry_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1759,14 +1759,14 @@ class IndustryDirectoryWindow : public Window {
DropDownList list;

/* Add item for disabling filtering. */
list.push_back(std::make_unique<DropDownListStringItem>(this->GetCargoFilterLabel(CargoFilterCriteria::CF_ANY), CargoFilterCriteria::CF_ANY, false));
list.push_back(MakeDropDownListStringItem(this->GetCargoFilterLabel(CargoFilterCriteria::CF_ANY), CargoFilterCriteria::CF_ANY));
/* Add item for industries not producing anything, e.g. power plants */
list.push_back(std::make_unique<DropDownListStringItem>(this->GetCargoFilterLabel(CargoFilterCriteria::CF_NONE), CargoFilterCriteria::CF_NONE, false));
list.push_back(MakeDropDownListStringItem(this->GetCargoFilterLabel(CargoFilterCriteria::CF_NONE), CargoFilterCriteria::CF_NONE));

/* Add cargos */
Dimension d = GetLargestCargoIconSize();
for (const CargoSpec *cs : _sorted_standard_cargo_specs) {
list.push_back(std::make_unique<DropDownListIconItem>(d, cs->GetCargoIcon(), PAL_NONE, cs->name, cs->Index(), false));
list.push_back(MakeDropDownListIconItem(d, cs->GetCargoIcon(), PAL_NONE, cs->name, cs->Index()));
}

return list;
Expand Down Expand Up @@ -3101,7 +3101,7 @@ struct IndustryCargoesWindow : public Window {
DropDownList lst;
Dimension d = GetLargestCargoIconSize();
for (const CargoSpec *cs : _sorted_standard_cargo_specs) {
lst.push_back(std::make_unique<DropDownListIconItem>(d, cs->GetCargoIcon(), PAL_NONE, cs->name, cs->Index(), false));
lst.push_back(MakeDropDownListIconItem(d, cs->GetCargoIcon(), PAL_NONE, cs->name, cs->Index()));
}
if (!lst.empty()) {
int selected = (this->ind_cargo >= NUM_INDUSTRYTYPES) ? (int)(this->ind_cargo - NUM_INDUSTRYTYPES) : -1;
Expand All @@ -3115,7 +3115,7 @@ struct IndustryCargoesWindow : public Window {
for (IndustryType ind : _sorted_industry_types) {
const IndustrySpec *indsp = GetIndustrySpec(ind);
if (!indsp->enabled) continue;
lst.push_back(std::make_unique<DropDownListStringItem>(indsp->name, ind, false));
lst.push_back(MakeDropDownListStringItem(indsp->name, ind));
}
if (!lst.empty()) {
int selected = (this->ind_cargo < NUM_INDUSTRYTYPES) ? (int)this->ind_cargo : -1;
Expand Down
14 changes: 7 additions & 7 deletions src/network/network_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ static DropDownList BuildVisibilityDropDownList()
{
DropDownList list;

list.push_back(std::make_unique<DropDownListStringItem>(STR_NETWORK_SERVER_VISIBILITY_LOCAL, SERVER_GAME_TYPE_LOCAL, false));
list.push_back(std::make_unique<DropDownListStringItem>(STR_NETWORK_SERVER_VISIBILITY_INVITE_ONLY, SERVER_GAME_TYPE_INVITE_ONLY, false));
list.push_back(std::make_unique<DropDownListStringItem>(STR_NETWORK_SERVER_VISIBILITY_PUBLIC, SERVER_GAME_TYPE_PUBLIC, false));
list.push_back(MakeDropDownListStringItem(STR_NETWORK_SERVER_VISIBILITY_LOCAL, SERVER_GAME_TYPE_LOCAL));
list.push_back(MakeDropDownListStringItem(STR_NETWORK_SERVER_VISIBILITY_INVITE_ONLY, SERVER_GAME_TYPE_INVITE_ONLY));
list.push_back(MakeDropDownListStringItem(STR_NETWORK_SERVER_VISIBILITY_PUBLIC, SERVER_GAME_TYPE_PUBLIC));

return list;
}
Expand Down Expand Up @@ -1496,8 +1496,8 @@ struct NetworkClientListWindow : Window {
static void OnClickClientAdmin([[maybe_unused]] NetworkClientListWindow *w, [[maybe_unused]] Point pt, ClientID client_id)
{
DropDownList list;
list.push_back(std::make_unique<DropDownListStringItem>(STR_NETWORK_CLIENT_LIST_ADMIN_CLIENT_KICK, DD_CLIENT_ADMIN_KICK, false));
list.push_back(std::make_unique<DropDownListStringItem>(STR_NETWORK_CLIENT_LIST_ADMIN_CLIENT_BAN, DD_CLIENT_ADMIN_BAN, false));
list.push_back(MakeDropDownListStringItem(STR_NETWORK_CLIENT_LIST_ADMIN_CLIENT_KICK, DD_CLIENT_ADMIN_KICK));
list.push_back(MakeDropDownListStringItem(STR_NETWORK_CLIENT_LIST_ADMIN_CLIENT_BAN, DD_CLIENT_ADMIN_BAN));

Rect wi_rect;
wi_rect.left = pt.x;
Expand All @@ -1518,8 +1518,8 @@ struct NetworkClientListWindow : Window {
static void OnClickCompanyAdmin([[maybe_unused]] NetworkClientListWindow *w, [[maybe_unused]] Point pt, CompanyID company_id)
{
DropDownList list;
list.push_back(std::make_unique<DropDownListStringItem>(STR_NETWORK_CLIENT_LIST_ADMIN_COMPANY_RESET, DD_COMPANY_ADMIN_RESET, NetworkCompanyHasClients(company_id)));
list.push_back(std::make_unique<DropDownListStringItem>(STR_NETWORK_CLIENT_LIST_ADMIN_COMPANY_UNLOCK, DD_COMPANY_ADMIN_UNLOCK, !NetworkCompanyIsPassworded(company_id)));
list.push_back(MakeDropDownListStringItem(STR_NETWORK_CLIENT_LIST_ADMIN_COMPANY_RESET, DD_COMPANY_ADMIN_RESET, NetworkCompanyHasClients(company_id)));
list.push_back(MakeDropDownListStringItem(STR_NETWORK_CLIENT_LIST_ADMIN_COMPANY_UNLOCK, DD_COMPANY_ADMIN_UNLOCK, !NetworkCompanyIsPassworded(company_id)));

Rect wi_rect;
wi_rect.left = pt.x;
Expand Down
6 changes: 3 additions & 3 deletions src/newgrf_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ struct NewGRFParametersWindow : public Window {

DropDownList list;
for (uint32_t i = par_info.min_value; i <= par_info.max_value; i++) {
list.push_back(std::make_unique<DropDownListStringItem>(GetGRFStringFromGRFText(par_info.value_names.find(i)->second), i, false));
list.push_back(MakeDropDownListStringItem(GetGRFStringFromGRFText(par_info.value_names.find(i)->second), i));
}

ShowDropDownListAt(this, std::move(list), old_val, WID_NP_SETTING_DROPDOWN, wi_rect, COLOUR_ORANGE);
Expand Down Expand Up @@ -955,10 +955,10 @@ struct NewGRFWindow : public Window, NewGRFScanCallback {
DropDownList list;

/* Add 'None' option for clearing list */
list.push_back(std::make_unique<DropDownListStringItem>(STR_NONE, -1, false));
list.push_back(MakeDropDownListStringItem(STR_NONE, -1));

for (uint i = 0; i < this->grf_presets.size(); i++) {
list.push_back(std::make_unique<DropDownListStringItem>(this->grf_presets[i], i, false));
list.push_back(MakeDropDownListStringItem(this->grf_presets[i], i));
}

this->CloseChildWindows(WC_QUERY_STRING); // Remove the parameter query window
Expand Down
2 changes: 1 addition & 1 deletion src/order_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1350,7 +1350,7 @@ struct OrdersWindow : public Window {
case WID_O_COND_VARIABLE: {
DropDownList list;
for (uint i = 0; i < lengthof(_order_conditional_variable); i++) {
list.push_back(std::make_unique<DropDownListStringItem>(STR_ORDER_CONDITIONAL_LOAD_PERCENTAGE + _order_conditional_variable[i], _order_conditional_variable[i], false));
list.push_back(MakeDropDownListStringItem(STR_ORDER_CONDITIONAL_LOAD_PERCENTAGE + _order_conditional_variable[i], _order_conditional_variable[i]));
}
ShowDropDownList(this, std::move(list), this->vehicle->GetOrder(this->OrderGetSel())->GetConditionVariable(), WID_O_COND_VARIABLE);
break;
Expand Down
9 changes: 5 additions & 4 deletions src/rail_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "sound_func.h"
#include "company_func.h"
#include "dropdown_type.h"
#include "dropdown_func.h"
#include "tunnelbridge.h"
#include "tilehighlight_func.h"
#include "spritecache.h"
Expand Down Expand Up @@ -2385,7 +2386,7 @@ DropDownList GetRailTypeDropDownList(bool for_replacement, bool all_option)
DropDownList list;

if (all_option) {
list.push_back(std::make_unique<DropDownListStringItem>(STR_REPLACE_ALL_RAILTYPE, INVALID_RAILTYPE, false));
list.push_back(MakeDropDownListStringItem(STR_REPLACE_ALL_RAILTYPE, INVALID_RAILTYPE));
}

Dimension d = { 0, 0 };
Expand All @@ -2407,16 +2408,16 @@ DropDownList GetRailTypeDropDownList(bool for_replacement, bool all_option)
SetDParam(0, rti->strings.menu_text);
SetDParam(1, rti->max_speed);
if (for_replacement) {
list.push_back(std::make_unique<DropDownListStringItem>(rti->strings.replace_text, rt, !HasBit(avail_railtypes, rt)));
list.push_back(MakeDropDownListStringItem(rti->strings.replace_text, rt, !HasBit(avail_railtypes, rt)));
} else {
StringID str = rti->max_speed > 0 ? STR_TOOLBAR_RAILTYPE_VELOCITY : STR_JUST_STRING;
list.push_back(std::make_unique<DropDownListIconItem>(d, rti->gui_sprites.build_x_rail, PAL_NONE, str, rt, !HasBit(avail_railtypes, rt)));
list.push_back(MakeDropDownListIconItem(d, rti->gui_sprites.build_x_rail, PAL_NONE, str, rt, !HasBit(avail_railtypes, rt)));
}
}

if (list.empty()) {
/* Empty dropdowns are not allowed */
list.push_back(std::make_unique<DropDownListStringItem>(STR_NONE, INVALID_RAILTYPE, true));
list.push_back(MakeDropDownListStringItem(STR_NONE, INVALID_RAILTYPE, true));
}

return list;
Expand Down
Loading

0 comments on commit 56cac21

Please sign in to comment.