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
1317namespace PokemonAutomation {
1418namespace 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
3238SnapshotDumper::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+
52107void 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
0 commit comments