Add option to prompt user for value to be assigned to variable

This commit is contained in:
WarmUpTill 2023-05-21 00:25:40 +02:00 committed by WarmUpTill
parent 258a5f6ab7
commit dea4ff47cd
5 changed files with 51 additions and 3 deletions

View File

@ -651,6 +651,8 @@ AdvSceneSwitcher.action.variable.type.roundToInt="Round to nearest integer"
AdvSceneSwitcher.action.variable.type.subString="Set to substring of current value"
AdvSceneSwitcher.action.variable.type.findAndReplace="Find and replace in current value"
AdvSceneSwitcher.action.variable.type.mathExpression="Mathematical expression"
AdvSceneSwitcher.action.variable.type.askForValue="Get user input"
AdvSceneSwitcher.action.variable.askForValuePrompt="Assign value to variable \"%1\":"
AdvSceneSwitcher.action.variable.mathExpression.example="( 1 + 2 * 3 ) / 4"
AdvSceneSwitcher.action.variable.findAndReplace.find="Text to find"
AdvSceneSwitcher.action.variable.findAndReplace.replace="Text to replace with"

View File

@ -439,6 +439,8 @@ void SwitcherData::Start()
}
}
bool CloseAllInputDialogs();
void SwitcherData::Stop()
{
if (th && th->isRunning()) {
@ -447,10 +449,23 @@ void SwitcherData::Stop()
abortMacroWait = true;
macroWaitCv.notify_all();
macroTransitionCv.notify_all();
th->wait();
delete th;
th = nullptr;
// Not waiting if a dialog was closed is a workaround to avoid
// deadlocks when a variable input dialog is opened while Stop()
// is called.
//
// The QEventLoop handling the dialog would not exit until Stop()
// would return, which itself would wait for the switcher thread
// to complete, which itself is blocked by the variable input
// dialog.
//
// Must be reworked in the future, as this will not go well when
// OBS is shutting down and a dialog is still opened.
if (!CloseAllInputDialogs()) {
th->wait();
delete th;
th = nullptr;
}
writeToStatusFile("Advanced Scene Switcher stopped");
}

View File

@ -4,6 +4,7 @@
#include "advanced-scene-switcher.hpp"
#include "macro.hpp"
#include "math-helpers.hpp"
#include "non-modal-dialog.hpp"
#include "utility.hpp"
namespace advss {
@ -38,6 +39,8 @@ static std::map<MacroActionVariable::Type, std::string> actionTypes = {
"AdvSceneSwitcher.action.variable.type.findAndReplace"},
{MacroActionVariable::Type::MATH_EXPRESSION,
"AdvSceneSwitcher.action.variable.type.mathExpression"},
{MacroActionVariable::Type::USER_INPUT,
"AdvSceneSwitcher.action.variable.type.askForValue"},
};
static void apppend(Variable &var, const std::string &value)
@ -121,6 +124,23 @@ void MacroActionVariable::HandleMathExpression(Variable *var)
var->SetValue(std::get<double>(result));
}
struct AskForInputParams {
const std::string varName;
std::optional<std::string> result;
};
static void askForInput(void *param)
{
auto parameters = static_cast<AskForInputParams *>(param);
QString prompt(obs_module_text(
"AdvSceneSwitcher.action.variable.askForValuePrompt"));
auto dialog = new NonModalMessageDialog(
prompt.arg(QString::fromStdString(parameters->varName)),
NonModalMessageDialog::Type::INPUT);
parameters->result = dialog->GetInput();
}
bool MacroActionVariable::PerformAction()
{
auto var = _variable.lock();
@ -189,6 +209,15 @@ bool MacroActionVariable::PerformAction()
HandleMathExpression(var.get());
return true;
}
case Type::USER_INPUT: {
AskForInputParams params{var->Name(), {}};
obs_queue_task(OBS_TASK_UI, askForInput, &params, true);
if (!params.result.has_value()) {
return false;
}
var->SetValue(*params.result);
return true;
}
}
return true;

View File

@ -35,6 +35,7 @@ public:
SUBSTRING,
FIND_AND_REPLACE,
MATH_EXPRESSION,
USER_INPUT,
};
Type _type = Type::SET_FIXED_VALUE;

View File

@ -21,6 +21,7 @@
#include <QSystemTrayIcon>
#include <QGuiApplication>
#include <QCursor>
#include <QMainWindow>
#include <QScreen>
#include <unordered_map>
#include <regex>