Codefix: Documentation comment in IndustryDirectoryWindow (#13059)
[openttd-github.git] / src / viewport_gui.cpp
blobdd3d6fb59fbc46a6a9203669c0fea544da2ec28e
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 */
8 /** @file viewport_gui.cpp Extra viewport window. */
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"
18 #include "widgets/viewport_widget.h"
20 #include "table/strings.h"
21 #include "table/sprites.h"
23 #include "safeguards.h"
25 /* Extra Viewport Window Stuff */
26 static constexpr 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(),
53 class ExtraViewportWindow : public Window {
54 public:
55 ExtraViewportWindow(WindowDesc &desc, int window_number, TileIndex tile) : Window(desc)
57 this->InitNested(window_number);
59 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_EV_VIEWPORT);
60 nvp->InitializeViewport(this, tile, ScaleZoomGUI(ZOOM_LVL_VIEWPORT));
61 if (_settings_client.gui.zoom_min == viewport->zoom) this->DisableWidget(WID_EV_ZOOM_IN);
64 void SetStringParameters(WidgetID widget) const override
66 switch (widget) {
67 case WID_EV_CAPTION:
68 /* set the number in the title bar */
69 SetDParam(0, this->window_number + 1);
70 break;
74 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
76 switch (widget) {
77 case WID_EV_ZOOM_IN: DoZoomInOutWindow(ZOOM_IN, this); break;
78 case WID_EV_ZOOM_OUT: DoZoomInOutWindow(ZOOM_OUT, this); break;
80 case WID_EV_MAIN_TO_VIEW: { // location button (move main view to same spot as this view) 'Paste Location'
81 Window *w = GetMainWindow();
82 int x = this->viewport->scrollpos_x; // Where is the main looking at
83 int y = this->viewport->scrollpos_y;
85 /* set this view to same location. Based on the center, adjusting for zoom */
86 w->viewport->dest_scrollpos_x = x - (w->viewport->virtual_width - this->viewport->virtual_width) / 2;
87 w->viewport->dest_scrollpos_y = y - (w->viewport->virtual_height - this->viewport->virtual_height) / 2;
88 w->viewport->follow_vehicle = INVALID_VEHICLE;
89 break;
92 case WID_EV_VIEW_TO_MAIN: { // inverse location button (move this view to same spot as main view) 'Copy Location'
93 const Window *w = GetMainWindow();
94 int x = w->viewport->scrollpos_x;
95 int y = w->viewport->scrollpos_y;
97 this->viewport->dest_scrollpos_x = x + (w->viewport->virtual_width - this->viewport->virtual_width) / 2;
98 this->viewport->dest_scrollpos_y = y + (w->viewport->virtual_height - this->viewport->virtual_height) / 2;
99 break;
104 void OnResize() override
106 if (this->viewport != nullptr) {
107 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_EV_VIEWPORT);
108 nvp->UpdateViewportCoordinates(this);
112 void OnScroll(Point delta) override
114 this->viewport->scrollpos_x += ScaleByZoom(delta.x, this->viewport->zoom);
115 this->viewport->scrollpos_y += ScaleByZoom(delta.y, this->viewport->zoom);
116 this->viewport->dest_scrollpos_x = this->viewport->scrollpos_x;
117 this->viewport->dest_scrollpos_y = this->viewport->scrollpos_y;
120 bool OnRightClick([[maybe_unused]] Point pt, WidgetID widget) override
122 return widget == WID_EV_VIEWPORT;
125 void OnMouseWheel(int wheel) override
127 if (_settings_client.gui.scrollwheel_scrolling != SWS_OFF) {
128 ZoomInOrOutToCursorWindow(wheel < 0, this);
133 * Some data on this window has become invalid.
134 * @param data Information about the changed data.
135 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
137 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
139 if (!gui_scope) return;
140 /* Only handle zoom message if intended for us (msg ZOOM_IN/ZOOM_OUT) */
141 HandleZoomMessage(this, this->viewport, WID_EV_ZOOM_IN, WID_EV_ZOOM_OUT);
145 static WindowDesc _extra_viewport_desc(
146 WDP_AUTO, "extra_viewport", 300, 268,
147 WC_EXTRA_VIEWPORT, WC_NONE,
149 _nested_extra_viewport_widgets
153 * Show a new Extra Viewport window.
154 * @param tile Tile to center the view on. INVALID_TILE means to use the center of main viewport.
156 void ShowExtraViewportWindow(TileIndex tile)
158 int i = 0;
160 /* find next free window number for extra viewport */
161 while (FindWindowById(WC_EXTRA_VIEWPORT, i) != nullptr) i++;
163 new ExtraViewportWindow(_extra_viewport_desc, i, tile);
167 * Show a new Extra Viewport window.
168 * Center it on the tile under the cursor, if the cursor is inside a viewport.
169 * If that fails, center it on main viewport center.
171 void ShowExtraViewportWindowForTileUnderCursor()
173 /* Use tile under mouse as center for new viewport.
174 * Do this before creating the window, it might appear just below the mouse. */
175 Point pt = GetTileBelowCursor();
176 ShowExtraViewportWindow(pt.x != -1 ? TileVirtXY(pt.x, pt.y) : INVALID_TILE);