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/>.
8 /** @file viewport_gui.cpp Extra viewport window. */
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 const NWidgetPart _nested_extra_view_port_widgets
[] = {
27 NWidget(NWID_HORIZONTAL
),
28 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
29 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_EV_CAPTION
), SetDataTip(STR_EXTRA_VIEW_PORT_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
),
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),
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
),
47 NWidget(NWID_HORIZONTAL
),
48 NWidget(WWT_PANEL
, COLOUR_GREY
), SetFill(1, 1), SetResize(1, 0), EndContainer(),
49 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
53 class ExtraViewportWindow
: public Window
{
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, 0, ZOOM_LVL_VIEWPORT
);
61 if (_settings_client
.gui
.zoom_min
== ZOOM_LVL_VIEWPORT
) this->DisableWidget(WID_EV_ZOOM_IN
);
64 if (tile
== INVALID_TILE
) {
65 /* No tile? Use center of main viewport. */
66 const Window
*w
= FindWindowById(WC_MAIN_WINDOW
, 0);
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;
72 pt
= RemapCoords(TileX(tile
) * TILE_SIZE
+ TILE_SIZE
/ 2, TileY(tile
) * TILE_SIZE
+ TILE_SIZE
/ 2, TilePixelHeight(tile
));
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
;
81 void SetStringParameters(int widget
) const override
85 /* set the number in the title bar */
86 SetDParam(0, this->window_number
+ 1);
91 void OnClick(Point pt
, int widget
, int click_count
) override
94 case WID_EV_ZOOM_IN
: DoZoomInOutWindow(ZOOM_IN
, this); break;
95 case WID_EV_ZOOM_OUT
: DoZoomInOutWindow(ZOOM_OUT
, this); break;
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
;
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
;
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
;
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;
121 void OnResize() override
123 if (this->viewport
!= nullptr) {
124 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_EV_VIEWPORT
);
125 nvp
->UpdateViewportCoordinates(this);
129 void OnScroll(Point delta
) override
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
;
137 void OnMouseWheel(int wheel
) override
139 if (_settings_client
.gui
.scrollwheel_scrolling
!= 2) {
140 ZoomInOrOutToCursorWindow(wheel
< 0, this);
145 * Some data on this window has become invalid.
146 * @param data Information about the changed data.
147 * @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.
149 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
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
);
157 static WindowDesc
_extra_view_port_desc(
158 WDP_AUTO
, "extra_viewport", 300, 268,
159 WC_EXTRA_VIEW_PORT
, WC_NONE
,
161 _nested_extra_view_port_widgets
, lengthof(_nested_extra_view_port_widgets
)
165 * Show a new Extra Viewport window.
166 * @param tile Tile to center the view on. INVALID_TILE means to use the center of main viewport.
168 void ShowExtraViewPortWindow(TileIndex tile
)
172 /* find next free window number for extra viewport */
173 while (FindWindowById(WC_EXTRA_VIEW_PORT
, i
) != nullptr) i
++;
175 new ExtraViewportWindow(&_extra_view_port_desc
, i
, tile
);
179 * Show a new Extra Viewport window.
180 * Center it on the tile under the cursor, if the cursor is inside a viewport.
181 * If that fails, center it on main viewport center.
183 void ShowExtraViewPortWindowForTileUnderCursor()
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
);