Skip to content

Commit

Permalink
Add option to clear message buffer on match
Browse files Browse the repository at this point in the history
  • Loading branch information
WarmUpTill committed Jun 27, 2024
1 parent dfedbcf commit 7ba487f
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 88 deletions.
2 changes: 2 additions & 0 deletions data/locale/en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1869,6 +1869,8 @@ AdvSceneSwitcher.audio.monitor.both="Monitor and Output"

AdvSceneSwitcher.noSettingsButtons="No buttons found!"

AdvSceneSwitcher.clearBufferOnMatch="Clear message buffer when matching message was found"

; Legacy tabs below - please don't waste your time adding translations for these :)
; Transition Tab
AdvSceneSwitcher.transitionTab.title="Transition"
Expand Down
69 changes: 41 additions & 28 deletions plugins/base/macro-condition-websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,27 @@ bool MacroConditionWebsocket::CheckCondition()
continue;
}
if (_regex.Enabled()) {
if (_regex.Matches(*message, _message)) {
SetTempVarValue("message", *message);
SetVariableValue(*message);
return true;
if (!_regex.Matches(*message, _message)) {
continue;
}

SetTempVarValue("message", *message);
SetVariableValue(*message);
if (_clearBufferOnMatch) {
_messageBuffer->Clear();
}
return true;
} else {
if (*message == std::string(_message)) {
SetTempVarValue("message", *message);
SetVariableValue(*message);
return true;
if (*message != std::string(_message)) {
continue;
}

SetTempVarValue("message", *message);
SetVariableValue(*message);
if (_clearBufferOnMatch) {
_messageBuffer->Clear();
}
return true;
}
}
SetVariableValue("");
Expand All @@ -72,6 +82,8 @@ bool MacroConditionWebsocket::Save(obs_data_t *obj) const
_regex.Save(obj);
obs_data_set_string(obj, "connection",
GetWeakConnectionName(_connection).c_str());
obs_data_set_bool(obj, "clearBufferOnMatch", _clearBufferOnMatch);
obs_data_set_int(obj, "version", 1);
return true;
}

Expand All @@ -89,6 +101,11 @@ bool MacroConditionWebsocket::Load(obs_data_t *obj)
_connection =
GetWeakConnectionByName(obs_data_get_string(obj, "connection"));

_clearBufferOnMatch = obs_data_get_bool(obj, "clearBufferOnMatch");
if (!obs_data_has_user_value(obj, "version")) {
_clearBufferOnMatch = true;
}

SetType(_type);
return true;
}
Expand Down Expand Up @@ -160,6 +177,8 @@ MacroConditionWebsocketEdit::MacroConditionWebsocketEdit(
_message(new VariableTextEdit(this)),
_regex(new RegexConfigWidget(parent)),
_connection(new WSConnectionSelection(this)),
_clearBufferOnMatch(new QCheckBox(
obs_module_text("AdvSceneSwitcher.clearBufferOnMatch"))),
_editLayout(new QHBoxLayout())
{
populateConditionSelection(_conditions);
Expand All @@ -174,6 +193,8 @@ MacroConditionWebsocketEdit::MacroConditionWebsocketEdit(
QWidget::connect(_connection, SIGNAL(SelectionChanged(const QString &)),
this,
SLOT(ConnectionSelectionChanged(const QString &)));
QWidget::connect(_clearBufferOnMatch, SIGNAL(stateChanged(int)), this,
SLOT(ClearBufferOnMatchChanged(int)));

QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(_editLayout);
Expand All @@ -183,6 +204,7 @@ MacroConditionWebsocketEdit::MacroConditionWebsocketEdit(
regexLayout->addStretch();
regexLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->addLayout(regexLayout);
mainLayout->addWidget(_clearBufferOnMatch);
setLayout(mainLayout);

_entryData = entryData;
Expand Down Expand Up @@ -232,6 +254,7 @@ void MacroConditionWebsocketEdit::UpdateEntryData()
_message->setPlainText(_entryData->_message);
_regex->SetRegexConfig(_entryData->_regex);
_connection->SetConnection(_entryData->GetConnection());
_clearBufferOnMatch->setChecked(_entryData->_clearBufferOnMatch);

if (_entryData->GetType() == MacroConditionWebsocket::Type::REQUEST) {
SetupRequestEdit();
Expand All @@ -245,11 +268,7 @@ void MacroConditionWebsocketEdit::UpdateEntryData()

void MacroConditionWebsocketEdit::ConditionChanged(int index)
{
if (_loading || !_entryData) {
return;
}

auto lock = LockContext();
GUARD_LOADING_AND_LOCK();
_entryData->SetType(static_cast<MacroConditionWebsocket::Type>(index));
if (_entryData->GetType() == MacroConditionWebsocket::Type::REQUEST) {
SetupRequestEdit();
Expand All @@ -262,11 +281,7 @@ void MacroConditionWebsocketEdit::ConditionChanged(int index)

void MacroConditionWebsocketEdit::MessageChanged()
{
if (_loading || !_entryData) {
return;
}

auto lock = LockContext();
GUARD_LOADING_AND_LOCK();
_entryData->_message = _message->toPlainText().toUtf8().constData();

adjustSize();
Expand All @@ -276,22 +291,20 @@ void MacroConditionWebsocketEdit::MessageChanged()
void MacroConditionWebsocketEdit::ConnectionSelectionChanged(
const QString &connection)
{
if (_loading || !_entryData) {
return;
}

auto lock = LockContext();
GUARD_LOADING_AND_LOCK();
_entryData->SetConnection(connection.toStdString());
emit(HeaderInfoChanged(connection));
}

void MacroConditionWebsocketEdit::RegexChanged(const RegexConfig &conf)
void MacroConditionWebsocketEdit::ClearBufferOnMatchChanged(int value)
{
if (_loading || !_entryData) {
return;
}
GUARD_LOADING_AND_LOCK();
_entryData->_clearBufferOnMatch = value;
}

auto lock = LockContext();
void MacroConditionWebsocketEdit::RegexChanged(const RegexConfig &conf)
{
GUARD_LOADING_AND_LOCK();
_entryData->_regex = conf;

adjustSize();
Expand Down
8 changes: 5 additions & 3 deletions plugins/base/macro-condition-websocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class MacroConditionWebsocket : public MacroCondition {
std::weak_ptr<WSConnection> GetConnection() const;
StringVariable _message = obs_module_text("AdvSceneSwitcher.enterText");
RegexConfig _regex;
bool _clearBufferOnMatch = true;

private:
void SetupTempVars();
Expand Down Expand Up @@ -69,12 +70,10 @@ private slots:
void MessageChanged();
void RegexChanged(const RegexConfig &);
void ConnectionSelectionChanged(const QString &);
void ClearBufferOnMatchChanged(int);
signals:
void HeaderInfoChanged(const QString &);

protected:
std::shared_ptr<MacroConditionWebsocket> _entryData;

private:
void SetupRequestEdit();
void SetupEventEdit();
Expand All @@ -83,7 +82,10 @@ private slots:
VariableTextEdit *_message;
RegexConfigWidget *_regex;
WSConnectionSelection *_connection;
QCheckBox *_clearBufferOnMatch;
QHBoxLayout *_editLayout;

std::shared_ptr<MacroConditionWebsocket> _entryData;
bool _loading = true;
};

Expand Down
29 changes: 23 additions & 6 deletions plugins/midi/macro-condition-midi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ bool MacroConditionMidi::CheckCondition()
}
if (message->Matches(_message)) {
SetVariableValues(*message);
if (_clearBufferOnMatch) {
_messageBuffer->Clear();
}
return true;
}
}
Expand All @@ -45,6 +48,8 @@ bool MacroConditionMidi::Save(obs_data_t *obj) const
MacroCondition::Save(obj);
_message.Save(obj);
_device.Save(obj);
obs_data_set_bool(obj, "clearBufferOnMatch", _clearBufferOnMatch);
obs_data_set_int(obj, "version", 1);
return true;
}

Expand All @@ -54,6 +59,10 @@ bool MacroConditionMidi::Load(obs_data_t *obj)
_message.Load(obj);
_device.Load(obj);
_messageBuffer = _device.RegisterForMidiMessages();
_clearBufferOnMatch = obs_data_get_bool(obj, "clearBufferOnMatch");
if (!obs_data_has_user_value(obj, "version")) {
_clearBufferOnMatch = true;
}
return true;
}

Expand Down Expand Up @@ -106,7 +115,9 @@ MacroConditionMidiEdit::MacroConditionMidiEdit(
_resetMidiDevices(new QPushButton(
obs_module_text("AdvSceneSwitcher.midi.resetDevices"))),
_listen(new QPushButton(
obs_module_text("AdvSceneSwitcher.midi.startListen")))
obs_module_text("AdvSceneSwitcher.midi.startListen"))),
_clearBufferOnMatch(new QCheckBox(
obs_module_text("AdvSceneSwitcher.clearBufferOnMatch")))
{
QWidget::connect(_devices,
SIGNAL(DeviceSelectionChanged(const MidiDevice &)),
Expand All @@ -119,6 +130,8 @@ MacroConditionMidiEdit::MacroConditionMidiEdit(
SLOT(ResetMidiDevices()));
QWidget::connect(_listen, SIGNAL(clicked()), this,
SLOT(ToggleListen()));
QWidget::connect(_clearBufferOnMatch, SIGNAL(stateChanged(int)), this,
SLOT(ClearBufferOnMatchChanged(int)));
QWidget::connect(&_listenTimer, SIGNAL(timeout()), this,
SLOT(SetMessageSelectionToLastReceived()));

Expand All @@ -135,6 +148,7 @@ MacroConditionMidiEdit::MacroConditionMidiEdit(
mainLayout->addWidget(_message);
mainLayout->addLayout(listenLayout);
mainLayout->addWidget(_resetMidiDevices);
mainLayout->addWidget(_clearBufferOnMatch);
setLayout(mainLayout);

_listenTimer.setInterval(100);
Expand All @@ -157,6 +171,7 @@ void MacroConditionMidiEdit::UpdateEntryData()

_message->SetMessage(_entryData->_message);
_devices->SetDevice(_entryData->GetDevice());
_clearBufferOnMatch->setChecked(_entryData->_clearBufferOnMatch);

adjustSize();
updateGeometry();
Expand All @@ -182,14 +197,16 @@ void MacroConditionMidiEdit::DeviceSelectionChanged(const MidiDevice &device)

void MacroConditionMidiEdit::MidiMessageChanged(const MidiMessage &message)
{
if (_loading || !_entryData) {
return;
}

auto lock = LockContext();
GUARD_LOADING_AND_LOCK();
_entryData->_message = message;
}

void MacroConditionMidiEdit::ClearBufferOnMatchChanged(int value)
{
GUARD_LOADING_AND_LOCK();
_entryData->_clearBufferOnMatch = value;
}

void MacroConditionMidiEdit::ResetMidiDevices()
{
auto lock = LockContext();
Expand Down
8 changes: 6 additions & 2 deletions plugins/midi/macro-condition-midi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "macro-condition-edit.hpp"
#include "midi-helpers.hpp"

#include <QCheckBox>
#include <QPushButton>
#include <QTimer>

Expand All @@ -23,6 +24,7 @@ class MacroConditionMidi : public MacroCondition {
void SetDevice(const MidiDevice &dev);
const MidiDevice &GetDevice() const { return _device; }
MidiMessage _message;
bool _clearBufferOnMatch = true;

private:
void SetupTempVars();
Expand Down Expand Up @@ -55,6 +57,7 @@ class MacroConditionMidiEdit : public QWidget {
private slots:
void DeviceSelectionChanged(const MidiDevice &);
void MidiMessageChanged(const MidiMessage &);
void ClearBufferOnMatchChanged(int);
void ResetMidiDevices();
void ToggleListen();
void SetMessageSelectionToLastReceived();
Expand All @@ -64,12 +67,13 @@ private slots:
private:
void EnableListening(bool);

std::shared_ptr<MacroConditionMidi> _entryData;

MidiDeviceSelection *_devices;
MidiMessageSelection *_message;
QPushButton *_resetMidiDevices;
QPushButton *_listen;
QCheckBox *_clearBufferOnMatch;

std::shared_ptr<MacroConditionMidi> _entryData;
QTimer _listenTimer;
MidiMessageBuffer _messageBuffer;
bool _currentlyListening = false;
Expand Down
Loading

0 comments on commit 7ba487f

Please sign in to comment.