(svn r165) -Feature: Option to sort vehicles in vehicle-list window by different criteria. Total independent sort for all types and players. Periodic resort of list every 10 TTD days. Thank you for your graphical inspiration follow and buxo (since none of you provided any code).
-Fix: Sorter icon pointing down 'v' sorts in every window lowest value first, '^' highest value first -CodeChange: move Dropdownlist from settings_gui.c to widget.c. More in place there.pull/155/head
parent
df1397a47e
commit
bf0652d3fc
@ -0,0 +1,147 @@
|
||||
#include "stdafx.h"
|
||||
#include "ttd.h"
|
||||
|
||||
#include "vehicle.h"
|
||||
|
||||
/* General Vehicle GUI based procedures that are independent of vehicle types */
|
||||
void InitializeVehiclesGuiList()
|
||||
{
|
||||
bool *i;
|
||||
for (i = _train_sort_dirty; i != endof(_train_sort_dirty); i++)
|
||||
*i = true;
|
||||
|
||||
for (i = _aircraft_sort_dirty; i != endof(_aircraft_sort_dirty); i++)
|
||||
*i = true;
|
||||
|
||||
for (i = _ship_sort_dirty; i != endof(_ship_sort_dirty); i++)
|
||||
*i = true;
|
||||
|
||||
for (i = _road_sort_dirty; i != endof(_road_sort_dirty); i++)
|
||||
*i = true;
|
||||
|
||||
for (i = _vehicle_sort_dirty; i != endof(_vehicle_sort_dirty); i++)
|
||||
*i = true;
|
||||
|
||||
//memset(_train_sort_dirty, true, sizeof(_train_sort_dirty));
|
||||
}
|
||||
|
||||
// draw the vehicle profit button in the vehicle list window.
|
||||
void DrawVehicleProfitButton(Vehicle *v, int x, int y)
|
||||
{
|
||||
uint32 ormod;
|
||||
|
||||
// draw profit-based colored icons
|
||||
if(v->age <= 365 * 2)
|
||||
ormod = 0x3158000; // grey
|
||||
else if(v->profit_last_year < 0)
|
||||
ormod = 0x30b8000; //red
|
||||
else if(v->profit_last_year < 10000)
|
||||
ormod = 0x30a8000; // yellow
|
||||
else
|
||||
ormod = 0x30d8000; // green
|
||||
DrawSprite((SPR_OPENTTD_BASE + 10) | ormod, x, y);
|
||||
}
|
||||
|
||||
/************ Sorter functions *****************/
|
||||
int CDECL GeneralOwnerSorter(const void *a, const void *b)
|
||||
{
|
||||
return (*(SortStruct*)a).owner - (*(SortStruct*)b).owner;
|
||||
}
|
||||
|
||||
static char _bufcache[64]; // used together with _last_vehicle_idx to hopefully speed up stringsorting
|
||||
|
||||
/* Variables you need to set before calling this function!
|
||||
* 1. (uint32)_internal_name_sorter_id: default StringID of the vehicle when no name is set. eg
|
||||
* STR_SV_TRAIN_NAME for trains or STR_SV_AIRCRAFT_NAME for aircraft
|
||||
* 2. (bool)_internal_sort_order: sorting order, descending/ascending
|
||||
*/
|
||||
int CDECL VehicleNameSorter(const void *a, const void *b)
|
||||
{
|
||||
SortStruct *cmp1 = (SortStruct*)a;
|
||||
SortStruct *cmp2 = (SortStruct*)b;
|
||||
Vehicle *v;
|
||||
char buf1[64] = "\0";
|
||||
int r;
|
||||
|
||||
v = DEREF_VEHICLE(cmp1->index);
|
||||
if (v->string_id != _internal_name_sorter_id) {
|
||||
SET_DPARAM16(0, v->string_id);
|
||||
GetString(buf1, STR_0315);
|
||||
}
|
||||
|
||||
if ( cmp2->index != _last_vehicle_idx) {
|
||||
_last_vehicle_idx = cmp2->index;
|
||||
v = DEREF_VEHICLE(cmp2->index);
|
||||
_bufcache[0] = '\0';
|
||||
if (v->string_id != _internal_name_sorter_id) {
|
||||
SET_DPARAM16(0, v->string_id);
|
||||
GetString(_bufcache, STR_0315);
|
||||
}
|
||||
}
|
||||
|
||||
r = strcmp(buf1, _bufcache); // sort by name
|
||||
if (_internal_sort_order & 1) r = -r;
|
||||
return r;
|
||||
}
|
||||
|
||||
/* Variables you need to set before calling this function!
|
||||
* 1. (byte)_internal_sort_type: the criteria by which to sort these vehicles (number, age, etc)
|
||||
* 2. (bool)_internal_sort_order: sorting order, descending/ascending
|
||||
*/
|
||||
int CDECL GeneralVehicleSorter(const void *a, const void *b)
|
||||
{
|
||||
SortStruct *cmp1 = (SortStruct*)a;
|
||||
SortStruct *cmp2 = (SortStruct*)b;
|
||||
Vehicle *va = DEREF_VEHICLE(cmp1->index);
|
||||
Vehicle *vb = DEREF_VEHICLE(cmp2->index);
|
||||
int r;
|
||||
|
||||
switch (_internal_sort_type) {
|
||||
case SORT_BY_UNSORTED: /* Sort unsorted */
|
||||
return va->index - vb->index;
|
||||
case SORT_BY_NUMBER: /* Sort by Number */
|
||||
r = va->unitnumber - vb->unitnumber;
|
||||
break;
|
||||
/* case SORT_BY_NAME: Sort by Name (VehicleNameSorter)*/
|
||||
case SORT_BY_AGE: /* Sort by Age */
|
||||
r = va->age - vb->age;
|
||||
break;
|
||||
case SORT_BY_PROFIT_THIS_YEAR: /* Sort by Profit this year */
|
||||
r = va->profit_this_year - vb->profit_this_year;
|
||||
break;
|
||||
case SORT_BY_PROFIT_LAST_YEAR: /* Sort by Profit last year */
|
||||
r = va->profit_last_year - vb->profit_last_year;
|
||||
break;
|
||||
case SORT_BY_TOTAL_CAPACITY_PER_CARGOTYPE: { /* Sort by Total capacity per cargotype */
|
||||
// FIXME - someone write a normal cargo sorter that also works by cargo_cap,
|
||||
// FIXME - since I seem to be unable to do so :S
|
||||
Vehicle *ua = va;
|
||||
Vehicle *ub = vb;
|
||||
int i;
|
||||
byte _cargo_counta[NUM_CARGO];
|
||||
byte _cargo_countb[NUM_CARGO];
|
||||
do {
|
||||
_cargo_counta[ua->cargo_type]++;
|
||||
} while ( (ua = ua->next) != NULL);
|
||||
do {
|
||||
_cargo_countb[ub->cargo_type]++;
|
||||
} while ( (ub = ub->next) != NULL);
|
||||
|
||||
for (i = 0; i < NUM_CARGO; i++) {
|
||||
r = _cargo_counta[i] - _cargo_countb[i];
|
||||
if (r != 0)
|
||||
break;
|
||||
}
|
||||
} break;
|
||||
case SORT_BY_RELIABILITY: /* Sort by Reliability */
|
||||
r = va->reliability - vb->reliability;
|
||||
break;
|
||||
case SORT_BY_MAX_SPEED: /* Sort by Max speed */
|
||||
r = va->max_speed - vb->max_speed;
|
||||
break;
|
||||
default: NOT_REACHED();
|
||||
}
|
||||
|
||||
if (_internal_sort_order & 1) r = -r;
|
||||
return r;
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
#ifndef VEHICLE_GUI_H
|
||||
#define VEHICLE_GUI_H
|
||||
|
||||
void DrawVehicleProfitButton(Vehicle *v, int x, int y);
|
||||
void InitializeVehiclesGuiList();
|
||||
|
||||
/* sorter stuff */
|
||||
int CDECL GeneralOwnerSorter (const void *a, const void *b);
|
||||
int CDECL VehicleNameSorter (const void *a, const void *b);
|
||||
int CDECL GeneralVehicleSorter(const void *a, const void *b);
|
||||
VARDEF uint32 _internal_name_sorter_id; // internal StringID for default vehicle-names
|
||||
VARDEF uint32 _last_vehicle_idx; // cached index to hopefully speed up name-sorting
|
||||
VARDEF bool _internal_sort_order; // descending/ascending
|
||||
VARDEF byte _internal_sort_type; // Miscalleneous sorting criteria
|
||||
|
||||
typedef struct SortStruct { // store owner through sorting process
|
||||
uint32 index;
|
||||
byte owner;
|
||||
} SortStruct;
|
||||
|
||||
#define PERIODIC_RESORT_DAYS 10
|
||||
|
||||
enum VehicleSortListingTypes {
|
||||
SORT_BY_UNSORTED = 0,
|
||||
SORT_BY_NUMBER = 1,
|
||||
SORT_BY_NAME = 2,
|
||||
SORT_BY_AGE = 3,
|
||||
SORT_BY_PROFIT_THIS_YEAR = 4,
|
||||
SORT_BY_PROFIT_LAST_YEAR = 5,
|
||||
SORT_BY_TOTAL_CAPACITY_PER_CARGOTYPE = 6,
|
||||
SORT_BY_RELIABILITY = 7,
|
||||
SORT_BY_MAX_SPEED = 8
|
||||
};
|
||||
|
||||
static const uint16 _vehicle_sort_listing[] = {
|
||||
STR_SORT_BY_UNSORTED,
|
||||
STR_SORT_BY_NUMBER,
|
||||
STR_SORT_BY_DROPDOWN_NAME,
|
||||
STR_SORT_BY_AGE,
|
||||
STR_SORT_BY_PROFIT_THIS_YEAR,
|
||||
STR_SORT_BY_PROFIT_LAST_YEAR,
|
||||
STR_SORT_BY_TOTAL_CAPACITY_PER_CARGOTYPE,
|
||||
STR_SORT_BY_RELIABILITY,
|
||||
STR_SORT_BY_MAX_SPEED,
|
||||
INVALID_STRING_ID
|
||||
};
|
||||
|
||||
enum VehicleSortTypes {
|
||||
VEHTRAIN = 0,
|
||||
VEHROAD = 1,
|
||||
VEHSHIP = 2,
|
||||
VEHAIRCRAFT = 3
|
||||
};
|
||||
|
||||
VARDEF bool _vehicle_sort_dirty[4]; // global sort, vehicles added/removed (4 types of vehicles)
|
||||
|
||||
VARDEF bool _train_sort_dirty[MAX_PLAYERS]; // vehicles for a given player needs to be resorted (new criteria)
|
||||
VARDEF byte _train_sort_type[MAX_PLAYERS]; // different criteria for sorting
|
||||
VARDEF bool _train_sort_order[MAX_PLAYERS]; // sort descending/ascending
|
||||
|
||||
VARDEF bool _aircraft_sort_dirty[MAX_PLAYERS]; // vehicles for a given player needs to be resorted (new criteria)
|
||||
VARDEF byte _aircraft_sort_type[MAX_PLAYERS]; // different criteria for sorting
|
||||
VARDEF bool _aircraft_sort_order[MAX_PLAYERS]; // sort descending/ascending
|
||||
|
||||
VARDEF bool _ship_sort_dirty[MAX_PLAYERS]; // vehicles for a given player needs to be resorted (new criteria)
|
||||
VARDEF byte _ship_sort_type[MAX_PLAYERS]; // different criteria for sorting
|
||||
VARDEF bool _ship_sort_order[MAX_PLAYERS]; // sort descending/ascending
|
||||
|
||||
VARDEF bool _road_sort_dirty[MAX_PLAYERS]; // vehicles for a given player needs to be resorted (new criteria)
|
||||
VARDEF byte _road_sort_type[MAX_PLAYERS]; // different criteria for sorting
|
||||
VARDEF bool _road_sort_order[MAX_PLAYERS]; // sort descending/ascending
|
||||
|
||||
enum {
|
||||
PLY_WND_PRC__OFFSET_TOP_WIDGET = 26,
|
||||
PLY_WND_PRC__SIZE_OF_ROW_SMALL = 26,
|
||||
PLY_WND_PRC__SIZE_OF_ROW_BIG = 36,
|
||||
};
|
||||
|
||||
#endif /* VEHICLE_GUI_H */
|
Loading…
Reference in New Issue