From dea4ff47cd71bf46a0f51fe8435b1c4c03c6ff1a Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Sun, 21 May 2023 00:25:40 +0200 Subject: [PATCH] Add option to prompt user for value to be assigned to variable --- data/locale/en-US.ini | 2 ++ src/advanced-scene-switcher.cpp | 21 ++++++++++++++--- src/macro-core/macro-action-variable.cpp | 29 ++++++++++++++++++++++++ src/macro-core/macro-action-variable.hpp | 1 + src/utils/utility.cpp | 1 + 5 files changed, 51 insertions(+), 3 deletions(-) diff --git a/data/locale/en-US.ini b/data/locale/en-US.ini index 7f67f747..f1fe89ba 100644 --- a/data/locale/en-US.ini +++ b/data/locale/en-US.ini @@ -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" diff --git a/src/advanced-scene-switcher.cpp b/src/advanced-scene-switcher.cpp index de2931c7..f48d59cc 100644 --- a/src/advanced-scene-switcher.cpp +++ b/src/advanced-scene-switcher.cpp @@ -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"); } diff --git a/src/macro-core/macro-action-variable.cpp b/src/macro-core/macro-action-variable.cpp index ecf98640..63d22d52 100644 --- a/src/macro-core/macro-action-variable.cpp +++ b/src/macro-core/macro-action-variable.cpp @@ -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 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(result)); } +struct AskForInputParams { + const std::string varName; + std::optional result; +}; + +static void askForInput(void *param) +{ + auto parameters = static_cast(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, ¶ms, true); + if (!params.result.has_value()) { + return false; + } + var->SetValue(*params.result); + return true; + } } return true; diff --git a/src/macro-core/macro-action-variable.hpp b/src/macro-core/macro-action-variable.hpp index 7acdb32e..7c7e82ef 100644 --- a/src/macro-core/macro-action-variable.hpp +++ b/src/macro-core/macro-action-variable.hpp @@ -35,6 +35,7 @@ public: SUBSTRING, FIND_AND_REPLACE, MATH_EXPRESSION, + USER_INPUT, }; Type _type = Type::SET_FIXED_VALUE; diff --git a/src/utils/utility.cpp b/src/utils/utility.cpp index f7255ad5..e7ef9851 100644 --- a/src/utils/utility.cpp +++ b/src/utils/utility.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include