src/roadveh_gui.cpp
changeset 8026 269979f5319d
parent 7988 6075538f6111
child 8149 eb78fb69096f
equal deleted inserted replaced
8025:39c5dc3c2b4a 8026:269979f5319d
    17 #include "command.h"
    17 #include "command.h"
    18 #include "depot.h"
    18 #include "depot.h"
    19 #include "vehicle_gui.h"
    19 #include "vehicle_gui.h"
    20 #include "newgrf_engine.h"
    20 #include "newgrf_engine.h"
    21 
    21 
       
    22 
       
    23 void DrawRoadVehDetails(const Vehicle *v, int x, int y)
       
    24 {
       
    25 	uint y_offset = RoadVehHasArticPart(v) ? 15 :0;
       
    26 	StringID str;
       
    27 
       
    28 	SetDParam(0, v->engine_type);
       
    29 	SetDParam(1, v->build_year);
       
    30 	SetDParam(2, v->value);
       
    31 	DrawString(x, y + y_offset, STR_9011_BUILT_VALUE, 0);
       
    32 
       
    33 	if (RoadVehHasArticPart(v)) {
       
    34 		AcceptedCargo max_cargo;
       
    35 		char capacity[512];
       
    36 
       
    37 		memset(max_cargo, 0, sizeof(max_cargo));
       
    38 
       
    39 		for (const Vehicle *u = v; u != NULL; u = u->Next()) {
       
    40 			max_cargo[u->cargo_type] += u->cargo_cap;
       
    41 		}
       
    42 
       
    43 		GetString(capacity, STR_ARTICULATED_RV_CAPACITY, lastof(capacity));
       
    44 
       
    45 		bool first = true;
       
    46 		for (CargoID i = 0; i < NUM_CARGO; i++) {
       
    47 			if (max_cargo[i] > 0) {
       
    48 				char buffer[128];
       
    49 
       
    50 				SetDParam(0, i);
       
    51 				SetDParam(1, max_cargo[i]);
       
    52 				GetString(buffer, STR_BARE_CARGO, lastof(buffer));
       
    53 
       
    54 				if (!first) strecat(capacity, ", ", lastof(capacity));
       
    55 				strecat(capacity, buffer, lastof(capacity));
       
    56 				first = false;
       
    57 			}
       
    58 		}
       
    59 
       
    60 		SetDParamStr(0, capacity);
       
    61 		DrawStringTruncated(x, y + 10 + y_offset, STR_JUST_STRING, 0, 380 - x);
       
    62 
       
    63 		for (const Vehicle *u = v; u != NULL; u = u->Next()) {
       
    64 			str = STR_8812_EMPTY;
       
    65 			if (!u->cargo.Empty()) {
       
    66 				SetDParam(0, u->cargo_type);
       
    67 				SetDParam(1, u->cargo.Count());
       
    68 				SetDParam(2, u->cargo.Source());
       
    69 				str = STR_8813_FROM;
       
    70 			}
       
    71 			DrawString(x, y + 21 + y_offset, str, 0);
       
    72 
       
    73 			y_offset += 11;
       
    74 		}
       
    75 
       
    76 		y_offset -= 11;
       
    77 	} else {
       
    78 		SetDParam(0, v->cargo_type);
       
    79 		SetDParam(1, v->cargo_cap);
       
    80 		DrawString(x, y + 10 + y_offset, STR_9012_CAPACITY, 0);
       
    81 
       
    82 		str = STR_8812_EMPTY;
       
    83 		if (!v->cargo.Empty()) {
       
    84 			SetDParam(0, v->cargo_type);
       
    85 			SetDParam(1, v->cargo.Count());
       
    86 			SetDParam(2, v->cargo.Source());
       
    87 			str = STR_8813_FROM;
       
    88 		}
       
    89 		DrawString(x, y + 21 + y_offset, str, 0);
       
    90 	}
       
    91 
       
    92 	/* Draw Transfer credits text */
       
    93 	SetDParam(0, v->cargo.FeederShare());
       
    94 	DrawString(x, y + 33 + y_offset, STR_FEEDER_CARGO_VALUE, 0);
       
    95 }
       
    96 
       
    97 
    22 static inline int RoadVehLengthToPixels(int length)
    98 static inline int RoadVehLengthToPixels(int length)
    23 {
    99 {
    24 	return (length * 28) / 8;
   100 	return (length * 28) / 8;
    25 }
   101 }
    26 
   102 
    27 void DrawRoadVehImage(const Vehicle *v, int x, int y, int count, VehicleID selection)
   103 void DrawRoadVehImage(const Vehicle *v, int x, int y, int count, VehicleID selection)
    28 {
   104 {
    29 	int dx = 0;
       
    30 
       
    31 	/* Road vehicle lengths are measured in eighths of the standard length, so
   105 	/* Road vehicle lengths are measured in eighths of the standard length, so
    32 	 * count is the number of standard vehicles that should be drawn. If it is
   106 	 * count is the number of standard vehicles that should be drawn. If it is
    33 	 * 0, we draw enough vehicles for 10 standard vehicle lengths. */
   107 	 * 0, we draw enough vehicles for 10 standard vehicle lengths. */
    34 	int max_length = (count == 0) ? 80 : count * 8;
   108 	int max_length = (count == 0) ? 80 : count * 8;
    35 
   109 
    36 	do {
   110 	for (int dx = 0 ; v != NULL && dx < max_length ; dx += v->u.road.cached_veh_length, v = v->Next()) {
    37 		int length = v->u.road.cached_veh_length;
   111 		if (dx + v->u.road.cached_veh_length > 0 && dx <= max_length) {
    38 
       
    39 		if (dx + length > 0 && dx <= max_length) {
       
    40 			SpriteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
   112 			SpriteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
    41 			DrawSprite(v->GetImage(DIR_W), pal, x + 14 + RoadVehLengthToPixels(dx), y + 6);
   113 			DrawSprite(v->GetImage(DIR_W), pal, x + 14 + RoadVehLengthToPixels(dx), y + 6);
    42 
   114 
    43 			if (v->index == selection) {
   115 			if (v->index == selection) {
    44 				DrawFrameRect(x - 1, y - 1, x + 28, y + 12, 15, FR_BORDERONLY);
   116 				DrawFrameRect(x - 1, y - 1, x + 28, y + 12, 15, FR_BORDERONLY);
    45 			}
   117 			}
    46 		}
   118 		}
    47 
       
    48 		dx += length;
       
    49 		v = v->Next();
       
    50 	} while (v != NULL && dx < max_length);
       
    51 }
       
    52 
       
    53 static void RoadVehDetailsWndProc(Window *w, WindowEvent *e)
       
    54 {
       
    55 	switch (e->event) {
       
    56 	case WE_CREATE: {
       
    57 		const Vehicle *v = GetVehicle(w->window_number);
       
    58 
       
    59 		if (!RoadVehHasArticPart(v)) break;
       
    60 
       
    61 		/* Draw the text under the vehicle instead of next to it, minus the
       
    62 		 * height already allocated for the cargo of the first vehicle. */
       
    63 		uint height_extension = 15 - 11;
       
    64 
       
    65 		/* Add space for the cargo amount for each part. */
       
    66 		do {
       
    67 			height_extension += 11;
       
    68 		} while ((v = v->Next()) != NULL);
       
    69 
       
    70 		ResizeWindow(w, 0, height_extension);
       
    71 	} break;
       
    72 
       
    73 	case WE_PAINT: {
       
    74 		const Vehicle *v = GetVehicle(w->window_number);
       
    75 		StringID str;
       
    76 		uint y_offset = RoadVehHasArticPart(v) ? 15 :0;
       
    77 
       
    78 		SetWindowWidgetDisabledState(w, 2, v->owner != _local_player);
       
    79 		/* disable service-scroller when interval is set to disabled */
       
    80 		SetWindowWidgetDisabledState(w, 5, !_patches.servint_roadveh);
       
    81 		SetWindowWidgetDisabledState(w, 6, !_patches.servint_roadveh);
       
    82 
       
    83 		SetDParam(0, v->index);
       
    84 		DrawWindowWidgets(w);
       
    85 
       
    86 		/* Draw running cost */
       
    87 		{
       
    88 			int year = v->age / 366;
       
    89 
       
    90 			SetDParam(1, year);
       
    91 
       
    92 			SetDParam(0, (v->age + 365 < v->max_age) ? STR_AGE : STR_AGE_RED);
       
    93 			SetDParam(2, v->max_age / 366);
       
    94 			SetDParam(3, v->GetDisplayRunningCost());
       
    95 			DrawString(2, 15, STR_900D_AGE_RUNNING_COST_YR, 0);
       
    96 		}
       
    97 
       
    98 		/* Draw max speed */
       
    99 		{
       
   100 			SetDParam(0, v->GetDisplayMaxSpeed());
       
   101 			DrawString(2, 25, STR_900E_MAX_SPEED, 0);
       
   102 		}
       
   103 
       
   104 		/* Draw profit */
       
   105 		{
       
   106 			SetDParam(0, v->profit_this_year);
       
   107 			SetDParam(1, v->profit_last_year);
       
   108 			DrawString(2, 35, STR_900F_PROFIT_THIS_YEAR_LAST_YEAR, 0);
       
   109 		}
       
   110 
       
   111 		/* Draw breakdown & reliability */
       
   112 		{
       
   113 			SetDParam(0, v->reliability * 100 >> 16);
       
   114 			SetDParam(1, v->breakdowns_since_last_service);
       
   115 			DrawString(2, 45, STR_9010_RELIABILITY_BREAKDOWNS, 0);
       
   116 		}
       
   117 
       
   118 		DrawRoadVehImage(v, 3, 57, 0, INVALID_VEHICLE);
       
   119 
       
   120 		SetDParam(0, v->engine_type);
       
   121 		SetDParam(1, v->build_year);
       
   122 		SetDParam(2, v->value);
       
   123 		DrawString(34, 57 + y_offset, STR_9011_BUILT_VALUE, 0);
       
   124 
       
   125 		if (RoadVehHasArticPart(v)) {
       
   126 			AcceptedCargo max_cargo;
       
   127 			char capacity[512];
       
   128 
       
   129 			memset(max_cargo, 0, sizeof(max_cargo));
       
   130 
       
   131 			for (const Vehicle *u = v; u != NULL; u = u->Next()) {
       
   132 				max_cargo[u->cargo_type] += u->cargo_cap;
       
   133 			}
       
   134 
       
   135 			GetString(capacity, STR_ARTICULATED_RV_CAPACITY, lastof(capacity));
       
   136 
       
   137 			bool first = true;
       
   138 			for (CargoID i = 0; i < NUM_CARGO; i++) {
       
   139 				if (max_cargo[i] > 0) {
       
   140 					char buffer[128];
       
   141 
       
   142 					SetDParam(0, i);
       
   143 					SetDParam(1, max_cargo[i]);
       
   144 					GetString(buffer, STR_BARE_CARGO, lastof(buffer));
       
   145 
       
   146 					if (!first) strecat(capacity, ", ", lastof(capacity));
       
   147 					strecat(capacity, buffer, lastof(capacity));
       
   148 
       
   149 					first = false;
       
   150 				}
       
   151 			}
       
   152 
       
   153 			SetDParamStr(0, capacity);
       
   154 			DrawStringTruncated(34, 67 + y_offset, STR_JUST_STRING, 0, w->width - 34);
       
   155 
       
   156 			for (const Vehicle *u = v; u != NULL; u = u->Next()) {
       
   157 				str = STR_8812_EMPTY;
       
   158 				if (!u->cargo.Empty()) {
       
   159 					SetDParam(0, u->cargo_type);
       
   160 					SetDParam(1, u->cargo.Count());
       
   161 					SetDParam(2, u->cargo.Source());
       
   162 					str = STR_8813_FROM;
       
   163 				}
       
   164 				DrawString(34, 78 + y_offset, str, 0);
       
   165 
       
   166 				y_offset += 11;
       
   167 			}
       
   168 
       
   169 			y_offset -= 11;
       
   170 		} else {
       
   171 			SetDParam(0, v->cargo_type);
       
   172 			SetDParam(1, v->cargo_cap);
       
   173 			DrawString(34, 67 + y_offset, STR_9012_CAPACITY, 0);
       
   174 
       
   175 			str = STR_8812_EMPTY;
       
   176 			if (!v->cargo.Empty()) {
       
   177 				SetDParam(0, v->cargo_type);
       
   178 				SetDParam(1, v->cargo.Count());
       
   179 				SetDParam(2, v->cargo.Source());
       
   180 				str = STR_8813_FROM;
       
   181 			}
       
   182 			DrawString(34, 78 + y_offset, str, 0);
       
   183 		}
       
   184 
       
   185 		/* Draw Transfer credits text */
       
   186 		SetDParam(0, v->cargo.FeederShare());
       
   187 		DrawString(34, 90 + y_offset, STR_FEEDER_CARGO_VALUE, 0);
       
   188 
       
   189 		/* Draw service interval text */
       
   190 		{
       
   191 			SetDParam(0, v->service_interval);
       
   192 			SetDParam(1, v->date_of_last_service);
       
   193 			DrawString(13, 102 + y_offset, _patches.servint_ispercent ? STR_SERVICING_INTERVAL_PERCENT : STR_883C_SERVICING_INTERVAL_DAYS, 0);
       
   194 		}
       
   195 	} break;
       
   196 
       
   197 	case WE_CLICK: {
       
   198 		int mod;
       
   199 		const Vehicle *v;
       
   200 		switch (e->we.click.widget) {
       
   201 		case 2: /* rename */
       
   202 			v = GetVehicle(w->window_number);
       
   203 			SetDParam(0, v->index);
       
   204 			ShowQueryString(STR_VEHICLE_NAME, STR_902C_NAME_ROAD_VEHICLE, 31, 150, w, CS_ALPHANUMERAL);
       
   205 			break;
       
   206 
       
   207 		case 5: /* increase int */
       
   208 			mod = _ctrl_pressed? 5 : 10;
       
   209 			goto do_change_service_int;
       
   210 		case 6: /* decrease int */
       
   211 			mod = _ctrl_pressed? -5 : -10;
       
   212 do_change_service_int:
       
   213 			v = GetVehicle(w->window_number);
       
   214 
       
   215 			mod = GetServiceIntervalClamped(mod + v->service_interval);
       
   216 			if (mod == v->service_interval) return;
       
   217 
       
   218 			DoCommandP(v->tile, v->index, mod, NULL, CMD_CHANGE_SERVICE_INT | CMD_MSG(STR_018A_CAN_T_CHANGE_SERVICING));
       
   219 			break;
       
   220 		}
       
   221 	} break;
       
   222 
       
   223 	case WE_ON_EDIT_TEXT: {
       
   224 		if (e->we.edittext.str[0] != '\0') {
       
   225 			_cmd_text = e->we.edittext.str;
       
   226 			DoCommandP(0, w->window_number, 0, NULL,
       
   227 				CMD_NAME_VEHICLE | CMD_MSG(STR_902D_CAN_T_NAME_ROAD_VEHICLE));
       
   228 		}
       
   229 	} break;
       
   230 
       
   231 	}
   119 	}
   232 }
       
   233 
       
   234 static const Widget _roadveh_details_widgets[] = {
       
   235 {   WWT_CLOSEBOX,   RESIZE_NONE,    14,     0,    10,     0,    13, STR_00C5,         STR_018B_CLOSE_WINDOW},
       
   236 {    WWT_CAPTION,   RESIZE_NONE,    14,    11,   339,     0,    13, STR_900C_DETAILS, STR_018C_WINDOW_TITLE_DRAG_THIS},
       
   237 { WWT_PUSHTXTBTN,   RESIZE_NONE,    14,   340,   379,     0,    13, STR_01AA_NAME,    STR_902E_NAME_ROAD_VEHICLE},
       
   238 {      WWT_PANEL,   RESIZE_NONE,    14,     0,   379,    14,    55, 0x0,              STR_NULL},
       
   239 {      WWT_PANEL,   RESIZE_BOTTOM,  14,     0,   379,    56,   100, 0x0,              STR_NULL},
       
   240 { WWT_PUSHTXTBTN,   RESIZE_TB,      14,     0,    10,   101,   106, STR_0188,         STR_884D_INCREASE_SERVICING_INTERVAL},
       
   241 { WWT_PUSHTXTBTN,   RESIZE_TB,      14,     0,    10,   107,   112, STR_0189,         STR_884E_DECREASE_SERVICING_INTERVAL},
       
   242 {      WWT_PANEL,   RESIZE_TB,      14,    11,   379,   101,   112, 0x0,              STR_NULL},
       
   243 {   WIDGETS_END},
       
   244 };
       
   245 
       
   246 static const WindowDesc _roadveh_details_desc = {
       
   247 	WDP_AUTO, WDP_AUTO, 380, 113, 380, 113,
       
   248 	WC_VEHICLE_DETAILS,WC_VEHICLE_VIEW,
       
   249 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
       
   250 	_roadveh_details_widgets,
       
   251 	RoadVehDetailsWndProc
       
   252 };
       
   253 
       
   254 
       
   255 void ShowRoadVehDetailsWindow(const Vehicle *v)
       
   256 {
       
   257 	Window *w;
       
   258 	VehicleID veh = v->index;
       
   259 
       
   260 	DeleteWindowById(WC_VEHICLE_ORDERS, veh);
       
   261 	DeleteWindowById(WC_VEHICLE_DETAILS, veh);
       
   262 
       
   263 	w = AllocateWindowDescFront(&_roadveh_details_desc, veh);
       
   264 	w->caption_color = v->owner;
       
   265 }
   120 }
   266 
   121 
   267 void CcBuildRoadVeh(bool success, TileIndex tile, uint32 p1, uint32 p2)
   122 void CcBuildRoadVeh(bool success, TileIndex tile, uint32 p1, uint32 p2)
   268 {
   123 {
   269 	const Vehicle *v;
   124 	const Vehicle *v;