mirror of
https://github.com/WarmUpTill/SceneSwitcher.git
synced 2026-03-21 17:34:57 -05:00
Add option to truncate or pad variable value
This commit is contained in:
parent
3fe8ea8961
commit
14dd390680
|
|
@ -201,6 +201,8 @@ target_sources(
|
|||
lib/utils/section.hpp
|
||||
lib/utils/selection-helpers.cpp
|
||||
lib/utils/selection-helpers.hpp
|
||||
lib/utils/single-char-selection.cpp
|
||||
lib/utils/single-char-selection.hpp
|
||||
lib/utils/slider-spinbox.cpp
|
||||
lib/utils/slider-spinbox.hpp
|
||||
lib/utils/source-helpers.cpp
|
||||
|
|
|
|||
|
|
@ -916,6 +916,10 @@ AdvSceneSwitcher.action.variable.type.stringLength="Set to length of string"
|
|||
AdvSceneSwitcher.action.variable.type.extractJson="Extract json field with name"
|
||||
AdvSceneSwitcher.action.variable.type.setToTempvar="Set to macro property"
|
||||
AdvSceneSwitcher.action.variable.type.sceneItemName="Set to scene item name at index"
|
||||
AdvSceneSwitcher.action.variable.type.padValue="Pad current value"
|
||||
AdvSceneSwitcher.action.variable.type.truncateValue="Truncate current value"
|
||||
AdvSceneSwitcher.action.variable.truncateOrPadDirection.left="Left"
|
||||
AdvSceneSwitcher.action.variable.truncateOrPadDirection.right="Right"
|
||||
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"
|
||||
|
|
@ -927,7 +931,9 @@ AdvSceneSwitcher.action.variable.invalidSelection="Invalid selection!"
|
|||
AdvSceneSwitcher.action.variable.actionNoVariableSupport="Getting variable values from %1 actions is not supported!"
|
||||
AdvSceneSwitcher.action.variable.conditionNoVariableSupport="Getting variable values from %1 conditions is not supported!"
|
||||
AdvSceneSwitcher.action.variable.currentSegmentValue="Current value:"
|
||||
AdvSceneSwitcher.action.variable.entry="{{actions}}{{variables}}{{variables2}}{{strValue}}{{numValue}}{{segmentIndex}}{{mathExpression}}{{envVariableName}}{{scenes}}{{tempVars}}{{sceneItemIndex}}"
|
||||
AdvSceneSwitcher.action.variable.entry.other="{{actions}}{{variables}}{{variables2}}{{strValue}}{{numValue}}{{segmentIndex}}{{mathExpression}}{{envVariableName}}{{scenes}}{{tempVars}}{{sceneItemIndex}}{{direction}}{{stringLength}}{{paddingCharSelection}}"
|
||||
AdvSceneSwitcher.action.variable.entry.pad="{{actions}}of{{variables}}to length{{stringLength}}by adding{{paddingCharSelection}}to the{{direction}}"
|
||||
AdvSceneSwitcher.action.variable.entry.truncate="{{actions}}of{{variables}}to length{{stringLength}}by removing characters from the{{direction}}"
|
||||
AdvSceneSwitcher.action.variable.entry.substringIndex="Substring start:{{subStringStart}}Substring size:{{subStringSize}}"
|
||||
AdvSceneSwitcher.action.variable.entry.substringRegex="Assign value of{{regexMatchIdx}}match using regular expression:"
|
||||
AdvSceneSwitcher.action.variable.entry.findAndReplace="{{findStr}}{{findRegex}}{{replaceStr}}"
|
||||
|
|
|
|||
|
|
@ -54,6 +54,10 @@ const static std::map<MacroActionVariable::Type, std::string> actionTypes = {
|
|||
"AdvSceneSwitcher.action.variable.type.setToTempvar"},
|
||||
{MacroActionVariable::Type::SCENE_ITEM_NAME,
|
||||
"AdvSceneSwitcher.action.variable.type.sceneItemName"},
|
||||
{MacroActionVariable::Type::PAD,
|
||||
"AdvSceneSwitcher.action.variable.type.padValue"},
|
||||
{MacroActionVariable::Type::TRUNCATE,
|
||||
"AdvSceneSwitcher.action.variable.type.truncateValue"},
|
||||
};
|
||||
|
||||
static void apppend(Variable &var, const std::string &value)
|
||||
|
|
@ -217,6 +221,38 @@ static void askForInput(void *param)
|
|||
parameters->resultReady = true;
|
||||
}
|
||||
|
||||
static std::string truncate(const std::string &input,
|
||||
MacroActionVariable::Direction direction,
|
||||
size_t length)
|
||||
{
|
||||
if (input.length() <= length) {
|
||||
return input;
|
||||
}
|
||||
|
||||
if (direction == MacroActionVariable::Direction::RIGHT) {
|
||||
return input.substr(0, length);
|
||||
} else {
|
||||
return input.substr(input.length() - length);
|
||||
}
|
||||
}
|
||||
|
||||
static std::string pad(const std::string &input,
|
||||
MacroActionVariable::Direction direction, size_t length,
|
||||
char paddingChar)
|
||||
{
|
||||
if (input.length() >= length) {
|
||||
return input;
|
||||
}
|
||||
|
||||
if (direction == MacroActionVariable::Direction::RIGHT) {
|
||||
return input +
|
||||
std::string(length - input.length(), paddingChar);
|
||||
} else {
|
||||
return std::string(length - input.length(), paddingChar) +
|
||||
input;
|
||||
}
|
||||
}
|
||||
|
||||
bool MacroActionVariable::PerformAction()
|
||||
{
|
||||
auto var = _variable.lock();
|
||||
|
|
@ -348,6 +384,14 @@ bool MacroActionVariable::PerformAction()
|
|||
case Type::SCENE_ITEM_NAME:
|
||||
SetToSceneItemName(var.get());
|
||||
return true;
|
||||
case Type::PAD:
|
||||
var->SetValue(pad(var->Value(), _direction, _stringLength,
|
||||
_paddingChar));
|
||||
return true;
|
||||
case Type::TRUNCATE:
|
||||
var->SetValue(
|
||||
truncate(var->Value(), _direction, _stringLength));
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
@ -381,6 +425,9 @@ bool MacroActionVariable::Save(obs_data_t *obj) const
|
|||
_scene.Save(obj);
|
||||
_tempVar.Save(obj);
|
||||
_sceneItemIndex.Save(obj, "sceneItemIndex");
|
||||
obs_data_set_int(obj, "direction", static_cast<int>(_direction));
|
||||
_stringLength.Save(obj, "stringLength");
|
||||
obs_data_set_int(obj, "paddingChar", _paddingChar);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -412,6 +459,13 @@ bool MacroActionVariable::Load(obs_data_t *obj)
|
|||
_scene.Load(obj);
|
||||
_tempVar.Load(obj, GetMacro());
|
||||
_sceneItemIndex.Load(obj, "sceneItemIndex");
|
||||
_direction = static_cast<Direction>(obs_data_get_int(obj, "direction"));
|
||||
_stringLength.Load(obj, "stringLength");
|
||||
if (obs_data_has_user_value(obj, "paddingChar")) {
|
||||
_paddingChar = obs_data_get_int(obj, "paddingChar");
|
||||
} else {
|
||||
_paddingChar = ' ';
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -530,6 +584,16 @@ static inline void populateTypeSelection(QComboBox *list)
|
|||
}
|
||||
}
|
||||
|
||||
static inline void populateDirectionSelection(QComboBox *list)
|
||||
{
|
||||
list->addItems(
|
||||
QStringList()
|
||||
<< obs_module_text(
|
||||
"AdvSceneSwitcher.action.variable.truncateOrPadDirection.left")
|
||||
<< obs_module_text(
|
||||
"AdvSceneSwitcher.action.variable.truncateOrPadDirection.right"));
|
||||
}
|
||||
|
||||
MacroActionVariableEdit::MacroActionVariableEdit(
|
||||
QWidget *parent, std::shared_ptr<MacroActionVariable> entryData)
|
||||
: QWidget(parent),
|
||||
|
|
@ -567,6 +631,9 @@ MacroActionVariableEdit::MacroActionVariableEdit(
|
|||
true)),
|
||||
_tempVars(new TempVariableSelection(this)),
|
||||
_sceneItemIndex(new VariableSpinBox()),
|
||||
_direction(new QComboBox()),
|
||||
_stringLength(new VariableSpinBox()),
|
||||
_paddingCharSelection(new SingleCharSelection()),
|
||||
_entryLayout(new QHBoxLayout())
|
||||
{
|
||||
_numValue->setMinimum(-9999999999);
|
||||
|
|
@ -586,7 +653,9 @@ MacroActionVariableEdit::MacroActionVariableEdit(
|
|||
_inputPrompt->setSizePolicy(QSizePolicy::MinimumExpanding,
|
||||
QSizePolicy::Preferred);
|
||||
_sceneItemIndex->setMinimum(1);
|
||||
_stringLength->setMaximum(999);
|
||||
populateTypeSelection(_actions);
|
||||
populateDirectionSelection(_direction);
|
||||
|
||||
QWidget::connect(_variables, SIGNAL(SelectionChanged(const QString &)),
|
||||
this, SLOT(VariableChanged(const QString &)));
|
||||
|
|
@ -642,8 +711,17 @@ MacroActionVariableEdit::MacroActionVariableEdit(
|
|||
_sceneItemIndex,
|
||||
SIGNAL(NumberVariableChanged(const NumberVariable<int> &)),
|
||||
this, SLOT(SceneItemIndexChanged(const NumberVariable<int> &)));
|
||||
QWidget::connect(_direction, SIGNAL(currentIndexChanged(int)), this,
|
||||
SLOT(DirectionChanged(int)));
|
||||
QWidget::connect(
|
||||
_stringLength,
|
||||
SIGNAL(NumberVariableChanged(const NumberVariable<int> &)),
|
||||
this, SLOT(StringLengthChanged(const NumberVariable<int> &)));
|
||||
QWidget::connect(_paddingCharSelection,
|
||||
SIGNAL(CharChanged(const QString &)), this,
|
||||
SLOT(CharSelectionChanged(const QString &)));
|
||||
|
||||
std::unordered_map<std::string, QWidget *> widgetPlaceholders = {
|
||||
const std::unordered_map<std::string, QWidget *> widgetPlaceholders = {
|
||||
{"{{variables}}", _variables},
|
||||
{"{{variables2}}", _variables2},
|
||||
{"{{actions}}", _actions},
|
||||
|
|
@ -665,9 +743,13 @@ MacroActionVariableEdit::MacroActionVariableEdit(
|
|||
{"{{scenes}}", _scenes},
|
||||
{"{{tempVars}}", _tempVars},
|
||||
{"{{sceneItemIndex}}", _sceneItemIndex},
|
||||
{"{{direction}}", _direction},
|
||||
{"{{stringLength}}", _stringLength},
|
||||
{"{{paddingCharSelection}}", _paddingCharSelection},
|
||||
};
|
||||
PlaceWidgets(obs_module_text("AdvSceneSwitcher.action.variable.entry"),
|
||||
_entryLayout, widgetPlaceholders);
|
||||
PlaceWidgets(
|
||||
obs_module_text("AdvSceneSwitcher.action.variable.entry.other"),
|
||||
_entryLayout, widgetPlaceholders);
|
||||
|
||||
PlaceWidgets(
|
||||
obs_module_text(
|
||||
|
|
@ -760,6 +842,10 @@ void MacroActionVariableEdit::UpdateEntryData()
|
|||
_scenes->SetScene(_entryData->_scene);
|
||||
_tempVars->SetVariable(_entryData->_tempVar);
|
||||
_sceneItemIndex->SetValue(_entryData->_sceneItemIndex);
|
||||
_direction->setCurrentIndex(static_cast<int>(_entryData->_direction));
|
||||
_stringLength->SetValue(_entryData->_stringLength);
|
||||
_paddingCharSelection->setText(
|
||||
QChar::fromLatin1(_entryData->_paddingChar));
|
||||
SetWidgetVisibility();
|
||||
}
|
||||
|
||||
|
|
@ -1129,12 +1215,84 @@ void MacroActionVariableEdit::SceneItemIndexChanged(
|
|||
_entryData->_sceneItemIndex = value;
|
||||
}
|
||||
|
||||
void MacroActionVariableEdit::DirectionChanged(int value)
|
||||
{
|
||||
if (_loading || !_entryData) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto lock = LockContext();
|
||||
_entryData->_direction =
|
||||
static_cast<MacroActionVariable::Direction>(value);
|
||||
}
|
||||
|
||||
void MacroActionVariableEdit::StringLengthChanged(
|
||||
const NumberVariable<int> &value)
|
||||
{
|
||||
if (_loading || !_entryData) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto lock = LockContext();
|
||||
_entryData->_stringLength = value;
|
||||
}
|
||||
|
||||
void MacroActionVariableEdit::CharSelectionChanged(const QString &character)
|
||||
{
|
||||
if (_loading || !_entryData) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto lock = LockContext();
|
||||
if (character.isEmpty()) {
|
||||
_entryData->_paddingChar = ' ';
|
||||
} else {
|
||||
_entryData->_paddingChar = character.toStdString().at(0);
|
||||
}
|
||||
}
|
||||
|
||||
void MacroActionVariableEdit::SetWidgetVisibility()
|
||||
{
|
||||
if (!_entryData) {
|
||||
return;
|
||||
}
|
||||
|
||||
const std::unordered_map<std::string, QWidget *> widgetPlaceholders = {
|
||||
{"{{variables}}", _variables},
|
||||
{"{{variables2}}", _variables2},
|
||||
{"{{actions}}", _actions},
|
||||
{"{{strValue}}", _strValue},
|
||||
{"{{numValue}}", _numValue},
|
||||
{"{{segmentIndex}}", _segmentIdx},
|
||||
{"{{mathExpression}}", _mathExpression},
|
||||
{"{{envVariableName}}", _envVariable},
|
||||
{"{{scenes}}", _scenes},
|
||||
{"{{tempVars}}", _tempVars},
|
||||
{"{{sceneItemIndex}}", _sceneItemIndex},
|
||||
{"{{direction}}", _direction},
|
||||
{"{{stringLength}}", _stringLength},
|
||||
{"{{paddingCharSelection}}", _paddingCharSelection},
|
||||
};
|
||||
|
||||
const char *layoutString = "";
|
||||
if (_entryData->_type == MacroActionVariable::Type::PAD) {
|
||||
layoutString = obs_module_text(
|
||||
"AdvSceneSwitcher.action.variable.entry.pad");
|
||||
} else if (_entryData->_type == MacroActionVariable::Type::TRUNCATE) {
|
||||
layoutString = obs_module_text(
|
||||
"AdvSceneSwitcher.action.variable.entry.truncate");
|
||||
} else {
|
||||
layoutString = obs_module_text(
|
||||
"AdvSceneSwitcher.action.variable.entry.other");
|
||||
}
|
||||
|
||||
for (const auto &[_, widget] : widgetPlaceholders) {
|
||||
_entryLayout->removeWidget(widget);
|
||||
}
|
||||
|
||||
ClearLayout(_entryLayout);
|
||||
PlaceWidgets(layoutString, _entryLayout, widgetPlaceholders);
|
||||
|
||||
if (_entryData->_type == MacroActionVariable::Type::SET_FIXED_VALUE ||
|
||||
_entryData->_type == MacroActionVariable::Type::APPEND ||
|
||||
_entryData->_type == MacroActionVariable::Type::MATH_EXPRESSION ||
|
||||
|
|
@ -1225,6 +1383,14 @@ void MacroActionVariableEdit::SetWidgetVisibility()
|
|||
MacroActionVariable::Type::SET_TO_TEMPVAR);
|
||||
_sceneItemIndex->setVisible(_entryData->_type ==
|
||||
MacroActionVariable::Type::SCENE_ITEM_NAME);
|
||||
_direction->setVisible(
|
||||
_entryData->_type == MacroActionVariable::Type::PAD ||
|
||||
_entryData->_type == MacroActionVariable::Type::TRUNCATE);
|
||||
_stringLength->setVisible(
|
||||
_entryData->_type == MacroActionVariable::Type::PAD ||
|
||||
_entryData->_type == MacroActionVariable::Type::TRUNCATE);
|
||||
_paddingCharSelection->setVisible(_entryData->_type ==
|
||||
MacroActionVariable::Type::PAD);
|
||||
adjustSize();
|
||||
updateGeometry();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include "regex-config.hpp"
|
||||
#include "resizing-text-edit.hpp"
|
||||
#include "scene-selection.hpp"
|
||||
#include "single-char-selection.hpp"
|
||||
#include "variable-line-edit.hpp"
|
||||
#include "variable-text-edit.hpp"
|
||||
#include "variable-spinbox.hpp"
|
||||
|
|
@ -45,6 +46,8 @@ public:
|
|||
EXTRACT_JSON,
|
||||
SET_TO_TEMPVAR,
|
||||
SCENE_ITEM_NAME,
|
||||
PAD,
|
||||
TRUNCATE,
|
||||
};
|
||||
|
||||
Type _type = Type::SET_FIXED_VALUE;
|
||||
|
|
@ -79,6 +82,11 @@ public:
|
|||
TempVariableRef _tempVar;
|
||||
IntVariable _sceneItemIndex = 1;
|
||||
|
||||
enum class Direction { LEFT, RIGHT };
|
||||
Direction _direction = Direction::LEFT;
|
||||
IntVariable _stringLength = 1;
|
||||
char _paddingChar = '0';
|
||||
|
||||
private:
|
||||
void DecrementCurrentSegmentVariableRef();
|
||||
void HandleIndexSubString(Variable *);
|
||||
|
|
@ -135,6 +143,9 @@ private slots:
|
|||
void SceneChanged(const SceneSelection &);
|
||||
void SelectionChanged(const TempVariableRef &var);
|
||||
void SceneItemIndexChanged(const NumberVariable<int> &);
|
||||
void DirectionChanged(int);
|
||||
void StringLengthChanged(const NumberVariable<int> &);
|
||||
void CharSelectionChanged(const QString &);
|
||||
|
||||
signals:
|
||||
void HeaderInfoChanged(const QString &);
|
||||
|
|
@ -175,6 +186,9 @@ private:
|
|||
SceneSelectionWidget *_scenes;
|
||||
TempVariableSelection *_tempVars;
|
||||
VariableSpinBox *_sceneItemIndex;
|
||||
QComboBox *_direction;
|
||||
VariableSpinBox *_stringLength;
|
||||
SingleCharSelection *_paddingCharSelection;
|
||||
QHBoxLayout *_entryLayout;
|
||||
|
||||
std::shared_ptr<MacroActionVariable> _entryData;
|
||||
|
|
|
|||
22
lib/utils/single-char-selection.cpp
Normal file
22
lib/utils/single-char-selection.cpp
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#include "single-char-selection.hpp"
|
||||
|
||||
namespace advss {
|
||||
|
||||
SingleCharSelection::SingleCharSelection(QWidget *parent) : QLineEdit(parent)
|
||||
{
|
||||
setMaxLength(1);
|
||||
setMaximumWidth(50);
|
||||
connect(this, &QLineEdit::textChanged, this,
|
||||
&SingleCharSelection::CharChanged);
|
||||
}
|
||||
|
||||
void SingleCharSelection::HandleTextChanged(const QString &text)
|
||||
{
|
||||
if (text.length() == 1) {
|
||||
emit CharChanged(text);
|
||||
} else if (text.length() > 1) {
|
||||
setText(text.left(1));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace advss
|
||||
18
lib/utils/single-char-selection.hpp
Normal file
18
lib/utils/single-char-selection.hpp
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#pragma once
|
||||
#include <QLineEdit>
|
||||
|
||||
namespace advss {
|
||||
|
||||
class SingleCharSelection : public QLineEdit {
|
||||
Q_OBJECT
|
||||
public:
|
||||
SingleCharSelection(QWidget *parent = nullptr);
|
||||
|
||||
signals:
|
||||
void CharChanged(const QString &character);
|
||||
|
||||
private slots:
|
||||
void HandleTextChanged(const QString &text);
|
||||
};
|
||||
|
||||
} // namespace advss
|
||||
Loading…
Reference in New Issue
Block a user