OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

waypoint_gui.cpp
Go to the documentation of this file.
1 /* $Id: waypoint_gui.cpp 27030 2014-10-21 19:16:47Z rubidium $ */
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
26 #include "widgets/waypoint_widget.h"
27
28 #include "table/strings.h"
29
30 #include "safeguards.h"
31
33 struct WaypointWindow : Window {
34 private:
35 VehicleType vt;
36 Waypoint *wp;
37
42 TileIndex GetCenterTile() const
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:
57 WaypointWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
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);
74 nvp->InitializeViewport(this, this->GetCenterTile(), ZOOM_LVL_VIEWPORT);
75
76 this->OnInvalidateData(0);
77 }
78
79 ~WaypointWindow()
80 {
81 DeleteWindowById(GetWindowClassForVehicleType(this->vt), VehicleListIdentifier(VL_STATION_LIST, this->vt, this->owner, this->window_number).Pack(), false);
82 }
83
84 virtual void SetStringParameters(int widget) const
85 {
86 if (widget == WID_W_CAPTION) SetDParam(0, this->wp->index);
87 }
88
89 virtual void OnClick(Point pt, int widget, int click_count)
90 {
91 switch (widget) {
92 case WID_W_CENTER_VIEW: // scroll to location
93 if (_ctrl_pressed) {
94 ShowExtraViewPortWindow(this->GetCenterTile());
95 } else {
96 ScrollMainWindowToTile(this->GetCenterTile());
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 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
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 */
122 this->SetWidgetDisabledState(WID_W_SHOW_VEHICLES, !this->wp->IsInUse());
123
124 ScrollWindowToTile(this->GetCenterTile(), this, true);
125 }
126
127 virtual void OnResize()
128 {
129 if (this->viewport != NULL) {
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 virtual void OnQueryTextFinished(char *str)
139 {
140 if (str == NULL) return;
141
142 DoCommandP(0, this->window_number, 0, CMD_RENAME_WAYPOINT | CMD_MSG(STR_ERROR_CAN_T_CHANGE_WAYPOINT_NAME), NULL, str);
143 }
144
145 };
146
148 static const NWidgetPart _nested_waypoint_view_widgets[] = {
149 NWidget(NWID_HORIZONTAL),
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(),
161 NWidget(NWID_HORIZONTAL),
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
170 static WindowDesc _waypoint_view_desc(
171 WDP_AUTO, "view_waypoint", 260, 118,
172 WC_WAYPOINT_VIEW, WC_NONE,
173 0,
174 _nested_waypoint_view_widgets, lengthof(_nested_waypoint_view_widgets)
175 );
176
181 void ShowWaypointWindow(const Waypoint *wp)
182 {
183 AllocateWindowDescFront<WaypointWindow>(&_waypoint_view_desc, wp->index);
184 }