Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup: Remove redundant implementation of TakeScreenshot #8224

Merged
merged 3 commits into from
Jun 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/screenshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "company_func.h"
#include "strings_func.h"
#include "error.h"
#include "textbuf_gui.h"
#include "window_gui.h"
#include "window_func.h"
#include "tile_map.h"
Expand Down Expand Up @@ -831,11 +832,52 @@ bool MakeHeightmapScreenshot(const char *filename)
return sf->proc(filename, HeightmapCallback, nullptr, MapSizeX(), MapSizeY(), 8, palette);
}

static ScreenshotType _confirmed_screenshot_type; ///< Screenshot type the current query is about to confirm.

/**
* Callback on the confirmation window for huge screenshots.
* @param w Window with viewport
* @param confirmed true on confirmation
*/
static void ScreenshotConfirmationCallback(Window *w, bool confirmed)
{
if (confirmed) MakeScreenshot(_confirmed_screenshot_type, nullptr);
}

/**
* Make a screenshot.
techgeeknz marked this conversation as resolved.
Show resolved Hide resolved
* Ask for confirmation first if the screenshot will be huge.
* @param t Screenshot type: World, defaultzoom, heightmap or viewport screenshot
* @see MakeScreenshot
*/
void MakeScreenshotWithConfirm(ScreenshotType t)
{
ViewPort vp;
SetupScreenshotViewport(t, &vp);

bool heightmap_or_minimap = t == SC_HEIGHTMAP || t == SC_MINIMAP;
uint64_t width = (heightmap_or_minimap ? MapSizeX() : vp.width);
uint64_t height = (heightmap_or_minimap ? MapSizeY() : vp.height);

if (width * height > 8192 * 8192) {
/* Ask for confirmation */
_confirmed_screenshot_type = t;
SetDParam(0, width);
SetDParam(1, height);
ShowQuery(STR_WARNING_SCREENSHOT_SIZE_CAPTION, STR_WARNING_SCREENSHOT_SIZE_MESSAGE, nullptr, ScreenshotConfirmationCallback);
} else {
/* Less than 64M pixels, just do it */
MakeScreenshot(t, nullptr);
}
}

/**
* Make an actual screenshot.
* Make a screenshot.
* Unconditionally take a screenshot of the requested type.
* @param t the type of screenshot to make.
* @param name the name to give to the screenshot.
* @return true iff the screenshot was made successfully
* @see MakeScreenshotWithConfirm
*/
bool MakeScreenshot(ScreenshotType t, const char *name)
{
Expand Down
1 change: 1 addition & 0 deletions src/screenshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ enum ScreenshotType {

void SetupScreenshotViewport(ScreenshotType t, struct ViewPort *vp);
bool MakeHeightmapScreenshot(const char *filename);
void MakeScreenshotWithConfirm(ScreenshotType t);
bool MakeScreenshot(ScreenshotType t, const char *name);
bool MakeMinimapWorldScreenshot();

Expand Down
52 changes: 9 additions & 43 deletions src/screenshot_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,26 @@
/** @file screenshot_gui.cpp GUI functions related to screenshots. */

#include "stdafx.h"
#include "gui.h"
#include "viewport_func.h"
#include "window_func.h"
#include "window_gui.h"
#include "screenshot.h"
#include "textbuf_gui.h"
#include "strings_func.h"

#include "widgets/screenshot_widget.h"

#include "table/strings.h"

static ScreenshotType _screenshot_type;

struct ScreenshotWindow : Window {
ScreenshotWindow(WindowDesc *desc) : Window(desc) {
ScreenshotWindow(WindowDesc *desc) : Window(desc)
{
this->CreateNestedTree();
this->FinishInitNested();
}

void OnPaint() override {
void OnPaint() override
{
this->DrawWidgets();
}

void OnClick(Point pt, int widget, int click_count) override {
void OnClick(Point pt, int widget, int click_count) override
{
if (widget < 0) return;
ScreenshotType st;
switch (widget) {
Expand All @@ -44,37 +39,7 @@ struct ScreenshotWindow : Window {
case WID_SC_TAKE_HEIGHTMAP: st = SC_HEIGHTMAP; break;
case WID_SC_TAKE_MINIMAP: st = SC_MINIMAP; break;
}
TakeScreenshot(st);
}

/**
* Make a screenshot.
* Ask for confirmation if the screenshot will be huge.
* @param t Screenshot type: World, defaultzoom, heightmap or viewport screenshot
*/
static void TakeScreenshot(ScreenshotType st) {
ViewPort vp;
SetupScreenshotViewport(st, &vp);
if ((uint64)vp.width * (uint64)vp.height > 8192 * 8192) {
/* Ask for confirmation */
_screenshot_type = st;
SetDParam(0, vp.width);
SetDParam(1, vp.height);
ShowQuery(STR_WARNING_SCREENSHOT_SIZE_CAPTION, STR_WARNING_SCREENSHOT_SIZE_MESSAGE, nullptr, ScreenshotConfirmationCallback);
}
else {
/* Less than 64M pixels, just do it */
MakeScreenshot(st, nullptr);
}
}

/**
* Callback on the confirmation window for huge screenshots.
* @param w Window with viewport
* @param confirmed true on confirmation
*/
static void ScreenshotConfirmationCallback(Window *w, bool confirmed) {
if (confirmed) MakeScreenshot(_screenshot_type, nullptr);
MakeScreenshotWithConfirm(st);
}
};

Expand Down Expand Up @@ -102,7 +67,8 @@ static WindowDesc _screenshot_window_desc(
_nested_screenshot, lengthof(_nested_screenshot)
);

void ShowScreenshotWindow() {
void ShowScreenshotWindow()
{
DeleteWindowById(WC_SCREENSHOT, 0);
new ScreenshotWindow(&_screenshot_window_desc);
}
49 changes: 8 additions & 41 deletions src/toolbar_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ RailType _last_built_railtype;
RoadType _last_built_roadtype;
RoadType _last_built_tramtype;

static ScreenshotType _confirmed_screenshot_type; ///< Screenshot type the current query is about to confirm.

/** Toobar modes */
enum ToolbarMode {
TB_NORMAL,
Expand Down Expand Up @@ -1071,37 +1069,6 @@ static CallBackFunction ToolbarHelpClick(Window *w)
return CBF_NONE;
}

/**
* Callback on the confirmation window for huge screenshots.
* @param w Window with viewport
* @param confirmed true on confirmation
*/
static void ScreenshotConfirmCallback(Window *w, bool confirmed)
{
if (confirmed) MakeScreenshot(_confirmed_screenshot_type, nullptr);
}

/**
* Make a screenshot of the world.
* Ask for confirmation if the screenshot will be huge.
* @param t Screenshot type: World or viewport screenshot
*/
static void MenuClickScreenshot(ScreenshotType t)
{
ViewPort vp;
SetupScreenshotViewport(t, &vp);
if ((uint64)vp.width * (uint64)vp.height > 8192 * 8192) {
/* Ask for confirmation */
SetDParam(0, vp.width);
SetDParam(1, vp.height);
_confirmed_screenshot_type = t;
ShowQuery(STR_WARNING_SCREENSHOT_SIZE_CAPTION, STR_WARNING_SCREENSHOT_SIZE_MESSAGE, nullptr, ScreenshotConfirmCallback);
} else {
/* Less than 64M pixels, just do it */
MakeScreenshot(t, nullptr);
}
}

/**
* Toggle drawing of sprites' bounding boxes.
* @note has only an effect when newgrf_developer_tools are active.
Expand Down Expand Up @@ -2119,10 +2086,10 @@ struct MainToolbarWindow : Window {
case MTHK_BUILD_TREES: ShowBuildTreesToolbar(); break;
case MTHK_MUSIC: ShowMusicWindow(); break;
case MTHK_AI_DEBUG: ShowAIDebugWindow(); break;
case MTHK_SMALL_SCREENSHOT: MenuClickScreenshot(SC_VIEWPORT); break;
case MTHK_ZOOMEDIN_SCREENSHOT: MenuClickScreenshot(SC_ZOOMEDIN); break;
case MTHK_DEFAULTZOOM_SCREENSHOT: MenuClickScreenshot(SC_DEFAULTZOOM); break;
case MTHK_GIANT_SCREENSHOT: MenuClickScreenshot(SC_WORLD); break;
case MTHK_SMALL_SCREENSHOT: MakeScreenshotWithConfirm(SC_VIEWPORT); break;
case MTHK_ZOOMEDIN_SCREENSHOT: MakeScreenshotWithConfirm(SC_ZOOMEDIN); break;
case MTHK_DEFAULTZOOM_SCREENSHOT: MakeScreenshotWithConfirm(SC_DEFAULTZOOM); break;
case MTHK_GIANT_SCREENSHOT: MakeScreenshotWithConfirm(SC_WORLD); break;
case MTHK_CHEATS: if (!_networking) ShowCheatWindow(); break;
case MTHK_TERRAFORM: ShowTerraformToolbar(); break;
case MTHK_EXTRA_VIEWPORT: ShowExtraViewPortWindowForTileUnderCursor(); break;
Expand Down Expand Up @@ -2494,10 +2461,10 @@ struct ScenarioEditorToolbarWindow : Window {
case MTEHK_SIGN: cbf = ToolbarScenPlaceSign(this); break;
case MTEHK_MUSIC: ShowMusicWindow(); break;
case MTEHK_LANDINFO: cbf = PlaceLandBlockInfo(); break;
case MTEHK_SMALL_SCREENSHOT: MenuClickScreenshot(SC_VIEWPORT); break;
case MTEHK_ZOOMEDIN_SCREENSHOT: MenuClickScreenshot(SC_ZOOMEDIN); break;
case MTEHK_DEFAULTZOOM_SCREENSHOT: MenuClickScreenshot(SC_DEFAULTZOOM); break;
case MTEHK_GIANT_SCREENSHOT: MenuClickScreenshot(SC_WORLD); break;
case MTEHK_SMALL_SCREENSHOT: MakeScreenshotWithConfirm(SC_VIEWPORT); break;
case MTEHK_ZOOMEDIN_SCREENSHOT: MakeScreenshotWithConfirm(SC_ZOOMEDIN); break;
case MTEHK_DEFAULTZOOM_SCREENSHOT: MakeScreenshotWithConfirm(SC_DEFAULTZOOM); break;
case MTEHK_GIANT_SCREENSHOT: MakeScreenshotWithConfirm(SC_WORLD); break;
case MTEHK_ZOOM_IN: ToolbarZoomInClick(this); break;
case MTEHK_ZOOM_OUT: ToolbarZoomOutClick(this); break;
case MTEHK_TERRAFORM: ShowEditorTerraformToolbar(); break;
Expand Down