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 "dropdown_func.h"
22 #include "tilehighlight_func.h"
23 #include "vehicle_gui_base.h"
24 #include "core/geometry_func.hpp"
25 #include "core/container_func.hpp"
26 #include "company_base.h"
27 #include "company_gui.h"
29 #include "group_cmd.h"
30 #include "group_gui.h"
31 #include "vehicle_cmd.h"
34 #include "widgets/group_widget.h"
36 #include "table/sprites.h"
38 #include "safeguards.h"
40 static constexpr NWidgetPart _nested_group_widgets
[] = {
41 NWidget(NWID_HORIZONTAL
), // Window header
42 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
43 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_GL_CAPTION
),
44 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
45 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
46 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
48 NWidget(NWID_HORIZONTAL
),
50 NWidget(NWID_VERTICAL
, NC_BIGFIRST
),
51 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_GL_ALL_VEHICLES
), SetFill(1, 0), EndContainer(),
52 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_GL_DEFAULT_VEHICLES
), SetFill(1, 0), EndContainer(),
53 NWidget(NWID_HORIZONTAL
),
54 NWidget(WWT_MATRIX
, COLOUR_GREY
, WID_GL_LIST_GROUP
), SetMatrixDataTip(1, 0, STR_GROUPS_CLICK_ON_GROUP_FOR_TOOLTIP
),
55 SetFill(1, 0), SetResize(0, 1), SetScrollbar(WID_GL_LIST_GROUP_SCROLLBAR
),
56 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_GL_LIST_GROUP_SCROLLBAR
),
58 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_GL_INFO
), SetFill(1, 1), SetMinimalTextLines(3, WidgetDimensions::unscaled
.framerect
.Vertical()), EndContainer(),
59 NWidget(NWID_HORIZONTAL
),
60 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_GL_CREATE_GROUP
),
61 SetDataTip(SPR_GROUP_CREATE_TRAIN
, STR_GROUP_CREATE_TOOLTIP
),
62 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_GL_DELETE_GROUP
),
63 SetDataTip(SPR_GROUP_DELETE_TRAIN
, STR_GROUP_DELETE_TOOLTIP
),
64 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_GL_RENAME_GROUP
),
65 SetDataTip(SPR_GROUP_RENAME_TRAIN
, STR_GROUP_RENAME_TOOLTIP
),
66 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_GL_LIVERY_GROUP
),
67 SetDataTip(SPR_GROUP_LIVERY_TRAIN
, STR_GROUP_LIVERY_TOOLTIP
),
68 NWidget(WWT_PANEL
, COLOUR_GREY
), SetFill(1, 0), EndContainer(),
69 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_GL_REPLACE_PROTECTION
),
70 SetDataTip(SPR_GROUP_REPLACE_OFF_TRAIN
, STR_GROUP_REPLACE_PROTECTION_TOOLTIP
),
74 NWidget(NWID_VERTICAL
),
75 NWidget(NWID_HORIZONTAL
),
76 NWidget(NWID_VERTICAL
, NC_EQUALSIZE
),
77 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_GL_GROUP_BY_ORDER
), SetFill(1, 1), SetMinimalSize(0, 12), SetDataTip(STR_STATION_VIEW_GROUP
, STR_TOOLTIP_GROUP_ORDER
),
78 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_GL_SORT_BY_ORDER
), SetFill(1, 1), SetMinimalSize(0, 12), SetDataTip(STR_BUTTON_SORT_BY
, STR_TOOLTIP_SORT_ORDER
),
80 NWidget(NWID_VERTICAL
, NC_EQUALSIZE
),
81 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_GL_GROUP_BY_DROPDOWN
), SetFill(1, 1), SetMinimalSize(0, 12), SetDataTip(0x0, STR_TOOLTIP_GROUP_ORDER
),
82 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_GL_SORT_BY_DROPDOWN
), SetFill(1, 1), SetMinimalSize(0, 12), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA
),
84 NWidget(NWID_VERTICAL
, NC_EQUALSIZE
),
85 NWidget(WWT_PANEL
, COLOUR_GREY
), SetMinimalTextLines(1, WidgetDimensions::unscaled
.framerect
.Vertical()), SetFill(0, 1), SetResize(1, 0), EndContainer(),
86 NWidget(NWID_HORIZONTAL
),
87 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_GL_FILTER_BY_CARGO
), SetMinimalSize(0, 12), SetFill(0, 1), SetDataTip(STR_JUST_STRING
, STR_TOOLTIP_FILTER_CRITERIA
),
88 NWidget(WWT_PANEL
, COLOUR_GREY
), SetMinimalSize(0, 12), SetFill(0, 1), SetResize(1, 0), EndContainer(),
92 NWidget(NWID_HORIZONTAL
),
93 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
),
94 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_GL_LIST_VEHICLE_SCROLLBAR
),
96 NWidget(WWT_PANEL
, COLOUR_GREY
), SetMinimalSize(1, 0), SetFill(1, 1), SetResize(1, 0), EndContainer(),
97 NWidget(NWID_HORIZONTAL
),
98 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_GL_AVAILABLE_VEHICLES
), SetMinimalSize(106, 12),
99 SetDataTip(STR_JUST_STRING
, STR_VEHICLE_LIST_AVAILABLE_ENGINES_TOOLTIP
),
100 NWidget(WWT_PANEL
, COLOUR_GREY
), SetMinimalSize(0, 12), SetFill(1, 0), SetResize(1, 0), EndContainer(),
101 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_GL_MANAGE_VEHICLES_DROPDOWN
), SetMinimalSize(118, 12),
102 SetDataTip(STR_VEHICLE_LIST_MANAGE_LIST
, STR_VEHICLE_LIST_MANAGE_LIST_TOOLTIP
),
103 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_GL_STOP_ALL
), SetAspect(WidgetDimensions::ASPECT_VEHICLE_FLAG
),
104 SetDataTip(SPR_FLAG_VEH_STOPPED
, STR_VEHICLE_LIST_MASS_STOP_LIST_TOOLTIP
),
105 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_GL_START_ALL
), SetAspect(WidgetDimensions::ASPECT_VEHICLE_FLAG
),
106 SetDataTip(SPR_FLAG_VEH_RUNNING
, STR_VEHICLE_LIST_MASS_START_LIST_TOOLTIP
),
107 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
114 * Add children to GUI group list to build a hierarchical tree.
115 * @param dst Destination list.
116 * @param src Source list.
117 * @param fold Whether to handle group folding/hiding.
118 * @param parent Current tree parent (set by self with recursion).
119 * @param indent Current tree indentation level (set by self with recursion).
121 static void GuiGroupListAddChildren(GUIGroupList
&dst
, const GUIGroupList
&src
, bool fold
, GroupID parent
= INVALID_GROUP
, uint8_t indent
= 0)
123 for (const auto &item
: src
) {
124 if (item
.group
->parent
!= parent
) continue;
126 dst
.emplace_back(item
.group
, indent
);
128 if (fold
&& item
.group
->folded
) {
129 /* Test if this group has children at all. If not, the folded flag should be cleared to avoid lingering unfold buttons in the list. */
130 GroupID groupid
= item
.group
->index
;
131 bool has_children
= std::any_of(src
.begin(), src
.end(), [groupid
](const auto &child
) { return child
.group
->parent
== groupid
; });
132 Group::Get(item
.group
->index
)->folded
= has_children
;
134 GuiGroupListAddChildren(dst
, src
, fold
, item
.group
->index
, indent
+ 1);
138 if (indent
> 0 || dst
.empty()) return;
140 /* Hierarchy is complete, traverse in reverse to find where indentation levels continue. */
141 uint16_t level_mask
= 0;
142 for (auto it
= std::rbegin(dst
); std::next(it
) != std::rend(dst
); ++it
) {
143 auto next_it
= std::next(it
);
144 AssignBit(level_mask
, it
->indent
, it
->indent
<= next_it
->indent
);
145 next_it
->level_mask
= level_mask
;
150 * Build GUI group list, a sorted hierarchical list of groups for owner and vehicle type.
151 * @param dst Destination list, owned by the caller.
152 * @param fold Whether to handle group folding/hiding.
153 * @param owner Owner of groups.
154 * @param veh_type Vehicle type of groups.
156 void BuildGuiGroupList(GUIGroupList
&dst
, bool fold
, Owner owner
, VehicleType veh_type
)
160 for (const Group
*g
: Group::Iterate()) {
161 if (g
->owner
== owner
&& g
->vehicle_type
== veh_type
) {
162 list
.emplace_back(g
, 0);
168 /* Sort the groups by their name */
169 std::array
<std::pair
<const Group
*, std::string
>, 2> last_group
{};
171 list
.Sort([&last_group
](const GUIGroupListItem
&a
, const GUIGroupListItem
&b
) -> bool {
172 if (a
.group
!= last_group
[0].first
) {
173 SetDParam(0, a
.group
->index
);
174 last_group
[0] = {a
.group
, GetString(STR_GROUP_NAME
)};
177 if (b
.group
!= last_group
[1].first
) {
178 SetDParam(0, b
.group
->index
);
179 last_group
[1] = {b
.group
, GetString(STR_GROUP_NAME
)};
182 int r
= StrNaturalCompare(last_group
[0].second
, last_group
[1].second
); // Sort by name (natural sorting).
183 if (r
== 0) return a
.group
->number
< b
.group
->number
;
187 GuiGroupListAddChildren(dst
, list
, fold
, INVALID_GROUP
, 0);
190 class VehicleGroupWindow
: public BaseVehicleListWindow
{
192 /* Columns in the group list */
194 VGC_FOLD
, ///< Fold / Unfold button.
195 VGC_NAME
, ///< Group name.
196 VGC_PROTECT
, ///< Autoreplace protect icon.
197 VGC_AUTOREPLACE
, ///< Autoreplace active icon.
198 VGC_PROFIT
, ///< Profit icon.
199 VGC_NUMBER
, ///< Number of vehicles in the group.
204 GroupID group_sel
; ///< Selected group (for drag/drop)
205 GroupID group_rename
; ///< Group being renamed, INVALID_GROUP if none
206 GroupID group_over
; ///< Group over which a vehicle is dragged, INVALID_GROUP if none
207 GroupID group_confirm
; ///< Group awaiting delete confirmation
208 GUIGroupList groups
; ///< List of groups
209 uint tiny_step_height
; ///< Step height for the group list
212 Dimension column_size
[VGC_END
]; ///< Size of the columns in the group list.
215 * (Re)Build the group list.
217 * @param owner The owner of the window
219 void BuildGroupList(Owner owner
)
221 if (!this->groups
.NeedRebuild()) return;
223 this->groups
.clear();
225 BuildGuiGroupList(this->groups
, true, owner
, this->vli
.vtype
);
227 this->groups
.RebuildDone();
231 * Compute tiny_step_height and column_size
232 * @return Total width required for the group list.
234 uint
ComputeGroupInfoSize()
236 this->column_size
[VGC_FOLD
] = maxdim(GetSpriteSize(SPR_CIRCLE_FOLDED
), GetSpriteSize(SPR_CIRCLE_UNFOLDED
));
237 this->tiny_step_height
= this->column_size
[VGC_FOLD
].height
;
239 this->column_size
[VGC_NAME
] = maxdim(GetStringBoundingBox(STR_GROUP_DEFAULT_TRAINS
+ this->vli
.vtype
), GetStringBoundingBox(STR_GROUP_ALL_TRAINS
+ this->vli
.vtype
));
240 this->column_size
[VGC_NAME
].width
= std::max(170u, this->column_size
[VGC_NAME
].width
) + WidgetDimensions::scaled
.hsep_indent
;
241 this->tiny_step_height
= std::max(this->tiny_step_height
, this->column_size
[VGC_NAME
].height
);
243 this->column_size
[VGC_PROTECT
] = GetSpriteSize(SPR_GROUP_REPLACE_PROTECT
);
244 this->tiny_step_height
= std::max(this->tiny_step_height
, this->column_size
[VGC_PROTECT
].height
);
246 this->column_size
[VGC_AUTOREPLACE
] = GetSpriteSize(SPR_GROUP_REPLACE_ACTIVE
);
247 this->tiny_step_height
= std::max(this->tiny_step_height
, this->column_size
[VGC_AUTOREPLACE
].height
);
249 this->column_size
[VGC_PROFIT
].width
= 0;
250 this->column_size
[VGC_PROFIT
].height
= 0;
251 static const SpriteID profit_sprites
[] = {SPR_PROFIT_NA
, SPR_PROFIT_NEGATIVE
, SPR_PROFIT_SOME
, SPR_PROFIT_LOT
};
252 for (const auto &profit_sprite
: profit_sprites
) {
253 Dimension d
= GetSpriteSize(profit_sprite
);
254 this->column_size
[VGC_PROFIT
] = maxdim(this->column_size
[VGC_PROFIT
], d
);
256 this->tiny_step_height
= std::max(this->tiny_step_height
, this->column_size
[VGC_PROFIT
].height
);
258 int num_vehicle
= GetGroupNumVehicle(this->vli
.company
, ALL_GROUP
, this->vli
.vtype
);
259 SetDParamMaxValue(0, num_vehicle
, 3, FS_SMALL
);
260 SetDParamMaxValue(1, num_vehicle
, 3, FS_SMALL
);
261 this->column_size
[VGC_NUMBER
] = GetStringBoundingBox(STR_GROUP_COUNT_WITH_SUBGROUP
);
262 this->tiny_step_height
= std::max(this->tiny_step_height
, this->column_size
[VGC_NUMBER
].height
);
264 this->tiny_step_height
+= WidgetDimensions::scaled
.framerect
.Vertical();
266 return WidgetDimensions::scaled
.framerect
.left
+
267 this->column_size
[VGC_FOLD
].width
+ WidgetDimensions::scaled
.hsep_normal
+
268 this->column_size
[VGC_NAME
].width
+ WidgetDimensions::scaled
.hsep_wide
+
269 this->column_size
[VGC_PROTECT
].width
+ WidgetDimensions::scaled
.hsep_normal
+
270 this->column_size
[VGC_AUTOREPLACE
].width
+ WidgetDimensions::scaled
.hsep_normal
+
271 this->column_size
[VGC_PROFIT
].width
+ WidgetDimensions::scaled
.hsep_normal
+
272 this->column_size
[VGC_NUMBER
].width
+
273 WidgetDimensions::scaled
.framerect
.right
;
277 * Draw a row in the group list.
278 * @param y Top of the row.
279 * @param left Left of the row.
280 * @param right Right of the row.
281 * @param g_id Group to list.
282 * @param indent Indentation level.
283 * @param protection Whether autoreplace protection is set.
284 * @param has_children Whether the group has children and should have a fold / unfold button.
286 void DrawGroupInfo(int y
, int left
, int right
, GroupID g_id
, uint16_t level_mask
= 0, uint8_t indent
= 0, bool protection
= false, bool has_children
= false) const
288 /* Highlight the group if a vehicle is dragged over it */
289 if (g_id
== this->group_over
) {
290 GfxFillRect(left
+ WidgetDimensions::scaled
.bevel
.left
, y
+ WidgetDimensions::scaled
.framerect
.top
, right
- WidgetDimensions::scaled
.bevel
.right
, y
+ this->tiny_step_height
- 1 - WidgetDimensions::scaled
.framerect
.bottom
, GetColourGradient(COLOUR_GREY
, SHADE_LIGHTEST
));
293 if (g_id
== NEW_GROUP
) return;
295 /* draw the selected group in white, else we draw it in black */
296 TextColour colour
= g_id
== this->vli
.index
? TC_WHITE
: TC_BLACK
;
297 const GroupStatistics
&stats
= GroupStatistics::Get(this->vli
.company
, g_id
, this->vli
.vtype
);
298 bool rtl
= _current_text_dir
== TD_RTL
;
300 const int offset
= (rtl
? -(int)this->column_size
[VGC_FOLD
].width
: (int)this->column_size
[VGC_FOLD
].width
) / 2;
301 const int level_width
= rtl
? -WidgetDimensions::scaled
.hsep_indent
: WidgetDimensions::scaled
.hsep_indent
;
302 const int linecolour
= GetColourGradient(COLOUR_ORANGE
, SHADE_NORMAL
);
305 /* Draw tree continuation lines. */
306 int tx
= (rtl
? right
- WidgetDimensions::scaled
.framerect
.right
: left
+ WidgetDimensions::scaled
.framerect
.left
) + offset
;
307 for (uint lvl
= 1; lvl
<= indent
; ++lvl
) {
308 if (HasBit(level_mask
, lvl
)) GfxDrawLine(tx
, y
, tx
, y
+ this->tiny_step_height
- 1, linecolour
, WidgetDimensions::scaled
.fullbevel
.top
);
309 if (lvl
< indent
) tx
+= level_width
;
311 /* Draw our node in the tree. */
312 int ycentre
= y
+ this->tiny_step_height
/ 2 - 1;
313 if (!HasBit(level_mask
, indent
)) GfxDrawLine(tx
, y
, tx
, ycentre
, linecolour
, WidgetDimensions::scaled
.fullbevel
.top
);
314 GfxDrawLine(tx
, ycentre
, tx
+ offset
- (rtl
? -1 : 1), ycentre
, linecolour
, WidgetDimensions::scaled
.fullbevel
.top
);
317 /* draw fold / unfold button */
318 int x
= rtl
? right
- WidgetDimensions::scaled
.framerect
.right
- this->column_size
[VGC_FOLD
].width
+ 1 : left
+ WidgetDimensions::scaled
.framerect
.left
;
320 DrawSprite(Group::Get(g_id
)->folded
? SPR_CIRCLE_FOLDED
: SPR_CIRCLE_UNFOLDED
, PAL_NONE
, x
+ indent
* level_width
, y
+ (this->tiny_step_height
- this->column_size
[VGC_FOLD
].height
) / 2);
323 /* draw group name */
325 if (IsAllGroupID(g_id
)) {
326 str
= STR_GROUP_ALL_TRAINS
+ this->vli
.vtype
;
327 } else if (IsDefaultGroupID(g_id
)) {
328 str
= STR_GROUP_DEFAULT_TRAINS
+ this->vli
.vtype
;
331 str
= STR_GROUP_NAME
;
333 x
= rtl
? x
- WidgetDimensions::scaled
.hsep_normal
- this->column_size
[VGC_NAME
].width
: x
+ WidgetDimensions::scaled
.hsep_normal
+ this->column_size
[VGC_FOLD
].width
;
334 DrawString(x
+ (rtl
? 0 : indent
* WidgetDimensions::scaled
.hsep_indent
), x
+ this->column_size
[VGC_NAME
].width
- 1 - (rtl
? indent
* WidgetDimensions::scaled
.hsep_indent
: 0), y
+ (this->tiny_step_height
- this->column_size
[VGC_NAME
].height
) / 2, str
, colour
);
336 /* draw autoreplace protection */
337 x
= rtl
? x
- WidgetDimensions::scaled
.hsep_wide
- this->column_size
[VGC_PROTECT
].width
: x
+ WidgetDimensions::scaled
.hsep_wide
+ this->column_size
[VGC_NAME
].width
;
338 if (protection
) DrawSprite(SPR_GROUP_REPLACE_PROTECT
, PAL_NONE
, x
, y
+ (this->tiny_step_height
- this->column_size
[VGC_PROTECT
].height
) / 2);
340 /* draw autoreplace status */
341 x
= rtl
? x
- WidgetDimensions::scaled
.hsep_normal
- this->column_size
[VGC_AUTOREPLACE
].width
: x
+ WidgetDimensions::scaled
.hsep_normal
+ this->column_size
[VGC_PROTECT
].width
;
342 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);
344 /* draw the profit icon */
345 x
= rtl
? x
- WidgetDimensions::scaled
.hsep_normal
- this->column_size
[VGC_PROFIT
].width
: x
+ WidgetDimensions::scaled
.hsep_normal
+ this->column_size
[VGC_AUTOREPLACE
].width
;
347 uint num_vehicle_min_age
= GetGroupNumVehicleMinAge(this->vli
.company
, g_id
, this->vli
.vtype
);
348 Money profit_last_year_min_age
= GetGroupProfitLastYearMinAge(this->vli
.company
, g_id
, this->vli
.vtype
);
349 if (num_vehicle_min_age
== 0) {
351 } else if (profit_last_year_min_age
< 0) {
352 spr
= SPR_PROFIT_NEGATIVE
;
353 } else if (profit_last_year_min_age
< VEHICLE_PROFIT_THRESHOLD
* num_vehicle_min_age
) {
354 spr
= SPR_PROFIT_SOME
;
356 spr
= SPR_PROFIT_LOT
;
358 DrawSprite(spr
, PAL_NONE
, x
, y
+ (this->tiny_step_height
- this->column_size
[VGC_PROFIT
].height
) / 2);
360 /* draw the number of vehicles of the group */
361 x
= rtl
? x
- WidgetDimensions::scaled
.hsep_normal
- this->column_size
[VGC_NUMBER
].width
: x
+ WidgetDimensions::scaled
.hsep_normal
+ this->column_size
[VGC_PROFIT
].width
;
362 int num_vehicle_with_subgroups
= GetGroupNumVehicle(this->vli
.company
, g_id
, this->vli
.vtype
);
363 int num_vehicle
= GroupStatistics::Get(this->vli
.company
, g_id
, this->vli
.vtype
).num_vehicle
;
364 if (IsAllGroupID(g_id
) || IsDefaultGroupID(g_id
) || num_vehicle_with_subgroups
== num_vehicle
) {
365 SetDParam(0, num_vehicle
);
366 DrawString(x
, x
+ this->column_size
[VGC_NUMBER
].width
- 1, y
+ (this->tiny_step_height
- this->column_size
[VGC_NUMBER
].height
) / 2, STR_JUST_COMMA
, colour
, SA_RIGHT
| SA_FORCE
, false, FS_SMALL
);
368 SetDParam(0, num_vehicle
);
369 SetDParam(1, num_vehicle_with_subgroups
- num_vehicle
);
370 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
);
375 * Mark the widget containing the currently highlighted group as dirty.
377 void DirtyHighlightedGroupWidget()
379 if (this->group_over
== INVALID_GROUP
) return;
381 if (IsAllGroupID(this->group_over
)) {
382 this->SetWidgetDirty(WID_GL_ALL_VEHICLES
);
383 } else if (IsDefaultGroupID(this->group_over
)) {
384 this->SetWidgetDirty(WID_GL_DEFAULT_VEHICLES
);
386 this->SetWidgetDirty(WID_GL_LIST_GROUP
);
391 VehicleGroupWindow(WindowDesc
&desc
, WindowNumber window_number
) : BaseVehicleListWindow(desc
, window_number
)
393 this->CreateNestedTree();
395 this->vscroll
= this->GetScrollbar(WID_GL_LIST_VEHICLE_SCROLLBAR
);
396 this->group_sb
= this->GetScrollbar(WID_GL_LIST_GROUP_SCROLLBAR
);
398 this->vli
.index
= ALL_GROUP
;
399 this->group_sel
= INVALID_GROUP
;
400 this->group_rename
= INVALID_GROUP
;
401 this->group_over
= INVALID_GROUP
;
403 this->groups
.ForceRebuild();
404 this->groups
.NeedResort();
405 this->BuildGroupList(vli
.company
);
406 this->group_sb
->SetCount(this->groups
.size());
408 this->GetWidget
<NWidgetCore
>(WID_GL_CAPTION
)->widget_data
= STR_VEHICLE_LIST_TRAIN_CAPTION
+ this->vli
.vtype
;
409 this->GetWidget
<NWidgetCore
>(WID_GL_LIST_VEHICLE
)->tool_tip
= STR_VEHICLE_LIST_TRAIN_LIST_TOOLTIP
+ this->vli
.vtype
;
411 this->GetWidget
<NWidgetCore
>(WID_GL_CREATE_GROUP
)->widget_data
+= this->vli
.vtype
;
412 this->GetWidget
<NWidgetCore
>(WID_GL_RENAME_GROUP
)->widget_data
+= this->vli
.vtype
;
413 this->GetWidget
<NWidgetCore
>(WID_GL_DELETE_GROUP
)->widget_data
+= this->vli
.vtype
;
414 this->GetWidget
<NWidgetCore
>(WID_GL_LIVERY_GROUP
)->widget_data
+= this->vli
.vtype
;
415 this->GetWidget
<NWidgetCore
>(WID_GL_REPLACE_PROTECTION
)->widget_data
+= this->vli
.vtype
;
417 this->FinishInitNested(window_number
);
418 this->owner
= vli
.company
;
420 this->BuildVehicleList();
421 this->SortVehicleList();
424 ~VehicleGroupWindow()
426 *this->sorting
= this->vehgroups
.GetListing();
429 void UpdateWidgetSize(WidgetID widget
, Dimension
&size
, [[maybe_unused
]] const Dimension
&padding
, [[maybe_unused
]] Dimension
&fill
, [[maybe_unused
]] Dimension
&resize
) override
432 case WID_GL_LIST_GROUP
:
433 size
.width
= this->ComputeGroupInfoSize();
434 resize
.height
= this->tiny_step_height
;
435 fill
.height
= this->tiny_step_height
;
438 case WID_GL_ALL_VEHICLES
:
439 case WID_GL_DEFAULT_VEHICLES
:
440 size
.width
= this->ComputeGroupInfoSize();
441 size
.height
= this->tiny_step_height
;
444 case WID_GL_SORT_BY_ORDER
: {
445 Dimension d
= GetStringBoundingBox(this->GetWidget
<NWidgetCore
>(widget
)->widget_data
);
446 d
.width
+= padding
.width
+ Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
447 d
.height
+= padding
.height
;
448 size
= maxdim(size
, d
);
452 case WID_GL_LIST_VEHICLE
:
453 this->ComputeGroupInfoSize();
454 resize
.height
= GetVehicleListHeight(this->vli
.vtype
, this->tiny_step_height
);
455 size
.height
= 4 * resize
.height
;
458 case WID_GL_GROUP_BY_DROPDOWN
:
459 size
.width
= GetStringListWidth(this->vehicle_group_by_names
) + padding
.width
;
462 case WID_GL_SORT_BY_DROPDOWN
:
463 size
.width
= GetStringListWidth(this->vehicle_group_none_sorter_names_calendar
);
464 size
.width
= std::max(size
.width
, GetStringListWidth(this->vehicle_group_none_sorter_names_wallclock
));
465 size
.width
= std::max(size
.width
, GetStringListWidth(this->vehicle_group_shared_orders_sorter_names_calendar
));
466 size
.width
= std::max(size
.width
, GetStringListWidth(this->vehicle_group_shared_orders_sorter_names_wallclock
));
467 size
.width
+= padding
.width
;
470 case WID_GL_FILTER_BY_CARGO
:
471 size
.width
= std::max(size
.width
, GetDropDownListDimension(this->BuildCargoDropDownList(true)).width
+ padding
.width
);
474 case WID_GL_MANAGE_VEHICLES_DROPDOWN
: {
475 Dimension d
= this->GetActionDropdownSize(true, true, true);
476 d
.height
+= padding
.height
;
477 d
.width
+= padding
.width
;
478 size
= maxdim(size
, d
);
485 * Some data on this window has become invalid.
486 * @param data Information about the changed data.
487 * @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.
489 void OnInvalidateData([[maybe_unused
]] int data
= 0, [[maybe_unused
]] bool gui_scope
= true) override
492 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
493 this->vehgroups
.ForceRebuild();
494 this->groups
.ForceRebuild();
496 this->vehgroups
.ForceResort();
497 this->groups
.ForceResort();
500 /* Process ID-invalidation in command-scope as well */
501 if (this->group_rename
!= INVALID_GROUP
&& !Group::IsValidID(this->group_rename
)) {
502 CloseWindowByClass(WC_QUERY_STRING
);
503 this->group_rename
= INVALID_GROUP
;
506 if (!(IsAllGroupID(this->vli
.index
) || IsDefaultGroupID(this->vli
.index
) || Group::IsValidID(this->vli
.index
))) {
507 this->vli
.index
= ALL_GROUP
;
508 this->CloseChildWindows(WC_DROPDOWN_MENU
);
513 void SetStringParameters(WidgetID widget
) const override
516 case WID_GL_FILTER_BY_CARGO
:
517 SetDParam(0, this->GetCargoFilterLabel(this->cargo_filter_criteria
));
520 case WID_GL_AVAILABLE_VEHICLES
:
521 SetDParam(0, STR_VEHICLE_LIST_AVAILABLE_TRAINS
+ this->vli
.vtype
);
525 /* If selected_group == DEFAULT_GROUP || ALL_GROUP, draw the standard caption
526 * We list all vehicles or ungrouped vehicles */
527 if (IsDefaultGroupID(this->vli
.index
) || IsAllGroupID(this->vli
.index
)) {
528 SetDParam(0, STR_COMPANY_NAME
);
529 SetDParam(1, this->vli
.company
);
530 SetDParam(2, this->vehicles
.size());
531 SetDParam(3, this->vehicles
.size());
533 uint num_vehicle
= GetGroupNumVehicle(this->vli
.company
, this->vli
.index
, this->vli
.vtype
);
535 SetDParam(0, STR_GROUP_NAME
);
536 SetDParam(1, this->vli
.index
);
537 SetDParam(2, num_vehicle
);
538 SetDParam(3, num_vehicle
);
544 void OnPaint() override
546 /* If we select the all vehicles, this->list will contain all vehicles of the owner
547 * else this->list will contain all vehicles which belong to the selected group */
548 this->BuildVehicleList();
549 this->SortVehicleList();
551 this->BuildGroupList(this->owner
);
553 this->group_sb
->SetCount(this->groups
.size());
554 this->vscroll
->SetCount(this->vehgroups
.size());
556 /* The drop down menu is out, *but* it may not be used, retract it. */
557 if (this->vehicles
.empty() && this->IsWidgetLowered(WID_GL_MANAGE_VEHICLES_DROPDOWN
)) {
558 this->RaiseWidget(WID_GL_MANAGE_VEHICLES_DROPDOWN
);
559 this->CloseChildWindows(WC_DROPDOWN_MENU
);
562 /* Disable all lists management button when the list is empty */
563 this->SetWidgetsDisabledState(this->vehicles
.empty() || _local_company
!= this->vli
.company
,
566 WID_GL_MANAGE_VEHICLES_DROPDOWN
);
568 /* Disable the group specific function when we select the default group or all vehicles */
569 this->SetWidgetsDisabledState(IsDefaultGroupID(this->vli
.index
) || IsAllGroupID(this->vli
.index
) || _local_company
!= this->vli
.company
,
573 WID_GL_REPLACE_PROTECTION
);
575 /* Disable remaining buttons for non-local companies
576 * Needed while changing _local_company, eg. by cheats
577 * All procedures (eg. move vehicle to another group)
578 * verify, whether you are the owner of the vehicle,
579 * so it doesn't have to be disabled
581 this->SetWidgetsDisabledState(_local_company
!= this->vli
.company
,
583 WID_GL_AVAILABLE_VEHICLES
);
585 /* If not a default group and the group has replace protection, show an enabled replace sprite. */
586 uint16_t protect_sprite
= SPR_GROUP_REPLACE_OFF_TRAIN
;
587 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
;
588 this->GetWidget
<NWidgetCore
>(WID_GL_REPLACE_PROTECTION
)->widget_data
= protect_sprite
+ this->vli
.vtype
;
590 /* Set text of "group by" dropdown widget. */
591 this->GetWidget
<NWidgetCore
>(WID_GL_GROUP_BY_DROPDOWN
)->widget_data
= std::data(this->vehicle_group_by_names
)[this->grouping
];
593 /* Set text of "sort by" dropdown widget. */
594 this->GetWidget
<NWidgetCore
>(WID_GL_SORT_BY_DROPDOWN
)->widget_data
= this->GetVehicleSorterNames()[this->vehgroups
.SortType()];
599 void DrawWidget(const Rect
&r
, WidgetID widget
) const override
602 case WID_GL_ALL_VEHICLES
:
603 DrawGroupInfo(r
.top
, r
.left
, r
.right
, ALL_GROUP
);
606 case WID_GL_DEFAULT_VEHICLES
:
607 DrawGroupInfo(r
.top
, r
.left
, r
.right
, DEFAULT_GROUP
);
613 uint64_t occupancy
= 0;
615 for (const Vehicle
* const v
: this->vehicles
) {
616 assert(v
->owner
== this->owner
);
618 this_year
+= v
->GetDisplayProfitThisYear();
619 last_year
+= v
->GetDisplayProfitLastYear();
620 occupancy
+= v
->trip_occupancy
;
623 Rect tr
= r
.Shrink(WidgetDimensions::scaled
.framerect
);
625 DrawString(tr
, TimerGameEconomy::UsingWallclockUnits() ? STR_GROUP_PROFIT_THIS_PERIOD
: STR_GROUP_PROFIT_THIS_YEAR
, TC_BLACK
);
626 SetDParam(0, this_year
);
627 DrawString(tr
, STR_JUST_CURRENCY_LONG
, TC_BLACK
, SA_RIGHT
);
629 tr
.top
+= GetCharacterHeight(FS_NORMAL
);
630 DrawString(tr
, TimerGameEconomy::UsingWallclockUnits() ? STR_GROUP_PROFIT_LAST_PERIOD
: STR_GROUP_PROFIT_LAST_YEAR
, TC_BLACK
);
631 SetDParam(0, last_year
);
632 DrawString(tr
, STR_JUST_CURRENCY_LONG
, TC_BLACK
, SA_RIGHT
);
634 tr
.top
+= GetCharacterHeight(FS_NORMAL
);
635 DrawString(tr
, STR_GROUP_OCCUPANCY
, TC_BLACK
);
636 const size_t vehicle_count
= this->vehicles
.size();
637 if (vehicle_count
> 0) {
638 SetDParam(0, occupancy
/ vehicle_count
);
639 DrawString(tr
, STR_GROUP_OCCUPANCY_VALUE
, TC_BLACK
, SA_RIGHT
);
645 case WID_GL_LIST_GROUP
: {
647 auto [first
, last
] = this->group_sb
->GetVisibleRangeIterators(this->groups
);
648 for (auto it
= first
; it
!= last
; ++it
) {
649 const Group
*g
= it
->group
;
651 assert(g
->owner
== this->owner
);
653 DrawGroupInfo(y1
, r
.left
, r
.right
, g
->index
, it
->level_mask
, it
->indent
, HasBit(g
->flags
, GroupFlags::GF_REPLACE_PROTECTION
), g
->folded
|| (std::next(it
) != std::end(this->groups
) && std::next(it
)->indent
> it
->indent
));
655 y1
+= this->tiny_step_height
;
657 if ((uint
)this->group_sb
->GetPosition() + this->group_sb
->GetCapacity() > this->groups
.size()) {
658 DrawGroupInfo(y1
, r
.left
, r
.right
, NEW_GROUP
);
663 case WID_GL_SORT_BY_ORDER
:
664 this->DrawSortButtonState(WID_GL_SORT_BY_ORDER
, this->vehgroups
.IsDescSortOrder() ? SBS_DOWN
: SBS_UP
);
667 case WID_GL_LIST_VEHICLE
:
668 if (this->vli
.index
!= ALL_GROUP
&& this->grouping
== GB_NONE
) {
669 /* Mark vehicles which are in sub-groups (only if we are not using shared order coalescing) */
670 Rect mr
= r
.WithHeight(this->resize
.step_height
);
671 auto [first
, last
] = this->vscroll
->GetVisibleRangeIterators(this->vehgroups
);
672 for (auto it
= first
; it
!= last
; ++it
) {
673 const Vehicle
*v
= it
->GetSingleVehicle();
674 if (v
->group_id
!= this->vli
.index
) {
675 GfxFillRect(mr
.Shrink(WidgetDimensions::scaled
.bevel
), GetColourGradient(COLOUR_GREY
, SHADE_DARK
), FILLRECT_CHECKER
);
677 mr
= mr
.Translate(0, this->resize
.step_height
);
681 this->DrawVehicleListItems(this->vehicle_sel
, this->resize
.step_height
, r
);
686 static void DeleteGroupCallback(Window
*win
, bool confirmed
)
689 VehicleGroupWindow
*w
= (VehicleGroupWindow
*)win
;
690 w
->vli
.index
= ALL_GROUP
;
691 Command
<CMD_DELETE_GROUP
>::Post(STR_ERROR_GROUP_CAN_T_DELETE
, w
->group_confirm
);
695 bool last_overlay_state
;
696 void OnMouseLoop() override
698 if (last_overlay_state
!= ShowCargoIconOverlay()) {
699 last_overlay_state
= ShowCargoIconOverlay();
704 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
707 case WID_GL_SORT_BY_ORDER
: // Flip sorting method ascending/descending
708 this->vehgroups
.ToggleSortOrder();
712 case WID_GL_GROUP_BY_DROPDOWN
: // Select grouping option dropdown menu
713 ShowDropDownMenu(this, this->vehicle_group_by_names
, this->grouping
, WID_GL_GROUP_BY_DROPDOWN
, 0, 0);
716 case WID_GL_SORT_BY_DROPDOWN
: // Select sorting criteria dropdown menu
717 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));
720 case WID_GL_FILTER_BY_CARGO
: // Select filtering criteria dropdown menu
721 ShowDropDownList(this, this->BuildCargoDropDownList(false), this->cargo_filter_criteria
, widget
);
724 case WID_GL_ALL_VEHICLES
: // All vehicles button
725 if (!IsAllGroupID(this->vli
.index
)) {
726 this->vli
.index
= ALL_GROUP
;
727 this->vehgroups
.ForceRebuild();
732 case WID_GL_DEFAULT_VEHICLES
: // Ungrouped vehicles button
733 if (!IsDefaultGroupID(this->vli
.index
)) {
734 this->vli
.index
= DEFAULT_GROUP
;
735 this->vehgroups
.ForceRebuild();
740 case WID_GL_LIST_GROUP
: { // Matrix Group
741 auto it
= this->group_sb
->GetScrolledItemFromWidget(this->groups
, pt
.y
, this, WID_GL_LIST_GROUP
);
742 if (it
== this->groups
.end()) return;
744 if (it
->group
->folded
|| (std::next(it
) != std::end(this->groups
) && std::next(it
)->indent
> it
->indent
)) {
745 /* The group has children, check if the user clicked the fold / unfold button. */
746 NWidgetCore
*group_display
= this->GetWidget
<NWidgetCore
>(widget
);
747 int x
= _current_text_dir
== TD_RTL
?
748 group_display
->pos_x
+ group_display
->current_x
- WidgetDimensions::scaled
.framerect
.right
- it
->indent
* WidgetDimensions::scaled
.hsep_indent
- this->column_size
[VGC_FOLD
].width
:
749 group_display
->pos_x
+ WidgetDimensions::scaled
.framerect
.left
+ it
->indent
* WidgetDimensions::scaled
.hsep_indent
;
750 if (click_count
> 1 || (pt
.x
>= x
&& pt
.x
< (int)(x
+ this->column_size
[VGC_FOLD
].width
))) {
752 GroupID g
= this->vli
.index
;
753 if (!IsAllGroupID(g
) && !IsDefaultGroupID(g
)) {
755 g
= Group::Get(g
)->parent
;
756 if (g
== it
->group
->index
) {
760 } while (g
!= INVALID_GROUP
);
763 Group::Get(it
->group
->index
)->folded
= !it
->group
->folded
;
764 this->groups
.ForceRebuild();
771 this->group_sel
= this->vli
.index
= it
->group
->index
;
773 SetObjectToPlaceWnd(SPR_CURSOR_MOUSE
, PAL_NONE
, HT_DRAG
, this);
775 this->vehgroups
.ForceRebuild();
780 case WID_GL_LIST_VEHICLE
: { // Matrix Vehicle
781 auto it
= this->vscroll
->GetScrolledItemFromWidget(this->vehgroups
, pt
.y
, this, WID_GL_LIST_VEHICLE
);
782 if (it
== this->vehgroups
.end()) return; // click out of list bound
784 const GUIVehicleGroup
&vehgroup
= *it
;
786 const Vehicle
*v
= nullptr;
788 switch (this->grouping
) {
790 const Vehicle
*v2
= vehgroup
.GetSingleVehicle();
791 if (VehicleClicked(v2
)) break;
796 case GB_SHARED_ORDERS
: {
797 assert(vehgroup
.NumVehicles() > 0);
798 v
= vehgroup
.vehicles_begin
[0];
800 * No VehicleClicked(v) support for now, because don't want
801 * to enable any contextual actions except perhaps clicking/ctrl-clicking to clone orders.
810 if (_ctrl_pressed
&& this->grouping
== GB_SHARED_ORDERS
) {
813 this->vehicle_sel
= v
->index
;
815 if (_ctrl_pressed
&& this->grouping
== GB_NONE
) {
817 * It only makes sense to select a group if not using shared orders
818 * since two vehicles sharing orders can be from different groups.
820 this->SelectGroup(v
->group_id
);
823 SetObjectToPlaceWnd(SPR_CURSOR_MOUSE
, PAL_NONE
, HT_DRAG
, this);
824 SetMouseCursorVehicle(v
, EIT_IN_LIST
);
825 _cursor
.vehchain
= true;
834 case WID_GL_CREATE_GROUP
: { // Create a new group
835 Command
<CMD_CREATE_GROUP
>::Post(STR_ERROR_GROUP_CAN_T_CREATE
, CcCreateGroup
, this->vli
.vtype
, this->vli
.index
);
839 case WID_GL_DELETE_GROUP
: { // Delete the selected group
840 this->group_confirm
= this->vli
.index
;
841 ShowQuery(STR_QUERY_GROUP_DELETE_CAPTION
, STR_GROUP_DELETE_QUERY_TEXT
, this, DeleteGroupCallback
);
845 case WID_GL_RENAME_GROUP
: // Rename the selected roup
846 this->ShowRenameGroupWindow(this->vli
.index
, false);
849 case WID_GL_LIVERY_GROUP
: // Set group livery
850 ShowCompanyLiveryWindow(this->owner
, this->vli
.index
);
853 case WID_GL_AVAILABLE_VEHICLES
:
854 ShowBuildVehicleWindow(INVALID_TILE
, this->vli
.vtype
);
857 case WID_GL_MANAGE_VEHICLES_DROPDOWN
: {
858 ShowDropDownList(this, this->BuildActionDropdownList(true, Group::IsValidID(this->vli
.index
), IsDefaultGroupID(this->vli
.index
)), -1, WID_GL_MANAGE_VEHICLES_DROPDOWN
);
862 case WID_GL_START_ALL
:
863 case WID_GL_STOP_ALL
: { // Start/stop all vehicles of the list
864 Command
<CMD_MASS_START_STOP
>::Post(0, widget
== WID_GL_START_ALL
, true, this->vli
);
868 case WID_GL_REPLACE_PROTECTION
: {
869 const Group
*g
= Group::GetIfValid(this->vli
.index
);
871 Command
<CMD_SET_GROUP_FLAG
>::Post(this->vli
.index
, GroupFlags::GF_REPLACE_PROTECTION
, !HasBit(g
->flags
, GroupFlags::GF_REPLACE_PROTECTION
), _ctrl_pressed
);
878 void OnDragDrop_Group(Point pt
, WidgetID widget
)
880 const Group
*g
= Group::Get(this->group_sel
);
883 case WID_GL_ALL_VEHICLES
: // All vehicles
884 case WID_GL_DEFAULT_VEHICLES
: // Ungrouped vehicles
885 if (g
->parent
!= INVALID_GROUP
) {
886 Command
<CMD_ALTER_GROUP
>::Post(STR_ERROR_GROUP_CAN_T_SET_PARENT
, AlterGroupMode::SetParent
, this->group_sel
, INVALID_GROUP
, {});
889 this->group_sel
= INVALID_GROUP
;
890 this->group_over
= INVALID_GROUP
;
894 case WID_GL_LIST_GROUP
: { // Matrix group
895 auto it
= this->group_sb
->GetScrolledItemFromWidget(this->groups
, pt
.y
, this, WID_GL_LIST_GROUP
);
896 GroupID new_g
= it
== this->groups
.end() ? INVALID_GROUP
: it
->group
->index
;
898 if (this->group_sel
!= new_g
&& g
->parent
!= new_g
) {
899 Command
<CMD_ALTER_GROUP
>::Post(STR_ERROR_GROUP_CAN_T_SET_PARENT
, AlterGroupMode::SetParent
, this->group_sel
, new_g
, {});
902 this->group_sel
= INVALID_GROUP
;
903 this->group_over
= INVALID_GROUP
;
910 void OnDragDrop_Vehicle(Point pt
, WidgetID widget
)
913 case WID_GL_DEFAULT_VEHICLES
: // Ungrouped vehicles
914 Command
<CMD_ADD_VEHICLE_GROUP
>::Post(STR_ERROR_GROUP_CAN_T_ADD_VEHICLE
, DEFAULT_GROUP
, this->vehicle_sel
, _ctrl_pressed
|| this->grouping
== GB_SHARED_ORDERS
, VehicleListIdentifier
{});
916 this->vehicle_sel
= INVALID_VEHICLE
;
917 this->group_over
= INVALID_GROUP
;
922 case WID_GL_LIST_GROUP
: { // Matrix group
923 const VehicleID vindex
= this->vehicle_sel
;
924 this->vehicle_sel
= INVALID_VEHICLE
;
925 this->group_over
= INVALID_GROUP
;
928 auto it
= this->group_sb
->GetScrolledItemFromWidget(this->groups
, pt
.y
, this, WID_GL_LIST_GROUP
);
929 GroupID new_g
= it
== this->groups
.end() ? NEW_GROUP
: it
->group
->index
;
931 Command
<CMD_ADD_VEHICLE_GROUP
>::Post(STR_ERROR_GROUP_CAN_T_ADD_VEHICLE
, new_g
== NEW_GROUP
? CcAddVehicleNewGroup
: nullptr, new_g
, vindex
, _ctrl_pressed
|| this->grouping
== GB_SHARED_ORDERS
, VehicleListIdentifier
{});
935 case WID_GL_LIST_VEHICLE
: { // Matrix vehicle
936 const VehicleID vindex
= this->vehicle_sel
;
937 this->vehicle_sel
= INVALID_VEHICLE
;
938 this->group_over
= INVALID_GROUP
;
941 auto it
= this->vscroll
->GetScrolledItemFromWidget(this->vehgroups
, pt
.y
, this, WID_GL_LIST_VEHICLE
);
942 if (it
== this->vehgroups
.end()) return; // click out of list bound
944 const GUIVehicleGroup
&vehgroup
= *it
;
945 switch (this->grouping
) {
947 const Vehicle
*v
= vehgroup
.GetSingleVehicle();
948 if (!VehicleClicked(v
) && vindex
== v
->index
) {
949 ShowVehicleViewWindow(v
);
954 case GB_SHARED_ORDERS
: {
955 if (!VehicleClicked(vehgroup
)) {
956 const Vehicle
*v
= vehgroup
.vehicles_begin
[0];
957 if (vindex
== v
->index
) {
958 if (vehgroup
.NumVehicles() == 1) {
959 ShowVehicleViewWindow(v
);
961 ShowVehicleListWindow(v
);
976 void OnDragDrop(Point pt
, WidgetID widget
) override
978 if (this->vehicle_sel
!= INVALID_VEHICLE
) OnDragDrop_Vehicle(pt
, widget
);
979 if (this->group_sel
!= INVALID_GROUP
) OnDragDrop_Group(pt
, widget
);
981 _cursor
.vehchain
= false;
984 void OnQueryTextFinished(std::optional
<std::string
> str
) override
986 if (str
.has_value()) Command
<CMD_ALTER_GROUP
>::Post(STR_ERROR_GROUP_CAN_T_RENAME
, AlterGroupMode::Rename
, this->group_rename
, 0, *str
);
987 this->group_rename
= INVALID_GROUP
;
990 void OnResize() override
992 this->group_sb
->SetCapacityFromWidget(this, WID_GL_LIST_GROUP
);
993 this->vscroll
->SetCapacityFromWidget(this, WID_GL_LIST_VEHICLE
);
996 void OnDropdownSelect(WidgetID widget
, int index
) override
999 case WID_GL_GROUP_BY_DROPDOWN
:
1000 this->UpdateVehicleGroupBy(static_cast<GroupBy
>(index
));
1003 case WID_GL_SORT_BY_DROPDOWN
:
1004 this->vehgroups
.SetSortType(index
);
1007 case WID_GL_FILTER_BY_CARGO
: // Select a cargo filter criteria
1008 this->SetCargoFilter(index
);
1011 case WID_GL_MANAGE_VEHICLES_DROPDOWN
:
1012 assert(!this->vehicles
.empty());
1015 case ADI_REPLACE
: // Replace window
1016 ShowReplaceGroupVehicleWindow(this->vli
.index
, this->vli
.vtype
);
1018 case ADI_SERVICE
: // Send for servicing
1019 case ADI_DEPOT
: { // Send to Depots
1020 Command
<CMD_SEND_VEHICLE_TO_DEPOT
>::Post(GetCmdSendToDepotMsg(this->vli
.vtype
), 0, DepotCommand::MassSend
| (index
== ADI_SERVICE
? DepotCommand::Service
: DepotCommand::None
), this->vli
);
1024 case ADI_CREATE_GROUP
: // Create group
1025 Command
<CMD_ADD_VEHICLE_GROUP
>::Post(CcAddVehicleNewGroup
, NEW_GROUP
, INVALID_VEHICLE
, false, this->vli
);
1028 case ADI_ADD_SHARED
: // Add shared Vehicles
1029 assert(Group::IsValidID(this->vli
.index
));
1031 Command
<CMD_ADD_SHARED_VEHICLE_GROUP
>::Post(STR_ERROR_GROUP_CAN_T_ADD_SHARED_VEHICLE
, this->vli
.index
, this->vli
.vtype
);
1033 case ADI_REMOVE_ALL
: // Remove all Vehicles from the selected group
1034 assert(Group::IsValidID(this->vli
.index
));
1036 Command
<CMD_REMOVE_ALL_VEHICLES_GROUP
>::Post(STR_ERROR_GROUP_CAN_T_REMOVE_ALL_VEHICLES
, this->vli
.index
);
1038 default: NOT_REACHED();
1042 default: NOT_REACHED();
1048 void OnGameTick() override
1050 if (this->groups
.NeedResort() || this->vehgroups
.NeedResort()) {
1055 void OnPlaceObjectAbort() override
1057 /* abort drag & drop */
1058 this->vehicle_sel
= INVALID_VEHICLE
;
1059 this->DirtyHighlightedGroupWidget();
1060 this->group_sel
= INVALID_GROUP
;
1061 this->group_over
= INVALID_GROUP
;
1062 this->SetWidgetDirty(WID_GL_LIST_VEHICLE
);
1065 void OnMouseDrag(Point pt
, WidgetID widget
) override
1067 if (this->vehicle_sel
== INVALID_VEHICLE
&& this->group_sel
== INVALID_GROUP
) return;
1069 /* A vehicle is dragged over... */
1070 GroupID new_group_over
= INVALID_GROUP
;
1072 case WID_GL_DEFAULT_VEHICLES
: // ... the 'default' group.
1073 new_group_over
= DEFAULT_GROUP
;
1076 case WID_GL_LIST_GROUP
: { // ... the list of custom groups.
1077 auto it
= this->group_sb
->GetScrolledItemFromWidget(this->groups
, pt
.y
, this, WID_GL_LIST_GROUP
);
1078 new_group_over
= it
== this->groups
.end() ? NEW_GROUP
: it
->group
->index
;
1083 /* Do not highlight when dragging over the current group */
1084 if (this->vehicle_sel
!= INVALID_VEHICLE
) {
1085 if (Vehicle::Get(vehicle_sel
)->group_id
== new_group_over
) new_group_over
= INVALID_GROUP
;
1086 } else if (this->group_sel
!= INVALID_GROUP
) {
1087 if (this->group_sel
== new_group_over
|| Group::Get(this->group_sel
)->parent
== new_group_over
) new_group_over
= INVALID_GROUP
;
1090 /* Mark widgets as dirty if the group changed. */
1091 if (new_group_over
!= this->group_over
) {
1092 this->DirtyHighlightedGroupWidget();
1093 this->group_over
= new_group_over
;
1094 this->DirtyHighlightedGroupWidget();
1098 void ShowRenameGroupWindow(GroupID group
, bool empty
)
1100 assert(Group::IsValidID(group
));
1101 this->group_rename
= group
;
1102 /* Show empty query for new groups */
1103 StringID str
= STR_EMPTY
;
1105 SetDParam(0, group
);
1106 str
= STR_GROUP_NAME
;
1108 ShowQueryString(str
, STR_GROUP_RENAME_CAPTION
, MAX_LENGTH_GROUP_NAME_CHARS
, this, CS_ALPHANUMERAL
, QSF_ENABLE_DEFAULT
| QSF_LEN_IN_CHARS
);
1112 * Tests whether a given vehicle is selected in the window, and unselects it if necessary.
1113 * Called when the vehicle is deleted.
1114 * @param vehicle Vehicle that is going to be deleted
1116 void UnselectVehicle(VehicleID vehicle
)
1118 if (this->vehicle_sel
== vehicle
) ResetObjectToPlace();
1122 * Selects the specified group in the list
1124 * @param g_id The ID of the group to be selected
1126 void SelectGroup(const GroupID g_id
)
1128 if (g_id
== INVALID_GROUP
|| g_id
== this->vli
.index
) return;
1130 this->vli
.index
= g_id
;
1131 if (g_id
!= ALL_GROUP
&& g_id
!= DEFAULT_GROUP
) {
1132 const Group
*g
= Group::Get(g_id
);
1134 auto found
= std::find_if(std::begin(this->groups
), std::end(this->groups
), [g
](const auto &item
) { return item
.group
== g
; });
1135 if (found
== std::end(this->groups
)) {
1136 /* The group's branch is maybe collapsed, so try to expand it. */
1137 for (auto pg
= Group::GetIfValid(g
->parent
); pg
!= nullptr; pg
= Group::GetIfValid(pg
->parent
)) {
1140 this->groups
.ForceRebuild();
1141 this->BuildGroupList(this->owner
);
1142 this->group_sb
->SetCount(this->groups
.size());
1143 found
= std::find_if(std::begin(this->groups
), std::end(this->groups
), [g
](const auto &item
) { return item
.group
== g
; });
1145 if (found
!= std::end(this->groups
)) this->group_sb
->ScrollTowards(std::distance(std::begin(this->groups
), found
));
1147 this->vehgroups
.ForceRebuild();
1154 static WindowDesc
_other_group_desc(
1155 WDP_AUTO
, "list_groups", 460, 246,
1156 WC_INVALID
, WC_NONE
,
1158 _nested_group_widgets
1161 static WindowDesc
_train_group_desc(
1162 WDP_AUTO
, "list_groups_train", 525, 246,
1163 WC_TRAINS_LIST
, WC_NONE
,
1165 _nested_group_widgets
1169 * Show the group window for the given company and vehicle type.
1170 * @param company The company to show the window for.
1171 * @param vehicle_type The type of vehicle to show it for.
1172 * @param group The group to be selected. Defaults to INVALID_GROUP.
1173 * @param need_existing_window Whether the existing window is needed. Defaults to false.
1175 void ShowCompanyGroup(CompanyID company
, VehicleType vehicle_type
, GroupID group
, bool need_existing_window
)
1177 if (!Company::IsValidID(company
)) return;
1179 const WindowNumber num
= VehicleListIdentifier(VL_GROUP_LIST
, vehicle_type
, company
).Pack();
1180 VehicleGroupWindow
*w
;
1181 if (vehicle_type
== VEH_TRAIN
) {
1182 w
= AllocateWindowDescFront
<VehicleGroupWindow
>(_train_group_desc
, num
, need_existing_window
);
1184 _other_group_desc
.cls
= GetWindowClassForVehicleType(vehicle_type
);
1185 w
= AllocateWindowDescFront
<VehicleGroupWindow
>(_other_group_desc
, num
, need_existing_window
);
1187 if (w
!= nullptr) w
->SelectGroup(group
);
1191 * Show the group window for the given vehicle.
1192 * @param v The vehicle to show the window for.
1194 void ShowCompanyGroupForVehicle(const Vehicle
*v
)
1196 ShowCompanyGroup(v
->owner
, v
->type
, v
->group_id
, true);
1200 * Finds a group list window determined by vehicle type and owner
1201 * @param vt vehicle type
1202 * @param owner owner of groups
1203 * @return pointer to VehicleGroupWindow, nullptr if not found
1205 static inline VehicleGroupWindow
*FindVehicleGroupWindow(VehicleType vt
, Owner owner
)
1207 return dynamic_cast<VehicleGroupWindow
*>(FindWindowById(GetWindowClassForVehicleType(vt
), VehicleListIdentifier(VL_GROUP_LIST
, vt
, owner
).Pack()));
1211 * Opens a 'Rename group' window for newly created group.
1212 * @param veh_type Vehicle type.
1214 static void CcCreateGroup(GroupID gid
, VehicleType veh_type
)
1216 VehicleGroupWindow
*w
= FindVehicleGroupWindow(veh_type
, _current_company
);
1217 if (w
!= nullptr) w
->ShowRenameGroupWindow(gid
, true);
1221 * Opens a 'Rename group' window for newly created group.
1222 * @param result Did command succeed?
1223 * @param new_group ID of the created group.
1224 * @param vt Vehicle type.
1225 * @see CmdCreateGroup
1227 void CcCreateGroup(Commands
, const CommandCost
&result
, GroupID new_group
, VehicleType vt
, GroupID
)
1229 if (result
.Failed()) return;
1231 assert(vt
<= VEH_AIRCRAFT
);
1232 CcCreateGroup(new_group
, vt
);
1236 * Open rename window after adding a vehicle to a new group via drag and drop.
1237 * @param result Did command succeed?
1238 * @param new_group ID of the created group.
1240 void CcAddVehicleNewGroup(Commands
, const CommandCost
&result
, GroupID new_group
, GroupID
, VehicleID
, bool, const VehicleListIdentifier
&)
1242 if (result
.Failed()) return;
1244 const Group
*g
= Group::Get(new_group
);
1245 CcCreateGroup(new_group
, g
->vehicle_type
);
1249 * Removes the highlight of a vehicle in a group window
1250 * @param *v Vehicle to remove all highlights from
1252 void DeleteGroupHighlightOfVehicle(const Vehicle
*v
)
1254 /* If we haven't got any vehicles on the mouse pointer, we haven't got any highlighted in any group windows either
1255 * If that is the case, we can skip looping though the windows and save time
1257 if (_special_mouse_mode
!= WSM_DRAGDROP
) return;
1259 VehicleGroupWindow
*w
= FindVehicleGroupWindow(v
->type
, v
->owner
);
1260 if (w
!= nullptr) w
->UnselectVehicle(v
->index
);