OpenTTD Source: src/viewport_gui.cpp Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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 */
26static constexpr std::initializer_list<NWidgetPart> _nested_extra_viewport_widgets = {
27 NWidget(NWID_HORIZONTAL),
28 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
29 NWidget(WWT_CAPTION, COLOUR_GREY, WID_EV_CAPTION),
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), SetSpriteTip(SPR_IMG_ZOOMIN, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_IN),
39 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_EV_ZOOM_OUT), SetSpriteTip(SPR_IMG_ZOOMOUT, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_OUT),
40 NWidget(NWID_HORIZONTAL, NWidContainerFlag::EqualSize),
41 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_EV_MAIN_TO_VIEW), SetFill(1, 1), SetResize(1, 0),
42 SetStringTip(STR_EXTRA_VIEW_MOVE_MAIN_TO_VIEW, STR_EXTRA_VIEW_MOVE_MAIN_TO_VIEW_TOOLTIP),
43 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_EV_VIEW_TO_MAIN), SetFill(1, 1), SetResize(1, 0),
44 SetStringTip(STR_EXTRA_VIEW_MOVE_VIEW_TO_MAIN, STR_EXTRA_VIEW_MOVE_VIEW_TO_MAIN_TOOLTIP),
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 {
54public:
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, tile, ScaleZoomGUI(ZoomLevel::Viewport));
61 if (_settings_client.gui.zoom_min == viewport->zoom) this->DisableWidget(WID_EV_ZOOM_IN);
62 }
63
64 std::string GetWidgetString(WidgetID widget, StringID stringid) const override
65 {
66 switch (widget) {
67 case WID_EV_CAPTION:
68 /* set the number in the title bar */
69 return GetString(STR_EXTRA_VIEWPORT_TITLE, this->window_number + 1);
70
71 default:
72 return this->Window::GetWidgetString(widget, stringid);
73 }
74 }
75
76 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
77 {
78 switch (widget) {
79 case WID_EV_ZOOM_IN: DoZoomInOutWindow(ZOOM_IN, this); break;
80 case WID_EV_ZOOM_OUT: DoZoomInOutWindow(ZOOM_OUT, this); break;
81
82 case WID_EV_MAIN_TO_VIEW: { // location button (move main view to same spot as this view) 'Paste Location'
83 Window *w = GetMainWindow();
84 int x = this->viewport->scrollpos_x; // Where is the main looking at
85 int y = this->viewport->scrollpos_y;
86
87 /* set this view to same location. Based on the center, adjusting for zoom */
88 w->viewport->dest_scrollpos_x = x - (w->viewport->virtual_width - this->viewport->virtual_width) / 2;
89 w->viewport->dest_scrollpos_y = y - (w->viewport->virtual_height - this->viewport->virtual_height) / 2;
90 w->viewport->follow_vehicle = VehicleID::Invalid();
91 break;
92 }
93
94 case WID_EV_VIEW_TO_MAIN: { // inverse location button (move this view to same spot as main view) 'Copy Location'
95 const Window *w = GetMainWindow();
96 int x = w->viewport->scrollpos_x;
97 int y = w->viewport->scrollpos_y;
98
99 this->viewport->dest_scrollpos_x = x + (w->viewport->virtual_width - this->viewport->virtual_width) / 2;
100 this->viewport->dest_scrollpos_y = y + (w->viewport->virtual_height - this->viewport->virtual_height) / 2;
101 break;
102 }
103 }
104 }
105
106 void OnResize() override
107 {
108 if (this->viewport != nullptr) {
109 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_EV_VIEWPORT);
110 nvp->UpdateViewportCoordinates(this);
111 }
112 }
113
114 void OnScroll(Point delta) override
115 {
116 this->viewport->scrollpos_x += ScaleByZoom(delta.x, this->viewport->zoom);
117 this->viewport->scrollpos_y += ScaleByZoom(delta.y, this->viewport->zoom);
118 this->viewport->dest_scrollpos_x = this->viewport->scrollpos_x;
119 this->viewport->dest_scrollpos_y = this->viewport->scrollpos_y;
120 }
121
122 bool OnRightClick([[maybe_unused]] Point pt, WidgetID widget) override
123 {
124 return widget == WID_EV_VIEWPORT;
125 }
126
127 void OnMouseWheel(int wheel, WidgetID widget) override
128 {
129 if (widget != WID_EV_VIEWPORT) return;
130 if (_settings_client.gui.scrollwheel_scrolling != SWS_OFF) {
131 ZoomInOrOutToCursorWindow(wheel < 0, this);
132 }
133 }
134
140 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
141 {
142 if (!gui_scope) return;
143 /* Only handle zoom message if intended for us (msg ZOOM_IN/ZOOM_OUT) */
144 HandleZoomMessage(this, *this->viewport, WID_EV_ZOOM_IN, WID_EV_ZOOM_OUT);
145 }
146};
147
148static WindowDesc _extra_viewport_desc(
149 WDP_AUTO, "extra_viewport", 300, 268,
150 WC_EXTRA_VIEWPORT, WC_NONE,
151 {},
152 _nested_extra_viewport_widgets
153);
154
159 void ShowExtraViewportWindow(TileIndex tile)
160{
161 int i = 0;
162
163 /* find next free window number for extra viewport */
164 while (FindWindowById(WC_EXTRA_VIEWPORT, i) != nullptr) i++;
165
166 new ExtraViewportWindow(_extra_viewport_desc, i, tile);
167}
168
174 void ShowExtraViewportWindowForTileUnderCursor()
175{
176 /* Use tile under mouse as center for new viewport.
177 * Do this before creating the window, it might appear just below the mouse. */
178 Point pt = GetTileBelowCursor();
179 ShowExtraViewportWindow(pt.x != -1 ? TileVirtXY(pt.x, pt.y) : INVALID_TILE);
180}
ExtraViewportWindow
Definition viewport_gui.cpp:53
ExtraViewportWindow::OnMouseWheel
void OnMouseWheel(int wheel, WidgetID widget) override
The mouse wheel has been turned.
Definition viewport_gui.cpp:127
ExtraViewportWindow::OnScroll
void OnScroll(Point delta) override
Handle the request for (viewport) scrolling.
Definition viewport_gui.cpp:114
ExtraViewportWindow::GetWidgetString
std::string GetWidgetString(WidgetID widget, StringID stringid) const override
Get the raw string for a widget.
Definition viewport_gui.cpp:64
ExtraViewportWindow::OnInvalidateData
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
Definition viewport_gui.cpp:140
ExtraViewportWindow::OnRightClick
bool OnRightClick(Point pt, WidgetID widget) override
A click with the right mouse button has been made on the window.
Definition viewport_gui.cpp:122
ExtraViewportWindow::OnClick
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition viewport_gui.cpp:76
ExtraViewportWindow::OnResize
void OnResize() override
Called after the window got resized.
Definition viewport_gui.cpp:106
NWidgetViewport
Nested widget to display a viewport in a window.
Definition widget_type.h:697
NWidgetViewport::UpdateViewportCoordinates
void UpdateViewportCoordinates(Window *w)
Update the position and size of the viewport (after eg a resize).
Definition widget.cpp:2430
NWidgetViewport::InitializeViewport
void InitializeViewport(Window *w, std::variant< TileIndex, VehicleID > focus, ZoomLevel zoom)
Initialize the viewport of the window.
Definition widget.cpp:2421
SetFill
constexpr NWidgetPart SetFill(uint16_t fill_x, uint16_t fill_y)
Widget part function for setting filling.
Definition widget_type.h:1222
SetSpriteTip
constexpr NWidgetPart SetSpriteTip(SpriteID sprite, StringID tip={})
Widget part function for setting the sprite and tooltip.
Definition widget_type.h:1254
SetPadding
constexpr NWidgetPart SetPadding(uint8_t top, uint8_t right, uint8_t bottom, uint8_t left)
Widget part function for setting additional space around a widget.
Definition widget_type.h:1335
SetStringTip
constexpr NWidgetPart SetStringTip(StringID string, StringID tip={})
Widget part function for setting the string and tooltip.
Definition widget_type.h:1243
EndContainer
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
Definition widget_type.h:1232
NWidget
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=INVALID_WIDGET)
Widget part function for starting a new 'real' widget.
Definition widget_type.h:1427
SetResize
constexpr NWidgetPart SetResize(int16_t dx, int16_t dy)
Widget part function for setting the resize step.
Definition widget_type.h:1148
landscape.h
Functions related to OTTD's landscape.
DoZoomInOutWindow
bool DoZoomInOutWindow(ZoomStateChange how, Window *w)
Zooms a viewport in a window in or out.
Definition main_gui.cpp:93
TileVirtXY
static TileIndex TileVirtXY(uint x, uint y)
Get a tile from the virtual XY-coordinate.
Definition map_func.h:416
safeguards.h
A number of safeguards to prevent using unsafe methods.
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
SWS_OFF
@ SWS_OFF
Scroll wheel has no effect.
Definition settings_type.h:159
sprites.h
This file contains all sprite-related enums and defines.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
GetString
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:424
strings_func.h
Functions related to OTTD's strings.
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition settings_type.h:650
Coord2D< int >
Coord2D::y
T y
Y coordinate.
Definition geometry_type.hpp:35
Coord2D::x
T x
X coordinate.
Definition geometry_type.hpp:34
GUISettings::zoom_min
ZoomLevel zoom_min
minimum zoom out level
Definition settings_type.h:191
GUISettings::scrollwheel_scrolling
uint8_t scrollwheel_scrolling
scrolling using the scroll wheel?
Definition settings_type.h:203
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
WindowDesc
High level window description.
Definition window_gui.h:168
Window
Data structure for an opened window.
Definition window_gui.h:274
Window::viewport
std::unique_ptr< ViewportData > viewport
Pointer to viewport data, if present.
Definition window_gui.h:319
Window::GetWidgetString
virtual std::string GetWidgetString(WidgetID widget, StringID stringid) const
Get the raw string for a widget.
Definition window.cpp:506
Window::DisableWidget
void DisableWidget(WidgetID widget_index)
Sets a widget to disabled.
Definition window_gui.h:392
Window::InitNested
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition window.cpp:1823
Window::window_number
WindowNumber window_number
Window number within the window class.
Definition window_gui.h:303
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
HandleZoomMessage
void HandleZoomMessage(Window *w, const Viewport &vp, WidgetID widget_zoom_in, WidgetID widget_zoom_out)
Update the status of the zoom-buttons according to the zoom-level of the viewport.
Definition viewport.cpp:486
viewport_func.h
Functions related to (drawing on) viewports.
ShowExtraViewportWindowForTileUnderCursor
void ShowExtraViewportWindowForTileUnderCursor()
Show a new Extra Viewport window.
Definition viewport_gui.cpp:174
ShowExtraViewportWindow
void ShowExtraViewportWindow(TileIndex tile)
Show a new Extra Viewport window.
Definition viewport_gui.cpp:159
ZOOM_IN
@ ZOOM_IN
Zoom in (get more detailed view).
Definition viewport_type.h:81
ZOOM_OUT
@ ZOOM_OUT
Zoom out (get helicopter view).
Definition viewport_type.h:82
viewport_widget.h
Types related to the viewport widgets.
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
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
WID_EV_CAPTION
@ WID_EV_CAPTION
Caption of window.
Definition viewport_widget.h:15
WID_EV_ZOOM_OUT
@ WID_EV_ZOOM_OUT
Zoom out.
Definition viewport_widget.h:18
WID_EV_VIEWPORT
@ WID_EV_VIEWPORT
The viewport.
Definition viewport_widget.h:16
WID_EV_ZOOM_IN
@ WID_EV_ZOOM_IN
Zoom in.
Definition viewport_widget.h:17
WWT_PUSHTXTBTN
@ WWT_PUSHTXTBTN
Normal push-button (no toggle button) with text caption.
Definition widget_type.h:105
WWT_PUSHIMGBTN
@ WWT_PUSHIMGBTN
Normal push-button (no toggle button) with image caption.
Definition widget_type.h:106
NWID_HORIZONTAL
@ NWID_HORIZONTAL
Horizontal container.
Definition widget_type.h:66
WWT_PANEL
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:39
WWT_STICKYBOX
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition widget_type.h:57
WWT_SHADEBOX
@ WWT_SHADEBOX
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition widget_type.h:55
WWT_CAPTION
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition widget_type.h:52
WWT_CLOSEBOX
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition widget_type.h:60
WWT_RESIZEBOX
@ WWT_RESIZEBOX
Resize box (normally at bottom-right of a window)
Definition widget_type.h:59
WWT_DEFSIZEBOX
@ WWT_DEFSIZEBOX
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX)
Definition widget_type.h:56
NWID_VIEWPORT
@ NWID_VIEWPORT
Nested widget containing a viewport.
Definition widget_type.h:73
EqualSize
@ EqualSize
Containers should keep all their (resizing) children equally large.
Definition widget_type.h:544
GetMainWindow
Window * GetMainWindow()
Get the main window, i.e.
Definition window.cpp:1182
FindWindowById
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition window.cpp:1153
window_func.h
Window functions not directly related to making/drawing windows.
window_gui.h
Functions, definitions and such used only by the GUI.
WDP_AUTO
@ WDP_AUTO
Find a place automatically.
Definition window_gui.h:144
WidgetID
int WidgetID
Widget ID.
Definition window_type.h:20
WC_EXTRA_VIEWPORT
@ WC_EXTRA_VIEWPORT
Extra viewport; Window numbers:
Definition window_type.h:653
WC_NONE
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition window_type.h:50
zoom_func.h
Functions related to zooming.
ScaleByZoom
int ScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZoomLevel::Min) When shifting right,...
Definition zoom_func.h:22
ScaleZoomGUI
ZoomLevel ScaleZoomGUI(ZoomLevel value)
Scale zoom level relative to GUI zoom.
Definition zoom_func.h:87
ZoomLevel::Viewport
@ Viewport
Default zoom level for viewports.