From 1bfe56992b33de08d69a11b2f3060d66929dc54b Mon Sep 17 00:00:00 2001 From: WarmUpTill <19472752+WarmUpTill@users.noreply.github.com> Date: Mon, 19 Oct 2026 18:44:30 +0200 Subject: [PATCH] Enable search for variable in macro --- data/locale/en-US.ini | 1 + lib/macro/macro-search.cpp | 51 +++++++++++++++++++++++++++++++- lib/macro/macro-search.hpp | 1 + lib/variables/variable-usage.cpp | 9 ++++++ lib/variables/variable-usage.hpp | 4 +++ 5 files changed, 65 insertions(+), 1 deletion(-) diff --git a/data/locale/en-US.ini b/data/locale/en-US.ini index 438abf7a..76b72857 100644 --- a/data/locale/en-US.ini +++ b/data/locale/en-US.ini @@ -347,6 +347,7 @@ AdvSceneSwitcher.macroTab.search.allSegments="Segment type" AdvSceneSwitcher.macroTab.search.conditions="Condition type" AdvSceneSwitcher.macroTab.search.actions="Action type" AdvSceneSwitcher.macroTab.search.label="Segment label" +AdvSceneSwitcher.macroTab.search.variable="Variable" AdvSceneSwitcher.macroDock.pause="Pause" AdvSceneSwitcher.macroDock.unpause="Unpause" diff --git a/lib/macro/macro-search.cpp b/lib/macro/macro-search.cpp index 1e697320..b6a8857d 100644 --- a/lib/macro/macro-search.cpp +++ b/lib/macro/macro-search.cpp @@ -7,6 +7,8 @@ #include "macro-signals.hpp" #include "plugin-state-helpers.hpp" #include "ui-helpers.hpp" +#include "variable-usage.hpp" +#include "variable.hpp" #include @@ -27,6 +29,9 @@ static RegexConfigWidget *searchRegex = nullptr; static QPushButton *showSettings = nullptr; static std::function refreshFilter; +// Saving a macro is too expensive to do for every size hint request +static std::unordered_map> usedVariablesCache; + static void save(obs_data_t *data) { GetMacroSearchSettings().Save(data, "macroSearchSettings"); @@ -103,7 +108,10 @@ void SetupMacroSearchWidgets(QLayout *searchLayoutArg, QLineEdit *searchText, searchLayout = searchLayoutArg; searchType = searchTypeArg; searchRegex = searchRegexArg; - refreshFilter = refreshArg; + refreshFilter = [refreshArg]() { + usedVariablesCache.clear(); + refreshArg(); + }; showSettings = showSettingsArg; searchClear->setMaximumWidth(22); @@ -150,6 +158,9 @@ void SetupMacroSearchWidgets(QLayout *searchLayoutArg, QLineEdit *searchText, searchType->addItem( obs_module_text("AdvSceneSwitcher.macroTab.search.label"), static_cast(MacroSearchSettings::SearchType::LABEL)); + searchType->addItem( + obs_module_text("AdvSceneSwitcher.macroTab.search.variable"), + static_cast(MacroSearchSettings::SearchType::VARIABLE)); searchType->setCurrentIndex(searchType->findData( static_cast(GetMacroSearchSettings().searchType))); QWidget::connect( @@ -178,6 +189,18 @@ void SetupMacroSearchWidgets(QLayout *searchLayoutArg, QLineEdit *searchText, QWidget::connect(MacroSignalManager::Instance(), &MacroSignalManager::Rename, searchText, []() { refreshFilter(); }); + QWidget::connect(MacroSignalManager::Instance(), + &MacroSignalManager::Add, searchText, + []() { usedVariablesCache.clear(); }); + QWidget::connect(MacroSignalManager::Instance(), + &MacroSignalManager::Remove, searchText, + []() { usedVariablesCache.clear(); }); + QWidget::connect(VariableSignalManager::Instance(), + &VariableSignalManager::Rename, searchText, + []() { refreshFilter(); }); + QWidget::connect(VariableSignalManager::Instance(), + &VariableSignalManager::Remove, searchText, + []() { refreshFilter(); }); CheckMacroSearchVisibility(); } @@ -229,6 +252,30 @@ static bool segmentLabelMatches(MacroSegment *segment) return stringMatches(label); } +static const std::vector &getUsedVariables(Macro *macro) +{ + auto it = usedVariablesCache.find(macro); + if (it != usedVariablesCache.end()) { + return it->second; + } + + std::vector names; + for (const auto &[name, count] : GetVariablesUsedByMacro(macro)) { + names.emplace_back(name); + } + return usedVariablesCache.emplace(macro, std::move(names)).first->second; +} + +static bool usedVariableMatches(Macro *macro) +{ + for (const auto &name : getUsedVariables(macro)) { + if (stringMatches(name)) { + return true; + } + } + return false; +} + bool MacroMatchesSearchFilter(Macro *macro) { assert(macro); @@ -311,6 +358,8 @@ bool MacroMatchesSearchFilter(Macro *macro) } } return false; + case MacroSearchSettings::SearchType::VARIABLE: + return usedVariableMatches(macro); default: break; } diff --git a/lib/macro/macro-search.hpp b/lib/macro/macro-search.hpp index 193d2079..4f263326 100644 --- a/lib/macro/macro-search.hpp +++ b/lib/macro/macro-search.hpp @@ -23,6 +23,7 @@ struct MacroSearchSettings { CONDITIONS = 20, ACTIONS = 30, LABEL = 40, + VARIABLE = 50, }; SearchType searchType = SearchType::NAME; diff --git a/lib/variables/variable-usage.cpp b/lib/variables/variable-usage.cpp index a353630b..468ef999 100644 --- a/lib/variables/variable-usage.cpp +++ b/lib/variables/variable-usage.cpp @@ -32,4 +32,13 @@ std::unordered_map GetVariableUsage() return result; } +std::unordered_map GetVariablesUsedByMacro(Macro *macro) +{ + if (!macro) { + return {}; + } + + return collectReferences(macro); +} + } // namespace advss diff --git a/lib/variables/variable-usage.hpp b/lib/variables/variable-usage.hpp index 4847e231..574168d1 100644 --- a/lib/variables/variable-usage.hpp +++ b/lib/variables/variable-usage.hpp @@ -1,15 +1,19 @@ #pragma once +#include #include #include #include namespace advss { +class Macro; + struct VariableUsage { int referenceCount = 0; std::vector macroNames; }; std::unordered_map GetVariableUsage(); +std::unordered_map GetVariablesUsedByMacro(Macro *macro); } // namespace advss