OpenTTD
waypoint_gui.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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  */
9 
12 #include "stdafx.h"
13 #include "window_gui.h"
14 #include "gui.h"
15 #include "textbuf_gui.h"
16 #include "vehiclelist.h"
17 #include "vehicle_gui.h"
18 #include "viewport_func.h"
19 #include "strings_func.h"
20 #include "command_func.h"
21 #include "company_func.h"
22 #include "company_base.h"
23 #include "window_func.h"
24 #include "waypoint_base.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  TileArea ta;
47  this->wp->GetTileArea(&ta, this->vt == VEH_TRAIN ? STATION_WAYPOINT : STATION_BUOY);
48  return ta.GetCenterTile();
49  }
50 
51 public:
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);
75 
76  this->OnInvalidateData(0);
77  }
78 
80  {
81  DeleteWindowById(GetWindowClassForVehicleType(this->vt), VehicleListIdentifier(VL_STATION_LIST, this->vt, this->owner, this->window_number).Pack(), false);
82  }
83 
84  void SetStringParameters(int widget) const override
85  {
86  if (widget == WID_W_CAPTION) SetDParam(0, this->wp->index);
87  }
88 
89  void OnClick(Point pt, int widget, int click_count) override
90  {
91  switch (widget) {
92  case WID_W_CENTER_VIEW: // scroll to location
93  if (_ctrl_pressed) {
95  } else {
97  }
98  break;
99 
100  case WID_W_RENAME: // rename
101  SetDParam(0, this->wp->index);
102  ShowQueryString(STR_WAYPOINT_NAME, STR_EDIT_WAYPOINT_NAME, MAX_LENGTH_STATION_NAME_CHARS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
103  break;
104 
105  case WID_W_SHOW_VEHICLES: // show list of vehicles having this waypoint in their orders
106  ShowVehicleListWindow(this->wp->owner, this->vt, this->wp->index);
107  break;
108  }
109  }
110 
116  void OnInvalidateData(int data = 0, bool gui_scope = true) override
117  {
118  if (!gui_scope) return;
119  /* You can only change your own waypoints */
120  this->SetWidgetDisabledState(WID_W_RENAME, !this->wp->IsInUse() || (this->wp->owner != _local_company && this->wp->owner != OWNER_NONE));
121  /* Disable the widget for waypoints with no use */
123 
124  ScrollWindowToTile(this->GetCenterTile(), this, true);
125  }
126 
127  void OnResize() override
128  {
129  if (this->viewport != nullptr) {
130  NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_W_VIEWPORT);
131  nvp->UpdateViewportCoordinates(this);
132  this->wp->UpdateVirtCoord();
133 
134  ScrollWindowToTile(this->GetCenterTile(), this, true); // Re-center viewport.
135  }
136  }
137 
138  void OnQueryTextFinished(char *str) override
139  {
140  if (str == nullptr) return;
141 
142  DoCommandP(0, this->window_number, 0, CMD_RENAME_WAYPOINT | CMD_MSG(STR_ERROR_CAN_T_CHANGE_WAYPOINT_NAME), nullptr, str);
143  }
144 
145 };
146 
150  NWidget(WWT_CLOSEBOX, COLOUR_GREY),
151  NWidget(WWT_CAPTION, COLOUR_GREY, WID_W_CAPTION), SetDataTip(STR_WAYPOINT_VIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
152  NWidget(WWT_SHADEBOX, COLOUR_GREY),
153  NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
154  NWidget(WWT_STICKYBOX, COLOUR_GREY),
155  EndContainer(),
156  NWidget(WWT_PANEL, COLOUR_GREY),
157  NWidget(WWT_INSET, COLOUR_GREY), SetPadding(2, 2, 2, 2),
158  NWidget(NWID_VIEWPORT, COLOUR_GREY, WID_W_VIEWPORT), SetMinimalSize(256, 88), SetPadding(1, 1, 1, 1), SetResize(1, 1),
159  EndContainer(),
160  EndContainer(),
162  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_W_CENTER_VIEW), SetMinimalSize(100, 12), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_BUTTON_LOCATION, STR_BUOY_VIEW_CENTER_TOOLTIP),
163  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_W_RENAME), SetMinimalSize(100, 12), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_BUTTON_RENAME, STR_BUOY_VIEW_CHANGE_BUOY_NAME),
164  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_W_SHOW_VEHICLES), SetMinimalSize(15, 12), SetDataTip(STR_SHIP, STR_STATION_VIEW_SCHEDULED_SHIPS_TOOLTIP),
165  NWidget(WWT_RESIZEBOX, COLOUR_GREY),
166  EndContainer(),
167 };
168 
171  WDP_AUTO, "view_waypoint", 260, 118,
173  0,
174  _nested_waypoint_view_widgets, lengthof(_nested_waypoint_view_widgets)
175 );
176 
182 {
183  AllocateWindowDescFront<WaypointWindow>(&_waypoint_view_desc, wp->index);
184 }
Nested widget containing a viewport.
Definition: widget_type.h:81
Functions related to OTTD&#39;s strings.
Definition of stuff that is very close to a company, like the company struct itself.
void UpdateVirtCoord() override
Update the virtual coords needed to draw the waypoint sign.
The information about a vehicle list.
Definition: vehiclelist.h:31
static NWidgetPart SetResize(int16 dx, int16 dy)
Widget part function for setting the resize step.
Definition: widget_type.h:930
void SetWidgetDisabledState(byte widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
Definition: window_gui.h:394
High level window description.
Definition: window_gui.h:168
Functions and type for generating vehicle lists.
WindowFlags flags
Window flags.
Definition: window_gui.h:312
Train vehicle type.
Definition: vehicle_type.h:26
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
void UpdateViewportCoordinates(Window *w)
Update the position and size of the viewport (after eg a resize).
Definition: widget.cpp:1936
Horizontal container.
Definition: widget_type.h:75
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:1121
Ship vehicle type.
Definition: vehicle_type.h:28
VehicleType
Available vehicle types.
Definition: vehicle_type.h:23
bool IsInUse() const
Check whether the base station currently is in use; in use means that it is not scheduled for deletio...
Caption of window.
void SetStringParameters(int widget) const override
Initialize string parameters for a widget.
Resize box (normally at bottom-right of a window)
Definition: widget_type.h:68
Pressed (inset) panel, most commonly used as combo box text area.
Definition: widget_type.h:51
The viewport on this waypoint.
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
Close box (at top-left of a window)
Definition: widget_type.h:69
Representation of a waypoint.
Definition: waypoint_base.h:18
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
void OnResize() override
Called after the window got resized.
Stuff related to the text buffer GUI.
void InitializeViewport(Window *w, uint32 follow_flags, ZoomLevel zoom)
Initialize the viewport of the window.
Definition: widget.cpp:1927
Center the main view on this waypoint.
WaypointWindow(WindowDesc *desc, WindowNumber window_number)
Construct the window.
Nested widget to display a viewport in a window.
Definition: widget_type.h:575
Show the vehicles visiting this waypoint.
void CreateNestedTree(bool fill_nested=true)
Perform the first part of the initialization of a nested widget tree.
Definition: window.cpp:1812
Functions related to the vehicle&#39;s GUIs.
Functions, definitions and such used only by the GUI.
void ShowWaypointWindow(const Waypoint *wp)
Show the window for the given waypoint.
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:910
Functions related to (drawing on) viewports.
Data structure for an opened window.
Definition: window_gui.h:278
bool _ctrl_pressed
Is Ctrl pressed?
Definition: gfx.cpp:37
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition: window.cpp:1828
static NWidgetPart SetPadding(uint8 top, uint8 right, uint8 bottom, uint8 left)
Widget part function for setting additional space around a widget.
Definition: widget_type.h:1046
enable the &#39;Default&#39; button ("\0" is returned)
Definition: textbuf_gui.h:23
The tile has no ownership.
Definition: company_type.h:27
Waypoint view; Window numbers:
Definition: window_type.h:352
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX) ...
Definition: widget_type.h:65
static NWidgetPart SetDataTip(uint32 data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1014
static NWidgetPart SetMinimalSize(int16 x, int16 y)
Widget part function for setting the minimal size.
Definition: widget_type.h:947
Types related to the waypoint widgets.
Definition of base types and functions in a cross-platform compatible way.
TileIndex GetCenterTile() const
Get the center tile.
Definition: tilearea_type.h:59
the length of the string is counted in characters
Definition: textbuf_gui.h:24
A number of safeguards to prevent using unsafe methods.
Normal push-button (no toggle button) with text caption.
Definition: widget_type.h:104
Base of waypoints.
Simple depressed panel.
Definition: widget_type.h:50
Represents the covered area of e.g.
Definition: tilearea_type.h:18
void OnQueryTextFinished(char *str) override
The query window opened from this window has closed.
static NWidgetPart NWidget(WidgetType tp, Colours col, int16 idx=-1)
Widget part function for starting a new &#39;real&#39; widget.
Definition: widget_type.h:1114
bool DoCommandP(const CommandContainer *container, bool my_cmd)
Shortcut for the long DoCommandP when having a container with the data.
Definition: command.cpp:534
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
Rename this waypoint.
bool ScrollWindowToTile(TileIndex tile, Window *w, bool instant)
Scrolls the viewport in a window to a given location.
Definition: viewport.cpp:2406
rename a waypoint
Definition: command_type.h:196
void ShowExtraViewPortWindow(TileIndex tile=INVALID_TILE)
Show a new Extra Viewport window.
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:40
void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
Delete a window by its class and window number (if it is open).
Definition: window.cpp:1146
Functions related to companies.
Both numeric and alphabetic and spaces and stuff.
Definition: string_type.h:29
static WindowDesc _waypoint_view_desc(WDP_AUTO, "view_waypoint", 260, 118, WC_WAYPOINT_VIEW, WC_NONE, 0, _nested_waypoint_view_widgets, lengthof(_nested_waypoint_view_widgets))
The description of the waypoint view.
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:61
GUI for accessing waypoints and buoys.
Default zoom level for viewports.
Definition: zoom_type.h:35
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
static const NWidgetPart _nested_waypoint_view_widgets[]
The widgets of the waypoint view.
TileIndex xy
Base tile of the station.
static NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME, WWT_INSET, or WWT_PANEL).
Definition: widget_type.h:999
TileIndex GetCenterTile() const
Get the center tile of the waypoint.
bool ScrollMainWindowToTile(TileIndex tile, bool instant)
Scrolls the viewport of the main window to a given location.
Definition: viewport.cpp:2417
Functions related to commands.
Coordinates of a point in 2D.
Owner owner
The owner of this station.
static WindowClass GetWindowClassForVehicleType(VehicleType vt)
Get WindowClass for vehicle list of given vehicle type.
Definition: vehicle_gui.h:93
Window does not do autoscroll,.
Definition: window_gui.h:241
Owner owner
The owner of the content shown in this window. Company colour is acquired from this variable...
Definition: window_gui.h:326
static const uint MAX_LENGTH_STATION_NAME_CHARS
The maximum length of a station name in characters including &#39;\0&#39;.
Definition: station_type.h:89
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition: widget_type.h:66
static NWidgetPart SetFill(uint fill_x, uint fill_y)
Widget part function for setting filling.
Definition: widget_type.h:983
#define CMD_MSG(x)
Used to combine a StringID with the command.
Definition: command_type.h:370
int32 WindowNumber
Number to differentiate different windows of the same class.
Definition: window_type.h:707
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:314
Waypoint * wp
Waypoint displayed by the window.
Window functions not directly related to making/drawing windows.
Find a place automatically.
Definition: window_gui.h:156
VehicleType vt
Vehicle type using the waypoint.
ViewportData * viewport
Pointer to viewport data, if present.
Definition: window_gui.h:328
GUI functions that shouldn&#39;t be here.
static Waypoint * Get(size_t index)
Gets station with given index.
StringID string_id
Default name (town area) of station.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition: widget_type.h:64
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:201