OpenTTD Source: src/waypoint_gui.cpp Source File

OpenTTD Source 14.0-beta3

waypoint_gui.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10 #include "stdafx.h"
11 #include "window_gui.h"
12 #include "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"
25
26 #include "widgets/waypoint_widget.h"
27
28 #include "table/strings.h"
29
30 #include "safeguards.h"
31
33 struct WaypointWindow : Window {
34 private:
35 VehicleType vt;
36 Waypoint *wp;
37
42 TileIndex GetCenterTile() const
43 {
44 if (!this->wp->IsInUse()) return this->wp->xy;
45
46 TileArea ta;
47 this->wp->GetTileArea(&ta, this->vt == VEH_TRAIN ? STATION_WAYPOINT : STATION_BUOY);
48 return ta.GetCenterTile();
49 }
50
51 public:
57 WaypointWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
58 {
59 this->wp = Waypoint::Get(window_number);
60 this->vt = (wp->string_id == STR_SV_STNAME_WAYPOINT) ? VEH_TRAIN : VEH_SHIP;
61
62 this->CreateNestedTree();
63 if (this->vt == VEH_TRAIN) {
64 this->GetWidget<NWidgetCore>(WID_W_SHOW_VEHICLES)->SetDataTip(STR_TRAIN, STR_STATION_VIEW_SCHEDULED_TRAINS_TOOLTIP);
65 this->GetWidget<NWidgetCore>(WID_W_CENTER_VIEW)->tool_tip = STR_WAYPOINT_VIEW_CENTER_TOOLTIP;
66 this->GetWidget<NWidgetCore>(WID_W_RENAME)->tool_tip = STR_WAYPOINT_VIEW_CHANGE_WAYPOINT_NAME;
67 }
68 this->FinishInitNested(window_number);
69
70 this->owner = this->wp->owner;
71 this->flags |= WF_DISABLE_VP_SCROLL;
72
73 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_W_VIEWPORT);
74 nvp->InitializeViewport(this, this->GetCenterTile(), ScaleZoomGUI(ZOOM_LVL_VIEWPORT));
75
76 this->OnInvalidateData(0);
77 }
78
79 void Close([[maybe_unused]] int data = 0) override
80 {
81 CloseWindowById(GetWindowClassForVehicleType(this->vt), VehicleListIdentifier(VL_STATION_LIST, this->vt, this->owner, this->window_number).Pack(), false);
82 SetViewportCatchmentWaypoint(Waypoint::Get(this->window_number), false);
83 this->Window::Close();
84 }
85
86 void SetStringParameters(WidgetID widget) const override
87 {
88 if (widget == WID_W_CAPTION) SetDParam(0, this->wp->index);
89 }
90
91 void OnPaint() override
92 {
93 extern const Waypoint *_viewport_highlight_waypoint;
94 this->SetWidgetDisabledState(WID_W_CATCHMENT, !this->wp->IsInUse());
95 this->SetWidgetLoweredState(WID_W_CATCHMENT, _viewport_highlight_waypoint == this->wp);
96
97 this->DrawWidgets();
98 }
99
100 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
101 {
102 switch (widget) {
103 case WID_W_CENTER_VIEW: // scroll to location
104 if (_ctrl_pressed) {
105 ShowExtraViewportWindow(this->GetCenterTile());
106 } else {
107 ScrollMainWindowToTile(this->GetCenterTile());
108 }
109 break;
110
111 case WID_W_RENAME: // rename
112 SetDParam(0, this->wp->index);
113 ShowQueryString(STR_WAYPOINT_NAME, STR_EDIT_WAYPOINT_NAME, MAX_LENGTH_STATION_NAME_CHARS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
114 break;
115
116 case WID_W_SHOW_VEHICLES: // show list of vehicles having this waypoint in their orders
117 ShowVehicleListWindow(this->wp->owner, this->vt, this->wp->index);
118 break;
119
120 case WID_W_CATCHMENT:
121 SetViewportCatchmentWaypoint(Waypoint::Get(this->window_number), !this->IsWidgetLowered(WID_W_CATCHMENT));
122 break;
123 }
124 }
125
131 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
132 {
133 if (!gui_scope) return;
134 /* You can only change your own waypoints */
135 this->SetWidgetDisabledState(WID_W_RENAME, !this->wp->IsInUse() || (this->wp->owner != _local_company && this->wp->owner != OWNER_NONE));
136 /* Disable the widget for waypoints with no use */
137 this->SetWidgetDisabledState(WID_W_SHOW_VEHICLES, !this->wp->IsInUse());
138
139 ScrollWindowToTile(this->GetCenterTile(), this, true);
140 }
141
142 void OnResize() override
143 {
144 if (this->viewport != nullptr) {
145 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_W_VIEWPORT);
146 nvp->UpdateViewportCoordinates(this);
147 this->wp->UpdateVirtCoord();
148
149 ScrollWindowToTile(this->GetCenterTile(), this, true); // Re-center viewport.
150 }
151 }
152
153 void OnQueryTextFinished(char *str) override
154 {
155 if (str == nullptr) return;
156
157 Command<CMD_RENAME_WAYPOINT>::Post(STR_ERROR_CAN_T_CHANGE_WAYPOINT_NAME, this->window_number, str);
158 }
159
160 };
161
163 static constexpr NWidgetPart _nested_waypoint_view_widgets[] = {
164 NWidget(NWID_HORIZONTAL),
165 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
166 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_W_RENAME), SetMinimalSize(12, 14), SetDataTip(SPR_RENAME, STR_BUOY_VIEW_CHANGE_BUOY_NAME),
167 NWidget(WWT_CAPTION, COLOUR_GREY, WID_W_CAPTION), SetDataTip(STR_WAYPOINT_VIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
168 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_W_CENTER_VIEW), SetMinimalSize(12, 14), SetDataTip(SPR_GOTO_LOCATION, STR_BUOY_VIEW_CENTER_TOOLTIP),
169 NWidget(WWT_SHADEBOX, COLOUR_GREY),
170 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
171 NWidget(WWT_STICKYBOX, COLOUR_GREY),
172 EndContainer(),
173 NWidget(WWT_PANEL, COLOUR_GREY),
174 NWidget(WWT_INSET, COLOUR_GREY), SetPadding(2, 2, 2, 2),
175 NWidget(NWID_VIEWPORT, COLOUR_GREY, WID_W_VIEWPORT), SetMinimalSize(256, 88), SetResize(1, 1),
176 EndContainer(),
177 EndContainer(),
178 NWidget(NWID_HORIZONTAL),
179 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_W_CATCHMENT), SetMinimalSize(50, 12), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_BUTTON_CATCHMENT, STR_TOOLTIP_CATCHMENT),
180 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_W_SHOW_VEHICLES), SetMinimalSize(15, 12), SetDataTip(STR_SHIP, STR_STATION_VIEW_SCHEDULED_SHIPS_TOOLTIP),
181 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
182 EndContainer(),
183 };
184
186 static WindowDesc _waypoint_view_desc(__FILE__, __LINE__,
187 WDP_AUTO, "view_waypoint", 260, 118,
188 WC_WAYPOINT_VIEW, WC_NONE,
189 0,
190 std::begin(_nested_waypoint_view_widgets), std::end(_nested_waypoint_view_widgets)
191 );
192
197 void ShowWaypointWindow(const Waypoint *wp)
198 {
199 AllocateWindowDescFront<WaypointWindow>(&_waypoint_view_desc, wp->index);
200 }
waypoint_widget.h
_viewport_highlight_waypoint
const Waypoint * _viewport_highlight_waypoint
Currently selected waypoint for coverage area highlight.
Definition: viewport.cpp:1003
SetFill
constexpr NWidgetPart SetFill(uint16_t fill_x, uint16_t fill_y)
Widget part function for setting filling.
Definition: widget_type.h:1141
ScrollMainWindowToTile
bool ScrollMainWindowToTile(TileIndex tile, bool instant)
Scrolls the viewport of the main window to a given location.
Definition: viewport.cpp:2509
WID_W_CAPTION
@ WID_W_CAPTION
Caption of window.
Definition: waypoint_widget.h:15
vehicle_gui.h
ShowExtraViewportWindow
void ShowExtraViewportWindow(TileIndex tile=INVALID_TILE)
Show a new Extra Viewport window.
Definition: viewport_gui.cpp:156
command_func.h
WWT_STICKYBOX
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition: widget_type.h:68
VehicleListIdentifier
The information about a vehicle list.
Definition: vehiclelist.h:28
WID_W_CENTER_VIEW
@ WID_W_CENTER_VIEW
Center the main view on this waypoint.
Definition: waypoint_widget.h:17
company_base.h
NWidgetViewport
Nested widget to display a viewport in a window.
Definition: widget_type.h:664
WWT_CAPTION
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:63
waypoint_cmd.h
NWidgetViewport::InitializeViewport
void InitializeViewport(Window *w, std::variant< TileIndex, VehicleID > focus, ZoomLevel zoom)
Initialize the viewport of the window.
Definition: widget.cpp:2228
WF_DISABLE_VP_SCROLL
@ WF_DISABLE_VP_SCROLL
Window does not do autoscroll,.
Definition: window_gui.h:229
Window::viewport
ViewportData * viewport
Pointer to viewport data, if present.
Definition: window_gui.h:312
CloseWindowById
void CloseWindowById(WindowClass cls, WindowNumber number, bool force, int data)
Close a window by its class and window number (if it is open).
Definition: window.cpp:1141
vehiclelist.h
WWT_DEFSIZEBOX
@ WWT_DEFSIZEBOX
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX)
Definition: widget_type.h:67
WaypointWindow::OnResize
void OnResize() override
Called after the window got resized.
Definition: waypoint_gui.cpp:142
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:234
NWID_HORIZONTAL
@ NWID_HORIZONTAL
Horizontal container.
Definition: widget_type.h:77
Window::Close
virtual void Close(int data=0)
Hide the window and all its child windows, and mark them for a later deletion.
Definition: window.cpp:1048
EndContainer
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
Definition: widget_type.h:1151
Waypoint
Representation of a waypoint.
Definition: waypoint_base.h:16
_ctrl_pressed
bool _ctrl_pressed
Is Ctrl pressed?
Definition: gfx.cpp:37
zoom_func.h
SpecializedStation< Waypoint, true >::Get
static Waypoint * Get(size_t index)
Gets station with given index.
Definition: base_station_base.h:259
Window::owner
Owner owner
The owner of the content shown in this window. Company colour is acquired from this variable.
Definition: window_gui.h:310
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
ScaleZoomGUI
ZoomLevel ScaleZoomGUI(ZoomLevel value)
Scale zoom level relative to GUI zoom.
Definition: zoom_func.h:87
WID_W_CATCHMENT
@ WID_W_CATCHMENT
Coverage button.
Definition: waypoint_widget.h:20
Waypoint::GetTileArea
void GetTileArea(TileArea *ta, StationType type) const override
Get the tile area for a given station type.
Definition: waypoint.cpp:34
BaseStation::owner
Owner owner
The owner of this station.
Definition: base_station_base.h:74
NWidgetPart
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:1038
textbuf_gui.h
BaseStation::string_id
StringID string_id
Default name (town area) of station.
Definition: base_station_base.h:70
QSF_LEN_IN_CHARS
@ QSF_LEN_IN_CHARS
the length of the string is counted in characters
Definition: textbuf_gui.h:22
WID_W_VIEWPORT
@ WID_W_VIEWPORT
The viewport on this waypoint.
Definition: waypoint_widget.h:16
WindowDesc
High level window description.
Definition: window_gui.h:153
WidgetID
int WidgetID
Widget ID.
Definition: window_type.h:18
Waypoint::UpdateVirtCoord
void UpdateVirtCoord() override
Update the virtual coords needed to draw the waypoint sign.
Definition: waypoint_cmd.cpp:41
window_gui.h
SetPadding
constexpr NWidgetPart SetPadding(uint8_t top, uint8_t right, uint8_t bottom, uint8_t left)
Widget part function for setting additional space around a widget.
Definition: widget_type.h:1188
SetResize
constexpr NWidgetPart SetResize(int16_t dx, int16_t dy)
Widget part function for setting the resize step.
Definition: widget_type.h:1086
WID_W_RENAME
@ WID_W_RENAME
Rename this waypoint.
Definition: waypoint_widget.h:18
WDP_AUTO
@ WDP_AUTO
Find a place automatically.
Definition: window_gui.h:141
NWidgetViewport::UpdateViewportCoordinates
void UpdateViewportCoordinates(Window *w)
Update the position and size of the viewport (after eg a resize).
Definition: widget.cpp:2237
WindowNumber
int32_t WindowNumber
Number to differentiate different windows of the same class.
Definition: window_type.h:732
NWID_VIEWPORT
@ NWID_VIEWPORT
Nested widget containing a viewport.
Definition: widget_type.h:83
WaypointWindow::WaypointWindow
WaypointWindow(WindowDesc *desc, WindowNumber window_number)
Construct the window.
Definition: waypoint_gui.cpp:57
ScrollWindowToTile
bool ScrollWindowToTile(TileIndex tile, Window *w, bool instant)
Scrolls the viewport in a window to a given location.
Definition: viewport.cpp:2498
ShowWaypointWindow
void ShowWaypointWindow(const Waypoint *wp)
Show the window for the given waypoint.
Definition: waypoint_gui.cpp:197
WaypointWindow::OnPaint
void OnPaint() override
The window must be repainted.
Definition: waypoint_gui.cpp:91
WWT_PUSHTXTBTN
@ WWT_PUSHTXTBTN
Normal push-button (no toggle button) with text caption.
Definition: widget_type.h:110
OrthogonalTileArea
Represents the covered area of e.g.
Definition: tilearea_type.h:18
NWidget
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=-1)
Widget part function for starting a new 'real' widget.
Definition: widget_type.h:1258
_local_company
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:49
safeguards.h
ShowQueryString
void ShowQueryString(StringID str, StringID caption, uint maxsize, Window *parent, CharSetFilter afilter, QueryStringFlags flags)
Show a query popup window with a textbox in it.
Definition: misc_gui.cpp:1086
WC_WAYPOINT_VIEW
@ WC_WAYPOINT_VIEW
Waypoint view; Window numbers:
Definition: window_type.h:357
Window::flags
WindowFlags flags
Window flags.
Definition: window_gui.h:294
WaypointWindow::wp
Waypoint * wp
Waypoint displayed by the window.
Definition: waypoint_gui.cpp:36
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
WaypointWindow::OnInvalidateData
void OnInvalidateData([[maybe_unused]] int data=0, [[maybe_unused]] bool gui_scope=true) override
Some data on this window has become invalid.
Definition: waypoint_gui.cpp:131
stdafx.h
ZOOM_LVL_VIEWPORT
@ ZOOM_LVL_VIEWPORT
Default zoom level for viewports.
Definition: zoom_type.h:31
Window::window_number
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:296
VehicleType
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
WaypointWindow::GetCenterTile
TileIndex GetCenterTile() const
Get the center tile of the waypoint.
Definition: waypoint_gui.cpp:42
CS_ALPHANUMERAL
@ CS_ALPHANUMERAL
Both numeric and alphabetic and spaces and stuff.
Definition: string_type.h:25
viewport_func.h
_waypoint_view_desc
static WindowDesc _waypoint_view_desc(__FILE__, __LINE__, WDP_AUTO, "view_waypoint", 260, 118, WC_WAYPOINT_VIEW, WC_NONE, 0, std::begin(_nested_waypoint_view_widgets), std::end(_nested_waypoint_view_widgets))
The description of the waypoint view.
WC_NONE
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:45
Window::SetWidgetLoweredState
void SetWidgetLoweredState(WidgetID widget_index, bool lowered_stat)
Sets the lowered/raised status of a widget.
Definition: window_gui.h:441
OrthogonalTileArea::GetCenterTile
TileIndex GetCenterTile() const
Get the center tile.
Definition: tilearea_type.h:59
Window::SetWidgetDisabledState
void SetWidgetDisabledState(WidgetID widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
Definition: window_gui.h:381
WWT_CLOSEBOX
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition: widget_type.h:71
WWT_RESIZEBOX
@ WWT_RESIZEBOX
Resize box (normally at bottom-right of a window)
Definition: widget_type.h:70
MAX_LENGTH_STATION_NAME_CHARS
static const uint MAX_LENGTH_STATION_NAME_CHARS
The maximum length of a station name in characters including '\0'.
Definition: station_type.h:87
Window::IsWidgetLowered
bool IsWidgetLowered(WidgetID widget_index) const
Gets the lowered state of a widget.
Definition: window_gui.h:491
WaypointWindow::vt
VehicleType vt
Vehicle type using the waypoint.
Definition: waypoint_gui.cpp:35
WWT_PUSHIMGBTN
@ WWT_PUSHIMGBTN
Normal push-button (no toggle button) with image caption.
Definition: widget_type.h:111
Window::CreateNestedTree
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
Definition: window.cpp:1724
strings_func.h
SetDParam
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings.cpp:104
WWT_PANEL
@ WWT_PANEL
Simple depressed panel.
Definition: widget_type.h:52
OWNER_NONE
@ OWNER_NONE
The tile has no ownership.
Definition: company_type.h:25
waypoint_base.h
SetViewportCatchmentWaypoint
void SetViewportCatchmentWaypoint(const Waypoint *wp, bool sel)
Select or deselect waypoint for coverage area highlight.
Definition: viewport.cpp:3623
Window::FinishInitNested
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition: window.cpp:1734
BaseStation::xy
TileIndex xy
Base tile of the station.
Definition: base_station_base.h:65
company_func.h
WWT_INSET
@ WWT_INSET
Pressed (inset) panel, most commonly used as combo box text area.
Definition: widget_type.h:53
_nested_waypoint_view_widgets
static constexpr NWidgetPart _nested_waypoint_view_widgets[]
The widgets of the waypoint view.
Definition: waypoint_gui.cpp:163
CommandHelper
Definition: command_func.h:93
window_func.h
SetMinimalSize
constexpr NWidgetPart SetMinimalSize(int16_t x, int16_t y)
Widget part function for setting the minimal size.
Definition: widget_type.h:1097
WID_W_SHOW_VEHICLES
@ WID_W_SHOW_VEHICLES
Show the vehicles visiting this waypoint.
Definition: waypoint_widget.h:19
gui.h
WaypointWindow
GUI for accessing waypoints and buoys.
Definition: waypoint_gui.cpp:33
BaseStation::IsInUse
bool IsInUse() const
Check whether the base station currently is in use; in use means that it is not scheduled for deletio...
Definition: base_station_base.h:182
Window
Data structure for an opened window.
Definition: window_gui.h:267
VEH_TRAIN
@ VEH_TRAIN
Train vehicle type.
Definition: vehicle_type.h:24
Window::DrawWidgets
void DrawWidgets() const
Paint all widgets of a window.
Definition: widget.cpp:731
SetDataTip
constexpr NWidgetPart SetDataTip(uint32_t data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1162
VEH_SHIP
@ VEH_SHIP
Ship vehicle type.
Definition: vehicle_type.h:26
QSF_ENABLE_DEFAULT
@ QSF_ENABLE_DEFAULT
enable the 'Default' button ("\0" is returned)
Definition: textbuf_gui.h:21
WWT_TEXTBTN
@ WWT_TEXTBTN
(Toggle) Button with text
Definition: widget_type.h:57
GetWindowClassForVehicleType
WindowClass GetWindowClassForVehicleType(VehicleType vt)
Get WindowClass for vehicle list of given vehicle type.
Definition: vehicle_gui.h:97
WWT_SHADEBOX
@ WWT_SHADEBOX
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition: widget_type.h:66