From 074d734b7db713c5d0fc0848719c85fc2ded5cc5 Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Sat, 2 Mar 2024 12:06:07 +0100 Subject: [PATCH] Add "Log" action This action will allow you to write custom messages to the OBS log --- data/locale/en-US.ini | 3 ++ plugins/base/CMakeLists.txt | 2 + plugins/base/macro-action-log.cpp | 89 +++++++++++++++++++++++++++++++ plugins/base/macro-action-log.hpp | 50 +++++++++++++++++ 4 files changed, 144 insertions(+) create mode 100644 plugins/base/macro-action-log.cpp create mode 100644 plugins/base/macro-action-log.hpp diff --git a/data/locale/en-US.ini b/data/locale/en-US.ini index a6070de08..dc5773ff0 100644 --- a/data/locale/en-US.ini +++ b/data/locale/en-US.ini @@ -972,6 +972,9 @@ AdvSceneSwitcher.action.window.type.maximizeWindow="Maximize window" AdvSceneSwitcher.action.window.type.minimizeWindow="Minimize window" AdvSceneSwitcher.action.window.type.closeWindow="Close window" AdvSceneSwitcher.action.window.entry="{{actions}}{{windows}}{{regex}}" +AdvSceneSwitcher.action.log="Log" +AdvSceneSwitcher.action.log.placeholder="My log message!" +AdvSceneSwitcher.action.log.entry="Write to OBS log:{{logMessage}}" ; Hotkey AdvSceneSwitcher.hotkey.startSwitcherHotkey="Start the Advanced Scene Switcher" diff --git a/plugins/base/CMakeLists.txt b/plugins/base/CMakeLists.txt index ba2f4ee98..9f2b8b7b0 100644 --- a/plugins/base/CMakeLists.txt +++ b/plugins/base/CMakeLists.txt @@ -17,6 +17,8 @@ target_sources( macro-action-hotkey.hpp macro-action-http.cpp macro-action-http.hpp + macro-action-log.cpp + macro-action-log.hpp macro-action-media.cpp macro-action-media.hpp macro-action-osc.cpp diff --git a/plugins/base/macro-action-log.cpp b/plugins/base/macro-action-log.cpp new file mode 100644 index 000000000..00ba3a5ce --- /dev/null +++ b/plugins/base/macro-action-log.cpp @@ -0,0 +1,89 @@ +#include "macro-action-log.hpp" +#include "layout-helpers.hpp" + +namespace advss { + +const std::string MacroActionLog::id = "log"; + +bool MacroActionLog::_registered = MacroActionFactory::Register( + MacroActionLog::id, {MacroActionLog::Create, MacroActionLogEdit::Create, + "AdvSceneSwitcher.action.log"}); + +bool MacroActionLog::PerformAction() +{ + blog(LOG_INFO, "%s", std::string(_logMessage).c_str()); + return true; +} + +bool MacroActionLog::Save(obs_data_t *obj) const +{ + MacroAction::Save(obj); + _logMessage.Save(obj, "logMessage"); + return true; +} + +bool MacroActionLog::Load(obs_data_t *obj) +{ + MacroAction::Load(obj); + _logMessage.Load(obj, "logMessage"); + return true; +} + +std::shared_ptr MacroActionLog::Create(Macro *m) +{ + return std::make_shared(m); +} + +std::shared_ptr MacroActionLog::Copy() const +{ + return std::make_shared(*this); +} + +void MacroActionLog::ResolveVariablesToFixedValues() +{ + _logMessage.ResolveVariables(); +} + +MacroActionLogEdit::MacroActionLogEdit( + QWidget *parent, std::shared_ptr entryData) + : QWidget(parent), + _logMessage(new VariableTextEdit(this, 5, 1, 1)) +{ + QWidget::connect(_logMessage, SIGNAL(textChanged()), this, + SLOT(LogMessageChanged())); + + auto layout = new QHBoxLayout(); + PlaceWidgets(obs_module_text("AdvSceneSwitcher.action.log.entry"), + layout, {{"{{logMessage}}", _logMessage}}, false); + setLayout(layout); + + _entryData = entryData; + UpdateEntryData(); + _loading = false; +} + +void MacroActionLogEdit::UpdateEntryData() +{ + if (!_entryData) { + return; + } + + _logMessage->setPlainText(_entryData->_logMessage); + adjustSize(); + updateGeometry(); +} + +void MacroActionLogEdit::LogMessageChanged() +{ + if (_loading || !_entryData) { + return; + } + + auto lock = LockContext(); + _entryData->_logMessage = _logMessage->toPlainText().toStdString(); + + adjustSize(); + updateGeometry(); +} + +} // namespace advss diff --git a/plugins/base/macro-action-log.hpp b/plugins/base/macro-action-log.hpp new file mode 100644 index 000000000..7e57a0b0a --- /dev/null +++ b/plugins/base/macro-action-log.hpp @@ -0,0 +1,50 @@ +#pragma once +#include "macro-action-edit.hpp" +#include "variable-text-edit.hpp" + +namespace advss { + +class MacroActionLog : public MacroAction { +public: + MacroActionLog(Macro *m) : MacroAction(m) {} + bool PerformAction(); + bool Save(obs_data_t *obj) const; + bool Load(obs_data_t *obj); + std::string GetId() const { return id; }; + static std::shared_ptr Create(Macro *m); + std::shared_ptr Copy() const; + void ResolveVariablesToFixedValues(); + + StringVariable _logMessage = + obs_module_text("AdvSceneSwitcher.action.log.placeholder"); + +private: + static bool _registered; + static const std::string id; +}; + +class MacroActionLogEdit : public QWidget { + Q_OBJECT + +public: + MacroActionLogEdit(QWidget *parent, + std::shared_ptr entryData = nullptr); + void UpdateEntryData(); + static QWidget *Create(QWidget *parent, + std::shared_ptr action) + { + return new MacroActionLogEdit( + parent, + std::dynamic_pointer_cast(action)); + } + +private slots: + void LogMessageChanged(); + +private: + VariableTextEdit *_logMessage; + std::shared_ptr _entryData; + bool _loading = true; +}; + +} // namespace advss