2009-10-20 18:38:38 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file viewport_gui.cpp Extra viewport window. */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "landscape.h"
|
|
|
|
#include "window_gui.h"
|
|
|
|
#include "viewport_func.h"
|
|
|
|
#include "strings_func.h"
|
|
|
|
#include "zoom_func.h"
|
|
|
|
#include "window_func.h"
|
|
|
|
|
2011-12-15 22:22:55 +00:00
|
|
|
#include "widgets/viewport_widget.h"
|
|
|
|
|
2009-10-20 18:38:38 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
#include "table/sprites.h"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2020-06-29 01:38:29 +00:00
|
|
|
/* Extra Viewport Window Stuff */
|
|
|
|
static const NWidgetPart _nested_extra_viewport_widgets[] = {
|
2009-10-20 18:38:38 +00:00
|
|
|
NWidget(NWID_HORIZONTAL),
|
2009-11-24 18:05:55 +00:00
|
|
|
NWidget(WWT_CLOSEBOX, COLOUR_GREY),
|
2020-06-29 01:38:29 +00:00
|
|
|
NWidget(WWT_CAPTION, COLOUR_GREY, WID_EV_CAPTION), SetDataTip(STR_EXTRA_VIEWPORT_TITLE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
|
2009-12-21 16:24:29 +00:00
|
|
|
NWidget(WWT_SHADEBOX, COLOUR_GREY),
|
2013-05-26 19:30:07 +00:00
|
|
|
NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
|
2009-11-24 18:05:55 +00:00
|
|
|
NWidget(WWT_STICKYBOX, COLOUR_GREY),
|
2009-10-20 18:38:38 +00:00
|
|
|
EndContainer(),
|
2009-11-24 21:13:36 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY),
|
2011-12-16 16:29:53 +00:00
|
|
|
NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_EV_VIEWPORT), SetPadding(2, 2, 2, 2), SetResize(1, 1), SetFill(1, 1),
|
2009-10-20 18:38:38 +00:00
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
2011-12-16 16:29:53 +00:00
|
|
|
NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_EV_ZOOM_IN), SetDataTip(SPR_IMG_ZOOMIN, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_IN),
|
|
|
|
NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_EV_ZOOM_OUT), SetDataTip(SPR_IMG_ZOOMOUT, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_OUT),
|
2009-10-20 18:38:38 +00:00
|
|
|
NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
|
2011-12-16 16:29:53 +00:00
|
|
|
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_EV_MAIN_TO_VIEW), SetFill(1, 1), SetResize(1, 0),
|
2009-10-20 18:38:38 +00:00
|
|
|
SetDataTip(STR_EXTRA_VIEW_MOVE_MAIN_TO_VIEW, STR_EXTRA_VIEW_MOVE_MAIN_TO_VIEW_TT),
|
2011-12-16 16:29:53 +00:00
|
|
|
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_EV_VIEW_TO_MAIN), SetFill(1, 1), SetResize(1, 0),
|
2009-10-20 18:38:38 +00:00
|
|
|
SetDataTip(STR_EXTRA_VIEW_MOVE_VIEW_TO_MAIN, STR_EXTRA_VIEW_MOVE_VIEW_TO_MAIN_TT),
|
|
|
|
EndContainer(),
|
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
2009-11-24 21:13:36 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), SetResize(1, 0), EndContainer(),
|
2009-11-24 18:05:55 +00:00
|
|
|
NWidget(WWT_RESIZEBOX, COLOUR_GREY),
|
2009-10-20 18:38:38 +00:00
|
|
|
EndContainer(),
|
|
|
|
};
|
|
|
|
|
|
|
|
class ExtraViewportWindow : public Window {
|
|
|
|
public:
|
2013-05-26 19:23:42 +00:00
|
|
|
ExtraViewportWindow(WindowDesc *desc, int window_number, TileIndex tile) : Window(desc)
|
2009-10-20 18:38:38 +00:00
|
|
|
{
|
2013-05-26 19:23:42 +00:00
|
|
|
this->InitNested(window_number);
|
2009-10-20 18:38:38 +00:00
|
|
|
|
2011-12-16 16:29:53 +00:00
|
|
|
NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_EV_VIEWPORT);
|
2011-11-24 18:55:56 +00:00
|
|
|
nvp->InitializeViewport(this, 0, ZOOM_LVL_VIEWPORT);
|
2011-12-16 16:29:53 +00:00
|
|
|
if (_settings_client.gui.zoom_min == ZOOM_LVL_VIEWPORT) this->DisableWidget(WID_EV_ZOOM_IN);
|
2009-10-20 18:38:38 +00:00
|
|
|
|
|
|
|
Point pt;
|
|
|
|
if (tile == INVALID_TILE) {
|
2010-10-17 13:54:05 +00:00
|
|
|
/* No tile? Use center of main viewport. */
|
2009-10-20 18:38:38 +00:00
|
|
|
const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
|
|
|
|
|
|
|
|
/* center on same place as main window (zoom is maximum, no adjustment needed) */
|
2010-01-05 14:26:15 +00:00
|
|
|
pt.x = w->viewport->scrollpos_x + w->viewport->virtual_width / 2;
|
2009-10-20 18:38:38 +00:00
|
|
|
pt.y = w->viewport->scrollpos_y + w->viewport->virtual_height / 2;
|
|
|
|
} else {
|
2017-02-05 17:54:46 +00:00
|
|
|
pt = RemapCoords(TileX(tile) * TILE_SIZE + TILE_SIZE / 2, TileY(tile) * TILE_SIZE + TILE_SIZE / 2, TilePixelHeight(tile));
|
2009-10-20 18:38:38 +00:00
|
|
|
}
|
|
|
|
|
2010-01-05 14:26:15 +00:00
|
|
|
this->viewport->scrollpos_x = pt.x - this->viewport->virtual_width / 2;
|
|
|
|
this->viewport->scrollpos_y = pt.y - this->viewport->virtual_height / 2;
|
2009-10-20 18:38:38 +00:00
|
|
|
this->viewport->dest_scrollpos_x = this->viewport->scrollpos_x;
|
|
|
|
this->viewport->dest_scrollpos_y = this->viewport->scrollpos_y;
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void SetStringParameters(int widget) const override
|
2009-10-20 18:38:38 +00:00
|
|
|
{
|
|
|
|
switch (widget) {
|
2011-12-16 16:29:53 +00:00
|
|
|
case WID_EV_CAPTION:
|
2009-10-20 18:38:38 +00:00
|
|
|
/* set the number in the title bar */
|
|
|
|
SetDParam(0, this->window_number + 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnClick(Point pt, int widget, int click_count) override
|
2009-10-20 18:38:38 +00:00
|
|
|
{
|
|
|
|
switch (widget) {
|
2011-12-16 16:29:53 +00:00
|
|
|
case WID_EV_ZOOM_IN: DoZoomInOutWindow(ZOOM_IN, this); break;
|
|
|
|
case WID_EV_ZOOM_OUT: DoZoomInOutWindow(ZOOM_OUT, this); break;
|
2009-10-20 18:38:38 +00:00
|
|
|
|
2011-12-16 16:29:53 +00:00
|
|
|
case WID_EV_MAIN_TO_VIEW: { // location button (move main view to same spot as this view) 'Paste Location'
|
2009-10-20 18:38:38 +00:00
|
|
|
Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
|
|
|
|
int x = this->viewport->scrollpos_x; // Where is the main looking at
|
|
|
|
int y = this->viewport->scrollpos_y;
|
|
|
|
|
|
|
|
/* set this view to same location. Based on the center, adjusting for zoom */
|
|
|
|
w->viewport->dest_scrollpos_x = x - (w->viewport->virtual_width - this->viewport->virtual_width) / 2;
|
|
|
|
w->viewport->dest_scrollpos_y = y - (w->viewport->virtual_height - this->viewport->virtual_height) / 2;
|
|
|
|
w->viewport->follow_vehicle = INVALID_VEHICLE;
|
2010-08-01 18:53:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-10-20 18:38:38 +00:00
|
|
|
|
2011-12-16 16:29:53 +00:00
|
|
|
case WID_EV_VIEW_TO_MAIN: { // inverse location button (move this view to same spot as main view) 'Copy Location'
|
2009-10-20 18:38:38 +00:00
|
|
|
const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
|
|
|
|
int x = w->viewport->scrollpos_x;
|
|
|
|
int y = w->viewport->scrollpos_y;
|
|
|
|
|
|
|
|
this->viewport->dest_scrollpos_x = x + (w->viewport->virtual_width - this->viewport->virtual_width) / 2;
|
|
|
|
this->viewport->dest_scrollpos_y = y + (w->viewport->virtual_height - this->viewport->virtual_height) / 2;
|
2010-08-01 18:53:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-10-20 18:38:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnResize() override
|
2009-10-20 18:38:38 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (this->viewport != nullptr) {
|
2011-12-16 16:29:53 +00:00
|
|
|
NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_EV_VIEWPORT);
|
2009-10-20 18:38:38 +00:00
|
|
|
nvp->UpdateViewportCoordinates(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnScroll(Point delta) override
|
2009-10-20 18:38:38 +00:00
|
|
|
{
|
2011-01-18 21:09:13 +00:00
|
|
|
this->viewport->scrollpos_x += ScaleByZoom(delta.x, this->viewport->zoom);
|
|
|
|
this->viewport->scrollpos_y += ScaleByZoom(delta.y, this->viewport->zoom);
|
2009-10-20 18:38:38 +00:00
|
|
|
this->viewport->dest_scrollpos_x = this->viewport->scrollpos_x;
|
|
|
|
this->viewport->dest_scrollpos_y = this->viewport->scrollpos_y;
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnMouseWheel(int wheel) override
|
2009-10-20 18:38:38 +00:00
|
|
|
{
|
2018-04-27 22:12:49 +00:00
|
|
|
if (_settings_client.gui.scrollwheel_scrolling != 2) {
|
2011-02-05 16:36:37 +00:00
|
|
|
ZoomInOrOutToCursorWindow(wheel < 0, this);
|
|
|
|
}
|
2009-10-20 18:38:38 +00:00
|
|
|
}
|
|
|
|
|
2011-03-13 21:31:29 +00:00
|
|
|
/**
|
|
|
|
* Some data on this window has become invalid.
|
|
|
|
* @param data Information about the changed data.
|
|
|
|
* @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.
|
|
|
|
*/
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnInvalidateData(int data = 0, bool gui_scope = true) override
|
2009-10-20 18:38:38 +00:00
|
|
|
{
|
2011-03-13 21:31:29 +00:00
|
|
|
if (!gui_scope) return;
|
2009-10-20 18:38:38 +00:00
|
|
|
/* Only handle zoom message if intended for us (msg ZOOM_IN/ZOOM_OUT) */
|
2011-12-16 16:29:53 +00:00
|
|
|
HandleZoomMessage(this, this->viewport, WID_EV_ZOOM_IN, WID_EV_ZOOM_OUT);
|
2009-10-20 18:38:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-29 01:38:29 +00:00
|
|
|
static WindowDesc _extra_viewport_desc(
|
2013-05-26 19:25:01 +00:00
|
|
|
WDP_AUTO, "extra_viewport", 300, 268,
|
2020-06-29 01:38:29 +00:00
|
|
|
WC_EXTRA_VIEWPORT, WC_NONE,
|
2012-11-11 16:10:43 +00:00
|
|
|
0,
|
2020-06-29 01:38:29 +00:00
|
|
|
_nested_extra_viewport_widgets, lengthof(_nested_extra_viewport_widgets)
|
2009-10-20 18:38:38 +00:00
|
|
|
);
|
|
|
|
|
2010-10-17 13:54:05 +00:00
|
|
|
/**
|
|
|
|
* Show a new Extra Viewport window.
|
|
|
|
* @param tile Tile to center the view on. INVALID_TILE means to use the center of main viewport.
|
|
|
|
*/
|
2020-06-29 01:38:29 +00:00
|
|
|
void ShowExtraViewportWindow(TileIndex tile)
|
2009-10-20 18:38:38 +00:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
/* find next free window number for extra viewport */
|
2020-06-29 01:38:29 +00:00
|
|
|
while (FindWindowById(WC_EXTRA_VIEWPORT, i) != nullptr) i++;
|
2009-10-20 18:38:38 +00:00
|
|
|
|
2020-06-29 01:38:29 +00:00
|
|
|
new ExtraViewportWindow(&_extra_viewport_desc, i, tile);
|
2009-10-20 18:38:38 +00:00
|
|
|
}
|
2010-10-17 13:54:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a new Extra Viewport window.
|
|
|
|
* Center it on the tile under the cursor, if the cursor is inside a viewport.
|
|
|
|
* If that fails, center it on main viewport center.
|
|
|
|
*/
|
2020-06-29 01:38:29 +00:00
|
|
|
void ShowExtraViewportWindowForTileUnderCursor()
|
2010-10-17 13:54:05 +00:00
|
|
|
{
|
|
|
|
/* Use tile under mouse as center for new viewport.
|
|
|
|
* Do this before creating the window, it might appear just below the mouse. */
|
|
|
|
Point pt = GetTileBelowCursor();
|
2020-06-29 01:38:29 +00:00
|
|
|
ShowExtraViewportWindow(pt.x != -1 ? TileVirtXY(pt.x, pt.y) : INVALID_TILE);
|
2010-10-17 13:54:05 +00:00
|
|
|
}
|