From c16b97e5cd08eb3bd23f8b726263b4dfc2ade39a Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Sat, 3 Sep 2022 11:22:22 +0200 Subject: [PATCH] Add option to save screenshot to custom path --- data/locale/en-US.ini | 4 +- data/locale/es-ES.ini | 1 - data/locale/tr-TR.ini | 1 - data/locale/zh-CN.ini | 1 - src/macro-core/macro-action-screenshot.cpp | 89 ++++++++++++++++++++-- src/macro-core/macro-action-screenshot.hpp | 18 +++++ 6 files changed, 105 insertions(+), 9 deletions(-) diff --git a/data/locale/en-US.ini b/data/locale/en-US.ini index f2eb9319..2b01cb9a 100644 --- a/data/locale/en-US.ini +++ b/data/locale/en-US.ini @@ -481,8 +481,10 @@ AdvSceneSwitcher.action.random.entry="Randomly run any of the following macros ( AdvSceneSwitcher.action.systray="System tray notification" AdvSceneSwitcher.action.systray.entry="Show notification: {{message}}" AdvSceneSwitcher.action.screenshot="Screenshot" +AdvSceneSwitcher.action.screenshot.save.default="Default" +AdvSceneSwitcher.action.screenshot.save.custom="Custom" AdvSceneSwitcher.action.screenshot.mainOutput="OBS's main output" -AdvSceneSwitcher.action.screenshot.entry="Screenshot {{sources}}" +AdvSceneSwitcher.action.screenshot.entry="Screenshot {{sources}} and save to {{saveType}} location" AdvSceneSwitcher.action.profile="Profile" AdvSceneSwitcher.action.profile.entry="Switch active profile to {{profiles}}" AdvSceneSwitcher.action.sceneCollection="Scene collection" diff --git a/data/locale/es-ES.ini b/data/locale/es-ES.ini index 854f8a73..9c862988 100644 --- a/data/locale/es-ES.ini +++ b/data/locale/es-ES.ini @@ -463,7 +463,6 @@ AdvSceneSwitcher.action.systray="Notificación de la bandeja del sistema" AdvSceneSwitcher.action.systray.entry="Mostrar notificación: {{message}}" AdvSceneSwitcher.action.screenshot="Captura de pantalla" AdvSceneSwitcher.action.screenshot.mainOutput="Salida principal de OBS" -AdvSceneSwitcher.action.screenshot.entry="Captura de pantalla {{sources}}" AdvSceneSwitcher.action.profile="Perfil" AdvSceneSwitcher.action.profile.entry="Cambiar perfil activo a {{profiles}}" AdvSceneSwitcher.action.sceneCollection="Colección de escenas" diff --git a/data/locale/tr-TR.ini b/data/locale/tr-TR.ini index db64f8cf..502823b9 100644 --- a/data/locale/tr-TR.ini +++ b/data/locale/tr-TR.ini @@ -383,7 +383,6 @@ AdvSceneSwitcher.action.systray="Sistem tepsisi bildirimi" AdvSceneSwitcher.action.systray.entry="Bildirimleri göster: {{message}}" AdvSceneSwitcher.action.screenshot="Ekran görüntüsü" AdvSceneSwitcher.action.screenshot.mainOutput="OBS'nin ana çıkışı" -AdvSceneSwitcher.action.screenshot.entry="Ekran görüntüsü {{sources}}" AdvSceneSwitcher.action.profile="Profil" AdvSceneSwitcher.action.profile.entry="Aktif profili şununla değiştir: {{profiles}}" AdvSceneSwitcher.action.sceneCollection="Sahne koleksiyonu" diff --git a/data/locale/zh-CN.ini b/data/locale/zh-CN.ini index 3fc048ce..deb9137e 100644 --- a/data/locale/zh-CN.ini +++ b/data/locale/zh-CN.ini @@ -424,7 +424,6 @@ AdvSceneSwitcher.action.systray="系统托盘通知" AdvSceneSwitcher.action.systray.entry="显示通知: {{message}}" AdvSceneSwitcher.action.screenshot="截图" AdvSceneSwitcher.action.screenshot.mainOutput="OBS 预览画面" -AdvSceneSwitcher.action.screenshot.entry="截图 {{sources}}" AdvSceneSwitcher.action.profile="配置文件" AdvSceneSwitcher.action.profile.entry="将活动配置文件切换到 {{profiles}}" AdvSceneSwitcher.action.sceneCollection="场景集合" diff --git a/src/macro-core/macro-action-screenshot.cpp b/src/macro-core/macro-action-screenshot.cpp index 8ab0c5d2..86895131 100644 --- a/src/macro-core/macro-action-screenshot.cpp +++ b/src/macro-core/macro-action-screenshot.cpp @@ -9,7 +9,7 @@ bool MacroActionScreenshot::_registered = MacroActionFactory::Register( {MacroActionScreenshot::Create, MacroActionScreenshotEdit::Create, "AdvSceneSwitcher.action.screenshot"}); -bool MacroActionScreenshot::PerformAction() +void MacroActionScreenshot::FrontendScreenshot() { if (_source) { auto s = obs_weak_source_get_source(_source); @@ -18,6 +18,29 @@ bool MacroActionScreenshot::PerformAction() } else { obs_frontend_take_screenshot(); } +} + +void MacroActionScreenshot::CustomScreenshot() +{ + auto s = obs_weak_source_get_source(_source); + _screenshot.~ScreenshotHelper(); + new (&_screenshot) ScreenshotHelper(s, true, _path); + obs_source_release(s); +} + +bool MacroActionScreenshot::PerformAction() +{ + switch (_saveType) { + case MacroActionScreenshot::SaveType::OBS_DEFAULT: + FrontendScreenshot(); + break; + case MacroActionScreenshot::SaveType::CUSTOM: + CustomScreenshot(); + break; + default: + break; + } + return true; } @@ -31,6 +54,8 @@ bool MacroActionScreenshot::Save(obs_data_t *obj) { MacroAction::Save(obj); obs_data_set_string(obj, "source", GetWeakSourceName(_source).c_str()); + obs_data_set_int(obj, "saveType", static_cast(_saveType)); + obs_data_set_string(obj, "savePath", _path.c_str()); return true; } @@ -39,6 +64,8 @@ bool MacroActionScreenshot::Load(obs_data_t *obj) MacroAction::Load(obj); const char *sourceName = obs_data_get_string(obj, "source"); _source = GetWeakSourceByName(sourceName); + _saveType = static_cast(obs_data_get_int(obj, "saveType")); + _path = obs_data_get_string(obj, "savePath"); return true; } @@ -57,25 +84,44 @@ void addOBSMainOutputEntry(QComboBox *cb) "AdvSceneSwitcher.action.screenshot.mainOutput")); } +void populateSaveTypeSelection(QComboBox *list) +{ + list->addItem(obs_module_text( + "AdvSceneSwitcher.action.screenshot.save.default")); + list->addItem(obs_module_text( + "AdvSceneSwitcher.action.screenshot.save.custom")); +} + MacroActionScreenshotEdit::MacroActionScreenshotEdit( QWidget *parent, std::shared_ptr entryData) - : QWidget(parent) + : QWidget(parent), + _sources(new QComboBox()), + _saveType(new QComboBox()), + _savePath(new FileSelection(FileSelection::Type::WRITE, this)) { - _sources = new QComboBox(); populateVideoSelection(_sources, true, false); addOBSMainOutputEntry(_sources); + populateSaveTypeSelection(_saveType); QWidget::connect(_sources, SIGNAL(currentTextChanged(const QString &)), this, SLOT(SourceChanged(const QString &))); + QWidget::connect(_saveType, SIGNAL(currentIndexChanged(int)), this, + SLOT(SaveTypeChanged(int))); + QWidget::connect(_savePath, SIGNAL(PathChanged(const QString &)), this, + SLOT(PathChanged(const QString &))); - QHBoxLayout *mainLayout = new QHBoxLayout; + QHBoxLayout *layout = new QHBoxLayout; std::unordered_map widgetPlaceholders = { {"{{sources}}", _sources}, + {"{{saveType}}", _saveType}, }; placeWidgets( obs_module_text("AdvSceneSwitcher.action.screenshot.entry"), - mainLayout, widgetPlaceholders); + layout, widgetPlaceholders); + QVBoxLayout *mainLayout = new QVBoxLayout; + mainLayout->addLayout(layout); + mainLayout->addWidget(_savePath); setLayout(mainLayout); _entryData = entryData; @@ -95,6 +141,29 @@ void MacroActionScreenshotEdit::UpdateEntryData() } else { _sources->setCurrentIndex(0); } + _saveType->setCurrentIndex(static_cast(_entryData->_saveType)); + _savePath->SetPath(QString::fromStdString(_entryData->_path)); + SetWidgetVisibility(); +} + +void MacroActionScreenshotEdit::SaveTypeChanged(int index) +{ + if (_loading || !_entryData) { + } + + _entryData->_saveType = + static_cast(index); + SetWidgetVisibility(); +} + +void MacroActionScreenshotEdit::PathChanged(const QString &text) +{ + if (_loading || !_entryData) { + return; + } + + std::lock_guard lock(switcher->m); + _entryData->_path = text.toUtf8().constData(); } void MacroActionScreenshotEdit::SourceChanged(const QString &text) @@ -108,3 +177,13 @@ void MacroActionScreenshotEdit::SourceChanged(const QString &text) emit HeaderInfoChanged( QString::fromStdString(_entryData->GetShortDesc())); } + +void MacroActionScreenshotEdit::SetWidgetVisibility() +{ + if (!_entryData) { + return; + } + _savePath->setVisible(_entryData->_saveType == + MacroActionScreenshot::SaveType::CUSTOM); + adjustSize(); +} diff --git a/src/macro-core/macro-action-screenshot.hpp b/src/macro-core/macro-action-screenshot.hpp index b744026f..cdd463ca 100644 --- a/src/macro-core/macro-action-screenshot.hpp +++ b/src/macro-core/macro-action-screenshot.hpp @@ -1,5 +1,7 @@ #pragma once #include "macro-action-edit.hpp" +#include "file-selection.hpp" +#include "screenshot-helper.hpp" #include @@ -17,8 +19,18 @@ public: return std::make_shared(m); } OBSWeakSource _source; + enum class SaveType { + OBS_DEFAULT, + CUSTOM, + }; + SaveType _saveType = SaveType::OBS_DEFAULT; + std::string _path = obs_module_text("AdvSceneSwitcher.enterPath"); private: + void FrontendScreenshot(); + void CustomScreenshot(); + + ScreenshotHelper _screenshot; static bool _registered; static const std::string id; }; @@ -41,13 +53,19 @@ public: } private slots: void SourceChanged(const QString &text); + void SaveTypeChanged(int index); + void PathChanged(const QString &text); signals: void HeaderInfoChanged(const QString &); protected: QComboBox *_sources; + QComboBox *_saveType; + FileSelection *_savePath; std::shared_ptr _entryData; private: + void SetWidgetVisibility(); + bool _loading = true; };