diff --git a/CMakeLists.txt b/CMakeLists.txt index d919bf8e..9d703ff4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/data/locale/en-US.ini b/data/locale/en-US.ini index b81228db..a587d256 100644 --- a/data/locale/en-US.ini +++ b/data/locale/en-US.ini @@ -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" diff --git a/src/headers/macro-action-screenshot.hpp b/src/headers/macro-action-screenshot.hpp new file mode 100644 index 00000000..a167f05a --- /dev/null +++ b/src/headers/macro-action-screenshot.hpp @@ -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 Create() + { + return std::make_shared(); + } + OBSWeakSource _source; + +private: + static bool _registered; + static const std::string id; +}; + +class MacroActionScreenshotEdit : public QWidget { + Q_OBJECT + +public: + MacroActionScreenshotEdit( + QWidget *parent, + std::shared_ptr entryData = nullptr); + void UpdateEntryData(); + static QWidget *Create(QWidget *parent, + std::shared_ptr action) + { + return new MacroActionScreenshotEdit( + parent, + std::dynamic_pointer_cast( + action)); + } +private slots: + void SourceChanged(const QString &text); +signals: + void HeaderInfoChanged(const QString &); + +protected: + QComboBox *_sources; + std::shared_ptr _entryData; + +private: + bool _loading = true; +}; diff --git a/src/macro-action-screenshot.cpp b/src/macro-action-screenshot.cpp new file mode 100644 index 00000000..6757bd5d --- /dev/null +++ b/src/macro-action-screenshot.cpp @@ -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 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 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 lock(switcher->m); + _entryData->_source = GetWeakSourceByQString(text); + emit HeaderInfoChanged( + QString::fromStdString(_entryData->GetShortDesc())); +}