1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
8 /** @file roadveh_gui.cpp GUI for road vehicles. */
9 
10 #include "stdafx.h"
11 #include "roadveh.h"
12 #include "window_gui.h"
13 #include "strings_func.h"
14 #include "vehicle_func.h"
15 #include "string_func.h"
16 #include "zoom_func.h"
17 
18 #include "table/strings.h"
19 
20 #include "safeguards.h"
21 
22 /**
23  * Draw the details for the given vehicle at the given position
24  *
25  * @param v     current vehicle
26  * @param left  The left most coordinate to draw
27  * @param right The right most coordinate to draw
28  * @param y     The y coordinate
29  */
DrawRoadVehDetails(const Vehicle * v,int left,int right,int y)30 void DrawRoadVehDetails(const Vehicle *v, int left, int right, int y)
31 {
32 	uint y_offset = v->HasArticulatedPart() ? ScaleGUITrad(15) : 0; // Draw the first line below the sprite of an articulated RV instead of after it.
33 	StringID str;
34 	Money feeder_share = 0;
35 
36 	SetDParam(0, v->engine_type);
37 	SetDParam(1, v->build_year);
38 	SetDParam(2, v->value);
39 	DrawString(left, right, y + y_offset, STR_VEHICLE_INFO_BUILT_VALUE);
40 
41 	if (v->HasArticulatedPart()) {
42 		CargoArray max_cargo;
43 		StringID subtype_text[NUM_CARGO];
44 		char capacity[512];
45 
46 		memset(subtype_text, 0, sizeof(subtype_text));
47 
48 		for (const Vehicle *u = v; u != nullptr; u = u->Next()) {
49 			max_cargo[u->cargo_type] += u->cargo_cap;
50 			if (u->cargo_cap > 0) {
51 				StringID text = GetCargoSubtypeText(u);
52 				if (text != STR_EMPTY) subtype_text[u->cargo_type] = text;
53 			}
54 		}
55 
56 		GetString(capacity, STR_VEHICLE_DETAILS_TRAIN_ARTICULATED_RV_CAPACITY, lastof(capacity));
57 
58 		bool first = true;
59 		for (CargoID i = 0; i < NUM_CARGO; i++) {
60 			if (max_cargo[i] > 0) {
61 				char buffer[128];
62 
63 				SetDParam(0, i);
64 				SetDParam(1, max_cargo[i]);
65 				GetString(buffer, STR_JUST_CARGO, lastof(buffer));
66 
67 				if (!first) strecat(capacity, ", ", lastof(capacity));
68 				strecat(capacity, buffer, lastof(capacity));
69 
70 				if (subtype_text[i] != 0) {
71 					GetString(buffer, subtype_text[i], lastof(buffer));
72 					strecat(capacity, buffer, lastof(capacity));
73 				}
74 
75 				first = false;
76 			}
77 		}
78 
79 		DrawString(left, right, y + FONT_HEIGHT_NORMAL + y_offset, capacity, TC_BLUE);
80 
81 		for (const Vehicle *u = v; u != nullptr; u = u->Next()) {
82 			if (u->cargo_cap == 0) continue;
83 
84 			str = STR_VEHICLE_DETAILS_CARGO_EMPTY;
85 			if (u->cargo.StoredCount() > 0) {
86 				SetDParam(0, u->cargo_type);
87 				SetDParam(1, u->cargo.StoredCount());
88 				SetDParam(2, u->cargo.Source());
89 				str = STR_VEHICLE_DETAILS_CARGO_FROM;
90 				feeder_share += u->cargo.FeederShare();
91 			}
92 			DrawString(left, right, y + 2 * FONT_HEIGHT_NORMAL + 1 + y_offset, str);
93 
94 			y_offset += FONT_HEIGHT_NORMAL + 1;
95 		}
96 
97 		y_offset -= FONT_HEIGHT_NORMAL + 1;
98 	} else {
99 		SetDParam(0, v->cargo_type);
100 		SetDParam(1, v->cargo_cap);
101 		SetDParam(4, GetCargoSubtypeText(v));
102 		DrawString(left, right, y + FONT_HEIGHT_NORMAL + y_offset, STR_VEHICLE_INFO_CAPACITY);
103 
104 		str = STR_VEHICLE_DETAILS_CARGO_EMPTY;
105 		if (v->cargo.StoredCount() > 0) {
106 			SetDParam(0, v->cargo_type);
107 			SetDParam(1, v->cargo.StoredCount());
108 			SetDParam(2, v->cargo.Source());
109 			str = STR_VEHICLE_DETAILS_CARGO_FROM;
110 			feeder_share += v->cargo.FeederShare();
111 		}
112 		DrawString(left, right, y + 2 * FONT_HEIGHT_NORMAL + 1 + y_offset, str);
113 	}
114 
115 	/* Draw Transfer credits text */
116 	SetDParam(0, feeder_share);
117 	DrawString(left, right, y + 3 * FONT_HEIGHT_NORMAL + 3 + y_offset, STR_VEHICLE_INFO_FEEDER_CARGO_VALUE);
118 }
119 
120 /**
121  * Draws an image of a road vehicle chain
122  * @param v         Front vehicle
123  * @param left      The minimum horizontal position
124  * @param right     The maximum horizontal position
125  * @param y         Vertical position to draw at
126  * @param selection Selected vehicle to draw a frame around
127  * @param skip      Number of pixels to skip at the front (for scrolling)
128  */
DrawRoadVehImage(const Vehicle * v,int left,int right,int y,VehicleID selection,EngineImageType image_type,int skip)129 void DrawRoadVehImage(const Vehicle *v, int left, int right, int y, VehicleID selection, EngineImageType image_type, int skip)
130 {
131 	bool rtl = _current_text_dir == TD_RTL;
132 	Direction dir = rtl ? DIR_E : DIR_W;
133 	const RoadVehicle *u = RoadVehicle::From(v);
134 
135 	DrawPixelInfo tmp_dpi, *old_dpi;
136 	int max_width = right - left + 1;
137 
138 	if (!FillDrawPixelInfo(&tmp_dpi, left, y, max_width, ScaleGUITrad(14))) return;
139 
140 	old_dpi = _cur_dpi;
141 	_cur_dpi = &tmp_dpi;
142 
143 	int px = rtl ? max_width + skip : -skip;
144 	for (; u != nullptr && (rtl ? px > 0 : px < max_width); u = u->Next()) {
145 		Point offset;
146 		int width = u->GetDisplayImageWidth(&offset);
147 
148 		if (rtl ? px + width > 0 : px - width < max_width) {
149 			PaletteID pal = (u->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(u);
150 			VehicleSpriteSeq seq;
151 			u->GetImage(dir, image_type, &seq);
152 			seq.Draw(px + (rtl ? -offset.x : offset.x), ScaleGUITrad(6) + offset.y, pal, (u->vehstatus & VS_CRASHED) != 0);
153 		}
154 
155 		px += rtl ? -width : width;
156 	}
157 
158 	if (v->index == selection) {
159 		DrawFrameRect((rtl ? px : 0), 0, (rtl ? max_width : px) - 1, ScaleGUITrad(13) - 1, COLOUR_WHITE, FR_BORDERONLY);
160 	}
161 
162 	_cur_dpi = old_dpi;
163 }
164