diff --git a/src/CalcViewModel/GraphingCalculator/GraphingSettingsViewModel.cpp b/src/CalcViewModel/GraphingCalculator/GraphingSettingsViewModel.cpp index b201b21..c261890 100644 --- a/src/CalcViewModel/GraphingCalculator/GraphingSettingsViewModel.cpp +++ b/src/CalcViewModel/GraphingCalculator/GraphingSettingsViewModel.cpp @@ -71,21 +71,6 @@ void GraphingSettingsViewModel::InitRanges() m_dontUpdateDisplayRange = false; } -void GraphingSettingsViewModel::RefreshPosition() -{ - if (HasError()) - { - InitRanges(); - } - else - { - if (m_Graph != nullptr) - { - m_Graph->SetDisplayRanges(m_XMinValue, m_XMaxValue, m_YMinValue, m_YMaxValue); - } - } -} - void GraphingSettingsViewModel::UpdateDisplayRange(bool XValuesModified) { if (m_Graph == nullptr || m_dontUpdateDisplayRange || HasError()) diff --git a/src/CalcViewModel/GraphingCalculator/GraphingSettingsViewModel.h b/src/CalcViewModel/GraphingCalculator/GraphingSettingsViewModel.h index b9fc475..ed250be 100644 --- a/src/CalcViewModel/GraphingCalculator/GraphingSettingsViewModel.h +++ b/src/CalcViewModel/GraphingCalculator/GraphingSettingsViewModel.h @@ -229,7 +229,6 @@ namespace CalculatorApp::ViewModel RaisePropertyChanged(L"TrigModeRadians"); RaisePropertyChanged(L"TrigModeDegrees"); RaisePropertyChanged(L"TrigModeGradians"); - RefreshPosition(); } } } @@ -248,7 +247,6 @@ namespace CalculatorApp::ViewModel RaisePropertyChanged(L"TrigModeDegrees"); RaisePropertyChanged(L"TrigModeRadians"); RaisePropertyChanged(L"TrigModeGradians"); - RefreshPosition(); } } } @@ -267,14 +265,12 @@ namespace CalculatorApp::ViewModel RaisePropertyChanged(L"TrigModeGradians"); RaisePropertyChanged(L"TrigModeDegrees"); RaisePropertyChanged(L"TrigModeRadians"); - RefreshPosition(); } } } public: void UpdateDisplayRange(bool XValuesModified); - void RefreshPosition(); public: void SetGrapher(GraphControl::Grapher ^ grapher); diff --git a/src/Calculator/Views/GraphingCalculator/GraphingCalculator.xaml.cpp b/src/Calculator/Views/GraphingCalculator/GraphingCalculator.xaml.cpp index e3342f0..1881394 100644 --- a/src/Calculator/Views/GraphingCalculator/GraphingCalculator.xaml.cpp +++ b/src/Calculator/Views/GraphingCalculator/GraphingCalculator.xaml.cpp @@ -160,7 +160,7 @@ void GraphingCalculator::OnEquationsVectorChanged(IObservableVectorEquations->Append(equationViewModel->GraphEquation); } - GraphingControl->PlotGraph(); + GraphingControl->PlotGraph(false); } void GraphingCalculator::OnTracePointChanged(Point newPoint) diff --git a/src/GraphControl/Control/Grapher.cpp b/src/GraphControl/Control/Grapher.cpp index 4825dca..f6953d9 100644 --- a/src/GraphControl/Control/Grapher.cpp +++ b/src/GraphControl/Control/Grapher.cpp @@ -121,7 +121,7 @@ namespace GraphControl m_renderMain->BackgroundColor = GraphBackground; } - TryUpdateGraph(); + TryUpdateGraph(false); } void Grapher::OnEquationsPropertyChanged(EquationCollection ^ oldValue, EquationCollection ^ newValue) @@ -157,7 +157,7 @@ namespace GraphControl ref new EquationChangedEventHandler(this, &Grapher::OnEquationLineEnabledChanged); } - PlotGraph(); + PlotGraph(false); } void Grapher::OnEquationChanged(Equation ^ equation) @@ -169,7 +169,7 @@ namespace GraphControl equation->HasGraphError = false; equation->IsValidated = false; - TryPlotGraph(shouldRetry); + TryPlotGraph(false, shouldRetry); } void Grapher::OnEquationStyleChanged(Equation ^) @@ -193,7 +193,7 @@ namespace GraphControl return; } - PlotGraph(); + PlotGraph(true); } KeyGraphFeaturesInfo ^ Grapher::AnalyzeEquation(Equation ^ equation) @@ -223,14 +223,14 @@ namespace GraphControl return KeyGraphFeaturesInfo::Create(CalculatorApp::AnalysisErrorType::AnalysisCouldNotBePerformed); } - void Grapher::PlotGraph() + void Grapher::PlotGraph(bool keepCurrentView) { - TryPlotGraph(false); + TryPlotGraph(keepCurrentView, false); } - void Grapher::TryPlotGraph(bool shouldRetry) + void Grapher::TryPlotGraph(bool keepCurrentView, bool shouldRetry) { - if (TryUpdateGraph()) + if (TryUpdateGraph(keepCurrentView)) { SetEquationsAsValid(); } @@ -241,12 +241,12 @@ namespace GraphControl // If we failed to plot the graph, try again after the bad equations are flagged. if (shouldRetry) { - TryUpdateGraph(); + TryUpdateGraph(keepCurrentView); } } } - bool Grapher::TryUpdateGraph() + bool Grapher::TryUpdateGraph(bool keepCurrentView) { optional>> initResult = nullopt; bool successful = false; @@ -289,8 +289,8 @@ namespace GraphControl if (graphExpression = m_solver->ParseInput(request)) { - initResult = m_graph->TryInitialize(graphExpression.get()); - + initResult = TryInitializeGraph(keepCurrentView, graphExpression.get()); + if (initResult != nullopt) { UpdateGraphOptions(m_graph->GetOptions(), validEqs); @@ -318,8 +318,7 @@ namespace GraphControl // Do not re-initialize the graph to empty if there are still valid equations graphed if (!shouldKeepPreviousGraph) { - initResult = m_graph->TryInitialize(); - + initResult = TryInitializeGraph(keepCurrentView, graphExpression.get()); if (initResult != nullopt) { UpdateGraphOptions(m_graph->GetOptions(), validEqs); @@ -373,7 +372,7 @@ namespace GraphControl shared_ptr Grapher::GetGraph(Equation ^ equation) { - std::shared_ptr graph = m_solver->CreateGrapher(); + shared_ptr graph = m_solver->CreateGrapher(); wstringstream ss{}; ss << s_getGraphOpeningTags; @@ -487,7 +486,7 @@ namespace GraphControl void Grapher::OnForceProportionalAxesPropertyChanged(bool /*oldValue*/, bool newValue) { m_calculatedForceProportional = newValue; - TryUpdateGraph(); + TryUpdateGraph(false); } void Grapher::OnPointerEntered(PointerRoutedEventArgs ^ e) @@ -646,14 +645,14 @@ namespace GraphControl { if (auto renderer = m_graph->GetRenderer()) { - std::shared_ptr BitmapOut; + shared_ptr BitmapOut; bool hasSomeMissingDataOut = false; HRESULT hr = E_FAIL; hr = renderer->GetBitmap(BitmapOut, hasSomeMissingDataOut); if (SUCCEEDED(hr)) { // Get the raw date - std::vector byteVector = BitmapOut->GetData(); + vector byteVector = BitmapOut->GetData(); auto arr = ref new Array(&byteVector[0], (unsigned int)byteVector.size()); // create a memory stream wrapper @@ -889,3 +888,19 @@ void Grapher::OnGraphBackgroundPropertyChanged(Windows::UI::Color /*oldValue*/, m_graph->GetOptions().SetBoxColor(color); } } + +optional>> Grapher::TryInitializeGraph(bool keepCurrentView, const IExpression* graphingExp) +{ + if (keepCurrentView) + { + double xMin, xMax, yMin, yMax; + m_graph->GetRenderer()->GetDisplayRanges(xMin, xMax, yMin, yMax); + auto initResult = m_graph->TryInitialize(graphingExp); + m_graph->GetRenderer()->SetDisplayRanges(xMin, xMax, yMin, yMax); + return initResult; + } + else + { + return m_graph->TryInitialize(graphingExp); + } +} diff --git a/src/GraphControl/Control/Grapher.h b/src/GraphControl/Control/Grapher.h index 9e15911..2b63383 100644 --- a/src/GraphControl/Control/Grapher.h +++ b/src/GraphControl/Control/Grapher.h @@ -104,7 +104,13 @@ public event Windows::Foundation::EventHandler ^> ^ VariablesUpdated; void SetVariable(Platform::String ^ variableName, double newValue); Platform::String ^ ConvertToLinear(Platform::String ^ mmlString); - void PlotGraph(); + + /// + /// Draw the graph. Call this method if you add or modify an equation. + /// + /// Force the graph control to not pan or zoom to adapt the view. + void PlotGraph(bool keepCurrentView); + GraphControl::KeyGraphFeaturesInfo ^ AnalyzeEquation(GraphControl::Equation ^ equation); // We can't use the EvalTrigUnitMode enum directly in as the property type because it comes from another module which doesn't expose @@ -116,7 +122,7 @@ public if (value != (int)m_solver->EvalOptions().GetTrigUnitMode()) { m_solver->EvalOptions().SetTrigUnitMode((Graphing::EvalTrigUnitMode)value); - PlotGraph(); + PlotGraph(true); } } @@ -265,8 +271,8 @@ public void OnEquationChanged(Equation ^ equation); void OnEquationStyleChanged(Equation ^ equation); void OnEquationLineEnabledChanged(Equation ^ equation); - bool TryUpdateGraph(); - void TryPlotGraph(bool shouldRetry); + bool TryUpdateGraph(bool keepCurrentView); + void TryPlotGraph(bool keepCurrentView, bool shouldRetry); void UpdateGraphOptions(Graphing::IGraphingOptions& options, const std::vector& validEqs); std::vector GetGraphableEquations(); void SetGraphArgs(); @@ -284,7 +290,7 @@ public void SetEquationsAsValid(); void SetEquationErrors(); - + std::optional>> TryInitializeGraph(bool keepCurrentView, _In_ const Graphing::IExpression* graphingExp = nullptr); private: DX::RenderMain ^ m_renderMain = nullptr;