SceneSwitcher/plugins/base/macro-condition-hotkey.cpp
WarmUpTill aeb0e7f8ad Fix hotkey condition evaluating to true if macro was paused
The condition check now also takes the time during which the macro was
paused into consideration
2024-01-27 14:10:34 +01:00

122 lines
3.4 KiB
C++

#include "macro-condition-hotkey.hpp"
#include "layout-helpers.hpp"
#include "macro-helpers.hpp"
namespace advss {
const std::string MacroConditionHotkey::id = "hotkey";
bool MacroConditionHotkey::_registered = MacroConditionFactory::Register(
MacroConditionHotkey::id,
{MacroConditionHotkey::Create, MacroConditionHotkeyEdit::Create,
"AdvSceneSwitcher.condition.hotkey"});
static uint32_t count = 1;
MacroConditionHotkey::MacroConditionHotkey(Macro *m) : MacroCondition(m)
{
auto name = obs_module_text("AdvSceneSwitcher.condition.hotkey.name") +
std::string(" ") + std::to_string(count);
_hotkey = Hotkey::GetHotkey(name, true);
count++;
}
bool MacroConditionHotkey::CheckCondition()
{
const bool hotkeyIsCurrentlyPressed = _hotkey->GetPressed();
const bool hotkeyWasPressedSinceLastCheck = _hotkey->GetLastPressed() >
_lastCheck;
const bool macroWasPausedSinceLastCheck =
MacroWasPausedSince(GetMacro(), _lastCheck);
bool ret = hotkeyIsCurrentlyPressed ||
(hotkeyWasPressedSinceLastCheck &&
!macroWasPausedSinceLastCheck);
_lastCheck = std::chrono::high_resolution_clock::now();
return ret;
}
bool MacroConditionHotkey::Save(obs_data_t *obj) const
{
MacroCondition::Save(obj);
_hotkey->Save(obj);
return true;
}
bool MacroConditionHotkey::Load(obs_data_t *obj)
{
MacroCondition::Load(obj);
if (!_hotkey->Load(obj)) {
auto description = obs_data_get_string(obj, "desc");
_hotkey = Hotkey::GetHotkey(description);
vblog(LOG_WARNING,
"hotkey name conflict for \"%s\" - using previous key bind",
description);
}
return true;
}
MacroConditionHotkeyEdit::MacroConditionHotkeyEdit(
QWidget *parent, std::shared_ptr<MacroConditionHotkey> entryData)
: QWidget(parent)
{
_name = new QLineEdit();
QLabel *line1 = new QLabel(obs_module_text(
"AdvSceneSwitcher.condition.hotkey.entry.line1"));
QLabel *hint = new QLabel(
obs_module_text("AdvSceneSwitcher.condition.hotkey.tip"));
QWidget::connect(_name, SIGNAL(editingFinished()), this,
SLOT(NameChanged()));
QHBoxLayout *switchLayout = new QHBoxLayout;
std::unordered_map<std::string, QWidget *> widgetPlaceholders = {
{"{{name}}", _name},
};
PlaceWidgets(obs_module_text(
"AdvSceneSwitcher.condition.hotkey.entry.line2"),
switchLayout, widgetPlaceholders);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(line1);
mainLayout->addLayout(switchLayout);
mainLayout->addWidget(hint);
setLayout(mainLayout);
_entryData = entryData;
UpdateEntryData();
_loading = false;
}
void MacroConditionHotkeyEdit::NameChanged()
{
if (_loading || !_entryData) {
return;
}
auto lock = LockContext();
const auto name = _name->text().toStdString();
// In case a hotkey is used by multiple conditions create a new hotkey
// with the new description or get an existing hotkey matching this
// description.
// If the hotkey is only used by this single condition instance try to
// update the description.
// If updating the description runs into a conflict with an existing
// hotkey use its settings instead.
if (_entryData->_hotkey.use_count() > 1 ||
!_entryData->_hotkey->UpdateDescription(name)) {
_entryData->_hotkey = Hotkey::GetHotkey(name);
}
}
void MacroConditionHotkeyEdit::UpdateEntryData()
{
if (!_entryData) {
return;
}
_name->setText(
QString::fromStdString(_entryData->_hotkey->GetDescription()));
}
} // namespace advss