Add macro action for triggering screenshots

This commit is contained in:
WarmUpTill 2021-11-23 20:50:51 +01:00 committed by WarmUpTill
parent 631423a251
commit bb954a2e9a
4 changed files with 165 additions and 0 deletions

View File

@ -224,6 +224,7 @@ set(advanced-scene-switcher_HEADERS
src/headers/macro-action-scene-switch.hpp
src/headers/macro-action-scene-transform.hpp
src/headers/macro-action-scene-visibility.hpp
src/headers/macro-action-screenshot.hpp
src/headers/macro-action-source.hpp
src/headers/macro-action-streaming.hpp
src/headers/macro-action-systray.hpp
@ -318,6 +319,7 @@ set(advanced-scene-switcher_SOURCES
src/macro-action-scene-switch.cpp
src/macro-action-scene-transform.cpp
src/macro-action-scene-visibility.cpp
src/macro-action-screenshot.cpp
src/macro-action-source.cpp
src/macro-action-streaming.cpp
src/macro-action-systray.cpp

View File

@ -361,6 +361,9 @@ AdvSceneSwitcher.action.random="Random"
AdvSceneSwitcher.action.random.entry="Randomly run any of the following macros (paused macros are ignored)"
AdvSceneSwitcher.action.systray="System tray notification"
AdvSceneSwitcher.action.systray.entry="Show notification: {{message}}"
AdvSceneSwitcher.action.screenshot="Screenshot"
AdvSceneSwitcher.action.screenshot.mainOutput="OBS's main output"
AdvSceneSwitcher.action.screenshot.entry="Screenshot {{sources}}"
; Transition Tab
AdvSceneSwitcher.transitionTab.title="Transition"

View File

@ -0,0 +1,50 @@
#pragma once
#include "macro-action-edit.hpp"
class MacroActionScreenshot : public MacroAction {
public:
bool PerformAction();
void LogAction();
bool Save(obs_data_t *obj);
bool Load(obs_data_t *obj);
std::string GetShortDesc();
std::string GetId() { return id; };
static std::shared_ptr<MacroAction> Create()
{
return std::make_shared<MacroActionScreenshot>();
}
OBSWeakSource _source;
private:
static bool _registered;
static const std::string id;
};
class MacroActionScreenshotEdit : public QWidget {
Q_OBJECT
public:
MacroActionScreenshotEdit(
QWidget *parent,
std::shared_ptr<MacroActionScreenshot> entryData = nullptr);
void UpdateEntryData();
static QWidget *Create(QWidget *parent,
std::shared_ptr<MacroAction> action)
{
return new MacroActionScreenshotEdit(
parent,
std::dynamic_pointer_cast<MacroActionScreenshot>(
action));
}
private slots:
void SourceChanged(const QString &text);
signals:
void HeaderInfoChanged(const QString &);
protected:
QComboBox *_sources;
std::shared_ptr<MacroActionScreenshot> _entryData;
private:
bool _loading = true;
};

View File

@ -0,0 +1,110 @@
#include "headers/macro-action-screenshot.hpp"
#include "headers/advanced-scene-switcher.hpp"
#include "headers/utility.hpp"
const std::string MacroActionScreenshot::id = "screenshot";
bool MacroActionScreenshot::_registered = MacroActionFactory::Register(
MacroActionScreenshot::id,
{MacroActionScreenshot::Create, MacroActionScreenshotEdit::Create,
"AdvSceneSwitcher.action.screenshot"});
bool MacroActionScreenshot::PerformAction()
{
if (_source) {
auto s = obs_weak_source_get_source(_source);
obs_frontend_take_source_screenshot(s);
obs_source_release(s);
} else {
obs_frontend_take_screenshot();
}
return true;
}
void MacroActionScreenshot::LogAction()
{
vblog(LOG_INFO, "trigger screenshot for source \"%s\"",
GetWeakSourceName(_source).c_str());
}
bool MacroActionScreenshot::Save(obs_data_t *obj)
{
MacroAction::Save(obj);
obs_data_set_string(obj, "source", GetWeakSourceName(_source).c_str());
return true;
}
bool MacroActionScreenshot::Load(obs_data_t *obj)
{
MacroAction::Load(obj);
const char *sourceName = obs_data_get_string(obj, "source");
_source = GetWeakSourceByName(sourceName);
return true;
}
std::string MacroActionScreenshot::GetShortDesc()
{
if (_source) {
return GetWeakSourceName(_source);
}
return "";
}
void addOBSMainOutputEntry(QComboBox *cb)
{
cb->insertItem(
0, obs_module_text(
"AdvSceneSwitcher.action.screenshot.mainOutput"));
}
MacroActionScreenshotEdit::MacroActionScreenshotEdit(
QWidget *parent, std::shared_ptr<MacroActionScreenshot> entryData)
: QWidget(parent)
{
_sources = new QComboBox();
populateVideoSelection(_sources, false);
addOBSMainOutputEntry(_sources);
QWidget::connect(_sources, SIGNAL(currentTextChanged(const QString &)),
this, SLOT(SourceChanged(const QString &)));
QHBoxLayout *mainLayout = new QHBoxLayout;
std::unordered_map<std::string, QWidget *> widgetPlaceholders = {
{"{{sources}}", _sources},
};
placeWidgets(
obs_module_text("AdvSceneSwitcher.action.screenshot.entry"),
mainLayout, widgetPlaceholders);
setLayout(mainLayout);
_entryData = entryData;
UpdateEntryData();
_loading = false;
}
void MacroActionScreenshotEdit::UpdateEntryData()
{
if (!_entryData) {
return;
}
if (_entryData->_source) {
_sources->setCurrentText(
GetWeakSourceName(_entryData->_source).c_str());
} else {
_sources->setCurrentIndex(0);
}
}
void MacroActionScreenshotEdit::SourceChanged(const QString &text)
{
if (_loading || !_entryData) {
return;
}
std::lock_guard<std::mutex> lock(switcher->m);
_entryData->_source = GetWeakSourceByQString(text);
emit HeaderInfoChanged(
QString::fromStdString(_entryData->GetShortDesc()));
}