diff --git a/lib/macro/macro-condition-macro.cpp b/lib/macro/macro-condition-macro.cpp index 766822de..2c3ba840 100644 --- a/lib/macro/macro-condition-macro.cpp +++ b/lib/macro/macro-condition-macro.cpp @@ -102,7 +102,7 @@ bool MacroConditionMacro::CheckActionStateCondition() if (!macro) { return false; } - if (!IsValidMacroSegmentIndex(macro.get(), _actionIndex - 1, false)) { + if (!IsValidActionIndex(macro.get(), _actionIndex - 1)) { return false; } if (_type == Type::ACTION_DISABLED) { diff --git a/lib/macro/macro-helpers.cpp b/lib/macro/macro-helpers.cpp index 5596d87e..2d6fa213 100644 --- a/lib/macro/macro-helpers.cpp +++ b/lib/macro/macro-helpers.cpp @@ -21,13 +21,13 @@ static void appendNestedMacros(std::deque> ¯os, for (const auto &action : actions) { const auto nestedMacroAction = dynamic_cast(action.get()); - if (nestedMacroAction) { - macros.push_back( - nestedMacroAction->_nestedMacro); - appendNestedMacros( - macros, - nestedMacroAction->_nestedMacro.get()); + if (!nestedMacroAction) { + continue; } + + macros.push_back(nestedMacroAction->_nestedMacro); + appendNestedMacros( + macros, nestedMacroAction->_nestedMacro.get()); } }; @@ -53,11 +53,13 @@ std::deque> GetAllMacros() for (const auto &topLevelMacro : macros) { appendNestedMacros(macros, topLevelMacro.get()); } + const auto &tempMacros = GetTemporaryMacros(); macros.insert(macros.end(), tempMacros.begin(), tempMacros.end()); - for (const auto &topLevelMacro : tempMacros) { - appendNestedMacros(macros, topLevelMacro.get()); + for (const auto &tempMacro : tempMacros) { + appendNestedMacros(macros, tempMacro.get()); } + return macros; } @@ -70,6 +72,15 @@ GetMacroActions(Macro *macro) return macro->Actions(); } +std::optional>> +GetMacroElseActions(Macro *macro) +{ + if (!macro) { + return {}; + } + return macro->ElseActions(); +} + std::optional>> GetMacroConditions(Macro *macro) { @@ -197,20 +208,42 @@ void ResetMacroRunCount(Macro *macro) macro->ResetRunCount(); } -bool IsValidMacroSegmentIndex(const Macro *m, const int idx, bool isCondition) +bool IsValidActionIndex(const Macro *m, const int idx) { if (!m || idx < 0) { return false; } - if (isCondition) { - if (idx >= (int)m->Conditions().size()) { - return false; - } - } else { - if (idx >= (int)m->Actions().size()) { - return false; - } + + if (idx >= (int)m->Actions().size()) { + return false; } + + return true; +} + +bool IsValidElseActionIndex(const Macro *m, const int idx) +{ + if (!m || idx < 0) { + return false; + } + + if (idx >= (int)m->ElseActions().size()) { + return false; + } + + return true; +} + +bool IsValidConditionIndex(const Macro *m, const int idx) +{ + if (!m || idx < 0) { + return false; + } + + if (idx >= (int)m->Conditions().size()) { + return false; + } + return true; } diff --git a/lib/macro/macro-helpers.hpp b/lib/macro/macro-helpers.hpp index 5d8b62af..b53e0f57 100644 --- a/lib/macro/macro-helpers.hpp +++ b/lib/macro/macro-helpers.hpp @@ -24,6 +24,8 @@ EXPORT std::deque> GetAllMacros(); EXPORT std::optional>> GetMacroActions(Macro *); +EXPORT std::optional>> +GetMacroElseActions(Macro *); EXPORT std::optional>> GetMacroConditions(Macro *); @@ -70,6 +72,8 @@ EXPORT void InvalidateMacroTempVarValues(); EXPORT void ResetMacroConditionTimers(Macro *); EXPORT void ResetMacroRunCount(Macro *); -bool IsValidMacroSegmentIndex(const Macro *m, const int idx, bool isCondition); +bool IsValidActionIndex(const Macro *m, const int idx); +bool IsValidElseActionIndex(const Macro *m, const int idx); +bool IsValidConditionIndex(const Macro *m, const int idx); } // namespace advss diff --git a/lib/macro/macro-segment-selection.cpp b/lib/macro/macro-segment-selection.cpp index d3531a9b..814ca612 100644 --- a/lib/macro/macro-segment-selection.cpp +++ b/lib/macro/macro-segment-selection.cpp @@ -73,35 +73,46 @@ void MacroSegmentSelection::MacroSegmentOrderChanged() } static QString GetMacroSegmentDescription(Macro *macro, int idx, - bool isCondition) + MacroSegmentSelection::Type type) { if (!macro) { return ""; } - if (!IsValidMacroSegmentIndex(macro, idx, isCondition)) { + + MacroSegment *segment; + + switch (type) { + case MacroSegmentSelection::Type::CONDITION: + if (!IsValidConditionIndex(macro, idx)) { + return ""; + } + segment = GetMacroConditions(macro)->at(idx).get(); + break; + case MacroSegmentSelection::Type::ACTION: + if (!IsValidActionIndex(macro, idx)) { + return ""; + } + segment = GetMacroActions(macro)->at(idx).get(); + break; + case MacroSegmentSelection::Type::ELSE_ACTION: + if (!IsValidElseActionIndex(macro, idx)) { + return ""; + } + segment = GetMacroElseActions(macro)->at(idx).get(); + break; + default: return ""; } - MacroSegment *segment; - if (isCondition) { - segment = GetMacroConditions(macro)->at(idx).get(); - } else { - segment = GetMacroActions(macro)->at(idx).get(); - } + const auto idToName = type == MacroSegmentSelection::Type::CONDITION + ? MacroConditionFactory::GetConditionName + : MacroActionFactory::GetActionName; + const QString typeStr = + obs_module_text(idToName(segment->GetId()).c_str()); + const QString description = + QString::fromStdString(segment->GetShortDesc()); - QString description = QString::fromStdString(segment->GetShortDesc()); - QString type; - if (isCondition) { - type = obs_module_text(MacroConditionFactory::GetConditionName( - segment->GetId()) - .c_str()); - } else { - type = obs_module_text( - MacroActionFactory::GetActionName(segment->GetId()) - .c_str()); - } - - QString result = type; + QString result = typeStr; if (!description.isEmpty()) { result += ": " + description; } @@ -123,11 +134,25 @@ void MacroSegmentSelection::SetupDescription() const return; } + bool validIndex = false; + switch (_type) { + case Type::CONDITION: + validIndex = IsValidConditionIndex(_macro, index - 1); + break; + case Type::ACTION: + validIndex = IsValidActionIndex(_macro, index - 1); + break; + case Type::ELSE_ACTION: + validIndex = IsValidElseActionIndex(_macro, index - 1); + break; + default: + break; + } + QString description; - if (IsValidMacroSegmentIndex(_macro, index - 1, - _type == Type::CONDITION)) { - description = GetMacroSegmentDescription( - _macro, index - 1, _type == Type::CONDITION); + if (validIndex) { + description = + GetMacroSegmentDescription(_macro, index - 1, _type); } else { description = obs_module_text( "AdvSceneSwitcher.macroSegmentSelection.invalid"); @@ -165,8 +190,23 @@ void MacroSegmentSelection::MarkSelectedSegment() if (index.GetType() == IntVariable::Type::VARIABLE) { return; } - if (!IsValidMacroSegmentIndex(_macro, index - 1, - _type == Type::CONDITION)) { + + bool validIndex = false; + switch (_type) { + case Type::CONDITION: + validIndex = IsValidConditionIndex(_macro, index - 1); + break; + case Type::ACTION: + validIndex = IsValidActionIndex(_macro, index - 1); + break; + case Type::ELSE_ACTION: + validIndex = IsValidElseActionIndex(_macro, index - 1); + break; + default: + break; + } + + if (!validIndex) { return; } diff --git a/lib/macro/macro-segment-selection.hpp b/lib/macro/macro-segment-selection.hpp index bf8af7fc..4dbf9c0a 100644 --- a/lib/macro/macro-segment-selection.hpp +++ b/lib/macro/macro-segment-selection.hpp @@ -8,7 +8,7 @@ class Macro; class MacroSegmentSelection : public QWidget { Q_OBJECT public: - enum class Type { CONDITION, ACTION }; + enum class Type { CONDITION, ACTION, ELSE_ACTION }; MacroSegmentSelection(QWidget *parent, Type type, bool allowVariables = true);