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/>.
8 /** @file group_gui.cpp GUI for the group window. */
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"
29 #include "widgets/group_widget.h"
31 #include "table/sprites.h"
33 #include "safeguards.h"
35 static const int LEVEL_WIDTH
= 10; ///< Indenting width of a sub-group in pixels
37 typedef GUIList
<const Group
*> GUIGroupList
;
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
),
47 NWidget(NWID_HORIZONTAL
),
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
),
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
),
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(),
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(),
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
),
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
),
105 class VehicleGroupWindow
: public BaseVehicleListWindow
{
107 /* Columns in the group list */
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.
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
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
);
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
;
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();
163 for (const Group
*g
: Group::Iterate()) {
164 if (g
->owner
== owner
&& g
->vehicle_type
== this->vli
.vtype
) {
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]) {
177 SetDParam(0, a
->index
);
178 GetString(last_name
[0], STR_GROUP_NAME
, lastof(last_name
[0]));
181 if (b
!= last_group
[1]) {
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
;
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_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
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 +
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
+ 1, right
- WD_FRAMERECT_RIGHT
, y
+ this->tiny_step_height
- WD_FRAMERECT_BOTTOM
- 1, _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;
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 */
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
;
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
;
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) {
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
;
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
);
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
);
337 this->SetWidgetDirty(WID_GL_LIST_GROUP
);
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
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
);
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
);
405 case WID_GL_ALL_VEHICLES
:
406 case WID_GL_DEFAULT_VEHICLES
:
407 size
->width
= this->ComputeGroupInfoSize();
408 size
->height
= this->tiny_step_height
;
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
);
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
;
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
);
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.
440 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
443 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
444 this->vehgroups
.ForceRebuild();
445 this->groups
.ForceRebuild();
447 this->vehgroups
.ForceResort();
448 this->groups
.ForceResort();
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
;
457 if (!(IsAllGroupID(this->vli
.index
) || IsDefaultGroupID(this->vli
.index
) || Group::IsValidID(this->vli
.index
))) {
458 this->vli
.index
= ALL_GROUP
;
459 HideDropDownMenu(this);
464 void SetStringParameters(int widget
) const override
467 case WID_GL_AVAILABLE_VEHICLES
:
468 SetDParam(0, STR_VEHICLE_LIST_AVAILABLE_TRAINS
+ this->vli
.vtype
);
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());
480 uint num_vehicle
= GetGroupNumVehicle(this->vli
.company
, this->vli
.index
, this->vli
.vtype
);
482 SetDParam(0, STR_GROUP_NAME
);
483 SetDParam(1, this->vli
.index
);
484 SetDParam(2, num_vehicle
);
485 SetDParam(3, num_vehicle
);
491 void OnPaint() override
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();
498 this->BuildGroupList(this->owner
);
500 this->group_sb
->SetCount(static_cast<int>(this->groups
.size()));
501 this->vscroll
->SetCount(static_cast<int>(this->vehgroups
.size()));
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);
509 /* Disable all lists management button when the list is empty */
510 this->SetWidgetsDisabledState(this->vehicles
.size() == 0 || _local_company
!= this->vli
.company
,
513 WID_GL_MANAGE_VEHICLES_DROPDOWN
,
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
,
521 WID_GL_REPLACE_PROTECTION
,
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
530 this->SetWidgetsDisabledState(_local_company
!= this->vli
.company
,
532 WID_GL_AVAILABLE_VEHICLES
,
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
;
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
];
543 /* Set text of "sort by" dropdown widget. */
544 this->GetWidget
<NWidgetCore
>(WID_GL_SORT_BY_DROPDOWN
)->widget_data
= this->GetVehicleSorterNames()[this->vehgroups
.SortType()];
549 void DrawWidget(const Rect
&r
, int widget
) const override
552 case WID_GL_ALL_VEHICLES
:
553 DrawGroupInfo(r
.top
, r
.left
, r
.right
, ALL_GROUP
);
556 case WID_GL_DEFAULT_VEHICLES
:
557 DrawGroupInfo(r
.top
, r
.left
, r
.right
, DEFAULT_GROUP
);
563 uint64 occupancy
= 0;
565 for (const Vehicle
* const v
: this->vehicles
) {
566 assert(v
->owner
== this->owner
);
568 this_year
+= v
->GetDisplayProfitThisYear();
569 last_year
+= v
->GetDisplayProfitLastYear();
570 occupancy
+= v
->trip_occupancy
;
573 const int left
= r
.left
+ WD_FRAMERECT_LEFT
+ 8;
574 const int right
= r
.right
- WD_FRAMERECT_RIGHT
- 8;
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
);
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
);
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
);
597 case WID_GL_LIST_GROUP
: {
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
];
603 assert(g
->owner
== this->owner
);
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
]));
607 y1
+= this->tiny_step_height
;
609 if ((uint
)this->group_sb
->GetPosition() + this->group_sb
->GetCapacity() > this->groups
.size()) {
610 DrawGroupInfo(y1
, r
.left
, r
.right
, NEW_GROUP
);
615 case WID_GL_SORT_BY_ORDER
:
616 this->DrawSortButtonState(WID_GL_SORT_BY_ORDER
, this->vehgroups
.IsDescSortOrder() ? SBS_DOWN
: SBS_UP
);
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) */
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
);
629 y
+= this->resize
.step_height
;
633 this->DrawVehicleListItems(this->vehicle_sel
, this->resize
.step_height
, r
);
638 static void DeleteGroupCallback(Window
*win
, bool 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
));
647 void OnClick(Point pt
, int widget
, int click_count
) override
650 case WID_GL_SORT_BY_ORDER
: // Flip sorting method ascending/descending
651 this->vehgroups
.ToggleSortOrder();
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);
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));
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();
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();
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;
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
))) {
691 GroupID g
= this->vli
.index
;
692 if (!IsAllGroupID(g
) && !IsDefaultGroupID(g
)) {
694 g
= Group::Get(g
)->parent
;
695 if (g
== groups
[id_g
]->index
) {
699 } while (g
!= INVALID_GROUP
);
702 Group::Get(groups
[id_g
]->index
)->folded
= !groups
[id_g
]->folded
;
703 this->groups
.ForceRebuild();
710 this->group_sel
= this->vli
.index
= this->groups
[id_g
]->index
;
712 SetObjectToPlaceWnd(SPR_CURSOR_MOUSE
, PAL_NONE
, HT_DRAG
, this);
714 this->vehgroups
.ForceRebuild();
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
723 const GUIVehicleGroup
&vehgroup
= this->vehgroups
[id_v
];
725 const Vehicle
*v
= nullptr;
727 switch (this->grouping
) {
729 const Vehicle
*v2
= vehgroup
.GetSingleVehicle();
730 if (VehicleClicked(v2
)) break;
735 case GB_SHARED_ORDERS
: {
736 assert(vehgroup
.NumVehicles() > 0);
737 v
= vehgroup
.vehicles_begin
[0];
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.
750 if (this->grouping
== GB_NONE
) {
751 this->SelectGroup(v
->group_id
);
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;
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
);
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
);
778 case WID_GL_RENAME_GROUP
: // Rename the selected roup
779 this->ShowRenameGroupWindow(this->vli
.index
, false);
782 case WID_GL_LIVERY_GROUP
: // Set group livery
783 ShowCompanyLiveryWindow(this->owner
, this->vli
.index
);
786 case WID_GL_AVAILABLE_VEHICLES
:
787 ShowBuildVehicleWindow(INVALID_TILE
, this->vli
.vtype
);
790 case WID_GL_MANAGE_VEHICLES_DROPDOWN
: {
791 ShowDropDownList(this, this->BuildActionDropdownList(true, Group::IsValidID(this->vli
.index
)), 0, WID_GL_MANAGE_VEHICLES_DROPDOWN
);
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
);
801 case WID_GL_REPLACE_PROTECTION
: {
802 const Group
*g
= Group::GetIfValid(this->vli
.index
);
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
);
811 void OnDragDrop_Group(Point pt
, int widget
)
813 const Group
*g
= Group::Get(this->group_sel
);
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
));
822 this->group_sel
= INVALID_GROUP
;
823 this->group_over
= INVALID_GROUP
;
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
;
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
));
835 this->group_sel
= INVALID_GROUP
;
836 this->group_over
= INVALID_GROUP
;
843 void OnDragDrop_Vehicle(Point pt
, int 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
));
849 this->vehicle_sel
= INVALID_VEHICLE
;
850 this->group_over
= INVALID_GROUP
;
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
;
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
;
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);
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
;
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
877 const GUIVehicleGroup
&vehgroup
= this->vehgroups
[id_v
];
878 switch (this->grouping
) {
880 const Vehicle
*v
= vehgroup
.GetSingleVehicle();
881 if (!VehicleClicked(v
) && vindex
== v
->index
) {
882 ShowVehicleViewWindow(v
);
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 */
891 if (vindex
== v
->index
) {
892 if (vehgroup
.NumVehicles() == 1) {
893 ShowVehicleViewWindow(v
);
895 ShowVehicleListWindow(v
);
909 void OnDragDrop(Point pt
, int widget
) override
911 if (this->vehicle_sel
!= INVALID_VEHICLE
) OnDragDrop_Vehicle(pt
, widget
);
912 if (this->group_sel
!= INVALID_GROUP
) OnDragDrop_Group(pt
, widget
);
914 _cursor
.vehchain
= false;
917 void OnQueryTextFinished(char *str
) override
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
;
923 void OnResize() override
925 this->group_sb
->SetCapacityFromWidget(this, WID_GL_LIST_GROUP
);
926 this->vscroll
->SetCapacityFromWidget(this, WID_GL_LIST_VEHICLE
);
929 void OnDropdownSelect(int widget
, int index
) override
932 case WID_GL_GROUP_BY_DROPDOWN
:
933 this->UpdateVehicleGroupBy(static_cast<GroupBy
>(index
));
936 case WID_GL_SORT_BY_DROPDOWN
:
937 this->vehgroups
.SetSortType(index
);
940 case WID_GL_MANAGE_VEHICLES_DROPDOWN
:
941 assert(this->vehicles
.size() != 0);
944 case ADI_REPLACE
: // Replace window
945 ShowReplaceGroupVehicleWindow(this->vli
.index
, this->vli
.vtype
);
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
));
953 case ADI_ADD_SHARED
: // Add shared Vehicles
954 assert(Group::IsValidID(this->vli
.index
));
956 DoCommandP(0, this->vli
.index
, this->vli
.vtype
, CMD_ADD_SHARED_VEHICLE_GROUP
| CMD_MSG(STR_ERROR_GROUP_CAN_T_ADD_SHARED_VEHICLE
));
958 case ADI_REMOVE_ALL
: // Remove all Vehicles from the selected group
959 assert(Group::IsValidID(this->vli
.index
));
961 DoCommandP(0, this->vli
.index
, 0, CMD_REMOVE_ALL_VEHICLES_GROUP
| CMD_MSG(STR_ERROR_GROUP_CAN_T_REMOVE_ALL_VEHICLES
));
963 default: NOT_REACHED();
967 default: NOT_REACHED();
973 void OnGameTick() override
975 if (this->groups
.NeedResort() || this->vehgroups
.NeedResort()) {
980 void OnPlaceObjectAbort() override
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
);
990 void OnMouseDrag(Point pt
, int widget
) override
992 if (this->vehicle_sel
== INVALID_VEHICLE
&& this->group_sel
== INVALID_GROUP
) return;
994 /* A vehicle is dragged over... */
995 GroupID new_group_over
= INVALID_GROUP
;
997 case WID_GL_DEFAULT_VEHICLES
: // ... the 'default' group.
998 new_group_over
= DEFAULT_GROUP
;
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
;
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
;
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();
1023 void ShowRenameGroupWindow(GroupID group
, bool empty
)
1025 assert(Group::IsValidID(group
));
1026 this->group_rename
= group
;
1027 /* Show empty query for new groups */
1028 StringID str
= STR_EMPTY
;
1030 SetDParam(0, group
);
1031 str
= STR_GROUP_NAME
;
1033 ShowQueryString(str
, STR_GROUP_RENAME_CAPTION
, MAX_LENGTH_GROUP_NAME_CHARS
, this, CS_ALPHANUMERAL
, QSF_ENABLE_DEFAULT
| QSF_LEN_IN_CHARS
);
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
1041 void UnselectVehicle(VehicleID vehicle
)
1043 if (this->vehicle_sel
== vehicle
) ResetObjectToPlace();
1047 * Selects the specified group in the list
1049 * @param g_id The ID of the group to be selected
1051 void SelectGroup(const GroupID g_id
)
1053 if (g_id
== INVALID_GROUP
|| g_id
== this->vli
.index
) return;
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
1061 for (auto pg
= Group::GetIfValid(g
->parent
); pg
!= nullptr; pg
= Group::GetIfValid(pg
->parent
)) {
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
);
1069 this->group_sb
->ScrollTowards(id_g
);
1071 this->vehgroups
.ForceRebuild();
1078 static WindowDesc
_other_group_desc(
1079 WDP_AUTO
, "list_groups", 460, 246,
1080 WC_INVALID
, WC_NONE
,
1082 _nested_group_widgets
, lengthof(_nested_group_widgets
)
1085 static WindowDesc
_train_group_desc(
1086 WDP_AUTO
, "list_groups_train", 525, 246,
1087 WC_TRAINS_LIST
, WC_NONE
,
1089 _nested_group_widgets
, lengthof(_nested_group_widgets
)
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.
1099 void ShowCompanyGroup(CompanyID company
, VehicleType vehicle_type
, GroupID group
= INVALID_GROUP
, bool need_existing_window
= false)
1101 if (!Company::IsValidID(company
)) return;
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
);
1108 _other_group_desc
.cls
= GetWindowClassForVehicleType(vehicle_type
);
1109 w
= AllocateWindowDescFront
<VehicleGroupWindow
>(&_other_group_desc
, num
, need_existing_window
);
1111 if (w
!= nullptr) w
->SelectGroup(group
);
1115 * Show the group window for the given vehicle.
1116 * @param v The vehicle to show the window for.
1118 void ShowCompanyGroupForVehicle(const Vehicle
*v
)
1120 ShowCompanyGroup(v
->owner
, v
->type
, v
->group_id
, true);
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
1129 static inline VehicleGroupWindow
*FindVehicleGroupWindow(VehicleType vt
, Owner owner
)
1131 return (VehicleGroupWindow
*)FindWindowById(GetWindowClassForVehicleType(vt
), VehicleListIdentifier(VL_GROUP_LIST
, vt
, owner
).Pack());
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.
1140 * @param cmd Unused.
1141 * @see CmdCreateGroup
1143 void CcCreateGroup(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
, uint32 cmd
)
1145 if (result
.Failed()) return;
1146 assert(p1
<= VEH_AIRCRAFT
);
1148 VehicleGroupWindow
*w
= FindVehicleGroupWindow((VehicleType
)p1
, _current_company
);
1149 if (w
!= nullptr) w
->ShowRenameGroupWindow(_new_group_id
, true);
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.
1157 * @param p2 Bit 0-19: Vehicle ID.
1158 * @param cmd Unused.
1160 void CcAddVehicleNewGroup(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
, uint32 cmd
)
1162 if (result
.Failed()) return;
1163 assert(Vehicle::IsValidID(GB(p2
, 0, 20)));
1165 CcCreateGroup(result
, 0, Vehicle::Get(GB(p2
, 0, 20))->type
, 0, cmd
);
1169 * Removes the highlight of a vehicle in a group window
1170 * @param *v Vehicle to remove all highlights from
1172 void DeleteGroupHighlightOfVehicle(const Vehicle
*v
)
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
1177 if (_special_mouse_mode
!= WSM_DRAGDROP
) return;
1179 VehicleGroupWindow
*w
= FindVehicleGroupWindow(v
->type
, v
->owner
);
1180 if (w
!= nullptr) w
->UnselectVehicle(v
->index
);