From c1e23f9c7aaaa4ddb2e74de0bdb979d868ab0268 Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Fri, 14 Jun 2024 23:51:01 +0200 Subject: [PATCH] Improve slide show condition * Add temp var support * Add regex support for path check * Improve layout stretch handling --- data/locale/en-US.ini | 9 +- plugins/base/macro-condition-slideshow.cpp | 102 ++++++++++++++------- plugins/base/macro-condition-slideshow.hpp | 7 ++ 3 files changed, 86 insertions(+), 32 deletions(-) diff --git a/data/locale/en-US.ini b/data/locale/en-US.ini index c6ce97a2..3c7ab785 100644 --- a/data/locale/en-US.ini +++ b/data/locale/en-US.ini @@ -620,7 +620,7 @@ AdvSceneSwitcher.condition.slideshow.condition.slideChanged="Slide changed" AdvSceneSwitcher.condition.slideshow.condition.slideIndex="Current slide number is" AdvSceneSwitcher.condition.slideshow.condition.slidePath="Current slide path is" AdvSceneSwitcher.condition.slideshow.updateIntervalTooltip="Information about the slide show status will only be updated based on the configured time between slides" -AdvSceneSwitcher.condition.slideshow.entry="{{sources}}{{conditions}}{{index}}{{path}}" +AdvSceneSwitcher.condition.slideshow.entry="{{sources}}{{conditions}}{{index}}{{path}}{{regex}}" AdvSceneSwitcher.condition.twitch="Twitch" AdvSceneSwitcher.condition.twitch.type.event.channel.stream.online="Stream went online" AdvSceneSwitcher.condition.twitch.type.event.channel.stream.offline="Stream went offline" @@ -1764,6 +1764,13 @@ AdvSceneSwitcher.tempVar.source.settings="Settings string" AdvSceneSwitcher.tempVar.filter.setting="Setting value" AdvSceneSwitcher.tempVar.filter.settings="Settings string" +AdvSceneSwitcher.tempVar.slideShow.index="Slide index" +AdvSceneSwitcher.tempVar.slideShow.index.description="The index of the slide currently being displayed.\nIf the index cannot be determined, -1 is returned." +AdvSceneSwitcher.tempVar.slideShow.path="Slide path" +AdvSceneSwitcher.tempVar.slideShow.path.description="The filesystem path of the slide currently being displayed." +AdvSceneSwitcher.tempVar.slideShow.fileName="Slide file name" +AdvSceneSwitcher.tempVar.slideShow.fileName.description="The name of file of the slide currently being displayed." + AdvSceneSwitcher.selectScene="--select scene--" AdvSceneSwitcher.selectPreviousScene="Previous Scene" AdvSceneSwitcher.selectCurrentScene="Current Scene" diff --git a/plugins/base/macro-condition-slideshow.cpp b/plugins/base/macro-condition-slideshow.cpp index 81274a24..0745b657 100644 --- a/plugins/base/macro-condition-slideshow.cpp +++ b/plugins/base/macro-condition-slideshow.cpp @@ -2,6 +2,8 @@ #include "macro-helpers.hpp" #include "layout-helpers.hpp" +#include + namespace advss { const std::string MacroConditionSlideshow::id = "slideshow"; @@ -50,25 +52,28 @@ bool MacroConditionSlideshow::CheckCondition() switch (_condition) { case MacroConditionSlideshow::Condition::SLIDE_CHANGED: if (!_slideChanged) { - SetVariableValue("false"); + SetVariableValues("false"); return false; } _slideChanged = false; - SetVariableValue("true"); + SetVariableValues("true"); return true; case MacroConditionSlideshow::Condition::SLIDE_INDEX: if (_currentIndex == -1) { - SetVariableValue("-1"); + SetVariableValues("-1"); return false; } - SetVariableValue(std::to_string(_currentIndex + 1)); + SetVariableValues(std::to_string(_currentIndex + 1)); return _currentIndex == _index - 1; case MacroConditionSlideshow::Condition::SLIDE_PATH: if (_currentPath[0] == '\0') { - SetVariableValue(""); + SetVariableValues(""); return false; } - SetVariableValue(_currentPath); + SetVariableValues(_currentPath); + if (_regex.Enabled()) { + return _regex.Matches(_currentPath, _path); + } return std::string(_path) == std::string(_currentPath); } @@ -82,6 +87,7 @@ bool MacroConditionSlideshow::Save(obs_data_t *obj) const _source.Save(obj); _index.Save(obj, "index"); _path.Save(obj, "path"); + _regex.Save(obj); return true; } @@ -92,6 +98,7 @@ bool MacroConditionSlideshow::Load(obs_data_t *obj) _source.Load(obj); _index.Load(obj, "index"); _path.Load(obj, "path"); + _regex.Load(obj); auto s = _source.GetSource(); AddSignalHandler(s); @@ -175,6 +182,35 @@ void MacroConditionSlideshow::Reset() _currentPath = ""; } +void MacroConditionSlideshow::SetupTempVars() +{ + MacroCondition::SetupTempVars(); + AddTempvar( + "index", + obs_module_text("AdvSceneSwitcher.tempVar.slideShow.index"), + obs_module_text( + "AdvSceneSwitcher.tempVar.slideShow.index.description")); + AddTempvar( + "path", + obs_module_text("AdvSceneSwitcher.tempVar.slideShow.path"), + obs_module_text( + "AdvSceneSwitcher.tempVar.slideShow.path.description")); + AddTempvar( + "fileName", + obs_module_text("AdvSceneSwitcher.tempVar.slideShow.fileName"), + obs_module_text( + "AdvSceneSwitcher.tempVar.slideShow.fileName.description")); +} + +void MacroConditionSlideshow::SetVariableValues(const std::string &value) +{ + SetVariableValue(value); + SetTempVarValue("index", std::to_string(_currentIndex + 1)); + SetTempVarValue("path", _currentPath ? _currentPath : ""); + QFileInfo fileInfo(_currentPath ? _currentPath : ""); + SetTempVarValue("fileName", fileInfo.fileName().toStdString()); +} + static void populateConditionSelection(QComboBox *list) { for (const auto &[_, name] : conditions) { @@ -208,7 +244,9 @@ MacroConditionSlideshowEdit::MacroConditionSlideshowEdit( _conditions(new QComboBox(this)), _index(new VariableSpinBox(this)), _path(new VariableLineEdit(this)), - _sources(new SourceSelectionWidget(this, QStringList(), true)) + _sources(new SourceSelectionWidget(this, QStringList(), true)), + _regex(new RegexConfigWidget(this)), + _layout(new QHBoxLayout()) { setToolTip(obs_module_text( "AdvSceneSwitcher.condition.slideshow.updateIntervalTooltip")); @@ -230,16 +268,19 @@ MacroConditionSlideshowEdit::MacroConditionSlideshowEdit( this, SLOT(IndexChanged(const NumberVariable &))); QWidget::connect(_path, SIGNAL(editingFinished()), this, SLOT(PathChanged())); + QWidget::connect(_regex, + SIGNAL(RegexConfigChanged(const RegexConfig &)), this, + SLOT(RegexChanged(const RegexConfig &))); - auto layout = new QHBoxLayout; PlaceWidgets( obs_module_text("AdvSceneSwitcher.condition.slideshow.entry"), - layout, + _layout, {{"{{conditions}}", _conditions}, {"{{sources}}", _sources}, {"{{index}}", _index}, - {"{{path}}", _path}}); - setLayout(layout); + {"{{path}}", _path}, + {"{{regex}}", _regex}}); + setLayout(_layout); _entryData = entryData; UpdateEntryData(); @@ -248,11 +289,7 @@ MacroConditionSlideshowEdit::MacroConditionSlideshowEdit( void MacroConditionSlideshowEdit::ConditionChanged(int index) { - if (_loading || !_entryData) { - return; - } - - auto lock = LockContext(); + GUARD_LOADING_AND_LOCK(); _entryData->_condition = static_cast(index); SetWidgetVisibility(); @@ -260,11 +297,7 @@ void MacroConditionSlideshowEdit::ConditionChanged(int index) void MacroConditionSlideshowEdit::SourceChanged(const SourceSelection &source) { - if (_loading || !_entryData) { - return; - } - - auto lock = LockContext(); + GUARD_LOADING_AND_LOCK(); _entryData->SetSource(source); emit HeaderInfoChanged( QString::fromStdString(_entryData->GetShortDesc())); @@ -272,24 +305,22 @@ void MacroConditionSlideshowEdit::SourceChanged(const SourceSelection &source) void MacroConditionSlideshowEdit::IndexChanged(const NumberVariable &value) { - if (_loading || !_entryData) { - return; - } - - auto lock = LockContext(); + GUARD_LOADING_AND_LOCK(); _entryData->_index = value; } void MacroConditionSlideshowEdit::PathChanged() { - if (_loading || !_entryData) { - return; - } - - auto lock = LockContext(); + GUARD_LOADING_AND_LOCK(); _entryData->_path = _path->text().toStdString(); } +void MacroConditionSlideshowEdit::RegexChanged(const RegexConfig ®ex) +{ + GUARD_LOADING_AND_LOCK(); + _entryData->_regex = regex; +} + void MacroConditionSlideshowEdit::UpdateEntryData() { if (!_entryData) { @@ -300,6 +331,7 @@ void MacroConditionSlideshowEdit::UpdateEntryData() _sources->SetSource(_entryData->GetSource()); _index->SetValue(_entryData->_index); _path->setText(_entryData->_path); + _regex->SetRegexConfig(_entryData->_regex); SetWidgetVisibility(); } @@ -313,6 +345,14 @@ void MacroConditionSlideshowEdit::SetWidgetVisibility() MacroConditionSlideshow::Condition::SLIDE_INDEX); _path->setVisible(_entryData->_condition == MacroConditionSlideshow::Condition::SLIDE_PATH); + _regex->setVisible(_entryData->_condition == + MacroConditionSlideshow::Condition::SLIDE_PATH); + if (_entryData->_condition == + MacroConditionSlideshow::Condition::SLIDE_PATH) { + RemoveStretchIfPresent(_layout); + } else { + AddStretchIfNecessary(_layout); + } } } // namespace advss diff --git a/plugins/base/macro-condition-slideshow.hpp b/plugins/base/macro-condition-slideshow.hpp index e1bca245..fa97c287 100644 --- a/plugins/base/macro-condition-slideshow.hpp +++ b/plugins/base/macro-condition-slideshow.hpp @@ -1,6 +1,7 @@ #pragma once #include "macro-condition-edit.hpp" #include "source-selection.hpp" +#include "regex-config.hpp" #include "variable-spinbox.hpp" #include "variable-line-edit.hpp" @@ -31,12 +32,15 @@ public: Condition _condition = Condition::SLIDE_CHANGED; IntVariable _index; StringVariable _path; + RegexConfig _regex; private: static void SlideChanged(void *condition, calldata_t *); void RemoveSignalHandler(); void AddSignalHandler(const OBSWeakSource &); void Reset(); + void SetupTempVars(); + void SetVariableValues(const std::string &value); SourceSelection _source; OBSWeakSource _currentSignalSource; @@ -70,6 +74,7 @@ private slots: void SourceChanged(const SourceSelection &); void IndexChanged(const NumberVariable &); void PathChanged(); + void RegexChanged(const RegexConfig &); signals: void HeaderInfoChanged(const QString &); @@ -80,6 +85,8 @@ private: VariableSpinBox *_index; VariableLineEdit *_path; SourceSelectionWidget *_sources; + RegexConfigWidget *_regex; + QHBoxLayout *_layout; std::shared_ptr _entryData; bool _loading = true;