waypoint_gui.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "window_gui.h"
00014 #include "gui.h"
00015 #include "textbuf_gui.h"
00016 #include "vehiclelist.h"
00017 #include "vehicle_gui.h"
00018 #include "viewport_func.h"
00019 #include "strings_func.h"
00020 #include "command_func.h"
00021 #include "company_func.h"
00022 #include "company_base.h"
00023 #include "window_func.h"
00024 #include "waypoint_base.h"
00025 #include "departures_gui.h"
00026 
00027 #include "table/strings.h"
00028 
00030 enum WaypointWindowWidgets {
00031   WAYPVW_CAPTION,
00032   WAYPVW_VIEWPORT,
00033   WAYPVW_CENTERVIEW,
00034   WAYPVW_RENAME,
00035   WAYPVW_SHOW_VEHICLES,
00036   WAYPVW_DEPARTURES,
00037 };
00038 
00040 struct WaypointWindow : Window {
00041 private:
00042   VehicleType vt; 
00043   Waypoint *wp;   
00044 
00049   TileIndex GetCenterTile() const
00050   {
00051     if (!this->wp->IsInUse()) return this->wp->xy;
00052 
00053     TileArea ta;
00054     this->wp->GetTileArea(&ta, this->vt == VEH_TRAIN ? STATION_WAYPOINT : STATION_BUOY);
00055     return ta.GetCenterTile();
00056   }
00057 
00058 public:
00064   WaypointWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
00065   {
00066     this->wp = Waypoint::Get(window_number);
00067     this->vt = (wp->string_id == STR_SV_STNAME_WAYPOINT) ? VEH_TRAIN : VEH_SHIP;
00068 
00069     this->CreateNestedTree(desc);
00070     if (this->vt == VEH_TRAIN) {
00071       this->GetWidget<NWidgetCore>(WAYPVW_SHOW_VEHICLES)->SetDataTip(STR_TRAIN, STR_STATION_VIEW_SCHEDULED_TRAINS_TOOLTIP);
00072       this->GetWidget<NWidgetCore>(WAYPVW_CENTERVIEW)->tool_tip = STR_WAYPOINT_VIEW_CENTER_TOOLTIP;
00073       this->GetWidget<NWidgetCore>(WAYPVW_RENAME)->tool_tip = STR_WAYPOINT_VIEW_CHANGE_WAYPOINT_NAME;
00074     }
00075     this->FinishInitNested(desc, window_number);
00076 
00077     if (this->wp->owner != OWNER_NONE) this->owner = this->wp->owner;
00078     this->flags4 |= WF_DISABLE_VP_SCROLL;
00079 
00080     NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WAYPVW_VIEWPORT);
00081     nvp->InitializeViewport(this, this->GetCenterTile(), ZOOM_LVL_MIN);
00082 
00083     this->OnInvalidateData(0);
00084   }
00085 
00086   ~WaypointWindow()
00087   {
00088     Owner owner = this->owner;
00089 
00090     /* Buoys have no owner and can be used by everyone. Show only 'our' vehicles */
00091     if (!Company::IsValidID(owner)) owner = _local_company;
00092 
00093     /* Well, spectators otoh */
00094     if (Company::IsValidID(owner)) DeleteWindowById(GetWindowClassForVehicleType(this->vt), VehicleListIdentifier(VL_STATION_LIST, this->vt, owner, this->window_number).Pack(), false);
00095   }
00096 
00097   virtual void SetStringParameters(int widget) const
00098   {
00099     if (widget == WAYPVW_CAPTION) SetDParam(0, this->wp->index);
00100   }
00101 
00102   virtual void OnClick(Point pt, int widget, int click_count)
00103   {
00104     switch (widget) {
00105       case WAYPVW_CENTERVIEW: // scroll to location
00106         if (_ctrl_pressed) {
00107           ShowExtraViewPortWindow(this->GetCenterTile());
00108         } else {
00109           ScrollMainWindowToTile(this->GetCenterTile());
00110         }
00111         break;
00112 
00113       case WAYPVW_RENAME: // rename
00114         SetDParam(0, this->wp->index);
00115         ShowQueryString(STR_WAYPOINT_NAME, STR_EDIT_WAYPOINT_NAME, MAX_LENGTH_STATION_NAME_CHARS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
00116         break;
00117 
00118       case WAYPVW_SHOW_VEHICLES: // show list of vehicles having this waypoint in their orders
00119         ShowVehicleListWindow(this->owner, this->vt, this->wp->index);
00120         break;
00121 
00122       case WAYPVW_DEPARTURES: // show departure times of vehicles
00123         ShowWaypointDepartures((StationID)this->wp->index);
00124         break;
00125     }
00126   }
00127 
00133   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00134   {
00135     if (!gui_scope) return;
00136     /* You can only change your own waypoints */
00137     this->SetWidgetDisabledState(WAYPVW_RENAME, !this->wp->IsInUse() || (this->wp->owner != _local_company && this->wp->owner != OWNER_NONE));
00138     /* Disable the widget for waypoints with no use */
00139     this->SetWidgetDisabledState(WAYPVW_SHOW_VEHICLES, !this->wp->IsInUse());
00140 
00141     ScrollWindowToTile(this->GetCenterTile(), this, true);
00142   }
00143 
00144   virtual void OnResize()
00145   {
00146     if (this->viewport != NULL) {
00147       NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WAYPVW_VIEWPORT);
00148       nvp->UpdateViewportCoordinates(this);
00149       this->wp->UpdateVirtCoord();
00150 
00151       ScrollWindowToTile(this->GetCenterTile(), this, true); // Re-center viewport.
00152     }
00153   }
00154 
00155   virtual void OnQueryTextFinished(char *str)
00156   {
00157     if (str == NULL) return;
00158 
00159     DoCommandP(0, this->window_number, 0, CMD_RENAME_WAYPOINT | CMD_MSG(STR_ERROR_CAN_T_CHANGE_WAYPOINT_NAME), NULL, str);
00160   }
00161 
00162 };
00163 
00165 static const NWidgetPart _nested_waypoint_view_widgets[] = {
00166   NWidget(NWID_HORIZONTAL),
00167     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00168     NWidget(WWT_CAPTION, COLOUR_GREY, WAYPVW_CAPTION), SetDataTip(STR_WAYPOINT_VIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00169     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00170     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00171   EndContainer(),
00172   NWidget(WWT_PANEL, COLOUR_GREY),
00173     NWidget(WWT_INSET, COLOUR_GREY), SetPadding(2, 2, 2, 2),
00174       NWidget(NWID_VIEWPORT, COLOUR_GREY, WAYPVW_VIEWPORT), SetMinimalSize(256, 88), SetPadding(1, 1, 1, 1), SetResize(1, 1),
00175     EndContainer(),
00176   EndContainer(),
00177   NWidget(NWID_HORIZONTAL),
00178     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),
00179     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),
00180     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WAYPVW_DEPARTURES), SetMinimalSize(100, 12), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_STATION_VIEW_DEPARTURES_BUTTON, STR_STATION_VIEW_DEPARTURES_TOOLTIP),
00181     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WAYPVW_SHOW_VEHICLES), SetMinimalSize(15, 12), SetDataTip(STR_SHIP, STR_STATION_VIEW_SCHEDULED_SHIPS_TOOLTIP),
00182     NWidget(WWT_RESIZEBOX, COLOUR_GREY),
00183   EndContainer(),
00184 };
00185 
00187 static const WindowDesc _waypoint_view_desc(
00188   WDP_AUTO, 260, 118,
00189   WC_WAYPOINT_VIEW, WC_NONE,
00190   WDF_UNCLICK_BUTTONS,
00191   _nested_waypoint_view_widgets, lengthof(_nested_waypoint_view_widgets)
00192 );
00193 
00198 void ShowWaypointWindow(const Waypoint *wp)
00199 {
00200   AllocateWindowDescFront<WaypointWindow>(&_waypoint_view_desc, wp->index);
00201 }