diff --git a/data/locale/en-US.ini b/data/locale/en-US.ini index 60776611e..5dc3cdc17 100644 --- a/data/locale/en-US.ini +++ b/data/locale/en-US.ini @@ -820,6 +820,7 @@ AdvSceneSwitcher.action.variable.type.sceneItemCount="Set to scene item count of AdvSceneSwitcher.action.variable.type.stringLength="Set to length of string" AdvSceneSwitcher.action.variable.type.extractJson="Extract json field with name" AdvSceneSwitcher.action.variable.type.setToTempvar="Set to macro property" +AdvSceneSwitcher.action.variable.type.sceneItemName="Set to scene item name at index" AdvSceneSwitcher.action.variable.askForValuePromptDefault="Assign value to variable \"%1\":" AdvSceneSwitcher.action.variable.askForValuePrompt="Assign value to variable:" AdvSceneSwitcher.action.variable.mathExpression.example="( 1 + 2 * 3 ) / 4" @@ -831,7 +832,7 @@ AdvSceneSwitcher.action.variable.invalidSelection="Invalid selection!" AdvSceneSwitcher.action.variable.actionNoVariableSupport="Getting variable values from %1 actions is not supported!" AdvSceneSwitcher.action.variable.conditionNoVariableSupport="Getting variable values from %1 conditions is not supported!" AdvSceneSwitcher.action.variable.currentSegmentValue="Current value:" -AdvSceneSwitcher.action.variable.entry="{{actions}}{{variables}}{{variables2}}{{strValue}}{{numValue}}{{segmentIndex}}{{mathExpression}}{{envVariableName}}{{scenes}}{{tempVars}}" +AdvSceneSwitcher.action.variable.entry="{{actions}}{{variables}}{{variables2}}{{strValue}}{{numValue}}{{segmentIndex}}{{mathExpression}}{{envVariableName}}{{scenes}}{{tempVars}}{{sceneItemIndex}}" AdvSceneSwitcher.action.variable.entry.substringIndex="Substring start:{{subStringStart}}Substring size:{{subStringSize}}" AdvSceneSwitcher.action.variable.entry.substringRegex="Assign value of{{regexMatchIdx}}match using regular expression:" AdvSceneSwitcher.action.variable.entry.findAndReplace="{{findStr}}{{replaceStr}}" diff --git a/src/macro-core/macro-action-variable.cpp b/src/macro-core/macro-action-variable.cpp index 201cbc1a4..d9756cb23 100644 --- a/src/macro-core/macro-action-variable.cpp +++ b/src/macro-core/macro-action-variable.cpp @@ -51,6 +51,8 @@ const static std::map actionTypes = { "AdvSceneSwitcher.action.variable.type.extractJson"}, {MacroActionVariable::Type::SET_TO_TEMPVAR, "AdvSceneSwitcher.action.variable.type.setToTempvar"}, + {MacroActionVariable::Type::SCENE_ITEM_NAME, + "AdvSceneSwitcher.action.variable.type.sceneItemName"}, }; static void apppend(Variable &var, const std::string &value) @@ -134,6 +136,49 @@ void MacroActionVariable::HandleMathExpression(Variable *var) var->SetValue(std::get(result)); } +struct GetSceneItemNameHelper { + int curIdx = 0; + int targetIdx = 0; + std::string name = ""; +}; + +static bool getSceneItemAtIdx(obs_scene_t *, obs_sceneitem_t *item, void *ptr) +{ + auto data = reinterpret_cast(ptr); + if (obs_sceneitem_is_group(item)) { + obs_scene_t *scene = obs_sceneitem_group_get_scene(item); + obs_scene_enum_items(scene, getSceneItemAtIdx, ptr); + } + if (data->curIdx == data->targetIdx) { + data->name = + obs_source_get_name(obs_sceneitem_get_source(item)); + data->curIdx--; + return false; + } + data->curIdx--; + return true; +} + +void MacroActionVariable::SetToSceneItemName(Variable *var) +{ + auto weakSource = _scene.GetScene(); + if (!weakSource) { + var->SetValue(""); + return; + } + auto index = _sceneItemIndex.GetValue(); + if (index < 1) { + var->SetValue(""); + return; + } + auto sceneItemCount = GetSceneItemCount(weakSource); + OBSSourceAutoRelease source = obs_weak_source_get_source(weakSource); + auto scene = obs_scene_from_source(source); + GetSceneItemNameHelper data{sceneItemCount, index}; + obs_scene_enum_items(scene, getSceneItemAtIdx, &data); + var->SetValue(data.name); +} + struct AskForInputParams { QString prompt; QString placeholder; @@ -268,6 +313,9 @@ bool MacroActionVariable::PerformAction() var->SetValue(*value); return true; } + case Type::SCENE_ITEM_NAME: + SetToSceneItemName(var.get()); + return true; } return true; @@ -299,6 +347,7 @@ bool MacroActionVariable::Save(obs_data_t *obj) const _envVariableName.Save(obj, "environmentVariableName"); _scene.Save(obj); _tempVar.Save(obj); + _sceneItemIndex.Save(obj, "sceneItemIndex"); return true; } @@ -328,6 +377,7 @@ bool MacroActionVariable::Load(obs_data_t *obj) _envVariableName.Load(obj, "environmentVariableName"); _scene.Load(obj); _tempVar.Load(obj, GetMacro()); + _sceneItemIndex.Load(obj, "sceneItemIndex"); return true; } @@ -457,7 +507,8 @@ MacroActionVariableEdit::MacroActionVariableEdit( _envVariable(new VariableLineEdit(this)), _scenes(new SceneSelectionWidget(this, true, false, true, true, true)), - _tempVars(new TempVariableSelection(this)) + _tempVars(new TempVariableSelection(this)), + _sceneItemIndex(new VariableSpinBox()) { _numValue->setMinimum(-9999999999); _numValue->setMaximum(9999999999); @@ -475,6 +526,7 @@ MacroActionVariableEdit::MacroActionVariableEdit( _regexMatchIdx->setSuffix("."); _inputPrompt->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred); + _sceneItemIndex->setMinimum(1); populateTypeSelection(_actions); QWidget::connect(_variables, SIGNAL(SelectionChanged(const QString &)), @@ -523,6 +575,10 @@ MacroActionVariableEdit::MacroActionVariableEdit( QWidget::connect(_tempVars, SIGNAL(SelectionChanged(const TempVariableRef &)), this, SLOT(SelectionChanged(const TempVariableRef &))); + QWidget::connect( + _sceneItemIndex, + SIGNAL(NumberVariableChanged(const NumberVariable &)), + this, SLOT(SceneItemIndexChanged(const NumberVariable &))); std::unordered_map widgetPlaceholders = { {"{{variables}}", _variables}, @@ -544,6 +600,7 @@ MacroActionVariableEdit::MacroActionVariableEdit( {"{{envVariableName}}", _envVariable}, {"{{scenes}}", _scenes}, {"{{tempVars}}", _tempVars}, + {"{{sceneItemIndex}}", _sceneItemIndex}, }; auto entryLayout = new QHBoxLayout; PlaceWidgets(obs_module_text("AdvSceneSwitcher.action.variable.entry"), @@ -638,6 +695,7 @@ void MacroActionVariableEdit::UpdateEntryData() _envVariable->setText(_entryData->_envVariableName); _scenes->SetScene(_entryData->_scene); _tempVars->SetVariable(_entryData->_tempVar); + _sceneItemIndex->SetValue(_entryData->_sceneItemIndex); SetWidgetVisibility(); } @@ -982,6 +1040,17 @@ void MacroActionVariableEdit::SelectionChanged(const TempVariableRef &var) _entryData->_tempVar = var; } +void MacroActionVariableEdit::SceneItemIndexChanged( + const NumberVariable &value) +{ + if (_loading || !_entryData) { + return; + } + + auto lock = LockContext(); + _entryData->_sceneItemIndex = value; +} + void MacroActionVariableEdit::SetWidgetVisibility() { if (!_entryData) { @@ -1058,10 +1127,15 @@ void MacroActionVariableEdit::SetWidgetVisibility() } _envVariable->setVisible(_entryData->_type == MacroActionVariable::Type::ENV_VARIABLE); - _scenes->setVisible(_entryData->_type == - MacroActionVariable::Type::SCENE_ITEM_COUNT); + _scenes->setVisible( + _entryData->_type == + MacroActionVariable::Type::SCENE_ITEM_COUNT || + _entryData->_type == + MacroActionVariable::Type::SCENE_ITEM_NAME); _tempVars->setVisible(_entryData->_type == MacroActionVariable::Type::SET_TO_TEMPVAR); + _sceneItemIndex->setVisible(_entryData->_type == + MacroActionVariable::Type::SCENE_ITEM_NAME); adjustSize(); updateGeometry(); } diff --git a/src/macro-core/macro-action-variable.hpp b/src/macro-core/macro-action-variable.hpp index 6264396ba..35d938259 100644 --- a/src/macro-core/macro-action-variable.hpp +++ b/src/macro-core/macro-action-variable.hpp @@ -6,6 +6,7 @@ #include "scene-selection.hpp" #include "variable-line-edit.hpp" #include "variable-text-edit.hpp" +#include "variable-spinbox.hpp" namespace advss { @@ -44,6 +45,7 @@ class MacroActionVariable : public MacroAction { STRING_LENGTH, EXTRACT_JSON, SET_TO_TEMPVAR, + SCENE_ITEM_NAME, }; Type _type = Type::SET_FIXED_VALUE; @@ -75,6 +77,7 @@ class MacroActionVariable : public MacroAction { #endif SceneSelection _scene; TempVariableRef _tempVar; + IntVariable _sceneItemIndex = 1; private: void DecrementCurrentSegmentVariableRef(); @@ -82,6 +85,7 @@ class MacroActionVariable : public MacroAction { void HandleRegexSubString(Variable *); void HandleFindAndReplace(Variable *); void HandleMathExpression(Variable *); + void SetToSceneItemName(Variable *); std::weak_ptr _macroSegment; int _segmentIdxLoadValue = -1; @@ -129,6 +133,7 @@ private slots: void EnvVariableChanged(); void SceneChanged(const SceneSelection &); void SelectionChanged(const TempVariableRef &var); + void SceneItemIndexChanged(const NumberVariable &); signals: void HeaderInfoChanged(const QString &); @@ -167,6 +172,7 @@ private slots: VariableLineEdit *_envVariable; SceneSelectionWidget *_scenes; TempVariableSelection *_tempVars; + VariableSpinBox *_sceneItemIndex; std::shared_ptr _entryData; QTimer _timer;