viewport_gui.cpp

Go to the documentation of this file.
00001 /* $Id: viewport_gui.cpp 24700 2012-11-11 16:10:43Z frosch $ */
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 "landscape.h"
00014 #include "window_gui.h"
00015 #include "viewport_func.h"
00016 #include "strings_func.h"
00017 #include "zoom_func.h"
00018 #include "window_func.h"
00019 
00020 #include "widgets/viewport_widget.h"
00021 
00022 #include "table/strings.h"
00023 #include "table/sprites.h"
00024 
00025 /* Extra ViewPort Window Stuff */
00026 static const NWidgetPart _nested_extra_view_port_widgets[] = {
00027   NWidget(NWID_HORIZONTAL),
00028     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00029     NWidget(WWT_CAPTION, COLOUR_GREY, WID_EV_CAPTION), SetDataTip(STR_EXTRA_VIEW_PORT_TITLE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00030     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00031     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00032   EndContainer(),
00033   NWidget(WWT_PANEL, COLOUR_GREY),
00034     NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_EV_VIEWPORT), SetPadding(2, 2, 2, 2), SetResize(1, 1), SetFill(1, 1),
00035   EndContainer(),
00036   NWidget(NWID_HORIZONTAL),
00037     NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_EV_ZOOM_IN), SetDataTip(SPR_IMG_ZOOMIN, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_IN),
00038     NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_EV_ZOOM_OUT), SetDataTip(SPR_IMG_ZOOMOUT, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_OUT),
00039     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00040       NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_EV_MAIN_TO_VIEW), SetFill(1, 1), SetResize(1, 0),
00041                     SetDataTip(STR_EXTRA_VIEW_MOVE_MAIN_TO_VIEW, STR_EXTRA_VIEW_MOVE_MAIN_TO_VIEW_TT),
00042       NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_EV_VIEW_TO_MAIN), SetFill(1, 1), SetResize(1, 0),
00043                     SetDataTip(STR_EXTRA_VIEW_MOVE_VIEW_TO_MAIN, STR_EXTRA_VIEW_MOVE_VIEW_TO_MAIN_TT),
00044     EndContainer(),
00045   EndContainer(),
00046   NWidget(NWID_HORIZONTAL),
00047     NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), SetResize(1, 0), EndContainer(),
00048     NWidget(WWT_RESIZEBOX, COLOUR_GREY),
00049   EndContainer(),
00050 };
00051 
00052 class ExtraViewportWindow : public Window {
00053 public:
00054   ExtraViewportWindow(const WindowDesc *desc, int window_number, TileIndex tile) : Window()
00055   {
00056     this->InitNested(desc, window_number);
00057 
00058     NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_EV_VIEWPORT);
00059     nvp->InitializeViewport(this, 0, ZOOM_LVL_VIEWPORT);
00060     if (_settings_client.gui.zoom_min == ZOOM_LVL_VIEWPORT) this->DisableWidget(WID_EV_ZOOM_IN);
00061 
00062     Point pt;
00063     if (tile == INVALID_TILE) {
00064       /* No tile? Use center of main viewport. */
00065       const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
00066 
00067       /* center on same place as main window (zoom is maximum, no adjustment needed) */
00068       pt.x = w->viewport->scrollpos_x + w->viewport->virtual_width / 2;
00069       pt.y = w->viewport->scrollpos_y + w->viewport->virtual_height / 2;
00070     } else {
00071       pt = RemapCoords(TileX(tile) * TILE_SIZE + TILE_SIZE / 2, TileY(tile) * TILE_SIZE + TILE_SIZE / 2, TileHeight(tile));
00072     }
00073 
00074     this->viewport->scrollpos_x = pt.x - this->viewport->virtual_width / 2;
00075     this->viewport->scrollpos_y = pt.y - this->viewport->virtual_height / 2;
00076     this->viewport->dest_scrollpos_x = this->viewport->scrollpos_x;
00077     this->viewport->dest_scrollpos_y = this->viewport->scrollpos_y;
00078   }
00079 
00080   virtual void SetStringParameters(int widget) const
00081   {
00082     switch (widget) {
00083       case WID_EV_CAPTION:
00084         /* set the number in the title bar */
00085         SetDParam(0, this->window_number + 1);
00086         break;
00087     }
00088   }
00089 
00090   virtual void OnClick(Point pt, int widget, int click_count)
00091   {
00092     switch (widget) {
00093       case WID_EV_ZOOM_IN: DoZoomInOutWindow(ZOOM_IN,  this); break;
00094       case WID_EV_ZOOM_OUT: DoZoomInOutWindow(ZOOM_OUT, this); break;
00095 
00096       case WID_EV_MAIN_TO_VIEW: { // location button (move main view to same spot as this view) 'Paste Location'
00097         Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
00098         int x = this->viewport->scrollpos_x; // Where is the main looking at
00099         int y = this->viewport->scrollpos_y;
00100 
00101         /* set this view to same location. Based on the center, adjusting for zoom */
00102         w->viewport->dest_scrollpos_x =  x - (w->viewport->virtual_width -  this->viewport->virtual_width) / 2;
00103         w->viewport->dest_scrollpos_y =  y - (w->viewport->virtual_height - this->viewport->virtual_height) / 2;
00104         w->viewport->follow_vehicle   = INVALID_VEHICLE;
00105         break;
00106       }
00107 
00108       case WID_EV_VIEW_TO_MAIN: { // inverse location button (move this view to same spot as main view) 'Copy Location'
00109         const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
00110         int x = w->viewport->scrollpos_x;
00111         int y = w->viewport->scrollpos_y;
00112 
00113         this->viewport->dest_scrollpos_x =  x + (w->viewport->virtual_width -  this->viewport->virtual_width) / 2;
00114         this->viewport->dest_scrollpos_y =  y + (w->viewport->virtual_height - this->viewport->virtual_height) / 2;
00115         break;
00116       }
00117     }
00118   }
00119 
00120   virtual void OnResize()
00121   {
00122     if (this->viewport != NULL) {
00123       NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_EV_VIEWPORT);
00124       nvp->UpdateViewportCoordinates(this);
00125     }
00126   }
00127 
00128   virtual void OnScroll(Point delta)
00129   {
00130     this->viewport->scrollpos_x += ScaleByZoom(delta.x, this->viewport->zoom);
00131     this->viewport->scrollpos_y += ScaleByZoom(delta.y, this->viewport->zoom);
00132     this->viewport->dest_scrollpos_x = this->viewport->scrollpos_x;
00133     this->viewport->dest_scrollpos_y = this->viewport->scrollpos_y;
00134   }
00135 
00136   virtual void OnMouseWheel(int wheel)
00137   {
00138     if (_settings_client.gui.scrollwheel_scrolling == 0) {
00139       ZoomInOrOutToCursorWindow(wheel < 0, this);
00140     }
00141   }
00142 
00148   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00149   {
00150     if (!gui_scope) return;
00151     /* Only handle zoom message if intended for us (msg ZOOM_IN/ZOOM_OUT) */
00152     HandleZoomMessage(this, this->viewport, WID_EV_ZOOM_IN, WID_EV_ZOOM_OUT);
00153   }
00154 };
00155 
00156 static const WindowDesc _extra_view_port_desc(
00157   WDP_AUTO, 300, 268,
00158   WC_EXTRA_VIEW_PORT, WC_NONE,
00159   0,
00160   _nested_extra_view_port_widgets, lengthof(_nested_extra_view_port_widgets)
00161 );
00162 
00167 void ShowExtraViewPortWindow(TileIndex tile)
00168 {
00169   int i = 0;
00170 
00171   /* find next free window number for extra viewport */
00172   while (FindWindowById(WC_EXTRA_VIEW_PORT, i) != NULL) i++;
00173 
00174   new ExtraViewportWindow(&_extra_view_port_desc, i, tile);
00175 }
00176 
00182 void ShowExtraViewPortWindowForTileUnderCursor()
00183 {
00184   /* Use tile under mouse as center for new viewport.
00185    * Do this before creating the window, it might appear just below the mouse. */
00186   Point pt = GetTileBelowCursor();
00187   ShowExtraViewPortWindow(pt.x != -1 ? TileVirtXY(pt.x, pt.y) : INVALID_TILE);
00188 }