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 group_gui.cpp GUI for the group window. */
9
10 #include "stdafx.h"
11 #include "textbuf_gui.h"
12 #include "command_func.h"
13 #include "vehicle_gui.h"
14 #include "vehicle_base.h"
15 #include "string_func.h"
16 #include "strings_func.h"
17 #include "window_func.h"
18 #include "vehicle_func.h"
19 #include "autoreplace_gui.h"
20 #include "company_func.h"
21 #include "widgets/dropdown_func.h"
22 #include "tilehighlight_func.h"
23 #include "vehicle_gui_base.h"
24 #include "core/geometry_func.hpp"
25 #include "company_base.h"
26 #include "company_gui.h"
27 #include "gui.h"
28
29 #include "widgets/group_widget.h"
30
31 #include "table/sprites.h"
32
33 #include "safeguards.h"
34
35 static const int LEVEL_WIDTH = 10; ///< Indenting width of a sub-group in pixels
36
37 typedef GUIList<const Group*> GUIGroupList;
38
39 static const NWidgetPart _nested_group_widgets[] = {
40 NWidget(NWID_HORIZONTAL), // Window header
41 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
42 NWidget(WWT_CAPTION, COLOUR_GREY, WID_GL_CAPTION),
43 NWidget(WWT_SHADEBOX, COLOUR_GREY),
44 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
45 NWidget(WWT_STICKYBOX, COLOUR_GREY),
46 EndContainer(),
47 NWidget(NWID_HORIZONTAL),
48 /* left part */
49 NWidget(NWID_VERTICAL),
50 NWidget(WWT_PANEL, COLOUR_GREY, WID_GL_ALL_VEHICLES), SetFill(1, 0), EndContainer(),
51 NWidget(WWT_PANEL, COLOUR_GREY, WID_GL_DEFAULT_VEHICLES), SetFill(1, 0), EndContainer(),
52 NWidget(NWID_HORIZONTAL),
53 NWidget(WWT_MATRIX, COLOUR_GREY, WID_GL_LIST_GROUP), SetMatrixDataTip(1, 0, STR_GROUPS_CLICK_ON_GROUP_FOR_TOOLTIP),
54 SetFill(1, 0), SetResize(0, 1), SetScrollbar(WID_GL_LIST_GROUP_SCROLLBAR),
55 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_GL_LIST_GROUP_SCROLLBAR),
56 EndContainer(),
57 NWidget(WWT_PANEL, COLOUR_GREY, WID_GL_INFO), SetFill(1, 1), SetMinimalTextLines(3, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM), EndContainer(),
58 NWidget(NWID_HORIZONTAL),
59 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_CREATE_GROUP),
60 SetDataTip(SPR_GROUP_CREATE_TRAIN, STR_GROUP_CREATE_TOOLTIP),
61 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_DELETE_GROUP),
62 SetDataTip(SPR_GROUP_DELETE_TRAIN, STR_GROUP_DELETE_TOOLTIP),
63 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_RENAME_GROUP),
64 SetDataTip(SPR_GROUP_RENAME_TRAIN, STR_GROUP_RENAME_TOOLTIP),
65 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_LIVERY_GROUP),
66 SetDataTip(SPR_GROUP_LIVERY_TRAIN, STR_GROUP_LIVERY_TOOLTIP),
67 NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 0), EndContainer(),
68 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_REPLACE_PROTECTION),
69 SetDataTip(SPR_GROUP_REPLACE_OFF_TRAIN, STR_GROUP_REPLACE_PROTECTION_TOOLTIP),
70 EndContainer(),
71 EndContainer(),
72 /* right part */
73 NWidget(NWID_VERTICAL),
74 NWidget(NWID_HORIZONTAL),
75 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_GL_GROUP_BY_ORDER), SetMinimalSize(81, 12), SetDataTip(STR_STATION_VIEW_GROUP, STR_TOOLTIP_GROUP_ORDER),
76 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GL_GROUP_BY_DROPDOWN), SetMinimalSize(167, 12), SetDataTip(0x0, STR_TOOLTIP_GROUP_ORDER),
77 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(12, 12), SetResize(1, 0), EndContainer(),
78 EndContainer(),
79 NWidget(NWID_HORIZONTAL),
80 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GL_SORT_BY_ORDER), SetMinimalSize(81, 12), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
81 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GL_SORT_BY_DROPDOWN), SetMinimalSize(167, 12), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA),
82 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(12, 12), SetResize(1, 0), EndContainer(),
83 EndContainer(),
84 NWidget(NWID_HORIZONTAL),
85 NWidget(WWT_MATRIX, COLOUR_GREY, WID_GL_LIST_VEHICLE), SetMinimalSize(248, 0), SetMatrixDataTip(1, 0, STR_NULL), SetResize(1, 1), SetFill(1, 0), SetScrollbar(WID_GL_LIST_VEHICLE_SCROLLBAR),
86 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_GL_LIST_VEHICLE_SCROLLBAR),
87 EndContainer(),
88 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(1, 0), SetFill(1, 1), SetResize(1, 0), EndContainer(),
89 NWidget(NWID_HORIZONTAL),
90 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GL_AVAILABLE_VEHICLES), SetMinimalSize(106, 12),
91 SetDataTip(STR_BLACK_STRING, STR_VEHICLE_LIST_AVAILABLE_ENGINES_TOOLTIP),
92 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 0), SetResize(1, 0), EndContainer(),
93 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GL_MANAGE_VEHICLES_DROPDOWN), SetMinimalSize(118, 12),
94 SetDataTip(STR_VEHICLE_LIST_MANAGE_LIST, STR_VEHICLE_LIST_MANAGE_LIST_TOOLTIP),
95 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_STOP_ALL), SetMinimalSize(12, 12),
96 SetDataTip(SPR_FLAG_VEH_STOPPED, STR_VEHICLE_LIST_MASS_STOP_LIST_TOOLTIP),
97 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_START_ALL), SetMinimalSize(12, 12),
98 SetDataTip(SPR_FLAG_VEH_RUNNING, STR_VEHICLE_LIST_MASS_START_LIST_TOOLTIP),
99 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
100 EndContainer(),
101 EndContainer(),
102 EndContainer(),
103 };
104
105 class VehicleGroupWindow : public BaseVehicleListWindow {
106 private:
107 /* Columns in the group list */
108 enum ListColumns {
109 VGC_FOLD, ///< Fold / Unfold button.
110 VGC_NAME, ///< Group name.
111 VGC_PROTECT, ///< Autoreplace protect icon.
112 VGC_AUTOREPLACE, ///< Autoreplace active icon.
113 VGC_PROFIT, ///< Profit icon.
114 VGC_NUMBER, ///< Number of vehicles in the group.
115
116 VGC_END
117 };
118
119 VehicleID vehicle_sel; ///< Selected vehicle
120 GroupID group_sel; ///< Selected group (for drag/drop)
121 GroupID group_rename; ///< Group being renamed, INVALID_GROUP if none
122 GroupID group_over; ///< Group over which a vehicle is dragged, INVALID_GROUP if none
123 GroupID group_confirm; ///< Group awaiting delete confirmation
124 GUIGroupList groups; ///< List of groups
125 uint tiny_step_height; ///< Step height for the group list
126 Scrollbar *group_sb;
127
128 std::vector<int> indents; ///< Indentation levels
129
130 Dimension column_size[VGC_END]; ///< Size of the columns in the group list.
131
AddChildren(GUIGroupList * source,GroupID parent,int indent)132 void AddChildren(GUIGroupList *source, GroupID parent, int indent)
133 {
134 for (const Group *g : *source) {
135 if (g->parent != parent) continue;
136 this->groups.push_back(g);
137 this->indents.push_back(indent);
138 if (g->folded) {
139 /* Test if this group has children at all. If not, the folded flag should be cleared to avoid lingering unfold buttons in the list. */
140 auto child = std::find_if(source->begin(), source->end(), [g](const Group *child){ return child->parent == g->index; });
141 bool has_children = child != source->end();
142 Group::Get(g->index)->folded = has_children;
143 } else {
144 AddChildren(source, g->index, indent + 1);
145 }
146 }
147 }
148
149 /**
150 * (Re)Build the group list.
151 *
152 * @param owner The owner of the window
153 */
BuildGroupList(Owner owner)154 void BuildGroupList(Owner owner)
155 {
156 if (!this->groups.NeedRebuild()) return;
157
158 this->groups.clear();
159 this->indents.clear();
160
161 GUIGroupList list;
162
163 for (const Group *g : Group::Iterate()) {
164 if (g->owner == owner && g->vehicle_type == this->vli.vtype) {
165 list.push_back(g);
166 }
167 }
168
169 list.ForceResort();
170
171 /* Sort the groups by their name */
172 const Group *last_group[2] = { nullptr, nullptr };
173 char last_name[2][64] = { "", "" };
174 list.Sort([&](const Group * const &a, const Group * const &b) {
175 if (a != last_group[0]) {
176 last_group[0] = a;
177 SetDParam(0, a->index);
178 GetString(last_name[0], STR_GROUP_NAME, lastof(last_name[0]));
179 }
180
181 if (b != last_group[1]) {
182 last_group[1] = b;
183 SetDParam(0, b->index);
184 GetString(last_name[1], STR_GROUP_NAME, lastof(last_name[1]));
185 }
186
187 int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting).
188 if (r == 0) return a->index < b->index;
189 return r < 0;
190 });
191
192 AddChildren(&list, INVALID_GROUP, 0);
193
194 this->groups.shrink_to_fit();
195 this->groups.RebuildDone();
196 }
197
198 /**
199 * Compute tiny_step_height and column_size
200 * @return Total width required for the group list.
201 */
ComputeGroupInfoSize()202 uint ComputeGroupInfoSize()
203 {
204 this->column_size[VGC_FOLD] = maxdim(GetSpriteSize(SPR_CIRCLE_FOLDED), GetSpriteSize(SPR_CIRCLE_UNFOLDED));
205 this->tiny_step_height = this->column_size[VGC_FOLD].height;
206
207 this->column_size[VGC_NAME] = maxdim(GetStringBoundingBox(STR_GROUP_DEFAULT_TRAINS + this->vli.vtype), GetStringBoundingBox(STR_GROUP_ALL_TRAINS + this->vli.vtype));
208 this->column_size[VGC_NAME].width = std::max(170u, this->column_size[VGC_NAME].width);
209 this->tiny_step_height = std::max(this->tiny_step_height, this->column_size[VGC_NAME].height);
210
211 this->column_size[VGC_PROTECT] = GetSpriteSize(SPR_GROUP_REPLACE_PROTECT);
212 this->tiny_step_height = std::max(this->tiny_step_height, this->column_size[VGC_PROTECT].height);
213
214 this->column_size[VGC_AUTOREPLACE] = GetSpriteSize(SPR_GROUP_REPLACE_ACTIVE);
215 this->tiny_step_height = std::max(this->tiny_step_height, this->column_size[VGC_AUTOREPLACE].height);
216
217 this->column_size[VGC_PROFIT].width = 0;
218 this->column_size[VGC_PROFIT].height = 0;
219 static const SpriteID profit_sprites[] = {SPR_PROFIT_NA, SPR_PROFIT_NEGATIVE, SPR_PROFIT_SOME, SPR_PROFIT_LOT};
220 for (uint i = 0; i < lengthof(profit_sprites); i++) {
221 Dimension d = GetSpriteSize(profit_sprites[i]);
222 this->column_size[VGC_PROFIT] = maxdim(this->column_size[VGC_PROFIT], d);
223 }
224 this->tiny_step_height = std::max(this->tiny_step_height, this->column_size[VGC_PROFIT].height);
225
226 int num_vehicle = GetGroupNumVehicle(this->vli.company, ALL_GROUP, this->vli.vtype);
227 SetDParamMaxValue(0, num_vehicle, 3, FS_SMALL);
228 SetDParamMaxValue(1, num_vehicle, 3, FS_SMALL);
229 this->column_size[VGC_NUMBER] = GetStringBoundingBox(STR_GROUP_COUNT_WITH_SUBGROUP);
230 this->tiny_step_height = std::max(this->tiny_step_height, this->column_size[VGC_NUMBER].height);
231
232 this->tiny_step_height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
233
234 return WD_FRAMERECT_LEFT + 8 +
235 this->column_size[VGC_FOLD].width + 2 +
236 this->column_size[VGC_NAME].width + 8 +
237 this->column_size[VGC_PROTECT].width + 2 +
238 this->column_size[VGC_AUTOREPLACE].width + 2 +
239 this->column_size[VGC_PROFIT].width + 2 +
240 this->column_size[VGC_NUMBER].width + 2 +
241 WD_FRAMERECT_RIGHT;
242 }
243
244 /**
245 * Draw a row in the group list.
246 * @param y Top of the row.
247 * @param left Left of the row.
248 * @param right Right of the row.
249 * @param g_id Group to list.
250 * @param indent Indentation level.
251 * @param protection Whether autoreplace protection is set.
252 * @param has_children Whether the group has children and should have a fold / unfold button.
253 */
DrawGroupInfo(int y,int left,int right,GroupID g_id,int indent=0,bool protection=false,bool has_children=false) const254 void DrawGroupInfo(int y, int left, int right, GroupID g_id, int indent = 0, bool protection = false, bool has_children = false) const
255 {
256 /* Highlight the group if a vehicle is dragged over it */
257 if (g_id == this->group_over) {
258 GfxFillRect(left + WD_FRAMERECT_LEFT, y + WD_FRAMERECT_TOP + 1, right - WD_FRAMERECT_RIGHT, y + this->tiny_step_height - WD_FRAMERECT_BOTTOM - 1, _colour_gradient[COLOUR_GREY][7]);
259 }
260
261 if (g_id == NEW_GROUP) return;
262
263 /* draw the selected group in white, else we draw it in black */
264 TextColour colour = g_id == this->vli.index ? TC_WHITE : TC_BLACK;
265 const GroupStatistics &stats = GroupStatistics::Get(this->vli.company, g_id, this->vli.vtype);
266 bool rtl = _current_text_dir == TD_RTL;
267
268 /* draw fold / unfold button */
269 int x = rtl ? right - WD_FRAMERECT_RIGHT - 8 - this->column_size[VGC_FOLD].width + 1 : left + WD_FRAMERECT_LEFT + 8;
270 if (has_children) {
271 DrawSprite(Group::Get(g_id)->folded ? SPR_CIRCLE_FOLDED : SPR_CIRCLE_UNFOLDED, PAL_NONE, rtl ? x - indent : x + indent, y + (this->tiny_step_height - this->column_size[VGC_FOLD].height) / 2);
272 }
273
274 /* draw group name */
275 StringID str;
276 if (IsAllGroupID(g_id)) {
277 str = STR_GROUP_ALL_TRAINS + this->vli.vtype;
278 } else if (IsDefaultGroupID(g_id)) {
279 str = STR_GROUP_DEFAULT_TRAINS + this->vli.vtype;
280 } else {
281 SetDParam(0, g_id);
282 str = STR_GROUP_NAME;
283 }
284 x = rtl ? x - 2 - this->column_size[VGC_NAME].width : x + 2 + this->column_size[VGC_FOLD].width;
285 DrawString(x + (rtl ? 0 : indent), x + this->column_size[VGC_NAME].width - 1 - (rtl ? indent : 0), y + (this->tiny_step_height - this->column_size[VGC_NAME].height) / 2, str, colour);
286
287 /* draw autoreplace protection */
288 x = rtl ? x - 8 - this->column_size[VGC_PROTECT].width : x + 8 + this->column_size[VGC_NAME].width;
289 if (protection) DrawSprite(SPR_GROUP_REPLACE_PROTECT, PAL_NONE, x, y + (this->tiny_step_height - this->column_size[VGC_PROTECT].height) / 2);
290
291 /* draw autoreplace status */
292 x = rtl ? x - 2 - this->column_size[VGC_AUTOREPLACE].width : x + 2 + this->column_size[VGC_PROTECT].width;
293 if (stats.autoreplace_defined) DrawSprite(SPR_GROUP_REPLACE_ACTIVE, stats.autoreplace_finished ? PALETTE_CRASH : PAL_NONE, x, y + (this->tiny_step_height - this->column_size[VGC_AUTOREPLACE].height) / 2);
294
295 /* draw the profit icon */
296 x = rtl ? x - 2 - this->column_size[VGC_PROFIT].width : x + 2 + this->column_size[VGC_AUTOREPLACE].width;
297 SpriteID spr;
298 uint num_profit_vehicle = GetGroupNumProfitVehicle(this->vli.company, g_id, this->vli.vtype);
299 Money profit_last_year = GetGroupProfitLastYear(this->vli.company, g_id, this->vli.vtype);
300 if (num_profit_vehicle == 0) {
301 spr = SPR_PROFIT_NA;
302 } else if (profit_last_year < 0) {
303 spr = SPR_PROFIT_NEGATIVE;
304 } else if (profit_last_year < VEHICLE_PROFIT_THRESHOLD * num_profit_vehicle) {
305 spr = SPR_PROFIT_SOME;
306 } else {
307 spr = SPR_PROFIT_LOT;
308 }
309 DrawSprite(spr, PAL_NONE, x, y + (this->tiny_step_height - this->column_size[VGC_PROFIT].height) / 2);
310
311 /* draw the number of vehicles of the group */
312 x = rtl ? x - 2 - this->column_size[VGC_NUMBER].width : x + 2 + this->column_size[VGC_PROFIT].width;
313 int num_vehicle_with_subgroups = GetGroupNumVehicle(this->vli.company, g_id, this->vli.vtype);
314 int num_vehicle = GroupStatistics::Get(this->vli.company, g_id, this->vli.vtype).num_vehicle;
315 if (IsAllGroupID(g_id) || IsDefaultGroupID(g_id) || num_vehicle_with_subgroups == num_vehicle) {
316 SetDParam(0, num_vehicle);
317 DrawString(x, x + this->column_size[VGC_NUMBER].width - 1, y + (this->tiny_step_height - this->column_size[VGC_NUMBER].height) / 2, STR_TINY_COMMA, colour, SA_RIGHT | SA_FORCE);
318 } else {
319 SetDParam(0, num_vehicle);
320 SetDParam(1, num_vehicle_with_subgroups - num_vehicle);
321 DrawString(x, x + this->column_size[VGC_NUMBER].width - 1, y + (this->tiny_step_height - this->column_size[VGC_NUMBER].height) / 2, STR_GROUP_COUNT_WITH_SUBGROUP, colour, SA_RIGHT | SA_FORCE);
322 }
323 }
324
325 /**
326 * Mark the widget containing the currently highlighted group as dirty.
327 */
DirtyHighlightedGroupWidget()328 void DirtyHighlightedGroupWidget()
329 {
330 if (this->group_over == INVALID_GROUP) return;
331
332 if (IsAllGroupID(this->group_over)) {
333 this->SetWidgetDirty(WID_GL_ALL_VEHICLES);
334 } else if (IsDefaultGroupID(this->group_over)) {
335 this->SetWidgetDirty(WID_GL_DEFAULT_VEHICLES);
336 } else {
337 this->SetWidgetDirty(WID_GL_LIST_GROUP);
338 }
339 }
340
341 public:
VehicleGroupWindow(WindowDesc * desc,WindowNumber window_number)342 VehicleGroupWindow(WindowDesc *desc, WindowNumber window_number) : BaseVehicleListWindow(desc, window_number)
343 {
344 this->CreateNestedTree();
345
346 this->vscroll = this->GetScrollbar(WID_GL_LIST_VEHICLE_SCROLLBAR);
347 this->group_sb = this->GetScrollbar(WID_GL_LIST_GROUP_SCROLLBAR);
348
349 this->vli.index = ALL_GROUP;
350 this->vehicle_sel = INVALID_VEHICLE;
351 this->group_sel = INVALID_GROUP;
352 this->group_rename = INVALID_GROUP;
353 this->group_over = INVALID_GROUP;
354
355 this->BuildVehicleList();
356 this->SortVehicleList();
357
358 this->groups.ForceRebuild();
359 this->groups.NeedResort();
360 this->BuildGroupList(vli.company);
361 this->group_sb->SetCount((uint)this->groups.size());
362
363 this->GetWidget<NWidgetCore>(WID_GL_CAPTION)->widget_data = STR_VEHICLE_LIST_TRAIN_CAPTION + this->vli.vtype;
364 this->GetWidget<NWidgetCore>(WID_GL_LIST_VEHICLE)->tool_tip = STR_VEHICLE_LIST_TRAIN_LIST_TOOLTIP + this->vli.vtype;
365
366 this->GetWidget<NWidgetCore>(WID_GL_CREATE_GROUP)->widget_data += this->vli.vtype;
367 this->GetWidget<NWidgetCore>(WID_GL_RENAME_GROUP)->widget_data += this->vli.vtype;
368 this->GetWidget<NWidgetCore>(WID_GL_DELETE_GROUP)->widget_data += this->vli.vtype;
369 this->GetWidget<NWidgetCore>(WID_GL_LIVERY_GROUP)->widget_data += this->vli.vtype;
370 this->GetWidget<NWidgetCore>(WID_GL_REPLACE_PROTECTION)->widget_data += this->vli.vtype;
371
372 this->FinishInitNested(window_number);
373 this->owner = vli.company;
374 }
375
~VehicleGroupWindow()376 ~VehicleGroupWindow()
377 {
378 *this->sorting = this->vehgroups.GetListing();
379 }
380
UpdateWidgetSize(int widget,Dimension * size,const Dimension & padding,Dimension * fill,Dimension * resize)381 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
382 {
383 switch (widget) {
384 case WID_GL_LIST_GROUP: {
385 size->width = this->ComputeGroupInfoSize();
386 resize->height = this->tiny_step_height;
387
388 /* Minimum height is the height of the list widget minus all and default vehicles... */
389 size->height = 4 * GetVehicleListHeight(this->vli.vtype, this->tiny_step_height);
390
391 /* ... minus the buttons at the bottom ... */
392 uint max_icon_height = GetSpriteSize(this->GetWidget<NWidgetCore>(WID_GL_CREATE_GROUP)->widget_data).height;
393 max_icon_height = std::max(max_icon_height, GetSpriteSize(this->GetWidget<NWidgetCore>(WID_GL_RENAME_GROUP)->widget_data).height);
394 max_icon_height = std::max(max_icon_height, GetSpriteSize(this->GetWidget<NWidgetCore>(WID_GL_DELETE_GROUP)->widget_data).height);
395 max_icon_height = std::max(max_icon_height, GetSpriteSize(this->GetWidget<NWidgetCore>(WID_GL_REPLACE_PROTECTION)->widget_data).height);
396
397 /* ... minus the height of the group info ... */
398 max_icon_height += (FONT_HEIGHT_NORMAL * 3) + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
399
400 /* Get a multiple of tiny_step_height of that amount */
401 size->height = Ceil(size->height - max_icon_height, tiny_step_height);
402 break;
403 }
404
405 case WID_GL_ALL_VEHICLES:
406 case WID_GL_DEFAULT_VEHICLES:
407 size->width = this->ComputeGroupInfoSize();
408 size->height = this->tiny_step_height;
409 break;
410
411 case WID_GL_SORT_BY_ORDER: {
412 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
413 d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
414 d.height += padding.height;
415 *size = maxdim(*size, d);
416 break;
417 }
418
419 case WID_GL_LIST_VEHICLE:
420 this->ComputeGroupInfoSize();
421 resize->height = GetVehicleListHeight(this->vli.vtype, this->tiny_step_height);
422 size->height = 4 * resize->height;
423 break;
424
425 case WID_GL_MANAGE_VEHICLES_DROPDOWN: {
426 Dimension d = this->GetActionDropdownSize(true, true);
427 d.height += padding.height;
428 d.width += padding.width;
429 *size = maxdim(*size, d);
430 break;
431 }
432 }
433 }
434
435 /**
436 * Some data on this window has become invalid.
437 * @param data Information about the changed data.
438 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
439 */
OnInvalidateData(int data=0,bool gui_scope=true)440 void OnInvalidateData(int data = 0, bool gui_scope = true) override
441 {
442 if (data == 0) {
443 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
444 this->vehgroups.ForceRebuild();
445 this->groups.ForceRebuild();
446 } else {
447 this->vehgroups.ForceResort();
448 this->groups.ForceResort();
449 }
450
451 /* Process ID-invalidation in command-scope as well */
452 if (this->group_rename != INVALID_GROUP && !Group::IsValidID(this->group_rename)) {
453 CloseWindowByClass(WC_QUERY_STRING);
454 this->group_rename = INVALID_GROUP;
455 }
456
457 if (!(IsAllGroupID(this->vli.index) || IsDefaultGroupID(this->vli.index) || Group::IsValidID(this->vli.index))) {
458 this->vli.index = ALL_GROUP;
459 HideDropDownMenu(this);
460 }
461 this->SetDirty();
462 }
463
SetStringParameters(int widget) const464 void SetStringParameters(int widget) const override
465 {
466 switch (widget) {
467 case WID_GL_AVAILABLE_VEHICLES:
468 SetDParam(0, STR_VEHICLE_LIST_AVAILABLE_TRAINS + this->vli.vtype);
469 break;
470
471 case WID_GL_CAPTION:
472 /* If selected_group == DEFAULT_GROUP || ALL_GROUP, draw the standard caption
473 * We list all vehicles or ungrouped vehicles */
474 if (IsDefaultGroupID(this->vli.index) || IsAllGroupID(this->vli.index)) {
475 SetDParam(0, STR_COMPANY_NAME);
476 SetDParam(1, this->vli.company);
477 SetDParam(2, this->vehicles.size());
478 SetDParam(3, this->vehicles.size());
479 } else {
480 uint num_vehicle = GetGroupNumVehicle(this->vli.company, this->vli.index, this->vli.vtype);
481
482 SetDParam(0, STR_GROUP_NAME);
483 SetDParam(1, this->vli.index);
484 SetDParam(2, num_vehicle);
485 SetDParam(3, num_vehicle);
486 }
487 break;
488 }
489 }
490
OnPaint()491 void OnPaint() override
492 {
493 /* If we select the all vehicles, this->list will contain all vehicles of the owner
494 * else this->list will contain all vehicles which belong to the selected group */
495 this->BuildVehicleList();
496 this->SortVehicleList();
497
498 this->BuildGroupList(this->owner);
499
500 this->group_sb->SetCount(static_cast<int>(this->groups.size()));
501 this->vscroll->SetCount(static_cast<int>(this->vehgroups.size()));
502
503 /* The drop down menu is out, *but* it may not be used, retract it. */
504 if (this->vehicles.size() == 0 && this->IsWidgetLowered(WID_GL_MANAGE_VEHICLES_DROPDOWN)) {
505 this->RaiseWidget(WID_GL_MANAGE_VEHICLES_DROPDOWN);
506 HideDropDownMenu(this);
507 }
508
509 /* Disable all lists management button when the list is empty */
510 this->SetWidgetsDisabledState(this->vehicles.size() == 0 || _local_company != this->vli.company,
511 WID_GL_STOP_ALL,
512 WID_GL_START_ALL,
513 WID_GL_MANAGE_VEHICLES_DROPDOWN,
514 WIDGET_LIST_END);
515
516 /* Disable the group specific function when we select the default group or all vehicles */
517 this->SetWidgetsDisabledState(IsDefaultGroupID(this->vli.index) || IsAllGroupID(this->vli.index) || _local_company != this->vli.company,
518 WID_GL_DELETE_GROUP,
519 WID_GL_RENAME_GROUP,
520 WID_GL_LIVERY_GROUP,
521 WID_GL_REPLACE_PROTECTION,
522 WIDGET_LIST_END);
523
524 /* Disable remaining buttons for non-local companies
525 * Needed while changing _local_company, eg. by cheats
526 * All procedures (eg. move vehicle to another group)
527 * verify, whether you are the owner of the vehicle,
528 * so it doesn't have to be disabled
529 */
530 this->SetWidgetsDisabledState(_local_company != this->vli.company,
531 WID_GL_CREATE_GROUP,
532 WID_GL_AVAILABLE_VEHICLES,
533 WIDGET_LIST_END);
534
535 /* If not a default group and the group has replace protection, show an enabled replace sprite. */
536 uint16 protect_sprite = SPR_GROUP_REPLACE_OFF_TRAIN;
537 if (!IsDefaultGroupID(this->vli.index) && !IsAllGroupID(this->vli.index) && HasBit(Group::Get(this->vli.index)->flags, GroupFlags::GF_REPLACE_PROTECTION)) protect_sprite = SPR_GROUP_REPLACE_ON_TRAIN;
538 this->GetWidget<NWidgetCore>(WID_GL_REPLACE_PROTECTION)->widget_data = protect_sprite + this->vli.vtype;
539
540 /* Set text of "group by" dropdown widget. */
541 this->GetWidget<NWidgetCore>(WID_GL_GROUP_BY_DROPDOWN)->widget_data = this->vehicle_group_by_names[this->grouping];
542
543 /* Set text of "sort by" dropdown widget. */
544 this->GetWidget<NWidgetCore>(WID_GL_SORT_BY_DROPDOWN)->widget_data = this->GetVehicleSorterNames()[this->vehgroups.SortType()];
545
546 this->DrawWidgets();
547 }
548
DrawWidget(const Rect & r,int widget) const549 void DrawWidget(const Rect &r, int widget) const override
550 {
551 switch (widget) {
552 case WID_GL_ALL_VEHICLES:
553 DrawGroupInfo(r.top, r.left, r.right, ALL_GROUP);
554 break;
555
556 case WID_GL_DEFAULT_VEHICLES:
557 DrawGroupInfo(r.top, r.left, r.right, DEFAULT_GROUP);
558 break;
559
560 case WID_GL_INFO: {
561 Money this_year = 0;
562 Money last_year = 0;
563 uint64 occupancy = 0;
564
565 for (const Vehicle * const v : this->vehicles) {
566 assert(v->owner == this->owner);
567
568 this_year += v->GetDisplayProfitThisYear();
569 last_year += v->GetDisplayProfitLastYear();
570 occupancy += v->trip_occupancy;
571 }
572
573 const int left = r.left + WD_FRAMERECT_LEFT + 8;
574 const int right = r.right - WD_FRAMERECT_RIGHT - 8;
575
576 int y = r.top + WD_FRAMERECT_TOP;
577 DrawString(left, right, y, STR_GROUP_PROFIT_THIS_YEAR, TC_BLACK);
578 SetDParam(0, this_year);
579 DrawString(left, right, y, STR_JUST_CURRENCY_LONG, TC_BLACK, SA_RIGHT);
580
581 y += FONT_HEIGHT_NORMAL;
582 DrawString(left, right, y, STR_GROUP_PROFIT_LAST_YEAR, TC_BLACK);
583 SetDParam(0, last_year);
584 DrawString(left, right, y, STR_JUST_CURRENCY_LONG, TC_BLACK, SA_RIGHT);
585
586 y += FONT_HEIGHT_NORMAL;
587 DrawString(left, right, y, STR_GROUP_OCCUPANCY, TC_BLACK);
588 const size_t vehicle_count = this->vehicles.size();
589 if (vehicle_count > 0) {
590 SetDParam(0, occupancy / vehicle_count);
591 DrawString(left, right, y, STR_GROUP_OCCUPANCY_VALUE, TC_BLACK, SA_RIGHT);
592 }
593
594 break;
595 }
596
597 case WID_GL_LIST_GROUP: {
598 int y1 = r.top;
599 int max = std::min<size_t>(this->group_sb->GetPosition() + this->group_sb->GetCapacity(), this->groups.size());
600 for (int i = this->group_sb->GetPosition(); i < max; ++i) {
601 const Group *g = this->groups[i];
602
603 assert(g->owner == this->owner);
604
605 DrawGroupInfo(y1, r.left, r.right, g->index, this->indents[i] * LEVEL_WIDTH, HasBit(g->flags, GroupFlags::GF_REPLACE_PROTECTION), g->folded || (i + 1 < (int)this->groups.size() && indents[i + 1] > this->indents[i]));
606
607 y1 += this->tiny_step_height;
608 }
609 if ((uint)this->group_sb->GetPosition() + this->group_sb->GetCapacity() > this->groups.size()) {
610 DrawGroupInfo(y1, r.left, r.right, NEW_GROUP);
611 }
612 break;
613 }
614
615 case WID_GL_SORT_BY_ORDER:
616 this->DrawSortButtonState(WID_GL_SORT_BY_ORDER, this->vehgroups.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
617 break;
618
619 case WID_GL_LIST_VEHICLE:
620 if (this->vli.index != ALL_GROUP && this->grouping == GB_NONE) {
621 /* Mark vehicles which are in sub-groups (only if we are not using shared order coalescing) */
622 int y = r.top;
623 uint max = static_cast<uint>(std::min<size_t>(this->vscroll->GetPosition() + this->vscroll->GetCapacity(), this->vehgroups.size()));
624 for (uint i = this->vscroll->GetPosition(); i < max; ++i) {
625 const Vehicle *v = this->vehgroups[i].GetSingleVehicle();
626 if (v->group_id != this->vli.index) {
627 GfxFillRect(r.left + 1, y + 1, r.right - 1, y + this->resize.step_height - 2, _colour_gradient[COLOUR_GREY][3], FILLRECT_CHECKER);
628 }
629 y += this->resize.step_height;
630 }
631 }
632
633 this->DrawVehicleListItems(this->vehicle_sel, this->resize.step_height, r);
634 break;
635 }
636 }
637
DeleteGroupCallback(Window * win,bool confirmed)638 static void DeleteGroupCallback(Window *win, bool confirmed)
639 {
640 if (confirmed) {
641 VehicleGroupWindow *w = (VehicleGroupWindow*)win;
642 w->vli.index = ALL_GROUP;
643 DoCommandP(0, w->group_confirm, 0, CMD_DELETE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_DELETE));
644 }
645 }
646
OnClick(Point pt,int widget,int click_count)647 void OnClick(Point pt, int widget, int click_count) override
648 {
649 switch (widget) {
650 case WID_GL_SORT_BY_ORDER: // Flip sorting method ascending/descending
651 this->vehgroups.ToggleSortOrder();
652 this->SetDirty();
653 break;
654
655 case WID_GL_GROUP_BY_DROPDOWN: // Select grouping option dropdown menu
656 ShowDropDownMenu(this, this->vehicle_group_by_names, this->grouping, WID_GL_GROUP_BY_DROPDOWN, 0, 0);
657 return;
658
659 case WID_GL_SORT_BY_DROPDOWN: // Select sorting criteria dropdown menu
660 ShowDropDownMenu(this, this->GetVehicleSorterNames(), this->vehgroups.SortType(), WID_GL_SORT_BY_DROPDOWN, 0, (this->vli.vtype == VEH_TRAIN || this->vli.vtype == VEH_ROAD) ? 0 : (1 << 10));
661 return;
662
663 case WID_GL_ALL_VEHICLES: // All vehicles button
664 if (!IsAllGroupID(this->vli.index)) {
665 this->vli.index = ALL_GROUP;
666 this->vehgroups.ForceRebuild();
667 this->SetDirty();
668 }
669 break;
670
671 case WID_GL_DEFAULT_VEHICLES: // Ungrouped vehicles button
672 if (!IsDefaultGroupID(this->vli.index)) {
673 this->vli.index = DEFAULT_GROUP;
674 this->vehgroups.ForceRebuild();
675 this->SetDirty();
676 }
677 break;
678
679 case WID_GL_LIST_GROUP: { // Matrix Group
680 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP);
681 if (id_g >= this->groups.size()) return;
682
683 if (groups[id_g]->folded || (id_g + 1 < this->groups.size() && this->indents[id_g + 1] > this->indents[id_g])) {
684 /* The group has children, check if the user clicked the fold / unfold button. */
685 NWidgetCore *group_display = this->GetWidget<NWidgetCore>(widget);
686 int x = _current_text_dir == TD_RTL ?
687 group_display->pos_x + group_display->current_x - WD_FRAMERECT_RIGHT - 8 - this->indents[id_g] * LEVEL_WIDTH - this->column_size[VGC_FOLD].width :
688 group_display->pos_x + WD_FRAMERECT_LEFT + 8 + this->indents[id_g] * LEVEL_WIDTH;
689 if (click_count > 1 || (pt.x >= x && pt.x < (int)(x + this->column_size[VGC_FOLD].width))) {
690
691 GroupID g = this->vli.index;
692 if (!IsAllGroupID(g) && !IsDefaultGroupID(g)) {
693 do {
694 g = Group::Get(g)->parent;
695 if (g == groups[id_g]->index) {
696 this->vli.index = g;
697 break;
698 }
699 } while (g != INVALID_GROUP);
700 }
701
702 Group::Get(groups[id_g]->index)->folded = !groups[id_g]->folded;
703 this->groups.ForceRebuild();
704
705 this->SetDirty();
706 break;
707 }
708 }
709
710 this->group_sel = this->vli.index = this->groups[id_g]->index;
711
712 SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
713
714 this->vehgroups.ForceRebuild();
715 this->SetDirty();
716 break;
717 }
718
719 case WID_GL_LIST_VEHICLE: { // Matrix Vehicle
720 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_VEHICLE);
721 if (id_v >= this->vehgroups.size()) return; // click out of list bound
722
723 const GUIVehicleGroup &vehgroup = this->vehgroups[id_v];
724
725 const Vehicle *v = nullptr;
726
727 switch (this->grouping) {
728 case GB_NONE: {
729 const Vehicle *v2 = vehgroup.GetSingleVehicle();
730 if (VehicleClicked(v2)) break;
731 v = v2;
732 break;
733 }
734
735 case GB_SHARED_ORDERS: {
736 assert(vehgroup.NumVehicles() > 0);
737 v = vehgroup.vehicles_begin[0];
738 /*
739 * No VehicleClicked(v) support for now, because don't want
740 * to enable any contextual actions except perhaps clicking/ctrl-clicking to clone orders.
741 */
742 break;
743 }
744
745 default:
746 NOT_REACHED();
747 }
748 if (v) {
749 if (_ctrl_pressed) {
750 if (this->grouping == GB_NONE) {
751 this->SelectGroup(v->group_id);
752 } else {
753 ShowOrdersWindow(v);
754 }
755 } else {
756 this->vehicle_sel = v->index;
757 SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
758 SetMouseCursorVehicle(v, EIT_IN_LIST);
759 _cursor.vehchain = true;
760 this->SetDirty();
761 }
762 }
763
764 break;
765 }
766
767 case WID_GL_CREATE_GROUP: { // Create a new group
768 DoCommandP(0, this->vli.vtype, this->vli.index, CMD_CREATE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_CREATE), CcCreateGroup);
769 break;
770 }
771
772 case WID_GL_DELETE_GROUP: { // Delete the selected group
773 this->group_confirm = this->vli.index;
774 ShowQuery(STR_QUERY_GROUP_DELETE_CAPTION, STR_GROUP_DELETE_QUERY_TEXT, this, DeleteGroupCallback);
775 break;
776 }
777
778 case WID_GL_RENAME_GROUP: // Rename the selected roup
779 this->ShowRenameGroupWindow(this->vli.index, false);
780 break;
781
782 case WID_GL_LIVERY_GROUP: // Set group livery
783 ShowCompanyLiveryWindow(this->owner, this->vli.index);
784 break;
785
786 case WID_GL_AVAILABLE_VEHICLES:
787 ShowBuildVehicleWindow(INVALID_TILE, this->vli.vtype);
788 break;
789
790 case WID_GL_MANAGE_VEHICLES_DROPDOWN: {
791 ShowDropDownList(this, this->BuildActionDropdownList(true, Group::IsValidID(this->vli.index)), 0, WID_GL_MANAGE_VEHICLES_DROPDOWN);
792 break;
793 }
794
795 case WID_GL_START_ALL:
796 case WID_GL_STOP_ALL: { // Start/stop all vehicles of the list
797 DoCommandP(0, (1 << 1) | (widget == WID_GL_START_ALL ? (1 << 0) : 0), this->vli.Pack(), CMD_MASS_START_STOP);
798 break;
799 }
800
801 case WID_GL_REPLACE_PROTECTION: {
802 const Group *g = Group::GetIfValid(this->vli.index);
803 if (g != nullptr) {
804 DoCommandP(0, this->vli.index | (GroupFlags::GF_REPLACE_PROTECTION << 16), (HasBit(g->flags, GroupFlags::GF_REPLACE_PROTECTION) ? 0 : 1) | (_ctrl_pressed << 1), CMD_SET_GROUP_FLAG);
805 }
806 break;
807 }
808 }
809 }
810
OnDragDrop_Group(Point pt,int widget)811 void OnDragDrop_Group(Point pt, int widget)
812 {
813 const Group *g = Group::Get(this->group_sel);
814
815 switch (widget) {
816 case WID_GL_ALL_VEHICLES: // All vehicles
817 case WID_GL_DEFAULT_VEHICLES: // Ungrouped vehicles
818 if (g->parent != INVALID_GROUP) {
819 DoCommandP(0, this->group_sel | (1 << 16), INVALID_GROUP, CMD_ALTER_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_SET_PARENT));
820 }
821
822 this->group_sel = INVALID_GROUP;
823 this->group_over = INVALID_GROUP;
824 this->SetDirty();
825 break;
826
827 case WID_GL_LIST_GROUP: { // Matrix group
828 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP);
829 GroupID new_g = id_g >= this->groups.size() ? INVALID_GROUP : this->groups[id_g]->index;
830
831 if (this->group_sel != new_g && g->parent != new_g) {
832 DoCommandP(0, this->group_sel | (1 << 16), new_g, CMD_ALTER_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_SET_PARENT));
833 }
834
835 this->group_sel = INVALID_GROUP;
836 this->group_over = INVALID_GROUP;
837 this->SetDirty();
838 break;
839 }
840 }
841 }
842
OnDragDrop_Vehicle(Point pt,int widget)843 void OnDragDrop_Vehicle(Point pt, int widget)
844 {
845 switch (widget) {
846 case WID_GL_DEFAULT_VEHICLES: // Ungrouped vehicles
847 DoCommandP(0, DEFAULT_GROUP, this->vehicle_sel | (_ctrl_pressed || this->grouping == GB_SHARED_ORDERS ? 1 << 31 : 0), CMD_ADD_VEHICLE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_ADD_VEHICLE));
848
849 this->vehicle_sel = INVALID_VEHICLE;
850 this->group_over = INVALID_GROUP;
851
852 this->SetDirty();
853 break;
854
855 case WID_GL_LIST_GROUP: { // Matrix group
856 const VehicleID vindex = this->vehicle_sel;
857 this->vehicle_sel = INVALID_VEHICLE;
858 this->group_over = INVALID_GROUP;
859 this->SetDirty();
860
861 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP);
862 GroupID new_g = id_g >= this->groups.size() ? NEW_GROUP : this->groups[id_g]->index;
863
864 DoCommandP(0, new_g, vindex | (_ctrl_pressed || this->grouping == GB_SHARED_ORDERS ? 1 << 31 : 0), CMD_ADD_VEHICLE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_ADD_VEHICLE), new_g == NEW_GROUP ? CcAddVehicleNewGroup : nullptr);
865 break;
866 }
867
868 case WID_GL_LIST_VEHICLE: { // Matrix vehicle
869 const VehicleID vindex = this->vehicle_sel;
870 this->vehicle_sel = INVALID_VEHICLE;
871 this->group_over = INVALID_GROUP;
872 this->SetDirty();
873
874 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_VEHICLE);
875 if (id_v >= this->vehgroups.size()) return; // click out of list bound
876
877 const GUIVehicleGroup &vehgroup = this->vehgroups[id_v];
878 switch (this->grouping) {
879 case GB_NONE: {
880 const Vehicle *v = vehgroup.GetSingleVehicle();
881 if (!VehicleClicked(v) && vindex == v->index) {
882 ShowVehicleViewWindow(v);
883 }
884 break;
885 }
886
887 case GB_SHARED_ORDERS: {
888 const Vehicle *v = vehgroup.vehicles_begin[0];
889 /* We do not support VehicleClicked() here since the contextual action may only make sense for individual vehicles */
890
891 if (vindex == v->index) {
892 if (vehgroup.NumVehicles() == 1) {
893 ShowVehicleViewWindow(v);
894 } else {
895 ShowVehicleListWindow(v);
896 }
897 }
898 break;
899 }
900
901 default:
902 NOT_REACHED();
903 }
904 break;
905 }
906 }
907 }
908
OnDragDrop(Point pt,int widget)909 void OnDragDrop(Point pt, int widget) override
910 {
911 if (this->vehicle_sel != INVALID_VEHICLE) OnDragDrop_Vehicle(pt, widget);
912 if (this->group_sel != INVALID_GROUP) OnDragDrop_Group(pt, widget);
913
914 _cursor.vehchain = false;
915 }
916
OnQueryTextFinished(char * str)917 void OnQueryTextFinished(char *str) override
918 {
919 if (str != nullptr) DoCommandP(0, this->group_rename, 0, CMD_ALTER_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_RENAME), nullptr, str);
920 this->group_rename = INVALID_GROUP;
921 }
922
OnResize()923 void OnResize() override
924 {
925 this->group_sb->SetCapacityFromWidget(this, WID_GL_LIST_GROUP);
926 this->vscroll->SetCapacityFromWidget(this, WID_GL_LIST_VEHICLE);
927 }
928
OnDropdownSelect(int widget,int index)929 void OnDropdownSelect(int widget, int index) override
930 {
931 switch (widget) {
932 case WID_GL_GROUP_BY_DROPDOWN:
933 this->UpdateVehicleGroupBy(static_cast<GroupBy>(index));
934 break;
935
936 case WID_GL_SORT_BY_DROPDOWN:
937 this->vehgroups.SetSortType(index);
938 break;
939
940 case WID_GL_MANAGE_VEHICLES_DROPDOWN:
941 assert(this->vehicles.size() != 0);
942
943 switch (index) {
944 case ADI_REPLACE: // Replace window
945 ShowReplaceGroupVehicleWindow(this->vli.index, this->vli.vtype);
946 break;
947 case ADI_SERVICE: // Send for servicing
948 case ADI_DEPOT: { // Send to Depots
949 DoCommandP(0, DEPOT_MASS_SEND | (index == ADI_SERVICE ? DEPOT_SERVICE : 0U), this->vli.Pack(), GetCmdSendToDepot(this->vli.vtype));
950 break;
951 }
952
953 case ADI_ADD_SHARED: // Add shared Vehicles
954 assert(Group::IsValidID(this->vli.index));
955
956 DoCommandP(0, this->vli.index, this->vli.vtype, CMD_ADD_SHARED_VEHICLE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_ADD_SHARED_VEHICLE));
957 break;
958 case ADI_REMOVE_ALL: // Remove all Vehicles from the selected group
959 assert(Group::IsValidID(this->vli.index));
960
961 DoCommandP(0, this->vli.index, 0, CMD_REMOVE_ALL_VEHICLES_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_REMOVE_ALL_VEHICLES));
962 break;
963 default: NOT_REACHED();
964 }
965 break;
966
967 default: NOT_REACHED();
968 }
969
970 this->SetDirty();
971 }
972
OnGameTick()973 void OnGameTick() override
974 {
975 if (this->groups.NeedResort() || this->vehgroups.NeedResort()) {
976 this->SetDirty();
977 }
978 }
979
OnPlaceObjectAbort()980 void OnPlaceObjectAbort() override
981 {
982 /* abort drag & drop */
983 this->vehicle_sel = INVALID_VEHICLE;
984 this->DirtyHighlightedGroupWidget();
985 this->group_sel = INVALID_GROUP;
986 this->group_over = INVALID_GROUP;
987 this->SetWidgetDirty(WID_GL_LIST_VEHICLE);
988 }
989
OnMouseDrag(Point pt,int widget)990 void OnMouseDrag(Point pt, int widget) override
991 {
992 if (this->vehicle_sel == INVALID_VEHICLE && this->group_sel == INVALID_GROUP) return;
993
994 /* A vehicle is dragged over... */
995 GroupID new_group_over = INVALID_GROUP;
996 switch (widget) {
997 case WID_GL_DEFAULT_VEHICLES: // ... the 'default' group.
998 new_group_over = DEFAULT_GROUP;
999 break;
1000
1001 case WID_GL_LIST_GROUP: { // ... the list of custom groups.
1002 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP);
1003 new_group_over = id_g >= this->groups.size() ? NEW_GROUP : this->groups[id_g]->index;
1004 break;
1005 }
1006 }
1007
1008 /* Do not highlight when dragging over the current group */
1009 if (this->vehicle_sel != INVALID_VEHICLE) {
1010 if (Vehicle::Get(vehicle_sel)->group_id == new_group_over) new_group_over = INVALID_GROUP;
1011 } else if (this->group_sel != INVALID_GROUP) {
1012 if (this->group_sel == new_group_over || Group::Get(this->group_sel)->parent == new_group_over) new_group_over = INVALID_GROUP;
1013 }
1014
1015 /* Mark widgets as dirty if the group changed. */
1016 if (new_group_over != this->group_over) {
1017 this->DirtyHighlightedGroupWidget();
1018 this->group_over = new_group_over;
1019 this->DirtyHighlightedGroupWidget();
1020 }
1021 }
1022
ShowRenameGroupWindow(GroupID group,bool empty)1023 void ShowRenameGroupWindow(GroupID group, bool empty)
1024 {
1025 assert(Group::IsValidID(group));
1026 this->group_rename = group;
1027 /* Show empty query for new groups */
1028 StringID str = STR_EMPTY;
1029 if (!empty) {
1030 SetDParam(0, group);
1031 str = STR_GROUP_NAME;
1032 }
1033 ShowQueryString(str, STR_GROUP_RENAME_CAPTION, MAX_LENGTH_GROUP_NAME_CHARS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
1034 }
1035
1036 /**
1037 * Tests whether a given vehicle is selected in the window, and unselects it if necessary.
1038 * Called when the vehicle is deleted.
1039 * @param vehicle Vehicle that is going to be deleted
1040 */
UnselectVehicle(VehicleID vehicle)1041 void UnselectVehicle(VehicleID vehicle)
1042 {
1043 if (this->vehicle_sel == vehicle) ResetObjectToPlace();
1044 }
1045
1046 /**
1047 * Selects the specified group in the list
1048 *
1049 * @param g_id The ID of the group to be selected
1050 */
SelectGroup(const GroupID g_id)1051 void SelectGroup(const GroupID g_id)
1052 {
1053 if (g_id == INVALID_GROUP || g_id == this->vli.index) return;
1054
1055 this->vli.index = g_id;
1056 if (g_id != ALL_GROUP && g_id != DEFAULT_GROUP) {
1057 const Group *g = Group::Get(g_id);
1058 int id_g = find_index(this->groups, g);
1059 // The group's branch is maybe collapsed, so try to expand it
1060 if (id_g == -1) {
1061 for (auto pg = Group::GetIfValid(g->parent); pg != nullptr; pg = Group::GetIfValid(pg->parent)) {
1062 pg->folded = false;
1063 }
1064 this->groups.ForceRebuild();
1065 this->BuildGroupList(this->owner);
1066 this->group_sb->SetCount((uint)this->groups.size());
1067 id_g = find_index(this->groups, g);
1068 }
1069 this->group_sb->ScrollTowards(id_g);
1070 }
1071 this->vehgroups.ForceRebuild();
1072 this->SetDirty();
1073 }
1074
1075 };
1076
1077
1078 static WindowDesc _other_group_desc(
1079 WDP_AUTO, "list_groups", 460, 246,
1080 WC_INVALID, WC_NONE,
1081 0,
1082 _nested_group_widgets, lengthof(_nested_group_widgets)
1083 );
1084
1085 static WindowDesc _train_group_desc(
1086 WDP_AUTO, "list_groups_train", 525, 246,
1087 WC_TRAINS_LIST, WC_NONE,
1088 0,
1089 _nested_group_widgets, lengthof(_nested_group_widgets)
1090 );
1091
1092 /**
1093 * Show the group window for the given company and vehicle type.
1094 * @param company The company to show the window for.
1095 * @param vehicle_type The type of vehicle to show it for.
1096 * @param group The group to be selected. Defaults to INVALID_GROUP.
1097 * @param need_existing_window Whether the existing window is needed. Defaults to false.
1098 */
ShowCompanyGroup(CompanyID company,VehicleType vehicle_type,GroupID group=INVALID_GROUP,bool need_existing_window=false)1099 void ShowCompanyGroup(CompanyID company, VehicleType vehicle_type, GroupID group = INVALID_GROUP, bool need_existing_window = false)
1100 {
1101 if (!Company::IsValidID(company)) return;
1102
1103 const WindowNumber num = VehicleListIdentifier(VL_GROUP_LIST, vehicle_type, company).Pack();
1104 VehicleGroupWindow *w;
1105 if (vehicle_type == VEH_TRAIN) {
1106 w = AllocateWindowDescFront<VehicleGroupWindow>(&_train_group_desc, num, need_existing_window);
1107 } else {
1108 _other_group_desc.cls = GetWindowClassForVehicleType(vehicle_type);
1109 w = AllocateWindowDescFront<VehicleGroupWindow>(&_other_group_desc, num, need_existing_window);
1110 }
1111 if (w != nullptr) w->SelectGroup(group);
1112 }
1113
1114 /**
1115 * Show the group window for the given vehicle.
1116 * @param v The vehicle to show the window for.
1117 */
ShowCompanyGroupForVehicle(const Vehicle * v)1118 void ShowCompanyGroupForVehicle(const Vehicle *v)
1119 {
1120 ShowCompanyGroup(v->owner, v->type, v->group_id, true);
1121 }
1122
1123 /**
1124 * Finds a group list window determined by vehicle type and owner
1125 * @param vt vehicle type
1126 * @param owner owner of groups
1127 * @return pointer to VehicleGroupWindow, nullptr if not found
1128 */
FindVehicleGroupWindow(VehicleType vt,Owner owner)1129 static inline VehicleGroupWindow *FindVehicleGroupWindow(VehicleType vt, Owner owner)
1130 {
1131 return (VehicleGroupWindow *)FindWindowById(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_GROUP_LIST, vt, owner).Pack());
1132 }
1133
1134 /**
1135 * Opens a 'Rename group' window for newly created group.
1136 * @param result Did command succeed?
1137 * @param tile Unused.
1138 * @param p1 Vehicle type.
1139 * @param p2 Unused.
1140 * @param cmd Unused.
1141 * @see CmdCreateGroup
1142 */
CcCreateGroup(const CommandCost & result,TileIndex tile,uint32 p1,uint32 p2,uint32 cmd)1143 void CcCreateGroup(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
1144 {
1145 if (result.Failed()) return;
1146 assert(p1 <= VEH_AIRCRAFT);
1147
1148 VehicleGroupWindow *w = FindVehicleGroupWindow((VehicleType)p1, _current_company);
1149 if (w != nullptr) w->ShowRenameGroupWindow(_new_group_id, true);
1150 }
1151
1152 /**
1153 * Open rename window after adding a vehicle to a new group via drag and drop.
1154 * @param result Did command succeed?
1155 * @param tile Unused.
1156 * @param p1 Unused.
1157 * @param p2 Bit 0-19: Vehicle ID.
1158 * @param cmd Unused.
1159 */
CcAddVehicleNewGroup(const CommandCost & result,TileIndex tile,uint32 p1,uint32 p2,uint32 cmd)1160 void CcAddVehicleNewGroup(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
1161 {
1162 if (result.Failed()) return;
1163 assert(Vehicle::IsValidID(GB(p2, 0, 20)));
1164
1165 CcCreateGroup(result, 0, Vehicle::Get(GB(p2, 0, 20))->type, 0, cmd);
1166 }
1167
1168 /**
1169 * Removes the highlight of a vehicle in a group window
1170 * @param *v Vehicle to remove all highlights from
1171 */
DeleteGroupHighlightOfVehicle(const Vehicle * v)1172 void DeleteGroupHighlightOfVehicle(const Vehicle *v)
1173 {
1174 /* If we haven't got any vehicles on the mouse pointer, we haven't got any highlighted in any group windows either
1175 * If that is the case, we can skip looping though the windows and save time
1176 */
1177 if (_special_mouse_mode != WSM_DRAGDROP) return;
1178
1179 VehicleGroupWindow *w = FindVehicleGroupWindow(v->type, v->owner);
1180 if (w != nullptr) w->UnselectVehicle(v->index);
1181 }
1182