# airport_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: airport_gui.cpp 18324 2009-11-28 15:01:49Z rubidium $ */
    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 "station_gui.h"
    00015 #include "terraform_gui.h"
    00016 #include "airport.h"
    00017 #include "sound_func.h"
    00018 #include "window_func.h"
    00019 #include "strings_func.h"
    00020 #include "viewport_func.h"
    00021 #include "gfx_func.h"
    00022 #include "company_func.h"
    00023 #include "tilehighlight_func.h"
    00024 #include "company_base.h"
    00025
    00026 #include "table/sprites.h"
    00027 #include "table/strings.h"
    00028
    00029 static byte _selected_airport_type;
    00030
    00031 static void ShowBuildAirportPicker(Window *parent);
    00032
    00033
    00034 void CcBuildAirport(bool success, TileIndex tile, uint32 p1, uint32 p2)
    00035 {
    00036   if (success) {
    00037     SndPlayTileFx(SND_1F_SPLAT, tile);
    00038     if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
    00039   }
    00040 }
    00041
    00042 static void PlaceAirport(TileIndex tile)
    00043 {
    00044   uint32 p2 = _ctrl_pressed;
    00045   SB(p2, 16, 16, INVALID_STATION); // no station to join
    00046
    00047   CommandContainer cmdcont = { tile, _selected_airport_type, p2, CMD_BUILD_AIRPORT | CMD_MSG(STR_ERROR_CAN_T_BUILD_AIRPORT_HERE), CcBuildAirport, "" };
    00048   ShowSelectStationIfNeeded(cmdcont, TileArea(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE));
    00049 }
    00050
    00052 enum {
    00053   ATW_AIRPORT,
    00054   ATW_DEMOLISH,
    00055 };
    00056
    00057
    00058 static void BuildAirClick_Airport(Window *w)
    00059 {
    00060   if (HandlePlacePushButton(w, ATW_AIRPORT, SPR_CURSOR_AIRPORT, HT_RECT, PlaceAirport)) ShowBuildAirportPicker(w);
    00061 }
    00062
    00063 static void BuildAirClick_Demolish(Window *w)
    00064 {
    00065   HandlePlacePushButton(w, ATW_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT, PlaceProc_DemolishArea);
    00066 }
    00067
    00068
    00069 typedef void OnButtonClick(Window *w);
    00070 static OnButtonClick * const _build_air_button_proc[] = {
    00071   BuildAirClick_Airport,
    00072   BuildAirClick_Demolish,
    00073 };
    00074
    00075 struct BuildAirToolbarWindow : Window {
    00076   BuildAirToolbarWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
    00077   {
    00078     this->InitNested(desc, window_number);
    00079     if (_settings_client.gui.link_terraform_toolbar) ShowTerraformToolbar(this);
    00080   }
    00081
    00082   ~BuildAirToolbarWindow()
    00083   {
    00084     if (_settings_client.gui.link_terraform_toolbar) DeleteWindowById(WC_SCEN_LAND_GEN, 0, false);
    00085   }
    00086
    00087   virtual void OnPaint()
    00088   {
    00089     this->DrawWidgets();
    00090   }
    00091
    00092   virtual void OnClick(Point pt, int widget)
    00093   {
    00094     if (!IsInsideBS(widget, ATW_AIRPORT, lengthof(_build_air_button_proc))) return;
    00095
    00096     _build_air_button_proc[widget - ATW_AIRPORT](this);
    00097   }
    00098
    00099
    00100   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    00101   {
    00102     switch (keycode) {
    00103       case '1': BuildAirClick_Airport(this); break;
    00104       case '2': BuildAirClick_Demolish(this); break;
    00105       default: return ES_NOT_HANDLED;
    00106     }
    00107     return ES_HANDLED;
    00108   }
    00109
    00110   virtual void OnPlaceObject(Point pt, TileIndex tile)
    00111   {
    00112     _place_proc(tile);
    00113   }
    00114
    00115   virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
    00116   {
    00117     VpSelectTilesWithMethod(pt.x, pt.y, select_method);
    00118   }
    00119
    00120   virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
    00121   {
    00122     if (pt.x != -1 && select_proc == DDSP_DEMOLISH_AREA) {
    00123       GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
    00124     }
    00125   }
    00126
    00127   virtual void OnPlaceObjectAbort()
    00128   {
    00129     this->RaiseButtons();
    00130
    00131     DeleteWindowById(WC_BUILD_STATION, TRANSPORT_AIR);
    00132     DeleteWindowById(WC_SELECT_STATION, 0);
    00133   }
    00134 };
    00135
    00136 static const NWidgetPart _nested_air_toolbar_widgets[] = {
    00137   NWidget(NWID_HORIZONTAL),
    00138     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    00139     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_TOOLBAR_AIRCRAFT_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00140     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
    00141   EndContainer(),
    00142   NWidget(NWID_HORIZONTAL),
    00143     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, ATW_AIRPORT), SetFill(0, 1), SetMinimalSize(42, 22), SetDataTip(SPR_IMG_AIRPORT, STR_TOOLBAR_AIRCRAFT_BUILD_AIRPORT_TOOLTIP),
    00144     NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetMinimalSize(4, 22), SetFill(1, 1), EndContainer(),
    00145     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, ATW_DEMOLISH), SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
    00146   EndContainer(),
    00147 };
    00148
    00149 static const WindowDesc _air_toolbar_desc(
    00150   WDP_ALIGN_TOOLBAR, 0, 0,
    00151   WC_BUILD_TOOLBAR, WC_NONE,
    00152   WDF_CONSTRUCTION,
    00153   _nested_air_toolbar_widgets, lengthof(_nested_air_toolbar_widgets)
    00154 );
    00155
    00156 void ShowBuildAirToolbar()
    00157 {
    00158   if (!Company::IsValidID(_local_company)) return;
    00159
    00160   DeleteWindowByClass(WC_BUILD_TOOLBAR);
    00161   AllocateWindowDescFront<BuildAirToolbarWindow>(&_air_toolbar_desc, TRANSPORT_AIR);
    00162 }
    00163
    00165 enum AirportPickerWidgets {
    00166   /* Airport selection buttons. */
    00167   BAW_SMALL_AIRPORT,
    00168   BAW_CITY_AIRPORT,
    00169   BAW_HELIPORT,
    00170   BAW_METRO_AIRPORT,
    00171   BAW_INTERNATIONAL_AIRPORT,
    00172   BAW_COMMUTER_AIRPORT,
    00173   BAW_HELIDEPOT,
    00174   BAW_INTERCONTINENTAL_AIRPORT,
    00175   BAW_HELISTATION,
    00176
    00177   BAW_BOTTOMPANEL,
    00178   BAW_BTN_DONTHILIGHT,
    00179   BAW_BTN_DOHILIGHT,
    00180
    00181   BAW_LAST_AIRPORT = BAW_HELISTATION,
    00182   BAW_AIRPORT_COUNT = BAW_LAST_AIRPORT - BAW_SMALL_AIRPORT + 1,
    00183 };
    00184
    00185 class AirportPickerWindow : public PickerWindowBase {
    00186 public:
    00187   AirportPickerWindow(const WindowDesc *desc, Window *parent) : PickerWindowBase(parent)
    00188   {
    00189     this->InitNested(desc, TRANSPORT_AIR);
    00190     this->SetWidgetLoweredState(BAW_BTN_DONTHILIGHT, !_settings_client.gui.station_show_coverage);
    00191     this->SetWidgetLoweredState(BAW_BTN_DOHILIGHT, _settings_client.gui.station_show_coverage);
    00192     this->OnInvalidateData();
    00193     this->SelectOtherAirport(_selected_airport_type);
    00194   }
    00195
    00196   virtual ~AirportPickerWindow()
    00197   {
    00198     DeleteWindowById(WC_SELECT_STATION, 0);
    00199   }
    00200
    00201   virtual void OnPaint()
    00202   {
    00203     this->DrawWidgets();
    00204
    00205     const AirportFTAClass *airport = GetAirport(_selected_airport_type);
    00206     int rad = _settings_game.station.modified_catchment ? airport->catchment : (uint)CA_UNMODIFIED;
    00207
    00208     uint16 top = this->GetWidget<NWidgetBase>(BAW_BTN_DOHILIGHT)->pos_y + this->GetWidget<NWidgetBase>(BAW_BTN_DOHILIGHT)->current_y + WD_PAR_VSEP_NORMAL;
    00209     NWidgetBase *panel_nwi = this->GetWidget<NWidgetBase>(BAW_BOTTOMPANEL);
    00210     int right = panel_nwi->pos_x +  panel_nwi->current_x;
    00211     int bottom = panel_nwi->pos_y +  panel_nwi->current_y;
    00212     /* only show the station (airport) noise, if the noise option is activated */
    00213     if (_settings_game.economy.station_noise_level) {
    00214       /* show the noise of the selected airport */
    00215       SetDParam(0, airport->noise_level);
    00216       DrawString(panel_nwi->pos_x + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, top, STR_STATION_BUILD_NOISE);
    00217       top += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
    00218     }
    00219
    00220     /* strings such as 'Size' and 'Coverage Area' */
    00221     top = DrawStationCoverageAreaText(panel_nwi->pos_x + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, top, SCT_ALL, rad, false) + WD_PAR_VSEP_NORMAL;
    00222     top = DrawStationCoverageAreaText(panel_nwi->pos_x + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, top, SCT_ALL, rad, true) + WD_PAR_VSEP_NORMAL;
    00223     /* Resize background if the text is not equally long as the window. */
    00224     if (top > bottom || (top < bottom && panel_nwi->current_y > panel_nwi->smallest_y)) {
    00225       ResizeWindow(this, 0, top - bottom);
    00226     }
    00227   }
    00228
    00229   void SelectOtherAirport(byte airport_id)
    00230   {
    00231     this->RaiseWidget(_selected_airport_type + BAW_SMALL_AIRPORT);
    00232     _selected_airport_type = airport_id;
    00233     this->LowerWidget(airport_id + BAW_SMALL_AIRPORT);
    00234
    00235     const AirportFTAClass *airport = GetAirport(airport_id);
    00236     SetTileSelectSize(airport->size_x, airport->size_y);
    00237
    00238     int rad = _settings_game.station.modified_catchment ? airport->catchment : (uint)CA_UNMODIFIED;
    00239     if (_settings_client.gui.station_show_coverage) SetTileSelectBigSize(-rad, -rad, 2 * rad, 2 * rad);
    00240
    00241     this->SetDirty();
    00242   }
    00243
    00244   virtual void OnInvalidateData(int data = 0)
    00245   {
    00246     if (!GetAirport(_selected_airport_type)->IsAvailable()) {
    00247       for (int i = 0; i < BAW_AIRPORT_COUNT; i++) {
    00248         if (GetAirport(i)->IsAvailable()) {
    00249           this->SelectOtherAirport(i);
    00250           break;
    00251         }
    00252       }
    00253     }
    00254     for (int i = 0; i < BAW_AIRPORT_COUNT; i++) {
    00255       this->SetWidgetDisabledState(i + BAW_SMALL_AIRPORT, !GetAirport(i)->IsAvailable());
    00256     }
    00257   }
    00258
    00259   virtual void OnClick(Point pt, int widget)
    00260   {
    00261     switch (widget) {
    00262       case BAW_SMALL_AIRPORT: case BAW_CITY_AIRPORT: case BAW_HELIPORT: case BAW_METRO_AIRPORT:
    00263       case BAW_INTERNATIONAL_AIRPORT: case BAW_COMMUTER_AIRPORT: case BAW_HELIDEPOT:
    00264       case BAW_INTERCONTINENTAL_AIRPORT: case BAW_HELISTATION:
    00265         this->SelectOtherAirport(widget - BAW_SMALL_AIRPORT);
    00266         SndPlayFx(SND_15_BEEP);
    00267         DeleteWindowById(WC_SELECT_STATION, 0);
    00268         break;
    00269
    00270       case BAW_BTN_DONTHILIGHT: case BAW_BTN_DOHILIGHT:
    00271         _settings_client.gui.station_show_coverage = (widget != BAW_BTN_DONTHILIGHT);
    00272         this->SetWidgetLoweredState(BAW_BTN_DONTHILIGHT, !_settings_client.gui.station_show_coverage);
    00273         this->SetWidgetLoweredState(BAW_BTN_DOHILIGHT, _settings_client.gui.station_show_coverage);
    00274         SndPlayFx(SND_15_BEEP);
    00275         this->SelectOtherAirport(_selected_airport_type);
    00276         break;
    00277     }
    00278   }
    00279
    00280   virtual void OnTick()
    00281   {
    00282     CheckRedrawStationCoverage(this);
    00283   }
    00284 };
    00285
    00286 static const NWidgetPart _nested_build_airport_widgets[] = {
    00287   NWidget(NWID_HORIZONTAL),
    00288     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    00289     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_STATION_BUILD_AIRPORT_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00290   EndContainer(),
    00291   /* Small airports. */
    00292   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
    00293     NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetFill(1, 0), SetDataTip(STR_STATION_BUILD_AIRPORT_SMALL_AIRPORTS, STR_NULL),
    00294     NWidget(NWID_HORIZONTAL),
    00295       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
    00296       NWidget(NWID_VERTICAL),
    00297         NWidget(WWT_TEXTBTN, COLOUR_GREY, BAW_SMALL_AIRPORT), SetMinimalSize(144, 12), SetFill(1, 0),
    00298                   SetDataTip(STR_STATION_BUILD_AIRPORT_SMALL_AIRPORT, STR_STATION_BUILD_AIRPORT_TOOLTIP),
    00299         NWidget(WWT_TEXTBTN, COLOUR_GREY, BAW_COMMUTER_AIRPORT), SetMinimalSize(144, 12), SetFill(1, 0),
    00300                   SetDataTip(STR_STATION_BUILD_AIRPORT_COMMUTER_AIRPORT, STR_STATION_BUILD_AIRPORT_TOOLTIP),
    00301         NWidget(NWID_SPACER), SetMinimalSize(0, 1), SetFill(1, 0),
    00302       EndContainer(),
    00303       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
    00304     EndContainer(),
    00305   EndContainer(),
    00306   /* Large airports. */
    00307   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
    00308     NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetFill(1, 0), SetDataTip(STR_STATION_BUILD_AIRPORT_LARGE_AIRPORTS, STR_NULL),
    00309     NWidget(NWID_HORIZONTAL),
    00310       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
    00311       NWidget(NWID_VERTICAL),
    00312         NWidget(WWT_TEXTBTN, COLOUR_GREY, BAW_CITY_AIRPORT), SetMinimalSize(144, 12), SetFill(1, 0),
    00313                   SetDataTip(STR_STATION_BUILD_AIRPORT_CITY_AIRPORT, STR_STATION_BUILD_AIRPORT_TOOLTIP),
    00314         NWidget(WWT_TEXTBTN, COLOUR_GREY, BAW_METRO_AIRPORT), SetMinimalSize(144, 12), SetFill(1, 0),
    00315                   SetDataTip(STR_STATION_BUILD_AIRPORT_METRO_AIRPORT, STR_STATION_BUILD_AIRPORT_TOOLTIP),
    00316         NWidget(NWID_SPACER), SetMinimalSize(0, 1), SetFill(1, 0),
    00317       EndContainer(),
    00318       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
    00319     EndContainer(),
    00320   EndContainer(),
    00321   /* Hub airports. */
    00322   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
    00323     NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetFill(1, 0), SetDataTip(STR_STATION_BUILD_AIRPORT_HUB_AIRPORTS, STR_NULL),
    00324     NWidget(NWID_HORIZONTAL),
    00325       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
    00326       NWidget(NWID_VERTICAL),
    00327         NWidget(WWT_TEXTBTN, COLOUR_GREY, BAW_INTERNATIONAL_AIRPORT), SetMinimalSize(144, 12), SetFill(1, 0),
    00328                   SetDataTip(STR_STATION_BUILD_AIRPORT_INTERNATIONAL_AIRPORT, STR_STATION_BUILD_AIRPORT_TOOLTIP),
    00329         NWidget(WWT_TEXTBTN, COLOUR_GREY, BAW_INTERCONTINENTAL_AIRPORT), SetMinimalSize(144, 12), SetFill(1, 0),
    00330                   SetDataTip(STR_STATION_BUILD_AIRPORT_INTERCONTINENTAL_AIRPORT, STR_STATION_BUILD_AIRPORT_TOOLTIP),
    00331         NWidget(NWID_SPACER), SetMinimalSize(0, 1), SetFill(1, 0),
    00332       EndContainer(),
    00333       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
    00334     EndContainer(),
    00335   EndContainer(),
    00336   /* Heliports. */
    00337   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
    00338     NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetFill(1, 0), SetDataTip(STR_STATION_BUILD_AIRPORT_HELIPORTS, STR_NULL),
    00339     NWidget(NWID_HORIZONTAL),
    00340       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
    00341       NWidget(NWID_VERTICAL),
    00342         NWidget(WWT_TEXTBTN, COLOUR_GREY, BAW_HELIPORT), SetMinimalSize(144, 12), SetFill(1, 0),
    00343                   SetDataTip(STR_STATION_BUILD_AIRPORT_HELIPORT, STR_STATION_BUILD_AIRPORT_TOOLTIP),
    00344         NWidget(WWT_TEXTBTN, COLOUR_GREY, BAW_HELISTATION), SetMinimalSize(144, 12), SetFill(1, 0),
    00345                   SetDataTip(STR_STATION_BUILD_AIRPORT_HELISTATION, STR_STATION_BUILD_AIRPORT_TOOLTIP),
    00346         NWidget(WWT_TEXTBTN, COLOUR_GREY, BAW_HELIDEPOT), SetMinimalSize(144, 12), SetFill(1, 0),
    00347                   SetDataTip(STR_STATION_BUILD_AIRPORT_HELIDEPOT, STR_STATION_BUILD_AIRPORT_TOOLTIP),
    00348         NWidget(NWID_SPACER), SetMinimalSize(0, 1), SetFill(1, 0),
    00349       EndContainer(),
    00350       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
    00351     EndContainer(),
    00352   EndContainer(),
    00353   /* Bottom panel. */
    00354   NWidget(WWT_PANEL, COLOUR_DARK_GREEN, BAW_BOTTOMPANEL),
    00355     NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetFill(1, 0), SetDataTip(STR_STATION_BUILD_COVERAGE_AREA_TITLE, STR_NULL),
    00356     NWidget(NWID_HORIZONTAL),
    00357       NWidget(NWID_SPACER), SetMinimalSize(14, 0),
    00358       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00359         NWidget(WWT_TEXTBTN, COLOUR_GREY, BAW_BTN_DONTHILIGHT), SetMinimalSize(60, 12), SetFill(1, 0),
    00360                       SetDataTip(STR_STATION_BUILD_COVERAGE_OFF, STR_STATION_BUILD_COVERAGE_AREA_OFF_TOOLTIP),
    00361         NWidget(WWT_TEXTBTN, COLOUR_GREY, BAW_BTN_DOHILIGHT), SetMinimalSize(60, 12), SetFill(1, 0),
    00362                       SetDataTip(STR_STATION_BUILD_COVERAGE_ON, STR_STATION_BUILD_COVERAGE_AREA_ON_TOOLTIP),
    00363       EndContainer(),
    00364       NWidget(NWID_SPACER), SetMinimalSize(14, 0),
    00365     EndContainer(),
    00366     NWidget(NWID_SPACER), SetMinimalSize(0, 10), SetResize(0, 1), SetFill(1, 0),
    00367   EndContainer(),
    00368 };
    00369
    00370 static const WindowDesc _build_airport_desc(
    00371   WDP_AUTO, 0, 0,
    00372   WC_BUILD_STATION, WC_BUILD_TOOLBAR,
    00373   WDF_CONSTRUCTION,
    00374   _nested_build_airport_widgets, lengthof(_nested_build_airport_widgets)
    00375 );
    00376
    00377 static void ShowBuildAirportPicker(Window *parent)
    00378 {
    00379   new AirportPickerWindow(&_build_airport_desc, parent);
    00380 }
    00381
    00382 void InitializeAirportGui()
    00383 {
    00384   _selected_airport_type = AT_SMALL;
    00385 }