OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

viewport_gui.cpp
Go to the documentation of this file.
1 /* $Id: viewport_gui.cpp 27743 2017-02-05 17:54:46Z frosch $ */
2
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 */
9
12 #include "stdafx.h"
13 #include "landscape.h"
14 #include "window_gui.h"
15 #include "viewport_func.h"
16 #include "strings_func.h"
17 #include "zoom_func.h"
18 #include "window_func.h"
19
20 #include "widgets/viewport_widget.h"
21
22 #include "table/strings.h"
23 #include "table/sprites.h"
24
25 #include "safeguards.h"
26
27 /* Extra ViewPort Window Stuff */
28 static const NWidgetPart _nested_extra_view_port_widgets[] = {
29 NWidget(NWID_HORIZONTAL),
30 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
31 NWidget(WWT_CAPTION, COLOUR_GREY, WID_EV_CAPTION), SetDataTip(STR_EXTRA_VIEW_PORT_TITLE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
32 NWidget(WWT_SHADEBOX, COLOUR_GREY),
33 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
34 NWidget(WWT_STICKYBOX, COLOUR_GREY),
35 EndContainer(),
36 NWidget(WWT_PANEL, COLOUR_GREY),
37 NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_EV_VIEWPORT), SetPadding(2, 2, 2, 2), SetResize(1, 1), SetFill(1, 1),
38 EndContainer(),
39 NWidget(NWID_HORIZONTAL),
40 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_EV_ZOOM_IN), SetDataTip(SPR_IMG_ZOOMIN, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_IN),
41 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_EV_ZOOM_OUT), SetDataTip(SPR_IMG_ZOOMOUT, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_OUT),
42 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
43 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_EV_MAIN_TO_VIEW), SetFill(1, 1), SetResize(1, 0),
44 SetDataTip(STR_EXTRA_VIEW_MOVE_MAIN_TO_VIEW, STR_EXTRA_VIEW_MOVE_MAIN_TO_VIEW_TT),
45 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_EV_VIEW_TO_MAIN), SetFill(1, 1), SetResize(1, 0),
46 SetDataTip(STR_EXTRA_VIEW_MOVE_VIEW_TO_MAIN, STR_EXTRA_VIEW_MOVE_VIEW_TO_MAIN_TT),
47 EndContainer(),
48 EndContainer(),
49 NWidget(NWID_HORIZONTAL),
50 NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), SetResize(1, 0), EndContainer(),
51 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
52 EndContainer(),
53 };
54
55 class ExtraViewportWindow : public Window {
56 public:
57 ExtraViewportWindow(WindowDesc *desc, int window_number, TileIndex tile) : Window(desc)
58 {
59 this->InitNested(window_number);
60
61 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_EV_VIEWPORT);
62 nvp->InitializeViewport(this, 0, ZOOM_LVL_VIEWPORT);
63 if (_settings_client.gui.zoom_min == ZOOM_LVL_VIEWPORT) this->DisableWidget(WID_EV_ZOOM_IN);
64
65 Point pt;
66 if (tile == INVALID_TILE) {
67 /* No tile? Use center of main viewport. */
68 const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
69
70 /* center on same place as main window (zoom is maximum, no adjustment needed) */
71 pt.x = w->viewport->scrollpos_x + w->viewport->virtual_width / 2;
72 pt.y = w->viewport->scrollpos_y + w->viewport->virtual_height / 2;
73 } else {
74 pt = RemapCoords(TileX(tile) * TILE_SIZE + TILE_SIZE / 2, TileY(tile) * TILE_SIZE + TILE_SIZE / 2, TilePixelHeight(tile));
75 }
76
77 this->viewport->scrollpos_x = pt.x - this->viewport->virtual_width / 2;
78 this->viewport->scrollpos_y = pt.y - this->viewport->virtual_height / 2;
79 this->viewport->dest_scrollpos_x = this->viewport->scrollpos_x;
80 this->viewport->dest_scrollpos_y = this->viewport->scrollpos_y;
81 }
82
83 virtual void SetStringParameters(int widget) const
84 {
85 switch (widget) {
86 case WID_EV_CAPTION:
87 /* set the number in the title bar */
88 SetDParam(0, this->window_number + 1);
89 break;
90 }
91 }
92
93 virtual void OnClick(Point pt, int widget, int click_count)
94 {
95 switch (widget) {
96 case WID_EV_ZOOM_IN: DoZoomInOutWindow(ZOOM_IN, this); break;
97 case WID_EV_ZOOM_OUT: DoZoomInOutWindow(ZOOM_OUT, this); break;
98
99 case WID_EV_MAIN_TO_VIEW: { // location button (move main view to same spot as this view) 'Paste Location'
100 Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
101 int x = this->viewport->scrollpos_x; // Where is the main looking at
102 int y = this->viewport->scrollpos_y;
103
104 /* set this view to same location. Based on the center, adjusting for zoom */
105 w->viewport->dest_scrollpos_x = x - (w->viewport->virtual_width - this->viewport->virtual_width) / 2;
106 w->viewport->dest_scrollpos_y = y - (w->viewport->virtual_height - this->viewport->virtual_height) / 2;
107 w->viewport->follow_vehicle = INVALID_VEHICLE;
108 break;
109 }
110
111 case WID_EV_VIEW_TO_MAIN: { // inverse location button (move this view to same spot as main view) 'Copy Location'
112 const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
113 int x = w->viewport->scrollpos_x;
114 int y = w->viewport->scrollpos_y;
115
116 this->viewport->dest_scrollpos_x = x + (w->viewport->virtual_width - this->viewport->virtual_width) / 2;
117 this->viewport->dest_scrollpos_y = y + (w->viewport->virtual_height - this->viewport->virtual_height) / 2;
118 break;
119 }
120 }
121 }
122
123 virtual void OnResize()
124 {
125 if (this->viewport != NULL) {
126 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_EV_VIEWPORT);
127 nvp->UpdateViewportCoordinates(this);
128 }
129 }
130
131 virtual void OnScroll(Point delta)
132 {
133 this->viewport->scrollpos_x += ScaleByZoom(delta.x, this->viewport->zoom);
134 this->viewport->scrollpos_y += ScaleByZoom(delta.y, this->viewport->zoom);
135 this->viewport->dest_scrollpos_x = this->viewport->scrollpos_x;
136 this->viewport->dest_scrollpos_y = this->viewport->scrollpos_y;
137 }
138
139 virtual void OnMouseWheel(int wheel)
140 {
141 if (_settings_client.gui.scrollwheel_scrolling == 0) {
142 ZoomInOrOutToCursorWindow(wheel < 0, this);
143 }
144 }
145
151 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
152 {
153 if (!gui_scope) return;
154 /* Only handle zoom message if intended for us (msg ZOOM_IN/ZOOM_OUT) */
155 HandleZoomMessage(this, this->viewport, WID_EV_ZOOM_IN, WID_EV_ZOOM_OUT);
156 }
157 };
158
159 static WindowDesc _extra_view_port_desc(
160 WDP_AUTO, "extra_viewport", 300, 268,
161 WC_EXTRA_VIEW_PORT, WC_NONE,
162 0,
163 _nested_extra_view_port_widgets, lengthof(_nested_extra_view_port_widgets)
164 );
165
170 void ShowExtraViewPortWindow(TileIndex tile)
171 {
172 int i = 0;
173
174 /* find next free window number for extra viewport */
175 while (FindWindowById(WC_EXTRA_VIEW_PORT, i) != NULL) i++;
176
177 new ExtraViewportWindow(&_extra_view_port_desc, i, tile);
178 }
179
185 void ShowExtraViewPortWindowForTileUnderCursor()
186 {
187 /* Use tile under mouse as center for new viewport.
188 * Do this before creating the window, it might appear just below the mouse. */
189 Point pt = GetTileBelowCursor();
190 ShowExtraViewPortWindow(pt.x != -1 ? TileVirtXY(pt.x, pt.y) : INVALID_TILE);
191 }