From 9c4fe38c97792dfb9e32704e7bacaf0809c46b66 Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Tue, 27 Dec 2022 18:38:24 +0100 Subject: [PATCH] Add variable support for screenshot action --- src/macro-core/macro-action-screenshot.cpp | 61 ++++++++++++++-------- src/macro-core/macro-action-screenshot.hpp | 10 ++-- 2 files changed, 47 insertions(+), 24 deletions(-) diff --git a/src/macro-core/macro-action-screenshot.cpp b/src/macro-core/macro-action-screenshot.cpp index 82dad9b1..3f6025ac 100644 --- a/src/macro-core/macro-action-screenshot.cpp +++ b/src/macro-core/macro-action-screenshot.cpp @@ -2,6 +2,8 @@ #include "advanced-scene-switcher.hpp" #include "utility.hpp" +const uint32_t MacroActionScreenshot::_version = 1; + const std::string MacroActionScreenshot::id = "screenshot"; bool MacroActionScreenshot::_registered = MacroActionFactory::Register( @@ -38,7 +40,7 @@ bool MacroActionScreenshot::PerformAction() OBSWeakSource source = nullptr; switch (_targetType) { case MacroActionScreenshot::TargetType::SOURCE: - source = _source; + source = _source.GetSource(); break; case MacroActionScreenshot::TargetType::SCENE: source = _scene.GetScene(false); @@ -61,20 +63,31 @@ bool MacroActionScreenshot::PerformAction() void MacroActionScreenshot::LogAction() const { - vblog(LOG_INFO, "trigger screenshot for \"%s\"", - _targetType == TargetType::SOURCE - ? GetWeakSourceName(_source).c_str() - : _scene.ToString(true).c_str()); + switch (_targetType) { + case MacroActionScreenshot::TargetType::SOURCE: + vblog(LOG_INFO, "trigger screenshot of \"%s\"", + _source.ToString(true).c_str()); + break; + case MacroActionScreenshot::TargetType::SCENE: + vblog(LOG_INFO, "trigger screenshot of \"%s\"", + _scene.ToString(true).c_str()); + break; + case MacroActionScreenshot::TargetType::MAIN_OUTPUT: + vblog(LOG_INFO, "trigger screenshot of main output", + _scene.ToString(true).c_str()); + break; + } } bool MacroActionScreenshot::Save(obs_data_t *obj) const { MacroAction::Save(obj); _scene.Save(obj); - obs_data_set_string(obj, "source", GetWeakSourceName(_source).c_str()); + _source.Save(obj); obs_data_set_int(obj, "saveType", static_cast(_saveType)); obs_data_set_int(obj, "targetType", static_cast(_targetType)); obs_data_set_string(obj, "savePath", _path.c_str()); + obs_data_set_int(obj, "version", _version); return true; } @@ -82,12 +95,18 @@ bool MacroActionScreenshot::Load(obs_data_t *obj) { MacroAction::Load(obj); _scene.Load(obj); - const char *sourceName = obs_data_get_string(obj, "source"); - _source = GetWeakSourceByName(sourceName); + _source.Load(obj); _saveType = static_cast(obs_data_get_int(obj, "saveType")); _targetType = static_cast(obs_data_get_int(obj, "targetType")); _path = obs_data_get_string(obj, "savePath"); + + // TODO: Remove fallback for older versions + if (!obs_data_has_user_value(obj, "version")) { + if (!_source.GetSource() && !_scene.GetScene(false)) { + _targetType = TargetType::MAIN_OUTPUT; + } + } return true; } @@ -96,7 +115,7 @@ std::string MacroActionScreenshot::GetShortDesc() const if (_targetType == TargetType::SCENE) { return _scene.ToString(); } else { - return GetWeakSourceName(_source); + return _source.ToString(); } return ""; } @@ -115,6 +134,7 @@ static void populateTargetTypeSelection(QComboBox *list) "AdvSceneSwitcher.action.screenshot.type.source")); list->addItem(obs_module_text( "AdvSceneSwitcher.action.screenshot.type.scene")); + list->addItem(obs_module_text("AdvSceneSwitcher.OBSVideoOutput")); } MacroActionScreenshotEdit::MacroActionScreenshotEdit( @@ -122,21 +142,25 @@ MacroActionScreenshotEdit::MacroActionScreenshotEdit( : QWidget(parent), _scenes(new SceneSelectionWidget(this, true, false, true, true, true)), - _sources(new QComboBox()), + _sources(new SourceSelectionWidget(this, QStringList(), true)), _saveType(new QComboBox()), _targetType(new QComboBox()), _savePath(new FileSelection(FileSelection::Type::WRITE, this)) { setToolTip(obs_module_text( "AdvSceneSwitcher.action.screenshot.blackscreenNote")); - populateVideoSelection(_sources, true); + auto sources = GetVideoSourceNames(); + sources.sort(); + _sources->SetSourceNameList(sources); + populateSaveTypeSelection(_saveType); populateTargetTypeSelection(_targetType); QWidget::connect(_scenes, SIGNAL(SceneChanged(const SceneSelection &)), this, SLOT(SceneChanged(const SceneSelection &))); - QWidget::connect(_sources, SIGNAL(currentTextChanged(const QString &)), - this, SLOT(SourceChanged(const QString &))); + QWidget::connect(_sources, + SIGNAL(SourceChanged(const SourceSelection &)), this, + SLOT(SourceChanged(const SourceSelection &))); QWidget::connect(_saveType, SIGNAL(currentIndexChanged(int)), this, SLOT(SaveTypeChanged(int))); QWidget::connect(_targetType, SIGNAL(currentIndexChanged(int)), this, @@ -171,12 +195,7 @@ void MacroActionScreenshotEdit::UpdateEntryData() return; } - if (_entryData->_source) { - _sources->setCurrentText( - GetWeakSourceName(_entryData->_source).c_str()); - } else { - _sources->setCurrentIndex(1); - } + _sources->SetSource(_entryData->_source); _scenes->SetScene(_entryData->_scene); _saveType->setCurrentIndex(static_cast(_entryData->_saveType)); _targetType->setCurrentIndex(static_cast(_entryData->_targetType)); @@ -226,14 +245,14 @@ void MacroActionScreenshotEdit::PathChanged(const QString &text) _entryData->_path = text.toUtf8().constData(); } -void MacroActionScreenshotEdit::SourceChanged(const QString &text) +void MacroActionScreenshotEdit::SourceChanged(const SourceSelection &source) { if (_loading || !_entryData) { return; } std::lock_guard lock(switcher->m); - _entryData->_source = GetWeakSourceByQString(text); + _entryData->_source = source; emit HeaderInfoChanged( QString::fromStdString(_entryData->GetShortDesc())); } diff --git a/src/macro-core/macro-action-screenshot.hpp b/src/macro-core/macro-action-screenshot.hpp index 83da2585..a86dbb32 100644 --- a/src/macro-core/macro-action-screenshot.hpp +++ b/src/macro-core/macro-action-screenshot.hpp @@ -3,6 +3,7 @@ #include "file-selection.hpp" #include "scene-selection.hpp" #include "screenshot-helper.hpp" +#include "source-selection.hpp" #include @@ -27,10 +28,11 @@ public: enum class TargetType { SOURCE, SCENE, + MAIN_OUTPUT, }; TargetType _targetType = TargetType::SOURCE; SceneSelection _scene; - OBSWeakSource _source; + SourceSelection _source; std::string _path = obs_module_text("AdvSceneSwitcher.enterPath"); private: @@ -38,6 +40,8 @@ private: void CustomScreenshot(OBSWeakSource &); ScreenshotHelper _screenshot; + + const static uint32_t _version; static bool _registered; static const std::string id; }; @@ -60,7 +64,7 @@ public: } private slots: void SceneChanged(const SceneSelection &); - void SourceChanged(const QString &text); + void SourceChanged(const SourceSelection &); void SaveTypeChanged(int index); void TargetTypeChanged(int index); void PathChanged(const QString &text); @@ -69,7 +73,7 @@ signals: protected: SceneSelectionWidget *_scenes; - QComboBox *_sources; + SourceSelectionWidget *_sources; QComboBox *_saveType; QComboBox *_targetType; FileSelection *_savePath;