Move filter / source functions to utility

This commit is contained in:
WarmUpTill
2021-06-13 21:42:18 +02:00
committed by WarmUpTill
parent a7e03245bd
commit 75f8d0cb07
4 changed files with 90 additions and 34 deletions

View File

@@ -22,6 +22,9 @@ OBSWeakSource GetWeakFilterByName(OBSWeakSource source, const char *name);
OBSWeakSource GetWeakFilterByQString(OBSWeakSource source, const QString &name);
bool compareIgnoringLineEnding(QString &s1, QString &s2);
std::string getSourceSettings(OBSWeakSource ws);
void setSourceSettings(obs_source_t *s, const std::string &settings);
bool compareSourceSettings(const OBSWeakSource &source,
const std::string &settings, bool regex);
std::string getDataFilePath(const std::string &file);
/**
@@ -61,3 +64,6 @@ void populateSceneSelection(QComboBox *sel, bool addPrevious = false,
std::deque<SceneGroup> *sceneGroups = nullptr,
bool addSelect = true, std::string selectText = "",
bool selectable = false);
void populateSourcesWithFilterSelection(QComboBox *list);
void populateFilterSelection(QComboBox *list,
OBSWeakSource weakSource = nullptr);

View File

@@ -16,22 +16,6 @@ const static std::map<SourceAction, std::string> actionTypes = {
"AdvSceneSwitcher.action.source.type.settings"},
};
void setSourceSettings(obs_source_t *s, const std::string &settings)
{
if (settings.empty()) {
return;
}
obs_data_t *data = obs_data_create_from_json(settings.c_str());
if (!data) {
blog(LOG_WARNING, "invalid source settings provided: \n%s",
settings.c_str());
return;
}
obs_source_update(s, data);
obs_data_release(data);
}
bool MacroActionSource::PerformAction()
{
auto s = obs_weak_source_get_source(_source);

View File

@@ -21,23 +21,6 @@ static std::map<SourceCondition, std::string> sourceConditionTypes = {
"AdvSceneSwitcher.condition.source.type.settings"},
};
bool checkSettings(const OBSWeakSource &source, const std::string &settings,
bool useRegex)
{
bool ret = false;
std::string currentSettings = getSourceSettings(source);
if (useRegex) {
try {
std::regex expr(settings);
ret = std::regex_match(currentSettings, expr);
} catch (const std::regex_error &) {
}
} else {
ret = currentSettings == settings;
}
return ret;
}
bool MacroConditionSource::CheckCondition()
{
if (!_source) {
@@ -55,7 +38,7 @@ bool MacroConditionSource::CheckCondition()
ret = obs_source_showing(s);
break;
case SourceCondition::SETTINGS:
ret = checkSettings(_source, _settings, _regex);
ret = compareSourceSettings(_source, _settings, _regex);
break;
default:
break;

View File

@@ -11,6 +11,7 @@
#include <QTimer>
#include <QMessageBox>
#include <unordered_map>
#include <regex>
#include <obs-module.h>
#include <util/util.hpp>
@@ -217,6 +218,39 @@ std::string getSourceSettings(OBSWeakSource ws)
return settings;
}
void setSourceSettings(obs_source_t *s, const std::string &settings)
{
if (settings.empty()) {
return;
}
obs_data_t *data = obs_data_create_from_json(settings.c_str());
if (!data) {
blog(LOG_WARNING, "invalid source settings provided: \n%s",
settings.c_str());
return;
}
obs_source_update(s, data);
obs_data_release(data);
}
bool compareSourceSettings(const OBSWeakSource &source,
const std::string &settings, bool useRegex)
{
bool ret = false;
std::string currentSettings = getSourceSettings(source);
if (useRegex) {
try {
std::regex expr(settings);
ret = std::regex_match(currentSettings, expr);
} catch (const std::regex_error &) {
}
} else {
ret = currentSettings == settings;
}
return ret;
}
std::string getDataFilePath(const std::string &file)
{
std::string root_path = obs_get_module_data_path(obs_current_module());
@@ -501,6 +535,55 @@ void populateSceneSelection(QComboBox *sel, bool addPrevious,
sel->setCurrentIndex(0);
}
static inline void hasFilterEnum(obs_source_t *, obs_source_t *filter,
void *ptr)
{
if (!filter) {
return;
}
bool *hasFilter = reinterpret_cast<bool *>(ptr);
*hasFilter = true;
}
void populateSourcesWithFilterSelection(QComboBox *list)
{
auto enumSourcesWithFilters = [](void *param, obs_source_t *source) {
if (!source) {
return true;
}
QComboBox *list = reinterpret_cast<QComboBox *>(param);
bool hasFilter = false;
obs_source_enum_filters(source, hasFilterEnum, &hasFilter);
if (hasFilter) {
list->addItem(obs_source_get_name(source));
}
return true;
};
obs_enum_sources(enumSourcesWithFilters, list);
obs_enum_scenes(enumSourcesWithFilters, list);
list->model()->sort(0);
addSelectionEntry(list,
obs_module_text("AdvSceneSwitcher.selectSource"));
list->setCurrentIndex(0);
}
void populateFilterSelection(QComboBox *list, OBSWeakSource weakSource)
{
auto enumFilters = [](obs_source_t *, obs_source_t *filter, void *ptr) {
QComboBox *list = reinterpret_cast<QComboBox *>(ptr);
auto name = obs_source_get_name(filter);
list->addItem(name);
};
auto s = obs_weak_source_get_source(weakSource);
obs_source_enum_filters(s, enumFilters, list);
list->model()->sort(0);
addSelectionEntry(list,
obs_module_text("AdvSceneSwitcher.selectFilter"));
obs_source_release(s);
list->setCurrentIndex(0);
}
QMetaObject::Connection PulseWidget(QWidget *widget, QColor endColor,
QColor startColor, QString specifier)
{