mirror of
https://github.com/WarmUpTill/SceneSwitcher.git
synced 2026-03-26 03:45:24 -05:00
Add websocket condition
This commit is contained in:
parent
866c49bffd
commit
59cc37c90a
|
|
@ -227,6 +227,8 @@ void SwitcherData::Thread()
|
|||
}
|
||||
}
|
||||
|
||||
websocketMessages.clear();
|
||||
|
||||
// After this point we will call frontend functions like
|
||||
// obs_frontend_set_current_scene() and
|
||||
// obs_frontend_set_current_transition()
|
||||
|
|
|
|||
121
src/macro-core/macro-condition-websocket.cpp
Normal file
121
src/macro-core/macro-condition-websocket.cpp
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
#include "macro-condition-edit.hpp"
|
||||
#include "macro-condition-websocket.hpp"
|
||||
#include "utility.hpp"
|
||||
#include "advanced-scene-switcher.hpp"
|
||||
|
||||
#include <regex>
|
||||
|
||||
const std::string MacroConditionWebsocket::id = "websocket";
|
||||
|
||||
bool MacroConditionWebsocket::_registered = MacroConditionFactory::Register(
|
||||
MacroConditionWebsocket::id,
|
||||
{MacroConditionWebsocket::Create, MacroConditionWebsocketEdit::Create,
|
||||
"AdvSceneSwitcher.condition.websocket"});
|
||||
|
||||
static bool matchRegex(const std::string &msg, const std::string &expr)
|
||||
{
|
||||
try {
|
||||
std::regex regExpr(expr);
|
||||
return std::regex_match(msg, regExpr);
|
||||
} catch (const std::regex_error &) {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool MacroConditionWebsocket::CheckCondition()
|
||||
{
|
||||
for (const auto &msg : switcher->websocketMessages) {
|
||||
if (_useRegex) {
|
||||
if (matchRegex(msg, _message)) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
if (msg == _message) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool MacroConditionWebsocket::Save(obs_data_t *obj)
|
||||
{
|
||||
MacroCondition::Save(obj);
|
||||
obs_data_set_string(obj, "message", _message.c_str());
|
||||
obs_data_set_bool(obj, "useRegex", _useRegex);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MacroConditionWebsocket::Load(obs_data_t *obj)
|
||||
{
|
||||
MacroCondition::Load(obj);
|
||||
_message = obs_data_get_string(obj, "message");
|
||||
_useRegex = obs_data_get_bool(obj, "useRegex");
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string MacroConditionWebsocket::GetShortDesc()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
MacroConditionWebsocketEdit::MacroConditionWebsocketEdit(
|
||||
QWidget *parent, std::shared_ptr<MacroConditionWebsocket> entryData)
|
||||
: QWidget(parent),
|
||||
_message(new ResizingPlainTextEdit(this)),
|
||||
_useRegex(new QCheckBox(obs_module_text(
|
||||
"AdvSceneSwitcher.condition.websocket.useRegex")))
|
||||
{
|
||||
QWidget::connect(_message, SIGNAL(textChanged()), this,
|
||||
SLOT(MessageChanged()));
|
||||
QWidget::connect(_useRegex, SIGNAL(stateChanged(int)), this,
|
||||
SLOT(UseRegexChanged(int)));
|
||||
|
||||
QVBoxLayout *mainLayout = new QVBoxLayout;
|
||||
|
||||
mainLayout->addWidget(new QLabel(
|
||||
obs_module_text("AdvSceneSwitcher.condition.websocket.entry")));
|
||||
mainLayout->addWidget(_message);
|
||||
mainLayout->addWidget(_useRegex);
|
||||
setLayout(mainLayout);
|
||||
|
||||
_entryData = entryData;
|
||||
UpdateEntryData();
|
||||
_loading = false;
|
||||
}
|
||||
|
||||
void MacroConditionWebsocketEdit::UpdateEntryData()
|
||||
{
|
||||
if (!_entryData) {
|
||||
return;
|
||||
}
|
||||
|
||||
_message->setPlainText(QString::fromStdString(_entryData->_message));
|
||||
_useRegex->setChecked(_entryData->_useRegex);
|
||||
|
||||
adjustSize();
|
||||
updateGeometry();
|
||||
}
|
||||
|
||||
void MacroConditionWebsocketEdit::MessageChanged()
|
||||
{
|
||||
if (_loading || !_entryData) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(switcher->m);
|
||||
_entryData->_message = _message->toPlainText().toUtf8().constData();
|
||||
|
||||
adjustSize();
|
||||
updateGeometry();
|
||||
}
|
||||
|
||||
void MacroConditionWebsocketEdit::UseRegexChanged(int state)
|
||||
{
|
||||
if (_loading || !_entryData) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(switcher->m);
|
||||
_entryData->_useRegex = state;
|
||||
}
|
||||
58
src/macro-core/macro-condition-websocket.hpp
Normal file
58
src/macro-core/macro-condition-websocket.hpp
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
#pragma once
|
||||
#include "macro.hpp"
|
||||
#include "resizing-text-edit.hpp"
|
||||
|
||||
#include <QCheckBox>
|
||||
|
||||
class MacroConditionWebsocket : public MacroCondition {
|
||||
public:
|
||||
MacroConditionWebsocket(Macro *m) : MacroCondition(m) {}
|
||||
bool CheckCondition();
|
||||
bool Save(obs_data_t *obj);
|
||||
bool Load(obs_data_t *obj);
|
||||
std::string GetShortDesc();
|
||||
std::string GetId() { return id; };
|
||||
static std::shared_ptr<MacroCondition> Create(Macro *m)
|
||||
{
|
||||
return std::make_shared<MacroConditionWebsocket>(m);
|
||||
}
|
||||
|
||||
std::string _message = obs_module_text("AdvSceneSwitcher.enterText");
|
||||
bool _useRegex = false;
|
||||
|
||||
private:
|
||||
static bool _registered;
|
||||
static const std::string id;
|
||||
};
|
||||
|
||||
class MacroConditionWebsocketEdit : public QWidget {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
MacroConditionWebsocketEdit(
|
||||
QWidget *parent,
|
||||
std::shared_ptr<MacroConditionWebsocket> cond = nullptr);
|
||||
void UpdateEntryData();
|
||||
static QWidget *Create(QWidget *parent,
|
||||
std::shared_ptr<MacroCondition> cond)
|
||||
{
|
||||
return new MacroConditionWebsocketEdit(
|
||||
parent,
|
||||
std::dynamic_pointer_cast<MacroConditionWebsocket>(
|
||||
cond));
|
||||
}
|
||||
|
||||
private slots:
|
||||
void MessageChanged();
|
||||
void UseRegexChanged(int state);
|
||||
signals:
|
||||
void HeaderInfoChanged(const QString &);
|
||||
|
||||
protected:
|
||||
ResizingPlainTextEdit *_message;
|
||||
QCheckBox *_useRegex;
|
||||
std::shared_ptr<MacroConditionWebsocket> _entryData;
|
||||
|
||||
private:
|
||||
bool _loading = true;
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user