From 28dbdb3d946a3c4ad23e5a79457d509ae12ebb51 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Tue, 17 Mar 2020 11:27:00 -0700 Subject: [PATCH] Persist variable settings after graph is plotted (#1055) * Allow copying graph as image * Persist variables * Revert "Allow copying graph as image" This reverts commit 4fc9d798bc5f3ff82efc4fb00140103213fb81e2. * fix binding bug * undo cert change * fix animation * remove extra lines * remove overrides * undo key comment --- .../GraphingCalculatorViewModel.cpp | 7 +- .../GraphingCalculatorViewModel.h | 2 +- .../GraphingCalculator/VariableViewModel.h | 74 +++++++++++++++---- .../GraphingCalculator/EquationInputArea.xaml | 17 +++-- src/GraphControl/Control/Grapher.cpp | 27 +++---- src/GraphControl/Control/Grapher.h | 13 ++-- src/GraphControl/GraphControl.vcxproj | 1 + src/GraphControl/GraphControl.vcxproj.filters | 5 +- src/GraphControl/Models/Variable.h | 26 +++++++ src/Settings.XamlStyler | 3 +- 10 files changed, 125 insertions(+), 50 deletions(-) create mode 100644 src/GraphControl/Models/Variable.h diff --git a/src/CalcViewModel/GraphingCalculator/GraphingCalculatorViewModel.cpp b/src/CalcViewModel/GraphingCalculator/GraphingCalculatorViewModel.cpp index 974cf8f8..4e605612 100644 --- a/src/CalcViewModel/GraphingCalculator/GraphingCalculatorViewModel.cpp +++ b/src/CalcViewModel/GraphingCalculator/GraphingCalculatorViewModel.cpp @@ -10,6 +10,7 @@ using namespace Platform::Collections; using namespace Windows::Foundation; using namespace Windows::Foundation::Collections; using namespace Windows::UI::Xaml::Data; +using namespace GraphControl; namespace CalculatorApp::ViewModel { @@ -24,12 +25,12 @@ namespace CalculatorApp::ViewModel { } - void GraphingCalculatorViewModel::UpdateVariables(IMap ^ variables) + void GraphingCalculatorViewModel::UpdateVariables(IMap ^ variables) { Variables->Clear(); - for (auto var : variables) + for (auto variablePair : variables) { - auto variable = ref new VariableViewModel(var->Key, var->Value); + auto variable = ref new VariableViewModel(variablePair->Key, variablePair->Value); variable->VariableUpdated += ref new EventHandler([this, variable](Object ^ sender, VariableChangedEventArgs e) { VariableUpdated(variable, VariableChangedEventArgs{ e.variableName, e.newValue }); }); diff --git a/src/CalcViewModel/GraphingCalculator/GraphingCalculatorViewModel.h b/src/CalcViewModel/GraphingCalculator/GraphingCalculatorViewModel.h index 78e89479..eabd88e9 100644 --- a/src/CalcViewModel/GraphingCalculator/GraphingCalculatorViewModel.h +++ b/src/CalcViewModel/GraphingCalculator/GraphingCalculatorViewModel.h @@ -24,7 +24,7 @@ namespace CalculatorApp::ViewModel event Windows::Foundation::EventHandler ^ VariableUpdated; - void UpdateVariables(Windows::Foundation::Collections::IMap ^ variables); + void UpdateVariables(Windows::Foundation::Collections::IMap ^ variables); void SetSelectedEquation(EquationViewModel ^ equation); private: diff --git a/src/CalcViewModel/GraphingCalculator/VariableViewModel.h b/src/CalcViewModel/GraphingCalculator/VariableViewModel.h index 4d9240e8..b412073f 100644 --- a/src/CalcViewModel/GraphingCalculator/VariableViewModel.h +++ b/src/CalcViewModel/GraphingCalculator/VariableViewModel.h @@ -18,47 +18,89 @@ public [Windows::UI::Xaml::Data::Bindable] public ref class VariableViewModel sealed : public Windows::UI::Xaml::Data::INotifyPropertyChanged { public: - VariableViewModel(Platform::String ^ name, double value) + VariableViewModel(Platform::String ^ name, GraphControl::Variable ^ variable) : m_Name(name) - , m_Value(value) + , m_variable{ variable } , m_SliderSettingsVisible(false) - , m_Min(0.0) - , m_Step(0.1) - , m_Max(2.0) { } OBSERVABLE_OBJECT(); OBSERVABLE_PROPERTY_R(Platform::String ^, Name); - OBSERVABLE_PROPERTY_RW(double, Min); - OBSERVABLE_PROPERTY_RW(double, Step); - OBSERVABLE_PROPERTY_RW(double, Max); OBSERVABLE_PROPERTY_RW(bool, SliderSettingsVisible); + property double Min + { + double get() + { + return m_variable->Min; + } + void set(double value) + { + if (m_variable->Min != value) + { + m_variable->Min = value; + RaisePropertyChanged("Min"); + } + } + } + + property double Step + { + double get() + { + return m_variable->Step; + } + void set(double value) + { + if (m_variable->Step != value) + { + m_variable->Step = value; + RaisePropertyChanged("Step"); + } + } + } + + property double Max + { + double get() + { + return m_variable->Max; + } + void set(double value) + { + if (m_variable->Max != value) + { + m_variable->Max = value; + RaisePropertyChanged("Max"); + } + } + } + event Windows::Foundation::EventHandler ^ VariableUpdated; property double Value { double get() { - return m_Value; + return m_variable->Value; } void set(double value) { - if (value < Min) + if (value < m_variable->Min) { - Min = value; + m_variable->Min = value; RaisePropertyChanged(L"Min"); } - else if (value > Max) + else if (value > m_variable->Max) { - Max = value; + m_variable->Max = value; RaisePropertyChanged(L"Max"); } - if (Value != value) + if (m_variable->Value != value) { - m_Value = value; + m_variable->Value = value; VariableUpdated(this, VariableChangedEventArgs{ Name, value }); RaisePropertyChanged(L"Value"); } @@ -66,6 +108,6 @@ public } private: - double m_Value; + GraphControl::Variable ^ m_variable; }; } diff --git a/src/Calculator/Views/GraphingCalculator/EquationInputArea.xaml b/src/Calculator/Views/GraphingCalculator/EquationInputArea.xaml index b2fd608c..55491b4b 100644 --- a/src/Calculator/Views/GraphingCalculator/EquationInputArea.xaml +++ b/src/Calculator/Views/GraphingCalculator/EquationInputArea.xaml @@ -132,11 +132,11 @@ Margin="8,0,8,-6" VerticalAlignment="Center" DataContext="{x:Bind}" - Maximum="{x:Bind Max, Mode=TwoWay}" - Minimum="{x:Bind Min, Mode=TwoWay}" StepFrequency="{x:Bind Step, Mode=TwoWay}" ValueChanged="Slider_ValueChanged" - Value="{x:Bind Value, Mode=TwoWay}"/> + Value="{x:Bind Value, Mode=TwoWay}" + Maximum="{x:Bind Max, Mode=TwoWay}" + Minimum="{x:Bind Min, Mode=TwoWay}"/> - + - + @@ -882,10 +882,11 @@ + + + - - - + diff --git a/src/GraphControl/Control/Grapher.cpp b/src/GraphControl/Control/Grapher.cpp index da711387..a796dfd3 100644 --- a/src/GraphControl/Control/Grapher.cpp +++ b/src/GraphControl/Control/Grapher.cpp @@ -442,9 +442,9 @@ namespace GraphControl { critical_section::scoped_lock lock(m_renderMain->GetCriticalSection()); - for (auto variable : Variables) + for (auto variablePair : Variables) { - graph->SetArgValue(variable->Key->Data(), variable->Value); + graph->SetArgValue(variablePair->Key->Data(), variablePair->Value->Value); } } } @@ -470,7 +470,7 @@ namespace GraphControl void Grapher::UpdateVariables() { - auto updatedVariables = ref new Map(); + auto updatedVariables = ref new Map(); if (m_graph) { @@ -483,12 +483,19 @@ namespace GraphControl auto key = ref new String(graphVar->GetVariableName().data()); double value = 1.0; + Variable ^ variable; + if (Variables->HasKey(key)) { - value = Variables->Lookup(key); + variable = Variables->Lookup(key); } - updatedVariables->Insert(key, value); + if (variable == nullptr) + { + variable = ref new Variable(1.0); + } + + updatedVariables->Insert(key, variable); } } } @@ -504,17 +511,11 @@ namespace GraphControl void Grapher::SetVariable(Platform::String ^ variableName, double newValue) { - if (Variables->HasKey(variableName)) + if (!Variables->HasKey(variableName)) { - if (Variables->Lookup(variableName) == newValue) - { - return; - } - - Variables->Remove(variableName); + Variables->Insert(variableName, ref new Variable(newValue)); } - Variables->Insert(variableName, newValue); if (m_graph != nullptr && m_renderMain != nullptr) { diff --git a/src/GraphControl/Control/Grapher.h b/src/GraphControl/Control/Grapher.h index d1c443a7..5ca446ae 100644 --- a/src/GraphControl/Control/Grapher.h +++ b/src/GraphControl/Control/Grapher.h @@ -4,9 +4,10 @@ #pragma once #include "DirectX/RenderMain.h" -#include "../Models/Equation.h" -#include "../Models/EquationCollection.h" -#include "../Utils.h" +#include "Models/Equation.h" +#include "Models/EquationCollection.h" +#include "Models/Variable.h" +#include "Utils.h" #include "IGraphAnalyzer.h" #include "IMathSolver.h" #include "Common.h" @@ -43,9 +44,9 @@ public DEPENDENCY_PROPERTY_WITH_DEFAULT_AND_CALLBACK(bool, ForceProportionalAxes, true); DEPENDENCY_PROPERTY_WITH_DEFAULT_AND_CALLBACK(bool, UseCommaDecimalSeperator, false); DEPENDENCY_PROPERTY_WITH_DEFAULT( - SINGLE_ARG(Windows::Foundation::Collections::IObservableMap ^), + SINGLE_ARG(Windows::Foundation::Collections::IObservableMap ^), Variables, - SINGLE_ARG(ref new Platform::Collections::Map())); + SINGLE_ARG(ref new Platform::Collections::Map())); DEPENDENCY_PROPERTY_R_WITH_DEFAULT_AND_CALLBACK(GraphControl::EquationCollection ^, Equations, nullptr); DEPENDENCY_PROPERTY_WITH_DEFAULT_AND_CALLBACK(Windows::UI::Color, AxesColor, Windows::UI::Colors::Transparent); DEPENDENCY_PROPERTY_WITH_DEFAULT_AND_CALLBACK(Windows::UI::Color, GraphBackground, Windows::UI::Colors::Transparent); @@ -105,7 +106,7 @@ public } } - event Windows::Foundation::EventHandler ^> ^ VariablesUpdated; + event Windows::Foundation::EventHandler ^> ^ VariablesUpdated; void SetVariable(Platform::String ^ variableName, double newValue); Platform::String ^ ConvertToLinear(Platform::String ^ mmlString); Platform::String ^ FormatMathML(Platform::String ^ mmlString); diff --git a/src/GraphControl/GraphControl.vcxproj b/src/GraphControl/GraphControl.vcxproj index 0cf32b1b..7a64af77 100644 --- a/src/GraphControl/GraphControl.vcxproj +++ b/src/GraphControl/GraphControl.vcxproj @@ -300,6 +300,7 @@ + diff --git a/src/GraphControl/GraphControl.vcxproj.filters b/src/GraphControl/GraphControl.vcxproj.filters index e9d9d8f9..60ea5d32 100644 --- a/src/GraphControl/GraphControl.vcxproj.filters +++ b/src/GraphControl/GraphControl.vcxproj.filters @@ -60,6 +60,9 @@ Models + + Models + @@ -70,7 +73,5 @@ - - \ No newline at end of file diff --git a/src/GraphControl/Models/Variable.h b/src/GraphControl/Models/Variable.h new file mode 100644 index 00000000..536edef2 --- /dev/null +++ b/src/GraphControl/Models/Variable.h @@ -0,0 +1,26 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#pragma once +#include "Utils.h" + +namespace GraphControl +{ +public + ref class Variable sealed + { + public: + PROPERTY_RW(double, Value); + PROPERTY_RW(double, Step); + PROPERTY_RW(double, Min); + PROPERTY_RW(double, Max); + + Variable(double value) + : m_Value{ value } + , m_Step{ 0.1 } + , m_Min{ 0.0 } + , m_Max{ 2.0 } + { + } + }; +} diff --git a/src/Settings.XamlStyler b/src/Settings.XamlStyler index 07bd12eb..2c919209 100644 --- a/src/Settings.XamlStyler +++ b/src/Settings.XamlStyler @@ -19,7 +19,8 @@ "*:*, *", "PageSource, PageIndex, Offset, Color, TargetName, Property, Value, StartPoint, EndPoint", "mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText", - "IsEnabled, x:Load, Load" + "IsEnabled, x:Load, Load", + "Value, Maximum, Minimum" ], "OrderAttributesByName": true, "PutEndingBracketOnNewLine": false,