mirror of
https://github.com/WarmUpTill/SceneSwitcher.git
synced 2026-10-24 09:34:29 -05:00
Enable search for variable in macro
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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 <obs.hpp>
|
||||
|
||||
@@ -27,6 +29,9 @@ static RegexConfigWidget *searchRegex = nullptr;
|
||||
static QPushButton *showSettings = nullptr;
|
||||
static std::function<void()> refreshFilter;
|
||||
|
||||
// Saving a macro is too expensive to do for every size hint request
|
||||
static std::unordered_map<Macro *, std::vector<std::string>> 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<int>(MacroSearchSettings::SearchType::LABEL));
|
||||
searchType->addItem(
|
||||
obs_module_text("AdvSceneSwitcher.macroTab.search.variable"),
|
||||
static_cast<int>(MacroSearchSettings::SearchType::VARIABLE));
|
||||
searchType->setCurrentIndex(searchType->findData(
|
||||
static_cast<int>(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<std::string> &getUsedVariables(Macro *macro)
|
||||
{
|
||||
auto it = usedVariablesCache.find(macro);
|
||||
if (it != usedVariablesCache.end()) {
|
||||
return it->second;
|
||||
}
|
||||
|
||||
std::vector<std::string> 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;
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ struct MacroSearchSettings {
|
||||
CONDITIONS = 20,
|
||||
ACTIONS = 30,
|
||||
LABEL = 40,
|
||||
VARIABLE = 50,
|
||||
};
|
||||
|
||||
SearchType searchType = SearchType::NAME;
|
||||
|
||||
@@ -32,4 +32,13 @@ std::unordered_map<std::string, VariableUsage> GetVariableUsage()
|
||||
return result;
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, int> GetVariablesUsedByMacro(Macro *macro)
|
||||
{
|
||||
if (!macro) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return collectReferences(macro);
|
||||
}
|
||||
|
||||
} // namespace advss
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace advss {
|
||||
|
||||
class Macro;
|
||||
|
||||
struct VariableUsage {
|
||||
int referenceCount = 0;
|
||||
std::vector<std::string> macroNames;
|
||||
};
|
||||
|
||||
std::unordered_map<std::string, VariableUsage> GetVariableUsage();
|
||||
std::unordered_map<std::string, int> GetVariablesUsedByMacro(Macro *macro);
|
||||
|
||||
} // namespace advss
|
||||
|
||||
Reference in New Issue
Block a user