OpenTTD Source: src/viewport_gui.cpp Source File

OpenTTD Source 13.2.1

viewport_gui.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10 #include "stdafx.h"
11 #include "landscape.h"
12 #include "window_gui.h"
13 #include "viewport_func.h"
14 #include "strings_func.h"
15 #include "zoom_func.h"
16 #include "window_func.h"
17
18 #include "widgets/viewport_widget.h"
19
20 #include "table/strings.h"
21 #include "table/sprites.h"
22
23 #include "safeguards.h"
24
25 /* Extra Viewport Window Stuff */
26 static const NWidgetPart _nested_extra_viewport_widgets[] = {
27 NWidget(NWID_HORIZONTAL),
28 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
29 NWidget(WWT_CAPTION, COLOUR_GREY, WID_EV_CAPTION), SetDataTip(STR_EXTRA_VIEWPORT_TITLE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
30 NWidget(WWT_SHADEBOX, COLOUR_GREY),
31 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
32 NWidget(WWT_STICKYBOX, COLOUR_GREY),
33 EndContainer(),
34 NWidget(WWT_PANEL, COLOUR_GREY),
35 NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_EV_VIEWPORT), SetPadding(2, 2, 2, 2), SetResize(1, 1), SetFill(1, 1),
36 EndContainer(),
37 NWidget(NWID_HORIZONTAL),
38 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_EV_ZOOM_IN), SetDataTip(SPR_IMG_ZOOMIN, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_IN),
39 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_EV_ZOOM_OUT), SetDataTip(SPR_IMG_ZOOMOUT, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_OUT),
40 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
41 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_EV_MAIN_TO_VIEW), SetFill(1, 1), SetResize(1, 0),
42 SetDataTip(STR_EXTRA_VIEW_MOVE_MAIN_TO_VIEW, STR_EXTRA_VIEW_MOVE_MAIN_TO_VIEW_TT),
43 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_EV_VIEW_TO_MAIN), SetFill(1, 1), SetResize(1, 0),
44 SetDataTip(STR_EXTRA_VIEW_MOVE_VIEW_TO_MAIN, STR_EXTRA_VIEW_MOVE_VIEW_TO_MAIN_TT),
45 EndContainer(),
46 EndContainer(),
47 NWidget(NWID_HORIZONTAL),
48 NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), SetResize(1, 0), EndContainer(),
49 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
50 EndContainer(),
51 };
52
53 class ExtraViewportWindow : public Window {
54 public:
55 ExtraViewportWindow(WindowDesc *desc, int window_number, TileIndex tile) : Window(desc)
56 {
57 this->InitNested(window_number);
58
59 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_EV_VIEWPORT);
60 nvp->InitializeViewport(this, 0, ScaleZoomGUI(ZOOM_LVL_VIEWPORT));
61 if (_settings_client.gui.zoom_min == viewport->zoom) this->DisableWidget(WID_EV_ZOOM_IN);
62
63 Point pt;
64 if (tile == INVALID_TILE) {
65 /* No tile? Use center of main viewport. */
66 const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
67
68 /* center on same place as main window (zoom is maximum, no adjustment needed) */
69 pt.x = w->viewport->scrollpos_x + w->viewport->virtual_width / 2;
70 pt.y = w->viewport->scrollpos_y + w->viewport->virtual_height / 2;
71 } else {
72 pt = RemapCoords(TileX(tile) * TILE_SIZE + TILE_SIZE / 2, TileY(tile) * TILE_SIZE + TILE_SIZE / 2, TilePixelHeight(tile));
73 }
74
75 this->viewport->scrollpos_x = pt.x - this->viewport->virtual_width / 2;
76 this->viewport->scrollpos_y = pt.y - this->viewport->virtual_height / 2;
77 this->viewport->dest_scrollpos_x = this->viewport->scrollpos_x;
78 this->viewport->dest_scrollpos_y = this->viewport->scrollpos_y;
79 }
80
81 void SetStringParameters(int widget) const override
82 {
83 switch (widget) {
84 case WID_EV_CAPTION:
85 /* set the number in the title bar */
86 SetDParam(0, this->window_number + 1);
87 break;
88 }
89 }
90
91 void OnClick(Point pt, int widget, int click_count) override
92 {
93 switch (widget) {
94 case WID_EV_ZOOM_IN: DoZoomInOutWindow(ZOOM_IN, this); break;
95 case WID_EV_ZOOM_OUT: DoZoomInOutWindow(ZOOM_OUT, this); break;
96
97 case WID_EV_MAIN_TO_VIEW: { // location button (move main view to same spot as this view) 'Paste Location'
98 Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
99 int x = this->viewport->scrollpos_x; // Where is the main looking at
100 int y = this->viewport->scrollpos_y;
101
102 /* set this view to same location. Based on the center, adjusting for zoom */
103 w->viewport->dest_scrollpos_x = x - (w->viewport->virtual_width - this->viewport->virtual_width) / 2;
104 w->viewport->dest_scrollpos_y = y - (w->viewport->virtual_height - this->viewport->virtual_height) / 2;
105 w->viewport->follow_vehicle = INVALID_VEHICLE;
106 break;
107 }
108
109 case WID_EV_VIEW_TO_MAIN: { // inverse location button (move this view to same spot as main view) 'Copy Location'
110 const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
111 int x = w->viewport->scrollpos_x;
112 int y = w->viewport->scrollpos_y;
113
114 this->viewport->dest_scrollpos_x = x + (w->viewport->virtual_width - this->viewport->virtual_width) / 2;
115 this->viewport->dest_scrollpos_y = y + (w->viewport->virtual_height - this->viewport->virtual_height) / 2;
116 break;
117 }
118 }
119 }
120
121 void OnResize() override
122 {
123 if (this->viewport != nullptr) {
124 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_EV_VIEWPORT);
125 nvp->UpdateViewportCoordinates(this);
126 }
127 }
128
129 void OnScroll(Point delta) override
130 {
131 this->viewport->scrollpos_x += ScaleByZoom(delta.x, this->viewport->zoom);
132 this->viewport->scrollpos_y += ScaleByZoom(delta.y, this->viewport->zoom);
133 this->viewport->dest_scrollpos_x = this->viewport->scrollpos_x;
134 this->viewport->dest_scrollpos_y = this->viewport->scrollpos_y;
135 }
136
137 void OnMouseWheel(int wheel) override
138 {
139 if (_settings_client.gui.scrollwheel_scrolling != 2) {
140 ZoomInOrOutToCursorWindow(wheel < 0, this);
141 }
142 }
143
149 void OnInvalidateData(int data = 0, bool gui_scope = true) override
150 {
151 if (!gui_scope) return;
152 /* Only handle zoom message if intended for us (msg ZOOM_IN/ZOOM_OUT) */
153 HandleZoomMessage(this, this->viewport, WID_EV_ZOOM_IN, WID_EV_ZOOM_OUT);
154 }
155 };
156
157 static WindowDesc _extra_viewport_desc(
158 WDP_AUTO, "extra_viewport", 300, 268,
159 WC_EXTRA_VIEWPORT, WC_NONE,
160 0,
161 _nested_extra_viewport_widgets, lengthof(_nested_extra_viewport_widgets)
162 );
163
168 void ShowExtraViewportWindow(TileIndex tile)
169 {
170 int i = 0;
171
172 /* find next free window number for extra viewport */
173 while (FindWindowById(WC_EXTRA_VIEWPORT, i) != nullptr) i++;
174
175 new ExtraViewportWindow(&_extra_viewport_desc, i, tile);
176 }
177
183 void ShowExtraViewportWindowForTileUnderCursor()
184 {
185 /* Use tile under mouse as center for new viewport.
186 * Do this before creating the window, it might appear just below the mouse. */
187 Point pt = GetTileBelowCursor();
188 ShowExtraViewportWindow(pt.x != -1 ? TileVirtXY(pt.x, pt.y) : INVALID_TILE);
189 }
ZOOM_OUT
@ ZOOM_OUT
Zoom out (get helicopter view).
Definition: viewport_type.h:74
WWT_STICKYBOX
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition: widget_type.h:64
SetPadding
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:1143
ExtraViewportWindow::OnMouseWheel
void OnMouseWheel(int wheel) override
The mouse wheel has been turned.
Definition: viewport_gui.cpp:137
NWidgetViewport
Nested widget to display a viewport in a window.
Definition: widget_type.h:622
WWT_CAPTION
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:59
TilePixelHeight
static uint TilePixelHeight(TileIndex tile)
Returns the height of a tile in pixels.
Definition: tile_map.h:72
RemapCoords
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:82
Window::viewport
ViewportData * viewport
Pointer to viewport data, if present.
Definition: window_gui.h:255
ViewportData::scrollpos_y
int32 scrollpos_y
Currently shown y coordinate (virtual screen coordinate of topleft corner of the viewport).
Definition: window_gui.h:195
WWT_DEFSIZEBOX
@ WWT_DEFSIZEBOX
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX)
Definition: widget_type.h:63
WID_EV_VIEW_TO_MAIN
@ WID_EV_VIEW_TO_MAIN
Center the main view on the view of this viewport.
Definition: viewport_widget.h:20
viewport_widget.h
NWID_HORIZONTAL
@ NWID_HORIZONTAL
Horizontal container.
Definition: widget_type.h:73
FindWindowById
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition: window.cpp:1161
INVALID_TILE
static constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:108
TileIndex
The index/ID of a Tile.
Definition: tile_type.h:85
DoZoomInOutWindow
bool DoZoomInOutWindow(ZoomStateChange how, Window *w)
Zooms a viewport in a window in or out.
Definition: main_gui.cpp:92
SetResize
static NWidgetPart SetResize(int16 dx, int16 dy)
Widget part function for setting the resize step.
Definition: widget_type.h:997
zoom_func.h
TILE_SIZE
static const uint TILE_SIZE
Tile size in world coordinates.
Definition: tile_type.h:15
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:53
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:215
WID_EV_ZOOM_OUT
@ WID_EV_ZOOM_OUT
Zoom out.
Definition: viewport_widget.h:18
Window::Window
Window(WindowDesc *desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition: window.cpp:1814
SetDParam
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:196
NWidgetPart
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:975
SetDataTip
static NWidgetPart SetDataTip(uint32 data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1111
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:205
WindowDesc
High level window description.
Definition: window_gui.h:102
window_gui.h
NC_EQUALSIZE
@ NC_EQUALSIZE
Value of the NCB_EQUALSIZE flag.
Definition: widget_type.h:469
ZOOM_IN
@ ZOOM_IN
Zoom in (get more detailed view).
Definition: viewport_type.h:73
GUISettings::scrollwheel_scrolling
uint8 scrollwheel_scrolling
scrolling using the scroll wheel?
Definition: settings_type.h:146
ExtraViewportWindow::OnResize
void OnResize() override
Called after the window got resized.
Definition: viewport_gui.cpp:121
WDP_AUTO
@ WDP_AUTO
Find a place automatically.
Definition: window_gui.h:90
NWidgetViewport::UpdateViewportCoordinates
void UpdateViewportCoordinates(Window *w)
Update the position and size of the viewport (after eg a resize).
Definition: widget.cpp:2331
Window::InitNested
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition: window.cpp:1804
NWID_VIEWPORT
@ NWID_VIEWPORT
Nested widget containing a viewport.
Definition: widget_type.h:79
WID_EV_VIEWPORT
@ WID_EV_VIEWPORT
The viewport.
Definition: viewport_widget.h:16
INVALID_VEHICLE
static const VehicleID INVALID_VEHICLE
Constant representing a non-existing vehicle.
Definition: vehicle_type.h:55
WWT_PUSHTXTBTN
@ WWT_PUSHTXTBTN
Normal push-button (no toggle button) with text caption.
Definition: widget_type.h:104
safeguards.h
ExtraViewportWindow::OnInvalidateData
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
Definition: viewport_gui.cpp:149
sprites.h
Viewport::virtual_width
int virtual_width
width << zoom
Definition: viewport_type.h:30
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
WC_EXTRA_VIEWPORT
@ WC_EXTRA_VIEWPORT
Extra viewport; Window numbers:
Definition: window_type.h:624
ViewportData::dest_scrollpos_y
int32 dest_scrollpos_y
Current destination y coordinate to display (virtual screen coordinate of topleft corner of the viewp...
Definition: window_gui.h:197
stdafx.h
ZOOM_LVL_VIEWPORT
@ ZOOM_LVL_VIEWPORT
Default zoom level for viewports.
Definition: zoom_type.h:33
Window::window_number
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:241
landscape.h
viewport_func.h
WC_NONE
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:38
ExtraViewportWindow::OnClick
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition: viewport_gui.cpp:91
WWT_CLOSEBOX
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition: widget_type.h:67
WWT_RESIZEBOX
@ WWT_RESIZEBOX
Resize box (normally at bottom-right of a window)
Definition: widget_type.h:66
GUISettings::zoom_min
ZoomLevel zoom_min
minimum zoom out level
Definition: settings_type.h:134
ExtraViewportWindow::OnScroll
void OnScroll(Point delta) override
Handle the request for (viewport) scrolling.
Definition: viewport_gui.cpp:129
ShowExtraViewportWindowForTileUnderCursor
void ShowExtraViewportWindowForTileUnderCursor()
Show a new Extra Viewport window.
Definition: viewport_gui.cpp:183
WWT_PUSHIMGBTN
@ WWT_PUSHIMGBTN
Normal push-button (no toggle button) with image caption.
Definition: widget_type.h:105
ViewportData::scrollpos_x
int32 scrollpos_x
Currently shown x coordinate (virtual screen coordinate of topleft corner of the viewport).
Definition: window_gui.h:194
EndContainer
static NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
Definition: widget_type.h:1096
strings_func.h
ScaleByZoom
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:22
WID_EV_CAPTION
@ WID_EV_CAPTION
Caption of window.
Definition: viewport_widget.h:15
ScaleZoomGUI
static ZoomLevel ScaleZoomGUI(ZoomLevel value)
Scale zoom level relative to GUI zoom.
Definition: zoom_func.h:87
NWidget
static NWidgetPart NWidget(WidgetType tp, Colours col, int16 idx=-1)
Widget part function for starting a new 'real' widget.
Definition: widget_type.h:1229
NWidgetViewport::InitializeViewport
void InitializeViewport(Window *w, uint32 follow_flags, ZoomLevel zoom)
Initialize the viewport of the window.
Definition: widget.cpp:2322
WWT_PANEL
@ WWT_PANEL
Simple depressed panel.
Definition: widget_type.h:48
WC_MAIN_WINDOW
@ WC_MAIN_WINDOW
Main window; Window numbers:
Definition: window_type.h:44
ViewportData::follow_vehicle
VehicleID follow_vehicle
VehicleID to follow if following a vehicle, INVALID_VEHICLE otherwise.
Definition: window_gui.h:193
window_func.h
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:386
Viewport::zoom
ZoomLevel zoom
The zoom level of the viewport.
Definition: viewport_type.h:33
ViewportData::dest_scrollpos_x
int32 dest_scrollpos_x
Current destination x coordinate to display (virtual screen coordinate of topleft corner of the viewp...
Definition: window_gui.h:196
SetFill
static NWidgetPart SetFill(uint fill_x, uint fill_y)
Widget part function for setting filling.
Definition: widget_type.h:1080
Window
Data structure for an opened window.
Definition: window_gui.h:213
Viewport::virtual_height
int virtual_height
height << zoom
Definition: viewport_type.h:31
HandleZoomMessage
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:485
Window::DisableWidget
void DisableWidget(byte widget_index)
Sets a widget to disabled.
Definition: window_gui.h:331
ExtraViewportWindow
Definition: viewport_gui.cpp:53
WID_EV_ZOOM_IN
@ WID_EV_ZOOM_IN
Zoom in.
Definition: viewport_widget.h:17
ExtraViewportWindow::SetStringParameters
void SetStringParameters(int widget) const override
Initialize string parameters for a widget.
Definition: viewport_gui.cpp:81
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:604
TileVirtXY
static TileIndex TileVirtXY(uint x, uint y)
Get a tile from the virtual XY-coordinate.
Definition: map_func.h:194
WID_EV_MAIN_TO_VIEW
@ WID_EV_MAIN_TO_VIEW
Center the view of this viewport on the main view.
Definition: viewport_widget.h:19
ShowExtraViewportWindow
void ShowExtraViewportWindow(TileIndex tile)
Show a new Extra Viewport window.
Definition: viewport_gui.cpp:168
WWT_SHADEBOX
@ WWT_SHADEBOX
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition: widget_type.h:62