OpenTTD Source: src/screenshot_gui.cpp Source File

OpenTTD Source 15.0-RC2

screenshot_gui.cpp
Go to the documentation of this file.
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "stdafx.h"
11#include "window_func.h"
12#include "window_gui.h"
13#include "screenshot.h"
14#include "gfx_func.h"
15
16#include "widgets/screenshot_widget.h"
17
18#include "table/strings.h"
19
20#include "safeguards.h"
21
22 struct ScreenshotWindow : Window {
23 ScreenshotWindow(WindowDesc &desc) : Window(desc)
24 {
25 this->CreateNestedTree();
26 this->FinishInitNested();
27 }
28
29 void OnPaint() override
30 {
31 this->DrawWidgets();
32 }
33
34 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
35 {
36 ScreenshotType st;
37 switch (widget) {
38 default: return;
39 case WID_SC_TAKE: st = SC_VIEWPORT; break;
40 case WID_SC_TAKE_ZOOMIN: st = SC_ZOOMEDIN; break;
41 case WID_SC_TAKE_DEFAULTZOOM: st = SC_DEFAULTZOOM; break;
42 case WID_SC_TAKE_WORLD: st = SC_WORLD; break;
43 case WID_SC_TAKE_HEIGHTMAP: st = SC_HEIGHTMAP; break;
44 case WID_SC_TAKE_MINIMAP: st = SC_MINIMAP; break;
45 }
46 MakeScreenshotWithConfirm(st);
47 }
48};
49
50static constexpr std::initializer_list<NWidgetPart> _nested_screenshot = {
51 NWidget(NWID_HORIZONTAL),
52 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
53 NWidget(WWT_CAPTION, COLOUR_GREY), SetStringTip(STR_SCREENSHOT_CAPTION),
54 NWidget(WWT_SHADEBOX, COLOUR_GREY),
55 NWidget(WWT_STICKYBOX, COLOUR_GREY),
56 EndContainer(),
57 NWidget(NWID_VERTICAL, NWidContainerFlag::EqualSize),
58 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SC_TAKE), SetFill(1, 1), SetStringTip(STR_SCREENSHOT_SCREENSHOT), SetMinimalTextLines(2, 0),
59 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SC_TAKE_ZOOMIN), SetFill(1, 1), SetStringTip(STR_SCREENSHOT_ZOOMIN_SCREENSHOT), SetMinimalTextLines(2, 0),
60 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SC_TAKE_DEFAULTZOOM), SetFill(1, 1), SetStringTip(STR_SCREENSHOT_DEFAULTZOOM_SCREENSHOT), SetMinimalTextLines(2, 0),
61 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SC_TAKE_WORLD), SetFill(1, 1), SetStringTip(STR_SCREENSHOT_WORLD_SCREENSHOT), SetMinimalTextLines(2, 0),
62 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SC_TAKE_HEIGHTMAP), SetFill(1, 1), SetStringTip(STR_SCREENSHOT_HEIGHTMAP_SCREENSHOT), SetMinimalTextLines(2, 0),
63 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SC_TAKE_MINIMAP), SetFill(1, 1), SetStringTip(STR_SCREENSHOT_MINIMAP_SCREENSHOT), SetMinimalTextLines(2, 0),
64 EndContainer(),
65};
66
67static WindowDesc _screenshot_window_desc(
68 WDP_AUTO, "take_a_screenshot", 200, 100,
69 WC_SCREENSHOT, WC_NONE,
70 {},
71 _nested_screenshot
72);
73
74void ShowScreenshotWindow()
75{
76 CloseWindowById(WC_SCREENSHOT, 0);
77 new ScreenshotWindow(_screenshot_window_desc);
78}
79
84 void SetScreenshotWindowVisibility(bool hide)
85{
86 ScreenshotWindow *scw = dynamic_cast<ScreenshotWindow *>(FindWindowById(WC_SCREENSHOT, 0));
87
88 if (scw == nullptr) return;
89
90 if (hide) {
91 /* Set dirty the screen area where the window is covering (not the window itself), then move window off screen. */
92 scw->SetDirty();
93 scw->left += 2 * _screen.width;
94 } else {
95 /* Return window to original position. */
96 scw->left -= 2 * _screen.width;
97 scw->SetDirty();
98 }
99}
gfx_func.h
Functions related to the gfx engine.
SetFill
constexpr NWidgetPart SetFill(uint16_t fill_x, uint16_t fill_y)
Widget part function for setting filling.
Definition widget_type.h:1219
SetStringTip
constexpr NWidgetPart SetStringTip(StringID string, StringID tip={})
Widget part function for setting the string and tooltip.
Definition widget_type.h:1240
EndContainer
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
Definition widget_type.h:1229
NWidget
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=INVALID_WIDGET)
Widget part function for starting a new 'real' widget.
Definition widget_type.h:1424
SetMinimalTextLines
constexpr NWidgetPart SetMinimalTextLines(uint8_t lines, uint8_t spacing, FontSize size=FS_NORMAL)
Widget part function for setting the minimal text lines.
Definition widget_type.h:1187
Window::SetDirty
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition window.cpp:966
safeguards.h
A number of safeguards to prevent using unsafe methods.
MakeScreenshotWithConfirm
void MakeScreenshotWithConfirm(ScreenshotType t)
Make a screenshot.
Definition screenshot.cpp:353
screenshot.h
Functions to make screenshots.
ScreenshotType
ScreenshotType
Type of requested screenshot.
Definition screenshot.h:16
SC_VIEWPORT
@ SC_VIEWPORT
Screenshot of viewport.
Definition screenshot.h:17
SC_ZOOMEDIN
@ SC_ZOOMEDIN
Fully zoomed in screenshot of the visible area.
Definition screenshot.h:19
SC_HEIGHTMAP
@ SC_HEIGHTMAP
Heightmap of the world.
Definition screenshot.h:22
SC_WORLD
@ SC_WORLD
World screenshot.
Definition screenshot.h:21
SC_MINIMAP
@ SC_MINIMAP
Minimap screenshot.
Definition screenshot.h:23
SC_DEFAULTZOOM
@ SC_DEFAULTZOOM
Zoomed to default zoom level screenshot of the visible area.
Definition screenshot.h:20
SetScreenshotWindowVisibility
void SetScreenshotWindowVisibility(bool hide)
Set the visibility of the screenshot window when taking a screenshot.
Definition screenshot_gui.cpp:84
screenshot_widget.h
Types related to the screenshot widgets.
WID_SC_TAKE
@ WID_SC_TAKE
Button for taking a normal screenshot.
Definition screenshot_widget.h:16
WID_SC_TAKE_WORLD
@ WID_SC_TAKE_WORLD
Button for taking a screenshot of the whole world.
Definition screenshot_widget.h:19
WID_SC_TAKE_MINIMAP
@ WID_SC_TAKE_MINIMAP
Button for taking a minimap screenshot.
Definition screenshot_widget.h:21
WID_SC_TAKE_ZOOMIN
@ WID_SC_TAKE_ZOOMIN
Button for taking a zoomed in screenshot.
Definition screenshot_widget.h:17
WID_SC_TAKE_DEFAULTZOOM
@ WID_SC_TAKE_DEFAULTZOOM
Button for taking a screenshot at normal zoom.
Definition screenshot_widget.h:18
WID_SC_TAKE_HEIGHTMAP
@ WID_SC_TAKE_HEIGHTMAP
Button for taking a heightmap "screenshot".
Definition screenshot_widget.h:20
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
Coord2D< int >
ScreenshotWindow
Definition screenshot_gui.cpp:22
ScreenshotWindow::OnClick
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition screenshot_gui.cpp:34
ScreenshotWindow::OnPaint
void OnPaint() override
The window must be repainted.
Definition screenshot_gui.cpp:29
WindowDesc
High level window description.
Definition window_gui.h:168
Window
Data structure for an opened window.
Definition window_gui.h:274
Window::FinishInitNested
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition window.cpp:1807
Window::DrawWidgets
void DrawWidgets() const
Paint all widgets of a window.
Definition widget.cpp:766
Window::CreateNestedTree
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
Definition window.cpp:1797
Window::left
int left
x position of left edge of the window
Definition window_gui.h:310
WWT_PUSHTXTBTN
@ WWT_PUSHTXTBTN
Normal push-button (no toggle button) with text caption.
Definition widget_type.h:104
NWID_HORIZONTAL
@ NWID_HORIZONTAL
Horizontal container.
Definition widget_type.h:66
WWT_STICKYBOX
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition widget_type.h:57
WWT_SHADEBOX
@ WWT_SHADEBOX
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition widget_type.h:55
WWT_CAPTION
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition widget_type.h:52
NWID_VERTICAL
@ NWID_VERTICAL
Vertical container.
Definition widget_type.h:68
WWT_CLOSEBOX
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition widget_type.h:60
EqualSize
@ EqualSize
Containers should keep all their (resizing) children equally large.
Definition widget_type.h:541
CloseWindowById
void CloseWindowById(WindowClass cls, WindowNumber number, bool force, int data)
Close a window by its class and window number (if it is open).
Definition window.cpp:1193
FindWindowById
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition window.cpp:1151
window_func.h
Window functions not directly related to making/drawing windows.
window_gui.h
Functions, definitions and such used only by the GUI.
WDP_AUTO
@ WDP_AUTO
Find a place automatically.
Definition window_gui.h:144
WidgetID
int WidgetID
Widget ID.
Definition window_type.h:20
WC_NONE
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition window_type.h:50
WC_SCREENSHOT
@ WC_SCREENSHOT
Screenshot window; Window numbers:
Definition window_type.h:727