OpenTTD Source 20250312-master-gcdcc6b491d
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
34private:
36 Waypoint *wp = nullptr;
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 = StationType::RailWaypoint;
50 break;
51
52 case VEH_ROAD:
53 type = StationType::RoadWaypoint;
54 break;
55
56 case VEH_SHIP:
57 type = StationType::Buoy;
58 break;
59
60 default:
61 NOT_REACHED();
62 }
64 this->wp->GetTileArea(&ta, type);
65 return ta.GetCenterTile();
66 }
67
68public:
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).ToWindowNumber(), false);
109 this->Window::Close();
110 }
111
112 std::string GetWidgetString(WidgetID widget, StringID stringid) const override
113 {
114 if (widget == WID_W_CAPTION) return GetString(STR_WAYPOINT_VIEW_CAPTION, this->wp->index);
115
116 return this->Window::GetWidgetString(widget, stringid);
117 }
118
119 void OnPaint() override
120 {
123 this->SetWidgetLoweredState(WID_W_CATCHMENT, _viewport_highlight_waypoint == this->wp);
124
125 this->DrawWidgets();
126 }
127
128 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
129 {
130 switch (widget) {
131 case WID_W_CENTER_VIEW: // scroll to location
132 if (_ctrl_pressed) {
134 } else {
136 }
137 break;
138
139 case WID_W_RENAME: // rename
140 ShowQueryString(GetString(STR_WAYPOINT_NAME, this->wp->index), STR_EDIT_WAYPOINT_NAME, MAX_LENGTH_STATION_NAME_CHARS, this, CS_ALPHANUMERAL, {QueryStringFlag::EnableDefault, QueryStringFlag::LengthIsInChars});
141 break;
142
143 case WID_W_SHOW_VEHICLES: // show list of vehicles having this waypoint in their orders
144 ShowVehicleListWindow(this->wp->owner, this->vt, this->wp->index);
145 break;
146
147 case WID_W_CATCHMENT:
149 break;
150 }
151 }
152
158 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
159 {
160 if (!gui_scope) return;
161 /* You can only change your own waypoints */
162 this->SetWidgetDisabledState(WID_W_RENAME, !this->wp->IsInUse() || (this->wp->owner != _local_company && this->wp->owner != OWNER_NONE));
163 /* Disable the widget for waypoints with no use */
165
166 ScrollWindowToTile(this->GetCenterTile(), this, true);
167 }
168
169 void OnResize() override
170 {
171 if (this->viewport != nullptr) {
173 nvp->UpdateViewportCoordinates(this);
174 this->wp->UpdateVirtCoord();
175
176 ScrollWindowToTile(this->GetCenterTile(), this, true); // Re-center viewport.
177 }
178 }
179
180 void OnQueryTextFinished(std::optional<std::string> str) override
181 {
182 if (!str.has_value()) return;
183
185 }
186
187};
188
192 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
193 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_W_RENAME), SetAspect(WidgetDimensions::ASPECT_RENAME), SetSpriteTip(SPR_RENAME, STR_BUOY_VIEW_RENAME_TOOLTIP),
194 NWidget(WWT_CAPTION, COLOUR_GREY, WID_W_CAPTION),
195 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_W_CENTER_VIEW), SetAspect(WidgetDimensions::ASPECT_LOCATION), SetSpriteTip(SPR_GOTO_LOCATION, STR_BUOY_VIEW_CENTER_TOOLTIP),
196 NWidget(WWT_SHADEBOX, COLOUR_GREY),
197 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
198 NWidget(WWT_STICKYBOX, COLOUR_GREY),
199 EndContainer(),
200 NWidget(WWT_PANEL, COLOUR_GREY),
201 NWidget(WWT_INSET, COLOUR_GREY), SetPadding(2, 2, 2, 2),
202 NWidget(NWID_VIEWPORT, COLOUR_GREY, WID_W_VIEWPORT), SetMinimalSize(256, 88), SetResize(1, 1),
203 EndContainer(),
204 EndContainer(),
206 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_W_CATCHMENT), SetMinimalSize(50, 12), SetResize(1, 0), SetFill(1, 1), SetStringTip(STR_BUTTON_CATCHMENT, STR_TOOLTIP_CATCHMENT),
207 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_W_SHOW_VEHICLES), SetAspect(WidgetDimensions::ASPECT_VEHICLE_ICON), SetStringTip(STR_SHIP, STR_STATION_VIEW_SCHEDULED_SHIPS_TOOLTIP),
208 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
209 EndContainer(),
210};
211
214 WDP_AUTO, "view_waypoint", 260, 118,
216 {},
218);
219
225{
226 AllocateWindowDescFront<WaypointWindow>(_waypoint_view_desc, wp->index);
227}
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr Timpl & Set()
Set all bits.
Nested widget to display a viewport in a window.
void UpdateViewportCoordinates(Window *w)
Update the position and size of the viewport (after eg a resize).
Definition widget.cpp:2424
void InitializeViewport(Window *w, std::variant< TileIndex, VehicleID > focus, ZoomLevel zoom)
Initialize the viewport of the window.
Definition widget.cpp:2415
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.
Functions related to companies.
static constexpr Owner OWNER_NONE
The tile has no ownership.
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.
constexpr NWidgetPart SetSpriteTip(SpriteID sprite, StringID tip={})
Widget part function for setting the sprite and tooltip.
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.
constexpr NWidgetPart SetStringTip(StringID string, StringID tip={})
Widget part function for setting the string and tooltip.
constexpr NWidgetPart SetAspect(float ratio, AspectFlags flags=AspectFlag::ResizeX)
Widget part function for setting the aspect ratio.
constexpr NWidgetPart SetMinimalSize(int16_t x, int16_t y)
Widget part function for setting the minimal size.
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=-1)
Widget part function for starting a new 'real' widget.
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
constexpr NWidgetPart SetResize(int16_t dx, int16_t dy)
Widget part function for setting the resize step.
GUI functions that shouldn't be here.
void ShowExtraViewportWindow(TileIndex tile=INVALID_TILE)
Show a new Extra Viewport window.
void ShowQueryString(std::string_view str, StringID caption, uint maxsize, Window *parent, CharSetFilter afilter, QueryStringFlags flags)
Show a query popup window with a textbox in it.
A number of safeguards to prevent using unsafe methods.
StationType
Station types.
static const uint MAX_LENGTH_STATION_NAME_CHARS
The maximum length of a station name in characters including '\0'.
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
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:426
Functions related to OTTD's strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
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.
Represents the covered area of e.g.
Coordinates of a point in 2D.
Tindex index
Index of this pool item.
static Waypoint * Get(auto index)
Gets station with given index.
The information about a vehicle list.
Definition vehiclelist.h:32
GUI for accessing waypoints and buoys.
void OnQueryTextFinished(std::optional< std::string > str) override
The query window opened from this window has closed.
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 OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
std::string GetWidgetString(WidgetID widget, StringID stringid) const override
Get the raw string for a widget.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
void OnPaint() override
The window must be repainted.
void Close(int data=0) override
Hide the window and all its child windows, and mark them for a later deletion.
Representation of a waypoint.
uint16_t waypoint_flags
Waypoint flags, see WaypointFlags.
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:168
Number to differentiate different windows of the same class.
Data structure for an opened window.
Definition window_gui.h:274
virtual void Close(int data=0)
Hide the window and all its child windows, and mark them for a later deletion.
Definition window.cpp:1050
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition window.cpp:1736
void DrawWidgets() const
Paint all widgets of a window.
Definition widget.cpp:744
virtual std::string GetWidgetString(WidgetID widget, StringID stringid) const
Get the raw string for a widget.
Definition window.cpp:502
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
Definition window.cpp:1726
ViewportData * viewport
Pointer to viewport data, if present.
Definition window_gui.h:319
bool IsWidgetLowered(WidgetID widget_index) const
Gets the lowered state of a widget.
Definition window_gui.h:492
Owner owner
The owner of the content shown in this window. Company colour is acquired from this variable.
Definition window_gui.h:317
void SetWidgetLoweredState(WidgetID widget_index, bool lowered_stat)
Sets the lowered/raised status of a widget.
Definition window_gui.h:442
const NWID * GetWidget(WidgetID widnum) const
Get the nested widget with number widnum from the nested widget tree.
Definition window_gui.h:973
WindowFlags flags
Window flags.
Definition window_gui.h:301
void SetWidgetDisabledState(WidgetID widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
Definition window_gui.h:382
WindowNumber window_number
Window number within the window class.
Definition window_gui.h:303
Stuff related to the text buffer GUI.
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.
@ VEH_INVALID
Non-existing type of vehicle.
@ VEH_ROAD
Road vehicle type.
@ VEH_SHIP
Ship vehicle type.
@ VEH_TRAIN
Train vehicle type.
Functions and type for generating vehicle lists.
@ VL_STATION_LIST
Index is the station.
Definition vehiclelist.h:25
bool ScrollWindowToTile(TileIndex tile, Window *w, bool instant)
Scrolls the viewport in a window to a given location.
bool ScrollMainWindowToTile(TileIndex tile, bool instant)
Scrolls the viewport of the main window to a given location.
void SetViewportCatchmentWaypoint(const Waypoint *wp, bool sel)
Select or deselect waypoint for coverage area highlight.
const Waypoint * _viewport_highlight_waypoint
Currently selected waypoint for coverage area highlight.
Functions related to (drawing on) viewports.
Base of waypoints.
@ WPF_ROAD
This is a road waypoint.
Command definitions related to waypoints.
static constexpr NWidgetPart _nested_waypoint_view_widgets[]
The widgets of the waypoint view.
static WindowDesc _waypoint_view_desc(WDP_AUTO, "view_waypoint", 260, 118, WC_WAYPOINT_VIEW, WC_NONE, {}, _nested_waypoint_view_widgets)
The description 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.
@ WWT_INSET
Pressed (inset) panel, most commonly used as combo box text area.
Definition widget_type.h:41
@ WWT_PUSHIMGBTN
Normal push-button (no toggle button) with image caption.
@ NWID_HORIZONTAL
Horizontal container.
Definition widget_type.h:65
@ WWT_TEXTBTN
(Toggle) Button with text
Definition widget_type.h:45
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:40
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition widget_type.h:56
@ WWT_SHADEBOX
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition widget_type.h:54
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition widget_type.h:51
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition widget_type.h:59
@ WWT_RESIZEBOX
Resize box (normally at bottom-right of a window)
Definition widget_type.h:58
@ WWT_DEFSIZEBOX
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX)
Definition widget_type.h:55
@ NWID_VIEWPORT
Nested widget containing a viewport.
Definition widget_type.h:72
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:1143
Window functions not directly related to making/drawing windows.
Functions, definitions and such used only by the GUI.
@ DisableVpScroll
Window does not do autoscroll,.
@ WDP_AUTO
Find a place automatically.
Definition window_gui.h:145
int WidgetID
Widget ID.
Definition window_type.h:20
@ WC_WAYPOINT_VIEW
Waypoint view; Window numbers:
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition window_type.h:47
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