ControlReference: don't reparse expression when references are updated

This commit is contained in:
Michael M 2017-06-07 19:02:16 -07:00
parent c332580b83
commit 31f1c06226
8 changed files with 41 additions and 25 deletions

View File

@ -344,7 +344,7 @@ void HotkeyManager::LoadDefaults(const ControllerInterface& ciface)
auto set_key_expression = [this](int index, const std::string& expression) { auto set_key_expression = [this](int index, const std::string& expression) {
m_keys[FindGroupByID(index)] m_keys[FindGroupByID(index)]
->controls[GetIndexForGroup(FindGroupByID(index), index)] ->controls[GetIndexForGroup(FindGroupByID(index), index)]
->control_ref->expression = expression; ->control_ref->SetExpression(expression);
}; };
// General hotkeys // General hotkeys

View File

@ -110,7 +110,7 @@ void IOWindow::CreateMainLayout()
void IOWindow::Update() void IOWindow::Update()
{ {
m_expression_text->setPlainText(QString::fromStdString(m_reference->expression)); m_expression_text->setPlainText(QString::fromStdString(m_reference->GetExpression()));
m_range_spinbox->setValue(m_reference->range * SLIDER_TICK_COUNT); m_range_spinbox->setValue(m_reference->range * SLIDER_TICK_COUNT);
m_range_slider->setValue(m_reference->range * SLIDER_TICK_COUNT); m_range_slider->setValue(m_reference->range * SLIDER_TICK_COUNT);
@ -164,7 +164,7 @@ void IOWindow::OnDialogButtonPressed(QAbstractButton* button)
return; return;
} }
m_reference->expression = m_expression_text->toPlainText().toStdString(); m_reference->SetExpression(m_expression_text->toPlainText().toStdString());
if (button != m_apply_button) if (button != m_apply_button)
accept(); accept();

View File

@ -27,7 +27,7 @@ static QString EscapeAmpersand(QString&& string)
} }
MappingButton::MappingButton(MappingWidget* widget, ControlReference* ref) MappingButton::MappingButton(MappingWidget* widget, ControlReference* ref)
: ElidedButton(EscapeAmpersand(QString::fromStdString(ref->expression))), m_parent(widget), : ElidedButton(EscapeAmpersand(QString::fromStdString(ref->GetExpression()))), m_parent(widget),
m_reference(ref) m_reference(ref)
{ {
Connect(); Connect();
@ -66,7 +66,7 @@ void MappingButton::OnButtonPressed()
if (!expr.isEmpty()) if (!expr.isEmpty())
{ {
m_reference->expression = expr.toStdString(); m_reference->SetExpression(expr.toStdString());
Update(); Update();
} }
else else
@ -78,13 +78,13 @@ void MappingButton::OnButtonPressed()
void MappingButton::OnButtonTimeout() void MappingButton::OnButtonTimeout()
{ {
setText(EscapeAmpersand(QString::fromStdString(m_reference->expression))); setText(EscapeAmpersand(QString::fromStdString(m_reference->GetExpression())));
} }
void MappingButton::Clear() void MappingButton::Clear()
{ {
m_parent->Update(); m_parent->Update();
m_reference->expression.clear(); m_reference->SetExpression("");
Update(); Update();
} }
@ -92,7 +92,7 @@ void MappingButton::Update()
{ {
const auto lock = ControllerEmu::EmulatedController::GetStateLock(); const auto lock = ControllerEmu::EmulatedController::GetStateLock();
m_reference->UpdateReference(g_controller_interface, m_parent->GetParent()->GetDeviceQualifier()); m_reference->UpdateReference(g_controller_interface, m_parent->GetParent()->GetDeviceQualifier());
setText(EscapeAmpersand(QString::fromStdString(m_reference->expression))); setText(EscapeAmpersand(QString::fromStdString(m_reference->GetExpression())));
m_parent->SaveSettings(); m_parent->SaveSettings();
} }

View File

@ -243,7 +243,7 @@ ControlButton::ControlButton(wxWindow* const parent, ControlReference* const _re
m_configured_width(FromDIP(width)) m_configured_width(FromDIP(width))
{ {
if (label.empty()) if (label.empty())
SetLabelText(StrToWxStr(_ref->expression)); SetLabelText(StrToWxStr(_ref->GetExpression()));
else else
SetLabel(StrToWxStr(label)); SetLabel(StrToWxStr(label));
} }
@ -336,7 +336,7 @@ void ControlDialog::SelectControl(const std::string& name)
void ControlDialog::UpdateGUI() void ControlDialog::UpdateGUI()
{ {
// update textbox // update textbox
textctrl->SetValue(StrToWxStr(control_reference->expression)); textctrl->SetValue(StrToWxStr(control_reference->GetExpression()));
// updates the "bound controls:" label // updates the "bound controls:" label
m_bound_label->SetLabel( m_bound_label->SetLabel(
@ -365,7 +365,7 @@ void InputConfigDialog::UpdateGUI()
{ {
for (ControlButton* button : cgBox->control_buttons) for (ControlButton* button : cgBox->control_buttons)
{ {
button->SetLabelText(StrToWxStr(button->control_reference->expression)); button->SetLabelText(StrToWxStr(button->control_reference->GetExpression()));
} }
for (PadSetting* padSetting : cgBox->options) for (PadSetting* padSetting : cgBox->options)
@ -400,7 +400,7 @@ void InputConfigDialog::LoadDefaults(wxCommandEvent&)
bool ControlDialog::Validate() bool ControlDialog::Validate()
{ {
control_reference->expression = WxStrToStr(textctrl->GetValue()); control_reference->SetExpression(WxStrToStr(textctrl->GetValue()));
const auto lock = ControllerEmu::EmulatedController::GetStateLock(); const auto lock = ControllerEmu::EmulatedController::GetStateLock();
control_reference->UpdateReference(g_controller_interface, control_reference->UpdateReference(g_controller_interface,
@ -439,7 +439,7 @@ void ControlDialog::SetDevice(wxCommandEvent&)
void ControlDialog::ClearControl(wxCommandEvent&) void ControlDialog::ClearControl(wxCommandEvent&)
{ {
control_reference->expression.clear(); control_reference->SetExpression("");
const auto lock = ControllerEmu::EmulatedController::GetStateLock(); const auto lock = ControllerEmu::EmulatedController::GetStateLock();
control_reference->UpdateReference(g_controller_interface, control_reference->UpdateReference(g_controller_interface,
@ -498,7 +498,7 @@ void ControlDialog::SetSelectedControl(wxCommandEvent&)
return; return;
textctrl->WriteText(expr); textctrl->WriteText(expr);
control_reference->expression = textctrl->GetValue(); control_reference->SetExpression(WxStrToStr(textctrl->GetValue()));
const auto lock = ControllerEmu::EmulatedController::GetStateLock(); const auto lock = ControllerEmu::EmulatedController::GetStateLock();
control_reference->UpdateReference(g_controller_interface, control_reference->UpdateReference(g_controller_interface,
@ -534,7 +534,7 @@ void ControlDialog::AppendControl(wxCommandEvent& event)
} }
textctrl->WriteText(expr); textctrl->WriteText(expr);
control_reference->expression = textctrl->GetValue(); control_reference->SetExpression(WxStrToStr(textctrl->GetValue()));
const auto lock = ControllerEmu::EmulatedController::GetStateLock(); const auto lock = ControllerEmu::EmulatedController::GetStateLock();
control_reference->UpdateReference(g_controller_interface, control_reference->UpdateReference(g_controller_interface,
@ -638,7 +638,7 @@ void InputConfigDialog::ConfigControl(wxEvent& event)
void InputConfigDialog::ClearControl(wxEvent& event) void InputConfigDialog::ClearControl(wxEvent& event)
{ {
ControlButton* const btn = (ControlButton*)event.GetEventObject(); ControlButton* const btn = (ControlButton*)event.GetEventObject();
btn->control_reference->expression.clear(); btn->control_reference->SetExpression("");
btn->control_reference->range = 1.0; btn->control_reference->range = 1.0;
controller->UpdateReferences(g_controller_interface); controller->UpdateReferences(g_controller_interface);
@ -717,7 +717,7 @@ bool InputConfigDialog::DetectButton(ControlButton* button)
wxString control_name = ctrl->GetName(); wxString control_name = ctrl->GetName();
wxString expr; wxString expr;
GetExpressionForControl(expr, control_name); GetExpressionForControl(expr, control_name);
button->control_reference->expression = expr; button->control_reference->SetExpression(WxStrToStr(expr));
const auto lock = ControllerEmu::EmulatedController::GetStateLock(); const auto lock = ControllerEmu::EmulatedController::GetStateLock();
button->control_reference->UpdateReference(g_controller_interface, button->control_reference->UpdateReference(g_controller_interface,
controller->default_device); controller->default_device);

View File

@ -25,13 +25,12 @@ bool ControlReference::InputGateOn()
// UpdateReference // UpdateReference
// //
// Updates a controlreference's binded devices/controls // Updates a controlreference's binded devices/controls
// need to call this to re-parse a control reference's expression after changing it // need to call this to re-bind a control reference after changing its expression
// //
void ControlReference::UpdateReference(const ciface::Core::DeviceContainer& devices, void ControlReference::UpdateReference(const ciface::Core::DeviceContainer& devices,
const ciface::Core::DeviceQualifier& default_device) const ciface::Core::DeviceQualifier& default_device)
{ {
ControlFinder finder(devices, default_device, IsInput()); ControlFinder finder(devices, default_device, IsInput());
std::tie(m_parse_status, m_parsed_expression) = ParseExpression(expression);
if (m_parsed_expression) if (m_parsed_expression)
m_parsed_expression->UpdateReferences(finder); m_parsed_expression->UpdateReferences(finder);
} }
@ -49,6 +48,17 @@ ParseStatus ControlReference::GetParseStatus() const
return m_parse_status; return m_parse_status;
} }
std::string ControlReference::GetExpression() const
{
return m_expression;
}
void ControlReference::SetExpression(std::string expr)
{
m_expression = std::move(expr);
std::tie(m_parse_status, m_parsed_expression) = ParseExpression(m_expression);
}
ControlReference::ControlReference() : range(1), m_parsed_expression(nullptr) ControlReference::ControlReference() : range(1), m_parsed_expression(nullptr)
{ {
} }

View File

@ -34,12 +34,14 @@ public:
ciface::ExpressionParser::ParseStatus GetParseStatus() const; ciface::ExpressionParser::ParseStatus GetParseStatus() const;
void UpdateReference(const ciface::Core::DeviceContainer& devices, void UpdateReference(const ciface::Core::DeviceContainer& devices,
const ciface::Core::DeviceQualifier& default_device); const ciface::Core::DeviceQualifier& default_device);
std::string GetExpression() const;
void SetExpression(std::string expr);
ControlState range; ControlState range;
std::string expression;
protected: protected:
ControlReference(); ControlReference();
std::string m_expression;
std::unique_ptr<ciface::ExpressionParser::Expression> m_parsed_expression; std::unique_ptr<ciface::ExpressionParser::Expression> m_parsed_expression;
ciface::ExpressionParser::ParseStatus m_parse_status; ciface::ExpressionParser::ParseStatus m_parse_status;
}; };

View File

@ -216,7 +216,7 @@ public:
// Keep a shared_ptr to the device so the control pointer doesn't become invalid // Keep a shared_ptr to the device so the control pointer doesn't become invalid
std::shared_ptr<Device> m_device; std::shared_ptr<Device> m_device;
ControlExpression(ControlQualifier qualifier_) : qualifier(qualifier_) {} explicit ControlExpression(ControlQualifier qualifier_) : qualifier(qualifier_) {}
ControlState GetValue() const override { return control ? control->ToInput()->GetState() : 0.0; } ControlState GetValue() const override { return control ? control->ToInput()->GetState() : 0.0; }
void SetValue(ControlState value) override void SetValue(ControlState value) override
{ {

View File

@ -54,8 +54,12 @@ void ControlGroup::LoadConfig(IniFile::Section* sec, const std::string& defdev,
for (auto& c : controls) for (auto& c : controls)
{ {
// control expression {
sec->Get(group + c->name, &c->control_ref->expression, ""); // control expression
std::string expression;
sec->Get(group + c->name, &expression, "");
c->control_ref->SetExpression(std::move(expression));
}
// range // range
sec->Get(group + c->name + "/Range", &c->control_ref->range, 100.0); sec->Get(group + c->name + "/Range", &c->control_ref->range, 100.0);
@ -109,7 +113,7 @@ void ControlGroup::SaveConfig(IniFile::Section* sec, const std::string& defdev,
for (auto& c : controls) for (auto& c : controls)
{ {
// control expression // control expression
sec->Set(group + c->name, c->control_ref->expression, ""); sec->Set(group + c->name, c->control_ref->GetExpression(), "");
// range // range
sec->Set(group + c->name + "/Range", c->control_ref->range * 100.0, 100.0); sec->Set(group + c->name + "/Range", c->control_ref->range * 100.0, 100.0);
@ -128,6 +132,6 @@ void ControlGroup::SaveConfig(IniFile::Section* sec, const std::string& defdev,
void ControlGroup::SetControlExpression(int index, const std::string& expression) void ControlGroup::SetControlExpression(int index, const std::string& expression)
{ {
controls.at(index)->control_ref->expression = expression; controls.at(index)->control_ref->SetExpression(expression);
} }
} // namespace ControllerEmu } // namespace ControllerEmu