OpenTTD Source  20241108-master-g80f628063a
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 
27 
28 #include "table/strings.h"
29 
30 #include "safeguards.h"
31 
34 private:
37 
43  {
44  if (!this->wp->IsInUse()) return this->wp->xy;
45 
46  StationType type;
47  switch (this->vt) {
48  case VEH_TRAIN:
49  type = STATION_WAYPOINT;
50  break;
51 
52  case VEH_ROAD:
53  type = STATION_ROADWAYPOINT;
54  break;
55 
56  case VEH_SHIP:
57  type = STATION_BUOY;
58  break;
59 
60  default:
61  NOT_REACHED();
62  }
63  TileArea ta;
64  this->wp->GetTileArea(&ta, type);
65  return ta.GetCenterTile();
66  }
67 
68 public:
75  {
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;
79  } else {
80  this->vt = VEH_SHIP;
81  }
82 
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);
86  }
87  if (this->vt == VEH_ROAD) {
88  this->GetWidget<NWidgetCore>(WID_W_SHOW_VEHICLES)->SetDataTip(STR_LORRY, STR_STATION_VIEW_SCHEDULED_ROAD_VEHICLES_TOOLTIP);
89  }
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;
93  }
94  this->FinishInitNested(window_number);
95 
96  this->owner = this->wp->owner;
97  this->flags |= WF_DISABLE_VP_SCROLL;
98 
99  NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_W_VIEWPORT);
101 
102  this->OnInvalidateData(0);
103  }
104 
105  void Close([[maybe_unused]] int data = 0) override
106  {
107  CloseWindowById(GetWindowClassForVehicleType(this->vt), VehicleListIdentifier(VL_STATION_LIST, this->vt, this->owner, this->window_number).Pack(), false);
109  this->Window::Close();
110  }
111 
112  void SetStringParameters(WidgetID widget) const override
113  {
114  if (widget == WID_W_CAPTION) SetDParam(0, this->wp->index);
115  }
116 
117  void OnPaint() override
118  {
120  this->SetWidgetDisabledState(WID_W_CATCHMENT, !this->wp->IsInUse());
121  this->SetWidgetLoweredState(WID_W_CATCHMENT, _viewport_highlight_waypoint == this->wp);
122 
123  this->DrawWidgets();
124  }
125 
126  void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
127  {
128  switch (widget) {
129  case WID_W_CENTER_VIEW: // scroll to location
130  if (_ctrl_pressed) {
132  } else {
134  }
135  break;
136 
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);
140  break;
141 
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);
144  break;
145 
146  case WID_W_CATCHMENT:
148  break;
149  }
150  }
151 
157  void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
158  {
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 */
164 
165  ScrollWindowToTile(this->GetCenterTile(), this, true);
166  }
167 
168  void OnResize() override
169  {
170  if (this->viewport != nullptr) {
171  NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_W_VIEWPORT);
172  nvp->UpdateViewportCoordinates(this);
173  this->wp->UpdateVirtCoord();
174 
175  ScrollWindowToTile(this->GetCenterTile(), this, true); // Re-center viewport.
176  }
177  }
178 
179  void OnQueryTextFinished(std::optional<std::string> str) override
180  {
181  if (!str.has_value()) return;
182 
183  Command<CMD_RENAME_WAYPOINT>::Post(STR_ERROR_CAN_T_CHANGE_WAYPOINT_NAME, this->window_number, *str);
184  }
185 
186 };
187 
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),
198  EndContainer(),
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),
202  EndContainer(),
203  EndContainer(),
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),
208  EndContainer(),
209 };
210 
213  WDP_AUTO, "view_waypoint", 260, 118,
215  0,
217 );
218 
224 {
225  AllocateWindowDescFront<WaypointWindow>(_waypoint_view_desc, wp->index);
226 }
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Nested widget to display a viewport in a window.
Definition: widget_type.h:680
void UpdateViewportCoordinates(Window *w)
Update the position and size of the viewport (after eg a resize).
Definition: widget.cpp:2297
void InitializeViewport(Window *w, std::variant< TileIndex, VehicleID > focus, ZoomLevel zoom)
Initialize the viewport of the window.
Definition: widget.cpp:2288
Functions related to commands.
Definition of stuff that is very close to a company, like the company struct itself.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:52
Functions related to companies.
@ OWNER_NONE
The tile has no ownership.
Definition: company_type.h:25
bool _ctrl_pressed
Is Ctrl pressed?
Definition: gfx.cpp:38
constexpr NWidgetPart SetFill(uint16_t fill_x, uint16_t fill_y)
Widget part function for setting filling.
Definition: widget_type.h:1181
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:1228
constexpr NWidgetPart SetDataTip(uint32_t data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1202
constexpr NWidgetPart SetMinimalSize(int16_t x, int16_t y)
Widget part function for setting the minimal size.
Definition: widget_type.h:1137
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=-1)
Widget part function for starting a new 'real' widget.
Definition: widget_type.h:1309
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
Definition: widget_type.h:1191
constexpr NWidgetPart SetAspect(float ratio, AspectFlags flags=AspectFlags::ResizeX)
Widget part function for setting the aspect ratio.
Definition: widget_type.h:1295
constexpr NWidgetPart SetResize(int16_t dx, int16_t dy)
Widget part function for setting the resize step.
Definition: widget_type.h:1126
GUI functions that shouldn't be here.
void ShowExtraViewportWindow(TileIndex tile=INVALID_TILE)
Show a new Extra Viewport window.
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:1079
A number of safeguards to prevent using unsafe methods.
StationType
Station types.
Definition: station_type.h:31
static const uint MAX_LENGTH_STATION_NAME_CHARS
The maximum length of a station name in characters including '\0'.
Definition: station_type.h:87
Definition of base types and functions in a cross-platform compatible way.
@ CS_ALPHANUMERAL
Both numeric and alphabetic and spaces and stuff.
Definition: string_type.h:25
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
Functions related to OTTD's strings.
StringID string_id
Default name (town area) of station.
TileIndex xy
Base tile of the station.
Owner owner
The owner of this station.
bool IsInUse() const
Check whether the base station currently is in use; in use means that it is not scheduled for deletio...
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:1075
Represents the covered area of e.g.
Definition: tilearea_type.h:18
TileIndex GetCenterTile() const
Get the center tile.
Definition: tilearea_type.h:59
Coordinates of a point in 2D.
Tindex index
Index of this pool item.
Definition: pool_type.hpp:238
static Waypoint * Get(size_t index)
Gets station with given index.
The information about a vehicle list.
Definition: vehiclelist.h:28
GUI for accessing waypoints and buoys.
void OnInvalidateData([[maybe_unused]] int data=0, [[maybe_unused]] bool gui_scope=true) override
Some data on this window has become invalid.
TileIndex GetCenterTile() const
Get the center tile of the waypoint.
VehicleType vt
Vehicle type using the waypoint.
Waypoint * wp
Waypoint displayed by the window.
WaypointWindow(WindowDesc &desc, WindowNumber window_number)
Construct the window.
void OnResize() override
Called after the window got resized.
void OnPaint() override
The window must be repainted.
Representation of a waypoint.
Definition: waypoint_base.h:23
uint16_t waypoint_flags
Waypoint flags, see WaypointFlags.
Definition: waypoint_base.h:25
void GetTileArea(TileArea *ta, StationType type) const override
Get the tile area for a given station type.
Definition: waypoint.cpp:35
void UpdateVirtCoord() override
Update the virtual coords needed to draw the waypoint sign.
High level window description.
Definition: window_gui.h:159
Data structure for an opened window.
Definition: window_gui.h:273
virtual void Close(int data=0)
Hide the window and all its child windows, and mark them for a later deletion.
Definition: window.cpp:1047
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition: window.cpp:1733
void DrawWidgets() const
Paint all widgets of a window.
Definition: widget.cpp:731
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
Definition: window.cpp:1723
ViewportData * viewport
Pointer to viewport data, if present.
Definition: window_gui.h:318
bool IsWidgetLowered(WidgetID widget_index) const
Gets the lowered state of a widget.
Definition: window_gui.h:497
Owner owner
The owner of the content shown in this window. Company colour is acquired from this variable.
Definition: window_gui.h:316
void SetWidgetLoweredState(WidgetID widget_index, bool lowered_stat)
Sets the lowered/raised status of a widget.
Definition: window_gui.h:447
WindowFlags flags
Window flags.
Definition: window_gui.h:300
void SetWidgetDisabledState(WidgetID widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
Definition: window_gui.h:387
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:302
Stuff related to the text buffer GUI.
@ QSF_ENABLE_DEFAULT
enable the 'Default' button ("\0" is returned)
Definition: textbuf_gui.h:21
@ QSF_LEN_IN_CHARS
the length of the string is counted in characters
Definition: textbuf_gui.h:22
Functions related to the vehicle's GUIs.
WindowClass GetWindowClassForVehicleType(VehicleType vt)
Get WindowClass for vehicle list of given vehicle type.
Definition: vehicle_gui.h:97
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
@ VEH_ROAD
Road vehicle type.
Definition: vehicle_type.h:25
@ VEH_SHIP
Ship vehicle type.
Definition: vehicle_type.h:26
@ VEH_TRAIN
Train vehicle type.
Definition: vehicle_type.h:24
Functions and type for generating vehicle lists.
bool ScrollWindowToTile(TileIndex tile, Window *w, bool instant)
Scrolls the viewport in a window to a given location.
Definition: viewport.cpp:2504
bool ScrollMainWindowToTile(TileIndex tile, bool instant)
Scrolls the viewport of the main window to a given location.
Definition: viewport.cpp:2515
void SetViewportCatchmentWaypoint(const Waypoint *wp, bool sel)
Select or deselect waypoint for coverage area highlight.
Definition: viewport.cpp:3638
const Waypoint * _viewport_highlight_waypoint
Currently selected waypoint for coverage area highlight.
Definition: viewport.cpp:1006
Functions related to (drawing on) viewports.
Base of waypoints.
@ WPF_ROAD
This is a road waypoint.
Definition: waypoint_base.h:19
Command definitions related to waypoints.
static WindowDesc _waypoint_view_desc(WDP_AUTO, "view_waypoint", 260, 118, WC_WAYPOINT_VIEW, WC_NONE, 0, _nested_waypoint_view_widgets)
The description of the waypoint view.
static constexpr NWidgetPart _nested_waypoint_view_widgets[]
The widgets of the waypoint view.
void ShowWaypointWindow(const Waypoint *wp)
Show the window for the given waypoint.
Types related to the waypoint widgets.
@ WID_W_CAPTION
Caption of window.
@ WID_W_SHOW_VEHICLES
Show the vehicles visiting this waypoint.
@ WID_W_CATCHMENT
Coverage button.
@ WID_W_VIEWPORT
The viewport on this waypoint.
@ WID_W_CENTER_VIEW
Center the main view on this waypoint.
@ WID_W_RENAME
Rename this waypoint.
@ WWT_PUSHTXTBTN
Normal push-button (no toggle button) with text caption.
Definition: widget_type.h:112
@ WWT_INSET
Pressed (inset) panel, most commonly used as combo box text area.
Definition: widget_type.h:51
@ WWT_PUSHIMGBTN
Normal push-button (no toggle button) with image caption.
Definition: widget_type.h:113
@ NWID_HORIZONTAL
Horizontal container.
Definition: widget_type.h:75
@ WWT_TEXTBTN
(Toggle) Button with text
Definition: widget_type.h:55
@ WWT_PANEL
Simple depressed panel.
Definition: widget_type.h:50
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition: widget_type.h:66
@ WWT_SHADEBOX
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition: widget_type.h:64
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:61
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition: widget_type.h:69
@ WWT_RESIZEBOX
Resize box (normally at bottom-right of a window)
Definition: widget_type.h:68
@ WWT_DEFSIZEBOX
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX)
Definition: widget_type.h:65
@ NWID_VIEWPORT
Nested widget containing a viewport.
Definition: widget_type.h:81
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:1140
Window functions not directly related to making/drawing windows.
Functions, definitions and such used only by the GUI.
@ WF_DISABLE_VP_SCROLL
Window does not do autoscroll,.
Definition: window_gui.h:235
@ WDP_AUTO
Find a place automatically.
Definition: window_gui.h:147
int WidgetID
Widget ID.
Definition: window_type.h:18
int32_t WindowNumber
Number to differentiate different windows of the same class.
Definition: window_type.h:737
@ WC_WAYPOINT_VIEW
Waypoint view; Window numbers:
Definition: window_type.h:357
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:45
Functions related to zooming.
ZoomLevel ScaleZoomGUI(ZoomLevel value)
Scale zoom level relative to GUI zoom.
Definition: zoom_func.h:87
@ ZOOM_LVL_VIEWPORT
Default zoom level for viewports.
Definition: zoom_type.h:28