Skip to content

Commit b9afb13

Browse files
authored
SnapshotDumper: trigger snapshot on mouse click (#781)
1 parent faa8ac7 commit b9afb13

File tree

2 files changed

+93
-22
lines changed

2 files changed

+93
-22
lines changed

SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_SnapshotDumper.cpp

Lines changed: 79 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
#include "Common/Cpp/PrettyPrint.h"
99
#include "CommonFramework/Globals.h"
1010
#include "CommonFramework/VideoPipeline/VideoFeed.h"
11+
#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"
1112
#include "NintendoSwitch_SnapshotDumper.h"
13+
// #include <iostream>
14+
// using std::cout;
15+
// using std::endl;
1216

1317
namespace PokemonAutomation{
1418
namespace NintendoSwitch{
@@ -27,14 +31,21 @@ SnapshotDumper_Descriptor::SnapshotDumper_Descriptor()
2731
)
2832
{}
2933

30-
34+
SnapshotDumper::~SnapshotDumper(){
35+
CLICK_TO_SNAPSHOT.remove_listener(*this);
36+
}
3137

3238
SnapshotDumper::SnapshotDumper()
3339
: PERIOD_MILLISECONDS(
3440
"<b>Snapshot Period (milliseconds):</b><br>Take screenshot every this many milliseconds.",
3541
LockMode::UNLOCK_WHILE_RUNNING,
3642
1000
3743
)
44+
, CLICK_TO_SNAPSHOT(
45+
"<b>Click screen to trigger snapshot</b><br>",
46+
LockMode::UNLOCK_WHILE_RUNNING,
47+
false
48+
)
3849
, FORMAT(
3950
"<b>Image Format:</b>",
4051
{
@@ -45,34 +56,87 @@ SnapshotDumper::SnapshotDumper()
4556
Format::JPG
4657
)
4758
{
59+
PA_ADD_OPTION(CLICK_TO_SNAPSHOT);
4860
PA_ADD_OPTION(PERIOD_MILLISECONDS);
4961
PA_ADD_OPTION(FORMAT);
62+
CLICK_TO_SNAPSHOT.add_listener(*this);
5063
}
5164

65+
void SnapshotDumper::on_config_value_changed(void* object){
66+
PERIOD_MILLISECONDS.set_visibility(CLICK_TO_SNAPSHOT ? ConfigOptionState::HIDDEN : ConfigOptionState::ENABLED);
67+
}
68+
69+
class SnapshotTrigger : public VideoOverlay::MouseListener{
70+
public:
71+
~SnapshotTrigger(){
72+
detach();
73+
}
74+
SnapshotTrigger(VideoStream& stream, VideoOverlay& overlay, Format format)
75+
: m_stream(stream)
76+
, m_overlay(overlay)
77+
// , m_overlay_set(overlay)
78+
, m_format(format)
79+
{
80+
try{
81+
overlay.add_listener(*this);
82+
}catch (...){
83+
detach();
84+
throw;
85+
}
86+
}
87+
88+
virtual void on_mouse_press(double x, double y) override{
89+
dump_snapshot(m_stream, "ScreenshotDumper", to_format_string(m_format));
90+
}
91+
92+
93+
private:
94+
void detach(){
95+
m_overlay.remove_listener(*this);
96+
}
97+
98+
private:
99+
VideoStream& m_stream;
100+
VideoOverlay& m_overlay;
101+
// VideoOverlaySet m_overlay_set;
102+
Format m_format;
103+
// std::mutex m_lock;
104+
105+
};
106+
52107
void SnapshotDumper::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){
53108
std::string folder_path = USER_FILE_PATH() + "ScreenshotDumper/";
54109
QDir().mkpath(folder_path.c_str());
55-
while (true){
56-
VideoSnapshot last = env.console.video().snapshot();
57-
std::string filename = folder_path + now_to_filestring();
58-
switch (FORMAT){
59-
case Format::PNG:
60-
filename += ".png";
61-
break;
62-
case Format::JPG:
63-
filename += ".jpg";
64-
break;
110+
if (CLICK_TO_SNAPSHOT){
111+
SnapshotTrigger trigger(env.console, env.console.overlay(), FORMAT);
112+
context.wait_until_cancel();
113+
}else{
114+
while (true){
115+
VideoSnapshot last = env.console.video().snapshot();
116+
std::string filename = folder_path + now_to_filestring();
117+
last->save(filename + to_format_string(FORMAT));
118+
context.wait_until(last.timestamp + std::chrono::milliseconds(PERIOD_MILLISECONDS));
65119
}
66-
last->save(filename);
67-
context.wait_until(last.timestamp + std::chrono::milliseconds(PERIOD_MILLISECONDS));
68120
}
69121
}
70122

71-
void dump_snapshot(VideoStream& stream, std::string folder_name){
123+
std::string to_format_string(Format format){
124+
switch (format){
125+
case Format::PNG:
126+
return ".png";
127+
case Format::JPG:
128+
return ".jpg";
129+
default:
130+
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "to_format_string: Unknown Format enum.");
131+
break;
132+
}
133+
}
134+
135+
void dump_snapshot(VideoStream& stream, std::string folder_name, std::string format){
72136
std::string folder_path = USER_FILE_PATH() + folder_name + "/";
73137
QDir().mkpath(folder_path.c_str());
74138
VideoSnapshot last = stream.video().snapshot();
75-
std::string filename = folder_path + now_to_filestring() + ".png";
139+
std::string filename = folder_path + now_to_filestring() + format;
76140
last->save(filename);
77141
}
78142

SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_SnapshotDumper.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "Common/Cpp/Options/SimpleIntegerOption.h"
1111
#include "Common/Cpp/Options/EnumDropdownOption.h"
12+
#include "Common/Cpp/Options/BooleanCheckBoxOption.h"
1213
#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h"
1314

1415
namespace PokemonAutomation{
@@ -20,25 +21,31 @@ class SnapshotDumper_Descriptor : public SingleSwitchProgramDescriptor{
2021
SnapshotDumper_Descriptor();
2122
};
2223

24+
enum class Format{
25+
PNG,
26+
JPG,
27+
};
2328

24-
class SnapshotDumper : public SingleSwitchProgramInstance{
29+
class SnapshotDumper : public SingleSwitchProgramInstance, public ConfigOption::Listener{
2530
public:
31+
~SnapshotDumper();
2632
SnapshotDumper();
2733

2834
virtual void program(SingleSwitchProgramEnvironment& env, ProControllerContext& context) override;
2935

3036
private:
31-
SimpleIntegerOption<uint32_t> PERIOD_MILLISECONDS;
37+
virtual void on_config_value_changed(void* object) override;
3238

33-
enum class Format{
34-
PNG,
35-
JPG,
36-
};
39+
private:
40+
SimpleIntegerOption<uint32_t> PERIOD_MILLISECONDS;
41+
BooleanCheckBoxOption CLICK_TO_SNAPSHOT;
3742
EnumDropdownOption<Format> FORMAT;
3843
};
3944

45+
std::string to_format_string(Format format);
46+
4047
// takes a snapshot of the screen and saves it to the given folder_name
41-
void dump_snapshot(VideoStream& stream, std::string folder_name = "ScreenshotDumper");
48+
void dump_snapshot(VideoStream& stream, std::string folder_name = "ScreenshotDumper", std::string format = ".png");
4249

4350
}
4451
}

0 commit comments

Comments
 (0)