Go to the documentation of this file.
1 /* $Id: viewport_gui.cpp 27743 2017-02-05 17:54:46Z frosch $ */
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 <>.
8  */
12 #include "stdafx.h"
13 #include "landscape.h"
14 #include "window_gui.h"
15 #include "viewport_func.h"
16 #include "strings_func.h"
17 #include "zoom_func.h"
18 #include "window_func.h"
22 #include "table/strings.h"
23 #include "table/sprites.h"
25 #include "safeguards.h"
27 /* Extra ViewPort Window Stuff */
28 static const NWidgetPart _nested_extra_view_port_widgets[] = {
35  EndContainer(),
37  NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_EV_VIEWPORT), SetPadding(2, 2, 2, 2), SetResize(1, 1), SetFill(1, 1),
38  EndContainer(),
43  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_EV_MAIN_TO_VIEW), SetFill(1, 1), SetResize(1, 0),
45  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_EV_VIEW_TO_MAIN), SetFill(1, 1), SetResize(1, 0),
47  EndContainer(),
48  EndContainer(),
50  NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), SetResize(1, 0), EndContainer(),
52  EndContainer(),
53 };
55 class ExtraViewportWindow : public Window {
56 public:
58  {
59  this->InitNested(window_number);
61  NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_EV_VIEWPORT);
65  Point pt;
66  if (tile == INVALID_TILE) {
67  /* No tile? Use center of main viewport. */
68  const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
70  /* center on same place as main window (zoom is maximum, no adjustment needed) */
71  pt.x = w->viewport->scrollpos_x + w->viewport->virtual_width / 2;
72  pt.y = w->viewport->scrollpos_y + w->viewport->virtual_height / 2;
73  } else {
74  pt = RemapCoords(TileX(tile) * TILE_SIZE + TILE_SIZE / 2, TileY(tile) * TILE_SIZE + TILE_SIZE / 2, TilePixelHeight(tile));
75  }
77  this->viewport->scrollpos_x = pt.x - this->viewport->virtual_width / 2;
78  this->viewport->scrollpos_y = pt.y - this->viewport->virtual_height / 2;
81  }
83  virtual void SetStringParameters(int widget) const
84  {
85  switch (widget) {
86  case WID_EV_CAPTION:
87  /* set the number in the title bar */
88  SetDParam(0, this->window_number + 1);
89  break;
90  }
91  }
93  virtual void OnClick(Point pt, int widget, int click_count)
94  {
95  switch (widget) {
96  case WID_EV_ZOOM_IN: DoZoomInOutWindow(ZOOM_IN, this); break;
97  case WID_EV_ZOOM_OUT: DoZoomInOutWindow(ZOOM_OUT, this); break;
99  case WID_EV_MAIN_TO_VIEW: { // location button (move main view to same spot as this view) 'Paste Location'
101  int x = this->viewport->scrollpos_x; // Where is the main looking at
102  int y = this->viewport->scrollpos_y;
104  /* set this view to same location. Based on the center, adjusting for zoom */
108  break;
109  }
111  case WID_EV_VIEW_TO_MAIN: { // inverse location button (move this view to same spot as main view) 'Copy Location'
112  const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
113  int x = w->viewport->scrollpos_x;
114  int y = w->viewport->scrollpos_y;
116  this->viewport->dest_scrollpos_x = x + (w->viewport->virtual_width - this->viewport->virtual_width) / 2;
118  break;
119  }
120  }
121  }
123  virtual void OnResize()
124  {
125  if (this->viewport != NULL) {
126  NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_EV_VIEWPORT);
127  nvp->UpdateViewportCoordinates(this);
128  }
129  }
131  virtual void OnScroll(Point delta)
132  {
133  this->viewport->scrollpos_x += ScaleByZoom(delta.x, this->viewport->zoom);
134  this->viewport->scrollpos_y += ScaleByZoom(delta.y, this->viewport->zoom);
137  }
139  virtual void OnMouseWheel(int wheel)
140  {
142  ZoomInOrOutToCursorWindow(wheel < 0, this);
143  }
144  }
151  virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
152  {
153  if (!gui_scope) return;
154  /* Only handle zoom message if intended for us (msg ZOOM_IN/ZOOM_OUT) */
156  }
157 };
159 static WindowDesc _extra_view_port_desc(
160  WDP_AUTO, "extra_viewport", 300, 268,
162  0,
163  _nested_extra_view_port_widgets, lengthof(_nested_extra_view_port_widgets)
164 );
171 {
172  int i = 0;
174  /* find next free window number for extra viewport */
175  while (FindWindowById(WC_EXTRA_VIEW_PORT, i) != NULL) i++;
177  new ExtraViewportWindow(&_extra_view_port_desc, i, tile);
178 }
186 {
187  /* Use tile under mouse as center for new viewport.
188  * Do this before creating the window, it might appear just below the mouse. */
189  Point pt = GetTileBelowCursor();
190  ShowExtraViewPortWindow(pt.x != -1 ? TileVirtXY(pt.x, pt.y) : INVALID_TILE);
191 }
Nested widget containing a viewport.
Definition: widget_type.h:81
bool DoZoomInOutWindow(ZoomStateChange how, Window *w)
Zooms a viewport in a window in or out.
Definition: main_gui.cpp:143
Functions related to OTTD&#39;s strings.
Normal push-button (no toggle button) with image caption.
Definition: widget_type.h:105
static NWidgetPart SetResize(int16 dx, int16 dy)
Widget part function for setting the resize step.
Definition: widget_type.h:930
Window(WindowDesc *desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition: window.cpp:1843
High level window description.
Definition: window_gui.h:168
Zoom out (get helicopter view).
Definition: viewport_type.h:64
void HandleZoomMessage(Window *w, const ViewPort *vp, byte widget_zoom_in, byte widget_zoom_out)
Update the status of the zoom-buttons according to the zoom-level of the viewport.
Definition: viewport.cpp:506
static Point RemapCoords(int x, int y, int z)
Map 3D world or tile coordinate to equivalent 2D coordinate as used in the viewports and smallmap...
Definition: landscape.h:83
void UpdateViewportCoordinates(Window *w)
Update the position and size of the viewport (after eg a resize).
Definition: widget.cpp:1936
Horizontal container.
Definition: widget_type.h:75
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition: window.cpp:1105
Resize box (normally at bottom-right of a window)
Definition: widget_type.h:68
int virtual_height
height << zoom
Definition: viewport_type.h:33
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:207
virtual void SetStringParameters(int widget) const
Initialize string parameters for a widget.
static int ScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZOOM_LVL_NORMAL) When shifting right...
Definition: zoom_func.h:24
Center the main view on the view of this viewport.
Close box (at top-left of a window)
Definition: widget_type.h:69
Zoom in (get more detailed view).
Definition: viewport_type.h:63
void InitializeViewport(Window *w, uint32 follow_flags, ZoomLevel zoom)
Initialize the viewport of the window.
Definition: widget.cpp:1927
void DisableWidget(byte widget_index)
Sets a widget to disabled.
Definition: window_gui.h:397
static const VehicleID INVALID_VEHICLE
Constant representing a non-existing vehicle.
Definition: vehicle_type.h:57
Nested widget to display a viewport in a window.
Definition: widget_type.h:575
static const uint TILE_SIZE
Tile size in world coordinates.
Definition: tile_type.h:15
Functions, definitions and such used only by the GUI.
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:910
Functions related to (drawing on) viewports.
void ShowExtraViewPortWindow(TileIndex tile)
Show a new Extra Viewport window.
Data structure for an opened window.
Definition: window_gui.h:271
static NWidgetPart SetPadding(uint8 top, uint8 right, uint8 bottom, uint8 left)
Widget part function for setting additional space around a widget.
Definition: widget_type.h:1046
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition: window.cpp:1833
Main window; Window numbers:
Definition: window_type.h:46
The viewport.
virtual void OnScroll(Point delta)
Handle the request for (viewport) scrolling.
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX) ...
Definition: widget_type.h:65
virtual void OnMouseWheel(int wheel)
The mouse wheel has been turned.
virtual void OnResize()
Called after the window got resized.
static NWidgetPart SetDataTip(uint32 data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1014
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:76
uint8 scrollwheel_scrolling
scrolling using the scroll wheel?
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
Normal push-button (no toggle button) with text caption.
Definition: widget_type.h:104
Simple depressed panel.
Definition: widget_type.h:50
int32 scrollpos_x
Currently shown x coordinate (virtual screen coordinate of topleft corner of the viewport).
Definition: window_gui.h:260
int virtual_width
width << zoom
Definition: viewport_type.h:32
static NWidgetPart NWidget(WidgetType tp, Colours col, int16 idx=-1)
Widget part function for starting a new &#39;real&#39; widget.
Definition: widget_type.h:1114
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
VehicleID follow_vehicle
VehicleID to follow if following a vehicle, INVALID_VEHICLE otherwise.
Definition: window_gui.h:259
virtual void OnClick(Point pt, int widget, int click_count)
A click with the left mouse button has been made on the window.
Default zoom level for viewports.
Definition: zoom_type.h:35
int32 dest_scrollpos_y
Current destination y coordinate to display (virtual screen coordinate of topleft corner of the viewp...
Definition: window_gui.h:263
virtual void OnInvalidateData(int data=0, bool gui_scope=true)
Some data on this window has become invalid.
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:40
ZoomLevelByte zoom_min
minimum zoom out level
static TileIndex TileVirtXY(uint x, uint y)
Get a tile from the virtual XY-coordinate.
Definition: map_func.h:196
int32 dest_scrollpos_x
Current destination x coordinate to display (virtual screen coordinate of topleft corner of the viewp...
Definition: window_gui.h:262
GUISettings gui
settings related to the GUI
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:61
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:217
Caption of window.
static NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME, WWT_INSET, or WWT_PANEL).
Definition: widget_type.h:999
Functions related to zooming.
Functions related to OTTD&#39;s landscape.
Coordinates of a point in 2D.
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition: widget_type.h:66
static NWidgetPart SetFill(uint fill_x, uint fill_y)
Widget part function for setting filling.
Definition: widget_type.h:983
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:85
Extra viewport; Window numbers:
Definition: window_type.h:626
void ShowExtraViewPortWindowForTileUnderCursor()
Show a new Extra Viewport window.
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:307
Window functions not directly related to making/drawing windows.
Find a place automatically.
Definition: window_gui.h:156
ViewportData * viewport
Pointer to viewport data, if present.
Definition: window_gui.h:321
Center the view of this viewport on the main view.
Types related to the viewport widgets.
int32 scrollpos_y
Currently shown y coordinate (virtual screen coordinate of topleft corner of the viewport).
Definition: window_gui.h:261
Value of the NCB_EQUALSIZE flag.
Definition: widget_type.h:429
This file contains all sprite-related enums and defines.
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition: widget_type.h:64
static uint TilePixelHeight(TileIndex tile)
Returns the height of a tile in pixels.
Definition: tile_map.h:64
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:201