station_gui.c
changeset 164 0cbdf3c9bde1
parent 69 f8c29cdb388e
child 174 bd79fb899824
equal deleted inserted replaced
163:deb9b58db3f4 164:0cbdf3c9bde1
    42 	if (rating != 0) {
    42 	if (rating != 0) {
    43 		GfxFillRect(x+1, y+8, x+rating, y+8, 0xD0);
    43 		GfxFillRect(x+1, y+8, x+rating, y+8, 0xD0);
    44 	}
    44 	}
    45 }
    45 }
    46 
    46 
    47 // used to get a sorted list of the stations
    47 static SortStruct _station_sort[lengthof(_stations)];
    48 typedef struct StationSort {
       
    49 	uint16	index;
       
    50 	byte		owner;
       
    51 } StationSort;
       
    52 
       
    53 static StationSort _station_sort[lengthof(_stations)];
       
    54 static uint16 _num_station_sort[MAX_PLAYERS];
    48 static uint16 _num_station_sort[MAX_PLAYERS];
    55 
    49 
    56 static char _bufcache[64];
    50 static char _bufcache[64];
    57 static uint16 _last_station_idx;
    51 static uint16 _last_station_idx;
    58 
    52 
    59 static int CDECL StationSorterByName(const void *a, const void *b)
    53 static int CDECL StationNameSorter(const void *a, const void *b)
    60 {
    54 {
    61 	char buf1[64];
    55 	char buf1[64];
    62 	Station *st;
    56 	Station *st;
    63 	StationSort *cmp1, *cmp2;
    57 	SortStruct *cmp1, *cmp2;
    64 	cmp1 = (StationSort*)a;
    58 	cmp1 = (SortStruct*)a;
    65 	cmp2 = (StationSort*)b;
    59 	cmp2 = (SortStruct*)b;
    66 
    60 
    67 	// sort stations by owner, and inside owner by name
    61 	st = DEREF_STATION(cmp1->index);
    68 	if (cmp1->owner == cmp2->owner) {	// if same owner, sort by name
    62 	SET_DPARAM16(0, st->town->townnametype);
    69 		st = DEREF_STATION(cmp1->index);
    63 	SET_DPARAM32(1, st->town->townnameparts);
       
    64 	GetString(buf1, st->string_id);
       
    65 
       
    66 	if ( cmp2->index != _last_station_idx) {
       
    67 		_last_station_idx = cmp2->index;
       
    68 		st = DEREF_STATION(cmp2->index);
    70 		SET_DPARAM16(0, st->town->townnametype);
    69 		SET_DPARAM16(0, st->town->townnametype);
    71 		SET_DPARAM32(1, st->town->townnameparts);
    70 		SET_DPARAM32(1, st->town->townnameparts);
    72 		GetString(buf1, st->string_id);
    71 		GetString(_bufcache, st->string_id);
    73 
    72 	}
    74 		if ( cmp2->index != _last_station_idx) {
    73 
    75 			_last_station_idx = cmp2->index;
    74 	return strcmp(buf1, _bufcache);	// sort by name
    76 			st = DEREF_STATION(cmp2->index);
    75 }
    77 			SET_DPARAM16(0, st->town->townnametype);
    76 
    78 			SET_DPARAM32(1, st->town->townnameparts);
    77 static void MakeSortedStationList()
    79 			GetString(_bufcache, st->string_id);
       
    80 		}
       
    81 
       
    82 		return strcmp(buf1, _bufcache);	// sort by name
       
    83 	}
       
    84 	return cmp1->owner - cmp2->owner;	// sort by owner
       
    85 }
       
    86 
       
    87 static void MakeSortedStationList(Window *w)
       
    88 {
    78 {
    89 	Station *st;
    79 	Station *st;
    90 	uint16 n = 0;
    80 	uint16 n = 0;
    91 	uint16 *i;
    81 	uint16 *i;
    92 	// reset to 0 just to be sure
    82 	// reset to 0 just to be sure
   104 	// stations are stored as a cummulative index, eg 25, 41, 43. This means
    94 	// stations are stored as a cummulative index, eg 25, 41, 43. This means
   105 	// Player0: 25; Player1: (41-25) 16; Player2: (43-41) 2
    95 	// Player0: 25; Player1: (41-25) 16; Player2: (43-41) 2
   106 	for (i = &_num_station_sort[1]; i != endof(_num_station_sort); i++) {*i += *(i-1);}
    96 	for (i = &_num_station_sort[1]; i != endof(_num_station_sort); i++) {*i += *(i-1);}
   107 
    97 
   108 	_last_station_idx = 255; // used for "cache"
    98 	_last_station_idx = 255; // used for "cache"
   109 	qsort(_station_sort, n, sizeof(_station_sort[0]), StationSorterByName);
    99 
       
   100 	// sort by owner, then only subsort the requested owner-vehicles
       
   101 	qsort(_station_sort, n, sizeof(_station_sort[0]), GeneralOwnerSorter);
       
   102 
       
   103 	qsort(_station_sort, n, sizeof(_station_sort[0]), StationNameSorter);
   110 	
   104 	
   111 	DEBUG(misc, 1) ("Resorting Stations list...");
   105 	DEBUG(misc, 1) ("Resorting Stations list...");
   112 }
   106 }
   113 
   107 
   114 static void PlayerStationsWndProc(Window *w, WindowEvent *e)
   108 static void PlayerStationsWndProc(Window *w, WindowEvent *e)
   116 	switch(e->event) {
   110 	switch(e->event) {
   117 	case WE_PAINT: {
   111 	case WE_PAINT: {
   118 		byte i;
   112 		byte i;
   119 		if (_station_sort_dirty) {
   113 		if (_station_sort_dirty) {
   120 			_station_sort_dirty = false;
   114 			_station_sort_dirty = false;
   121 			MakeSortedStationList(w);
   115 			MakeSortedStationList();
   122 		}
   116 		}
   123 
   117 
   124 		// stations are stored as a cummulative index, eg 25, 41, 43. This means
   118 		// stations are stored as a cummulative index, eg 25, 41, 43. This means
   125 		// Player0: 25; Player1: (41-25) 16; Player2: (43-41) 2 stations
   119 		// Player0: 25; Player1: (41-25) 16; Player2: (43-41) 2 stations
   126 		i = (byte)(w->window_number == 0) ? 0 : _num_station_sort[w->window_number-1];;
   120 		i = (byte)(w->window_number == 0) ? 0 : _num_station_sort[w->window_number-1];
   127 		SetVScrollCount(w, _num_station_sort[w->window_number] - i);
   121 		SetVScrollCount(w, _num_station_sort[w->window_number] - i);
   128 
   122 
   129 		/* draw widgets, with player's name in the caption */
   123 		/* draw widgets, with player's name in the caption */
   130 		{
   124 		{
   131 			Player *p = DEREF_PLAYER(w->window_number);
   125 			Player *p = DEREF_PLAYER(w->window_number);
   165 						x += 10;
   159 						x += 10;
   166 					}
   160 					}
   167 				}
   161 				}
   168 				y += 10;
   162 				y += 10;
   169 				i++;	// next station
   163 				i++;	// next station
   170 				if (++p == 12) { break;} // max number of stations in 1 window
   164 				if (++p == w->vscroll.cap) { break;} // max number of stations in 1 window
   171 			}
   165 			}
   172 		}
   166 		}
   173 	} break;
   167 	} break;
   174 	case WE_CLICK: {
   168 	case WE_CLICK: {
   175 		switch(e->click.widget) {
   169 		switch(e->click.widget) {