summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/build_vehicle_gui.cpp29
-rw-r--r--src/vehicle.h13
2 files changed, 20 insertions, 22 deletions
diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp
index e957e9698..8154b1a43 100644
--- a/src/build_vehicle_gui.cpp
+++ b/src/build_vehicle_gui.cpp
@@ -95,14 +95,9 @@ static void SetupWindowStrings(const Window *w, byte type)
}
static bool _internal_sort_order; // descending/ascending
-static byte _last_sort_criteria_train = 0;
-static bool _last_sort_order_train = false;
-static byte _last_sort_criteria_ship = 0;
-static bool _last_sort_order_ship = false;
-
-static byte _last_sort_criteria_aircraft = 0;
-static bool _last_sort_order_aircraft = false;
+static byte _last_sort_criteria[] = {0, 0, 0, 0};
+static bool _last_sort_order[] = {false, false, false, false};
static int CDECL EngineNumberSorter(const void *a, const void *b)
{
@@ -749,10 +744,7 @@ static void BuildVehicleClickEvent(Window *w, WindowEvent *e)
switch (e->we.click.widget) {
case BUILD_VEHICLE_WIDGET_SORT_ASSENDING_DESCENDING:
bv->descending_sort_order ^= true;
- switch (bv->vehicle_type) {
- case VEH_Train: _last_sort_order_train = bv->descending_sort_order; break;
- case VEH_Aircraft: _last_sort_order_aircraft = bv->descending_sort_order; break;
- }
+ _last_sort_order[VehTypeToIndex(bv->vehicle_type)] = bv->descending_sort_order;
bv->regenerate_list = true;
SetWindowDirty(w);
break;
@@ -859,11 +851,7 @@ static void NewVehicleWndProc(Window *w, WindowEvent *e)
case WE_DROPDOWN_SELECT: /* we have selected a dropdown item in the list */
if (bv->sort_criteria != e->we.dropdown.index) {
bv->sort_criteria = e->we.dropdown.index;
- switch (bv->vehicle_type) {
- case VEH_Train: _last_sort_criteria_train = bv->sort_criteria; break;
- case VEH_Ship: _last_sort_criteria_ship = bv->sort_criteria; break;
- case VEH_Aircraft: _last_sort_criteria_aircraft = bv->sort_criteria; break;
- }
+ _last_sort_criteria[VehTypeToIndex(bv->vehicle_type)] = bv->sort_criteria;
bv->regenerate_list = true;
}
SetWindowDirty(w);
@@ -912,24 +900,21 @@ void ShowBuildVehicleWindow(TileIndex tile, byte type)
bv->vehicle_type = type;
bv->regenerate_list = false;
+ bv->sort_criteria = _last_sort_criteria[VehTypeToIndex(type)];
+ bv->descending_sort_order = _last_sort_order[VehTypeToIndex(type)];
+
switch (type) {
case VEH_Train:
WP(w,buildvehicle_d).filter.railtype = (tile == 0) ? RAILTYPE_END : GetRailType(tile);
ResizeWindow(w, 0, 16);
- bv->sort_criteria = _last_sort_criteria_train;
- bv->descending_sort_order = _last_sort_order_train;
break;
case VEH_Ship:
ResizeWindow(w, 27, 0);
- bv->sort_criteria = _last_sort_criteria_ship;
- bv->descending_sort_order = _last_sort_order_ship;
break;
case VEH_Aircraft:
AcceptPlanes acc_planes = (tile == 0) ? ALL : GetAirport(GetStationByTile(tile)->airport_type)->acc_planes;
bv->filter.acc_planes = acc_planes;
ResizeWindow(w, 12, 0);
- bv->sort_criteria = _last_sort_criteria_aircraft;
- bv->descending_sort_order = _last_sort_order_aircraft;
break;
}
SetupWindowStrings(w, type);
diff --git a/src/vehicle.h b/src/vehicle.h
index 1e23fe78c..cb0025a9d 100644
--- a/src/vehicle.h
+++ b/src/vehicle.h
@@ -424,6 +424,19 @@ static inline bool IsPlayerBuildableVehicleType(const Vehicle *v)
return IsPlayerBuildableVehicleType(v->type);
}
+/** Function to give index of a vehicle type
+ * Since the return value is 0 for VEH_train, it's perfect for index to arrays
+ */
+static inline byte VehTypeToIndex(byte type)
+{
+ return type - VEH_Train;
+}
+
+static inline byte VehTypeToIndex(const Vehicle *v)
+{
+ return VehTypeToIndex(v->type);
+}
+
#define FOR_ALL_VEHICLES_FROM(v, start) for (v = GetVehicle(start); v != NULL; v = (v->index + 1U < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) if (IsValidVehicle(v))
#define FOR_ALL_VEHICLES(v) FOR_ALL_VEHICLES_FROM(v, 0)