mirror of
https://github.com/WarmUpTill/SceneSwitcher.git
synced 2026-04-24 06:48:24 -05:00
Add support for variables
This commit is contained in:
parent
3ac4e22600
commit
c84c1638bf
|
|
@ -262,10 +262,12 @@ target_sources(
|
|||
src/utils/transition-selection.hpp
|
||||
src/utils/utility.cpp
|
||||
src/utils/utility.hpp
|
||||
src/utils/variable.cpp
|
||||
src/utils/variable.hpp
|
||||
src/utils/volume-control.cpp
|
||||
src/utils/volume-control.hpp
|
||||
src/utils/websocket-helpers.cpp
|
||||
src/utils/websocket-helpers.hpp
|
||||
src/utils/volume-control.hpp)
|
||||
src/utils/websocket-helpers.hpp)
|
||||
|
||||
# --- End of section ---
|
||||
|
||||
|
|
|
|||
|
|
@ -779,6 +779,9 @@ AdvSceneSwitcher.item.emptyName="Empty name not allowed!"
|
|||
AdvSceneSwitcher.item.nameNotAvailable="Name not available!"
|
||||
AdvSceneSwitcher.item.nameReserved="Name reserved!"
|
||||
|
||||
AdvSceneSwitcher.variable.select="--select variable--"
|
||||
AdvSceneSwitcher.variable.add="Add new variable"
|
||||
|
||||
AdvSceneSwitcher.connection.select="--select connection--"
|
||||
AdvSceneSwitcher.connection.add="Add new connection"
|
||||
AdvSceneSwitcher.connection.rename="Rename"
|
||||
|
|
|
|||
|
|
@ -95,6 +95,9 @@ signals:
|
|||
void ConnectionAdded(const QString &);
|
||||
void ConnectionRenamed(const QString &oldName, const QString &newName);
|
||||
void ConnectionRemoved(const QString &);
|
||||
void VariableAdded(const QString &);
|
||||
void VariableRenamed(const QString &oldName, const QString &newName);
|
||||
void VariableRemoved(const QString &);
|
||||
|
||||
public slots:
|
||||
void on_windowUp_clicked();
|
||||
|
|
|
|||
|
|
@ -503,6 +503,7 @@ void SwitcherData::loadSettings(obs_data_t *obj)
|
|||
// Needs to be loaded before any entries which might rely on scene group
|
||||
// selections to be available.
|
||||
loadSceneGroups(obj);
|
||||
loadVariables(obj);
|
||||
loadMacros(obj);
|
||||
loadConnections(obj);
|
||||
loadWindowTitleSwitches(obj);
|
||||
|
|
@ -537,6 +538,7 @@ void SwitcherData::saveSettings(obs_data_t *obj)
|
|||
saveSceneGroups(obj);
|
||||
saveMacros(obj);
|
||||
saveConnections(obj);
|
||||
saveVariables(obj);
|
||||
saveWindowTitleSwitches(obj);
|
||||
saveScreenRegionSwitches(obj);
|
||||
savePauseSwitches(obj);
|
||||
|
|
|
|||
|
|
@ -72,6 +72,7 @@ typedef struct transitionData {
|
|||
|
||||
} transitionData;
|
||||
|
||||
class Item;
|
||||
class SwitcherThread;
|
||||
|
||||
/********************************************************************************
|
||||
|
|
@ -141,6 +142,8 @@ struct SwitcherData {
|
|||
std::deque<Connection> connections;
|
||||
std::vector<std::string> websocketMessages;
|
||||
|
||||
std::deque<std::shared_ptr<Item>> variables;
|
||||
|
||||
std::deque<WindowSwitch> windowSwitches;
|
||||
std::vector<std::string> ignoreIdleWindows;
|
||||
std::string lastTitle;
|
||||
|
|
@ -291,6 +294,7 @@ struct SwitcherData {
|
|||
void saveSettings(obs_data_t *obj);
|
||||
void saveMacros(obs_data_t *obj);
|
||||
void saveConnections(obs_data_t *obj);
|
||||
void saveVariables(obs_data_t *obj);
|
||||
void saveWindowTitleSwitches(obs_data_t *obj);
|
||||
void saveScreenRegionSwitches(obs_data_t *obj);
|
||||
void savePauseSwitches(obs_data_t *obj);
|
||||
|
|
@ -315,6 +319,7 @@ struct SwitcherData {
|
|||
void loadSettings(obs_data_t *obj);
|
||||
void loadMacros(obs_data_t *obj);
|
||||
void loadConnections(obs_data_t *obj);
|
||||
void loadVariables(obs_data_t *obj);
|
||||
void loadWindowTitleSwitches(obs_data_t *obj);
|
||||
void loadScreenRegionSwitches(obs_data_t *obj);
|
||||
void loadPauseSwitches(obs_data_t *obj);
|
||||
|
|
|
|||
199
src/utils/variable.cpp
Normal file
199
src/utils/variable.cpp
Normal file
|
|
@ -0,0 +1,199 @@
|
|||
#include "variable.hpp"
|
||||
|
||||
#include <switcher-data-structs.hpp>
|
||||
|
||||
Variable::Variable() : Item() {}
|
||||
|
||||
void Variable::Load(obs_data_t *obj)
|
||||
{
|
||||
Item::Load(obj);
|
||||
_persist = obs_data_get_bool(obj, "persist");
|
||||
if (_persist) {
|
||||
_value = obs_data_get_string(obj, "value");
|
||||
}
|
||||
}
|
||||
|
||||
void Variable::Save(obs_data_t *obj) const
|
||||
{
|
||||
Item::Save(obj);
|
||||
obs_data_set_bool(obj, "persist", _persist);
|
||||
if (_persist) {
|
||||
obs_data_set_string(obj, "value", _value.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
bool Variable::DoubleValue(double &value) const
|
||||
{
|
||||
char *end = nullptr;
|
||||
value = strtod(_value.c_str(), &end);
|
||||
return end != _value.c_str() && *end == '\0' && value != HUGE_VAL;
|
||||
}
|
||||
|
||||
void Variable::SetValue(double value)
|
||||
{
|
||||
_value = std::to_string(value);
|
||||
}
|
||||
|
||||
Variable *GetVariableByName(const std::string &name)
|
||||
{
|
||||
for (const auto &v : switcher->variables) {
|
||||
if (v->Name() == name) {
|
||||
return dynamic_cast<Variable *>(v.get());
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Variable *GetVariableByQString(const QString &name)
|
||||
{
|
||||
return GetVariableByName(name.toStdString());
|
||||
}
|
||||
|
||||
std::weak_ptr<Variable> GetWeakVariableByName(const std::string &name)
|
||||
{
|
||||
for (const auto &v : switcher->variables) {
|
||||
if (v->Name() == name) {
|
||||
std::weak_ptr<Variable> wp =
|
||||
std::dynamic_pointer_cast<Variable>(v);
|
||||
return wp;
|
||||
}
|
||||
}
|
||||
return std::weak_ptr<Variable>();
|
||||
}
|
||||
|
||||
std::weak_ptr<Variable> GetWeakVariableByQString(const QString &name)
|
||||
{
|
||||
return GetWeakVariableByName(name.toStdString());
|
||||
}
|
||||
|
||||
QStringList GetVariablesNameList()
|
||||
{
|
||||
QStringList list;
|
||||
for (const auto &var : switcher->variables) {
|
||||
list << QString::fromStdString(var->Name());
|
||||
}
|
||||
list.sort();
|
||||
return list;
|
||||
}
|
||||
|
||||
void SwitcherData::saveVariables(obs_data_t *obj)
|
||||
{
|
||||
obs_data_array_t *variablesArray = obs_data_array_create();
|
||||
for (const auto &v : variables) {
|
||||
obs_data_t *array_obj = obs_data_create();
|
||||
v->Save(array_obj);
|
||||
obs_data_array_push_back(variablesArray, array_obj);
|
||||
obs_data_release(array_obj);
|
||||
}
|
||||
obs_data_set_array(obj, "variables", variablesArray);
|
||||
obs_data_array_release(variablesArray);
|
||||
}
|
||||
|
||||
void SwitcherData::loadVariables(obs_data_t *obj)
|
||||
{
|
||||
variables.clear();
|
||||
|
||||
obs_data_array_t *variablesArray = obs_data_get_array(obj, "variables");
|
||||
size_t count = obs_data_array_count(variablesArray);
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
obs_data_t *array_obj = obs_data_array_item(variablesArray, i);
|
||||
auto var = Variable::Create();
|
||||
variables.emplace_back(var);
|
||||
variables.back()->Load(array_obj);
|
||||
obs_data_release(array_obj);
|
||||
}
|
||||
obs_data_array_release(variablesArray);
|
||||
}
|
||||
|
||||
VariableSettingsDialog::VariableSettingsDialog(QWidget *parent,
|
||||
const Variable &settings)
|
||||
: ItemSettingsDialog(settings, switcher->variables,
|
||||
"AdvSceneSwitcher.variable.select",
|
||||
"AdvSceneSwitcher.variable.add", parent),
|
||||
_value(new QLineEdit()),
|
||||
_persist(new QCheckBox())
|
||||
{
|
||||
_value->setText(QString::fromStdString(settings._value));
|
||||
_persist->setChecked(settings._persist);
|
||||
|
||||
QGridLayout *layout = new QGridLayout;
|
||||
int row = 0;
|
||||
layout->addWidget(
|
||||
new QLabel(obs_module_text("AdvSceneSwitcher.variable.name")),
|
||||
row, 0);
|
||||
QHBoxLayout *nameLayout = new QHBoxLayout;
|
||||
nameLayout->addWidget(_name);
|
||||
nameLayout->addWidget(_nameHint);
|
||||
layout->addLayout(nameLayout, row, 1);
|
||||
++row;
|
||||
layout->addWidget(
|
||||
new QLabel(obs_module_text("AdvSceneSwitcher.variable.value")),
|
||||
row, 0);
|
||||
layout->addWidget(_value, row, 1);
|
||||
++row;
|
||||
layout->addWidget(new QLabel(obs_module_text(
|
||||
"AdvSceneSwitcher.variable.persist")),
|
||||
row, 0);
|
||||
layout->addWidget(_persist, row, 1);
|
||||
++row;
|
||||
layout->addWidget(_buttonbox, row, 0, 1, -1);
|
||||
setLayout(layout);
|
||||
}
|
||||
|
||||
bool VariableSettingsDialog::AskForSettings(QWidget *parent, Variable &settings)
|
||||
{
|
||||
VariableSettingsDialog dialog(parent, settings);
|
||||
dialog.setWindowTitle(obs_module_text("AdvSceneSwitcher.windowTitle"));
|
||||
if (dialog.exec() != DialogCode::Accepted) {
|
||||
return false;
|
||||
}
|
||||
|
||||
settings._name = dialog._name->text().toStdString();
|
||||
settings._value = dialog._value->text().toStdString();
|
||||
settings._persist = dialog._persist->isChecked();
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool AskForSettingsWrapper(QWidget *parent, Item &settings)
|
||||
{
|
||||
Variable &VariableSettings = dynamic_cast<Variable &>(settings);
|
||||
return VariableSettingsDialog::AskForSettings(parent, VariableSettings);
|
||||
}
|
||||
|
||||
VariableSelection::VariableSelection(QWidget *parent)
|
||||
: ItemSelection(switcher->variables, Variable::Create,
|
||||
AskForSettingsWrapper,
|
||||
"AdvSceneSwitcher.variable.select",
|
||||
"AdvSceneSwitcher.variable.add", parent)
|
||||
{
|
||||
// Connect to slots
|
||||
QWidget::connect(
|
||||
window(),
|
||||
SIGNAL(VariableRenamed(const QString &, const QString &)), this,
|
||||
SLOT(RenameItem(const QString &, const QString &)));
|
||||
QWidget::connect(window(), SIGNAL(VariableAdded(const QString &)), this,
|
||||
SLOT(AddItem(const QString &)));
|
||||
QWidget::connect(window(), SIGNAL(VariableRemoved(const QString &)),
|
||||
this, SLOT(RemoveItem(const QString &)));
|
||||
|
||||
// Forward signals
|
||||
QWidget::connect(
|
||||
this, SIGNAL(ItemRenamed(const QString &, const QString &)),
|
||||
window(),
|
||||
SIGNAL(VariableRenamed(const QString &, const QString &)));
|
||||
QWidget::connect(this, SIGNAL(ItemAdded(const QString &)), window(),
|
||||
SIGNAL(VariableAdded(const QString &)));
|
||||
QWidget::connect(this, SIGNAL(ItemRemoved(const QString &)), window(),
|
||||
SIGNAL(VariableRemoved(const QString &)));
|
||||
}
|
||||
|
||||
void VariableSelection::SetVariable(const std::string &variable)
|
||||
{
|
||||
const QSignalBlocker blocker(_selection);
|
||||
if (!!GetVariableByName(variable)) {
|
||||
_selection->setCurrentText(QString::fromStdString(variable));
|
||||
} else {
|
||||
_selection->setCurrentIndex(0);
|
||||
}
|
||||
}
|
||||
57
src/utils/variable.hpp
Normal file
57
src/utils/variable.hpp
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
#pragma once
|
||||
#include "item-selection-helpers.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <QStringList>
|
||||
#include <obs.hpp>
|
||||
|
||||
class VariableSelection;
|
||||
class VariableSettingsDialog;
|
||||
|
||||
class Variable : public Item {
|
||||
public:
|
||||
Variable();
|
||||
void Load(obs_data_t *obj);
|
||||
void Save(obs_data_t *obj) const;
|
||||
std::string Value() const { return _value; }
|
||||
bool DoubleValue(double &) const;
|
||||
void SetValue(const std::string &val) { _value = val; }
|
||||
void SetValue(double);
|
||||
static std::shared_ptr<Item> Create()
|
||||
{
|
||||
return std::make_shared<Variable>();
|
||||
}
|
||||
|
||||
private:
|
||||
bool _persist = false;
|
||||
std::string _value = "";
|
||||
|
||||
friend VariableSelection;
|
||||
friend VariableSettingsDialog;
|
||||
};
|
||||
|
||||
Variable *GetVariableByName(const std::string &name);
|
||||
Variable *GetVariableByQString(const QString &name);
|
||||
std::weak_ptr<Variable> GetWeakVariableByName(const std::string &name);
|
||||
std::weak_ptr<Variable> GetWeakVariableByQString(const QString &name);
|
||||
QStringList GetVariablesNameList();
|
||||
|
||||
class VariableSettingsDialog : public ItemSettingsDialog {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
VariableSettingsDialog(QWidget *parent, const Variable &);
|
||||
static bool AskForSettings(QWidget *parent, Variable &settings);
|
||||
|
||||
private:
|
||||
QLineEdit *_value;
|
||||
QCheckBox *_persist;
|
||||
};
|
||||
|
||||
class VariableSelection : public ItemSelection {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
VariableSelection(QWidget *parent = 0);
|
||||
void SetVariable(const std::string &);
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user