mirror of
https://github.com/WarmUpTill/SceneSwitcher.git
synced 2026-08-29 20:55:28 -05:00
Add option to set variable value based on condition or action
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "macro-action-variable.hpp"
|
||||
#include "advanced-scene-switcher.hpp"
|
||||
#include "macro-condition-edit.hpp"
|
||||
#include "utility.hpp"
|
||||
|
||||
const std::string MacroActionVariable::id = "variable";
|
||||
@@ -9,8 +10,8 @@ bool MacroActionVariable::_registered = MacroActionFactory::Register(
|
||||
{MacroActionVariable::Create, MacroActionVariableEdit::Create,
|
||||
"AdvSceneSwitcher.action.variable"});
|
||||
|
||||
static std::map<MacroActionVariable::Type, std::string> waitTypes = {
|
||||
{MacroActionVariable::Type::SET,
|
||||
static std::map<MacroActionVariable::Type, std::string> actionTypes = {
|
||||
{MacroActionVariable::Type::SET_FIXED_VALUE,
|
||||
"AdvSceneSwitcher.action.variable.type.set"},
|
||||
{MacroActionVariable::Type::APPEND,
|
||||
"AdvSceneSwitcher.action.variable.type.append"},
|
||||
@@ -20,6 +21,10 @@ static std::map<MacroActionVariable::Type, std::string> waitTypes = {
|
||||
"AdvSceneSwitcher.action.variable.type.increment"},
|
||||
{MacroActionVariable::Type::DECREMENT,
|
||||
"AdvSceneSwitcher.action.variable.type.decrement"},
|
||||
{MacroActionVariable::Type::SET_CONDITION_VALUE,
|
||||
"AdvSceneSwitcher.action.variable.type.setConditionValue"},
|
||||
{MacroActionVariable::Type::SET_ACTION_VALUE,
|
||||
"AdvSceneSwitcher.action.variable.type.setActionValue"},
|
||||
};
|
||||
|
||||
static void apppend(Variable &var, const std::string &value)
|
||||
@@ -41,6 +46,11 @@ static void modifyNumValue(Variable &var, double val, const bool increment)
|
||||
}
|
||||
}
|
||||
|
||||
MacroActionVariable::~MacroActionVariable()
|
||||
{
|
||||
DecrementCurrentSegmentVariableRef();
|
||||
}
|
||||
|
||||
bool MacroActionVariable::PerformAction()
|
||||
{
|
||||
auto var = GetVariableByName(_variableName);
|
||||
@@ -49,13 +59,13 @@ bool MacroActionVariable::PerformAction()
|
||||
}
|
||||
|
||||
switch (_type) {
|
||||
case MacroActionVariable::Type::SET:
|
||||
case Type::SET_FIXED_VALUE:
|
||||
var->SetValue(_strValue);
|
||||
break;
|
||||
case MacroActionVariable::Type::APPEND:
|
||||
case Type::APPEND:
|
||||
apppend(*var, _strValue);
|
||||
break;
|
||||
case MacroActionVariable::Type::APPEND_VAR: {
|
||||
case Type::APPEND_VAR: {
|
||||
auto var2 = GetVariableByName(_variable2Name);
|
||||
if (!var2) {
|
||||
return true;
|
||||
@@ -63,12 +73,28 @@ bool MacroActionVariable::PerformAction()
|
||||
apppend(*var, var2->Value());
|
||||
break;
|
||||
}
|
||||
case MacroActionVariable::Type::INCREMENT:
|
||||
case Type::INCREMENT:
|
||||
modifyNumValue(*var, _numValue, true);
|
||||
break;
|
||||
case MacroActionVariable::Type::DECREMENT:
|
||||
case Type::DECREMENT:
|
||||
modifyNumValue(*var, _numValue, false);
|
||||
break;
|
||||
case Type::SET_CONDITION_VALUE:
|
||||
case Type::SET_ACTION_VALUE: {
|
||||
auto m = GetMacro();
|
||||
if (!m) {
|
||||
return true;
|
||||
}
|
||||
if (GetSegmentIndexValue() == -1) {
|
||||
return true;
|
||||
}
|
||||
auto segment = _macroSegment.lock();
|
||||
if (!segment) {
|
||||
return true;
|
||||
}
|
||||
var->SetValue(segment->GetVariableValue());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -82,6 +108,7 @@ bool MacroActionVariable::Save(obs_data_t *obj) const
|
||||
obs_data_set_string(obj, "strValue", _strValue.c_str());
|
||||
obs_data_set_double(obj, "numValue", _numValue);
|
||||
obs_data_set_int(obj, "condition", static_cast<int>(_type));
|
||||
obs_data_set_int(obj, "segmentIdx", GetSegmentIndexValue());
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -93,6 +120,13 @@ bool MacroActionVariable::Load(obs_data_t *obj)
|
||||
_strValue = obs_data_get_string(obj, "strValue");
|
||||
_numValue = obs_data_get_double(obj, "numValue");
|
||||
_type = static_cast<Type>(obs_data_get_int(obj, "condition"));
|
||||
_segmentIdxLoadValue = obs_data_get_int(obj, "segmentIdx");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MacroActionVariable::PostLoad()
|
||||
{
|
||||
SetSegmentIndexValue(_segmentIdxLoadValue);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -101,9 +135,82 @@ std::string MacroActionVariable::GetShortDesc() const
|
||||
return _variableName;
|
||||
}
|
||||
|
||||
void MacroActionVariable::SetSegmentIndexValue(int value)
|
||||
{
|
||||
DecrementCurrentSegmentVariableRef();
|
||||
|
||||
auto m = GetMacro();
|
||||
if (!m) {
|
||||
_macroSegment.reset();
|
||||
return;
|
||||
}
|
||||
|
||||
if (value < 0) {
|
||||
_macroSegment.reset();
|
||||
return;
|
||||
}
|
||||
|
||||
std::shared_ptr<MacroSegment> segment;
|
||||
if (_type == Type::SET_CONDITION_VALUE) {
|
||||
if (value < m->Conditions().size()) {
|
||||
segment = m->Conditions().at(value);
|
||||
}
|
||||
} else if (_type == Type::SET_ACTION_VALUE) {
|
||||
if (value < m->Actions().size()) {
|
||||
segment = m->Actions().at(value);
|
||||
}
|
||||
}
|
||||
|
||||
_macroSegment = segment;
|
||||
if (segment) {
|
||||
segment->IncrementVariableRef();
|
||||
}
|
||||
}
|
||||
|
||||
int MacroActionVariable::GetSegmentIndexValue() const
|
||||
{
|
||||
auto m = GetMacro();
|
||||
if (!m) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
auto segment = _macroSegment.lock();
|
||||
if (!segment) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (_type == Type::SET_CONDITION_VALUE) {
|
||||
auto it = std::find(m->Conditions().begin(),
|
||||
m->Conditions().end(), segment);
|
||||
if (it != m->Conditions().end()) {
|
||||
return std::distance(m->Conditions().begin(), it);
|
||||
}
|
||||
return -1;
|
||||
} else if (_type == Type::SET_ACTION_VALUE) {
|
||||
auto it = std::find(m->Actions().begin(), m->Actions().end(),
|
||||
segment);
|
||||
if (it != m->Actions().end()) {
|
||||
return std::distance(m->Actions().begin(), it);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
void MacroActionVariable::DecrementCurrentSegmentVariableRef()
|
||||
{
|
||||
auto segment = _macroSegment.lock();
|
||||
if (!segment) {
|
||||
return;
|
||||
}
|
||||
|
||||
segment->DecrementVariableRef();
|
||||
}
|
||||
|
||||
static inline void populateTypeSelection(QComboBox *list)
|
||||
{
|
||||
for (auto entry : waitTypes) {
|
||||
for (auto entry : actionTypes) {
|
||||
list->addItem(obs_module_text(entry.second.c_str()));
|
||||
}
|
||||
}
|
||||
@@ -115,10 +222,17 @@ MacroActionVariableEdit::MacroActionVariableEdit(
|
||||
_variables2(new VariableSelection(this)),
|
||||
_actions(new QComboBox()),
|
||||
_strValue(new ResizingPlainTextEdit(this, 5, 1, 1)),
|
||||
_numValue(new QDoubleSpinBox())
|
||||
_numValue(new QDoubleSpinBox()),
|
||||
_segmentIdx(new QSpinBox()),
|
||||
_segmentValueStatus(new QLabel()),
|
||||
_segmentValue(new ResizingPlainTextEdit(this))
|
||||
{
|
||||
_numValue->setMinimum(-9999999999);
|
||||
_numValue->setMaximum(9999999999);
|
||||
_segmentIdx->setMinimum(0);
|
||||
_segmentIdx->setMaximum(99);
|
||||
_segmentIdx->setSpecialValueText("-");
|
||||
_segmentValue->setReadOnly(true);
|
||||
populateTypeSelection(_actions);
|
||||
|
||||
QWidget::connect(_variables, SIGNAL(SelectionChanged(const QString &)),
|
||||
@@ -131,21 +245,36 @@ MacroActionVariableEdit::MacroActionVariableEdit(
|
||||
SLOT(StrValueChanged()));
|
||||
QWidget::connect(_numValue, SIGNAL(valueChanged(double)), this,
|
||||
SLOT(NumValueChanged(double)));
|
||||
QWidget::connect(_segmentIdx, SIGNAL(valueChanged(int)), this,
|
||||
SLOT(SegmentIndexChanged(int)));
|
||||
QWidget::connect(window(), SIGNAL(MacroSegmentOrderChanged()), this,
|
||||
SLOT(MacroSegmentOrderChanged()));
|
||||
|
||||
std::unordered_map<std::string, QWidget *> widgetPlaceholders = {
|
||||
{"{{variables}}", _variables}, {"{{variables2}}", _variables2},
|
||||
{"{{actions}}", _actions}, {"{{strValue}}", _strValue},
|
||||
{"{{variables}}", _variables},
|
||||
{"{{variables2}}", _variables2},
|
||||
{"{{actions}}", _actions},
|
||||
{"{{strValue}}", _strValue},
|
||||
{"{{numValue}}", _numValue},
|
||||
{"{{segmentIndex}}", _segmentIdx},
|
||||
};
|
||||
|
||||
auto mainLayout = new QHBoxLayout;
|
||||
auto entryLayout = new QHBoxLayout;
|
||||
placeWidgets(obs_module_text("AdvSceneSwitcher.action.variable.entry"),
|
||||
mainLayout, widgetPlaceholders);
|
||||
setLayout(mainLayout);
|
||||
entryLayout, widgetPlaceholders);
|
||||
|
||||
auto layout = new QVBoxLayout;
|
||||
layout->addLayout(entryLayout);
|
||||
layout->addWidget(_segmentValueStatus);
|
||||
layout->addWidget(_segmentValue);
|
||||
setLayout(layout);
|
||||
|
||||
_entryData = entryData;
|
||||
UpdateEntryData();
|
||||
_loading = false;
|
||||
|
||||
connect(&_timer, SIGNAL(timeout()), this,
|
||||
SLOT(UpdateSegmentVariableValue()));
|
||||
_timer.start(1500);
|
||||
}
|
||||
|
||||
void MacroActionVariableEdit::UpdateEntryData()
|
||||
@@ -159,6 +288,7 @@ void MacroActionVariableEdit::UpdateEntryData()
|
||||
_actions->setCurrentIndex(static_cast<int>(_entryData->_type));
|
||||
_strValue->setPlainText(QString::fromStdString(_entryData->_strValue));
|
||||
_numValue->setValue(_entryData->_numValue);
|
||||
_segmentIdx->setValue(_entryData->GetSegmentIndexValue() + 1);
|
||||
SetWidgetVisibility();
|
||||
}
|
||||
|
||||
@@ -191,6 +321,12 @@ void MacroActionVariableEdit::ActionChanged(int value)
|
||||
std::lock_guard<std::mutex> lock(switcher->m);
|
||||
_entryData->_type = static_cast<MacroActionVariable::Type>(value);
|
||||
SetWidgetVisibility();
|
||||
|
||||
if (_entryData->_type == MacroActionVariable::Type::SET_ACTION_VALUE ||
|
||||
_entryData->_type ==
|
||||
MacroActionVariable::Type::SET_CONDITION_VALUE) {
|
||||
MarkSelectedSegment();
|
||||
}
|
||||
}
|
||||
|
||||
void MacroActionVariableEdit::StrValueChanged()
|
||||
@@ -214,40 +350,175 @@ void MacroActionVariableEdit::NumValueChanged(double val)
|
||||
_entryData->_numValue = val;
|
||||
}
|
||||
|
||||
void MacroActionVariableEdit::SegmentIndexChanged(int val)
|
||||
{
|
||||
if (_loading || !_entryData) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(switcher->m);
|
||||
_entryData->SetSegmentIndexValue(val - 1);
|
||||
MarkSelectedSegment();
|
||||
}
|
||||
|
||||
void MacroActionVariableEdit::SetSegmentValueError(const QString &text)
|
||||
{
|
||||
_segmentValueStatus->setText(text);
|
||||
_segmentValue->setPlainText("");
|
||||
_segmentValue->hide();
|
||||
|
||||
adjustSize();
|
||||
updateGeometry();
|
||||
}
|
||||
|
||||
void MacroActionVariableEdit::UpdateSegmentVariableValue()
|
||||
{
|
||||
if (!_entryData ||
|
||||
!(_entryData->_type ==
|
||||
MacroActionVariable::Type::SET_CONDITION_VALUE ||
|
||||
_entryData->_type ==
|
||||
MacroActionVariable::Type::SET_ACTION_VALUE)) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto m = _entryData->GetMacro();
|
||||
if (!m) {
|
||||
return;
|
||||
}
|
||||
|
||||
int index = _entryData->GetSegmentIndexValue();
|
||||
if (index < 0) {
|
||||
SetSegmentValueError(obs_module_text(
|
||||
"AdvSceneSwitcher.action.variable.invalidSelection"));
|
||||
const QSignalBlocker b(_segmentIdx);
|
||||
_segmentIdx->setValue(index);
|
||||
return;
|
||||
}
|
||||
|
||||
std::shared_ptr<MacroSegment> segment;
|
||||
if (_entryData->_type == MacroActionVariable::Type::SET_ACTION_VALUE) {
|
||||
const auto &actions = m->Actions();
|
||||
if (index < actions.size()) {
|
||||
segment = actions.at(index);
|
||||
}
|
||||
} else if (_entryData->_type ==
|
||||
MacroActionVariable::Type::SET_CONDITION_VALUE) {
|
||||
const auto &conditions = m->Conditions();
|
||||
if (index < conditions.size()) {
|
||||
segment = conditions.at(index);
|
||||
}
|
||||
}
|
||||
|
||||
if (!segment) {
|
||||
SetSegmentValueError(obs_module_text(
|
||||
"AdvSceneSwitcher.action.variable.invalidSelection"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!segment->SupportsVariableValue()) {
|
||||
std::string type;
|
||||
QString fmt;
|
||||
|
||||
if (_entryData->_type ==
|
||||
MacroActionVariable::Type::SET_ACTION_VALUE) {
|
||||
type = MacroActionFactory::GetActionName(
|
||||
segment->GetId());
|
||||
fmt = QString(obs_module_text(
|
||||
"AdvSceneSwitcher.action.variable.actionNoVariableSupport"));
|
||||
} else if (_entryData->_type ==
|
||||
MacroActionVariable::Type::SET_CONDITION_VALUE) {
|
||||
type = MacroConditionFactory::GetConditionName(
|
||||
segment->GetId());
|
||||
fmt = QString(obs_module_text(
|
||||
"AdvSceneSwitcher.action.variable.conditionNoVariableSupport"));
|
||||
}
|
||||
SetSegmentValueError(
|
||||
fmt.arg(QString(obs_module_text(type.c_str()))));
|
||||
return;
|
||||
}
|
||||
|
||||
_segmentValueStatus->setText(obs_module_text(
|
||||
"AdvSceneSwitcher.action.variable.currentSegmentValue"));
|
||||
_segmentValue->show();
|
||||
_segmentValue->setPlainText(
|
||||
QString::fromStdString(segment->GetVariableValue()));
|
||||
|
||||
adjustSize();
|
||||
updateGeometry();
|
||||
}
|
||||
|
||||
void MacroActionVariableEdit::MacroSegmentOrderChanged()
|
||||
{
|
||||
const QSignalBlocker b(_segmentIdx);
|
||||
_segmentIdx->setValue(_entryData->GetSegmentIndexValue() + 1);
|
||||
}
|
||||
|
||||
void MacroActionVariableEdit::MarkSelectedSegment()
|
||||
{
|
||||
if (switcher->disableHints) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto m = _entryData->GetMacro();
|
||||
if (!m) {
|
||||
return;
|
||||
}
|
||||
|
||||
int index = _entryData->GetSegmentIndexValue();
|
||||
if (index < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_entryData->_type == MacroActionVariable::Type::SET_ACTION_VALUE) {
|
||||
const auto &actions = m->Actions();
|
||||
if (index >= actions.size()) {
|
||||
return;
|
||||
}
|
||||
AdvSceneSwitcher::window->HighlightAction(
|
||||
index, QColor(Qt::lightGray));
|
||||
} else {
|
||||
const auto &conditions = m->Conditions();
|
||||
if (index >= conditions.size()) {
|
||||
return;
|
||||
}
|
||||
AdvSceneSwitcher::window->HighlightCondition(
|
||||
index, QColor(Qt::lightGray));
|
||||
}
|
||||
|
||||
PulseWidget(_segmentIdx, QColor(Qt::lightGray), QColor(0, 0, 0, 0),
|
||||
true);
|
||||
}
|
||||
|
||||
void MacroActionVariableEdit::SetWidgetVisibility()
|
||||
{
|
||||
if (!_entryData) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto type = _entryData->_type;
|
||||
switch (type) {
|
||||
case MacroActionVariable::Type::SET:
|
||||
_variables2->hide();
|
||||
_strValue->show();
|
||||
_numValue->hide();
|
||||
break;
|
||||
case MacroActionVariable::Type::APPEND:
|
||||
_variables2->hide();
|
||||
_strValue->show();
|
||||
_numValue->hide();
|
||||
break;
|
||||
case MacroActionVariable::Type::APPEND_VAR:
|
||||
_variables2->show();
|
||||
_strValue->hide();
|
||||
_numValue->hide();
|
||||
break;
|
||||
case MacroActionVariable::Type::INCREMENT:
|
||||
_variables2->hide();
|
||||
_strValue->hide();
|
||||
_numValue->show();
|
||||
break;
|
||||
case MacroActionVariable::Type::DECREMENT:
|
||||
_variables2->hide();
|
||||
_strValue->hide();
|
||||
_numValue->show();
|
||||
break;
|
||||
}
|
||||
_variables2->setVisible(_entryData->_type ==
|
||||
MacroActionVariable::Type::APPEND_VAR);
|
||||
_strValue->setVisible(
|
||||
_entryData->_type ==
|
||||
MacroActionVariable::Type::SET_FIXED_VALUE ||
|
||||
_entryData->_type == MacroActionVariable::Type::APPEND);
|
||||
_numValue->setVisible(
|
||||
_entryData->_type == MacroActionVariable::Type::INCREMENT ||
|
||||
_entryData->_type == MacroActionVariable::Type::DECREMENT);
|
||||
_segmentValueStatus->setVisible(
|
||||
_entryData->_type ==
|
||||
MacroActionVariable::Type::SET_ACTION_VALUE ||
|
||||
_entryData->_type ==
|
||||
MacroActionVariable::Type::SET_CONDITION_VALUE);
|
||||
_segmentValue->setVisible(
|
||||
_entryData->_type ==
|
||||
MacroActionVariable::Type::SET_ACTION_VALUE ||
|
||||
_entryData->_type ==
|
||||
MacroActionVariable::Type::SET_CONDITION_VALUE);
|
||||
_segmentIdx->setVisible(
|
||||
_entryData->_type ==
|
||||
MacroActionVariable::Type::SET_ACTION_VALUE ||
|
||||
_entryData->_type ==
|
||||
MacroActionVariable::Type::SET_CONDITION_VALUE);
|
||||
|
||||
adjustSize();
|
||||
updateGeometry();
|
||||
|
||||
Reference in New Issue
Block a user