Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to set variable value to length of string #896

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data/locale/en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ AdvSceneSwitcher.action.variable.type.mathExpression="Mathematical expression"
AdvSceneSwitcher.action.variable.type.askForValue="Get user input"
AdvSceneSwitcher.action.variable.type.environmentVariable="Set to environment variable value"
AdvSceneSwitcher.action.variable.type.sceneItemCount="Set to scene item count of scene"
AdvSceneSwitcher.action.variable.type.stringLength="Set to length of string"
AdvSceneSwitcher.action.variable.askForValuePromptDefault="Assign value to variable \"%1\":"
AdvSceneSwitcher.action.variable.askForValuePrompt="Assign value to variable:"
AdvSceneSwitcher.action.variable.mathExpression.example="( 1 + 2 * 3 ) / 4"
Expand Down
30 changes: 20 additions & 10 deletions src/macro-core/macro-action-variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const static std::map<MacroActionVariable::Type, std::string> actionTypes = {
"AdvSceneSwitcher.action.variable.type.environmentVariable"},
{MacroActionVariable::Type::SCENE_ITEM_COUNT,
"AdvSceneSwitcher.action.variable.type.sceneItemCount"},
{MacroActionVariable::Type::STRING_LENGTH,
"AdvSceneSwitcher.action.variable.type.stringLength"},
};

static void apppend(Variable &var, const std::string &value)
Expand Down Expand Up @@ -238,6 +240,10 @@ bool MacroActionVariable::PerformAction()
var->SetValue(GetSceneItemCount(_scene.GetScene(false)));
return true;
}
case Type::STRING_LENGTH: {
var->SetValue(std::string(_strValue).length());
return true;
}
}

return true;
Expand All @@ -250,7 +256,7 @@ bool MacroActionVariable::Save(obs_data_t *obj) const
GetWeakVariableName(_variable).c_str());
obs_data_set_string(obj, "variable2Name",
GetWeakVariableName(_variable2).c_str());
obs_data_set_string(obj, "strValue", _strValue.c_str());
_strValue.Save(obj, "strValue");
obs_data_set_double(obj, "numValue", _numValue);
obs_data_set_int(obj, "condition", static_cast<int>(_type));
obs_data_set_int(obj, "segmentIdx", GetSegmentIndexValue());
Expand Down Expand Up @@ -278,7 +284,7 @@ bool MacroActionVariable::Load(obs_data_t *obj)
GetWeakVariableByName(obs_data_get_string(obj, "variableName"));
_variable2 = GetWeakVariableByName(
obs_data_get_string(obj, "variable2Name"));
_strValue = obs_data_get_string(obj, "strValue");
_strValue.Load(obj, "strValue");
_numValue = obs_data_get_double(obj, "numValue");
_type = static_cast<Type>(obs_data_get_int(obj, "condition"));
_segmentIdxLoadValue = obs_data_get_int(obj, "segmentIdx");
Expand Down Expand Up @@ -385,8 +391,9 @@ void MacroActionVariable::DecrementCurrentSegmentVariableRef()

static inline void populateTypeSelection(QComboBox *list)
{
for (auto entry : actionTypes) {
list->addItem(obs_module_text(entry.second.c_str()));
for (const auto &[action, name] : actionTypes) {
list->addItem(obs_module_text(name.c_str()),
static_cast<int>(action));
}
}

Expand All @@ -396,7 +403,7 @@ MacroActionVariableEdit::MacroActionVariableEdit(
_variables(new VariableSelection(this)),
_variables2(new VariableSelection(this)),
_actions(new QComboBox()),
_strValue(new ResizingPlainTextEdit(this, 5, 1, 1)),
_strValue(new VariableTextEdit(this, 5, 1, 1)),
_numValue(new QDoubleSpinBox()),
_segmentIdx(new MacroSegmentSelection(
this, MacroSegmentSelection::Type::CONDITION, false)),
Expand Down Expand Up @@ -572,8 +579,9 @@ void MacroActionVariableEdit::UpdateEntryData()

_variables->SetVariable(_entryData->_variable);
_variables2->SetVariable(_entryData->_variable2);
_actions->setCurrentIndex(static_cast<int>(_entryData->_type));
_strValue->setPlainText(QString::fromStdString(_entryData->_strValue));
_actions->setCurrentIndex(
_actions->findData(static_cast<int>(_entryData->_type)));
_strValue->setPlainText(_entryData->_strValue);
_numValue->setValue(_entryData->_numValue);
_segmentIdx->SetValue(_entryData->GetSegmentIndexValue() + 1);
_segmentIdx->SetMacro(_entryData->GetMacro());
Expand Down Expand Up @@ -621,14 +629,15 @@ void MacroActionVariableEdit::Variable2Changed(const QString &text)
_entryData->_variable2 = GetWeakVariableByQString(text);
}

void MacroActionVariableEdit::ActionChanged(int value)
void MacroActionVariableEdit::ActionChanged(int idx)
{
if (_loading || !_entryData) {
return;
}

auto lock = LockContext();
_entryData->_type = static_cast<MacroActionVariable::Type>(value);
_entryData->_type = static_cast<MacroActionVariable::Type>(
_actions->itemData(idx).toInt());

if (_entryData->_type == MacroActionVariable::Type::SET_ACTION_VALUE) {
_segmentIdx->SetType(MacroSegmentSelection::Type::ACTION);
Expand Down Expand Up @@ -942,7 +951,8 @@ void MacroActionVariableEdit::SetWidgetVisibility()
_strValue->setVisible(
_entryData->_type ==
MacroActionVariable::Type::SET_FIXED_VALUE ||
_entryData->_type == MacroActionVariable::Type::APPEND);
_entryData->_type == MacroActionVariable::Type::APPEND ||
_entryData->_type == MacroActionVariable::Type::STRING_LENGTH);
_numValue->setVisible(
_entryData->_type == MacroActionVariable::Type::INCREMENT ||
_entryData->_type == MacroActionVariable::Type::DECREMENT);
Expand Down
6 changes: 4 additions & 2 deletions src/macro-core/macro-action-variable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "resizing-text-edit.hpp"
#include "scene-selection.hpp"
#include "variable-line-edit.hpp"
#include "variable-text-edit.hpp"

namespace advss {

Expand Down Expand Up @@ -40,12 +41,13 @@ class MacroActionVariable : public MacroAction {
USER_INPUT,
ENV_VARIABLE,
SCENE_ITEM_COUNT,
STRING_LENGTH,
};

Type _type = Type::SET_FIXED_VALUE;
std::weak_ptr<Variable> _variable;
std::weak_ptr<Variable> _variable2;
std::string _strValue = "";
StringVariable _strValue = "";
double _numValue = 0;
int _subStringStart = 0;
int _subStringSize = 0;
Expand Down Expand Up @@ -134,7 +136,7 @@ private slots:
VariableSelection *_variables;
VariableSelection *_variables2;
QComboBox *_actions;
ResizingPlainTextEdit *_strValue;
VariableTextEdit *_strValue;
QDoubleSpinBox *_numValue;
MacroSegmentSelection *_segmentIdx;
QLabel *_segmentValueStatus;
Expand Down
5 changes: 3 additions & 2 deletions src/utils/variable-text-edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

namespace advss {

VariableTextEdit::VariableTextEdit(QWidget *parent)
: ResizingPlainTextEdit(parent)
VariableTextEdit::VariableTextEdit(QWidget *parent, const int scrollAt,
const int minLines, const int paddingLines)
: ResizingPlainTextEdit(parent, scrollAt, minLines, paddingLines)
{
QPlainTextEdit::setToolTip(
obs_module_text("AdvSceneSwitcher.tooltip.availableVariables"));
Expand Down
3 changes: 2 additions & 1 deletion src/utils/variable-text-edit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ namespace advss {
class VariableTextEdit : public ResizingPlainTextEdit {
Q_OBJECT
public:
VariableTextEdit(QWidget *parent);
VariableTextEdit(QWidget *parent, const int scrollAt = 10,
const int minLines = 3, const int paddingLines = 2);
void setPlainText(const QString &);
void setPlainText(const StringVariable &);
void setToolTip(const QString &string);
Expand Down
Loading