1 /*
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/>.
6  */
7 
8  /** @file screenshot_gui.cpp GUI functions related to screenshots. */
9 
10 #include "stdafx.h"
11 #include "window_func.h"
12 #include "window_gui.h"
13 #include "screenshot.h"
14 #include "widgets/screenshot_widget.h"
15 #include "table/strings.h"
16 #include "gfx_func.h"
17 
18 struct ScreenshotWindow : Window {
ScreenshotWindowScreenshotWindow19 	ScreenshotWindow(WindowDesc *desc) : Window(desc)
20 	{
21 		this->CreateNestedTree();
22 		this->FinishInitNested();
23 	}
24 
OnPaintScreenshotWindow25 	void OnPaint() override
26 	{
27 		this->DrawWidgets();
28 	}
29 
OnClickScreenshotWindow30 	void OnClick(Point pt, int widget, int click_count) override
31 	{
32 		if (widget < 0) return;
33 		ScreenshotType st;
34 		switch (widget) {
35 			default:
36 			case WID_SC_TAKE:             st = SC_VIEWPORT;    break;
37 			case WID_SC_TAKE_ZOOMIN:      st = SC_ZOOMEDIN;    break;
38 			case WID_SC_TAKE_DEFAULTZOOM: st = SC_DEFAULTZOOM; break;
39 			case WID_SC_TAKE_WORLD:       st = SC_WORLD;       break;
40 			case WID_SC_TAKE_HEIGHTMAP:   st = SC_HEIGHTMAP;   break;
41 			case WID_SC_TAKE_MINIMAP:     st = SC_MINIMAP;     break;
42 		}
43 		MakeScreenshotWithConfirm(st);
44 	}
45 };
46 
47 static const NWidgetPart _nested_screenshot[] = {
48 	NWidget(NWID_HORIZONTAL),
49 		NWidget(WWT_CLOSEBOX, COLOUR_GREY),
50 		NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_SCREENSHOT_CAPTION, 0),
51 		NWidget(WWT_SHADEBOX, COLOUR_GREY),
52 		NWidget(WWT_STICKYBOX, COLOUR_GREY),
53 	EndContainer(),
54 	NWidget(NWID_VERTICAL, NC_EQUALSIZE),
55 		NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SC_TAKE), SetFill(1, 1), SetDataTip(STR_SCREENSHOT_SCREENSHOT, 0), SetMinimalTextLines(2, 0),
56 		NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SC_TAKE_ZOOMIN), SetFill(1, 1), SetDataTip(STR_SCREENSHOT_ZOOMIN_SCREENSHOT, 0), SetMinimalTextLines(2, 0),
57 		NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SC_TAKE_DEFAULTZOOM), SetFill(1, 1), SetDataTip(STR_SCREENSHOT_DEFAULTZOOM_SCREENSHOT, 0), SetMinimalTextLines(2, 0),
58 		NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SC_TAKE_WORLD), SetFill(1, 1), SetDataTip(STR_SCREENSHOT_WORLD_SCREENSHOT, 0), SetMinimalTextLines(2, 0),
59 		NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SC_TAKE_HEIGHTMAP), SetFill(1, 1), SetDataTip(STR_SCREENSHOT_HEIGHTMAP_SCREENSHOT, 0), SetMinimalTextLines(2, 0),
60 		NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SC_TAKE_MINIMAP), SetFill(1, 1), SetDataTip(STR_SCREENSHOT_MINIMAP_SCREENSHOT, 0), SetMinimalTextLines(2, 0),
61 	EndContainer(),
62 };
63 
64 static WindowDesc _screenshot_window_desc(
65 	WDP_AUTO, "take_a_screenshot", 200, 100,
66 	WC_SCREENSHOT, WC_NONE,
67 	0,
68 	_nested_screenshot, lengthof(_nested_screenshot)
69 );
70 
ShowScreenshotWindow()71 void ShowScreenshotWindow()
72 {
73 	CloseWindowById(WC_SCREENSHOT, 0);
74 	new ScreenshotWindow(&_screenshot_window_desc);
75 }
76 
77 /**
78  * Set the visibility of the screenshot window when taking a screenshot.
79  * @param hide Are we hiding the window or showing it again after the screenshot is taken?
80  */
SetScreenshotWindowVisibility(bool hide)81 void SetScreenshotWindowVisibility(bool hide)
82 {
83 	ScreenshotWindow *scw = (ScreenshotWindow *)FindWindowById(WC_SCREENSHOT, 0);
84 
85 	if (scw == nullptr) return;
86 
87 	if (hide) {
88 		/* Set dirty the screen area where the window is covering (not the window itself), then move window off screen. */
89 		scw->SetDirty();
90 		scw->left += 2 * _screen.width;
91 	} else {
92 		/* Return window to original position. */
93 		scw->left -= 2 * _screen.width;
94 		scw->SetDirty();
95 	}
96 }
97