train_gui.c
changeset 4764 79341214c15d
parent 4737 287bc9b53ec9
child 4765 d109bfb2cc73
equal deleted inserted replaced
4763:c74b3eac2b73 4764:79341214c15d
    21 #include "depot.h"
    21 #include "depot.h"
    22 #include "train.h"
    22 #include "train.h"
    23 #include "newgrf_engine.h"
    23 #include "newgrf_engine.h"
    24 #include "date.h"
    24 #include "date.h"
    25 
    25 
       
    26 typedef enum BuildTrainWidgets {
       
    27 	BUILD_TRAIN_WIDGET_CLOSEBOX = 0,
       
    28 	BUILD_TRAIN_WIDGET_CAPTION,
       
    29 	BUILD_TRAIN_WIDGET_LIST,
       
    30 	BUILD_TRAIN_WIDGET_SCROLLBAR,
       
    31 	BUILD_TRAIN_WIDGET_PANEL,
       
    32 	BUILD_TRAIN_WIDGET_BUILD,
       
    33 	BUILD_TRAIN_WIDGET_RENAME,
       
    34 	BUILD_TRAIN_WIDGET_RESIZE,
       
    35 } BuildTrainWidget;
       
    36 
       
    37 
    26 /**
    38 /**
    27  * Draw the purchase info details of train engine at a given location.
    39  * Draw the purchase info details of train engine at a given location.
    28  * @param x,y location where to draw the info
    40  * @param x,y location where to draw the info
    29  * @param engine_number the engine of which to draw the info of
    41  * @param engine_number the engine of which to draw the info of
    30  */
    42  */
   202 }
   214 }
   203 
   215 
   204 static void NewRailVehicleWndProc(Window *w, WindowEvent *e)
   216 static void NewRailVehicleWndProc(Window *w, WindowEvent *e)
   205 {
   217 {
   206 	switch (e->event) {
   218 	switch (e->event) {
   207 	case WE_PAINT:
   219 		case WE_PAINT:
   208 
   220 
   209 		SetWindowWidgetDisabledState(w, 5, w->window_number == 0);
   221 			SetWindowWidgetDisabledState(w, BUILD_TRAIN_WIDGET_BUILD, w->window_number == 0);
   210 
   222 
   211 		{
   223 			{
   212 			int count = 0;
   224 				int count = 0;
   213 			RailType railtype = WP(w,buildtrain_d).railtype;
   225 				RailType railtype = WP(w,buildtrain_d).railtype;
   214 			EngineID i;
   226 				EngineID i;
   215 
   227 
   216 			for (i = 0; i < NUM_TRAIN_ENGINES; i++) {
   228 				for (i = 0; i < NUM_TRAIN_ENGINES; i++) {
   217 				const Engine *e = GetEngine(i);
   229 					const Engine *e = GetEngine(i);
   218 				if (HasPowerOnRail(e->railtype, railtype) &&
   230 					if (HasPowerOnRail(e->railtype, railtype) &&
   219 						HASBIT(e->player_avail, _local_player)) {
   231 							HASBIT(e->player_avail, _local_player)) {
   220 					count++;
   232 						count++;
       
   233 					}
   221 				}
   234 				}
   222 			}
   235 				SetVScrollCount(w, count);
   223 			SetVScrollCount(w, count);
   236 			}
   224 		}
   237 
   225 
   238 			SetDParam(0, WP(w,buildtrain_d).railtype + STR_881C_NEW_RAIL_VEHICLES);
   226 		SetDParam(0, WP(w,buildtrain_d).railtype + STR_881C_NEW_RAIL_VEHICLES);
   239 			DrawWindowWidgets(w);
   227 		DrawWindowWidgets(w);
   240 
   228 
   241 			{
   229 		{
   242 				RailType railtype = WP(w,buildtrain_d).railtype;
   230 			RailType railtype = WP(w,buildtrain_d).railtype;
   243 				int sel = WP(w,buildtrain_d).sel_index;
   231 			int sel = WP(w,buildtrain_d).sel_index;
   244 				int pos = w->vscroll.pos;
   232 			int pos = w->vscroll.pos;
   245 				int x = 1;
   233 			int x = 1;
   246 				int y = 15;
   234 			int y = 15;
   247 				EngineID selected_id = INVALID_ENGINE;
   235 			EngineID selected_id = INVALID_ENGINE;
   248 
   236 
   249 				/* Ensure that custom engines which substituted wagons
   237 			/* Ensure that custom engines which substituted wagons
   250 				 * are sorted correctly.
   238 			 * are sorted correctly.
   251 				 * XXX - DO NOT EVER DO THIS EVER AGAIN! GRRR hacking in wagons as
   239 			 * XXX - DO NOT EVER DO THIS EVER AGAIN! GRRR hacking in wagons as
   252 				 * engines to get more types.. Stays here until we have our own format
   240 			 * engines to get more types.. Stays here until we have our own format
   253 				 * then it is exit!!! */
   241 			 * then it is exit!!! */
   254 				engine_drawing_loop(&x, &y, &pos, &sel, &selected_id, railtype, w->vscroll.cap, true); // True engines
   242 			engine_drawing_loop(&x, &y, &pos, &sel, &selected_id, railtype, w->vscroll.cap, true); // True engines
   255 				engine_drawing_loop(&x, &y, &pos, &sel, &selected_id, railtype, w->vscroll.cap, false); // Feeble wagons
   243 			engine_drawing_loop(&x, &y, &pos, &sel, &selected_id, railtype, w->vscroll.cap, false); // Feeble wagons
   256 
   244 
   257 				WP(w,buildtrain_d).sel_engine = selected_id;
   245 			WP(w,buildtrain_d).sel_engine = selected_id;
   258 
   246 
   259 				if (selected_id != INVALID_ENGINE) {
   247 			if (selected_id != INVALID_ENGINE) {
   260 					const RailVehicleInfo *rvi = RailVehInfo(selected_id);
   248 				const RailVehicleInfo *rvi = RailVehInfo(selected_id);
   261 
   249 
   262 					if (!(rvi->flags & RVI_WAGON)) {
   250 				if (!(rvi->flags & RVI_WAGON)) {
   263 						/* it's an engine */
   251 					/* it's an engine */
   264 						DrawTrainEnginePurchaseInfo(2, w->widget[BUILD_TRAIN_WIDGET_PANEL].top + 1,selected_id);
   252 					DrawTrainEnginePurchaseInfo(2, w->widget[4].top + 1,selected_id);
   265 					} else {
   253 				} else {
   266 						/* it's a wagon */
   254 					/* it's a wagon */
   267 						DrawTrainWagonPurchaseInfo(2, w->widget[BUILD_TRAIN_WIDGET_PANEL].top + 1, selected_id);
   255 					DrawTrainWagonPurchaseInfo(2, w->widget[4].top + 1, selected_id);
   268 					}
   256 				}
   269 				}
   257 			}
   270 			}
   258 		}
   271 		break;
   259 	break;
   272 
   260 
   273 		case WE_CLICK: {
   261 	case WE_CLICK: {
   274 			switch (e->we.click.widget) {
   262 		switch (e->we.click.widget) {
   275 				case BUILD_TRAIN_WIDGET_LIST: {
   263 		case 2: {
   276 					uint i = (e->we.click.pt.y - 14) / 14;
   264 			uint i = (e->we.click.pt.y - 14) / 14;
   277 					if (i < w->vscroll.cap) {
   265 			if (i < w->vscroll.cap) {
   278 						WP(w,buildtrain_d).sel_index = i + w->vscroll.pos;
   266 				WP(w,buildtrain_d).sel_index = i + w->vscroll.pos;
   279 						SetWindowDirty(w);
   267 				SetWindowDirty(w);
   280 					}
       
   281 				} break;
       
   282 
       
   283 				case BUILD_TRAIN_WIDGET_BUILD: {
       
   284 					EngineID sel_eng = WP(w,buildtrain_d).sel_engine;
       
   285 					if (sel_eng != INVALID_ENGINE)
       
   286 						DoCommandP(w->window_number, sel_eng, 0, (RailVehInfo(sel_eng)->flags & RVI_WAGON) ? CcBuildWagon : CcBuildLoco, CMD_BUILD_RAIL_VEHICLE | CMD_MSG(STR_882B_CAN_T_BUILD_RAILROAD_VEHICLE));
       
   287 				}	break;
       
   288 
       
   289 				case BUILD_TRAIN_WIDGET_RENAME: {
       
   290 					EngineID sel_eng = WP(w,buildtrain_d).sel_engine;
       
   291 					if (sel_eng != INVALID_ENGINE) {
       
   292 						WP(w,buildtrain_d).rename_engine = sel_eng;
       
   293 						ShowQueryString(GetCustomEngineName(sel_eng),
       
   294 										STR_886A_RENAME_TRAIN_VEHICLE_TYPE, 31, 160, w->window_class, w->window_number, CS_ALPHANUMERAL);
       
   295 					}
       
   296 				} break;
   268 			}
   297 			}
   269 		} break;
   298 		} break;
   270 		case 5: {
   299 
   271 			EngineID sel_eng = WP(w,buildtrain_d).sel_engine;
   300 		case WE_ON_EDIT_TEXT: {
   272 			if (sel_eng != INVALID_ENGINE)
   301 			if (e->we.edittext.str[0] != '\0') {
   273 				DoCommandP(w->window_number, sel_eng, 0, (RailVehInfo(sel_eng)->flags & RVI_WAGON) ? CcBuildWagon : CcBuildLoco, CMD_BUILD_RAIL_VEHICLE | CMD_MSG(STR_882B_CAN_T_BUILD_RAILROAD_VEHICLE));
   302 				_cmd_text = e->we.edittext.str;
   274 		}	break;
   303 				DoCommandP(0, WP(w,buildtrain_d).rename_engine, 0, NULL,
   275 		case 6: { /* rename */
   304 					CMD_RENAME_ENGINE | CMD_MSG(STR_886B_CAN_T_RENAME_TRAIN_VEHICLE));
   276 			EngineID sel_eng = WP(w,buildtrain_d).sel_engine;
       
   277 			if (sel_eng != INVALID_ENGINE) {
       
   278 				WP(w,buildtrain_d).rename_engine = sel_eng;
       
   279 				ShowQueryString(GetCustomEngineName(sel_eng),
       
   280 					STR_886A_RENAME_TRAIN_VEHICLE_TYPE, 31, 160, w->window_class, w->window_number, CS_ALPHANUMERAL);
       
   281 			}
   305 			}
   282 		} break;
   306 		} break;
   283 		}
   307 
   284 	} break;
   308 		case WE_RESIZE: {
   285 
   309 			if (e->we.sizing.diff.y == 0) break;
   286 	case WE_ON_EDIT_TEXT: {
   310 
   287 		if (e->we.edittext.str[0] != '\0') {
   311 			w->vscroll.cap += e->we.sizing.diff.y / 14;
   288 			_cmd_text = e->we.edittext.str;
   312 			w->widget[BUILD_TRAIN_WIDGET_LIST].data = (w->vscroll.cap << 8) + 1;
   289 			DoCommandP(0, WP(w,buildtrain_d).rename_engine, 0, NULL,
   313 		} break;
   290 				CMD_RENAME_ENGINE | CMD_MSG(STR_886B_CAN_T_RENAME_TRAIN_VEHICLE));
       
   291 		}
       
   292 	} break;
       
   293 
       
   294 	case WE_RESIZE: {
       
   295 		if (e->we.sizing.diff.y == 0)
       
   296 			break;
       
   297 
       
   298 		w->vscroll.cap += e->we.sizing.diff.y / 14;
       
   299 		w->widget[2].data = (w->vscroll.cap << 8) + 1;
       
   300 	} break;
       
   301 	}
   314 	}
   302 }
   315 }
   303 
   316 
   304 static const Widget _new_rail_vehicle_widgets[] = {
   317 static const Widget _new_rail_vehicle_widgets[] = {
   305 {   WWT_CLOSEBOX,   RESIZE_NONE,    14,     0,    10,     0,    13, STR_00C5,               STR_018B_CLOSE_WINDOW},
   318 {   WWT_CLOSEBOX,   RESIZE_NONE,    14,     0,    10,     0,    13, STR_00C5,               STR_018B_CLOSE_WINDOW},
   328 	DeleteWindowById(WC_BUILD_VEHICLE, tile);
   341 	DeleteWindowById(WC_BUILD_VEHICLE, tile);
   329 
   342 
   330 	w = AllocateWindowDesc(&_new_rail_vehicle_desc);
   343 	w = AllocateWindowDesc(&_new_rail_vehicle_desc);
   331 	w->window_number = tile;
   344 	w->window_number = tile;
   332 	w->vscroll.cap = 8;
   345 	w->vscroll.cap = 8;
   333 	w->widget[2].data = (w->vscroll.cap << 8) + 1;
   346 	w->widget[BUILD_TRAIN_WIDGET_LIST].data = (w->vscroll.cap << 8) + 1;
   334 
   347 
   335 	w->resize.step_height = 14;
   348 	w->resize.step_height = 14;
   336 	w->resize.height = w->height - 14 * 4; // Minimum of 4 vehicles in the display
   349 	w->resize.height = w->height - 14 * 4; // Minimum of 4 vehicles in the display
   337 
   350 
   338 	if (tile != 0) {
   351 	if (tile != 0) {