mirror of
https://github.com/WarmUpTill/SceneSwitcher.git
synced 2026-06-21 20:12:00 -05:00
Improve random action
* Allow duplicates to enable weighting macro occurance * Add option to allow consecutive execution of the same macro
This commit is contained in:
parent
e0602f8de2
commit
e147402250
|
|
@ -522,6 +522,7 @@ AdvSceneSwitcher.action.timer.type.reset="Reset"
|
|||
AdvSceneSwitcher.action.timer.type.setTimeRemaining="Set time remaining of"
|
||||
AdvSceneSwitcher.action.timer.entry="{{timerAction}} timers on {{macros}} {{duration}}"
|
||||
AdvSceneSwitcher.action.random="Random"
|
||||
AdvSceneSwitcher.action.random.allowRepeat="Allow consecutive execution of the same macro"
|
||||
AdvSceneSwitcher.action.random.entry="Randomly run any of the following macros (paused macros are ignored)"
|
||||
AdvSceneSwitcher.action.systray="System tray notification"
|
||||
AdvSceneSwitcher.action.systray.entry="Show notification: {{message}}"
|
||||
|
|
|
|||
|
|
@ -11,8 +11,8 @@ bool MacroActionRandom::_registered = MacroActionFactory::Register(
|
|||
{MacroActionRandom::Create, MacroActionRandomEdit::Create,
|
||||
"AdvSceneSwitcher.action.random"});
|
||||
|
||||
std::vector<MacroRef> getNextMacro(std::vector<MacroRef> ¯os,
|
||||
MacroRef &lastRandomMacro)
|
||||
std::vector<MacroRef> getNextMacros(std::vector<MacroRef> ¯os,
|
||||
MacroRef &lastRandomMacro, bool allowRepeat)
|
||||
{
|
||||
std::vector<MacroRef> res;
|
||||
if (macros.size() == 1) {
|
||||
|
|
@ -25,7 +25,7 @@ std::vector<MacroRef> getNextMacro(std::vector<MacroRef> ¯os,
|
|||
|
||||
for (auto &m : macros) {
|
||||
if (m.get() && !m->Paused() &&
|
||||
!(lastRandomMacro.get() == m.get())) {
|
||||
(allowRepeat || (lastRandomMacro.get() != m.get()))) {
|
||||
res.push_back(m);
|
||||
}
|
||||
}
|
||||
|
|
@ -38,7 +38,7 @@ bool MacroActionRandom::PerformAction()
|
|||
return true;
|
||||
}
|
||||
|
||||
auto macros = getNextMacro(_macros, lastRandomMacro);
|
||||
auto macros = getNextMacros(_macros, lastRandomMacro, _allowRepeat);
|
||||
if (macros.size() == 0) {
|
||||
return true;
|
||||
}
|
||||
|
|
@ -61,6 +61,7 @@ bool MacroActionRandom::Save(obs_data_t *obj) const
|
|||
{
|
||||
MacroAction::Save(obj);
|
||||
SaveMacroList(obj, _macros);
|
||||
obs_data_set_bool(obj, "allowRepeat", _allowRepeat);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -68,12 +69,16 @@ bool MacroActionRandom::Load(obs_data_t *obj)
|
|||
{
|
||||
MacroAction::Load(obj);
|
||||
LoadMacroList(obj, _macros);
|
||||
_allowRepeat = obs_data_get_bool(obj, "allowRepeat");
|
||||
return true;
|
||||
}
|
||||
|
||||
MacroActionRandomEdit::MacroActionRandomEdit(
|
||||
QWidget *parent, std::shared_ptr<MacroActionRandom> entryData)
|
||||
: QWidget(parent), _list(new MacroList(this, false, false))
|
||||
: QWidget(parent),
|
||||
_list(new MacroList(this, true, false)),
|
||||
_allowRepeat(new QCheckBox(obs_module_text(
|
||||
"AdvSceneSwitcher.action.random.allowRepeat")))
|
||||
{
|
||||
QWidget::connect(_list, SIGNAL(Added(const std::string &)), this,
|
||||
SLOT(Add(const std::string &)));
|
||||
|
|
@ -82,6 +87,8 @@ MacroActionRandomEdit::MacroActionRandomEdit(
|
|||
this, SLOT(Replace(int, const std::string &)));
|
||||
QWidget::connect(window(), SIGNAL(MacroRemoved(const QString &)), this,
|
||||
SLOT(MacroRemove(const QString &)));
|
||||
QWidget::connect(_allowRepeat, SIGNAL(stateChanged(int)), this,
|
||||
SLOT(AllowRepeatChanged(int)));
|
||||
|
||||
auto *entryLayout = new QHBoxLayout;
|
||||
std::unordered_map<std::string, QWidget *> widgetPlaceholders = {};
|
||||
|
|
@ -91,6 +98,7 @@ MacroActionRandomEdit::MacroActionRandomEdit(
|
|||
auto *mainLayout = new QVBoxLayout;
|
||||
mainLayout->addLayout(entryLayout);
|
||||
mainLayout->addWidget(_list);
|
||||
mainLayout->addWidget(_allowRepeat);
|
||||
setLayout(mainLayout);
|
||||
|
||||
_entryData = entryData;
|
||||
|
|
@ -105,6 +113,8 @@ void MacroActionRandomEdit::UpdateEntryData()
|
|||
}
|
||||
|
||||
_list->SetContent(_entryData->_macros);
|
||||
_allowRepeat->setChecked(_entryData->_allowRepeat);
|
||||
_allowRepeat->setVisible(ShouldShowAllowRepeat());
|
||||
adjustSize();
|
||||
}
|
||||
|
||||
|
|
@ -135,6 +145,7 @@ void MacroActionRandomEdit::Add(const std::string &name)
|
|||
std::lock_guard<std::mutex> lock(switcher->m);
|
||||
MacroRef macro(name);
|
||||
_entryData->_macros.push_back(macro);
|
||||
_allowRepeat->setVisible(ShouldShowAllowRepeat());
|
||||
adjustSize();
|
||||
}
|
||||
|
||||
|
|
@ -146,6 +157,7 @@ void MacroActionRandomEdit::Remove(int idx)
|
|||
|
||||
std::lock_guard<std::mutex> lock(switcher->m);
|
||||
_entryData->_macros.erase(std::next(_entryData->_macros.begin(), idx));
|
||||
_allowRepeat->setVisible(ShouldShowAllowRepeat());
|
||||
adjustSize();
|
||||
}
|
||||
|
||||
|
|
@ -160,3 +172,27 @@ void MacroActionRandomEdit::Replace(int idx, const std::string &name)
|
|||
_entryData->_macros[idx] = macro;
|
||||
adjustSize();
|
||||
}
|
||||
|
||||
void MacroActionRandomEdit::AllowRepeatChanged(int value)
|
||||
{
|
||||
if (_loading || !_entryData) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(switcher->m);
|
||||
_entryData->_allowRepeat = value;
|
||||
}
|
||||
|
||||
bool MacroActionRandomEdit::ShouldShowAllowRepeat()
|
||||
{
|
||||
if (_entryData->_macros.size() <= 1) {
|
||||
return false;
|
||||
}
|
||||
const auto macro = _entryData->_macros[0];
|
||||
for (const auto m : _entryData->_macros) {
|
||||
if (macro.get() != m.get()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
#include <QPushButton>
|
||||
#include <QListWidget>
|
||||
#include <unordered_map>
|
||||
#include <QCheckBox>
|
||||
|
||||
class MacroActionRandom : public MultiMacroRefAction {
|
||||
public:
|
||||
|
|
@ -19,8 +19,7 @@ public:
|
|||
return std::make_shared<MacroActionRandom>(m);
|
||||
}
|
||||
|
||||
// TODO: add weights to each macro ...
|
||||
// std::unordered_map<MacroRef, int> _weights;
|
||||
bool _allowRepeat = false;
|
||||
|
||||
private:
|
||||
MacroRef lastRandomMacro;
|
||||
|
|
@ -49,11 +48,15 @@ private slots:
|
|||
void Add(const std::string &);
|
||||
void Remove(int);
|
||||
void Replace(int, const std::string &);
|
||||
void AllowRepeatChanged(int);
|
||||
|
||||
protected:
|
||||
std::shared_ptr<MacroActionRandom> _entryData;
|
||||
|
||||
private:
|
||||
bool ShouldShowAllowRepeat();
|
||||
|
||||
MacroList *_list;
|
||||
QCheckBox *_allowRepeat;
|
||||
bool _loading = true;
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user