train_gui.c
changeset 524 bcbcedbf5ccd
parent 517 b1e18b1e480e
child 534 306bc86eb23e
equal deleted inserted replaced
523:8871819c9afd 524:bcbcedbf5ccd
    56 		RestoreVehicleOrders(v, _backup_orders_data);
    56 		RestoreVehicleOrders(v, _backup_orders_data);
    57 	}
    57 	}
    58 	ShowTrainViewWindow(v);
    58 	ShowTrainViewWindow(v);
    59 }
    59 }
    60 
    60 
       
    61 static void engine_drawing_loop(int *x, int *y, int *pos, int *sel,
       
    62 	int *selected_id, byte railtype, bool is_engine)
       
    63 {
       
    64 	int i;
       
    65 
       
    66 	for (i = 0; i < NUM_TRAIN_ENGINES; i++) {
       
    67 		const Engine *e = DEREF_ENGINE(i);
       
    68 		const RailVehicleInfo *rvi = &rail_vehinfo(i);
       
    69 
       
    70 		if (e->railtype != railtype || !(rvi->flags & RVI_WAGON) != is_engine ||
       
    71 				!HASBIT(e->player_avail, _local_player))
       
    72 			continue;
       
    73 
       
    74 		if (*sel == 0) *selected_id = i;
       
    75 
       
    76 		if (IS_INT_INSIDE(--*pos, -8, 0)) {
       
    77 			DrawString(*x + 59, *y + 2, GetCustomEngineName(i),
       
    78 				*sel == 0 ? 0xC : 0x10);
       
    79 			DrawTrainEngine(*x + 29, *y + 6 + _traininfo_vehicle_pitch, i,
       
    80 				SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_local_player)));
       
    81 			*y += 14;
       
    82 		}
       
    83 		--*sel;
       
    84 	}
       
    85 }
       
    86 
    61 static void NewRailVehicleWndProc(Window *w, WindowEvent *e)
    87 static void NewRailVehicleWndProc(Window *w, WindowEvent *e)
    62 {
    88 {
    63 	switch(e->event) {
    89 	switch(e->event) {
    64 	case WE_PAINT:
    90 	case WE_PAINT:
    65 
    91 
    70 			int count = 0;
    96 			int count = 0;
    71 			byte railtype = WP(w,buildtrain_d).railtype;
    97 			byte railtype = WP(w,buildtrain_d).railtype;
    72 			int i;
    98 			int i;
    73 
    99 
    74 			for (i = 0; i < NUM_TRAIN_ENGINES; i++) {
   100 			for (i = 0; i < NUM_TRAIN_ENGINES; i++) {
    75 				Engine *e = &_engines[i];
   101 				const Engine *e = DEREF_ENGINE(i);
    76 				if (e->railtype == railtype
   102 				if (e->railtype == railtype
    77 				    && HASBIT(e->player_avail, _local_player))
   103 				    && HASBIT(e->player_avail, _local_player))
    78 					count++;
   104 					count++;
    79 			}
   105 			}
    80 			SetVScrollCount(w, count);
   106 			SetVScrollCount(w, count);
    88 			int sel = WP(w,buildtrain_d).sel_index;
   114 			int sel = WP(w,buildtrain_d).sel_index;
    89 			int pos = w->vscroll.pos;
   115 			int pos = w->vscroll.pos;
    90 			int x = 1;
   116 			int x = 1;
    91 			int y = 15;
   117 			int y = 15;
    92 			int selected_id = -1;
   118 			int selected_id = -1;
    93 			int i;
       
    94 
   119 
    95 			/* Ensure that custom engines which substituted wagons
   120 			/* Ensure that custom engines which substituted wagons
    96 			 * are sorted correctly. */
   121 			 * are sorted correctly.
    97 #define engine_drawing_loop(cmp_) \
   122 			 * XXX - DO NOT EVER DO THIS EVER AGAIN! GRRR hacking in wagons as
    98 			for (i = 0; i < NUM_TRAIN_ENGINES; i++) { \
   123 			 * engines to get more types.. Stays here until we have our own format
    99 				Engine *e = DEREF_ENGINE(i); \
   124 			 * then it is exit!!! */
   100 				RailVehicleInfo *rvi = &rail_vehinfo(i); \
   125 			engine_drawing_loop(&x, &y, &pos, &sel, &selected_id, railtype, true); // True engines
   101  \
   126 			engine_drawing_loop(&x, &y, &pos, &sel, &selected_id, railtype, false); // Feeble wagons
   102  				if (e->railtype != railtype || (rvi->flags & RVI_WAGON) cmp_ 0 \
       
   103 				    || !HASBIT(e->player_avail, _local_player)) \
       
   104 					continue; \
       
   105  \
       
   106 				if (sel == 0) selected_id = i; \
       
   107 				if (IS_INT_INSIDE(--pos, -8, 0)) { \
       
   108 					DrawString(x+59, y+2, GetCustomEngineName(i), sel == 0 ? 0xC : 0x10); \
       
   109 					DrawTrainEngine(x+29, y+6+_traininfo_vehicle_pitch, i, \
       
   110 					                SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_local_player))); \
       
   111 					y += 14; \
       
   112 				} \
       
   113 				sel--; \
       
   114 			}
       
   115 
       
   116 			engine_drawing_loop(!=); // True engines
       
   117 			engine_drawing_loop(==); // Feeble wagons
       
   118 
   127 
   119 			WP(w,buildtrain_d).sel_engine = selected_id;
   128 			WP(w,buildtrain_d).sel_engine = selected_id;
   120 
   129 
   121 			if (selected_id != -1) {
   130 			if (selected_id != -1) {
   122 				const RailVehicleInfo *rvi = &_rail_vehicle_info[selected_id];
   131 				const RailVehicleInfo *rvi = &rail_vehinfo(selected_id);
   123 				Engine *e;
   132 				Engine *e;
   124 				YearMonthDay ymd;
   133 				YearMonthDay ymd;
   125 
   134 
   126 				if (!(rvi->flags & RVI_WAGON)) {
   135 				if (!(rvi->flags & RVI_WAGON)) {
   127 					/* it's an engine */
   136 					/* it's an engine */