viewport_gui.cpp

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

Generated on Tue Sep 14 17:06:57 2010 for OpenTTD by  doxygen 1.6.1