Make it possible to stop road vehicles from slowing down in curves so "diagonal"...
[openttd-joker.git] / src / viewport_gui.cpp
blob2f2b370d0427fd1a71b40d3d67ad617b51fca2e4
1 /* $Id$ */
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 <http://www.gnu.org/licenses/>.
8 */
10 /** @file viewport_gui.cpp Extra viewport window. */
12 #include "stdafx.h"
14 #include "gfx_func.h"
15 #include "landscape.h"
16 #include "strings_func.h"
17 #include "tilehighlight_func.h"
18 #include "tunnelbridge.h"
19 #include "viewport_func.h"
20 #include "window_func.h"
21 #include "window_gui.h"
22 #include "zoom_func.h"
24 #include "table/sprites.h"
25 #include "table/strings.h"
26 #include "widgets/viewport_widget.h"
28 #include "safeguards.h"
30 /* Extra ViewPort Window Stuff */
31 static const NWidgetPart _nested_extra_view_port_widgets[] = {
32 NWidget(NWID_HORIZONTAL),
33 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
34 NWidget(WWT_CAPTION, COLOUR_GREY, WID_EV_CAPTION), SetDataTip(STR_EXTRA_VIEW_PORT_TITLE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
35 NWidget(WWT_SHADEBOX, COLOUR_GREY),
36 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
37 NWidget(WWT_STICKYBOX, COLOUR_GREY),
38 EndContainer(),
39 NWidget(WWT_PANEL, COLOUR_GREY),
40 NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_EV_VIEWPORT), SetPadding(2, 2, 2, 2), SetResize(1, 1), SetFill(1, 1),
41 EndContainer(),
42 NWidget(NWID_HORIZONTAL),
43 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_EV_ZOOM_IN), SetDataTip(SPR_IMG_ZOOMIN, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_IN),
44 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_EV_ZOOM_OUT), SetDataTip(SPR_IMG_ZOOMOUT, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_OUT),
45 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
46 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_EV_MAIN_TO_VIEW), SetFill(1, 1), SetResize(1, 0),
47 SetDataTip(STR_EXTRA_VIEW_MOVE_MAIN_TO_VIEW, STR_EXTRA_VIEW_MOVE_MAIN_TO_VIEW_TT),
48 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_EV_VIEW_TO_MAIN), SetFill(1, 1), SetResize(1, 0),
49 SetDataTip(STR_EXTRA_VIEW_MOVE_VIEW_TO_MAIN, STR_EXTRA_VIEW_MOVE_VIEW_TO_MAIN_TT),
50 EndContainer(),
51 EndContainer(),
52 NWidget(NWID_HORIZONTAL),
53 NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), SetResize(1, 0), EndContainer(),
54 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
55 EndContainer(),
58 class ExtraViewportWindow : public Window {
59 public:
60 ExtraViewportWindow(WindowDesc *desc, int window_number, TileIndex tile) : Window(desc)
62 this->InitNested(window_number);
64 auto viewport = this->GetWidget<NWidgetViewport>(WID_EV_VIEWPORT);
65 viewport->InitializeViewport(this, 0, ZOOM_LVL_VIEWPORT);
66 if (_settings_client.gui.zoom_min == ZOOM_LVL_VIEWPORT) this->DisableWidget(WID_EV_ZOOM_IN);
68 Point point{};
70 if (tile == INVALID_TILE) {
71 /* No tile? Use center of main viewport. */
72 const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
74 /* center on same place as main window (zoom is maximum, no adjustment needed) */
75 point.x = w->viewport->scrollpos_x + w->viewport->virtual_width / 2;
76 point.y = w->viewport->scrollpos_y + w->viewport->virtual_height / 2;
77 } else {
78 point = RemapCoords(TileX(tile) * TILE_SIZE + TILE_SIZE / 2, TileY(tile) * TILE_SIZE + TILE_SIZE / 2, TilePixelHeight(tile));
81 this->viewport->scrollpos_x = point.x - this->viewport->virtual_width / 2;
82 this->viewport->scrollpos_y = point.y - this->viewport->virtual_height / 2;
83 this->viewport->dest_scrollpos_x = this->viewport->scrollpos_x;
84 this->viewport->dest_scrollpos_y = this->viewport->scrollpos_y;
85 this->viewport->map_type = static_cast<ViewportMapType>(_settings_client.gui.default_viewport_map_mode);
88 void SetStringParameters(int widget) const override
90 switch (widget) {
91 case WID_EV_CAPTION:
92 /* set the number in the title bar */
93 SetDParam(0, this->window_number + 1);
94 break;
96 default: ;
100 void OnClick(Point pt, int widget, int click_count) override
102 switch (widget) {
103 case WID_EV_ZOOM_IN: DoZoomInOutWindow(ZOOM_IN, this); break;
104 case WID_EV_ZOOM_OUT: DoZoomInOutWindow(ZOOM_OUT, this); break;
106 case WID_EV_MAIN_TO_VIEW: { // location button (move main view to same spot as this view) 'Paste Location'
107 Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
108 const int x = this->viewport->scrollpos_x; // Where is the main looking at
109 const int y = this->viewport->scrollpos_y;
111 /* set this view to same location. Based on the center, adjusting for zoom */
112 w->viewport->dest_scrollpos_x = x - (w->viewport->virtual_width - this->viewport->virtual_width) / 2;
113 w->viewport->dest_scrollpos_y = y - (w->viewport->virtual_height - this->viewport->virtual_height) / 2;
114 w->viewport->follow_vehicle = INVALID_VEHICLE;
115 break;
118 case WID_EV_VIEW_TO_MAIN: { // inverse location button (move this view to same spot as main view) 'Copy Location'
119 const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
120 const int x = w->viewport->scrollpos_x;
121 const int y = w->viewport->scrollpos_y;
123 this->viewport->dest_scrollpos_x = x + (w->viewport->virtual_width - this->viewport->virtual_width) / 2;
124 this->viewport->dest_scrollpos_y = y + (w->viewport->virtual_height - this->viewport->virtual_height) / 2;
125 break;
128 default:
129 break;
133 void OnResize() override
135 if (this->viewport != nullptr) {
136 auto viewport = this->GetWidget<NWidgetViewport>(WID_EV_VIEWPORT);
137 viewport->UpdateViewportCoordinates(this);
141 void OnScroll(Point delta) override
143 this->viewport->scrollpos_x += ScaleByZoom(delta.x, this->viewport->zoom);
144 this->viewport->scrollpos_y += ScaleByZoom(delta.y, this->viewport->zoom);
145 this->viewport->dest_scrollpos_x = this->viewport->scrollpos_x;
146 this->viewport->dest_scrollpos_y = this->viewport->scrollpos_y;
149 void OnMouseWheel(int wheel) override
151 if (_ctrl_pressed) {
152 /* Cycle through the drawing modes */
153 this->viewport->map_type = ChangeRenderMode(this->viewport, wheel < 0);
154 this->SetDirty();
155 } else if (_settings_client.gui.scrollwheel_scrolling == 0) {
156 ZoomInOrOutToCursorWindow(wheel < 0, this);
161 * Some data on this window has become invalid.
162 * @param data Information about the changed data.
163 * @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.
165 void OnInvalidateData(int data, bool gui_scope) override
167 if (!gui_scope) return;
168 /* Only handle zoom message if intended for us (msg ZOOM_IN/ZOOM_OUT) */
169 HandleZoomMessage(this, this->viewport, WID_EV_ZOOM_IN, WID_EV_ZOOM_OUT);
173 static WindowDesc _extra_view_port_desc(
174 WDP_AUTO, "extra_viewport", 300, 268,
175 WC_EXTRA_VIEW_PORT, WC_NONE,
177 _nested_extra_view_port_widgets, lengthof(_nested_extra_view_port_widgets)
181 * Show a new Extra Viewport window.
182 * @param tile Tile to center the view on. INVALID_TILE means to use the center of main viewport.
184 void ShowExtraViewPortWindow(TileIndex tile)
186 int i = 0;
188 /* find next free window number for extra viewport */
189 while (FindWindowById(WC_EXTRA_VIEW_PORT, i) != nullptr) i++;
191 new ExtraViewportWindow(&_extra_view_port_desc, i, tile);
195 * Show a new Extra Viewport window.
196 * When building a tunnel, the tunnel end-tile is used as center for new viewport.
197 * Otherwise center it on the tile under the cursor, if the cursor is inside a viewport.
198 * If that fails, center it on main viewport center.
200 void ShowExtraViewPortWindowForTileUnderCursor()
202 if (_build_tunnel_endtile != 0 && _thd.place_mode & HT_TUNNEL) {
203 ShowExtraViewPortWindow(_build_tunnel_endtile);
204 return;
207 /* Use tile under mouse as center for new viewport.
208 * Do this before creating the window, it might appear just below the mouse. */
209 const Point point = GetTileBelowCursor();
210 ShowExtraViewPortWindow(point.x != -1 ? TileVirtXY(point.x, point.y) : INVALID_TILE);