Add: allow making heightmap screenshot via console
[openttd-github.git] / src / group_gui.cpp
blobe711201a684799f96c3031a0025500275fee7cb7
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 */
8 /** @file group_gui.cpp GUI for the group window. */
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"
28 #include "widgets/group_widget.h"
30 #include "table/sprites.h"
32 #include "safeguards.h"
34 static const int LEVEL_WIDTH = 10; ///< Indenting width of a sub-group in pixels
36 typedef GUIList<const Group*> GUIGroupList;
38 static const NWidgetPart _nested_group_widgets[] = {
39 NWidget(NWID_HORIZONTAL), // Window header
40 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
41 NWidget(WWT_CAPTION, COLOUR_GREY, WID_GL_CAPTION),
42 NWidget(WWT_SHADEBOX, COLOUR_GREY),
43 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
44 NWidget(WWT_STICKYBOX, COLOUR_GREY),
45 EndContainer(),
46 NWidget(NWID_HORIZONTAL),
47 /* left part */
48 NWidget(NWID_VERTICAL),
49 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalTextLines(1, WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM), SetFill(1, 0), EndContainer(),
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, 0), EndContainer(),
58 NWidget(NWID_HORIZONTAL),
59 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_CREATE_GROUP), SetFill(0, 1),
60 SetDataTip(SPR_GROUP_CREATE_TRAIN, STR_GROUP_CREATE_TOOLTIP),
61 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_DELETE_GROUP), SetFill(0, 1),
62 SetDataTip(SPR_GROUP_DELETE_TRAIN, STR_GROUP_DELETE_TOOLTIP),
63 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_RENAME_GROUP), SetFill(0, 1),
64 SetDataTip(SPR_GROUP_RENAME_TRAIN, STR_GROUP_RENAME_TOOLTIP),
65 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_LIVERY_GROUP), SetFill(0, 1),
66 SetDataTip(SPR_GROUP_LIVERY_TRAIN, STR_GROUP_LIVERY_TOOLTIP),
67 NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), EndContainer(),
68 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_REPLACE_PROTECTION), SetFill(0, 1),
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), SetFill(0, 1),
91 SetDataTip(STR_BLACK_STRING, STR_VEHICLE_LIST_AVAILABLE_ENGINES_TOOLTIP),
92 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 1), SetResize(1, 0), EndContainer(),
93 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GL_MANAGE_VEHICLES_DROPDOWN), SetMinimalSize(118, 12), SetFill(0, 1),
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), SetFill(0, 1),
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), SetFill(0, 1),
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(),
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.
116 VGC_END
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;
128 std::vector<int> indents; ///< Indentation levels
130 Dimension column_size[VGC_END]; ///< Size of the columns in the group list.
132 void AddChildren(GUIGroupList *source, GroupID parent, int indent)
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);
150 * (Re)Build the group list.
152 * @param owner The owner of the window
154 void BuildGroupList(Owner owner)
156 if (!this->groups.NeedRebuild()) return;
158 this->groups.clear();
159 this->indents.clear();
161 GUIGroupList list;
163 for (const Group *g : Group::Iterate()) {
164 if (g->owner == owner && g->vehicle_type == this->vli.vtype) {
165 list.push_back(g);
169 list.ForceResort();
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]));
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]));
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;
192 AddChildren(&list, INVALID_GROUP, 0);
194 this->groups.shrink_to_fit();
195 this->groups.RebuildDone();
199 * Compute tiny_step_height and column_size
200 * @return Total width required for the group list.
202 uint ComputeGroupInfoSize()
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;
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);
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);
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);
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);
224 this->tiny_step_height = std::max(this->tiny_step_height, this->column_size[VGC_PROFIT].height);
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);
232 this->tiny_step_height += WD_MATRIX_TOP;
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;
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.
254 void DrawGroupInfo(int y, int left, int right, GroupID g_id, int indent = 0, bool protection = false, bool has_children = false) const
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, right - WD_FRAMERECT_RIGHT, y + this->tiny_step_height - WD_FRAMERECT_BOTTOM - WD_MATRIX_TOP, _colour_gradient[COLOUR_GREY][7]);
261 if (g_id == NEW_GROUP) return;
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;
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);
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;
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);
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);
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);
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;
309 DrawSprite(spr, PAL_NONE, x, y + (this->tiny_step_height - this->column_size[VGC_PROFIT].height) / 2);
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);
326 * Mark the widget containing the currently highlighted group as dirty.
328 void DirtyHighlightedGroupWidget()
330 if (this->group_over == INVALID_GROUP) return;
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);
341 public:
342 VehicleGroupWindow(WindowDesc *desc, WindowNumber window_number) : BaseVehicleListWindow(desc, window_number)
344 this->CreateNestedTree();
346 this->vscroll = this->GetScrollbar(WID_GL_LIST_VEHICLE_SCROLLBAR);
347 this->group_sb = this->GetScrollbar(WID_GL_LIST_GROUP_SCROLLBAR);
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;
355 this->BuildVehicleList();
356 this->SortVehicleList();
358 this->groups.ForceRebuild();
359 this->groups.NeedResort();
360 this->BuildGroupList(vli.company);
361 this->group_sb->SetCount((uint)this->groups.size());
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;
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;
372 this->FinishInitNested(window_number);
373 this->owner = vli.company;
376 ~VehicleGroupWindow()
378 *this->sorting = this->vehgroups.GetListing();
381 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
383 switch (widget) {
384 case WID_GL_LIST_GROUP: {
385 size->width = this->ComputeGroupInfoSize();
386 resize->height = this->tiny_step_height;
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) - 2 * this->tiny_step_height;
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);
397 /* ... minus the height of the group info ... */
398 max_icon_height += (FONT_HEIGHT_NORMAL * 3) + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
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;
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;
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;
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;
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;
433 case WID_GL_INFO: {
434 size->height = (FONT_HEIGHT_NORMAL * 3) + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
435 break;
441 * Some data on this window has become invalid.
442 * @param data Information about the changed data.
443 * @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.
445 void OnInvalidateData(int data = 0, bool gui_scope = true) override
447 if (data == 0) {
448 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
449 this->vehgroups.ForceRebuild();
450 this->groups.ForceRebuild();
451 } else {
452 this->vehgroups.ForceResort();
453 this->groups.ForceResort();
456 /* Process ID-invalidation in command-scope as well */
457 if (this->group_rename != INVALID_GROUP && !Group::IsValidID(this->group_rename)) {
458 DeleteWindowByClass(WC_QUERY_STRING);
459 this->group_rename = INVALID_GROUP;
462 if (!(IsAllGroupID(this->vli.index) || IsDefaultGroupID(this->vli.index) || Group::IsValidID(this->vli.index))) {
463 this->vli.index = ALL_GROUP;
464 HideDropDownMenu(this);
466 this->SetDirty();
469 void SetStringParameters(int widget) const override
471 switch (widget) {
472 case WID_GL_AVAILABLE_VEHICLES:
473 SetDParam(0, STR_VEHICLE_LIST_AVAILABLE_TRAINS + this->vli.vtype);
474 break;
476 case WID_GL_CAPTION:
477 /* If selected_group == DEFAULT_GROUP || ALL_GROUP, draw the standard caption
478 * We list all vehicles or ungrouped vehicles */
479 if (IsDefaultGroupID(this->vli.index) || IsAllGroupID(this->vli.index)) {
480 SetDParam(0, STR_COMPANY_NAME);
481 SetDParam(1, this->vli.company);
482 SetDParam(2, this->vehicles.size());
483 SetDParam(3, this->vehicles.size());
484 } else {
485 uint num_vehicle = GetGroupNumVehicle(this->vli.company, this->vli.index, this->vli.vtype);
487 SetDParam(0, STR_GROUP_NAME);
488 SetDParam(1, this->vli.index);
489 SetDParam(2, num_vehicle);
490 SetDParam(3, num_vehicle);
492 break;
496 void OnPaint() override
498 /* If we select the all vehicles, this->list will contain all vehicles of the owner
499 * else this->list will contain all vehicles which belong to the selected group */
500 this->BuildVehicleList();
501 this->SortVehicleList();
503 this->BuildGroupList(this->owner);
505 this->group_sb->SetCount(static_cast<int>(this->groups.size()));
506 this->vscroll->SetCount(static_cast<int>(this->vehgroups.size()));
508 /* The drop down menu is out, *but* it may not be used, retract it. */
509 if (this->vehicles.size() == 0 && this->IsWidgetLowered(WID_GL_MANAGE_VEHICLES_DROPDOWN)) {
510 this->RaiseWidget(WID_GL_MANAGE_VEHICLES_DROPDOWN);
511 HideDropDownMenu(this);
514 /* Disable all lists management button when the list is empty */
515 this->SetWidgetsDisabledState(this->vehicles.size() == 0 || _local_company != this->vli.company,
516 WID_GL_STOP_ALL,
517 WID_GL_START_ALL,
518 WID_GL_MANAGE_VEHICLES_DROPDOWN,
519 WIDGET_LIST_END);
521 /* Disable the group specific function when we select the default group or all vehicles */
522 this->SetWidgetsDisabledState(IsDefaultGroupID(this->vli.index) || IsAllGroupID(this->vli.index) || _local_company != this->vli.company,
523 WID_GL_DELETE_GROUP,
524 WID_GL_RENAME_GROUP,
525 WID_GL_LIVERY_GROUP,
526 WID_GL_REPLACE_PROTECTION,
527 WIDGET_LIST_END);
529 /* Disable remaining buttons for non-local companies
530 * Needed while changing _local_company, eg. by cheats
531 * All procedures (eg. move vehicle to another group)
532 * verify, whether you are the owner of the vehicle,
533 * so it doesn't have to be disabled
535 this->SetWidgetsDisabledState(_local_company != this->vli.company,
536 WID_GL_CREATE_GROUP,
537 WID_GL_AVAILABLE_VEHICLES,
538 WIDGET_LIST_END);
540 /* If not a default group and the group has replace protection, show an enabled replace sprite. */
541 uint16 protect_sprite = SPR_GROUP_REPLACE_OFF_TRAIN;
542 if (!IsDefaultGroupID(this->vli.index) && !IsAllGroupID(this->vli.index) && Group::Get(this->vli.index)->replace_protection) protect_sprite = SPR_GROUP_REPLACE_ON_TRAIN;
543 this->GetWidget<NWidgetCore>(WID_GL_REPLACE_PROTECTION)->widget_data = protect_sprite + this->vli.vtype;
545 /* Set text of "group by" dropdown widget. */
546 this->GetWidget<NWidgetCore>(WID_GL_GROUP_BY_DROPDOWN)->widget_data = this->vehicle_group_by_names[this->grouping];
548 /* Set text of "sort by" dropdown widget. */
549 this->GetWidget<NWidgetCore>(WID_GL_SORT_BY_DROPDOWN)->widget_data = this->GetVehicleSorterNames()[this->vehgroups.SortType()];
551 this->DrawWidgets();
554 void DrawWidget(const Rect &r, int widget) const override
556 switch (widget) {
557 case WID_GL_ALL_VEHICLES:
558 DrawGroupInfo(r.top + WD_FRAMERECT_TOP, r.left, r.right, ALL_GROUP);
559 break;
561 case WID_GL_DEFAULT_VEHICLES:
562 DrawGroupInfo(r.top + WD_FRAMERECT_TOP, r.left, r.right, DEFAULT_GROUP);
563 break;
565 case WID_GL_INFO: {
566 Money this_year = 0;
567 Money last_year = 0;
568 uint64 occupancy = 0;
570 for (const Vehicle * const v : this->vehicles) {
571 assert(v->owner == this->owner);
573 this_year += v->GetDisplayProfitThisYear();
574 last_year += v->GetDisplayProfitLastYear();
575 occupancy += v->trip_occupancy;
578 const int left = r.left + WD_FRAMERECT_LEFT + 8;
579 const int right = r.right - WD_FRAMERECT_RIGHT - 8;
581 int y = r.top + WD_FRAMERECT_TOP;
582 DrawString(left, right, y, STR_GROUP_PROFIT_THIS_YEAR, TC_BLACK);
583 SetDParam(0, this_year);
584 DrawString(left, right, y, STR_JUST_CURRENCY_LONG, TC_BLACK, SA_RIGHT);
586 y += FONT_HEIGHT_NORMAL;
587 DrawString(left, right, y, STR_GROUP_PROFIT_LAST_YEAR, TC_BLACK);
588 SetDParam(0, last_year);
589 DrawString(left, right, y, STR_JUST_CURRENCY_LONG, TC_BLACK, SA_RIGHT);
591 y += FONT_HEIGHT_NORMAL;
592 DrawString(left, right, y, STR_GROUP_OCCUPANCY, TC_BLACK);
593 const size_t vehicle_count = this->vehicles.size();
594 if (vehicle_count > 0) {
595 SetDParam(0, occupancy / vehicle_count);
596 DrawString(left, right, y, STR_GROUP_OCCUPANCY_VALUE, TC_BLACK, SA_RIGHT);
599 break;
602 case WID_GL_LIST_GROUP: {
603 int y1 = r.top + WD_FRAMERECT_TOP;
604 int max = std::min<size_t>(this->group_sb->GetPosition() + this->group_sb->GetCapacity(), this->groups.size());
605 for (int i = this->group_sb->GetPosition(); i < max; ++i) {
606 const Group *g = this->groups[i];
608 assert(g->owner == this->owner);
610 DrawGroupInfo(y1, r.left, r.right, g->index, this->indents[i] * LEVEL_WIDTH, g->replace_protection, g->folded || (i + 1 < (int)this->groups.size() && indents[i + 1] > this->indents[i]));
612 y1 += this->tiny_step_height;
614 if ((uint)this->group_sb->GetPosition() + this->group_sb->GetCapacity() > this->groups.size()) {
615 DrawGroupInfo(y1, r.left, r.right, NEW_GROUP);
617 break;
620 case WID_GL_SORT_BY_ORDER:
621 this->DrawSortButtonState(WID_GL_SORT_BY_ORDER, this->vehgroups.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
622 break;
624 case WID_GL_LIST_VEHICLE:
625 if (this->vli.index != ALL_GROUP && this->grouping == GB_NONE) {
626 /* Mark vehicles which are in sub-groups (only if we are not using shared order coalescing) */
627 int y = r.top;
628 uint max = static_cast<uint>(std::min<size_t>(this->vscroll->GetPosition() + this->vscroll->GetCapacity(), this->vehgroups.size()));
629 for (uint i = this->vscroll->GetPosition(); i < max; ++i) {
630 const Vehicle *v = this->vehgroups[i].GetSingleVehicle();
631 if (v->group_id != this->vli.index) {
632 GfxFillRect(r.left + 1, y + 1, r.right - 1, y + this->resize.step_height - 2, _colour_gradient[COLOUR_GREY][3], FILLRECT_CHECKER);
634 y += this->resize.step_height;
638 this->DrawVehicleListItems(this->vehicle_sel, this->resize.step_height, r);
639 break;
643 static void DeleteGroupCallback(Window *win, bool confirmed)
645 if (confirmed) {
646 VehicleGroupWindow *w = (VehicleGroupWindow*)win;
647 w->vli.index = ALL_GROUP;
648 DoCommandP(0, w->group_confirm, 0, CMD_DELETE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_DELETE));
652 void OnClick(Point pt, int widget, int click_count) override
654 switch (widget) {
655 case WID_GL_SORT_BY_ORDER: // Flip sorting method ascending/descending
656 this->vehgroups.ToggleSortOrder();
657 this->SetDirty();
658 break;
660 case WID_GL_GROUP_BY_DROPDOWN: // Select grouping option dropdown menu
661 ShowDropDownMenu(this, this->vehicle_group_by_names, this->grouping, WID_GL_GROUP_BY_DROPDOWN, 0, 0);
662 return;
664 case WID_GL_SORT_BY_DROPDOWN: // Select sorting criteria dropdown menu
665 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));
666 return;
668 case WID_GL_ALL_VEHICLES: // All vehicles button
669 if (!IsAllGroupID(this->vli.index)) {
670 this->vli.index = ALL_GROUP;
671 this->vehgroups.ForceRebuild();
672 this->SetDirty();
674 break;
676 case WID_GL_DEFAULT_VEHICLES: // Ungrouped vehicles button
677 if (!IsDefaultGroupID(this->vli.index)) {
678 this->vli.index = DEFAULT_GROUP;
679 this->vehgroups.ForceRebuild();
680 this->SetDirty();
682 break;
684 case WID_GL_LIST_GROUP: { // Matrix Group
685 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP, 0, this->tiny_step_height);
686 if (id_g >= this->groups.size()) return;
688 if (groups[id_g]->folded || (id_g + 1 < this->groups.size() && this->indents[id_g + 1] > this->indents[id_g])) {
689 /* The group has children, check if the user clicked the fold / unfold button. */
690 NWidgetCore *group_display = this->GetWidget<NWidgetCore>(widget);
691 int x = _current_text_dir == TD_RTL ?
692 group_display->pos_x + group_display->current_x - WD_FRAMERECT_RIGHT - 8 - this->indents[id_g] * LEVEL_WIDTH - this->column_size[VGC_FOLD].width :
693 group_display->pos_x + WD_FRAMERECT_LEFT + 8 + this->indents[id_g] * LEVEL_WIDTH;
694 if (click_count > 1 || (pt.x >= x && pt.x < (int)(x + this->column_size[VGC_FOLD].width))) {
696 GroupID g = this->vli.index;
697 if (!IsAllGroupID(g) && !IsDefaultGroupID(g)) {
698 do {
699 g = Group::Get(g)->parent;
700 if (g == groups[id_g]->index) {
701 this->vli.index = g;
702 break;
704 } while (g != INVALID_GROUP);
707 Group::Get(groups[id_g]->index)->folded = !groups[id_g]->folded;
708 this->groups.ForceRebuild();
710 this->SetDirty();
711 break;
715 this->group_sel = this->vli.index = this->groups[id_g]->index;
717 SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
719 this->vehgroups.ForceRebuild();
720 this->SetDirty();
721 break;
724 case WID_GL_LIST_VEHICLE: { // Matrix Vehicle
725 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_VEHICLE);
726 if (id_v >= this->vehgroups.size()) return; // click out of list bound
728 const GUIVehicleGroup &vehgroup = this->vehgroups[id_v];
730 const Vehicle *v = nullptr;
732 switch (this->grouping) {
733 case GB_NONE: {
734 const Vehicle *v2 = vehgroup.GetSingleVehicle();
735 if (VehicleClicked(v2)) break;
736 v = v2;
737 break;
740 case GB_SHARED_ORDERS: {
741 assert(vehgroup.NumVehicles() > 0);
742 v = vehgroup.vehicles_begin[0];
744 * No VehicleClicked(v) support for now, because don't want
745 * to enable any contextual actions except perhaps clicking/ctrl-clicking to clone orders.
747 break;
750 default:
751 NOT_REACHED();
753 if (v) {
754 this->vehicle_sel = v->index;
756 if (_ctrl_pressed) {
757 this->SelectGroup(v->group_id);
760 SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
761 SetMouseCursorVehicle(v, EIT_IN_LIST);
762 _cursor.vehchain = true;
764 this->SetDirty();
767 break;
770 case WID_GL_CREATE_GROUP: { // Create a new group
771 DoCommandP(0, this->vli.vtype, this->vli.index, CMD_CREATE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_CREATE), CcCreateGroup);
772 break;
775 case WID_GL_DELETE_GROUP: { // Delete the selected group
776 this->group_confirm = this->vli.index;
777 ShowQuery(STR_QUERY_GROUP_DELETE_CAPTION, STR_GROUP_DELETE_QUERY_TEXT, this, DeleteGroupCallback);
778 break;
781 case WID_GL_RENAME_GROUP: // Rename the selected roup
782 this->ShowRenameGroupWindow(this->vli.index, false);
783 break;
785 case WID_GL_LIVERY_GROUP: // Set group livery
786 ShowCompanyLiveryWindow(this->owner, this->vli.index);
787 break;
789 case WID_GL_AVAILABLE_VEHICLES:
790 ShowBuildVehicleWindow(INVALID_TILE, this->vli.vtype);
791 break;
793 case WID_GL_MANAGE_VEHICLES_DROPDOWN: {
794 ShowDropDownList(this, this->BuildActionDropdownList(true, Group::IsValidID(this->vli.index)), 0, WID_GL_MANAGE_VEHICLES_DROPDOWN);
795 break;
798 case WID_GL_START_ALL:
799 case WID_GL_STOP_ALL: { // Start/stop all vehicles of the list
800 DoCommandP(0, (1 << 1) | (widget == WID_GL_START_ALL ? (1 << 0) : 0), this->vli.Pack(), CMD_MASS_START_STOP);
801 break;
804 case WID_GL_REPLACE_PROTECTION: {
805 const Group *g = Group::GetIfValid(this->vli.index);
806 if (g != nullptr) {
807 DoCommandP(0, this->vli.index, (g->replace_protection ? 0 : 1) | (_ctrl_pressed << 1), CMD_SET_GROUP_REPLACE_PROTECTION);
809 break;
814 void OnDragDrop_Group(Point pt, int widget)
816 const Group *g = Group::Get(this->group_sel);
818 switch (widget) {
819 case WID_GL_ALL_VEHICLES: // All vehicles
820 case WID_GL_DEFAULT_VEHICLES: // Ungrouped vehicles
821 if (g->parent != INVALID_GROUP) {
822 DoCommandP(0, this->group_sel | (1 << 16), INVALID_GROUP, CMD_ALTER_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_SET_PARENT));
825 this->group_sel = INVALID_GROUP;
826 this->group_over = INVALID_GROUP;
827 this->SetDirty();
828 break;
830 case WID_GL_LIST_GROUP: { // Matrix group
831 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP, 0, this->tiny_step_height);
832 GroupID new_g = id_g >= this->groups.size() ? INVALID_GROUP : this->groups[id_g]->index;
834 if (this->group_sel != new_g && g->parent != new_g) {
835 DoCommandP(0, this->group_sel | (1 << 16), new_g, CMD_ALTER_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_SET_PARENT));
838 this->group_sel = INVALID_GROUP;
839 this->group_over = INVALID_GROUP;
840 this->SetDirty();
841 break;
846 void OnDragDrop_Vehicle(Point pt, int widget)
848 switch (widget) {
849 case WID_GL_DEFAULT_VEHICLES: // Ungrouped vehicles
850 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));
852 this->vehicle_sel = INVALID_VEHICLE;
853 this->group_over = INVALID_GROUP;
855 this->SetDirty();
856 break;
858 case WID_GL_LIST_GROUP: { // Matrix group
859 const VehicleID vindex = this->vehicle_sel;
860 this->vehicle_sel = INVALID_VEHICLE;
861 this->group_over = INVALID_GROUP;
862 this->SetDirty();
864 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP, 0, this->tiny_step_height);
865 GroupID new_g = id_g >= this->groups.size() ? NEW_GROUP : this->groups[id_g]->index;
867 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);
868 break;
871 case WID_GL_LIST_VEHICLE: { // Matrix vehicle
872 const VehicleID vindex = this->vehicle_sel;
873 this->vehicle_sel = INVALID_VEHICLE;
874 this->group_over = INVALID_GROUP;
875 this->SetDirty();
877 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_VEHICLE);
878 if (id_v >= this->vehgroups.size()) return; // click out of list bound
880 const GUIVehicleGroup &vehgroup = this->vehgroups[id_v];
881 switch (this->grouping) {
882 case GB_NONE: {
883 const Vehicle *v = vehgroup.GetSingleVehicle();
884 if (!VehicleClicked(v) && vindex == v->index) {
885 ShowVehicleViewWindow(v);
887 break;
890 case GB_SHARED_ORDERS: {
891 const Vehicle *v = vehgroup.vehicles_begin[0];
892 /* We do not support VehicleClicked() here since the contextual action may only make sense for individual vehicles */
894 if (vindex == v->index) {
895 ShowVehicleListWindow(v);
897 break;
900 default:
901 NOT_REACHED();
903 break;
908 void OnDragDrop(Point pt, int widget) override
910 if (this->vehicle_sel != INVALID_VEHICLE) OnDragDrop_Vehicle(pt, widget);
911 if (this->group_sel != INVALID_GROUP) OnDragDrop_Group(pt, widget);
913 _cursor.vehchain = false;
916 void OnQueryTextFinished(char *str) override
918 if (str != nullptr) DoCommandP(0, this->group_rename, 0, CMD_ALTER_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_RENAME), nullptr, str);
919 this->group_rename = INVALID_GROUP;
922 void OnResize() override
924 this->group_sb->SetCapacityFromWidget(this, WID_GL_LIST_GROUP);
925 this->vscroll->SetCapacityFromWidget(this, WID_GL_LIST_VEHICLE);
928 void OnDropdownSelect(int widget, int index) override
930 switch (widget) {
931 case WID_GL_GROUP_BY_DROPDOWN:
932 this->UpdateVehicleGroupBy(static_cast<GroupBy>(index));
933 break;
935 case WID_GL_SORT_BY_DROPDOWN:
936 this->vehgroups.SetSortType(index);
937 break;
939 case WID_GL_MANAGE_VEHICLES_DROPDOWN:
940 assert(this->vehicles.size() != 0);
942 switch (index) {
943 case ADI_REPLACE: // Replace window
944 ShowReplaceGroupVehicleWindow(this->vli.index, this->vli.vtype);
945 break;
946 case ADI_SERVICE: // Send for servicing
947 case ADI_DEPOT: { // Send to Depots
948 DoCommandP(0, DEPOT_MASS_SEND | (index == ADI_SERVICE ? DEPOT_SERVICE : 0U), this->vli.Pack(), GetCmdSendToDepot(this->vli.vtype));
949 break;
952 case ADI_ADD_SHARED: // Add shared Vehicles
953 assert(Group::IsValidID(this->vli.index));
955 DoCommandP(0, this->vli.index, this->vli.vtype, CMD_ADD_SHARED_VEHICLE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_ADD_SHARED_VEHICLE));
956 break;
957 case ADI_REMOVE_ALL: // Remove all Vehicles from the selected group
958 assert(Group::IsValidID(this->vli.index));
960 DoCommandP(0, this->vli.index, 0, CMD_REMOVE_ALL_VEHICLES_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_REMOVE_ALL_VEHICLES));
961 break;
962 default: NOT_REACHED();
964 break;
966 default: NOT_REACHED();
969 this->SetDirty();
972 void OnGameTick() override
974 if (this->groups.NeedResort() || this->vehgroups.NeedResort()) {
975 this->SetDirty();
979 void OnPlaceObjectAbort() override
981 /* abort drag & drop */
982 this->vehicle_sel = INVALID_VEHICLE;
983 this->DirtyHighlightedGroupWidget();
984 this->group_sel = INVALID_GROUP;
985 this->group_over = INVALID_GROUP;
986 this->SetWidgetDirty(WID_GL_LIST_VEHICLE);
989 void OnMouseDrag(Point pt, int widget) override
991 if (this->vehicle_sel == INVALID_VEHICLE && this->group_sel == INVALID_GROUP) return;
993 /* A vehicle is dragged over... */
994 GroupID new_group_over = INVALID_GROUP;
995 switch (widget) {
996 case WID_GL_DEFAULT_VEHICLES: // ... the 'default' group.
997 new_group_over = DEFAULT_GROUP;
998 break;
1000 case WID_GL_LIST_GROUP: { // ... the list of custom groups.
1001 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP, 0, this->tiny_step_height);
1002 new_group_over = id_g >= this->groups.size() ? NEW_GROUP : this->groups[id_g]->index;
1003 break;
1007 /* Do not highlight when dragging over the current group */
1008 if (this->vehicle_sel != INVALID_VEHICLE) {
1009 if (Vehicle::Get(vehicle_sel)->group_id == new_group_over) new_group_over = INVALID_GROUP;
1010 } else if (this->group_sel != INVALID_GROUP) {
1011 if (this->group_sel == new_group_over || Group::Get(this->group_sel)->parent == new_group_over) new_group_over = INVALID_GROUP;
1014 /* Mark widgets as dirty if the group changed. */
1015 if (new_group_over != this->group_over) {
1016 this->DirtyHighlightedGroupWidget();
1017 this->group_over = new_group_over;
1018 this->DirtyHighlightedGroupWidget();
1022 void ShowRenameGroupWindow(GroupID group, bool empty)
1024 assert(Group::IsValidID(group));
1025 this->group_rename = group;
1026 /* Show empty query for new groups */
1027 StringID str = STR_EMPTY;
1028 if (!empty) {
1029 SetDParam(0, group);
1030 str = STR_GROUP_NAME;
1032 ShowQueryString(str, STR_GROUP_RENAME_CAPTION, MAX_LENGTH_GROUP_NAME_CHARS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
1036 * Tests whether a given vehicle is selected in the window, and unselects it if necessary.
1037 * Called when the vehicle is deleted.
1038 * @param vehicle Vehicle that is going to be deleted
1040 void UnselectVehicle(VehicleID vehicle)
1042 if (this->vehicle_sel == vehicle) ResetObjectToPlace();
1046 * Selects the specified group in the list
1048 * @param g_id The ID of the group to be selected
1050 void SelectGroup(const GroupID g_id)
1052 if (g_id == INVALID_GROUP || g_id == this->vli.index) return;
1054 this->vli.index = g_id;
1055 if (g_id != ALL_GROUP && g_id != DEFAULT_GROUP) {
1056 const Group *g = Group::Get(g_id);
1057 int id_g = find_index(this->groups, g);
1058 // The group's branch is maybe collapsed, so try to expand it
1059 if (id_g == -1) {
1060 for (auto pg = Group::GetIfValid(g->parent); pg != nullptr; pg = Group::GetIfValid(pg->parent)) {
1061 pg->folded = false;
1063 this->groups.ForceRebuild();
1064 this->BuildGroupList(this->owner);
1065 this->group_sb->SetCount((uint)this->groups.size());
1066 id_g = find_index(this->groups, g);
1068 this->group_sb->ScrollTowards(id_g);
1070 this->vehgroups.ForceRebuild();
1071 this->SetDirty();
1077 static WindowDesc _other_group_desc(
1078 WDP_AUTO, "list_groups", 460, 246,
1079 WC_INVALID, WC_NONE,
1081 _nested_group_widgets, lengthof(_nested_group_widgets)
1084 static WindowDesc _train_group_desc(
1085 WDP_AUTO, "list_groups_train", 525, 246,
1086 WC_TRAINS_LIST, WC_NONE,
1088 _nested_group_widgets, lengthof(_nested_group_widgets)
1092 * Show the group window for the given company and vehicle type.
1093 * @param company The company to show the window for.
1094 * @param vehicle_type The type of vehicle to show it for.
1095 * @param group The group to be selected. Defaults to INVALID_GROUP.
1096 * @param need_existing_window Whether the existing window is needed. Defaults to false.
1098 void ShowCompanyGroup(CompanyID company, VehicleType vehicle_type, GroupID group = INVALID_GROUP, bool need_existing_window = false)
1100 if (!Company::IsValidID(company)) return;
1102 const WindowNumber num = VehicleListIdentifier(VL_GROUP_LIST, vehicle_type, company).Pack();
1103 VehicleGroupWindow *w;
1104 if (vehicle_type == VEH_TRAIN) {
1105 w = AllocateWindowDescFront<VehicleGroupWindow>(&_train_group_desc, num, need_existing_window);
1106 } else {
1107 _other_group_desc.cls = GetWindowClassForVehicleType(vehicle_type);
1108 w = AllocateWindowDescFront<VehicleGroupWindow>(&_other_group_desc, num, need_existing_window);
1110 if (w != nullptr) w->SelectGroup(group);
1114 * Show the group window for the given vehicle.
1115 * @param v The vehicle to show the window for.
1117 void ShowCompanyGroupForVehicle(const Vehicle *v)
1119 ShowCompanyGroup(v->owner, v->type, v->group_id, true);
1123 * Finds a group list window determined by vehicle type and owner
1124 * @param vt vehicle type
1125 * @param owner owner of groups
1126 * @return pointer to VehicleGroupWindow, nullptr if not found
1128 static inline VehicleGroupWindow *FindVehicleGroupWindow(VehicleType vt, Owner owner)
1130 return (VehicleGroupWindow *)FindWindowById(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_GROUP_LIST, vt, owner).Pack());
1134 * Opens a 'Rename group' window for newly created group.
1135 * @param result Did command succeed?
1136 * @param tile Unused.
1137 * @param p1 Vehicle type.
1138 * @param p2 Unused.
1139 * @param cmd Unused.
1140 * @see CmdCreateGroup
1142 void CcCreateGroup(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
1144 if (result.Failed()) return;
1145 assert(p1 <= VEH_AIRCRAFT);
1147 VehicleGroupWindow *w = FindVehicleGroupWindow((VehicleType)p1, _current_company);
1148 if (w != nullptr) w->ShowRenameGroupWindow(_new_group_id, true);
1152 * Open rename window after adding a vehicle to a new group via drag and drop.
1153 * @param result Did command succeed?
1154 * @param tile Unused.
1155 * @param p1 Unused.
1156 * @param p2 Bit 0-19: Vehicle ID.
1157 * @param cmd Unused.
1159 void CcAddVehicleNewGroup(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
1161 if (result.Failed()) return;
1162 assert(Vehicle::IsValidID(GB(p2, 0, 20)));
1164 CcCreateGroup(result, 0, Vehicle::Get(GB(p2, 0, 20))->type, 0, cmd);
1168 * Removes the highlight of a vehicle in a group window
1169 * @param *v Vehicle to remove all highlights from
1171 void DeleteGroupHighlightOfVehicle(const Vehicle *v)
1173 /* If we haven't got any vehicles on the mouse pointer, we haven't got any highlighted in any group windows either
1174 * If that is the case, we can skip looping though the windows and save time
1176 if (_special_mouse_mode != WSM_DRAGDROP) return;
1178 VehicleGroupWindow *w = FindVehicleGroupWindow(v->type, v->owner);
1179 if (w != nullptr) w->UnselectVehicle(v->index);