(svn r8336) -Codechange: added function to translate vehicle types to 0,1,2... for use for index to arrays

Used this function to get rid of some switch-cases in build_vehicle_gui.cpp
-Fix (r8335): ship build window didn't remember assending/decending sort setting
This commit is contained in:
bjarni 2007-01-22 01:13:10 +00:00
parent 14b2998023
commit 8f4e2e8040
2 changed files with 20 additions and 22 deletions

View File

@ -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);

View File

@ -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)