Add variable editing (#581)

This commit is contained in:
Pepe Rivera
2019-07-24 11:23:33 -07:00
committed by GitHub
parent a418777f02
commit 46f11c7c72
12 changed files with 519 additions and 6 deletions

View File

@@ -26,8 +26,12 @@ namespace
constexpr auto s_propertyName_EquationTemplate = L"EquationTemplate";
constexpr auto s_propertyName_Equations = L"Equations";
constexpr auto s_propertyName_EquationsSource = L"EquationsSource";
constexpr auto s_propertyName_Variables = L"Variables";
constexpr auto s_propertyName_ForceProportionalAxes = L"ForceProportionalAxes";
constexpr auto s_X = L"x";
constexpr auto s_Y = L"y";
// Helper function for converting a pointer position to a position that the graphing engine will understand.
// posX/posY are the pointer position elements and width,height are the dimensions of the graph surface.
// The graphing engine interprets x,y position between the range [-1, 1].
@@ -43,6 +47,7 @@ namespace GraphControl
DependencyProperty^ Grapher::s_equationTemplateProperty;
DependencyProperty^ Grapher::s_equationsProperty;
DependencyProperty^ Grapher::s_equationsSourceProperty;
DependencyProperty^ Grapher::s_variablesProperty;
DependencyProperty^ Grapher::s_forceProportionalAxesTemplateProperty;
Grapher::Grapher()
@@ -54,6 +59,7 @@ namespace GraphControl
DefaultStyleKey = StringReference(s_defaultStyleKey);
this->SetValue(EquationsProperty, ref new EquationCollection());
this->SetValue(VariablesProperty, ref new Map<String^, double>());
this->Loaded += ref new RoutedEventHandler(this, &Grapher::OnLoaded);
this->Unloaded += ref new RoutedEventHandler(this, &Grapher::OnUnloaded);
@@ -145,6 +151,17 @@ namespace GraphControl
ref new PropertyChangedCallback(&Grapher::OnCustomDependencyPropertyChanged)));
}
if (!s_variablesProperty)
{
s_variablesProperty = DependencyProperty::Register(
StringReference(s_propertyName_Variables),
IObservableMap<String^, double>::typeid,
Grapher::typeid,
ref new PropertyMetadata(
nullptr,
ref new PropertyChangedCallback(&Grapher::OnCustomDependencyPropertyChanged)));
}
if (!s_forceProportionalAxesTemplateProperty)
{
s_forceProportionalAxesTemplateProperty = DependencyProperty::Register(
@@ -350,8 +367,11 @@ namespace GraphControl
if (m_graph->TryInitialize(graphExpression.get()))
{
UpdateGraphOptions(m_graph->GetOptions(), validEqs);
SetGraphArgs();
m_renderMain->Graph = m_graph;
UpdateVariables();
}
}
}
@@ -360,13 +380,80 @@ namespace GraphControl
if (m_graph->TryInitialize())
{
UpdateGraphOptions(m_graph->GetOptions(), validEqs);
SetGraphArgs();
m_renderMain->Graph = m_graph;
UpdateVariables();
}
}
}
}
void Grapher::SetGraphArgs()
{
if (m_graph)
{
for (auto variable : Variables)
{
m_graph->SetArgValue(variable->Key->Data(), variable->Value);
}
}
}
void Grapher::UpdateVariables()
{
auto updatedVariables = ref new Map<String^, double>();
if (m_graph)
{
auto graphVariables = m_graph->GetVariables();
for (auto graphVar : graphVariables)
{
if (graphVar->GetVariableName() != s_X && graphVar->GetVariableName() != s_Y)
{
auto key = ref new String(graphVar->GetVariableName().data());
double value = 1.0;
if (Variables->HasKey(key))
{
value = Variables->Lookup(key);
}
updatedVariables->Insert(key, value);
}
}
}
Variables = updatedVariables;
VariablesUpdated(this, Variables);
}
void Grapher::SetVariable(Platform::String^ variableName, double newValue)
{
if (Variables->HasKey(variableName))
{
if (Variables->Lookup(variableName) == newValue)
{
return;
}
Variables->Remove(variableName);
}
Variables->Insert(variableName, newValue);
if (m_graph)
{
m_graph->SetArgValue(variableName->Data(), newValue);
if (m_renderMain)
{
m_renderMain->RunRenderPass();
}
}
}
void Grapher::UpdateGraphOptions(IGraphingOptions& options, const vector<Equation^>& validEqs)
{
options.SetForceProportional(ForceProportionalAxes);

View File

@@ -5,6 +5,7 @@
#include "Equation.h"
#include "EquationCollection.h"
#include "IMathSolver.h"
#include "Common.h"
namespace GraphControl
{
@@ -78,6 +79,29 @@ namespace GraphControl
}
#pragma endregion
#pragma region Windows::Foundation::Collections::IObservableMap<Platform::String^, double>^ Variables DependencyProperty
static property Windows::UI::Xaml::DependencyProperty^ VariablesProperty
{
Windows::UI::Xaml::DependencyProperty^ get()
{
return s_variablesProperty;
}
}
property Windows::Foundation::Collections::IObservableMap<Platform::String^, double>^ Variables
{
Windows::Foundation::Collections::IObservableMap<Platform::String^, double>^ get()
{
return static_cast<Windows::Foundation::Collections::IObservableMap<Platform::String^, double>^>(GetValue(s_variablesProperty));
}
void set(Windows::Foundation::Collections::IObservableMap<Platform::String^, double>^ value)
{
SetValue(s_variablesProperty, value);
}
}
#pragma endregion
#pragma region Windows::UI::Xaml::DataTemplate^ ForceProportionalAxes DependencyProperty
static property Windows::UI::Xaml::DependencyProperty^ ForceProportionalAxesTemplateProperty
{
@@ -100,6 +124,10 @@ namespace GraphControl
}
#pragma endregion
event Windows::Foundation::EventHandler<Windows::Foundation::Collections::IMap<Platform::String^, double>^>^ VariablesUpdated;
void SetVariable(Platform::String^ variableName, double newValue);
protected:
#pragma region Control Overrides
void OnApplyTemplate() override;
@@ -127,12 +155,14 @@ namespace GraphControl
void OnDataSourceChanged(GraphControl::InspectingDataSource^ sender, GraphControl::DataSourceChangedEventArgs args);
void OnEquationsChanged(Windows::UI::Xaml::DependencyPropertyChangedEventArgs^ args);
void OnEquationsVectorChanged(Windows::Foundation::Collections::IObservableVector<GraphControl::Equation ^> ^sender, Windows::Foundation::Collections::IVectorChangedEventArgs ^event);
void OnEquationsVectorChanged(Windows::Foundation::Collections::IObservableVector<GraphControl::Equation ^> ^sender, Windows::Foundation::Collections::IVectorChangedEventArgs^ event);
void OnEquationChanged();
void UpdateGraph();
void UpdateGraphOptions(Graphing::IGraphingOptions& options, const std::vector<Equation^>& validEqs);
std::vector<Equation^> GetValidEquations();
void SetGraphArgs();
void UpdateVariables();
void OnForceProportionalAxesChanged(Windows::UI::Xaml::DependencyPropertyChangedEventArgs^ args);
@@ -154,6 +184,7 @@ namespace GraphControl
Windows::Foundation::EventRegistrationToken m_tokenDataSourceChanged;
static Windows::UI::Xaml::DependencyProperty^ s_equationsProperty;
static Windows::UI::Xaml::DependencyProperty^ s_variablesProperty;
Windows::Foundation::EventRegistrationToken m_tokenEquationsChanged;
Windows::Foundation::EventRegistrationToken m_tokenEquationChanged;