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/>.
10 /** @file waypoint_gui.cpp Handling of waypoints gui. */
13 #include "window_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"
26 #include "table/strings.h"
28 /** Widget definitions for the waypoint window. */
29 enum WaypointWindowWidgets
{
37 /** GUI for accessing waypoints and buoys. */
38 struct WaypointWindow
: Window
{
40 VehicleType vt
; ///< Vehicle type using the waypoint.
41 Waypoint
*wp
; ///< Waypoint displayed by the window.
44 * Get the center tile of the waypoint.
45 * @return The center tile if the waypoint exists, otherwise the tile with the waypoint name.
47 TileIndex
GetCenterTile() const
49 if (!this->wp
->IsInUse()) return this->wp
->xy
;
52 this->wp
->GetTileArea(&ta
, this->vt
== VEH_TRAIN
? STATION_WAYPOINT
: STATION_BUOY
);
53 return ta
.GetCenterTile();
58 * Construct the window.
59 * @param desc The description of the window.
60 * @param window_number The window number, in this case the waypoint's ID.
62 WaypointWindow(const WindowDesc
*desc
, WindowNumber window_number
) : Window()
64 this->wp
= Waypoint::Get(window_number
);
65 this->vt
= (wp
->string_id
== STR_SV_STNAME_WAYPOINT
) ? VEH_TRAIN
: VEH_SHIP
;
67 this->CreateNestedTree(desc
);
68 if (this->vt
== VEH_TRAIN
) {
69 this->GetWidget
<NWidgetCore
>(WAYPVW_SHOW_VEHICLES
)->SetDataTip(STR_TRAIN
, STR_STATION_VIEW_SCHEDULED_TRAINS_TOOLTIP
);
70 this->GetWidget
<NWidgetCore
>(WAYPVW_CENTERVIEW
)->tool_tip
= STR_WAYPOINT_VIEW_CENTER_TOOLTIP
;
71 this->GetWidget
<NWidgetCore
>(WAYPVW_RENAME
)->tool_tip
= STR_WAYPOINT_VIEW_CHANGE_WAYPOINT_NAME
;
73 this->FinishInitNested(desc
, window_number
);
75 if (this->wp
->owner
!= OWNER_NONE
) this->owner
= this->wp
->owner
;
76 this->flags4
|= WF_DISABLE_VP_SCROLL
;
78 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WAYPVW_VIEWPORT
);
79 nvp
->InitializeViewport(this, this->GetCenterTile(), ZOOM_LVL_MIN
);
81 this->OnInvalidateData(0);
86 Owner owner
= this->owner
;
88 /* Buoys have no owner and can be used by everyone. Show only 'our' vehicles */
89 if (!Company::IsValidID(owner
)) owner
= _local_company
;
91 /* Well, spectators otoh */
92 if (Company::IsValidID(owner
)) DeleteWindowById(GetWindowClassForVehicleType(this->vt
), VehicleListIdentifier(VL_STATION_LIST
, this->vt
, owner
, this->window_number
).Pack(), false);
95 virtual void SetStringParameters(int widget
) const
97 if (widget
== WAYPVW_CAPTION
) SetDParam(0, this->wp
->index
);
100 virtual void OnClick(Point pt
, int widget
, int click_count
)
103 case WAYPVW_CENTERVIEW
: // scroll to location
105 ShowExtraViewPortWindow(this->GetCenterTile());
107 ScrollMainWindowToTile(this->GetCenterTile());
111 case WAYPVW_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
);
116 case WAYPVW_SHOW_VEHICLES
: // show list of vehicles having this waypoint in their orders
117 ShowVehicleListWindow(this->owner
, this->vt
, this->wp
->index
);
123 * Some data on this window has become invalid.
124 * @param data Information about the changed data.
125 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
127 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
129 if (!gui_scope
) return;
130 /* You can only change your own waypoints */
131 this->SetWidgetDisabledState(WAYPVW_RENAME
, !this->wp
->IsInUse() || (this->wp
->owner
!= _local_company
&& this->wp
->owner
!= OWNER_NONE
));
132 /* Disable the widget for waypoints with no use */
133 this->SetWidgetDisabledState(WAYPVW_SHOW_VEHICLES
, !this->wp
->IsInUse());
135 ScrollWindowToTile(this->GetCenterTile(), this, true);
138 virtual void OnResize()
140 if (this->viewport
!= NULL
) {
141 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WAYPVW_VIEWPORT
);
142 nvp
->UpdateViewportCoordinates(this);
143 this->wp
->UpdateVirtCoord();
145 ScrollWindowToTile(this->GetCenterTile(), this, true); // Re-center viewport.
149 virtual void OnQueryTextFinished(char *str
)
151 if (str
== NULL
) return;
153 DoCommandP(0, this->window_number
, 0, CMD_RENAME_WAYPOINT
| CMD_MSG(STR_ERROR_CAN_T_CHANGE_WAYPOINT_NAME
), NULL
, str
);
158 /** The widgets of the waypoint view. */
159 static const NWidgetPart _nested_waypoint_view_widgets
[] = {
160 NWidget(NWID_HORIZONTAL
),
161 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
162 NWidget(WWT_CAPTION
, COLOUR_GREY
, WAYPVW_CAPTION
), SetDataTip(STR_WAYPOINT_VIEW_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
163 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
164 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
166 NWidget(WWT_PANEL
, COLOUR_GREY
),
167 NWidget(WWT_INSET
, COLOUR_GREY
), SetPadding(2, 2, 2, 2),
168 NWidget(NWID_VIEWPORT
, COLOUR_GREY
, WAYPVW_VIEWPORT
), SetMinimalSize(256, 88), SetPadding(1, 1, 1, 1), SetResize(1, 1),
171 NWidget(NWID_HORIZONTAL
),
172 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WAYPVW_CENTERVIEW
), SetMinimalSize(100, 12), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_BUTTON_LOCATION
, STR_BUOY_VIEW_CENTER_TOOLTIP
),
173 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WAYPVW_RENAME
), SetMinimalSize(100, 12), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_BUTTON_RENAME
, STR_BUOY_VIEW_CHANGE_BUOY_NAME
),
174 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WAYPVW_SHOW_VEHICLES
), SetMinimalSize(15, 12), SetDataTip(STR_SHIP
, STR_STATION_VIEW_SCHEDULED_SHIPS_TOOLTIP
),
175 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
179 /** The description of the waypoint view. */
180 static const WindowDesc
_waypoint_view_desc(
182 WC_WAYPOINT_VIEW
, WC_NONE
,
184 _nested_waypoint_view_widgets
, lengthof(_nested_waypoint_view_widgets
)
188 * Show the window for the given waypoint.
189 * @param wp The waypoint to show the window for.
191 void ShowWaypointWindow(const Waypoint
*wp
)
193 AllocateWindowDescFront
<WaypointWindow
>(&_waypoint_view_desc
, wp
->index
);