Skip to content

Commit

Permalink
Add action to assign scene item count to variable
Browse files Browse the repository at this point in the history
To be able to iterate through the scene items of a scene
  • Loading branch information
WarmUpTill committed Sep 13, 2023
1 parent ea63a9f commit 3b3eac3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
3 changes: 2 additions & 1 deletion data/locale/en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ AdvSceneSwitcher.action.variable.type.findAndReplace="Find and replace in curren
AdvSceneSwitcher.action.variable.type.mathExpression="Mathematical expression"
AdvSceneSwitcher.action.variable.type.askForValue="Get user input"
AdvSceneSwitcher.action.variable.type.environmentVariable="Set to environment variable value"
AdvSceneSwitcher.action.variable.type.sceneItemCount="Set to scene item count of scene"
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"
Expand All @@ -750,7 +751,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}}"
AdvSceneSwitcher.action.variable.entry="{{actions}}{{variables}}{{variables2}}{{strValue}}{{numValue}}{{segmentIndex}}{{mathExpression}}{{envVariableName}}{{scenes}}"
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}}"
Expand Down
27 changes: 26 additions & 1 deletion src/macro-core/macro-action-variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const static std::map<MacroActionVariable::Type, std::string> actionTypes = {
"AdvSceneSwitcher.action.variable.type.askForValue"},
{MacroActionVariable::Type::ENV_VARIABLE,
"AdvSceneSwitcher.action.variable.type.environmentVariable"},
{MacroActionVariable::Type::SCENE_ITEM_COUNT,
"AdvSceneSwitcher.action.variable.type.sceneItemCount"},
};

static void apppend(Variable &var, const std::string &value)
Expand Down Expand Up @@ -232,6 +234,10 @@ bool MacroActionVariable::PerformAction()
var->SetValue(std::getenv(_envVariableName.c_str()));
return true;
}
case Type::SCENE_ITEM_COUNT: {
var->SetValue(GetSceneItemCount(_scene.GetScene(false)));
return true;
}
}

return true;
Expand Down Expand Up @@ -261,6 +267,7 @@ bool MacroActionVariable::Save(obs_data_t *obj) const
obs_data_set_bool(obj, "useInputPlaceholder", _useInputPlaceholder);
_inputPlaceholder.Save(obj, "inputPlaceholder");
_envVariableName.Save(obj, "environmentVariableName");
_scene.Save(obj);
return true;
}

Expand Down Expand Up @@ -288,6 +295,7 @@ bool MacroActionVariable::Load(obs_data_t *obj)
_useInputPlaceholder = obs_data_get_bool(obj, "useInputPlaceholder");
_inputPlaceholder.Load(obj, "inputPlaceholder");
_envVariableName.Load(obj, "environmentVariableName");
_scene.Load(obj);
return true;
}

Expand Down Expand Up @@ -413,7 +421,8 @@ MacroActionVariableEdit::MacroActionVariableEdit(
_placeholderLayout(new QHBoxLayout()),
_useInputPlaceholder(new QCheckBox()),
_inputPlaceholder(new VariableLineEdit(this)),
_envVariable(new VariableLineEdit(this))
_envVariable(new VariableLineEdit(this)),
_scenes(new SceneSelectionWidget(this, true, false, true, true, true))
{
_numValue->setMinimum(-9999999999);
_numValue->setMaximum(9999999999);
Expand Down Expand Up @@ -474,6 +483,8 @@ MacroActionVariableEdit::MacroActionVariableEdit(
SLOT(InputPlaceholderChanged()));
QWidget::connect(_envVariable, SIGNAL(editingFinished()), this,
SLOT(EnvVariableChanged()));
QWidget::connect(_scenes, SIGNAL(SceneChanged(const SceneSelection &)),
this, SLOT(SceneChanged(const SceneSelection &)));

std::unordered_map<std::string, QWidget *> widgetPlaceholders = {
{"{{variables}}", _variables},
Expand All @@ -493,6 +504,7 @@ MacroActionVariableEdit::MacroActionVariableEdit(
{"{{useInputPlaceholder}}", _useInputPlaceholder},
{"{{inputPlaceholder}}", _inputPlaceholder},
{"{{envVariableName}}", _envVariable},
{"{{scenes}}", _scenes},
};
auto entryLayout = new QHBoxLayout;
PlaceWidgets(obs_module_text("AdvSceneSwitcher.action.variable.entry"),
Expand Down Expand Up @@ -585,6 +597,7 @@ void MacroActionVariableEdit::UpdateEntryData()
_useInputPlaceholder->setChecked(_entryData->_useInputPlaceholder);
_inputPlaceholder->setText(_entryData->_inputPlaceholder);
_envVariable->setText(_entryData->_envVariableName);
_scenes->SetScene(_entryData->_scene);
SetWidgetVisibility();
}

Expand Down Expand Up @@ -902,6 +915,16 @@ void MacroActionVariableEdit::EnvVariableChanged()
_entryData->_envVariableName = _envVariable->text().toStdString();
}

void MacroActionVariableEdit::SceneChanged(const SceneSelection &scene)
{
if (_loading || !_entryData) {
return;
}

auto lock = LockContext();
_entryData->_scene = scene;
}

void MacroActionVariableEdit::SetWidgetVisibility()
{
if (!_entryData) {
Expand Down Expand Up @@ -976,6 +999,8 @@ void MacroActionVariableEdit::SetWidgetVisibility()
}
_envVariable->setVisible(_entryData->_type ==
MacroActionVariable::Type::ENV_VARIABLE);
_scenes->setVisible(_entryData->_type ==
MacroActionVariable::Type::SCENE_ITEM_COUNT);
adjustSize();
updateGeometry();
}
Expand Down
5 changes: 5 additions & 0 deletions src/macro-core/macro-action-variable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "macro-segment-selection.hpp"
#include "regex-config.hpp"
#include "resizing-text-edit.hpp"
#include "scene-selection.hpp"
#include "variable-line-edit.hpp"

namespace advss {
Expand Down Expand Up @@ -38,6 +39,7 @@ class MacroActionVariable : public MacroAction {
MATH_EXPRESSION,
USER_INPUT,
ENV_VARIABLE,
SCENE_ITEM_COUNT,
};

Type _type = Type::SET_FIXED_VALUE;
Expand Down Expand Up @@ -67,6 +69,7 @@ class MacroActionVariable : public MacroAction {
#else
StringVariable _envVariableName = "HOME";
#endif
SceneSelection _scene;

private:
void DecrementCurrentSegmentVariableRef();
Expand Down Expand Up @@ -119,6 +122,7 @@ private slots:
void UseInputPlaceholderChanged(int);
void InputPlaceholderChanged();
void EnvVariableChanged();
void SceneChanged(const SceneSelection &);

signals:
void HeaderInfoChanged(const QString &);
Expand Down Expand Up @@ -155,6 +159,7 @@ private slots:
QCheckBox *_useInputPlaceholder;
VariableLineEdit *_inputPlaceholder;
VariableLineEdit *_envVariable;
SceneSelectionWidget *_scenes;

std::shared_ptr<MacroActionVariable> _entryData;
QTimer _timer;
Expand Down

0 comments on commit 3b3eac3

Please sign in to comment.