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 waypoint_gui.cpp Handling of waypoints gui. */
11 #include "window_gui.h"
13 #include "textbuf_gui.h"
14 #include "vehiclelist.h"
15 #include "vehicle_gui.h"
16 #include "viewport_func.h"
17 #include "strings_func.h"
18 #include "command_func.h"
19 #include "company_func.h"
20 #include "company_base.h"
21 #include "window_func.h"
22 #include "waypoint_base.h"
23 #include "waypoint_cmd.h"
24 #include "zoom_func.h"
26 #include "widgets/waypoint_widget.h"
28 #include "table/strings.h"
30 #include "safeguards.h"
32 /** GUI for accessing waypoints and buoys. */
33 struct WaypointWindow
: Window
{
35 VehicleType vt
; ///< Vehicle type using the waypoint.
36 Waypoint
*wp
; ///< Waypoint displayed by the window.
39 * Get the center tile of the waypoint.
40 * @return The center tile if the waypoint exists, otherwise the tile with the waypoint name.
42 TileIndex
GetCenterTile() const
44 if (!this->wp
->IsInUse()) return this->wp
->xy
;
49 type
= STATION_WAYPOINT
;
53 type
= STATION_ROADWAYPOINT
;
64 this->wp
->GetTileArea(&ta
, type
);
65 return ta
.GetCenterTile();
70 * Construct the window.
71 * @param desc The description of the window.
72 * @param window_number The window number, in this case the waypoint's ID.
74 WaypointWindow(WindowDesc
&desc
, WindowNumber window_number
) : Window(desc
)
76 this->wp
= Waypoint::Get(window_number
);
77 if (wp
->string_id
== STR_SV_STNAME_WAYPOINT
) {
78 this->vt
= HasBit(this->wp
->waypoint_flags
, WPF_ROAD
) ? VEH_ROAD
: VEH_TRAIN
;
83 this->CreateNestedTree();
84 if (this->vt
== VEH_TRAIN
) {
85 this->GetWidget
<NWidgetCore
>(WID_W_SHOW_VEHICLES
)->SetDataTip(STR_TRAIN
, STR_STATION_VIEW_SCHEDULED_TRAINS_TOOLTIP
);
87 if (this->vt
== VEH_ROAD
) {
88 this->GetWidget
<NWidgetCore
>(WID_W_SHOW_VEHICLES
)->SetDataTip(STR_LORRY
, STR_STATION_VIEW_SCHEDULED_ROAD_VEHICLES_TOOLTIP
);
90 if (this->vt
!= VEH_SHIP
) {
91 this->GetWidget
<NWidgetCore
>(WID_W_CENTER_VIEW
)->tool_tip
= STR_WAYPOINT_VIEW_CENTER_TOOLTIP
;
92 this->GetWidget
<NWidgetCore
>(WID_W_RENAME
)->tool_tip
= STR_WAYPOINT_VIEW_CHANGE_WAYPOINT_NAME
;
94 this->FinishInitNested(window_number
);
96 this->owner
= this->wp
->owner
;
97 this->flags
|= WF_DISABLE_VP_SCROLL
;
99 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_W_VIEWPORT
);
100 nvp
->InitializeViewport(this, this->GetCenterTile(), ScaleZoomGUI(ZOOM_LVL_VIEWPORT
));
102 this->OnInvalidateData(0);
105 void Close([[maybe_unused
]] int data
= 0) override
107 CloseWindowById(GetWindowClassForVehicleType(this->vt
), VehicleListIdentifier(VL_STATION_LIST
, this->vt
, this->owner
, this->window_number
).Pack(), false);
108 SetViewportCatchmentWaypoint(Waypoint::Get(this->window_number
), false);
109 this->Window::Close();
112 void SetStringParameters(WidgetID widget
) const override
114 if (widget
== WID_W_CAPTION
) SetDParam(0, this->wp
->index
);
117 void OnPaint() override
119 extern const Waypoint
*_viewport_highlight_waypoint
;
120 this->SetWidgetDisabledState(WID_W_CATCHMENT
, !this->wp
->IsInUse());
121 this->SetWidgetLoweredState(WID_W_CATCHMENT
, _viewport_highlight_waypoint
== this->wp
);
126 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
129 case WID_W_CENTER_VIEW
: // scroll to location
131 ShowExtraViewportWindow(this->GetCenterTile());
133 ScrollMainWindowToTile(this->GetCenterTile());
137 case WID_W_RENAME
: // rename
138 SetDParam(0, this->wp
->index
);
139 ShowQueryString(STR_WAYPOINT_NAME
, STR_EDIT_WAYPOINT_NAME
, MAX_LENGTH_STATION_NAME_CHARS
, this, CS_ALPHANUMERAL
, QSF_ENABLE_DEFAULT
| QSF_LEN_IN_CHARS
);
142 case WID_W_SHOW_VEHICLES
: // show list of vehicles having this waypoint in their orders
143 ShowVehicleListWindow(this->wp
->owner
, this->vt
, this->wp
->index
);
146 case WID_W_CATCHMENT
:
147 SetViewportCatchmentWaypoint(Waypoint::Get(this->window_number
), !this->IsWidgetLowered(WID_W_CATCHMENT
));
153 * Some data on this window has become invalid.
154 * @param data Information about the changed data.
155 * @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.
157 void OnInvalidateData([[maybe_unused
]] int data
= 0, [[maybe_unused
]] bool gui_scope
= true) override
159 if (!gui_scope
) return;
160 /* You can only change your own waypoints */
161 this->SetWidgetDisabledState(WID_W_RENAME
, !this->wp
->IsInUse() || (this->wp
->owner
!= _local_company
&& this->wp
->owner
!= OWNER_NONE
));
162 /* Disable the widget for waypoints with no use */
163 this->SetWidgetDisabledState(WID_W_SHOW_VEHICLES
, !this->wp
->IsInUse());
165 ScrollWindowToTile(this->GetCenterTile(), this, true);
168 void OnResize() override
170 if (this->viewport
!= nullptr) {
171 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_W_VIEWPORT
);
172 nvp
->UpdateViewportCoordinates(this);
173 this->wp
->UpdateVirtCoord();
175 ScrollWindowToTile(this->GetCenterTile(), this, true); // Re-center viewport.
179 void OnQueryTextFinished(std::optional
<std::string
> str
) override
181 if (!str
.has_value()) return;
183 Command
<CMD_RENAME_WAYPOINT
>::Post(STR_ERROR_CAN_T_CHANGE_WAYPOINT_NAME
, this->window_number
, *str
);
188 /** The widgets of the waypoint view. */
189 static constexpr NWidgetPart _nested_waypoint_view_widgets
[] = {
190 NWidget(NWID_HORIZONTAL
),
191 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
192 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_W_RENAME
), SetAspect(WidgetDimensions::ASPECT_RENAME
), SetDataTip(SPR_RENAME
, STR_BUOY_VIEW_CHANGE_BUOY_NAME
),
193 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_W_CAPTION
), SetDataTip(STR_WAYPOINT_VIEW_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
194 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_W_CENTER_VIEW
), SetAspect(WidgetDimensions::ASPECT_LOCATION
), SetDataTip(SPR_GOTO_LOCATION
, STR_BUOY_VIEW_CENTER_TOOLTIP
),
195 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
196 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
197 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
199 NWidget(WWT_PANEL
, COLOUR_GREY
),
200 NWidget(WWT_INSET
, COLOUR_GREY
), SetPadding(2, 2, 2, 2),
201 NWidget(NWID_VIEWPORT
, COLOUR_GREY
, WID_W_VIEWPORT
), SetMinimalSize(256, 88), SetResize(1, 1),
204 NWidget(NWID_HORIZONTAL
),
205 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_W_CATCHMENT
), SetMinimalSize(50, 12), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_BUTTON_CATCHMENT
, STR_TOOLTIP_CATCHMENT
),
206 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_W_SHOW_VEHICLES
), SetAspect(WidgetDimensions::ASPECT_VEHICLE_ICON
), SetDataTip(STR_SHIP
, STR_STATION_VIEW_SCHEDULED_SHIPS_TOOLTIP
),
207 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
211 /** The description of the waypoint view. */
212 static WindowDesc
_waypoint_view_desc(
213 WDP_AUTO
, "view_waypoint", 260, 118,
214 WC_WAYPOINT_VIEW
, WC_NONE
,
216 _nested_waypoint_view_widgets
220 * Show the window for the given waypoint.
221 * @param wp The waypoint to show the window for.
223 void ShowWaypointWindow(const Waypoint
*wp
)
225 AllocateWindowDescFront
<WaypointWindow
>(_waypoint_view_desc
, wp
->index
);