Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
#include "Common/Cpp/PrettyPrint.h"
#include "CommonFramework/Globals.h"
#include "CommonFramework/VideoPipeline/VideoFeed.h"
#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"
#include "NintendoSwitch_SnapshotDumper.h"
// #include <iostream>
// using std::cout;
// using std::endl;

namespace PokemonAutomation{
namespace NintendoSwitch{
Expand All @@ -27,14 +31,21 @@ SnapshotDumper_Descriptor::SnapshotDumper_Descriptor()
)
{}


SnapshotDumper::~SnapshotDumper(){
CLICK_TO_SNAPSHOT.remove_listener(*this);
}

SnapshotDumper::SnapshotDumper()
: PERIOD_MILLISECONDS(
"<b>Snapshot Period (milliseconds):</b><br>Take screenshot every this many milliseconds.",
LockMode::UNLOCK_WHILE_RUNNING,
1000
)
, CLICK_TO_SNAPSHOT(
"<b>Click screen to trigger snapshot</b><br>",
LockMode::UNLOCK_WHILE_RUNNING,
false
)
, FORMAT(
"<b>Image Format:</b>",
{
Expand All @@ -45,34 +56,87 @@ SnapshotDumper::SnapshotDumper()
Format::JPG
)
{
PA_ADD_OPTION(CLICK_TO_SNAPSHOT);
PA_ADD_OPTION(PERIOD_MILLISECONDS);
PA_ADD_OPTION(FORMAT);
CLICK_TO_SNAPSHOT.add_listener(*this);
}

void SnapshotDumper::on_config_value_changed(void* object){
PERIOD_MILLISECONDS.set_visibility(CLICK_TO_SNAPSHOT ? ConfigOptionState::HIDDEN : ConfigOptionState::ENABLED);
}

class SnapshotTrigger : public VideoOverlay::MouseListener{
public:
~SnapshotTrigger(){
detach();
}
SnapshotTrigger(VideoStream& stream, VideoOverlay& overlay, Format format)
: m_stream(stream)
, m_overlay(overlay)
// , m_overlay_set(overlay)
, m_format(format)
{
try{
overlay.add_listener(*this);
}catch (...){
detach();
throw;
}
}

virtual void on_mouse_press(double x, double y) override{
dump_snapshot(m_stream, "ScreenshotDumper", to_format_string(m_format));
}


private:
void detach(){
m_overlay.remove_listener(*this);
}

private:
VideoStream& m_stream;
VideoOverlay& m_overlay;
// VideoOverlaySet m_overlay_set;
Format m_format;
// std::mutex m_lock;

};

void SnapshotDumper::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){
std::string folder_path = USER_FILE_PATH() + "ScreenshotDumper/";
QDir().mkpath(folder_path.c_str());
while (true){
VideoSnapshot last = env.console.video().snapshot();
std::string filename = folder_path + now_to_filestring();
switch (FORMAT){
case Format::PNG:
filename += ".png";
break;
case Format::JPG:
filename += ".jpg";
break;
if (CLICK_TO_SNAPSHOT){
SnapshotTrigger trigger(env.console, env.console.overlay(), FORMAT);
context.wait_until_cancel();
}else{
while (true){
VideoSnapshot last = env.console.video().snapshot();
std::string filename = folder_path + now_to_filestring();
last->save(filename + to_format_string(FORMAT));
context.wait_until(last.timestamp + std::chrono::milliseconds(PERIOD_MILLISECONDS));
}
last->save(filename);
context.wait_until(last.timestamp + std::chrono::milliseconds(PERIOD_MILLISECONDS));
}
}

void dump_snapshot(VideoStream& stream, std::string folder_name){
std::string to_format_string(Format format){
switch (format){
case Format::PNG:
return ".png";
case Format::JPG:
return ".jpg";
default:
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "to_format_string: Unknown Format enum.");
break;
}
}

void dump_snapshot(VideoStream& stream, std::string folder_name, std::string format){
std::string folder_path = USER_FILE_PATH() + folder_name + "/";
QDir().mkpath(folder_path.c_str());
VideoSnapshot last = stream.video().snapshot();
std::string filename = folder_path + now_to_filestring() + ".png";
std::string filename = folder_path + now_to_filestring() + format;
last->save(filename);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "Common/Cpp/Options/SimpleIntegerOption.h"
#include "Common/Cpp/Options/EnumDropdownOption.h"
#include "Common/Cpp/Options/BooleanCheckBoxOption.h"
#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h"

namespace PokemonAutomation{
Expand All @@ -20,25 +21,31 @@ class SnapshotDumper_Descriptor : public SingleSwitchProgramDescriptor{
SnapshotDumper_Descriptor();
};

enum class Format{
PNG,
JPG,
};

class SnapshotDumper : public SingleSwitchProgramInstance{
class SnapshotDumper : public SingleSwitchProgramInstance, public ConfigOption::Listener{
public:
~SnapshotDumper();
SnapshotDumper();

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

private:
SimpleIntegerOption<uint32_t> PERIOD_MILLISECONDS;
virtual void on_config_value_changed(void* object) override;

enum class Format{
PNG,
JPG,
};
private:
SimpleIntegerOption<uint32_t> PERIOD_MILLISECONDS;
BooleanCheckBoxOption CLICK_TO_SNAPSHOT;
EnumDropdownOption<Format> FORMAT;
};

std::string to_format_string(Format format);

// takes a snapshot of the screen and saves it to the given folder_name
void dump_snapshot(VideoStream& stream, std::string folder_name = "ScreenshotDumper");
void dump_snapshot(VideoStream& stream, std::string folder_name = "ScreenshotDumper", std::string format = ".png");

}
}
Expand Down