OpenTTD Source 20261112-master-gbcbec5e63f
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
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 "window_func.h"
21#include "waypoint_base.h"
22#include "station_base.h"
23#include "waypoint_cmd.h"
24#include "zoom_func.h"
25
27#include "widgets/misc_widget.h"
28
29#include "table/strings.h"
30
31#include "safeguards.h"
32
35private:
37 Waypoint *wp = nullptr;
38
44 {
45 if (!this->wp->IsInUse()) return this->wp->xy;
46
47 StationType type;
48 switch (this->vt) {
51 break;
52
55 break;
56
58 type = StationType::Buoy;
59 break;
60
61 default:
62 NOT_REACHED();
63 }
64 return this->wp->GetTileArea(type).GetCentreTile();
65 }
66
67public:
74 {
75 this->wp = Waypoint::Get(window_number);
76 if (wp->string_id == STR_SV_STNAME_WAYPOINT) {
78 } else {
79 this->vt = VehicleType::Ship;
80 }
81
82 this->CreateNestedTree();
83 if (this->vt == VehicleType::Train) {
84 this->GetWidget<NWidgetCore>(WID_W_SHOW_VEHICLES)->SetStringTip(STR_TRAIN, STR_STATION_VIEW_SCHEDULED_TRAINS_TOOLTIP);
85 }
86 if (this->vt == VehicleType::Road) {
87 this->GetWidget<NWidgetCore>(WID_W_SHOW_VEHICLES)->SetStringTip(STR_LORRY, STR_STATION_VIEW_SCHEDULED_ROAD_VEHICLES_TOOLTIP);
88 }
89 if (this->vt != VehicleType::Ship) {
90 this->GetWidget<NWidgetCore>(WID_W_CENTER_VIEW)->SetToolTip(STR_WAYPOINT_VIEW_CENTER_TOOLTIP);
91 this->GetWidget<NWidgetCore>(WID_W_RENAME)->SetToolTip(STR_WAYPOINT_VIEW_EDIT_TOOLTIP);
92 }
93 this->FinishInitNested(window_number);
94
95 this->owner = this->wp->owner;
97
100
101 this->OnInvalidateData(0);
102 }
103
104 void Close([[maybe_unused]] int data = 0) override
105 {
106 CloseWindowById(GetWindowClassForVehicleType(this->vt), VehicleListIdentifier(VehicleListType::Station, this->vt, this->owner, this->window_number).ToWindowNumber(), false);
108 this->Window::Close();
109 }
110
111 std::string GetWidgetString(WidgetID widget, StringID stringid) const override
112 {
113 if (widget == WID_W_CAPTION) return GetString(STR_WAYPOINT_VIEW_CAPTION, this->wp->index);
114
115 return this->Window::GetWidgetString(widget, stringid);
116 }
117
118 void OnPaint() override
119 {
123
124 this->DrawWidgets();
125 }
126
127 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
128 {
129 Window *w = FindWindowByClass(WindowClass::QueryString);
130
131 switch (widget) {
132 case WID_W_CENTER_VIEW: // scroll to location
133 if (IsCtrlPressed()) {
135 } else {
137 }
138 break;
139
140 case WID_W_RENAME: // rename
141 ShowQueryString(GetString(STR_WAYPOINT_NAME, this->wp->index), STR_WAYPOINT_VIEW_EDIT_WAYPOINT_SIGN, MAX_LENGTH_STATION_NAME_CHARS, this, CS_ALPHANUMERAL,
142 {QueryStringFlag::EnableDefault, QueryStringFlag::LengthIsInChars, QueryStringFlag::EnableMove});
143 break;
144
145 case WID_W_SHOW_VEHICLES: // show list of vehicles having this waypoint in their orders
146 ShowVehicleListWindow(this->wp->owner, this->vt, this->wp->index);
147 break;
148
149 case WID_W_CATCHMENT:
151
152 if (w != nullptr && this->IsWidgetLowered(WID_W_CATCHMENT)) {
153 if (w->parent->window_class == WindowClass::StationView && w->IsWidgetLowered(WID_QS_MOVE)) SetViewportStationRect(Station::Get(w->parent->window_number), true);
154 if (w->parent->window_class == WindowClass::WaypointView && w->IsWidgetLowered(WID_QS_MOVE)) SetViewportWaypointRect(Waypoint::Get(w->parent->window_number), true);
155 }
156 break;
157 }
158 }
159
165 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
166 {
167 if (!gui_scope) return;
168 /* You can only change your own waypoints */
169 this->SetWidgetDisabledState(WID_W_RENAME, !this->wp->IsInUse() || (this->wp->owner != _local_company && this->wp->owner != OWNER_NONE));
170 /* Disable the widget for waypoints with no use */
172
173 ScrollWindowToTile(this->GetCenterTile(), this, true);
174 }
175
176 void OnResize() override
177 {
178 if (this->viewport != nullptr) {
180 nvp->UpdateViewportCoordinates(this);
181 this->wp->UpdateVirtCoord();
182
183 ScrollWindowToTile(this->GetCenterTile(), this, true); // Re-center viewport.
184 }
185 }
186
187 void OnQueryTextFinished(std::optional<std::string> str) override
188 {
189 if (!str.has_value()) return;
190
191 Command<Commands::RenameWaypoint>::Post(STR_ERROR_CAN_T_CHANGE_WAYPOINT_NAME, this->window_number, *str);
192 }
193
194};
195
197static constexpr std::initializer_list<NWidgetPart> _nested_waypoint_view_widgets = {
200 NWidget(WWT_PUSHIMGBTN, Colours::Grey, WID_W_RENAME), SetAspect(WidgetDimensions::ASPECT_RENAME), SetSpriteTip(SPR_RENAME, STR_BUOY_VIEW_RENAME_TOOLTIP),
202 NWidget(WWT_PUSHIMGBTN, Colours::Grey, WID_W_CENTER_VIEW), SetAspect(WidgetDimensions::ASPECT_LOCATION), SetSpriteTip(SPR_GOTO_LOCATION, STR_BUOY_VIEW_CENTER_TOOLTIP),
206 EndContainer(),
210 EndContainer(),
211 EndContainer(),
213 NWidget(WWT_TEXTBTN, Colours::Grey, WID_W_CATCHMENT), SetMinimalSize(50, 12), SetResize(1, 0), SetFill(1, 1), SetStringTip(STR_BUTTON_CATCHMENT, STR_TOOLTIP_CATCHMENT),
214 NWidget(WWT_PUSHTXTBTN, Colours::Grey, WID_W_SHOW_VEHICLES), SetAspect(WidgetDimensions::ASPECT_VEHICLE_ICON), SetStringTip(STR_SHIP, STR_STATION_VIEW_SCHEDULED_SHIPS_TOOLTIP),
216 EndContainer(),
217};
218
221 WindowPosition::Automatic, "view_waypoint", 260, 118,
222 WindowClass::WaypointView, WindowClass::None,
223 {},
225);
226
constexpr 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.
void UpdateViewportCoordinates(Window *w)
Update the position and size of the viewport (after eg a resize).
Definition widget.cpp:2451
void InitializeViewport(Window *w, std::variant< TileIndex, VehicleID > focus, ZoomLevel zoom)
Initialize the viewport of the window.
Definition widget.cpp:2442
TileIndex GetCentreTile() const
Get the centre tile.
Functions related to commands.
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 IsCtrlPressed()
Test if the ctrl key is pressed.
Definition gfx_func.h:82
@ Grey
Grey.
Definition gfx_type.h:299
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 EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=INVALID_WIDGET)
Widget part function for starting a new 'real' widget.
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.
#define Point
Macro that prevents name conflicts between included headers.
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.
Types related to the misc widgets.
@ WID_QS_MOVE
Move button.
Definition misc_widget.h:39
A number of safeguards to prevent using unsafe methods.
Base classes/functions for stations.
StationType
Station types.
@ RailWaypoint
Waypoint for trains.
@ Buoy
Waypoint for ships.
@ RoadWaypoint
Waypoint for trucks and busses.
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:423
Functions related to OTTD's strings.
StrongType::Typedef< uint32_t, struct StringIDTag, StrongType::Compare, StrongType::Integer > StringID
Numeric value that represents a string, independent of the selected language.
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...
static Waypoint * Get(auto index)
The information about a vehicle list.
Definition vehiclelist.h:32
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.
TileArea GetTileArea(StationType type) const override
Get the tile area for a given station type.
Definition waypoint.cpp:36
uint16_t waypoint_flags
Waypoint flags, see WaypointFlags.
void UpdateVirtCoord() override
Update the virtual coords needed to draw the waypoint sign.
High level window description.
Definition window_gui.h:171
Number to differentiate different windows of the same class.
virtual void Close(int data=0)
Hide the window and all its child windows, and mark them for a later deletion.
Definition window.cpp:1150
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition window.cpp:1855
void DrawWidgets() const
Paint all widgets of a window.
Definition widget.cpp:792
Window * parent
Parent window.
Definition window_gui.h:327
std::unique_ptr< ViewportData > viewport
Pointer to viewport data, if present.
Definition window_gui.h:317
virtual std::string GetWidgetString(WidgetID widget, StringID stringid) const
Get the raw string for a widget.
Definition window.cpp:513
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
Definition window.cpp:1845
WindowClass window_class
Window class.
Definition window_gui.h:300
bool IsWidgetLowered(WidgetID widget_index) const
Gets the lowered state of a widget.
Definition window_gui.h:490
Owner owner
The owner of the content shown in this window. Company colour is acquired from this variable.
Definition window_gui.h:315
void SetWidgetLoweredState(WidgetID widget_index, bool lowered_stat)
Sets the lowered/raised status of a widget.
Definition window_gui.h:440
Window(WindowDesc &desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition window.cpp:1879
const NWID * GetWidget(WidgetID widnum) const
Get the nested widget with number widnum from the nested widget tree.
WindowFlags flags
Window flags.
Definition window_gui.h:299
void SetWidgetDisabledState(WidgetID widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
Definition window_gui.h:380
WindowNumber window_number
Window number within the window class.
Definition window_gui.h:301
Stuff related to the text buffer GUI.
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
Functions related to the vehicle's GUIs.
WindowClass GetWindowClassForVehicleType(VehicleType vt)
Get WindowClass for vehicle list of given vehicle type.
Definition vehicle_gui.h:96
VehicleType
Available vehicle types.
@ Ship
Ship vehicle type.
@ Invalid
Non-existing type of vehicle.
@ Road
Road vehicle type.
@ Train
Train vehicle type.
Functions and type for generating vehicle lists.
@ Station
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 SetViewportStationRect(const Station *st, bool sel)
Select or deselect station for rectangle area highlight.
void SetViewportCatchmentWaypoint(const Waypoint *wp, bool sel)
Select or deselect waypoint for coverage area highlight.
void SetViewportWaypointRect(const Waypoint *wp, bool sel)
Select or deselect waypoint for rectangle 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 WindowDesc _waypoint_view_desc(WindowPosition::Automatic, "view_waypoint", 260, 118, WindowClass::WaypointView, WindowClass::None, {}, _nested_waypoint_view_widgets)
The description of the waypoint view.
void ShowWaypointWindow(const Waypoint *wp)
Show the window for the given waypoint.
static constexpr std::initializer_list< NWidgetPart > _nested_waypoint_view_widgets
The widgets of the waypoint view.
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:40
@ WWT_PUSHIMGBTN
Normal push-button (no toggle button) with image caption.
@ NWID_HORIZONTAL
Horizontal container.
Definition widget_type.h:66
@ WWT_TEXTBTN
(Toggle) Button with text
Definition widget_type.h:44
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:39
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX).
Definition widget_type.h:57
@ WWT_SHADEBOX
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX).
Definition widget_type.h:55
@ WWT_CAPTION
Window caption (window title between closebox and stickybox).
Definition widget_type.h:52
@ WWT_CLOSEBOX
Close box (at top-left of a window).
Definition widget_type.h:60
@ WWT_RESIZEBOX
Resize box (normally at bottom-right of a window).
Definition widget_type.h:59
@ WWT_DEFSIZEBOX
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX).
Definition widget_type.h:56
@ NWID_VIEWPORT
Nested widget containing a viewport.
Definition widget_type.h:73
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:1242
Window * FindWindowByClass(WindowClass cls)
Find any window by its class.
Definition window.cpp:1214
Window functions not directly related to making/drawing windows.
Functions, definitions and such used only by the GUI.
Twindow * AllocateWindowDescFront(WindowDesc &desc, WindowNumber window_number, Targs... extra_arguments)
Open a new window.
@ DisableVpScroll
Window does not do autoscroll,.
Definition window_gui.h:230
@ Automatic
Find a place automatically.
Definition window_gui.h:145
int WidgetID
Widget ID.
Definition window_type.h:21
Functions related to zooming.
ZoomLevel ScaleZoomGUI(ZoomLevel value)
Scale zoom level relative to GUI zoom.
Definition zoom_func.h:87
@ Viewport
Default zoom level for viewports.
Definition zoom_type.h:34