(svn r23005) -Fix (r23004): Of course there's still the 16-sprite version for shore...
[openttd/fttd.git] / src / viewport_gui.cpp
blob103b27cb287d9b0773d524be25e8859d29bfd2f3
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"
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"
20 #include "table/strings.h"
21 #include "table/sprites.h"
23 /** Widget numbers of the extra viewport window. */
24 enum ExtraViewportWindowWidgets {
25 EVW_CAPTION,
26 EVW_VIEWPORT,
27 EVW_ZOOMIN,
28 EVW_ZOOMOUT,
29 EVW_MAIN_TO_VIEW,
30 EVW_VIEW_TO_MAIN,
33 /* Extra ViewPort Window Stuff */
34 static const NWidgetPart _nested_extra_view_port_widgets[] = {
35 NWidget(NWID_HORIZONTAL),
36 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
37 NWidget(WWT_CAPTION, COLOUR_GREY, EVW_CAPTION), SetDataTip(STR_EXTRA_VIEW_PORT_TITLE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
38 NWidget(WWT_SHADEBOX, COLOUR_GREY),
39 NWidget(WWT_STICKYBOX, COLOUR_GREY),
40 EndContainer(),
41 NWidget(WWT_PANEL, COLOUR_GREY),
42 NWidget(NWID_VIEWPORT, INVALID_COLOUR, EVW_VIEWPORT), SetPadding(2, 2, 2, 2), SetResize(1, 1), SetFill(1, 1),
43 EndContainer(),
44 NWidget(NWID_HORIZONTAL),
45 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, EVW_ZOOMIN), SetDataTip(SPR_IMG_ZOOMIN, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_IN),
46 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, EVW_ZOOMOUT), SetDataTip(SPR_IMG_ZOOMOUT, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_OUT),
47 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
48 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, EVW_MAIN_TO_VIEW), SetFill(1, 1), SetResize(1, 0),
49 SetDataTip(STR_EXTRA_VIEW_MOVE_MAIN_TO_VIEW, STR_EXTRA_VIEW_MOVE_MAIN_TO_VIEW_TT),
50 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, EVW_VIEW_TO_MAIN), SetFill(1, 1), SetResize(1, 0),
51 SetDataTip(STR_EXTRA_VIEW_MOVE_VIEW_TO_MAIN, STR_EXTRA_VIEW_MOVE_VIEW_TO_MAIN_TT),
52 EndContainer(),
53 EndContainer(),
54 NWidget(NWID_HORIZONTAL),
55 NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), SetResize(1, 0), EndContainer(),
56 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
57 EndContainer(),
60 class ExtraViewportWindow : public Window {
61 public:
62 ExtraViewportWindow(const WindowDesc *desc, int window_number, TileIndex tile) : Window()
64 this->InitNested(desc, window_number);
66 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(EVW_VIEWPORT);
67 nvp->InitializeViewport(this, 0, ZOOM_LVL_NORMAL);
68 this->DisableWidget(EVW_ZOOMIN);
70 Point pt;
71 if (tile == INVALID_TILE) {
72 /* No tile? Use center of main viewport. */
73 const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
75 /* center on same place as main window (zoom is maximum, no adjustment needed) */
76 pt.x = w->viewport->scrollpos_x + w->viewport->virtual_width / 2;
77 pt.y = w->viewport->scrollpos_y + w->viewport->virtual_height / 2;
78 } else {
79 pt = RemapCoords(TileX(tile) * TILE_SIZE + TILE_SIZE / 2, TileY(tile) * TILE_SIZE + TILE_SIZE / 2, TileHeight(tile));
82 this->viewport->scrollpos_x = pt.x - this->viewport->virtual_width / 2;
83 this->viewport->scrollpos_y = pt.y - this->viewport->virtual_height / 2;
84 this->viewport->dest_scrollpos_x = this->viewport->scrollpos_x;
85 this->viewport->dest_scrollpos_y = this->viewport->scrollpos_y;
88 virtual void SetStringParameters(int widget) const
90 switch (widget) {
91 case EVW_CAPTION:
92 /* set the number in the title bar */
93 SetDParam(0, this->window_number + 1);
94 break;
98 virtual void OnClick(Point pt, int widget, int click_count)
100 switch (widget) {
101 case EVW_ZOOMIN: DoZoomInOutWindow(ZOOM_IN, this); break;
102 case EVW_ZOOMOUT: DoZoomInOutWindow(ZOOM_OUT, this); break;
104 case EVW_MAIN_TO_VIEW: { // location button (move main view to same spot as this view) 'Paste Location'
105 Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
106 int x = this->viewport->scrollpos_x; // Where is the main looking at
107 int y = this->viewport->scrollpos_y;
109 /* set this view to same location. Based on the center, adjusting for zoom */
110 w->viewport->dest_scrollpos_x = x - (w->viewport->virtual_width - this->viewport->virtual_width) / 2;
111 w->viewport->dest_scrollpos_y = y - (w->viewport->virtual_height - this->viewport->virtual_height) / 2;
112 w->viewport->follow_vehicle = INVALID_VEHICLE;
113 break;
116 case EVW_VIEW_TO_MAIN: { // inverse location button (move this view to same spot as main view) 'Copy Location'
117 const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
118 int x = w->viewport->scrollpos_x;
119 int y = w->viewport->scrollpos_y;
121 this->viewport->dest_scrollpos_x = x + (w->viewport->virtual_width - this->viewport->virtual_width) / 2;
122 this->viewport->dest_scrollpos_y = y + (w->viewport->virtual_height - this->viewport->virtual_height) / 2;
123 break;
128 virtual void OnResize()
130 if (this->viewport != NULL) {
131 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(EVW_VIEWPORT);
132 nvp->UpdateViewportCoordinates(this);
136 virtual void OnScroll(Point delta)
138 this->viewport->scrollpos_x += ScaleByZoom(delta.x, this->viewport->zoom);
139 this->viewport->scrollpos_y += ScaleByZoom(delta.y, this->viewport->zoom);
140 this->viewport->dest_scrollpos_x = this->viewport->scrollpos_x;
141 this->viewport->dest_scrollpos_y = this->viewport->scrollpos_y;
144 virtual void OnMouseWheel(int wheel)
146 if (_settings_client.gui.scrollwheel_scrolling == 0) {
147 ZoomInOrOutToCursorWindow(wheel < 0, this);
152 * Some data on this window has become invalid.
153 * @param data Information about the changed data.
154 * @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.
156 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
158 if (!gui_scope) return;
159 /* Only handle zoom message if intended for us (msg ZOOM_IN/ZOOM_OUT) */
160 HandleZoomMessage(this, this->viewport, EVW_ZOOMIN, EVW_ZOOMOUT);
164 static const WindowDesc _extra_view_port_desc(
165 WDP_AUTO, 300, 268,
166 WC_EXTRA_VIEW_PORT, WC_NONE,
167 WDF_UNCLICK_BUTTONS,
168 _nested_extra_view_port_widgets, lengthof(_nested_extra_view_port_widgets)
172 * Show a new Extra Viewport window.
173 * @param tile Tile to center the view on. INVALID_TILE means to use the center of main viewport.
175 void ShowExtraViewPortWindow(TileIndex tile)
177 int i = 0;
179 /* find next free window number for extra viewport */
180 while (FindWindowById(WC_EXTRA_VIEW_PORT, i) != NULL) i++;
182 new ExtraViewportWindow(&_extra_view_port_desc, i, tile);
186 * Show a new Extra Viewport window.
187 * Center it on the tile under the cursor, if the cursor is inside a viewport.
188 * If that fails, center it on main viewport center.
190 void ShowExtraViewPortWindowForTileUnderCursor()
192 /* Use tile under mouse as center for new viewport.
193 * Do this before creating the window, it might appear just below the mouse. */
194 Point pt = GetTileBelowCursor();
195 ShowExtraViewPortWindow(pt.x != -1 ? TileVirtXY(pt.x, pt.y) : INVALID_TILE);