Prevent the graph to pan/zoom in some cases (#897)

* prevent the graph to change ranges when hide/show an equation or change the trig unit

* make sure to not zoom/pan when we hide the last visible equation
This commit is contained in:
Rudy Huyn 2020-01-06 13:10:53 -08:00 committed by Pepe Rivera
parent 8357f5d5c5
commit 7aaeee934c
5 changed files with 45 additions and 43 deletions

View File

@ -71,21 +71,6 @@ void GraphingSettingsViewModel::InitRanges()
m_dontUpdateDisplayRange = false; 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) void GraphingSettingsViewModel::UpdateDisplayRange(bool XValuesModified)
{ {
if (m_Graph == nullptr || m_dontUpdateDisplayRange || HasError()) if (m_Graph == nullptr || m_dontUpdateDisplayRange || HasError())

View File

@ -229,7 +229,6 @@ namespace CalculatorApp::ViewModel
RaisePropertyChanged(L"TrigModeRadians"); RaisePropertyChanged(L"TrigModeRadians");
RaisePropertyChanged(L"TrigModeDegrees"); RaisePropertyChanged(L"TrigModeDegrees");
RaisePropertyChanged(L"TrigModeGradians"); RaisePropertyChanged(L"TrigModeGradians");
RefreshPosition();
} }
} }
} }
@ -248,7 +247,6 @@ namespace CalculatorApp::ViewModel
RaisePropertyChanged(L"TrigModeDegrees"); RaisePropertyChanged(L"TrigModeDegrees");
RaisePropertyChanged(L"TrigModeRadians"); RaisePropertyChanged(L"TrigModeRadians");
RaisePropertyChanged(L"TrigModeGradians"); RaisePropertyChanged(L"TrigModeGradians");
RefreshPosition();
} }
} }
} }
@ -267,14 +265,12 @@ namespace CalculatorApp::ViewModel
RaisePropertyChanged(L"TrigModeGradians"); RaisePropertyChanged(L"TrigModeGradians");
RaisePropertyChanged(L"TrigModeDegrees"); RaisePropertyChanged(L"TrigModeDegrees");
RaisePropertyChanged(L"TrigModeRadians"); RaisePropertyChanged(L"TrigModeRadians");
RefreshPosition();
} }
} }
} }
public: public:
void UpdateDisplayRange(bool XValuesModified); void UpdateDisplayRange(bool XValuesModified);
void RefreshPosition();
public: public:
void SetGrapher(GraphControl::Grapher ^ grapher); void SetGrapher(GraphControl::Grapher ^ grapher);

View File

@ -160,7 +160,7 @@ void GraphingCalculator::OnEquationsVectorChanged(IObservableVector<EquationView
GraphingControl->Equations->Append(equationViewModel->GraphEquation); GraphingControl->Equations->Append(equationViewModel->GraphEquation);
} }
GraphingControl->PlotGraph(); GraphingControl->PlotGraph(false);
} }
void GraphingCalculator::OnTracePointChanged(Point newPoint) void GraphingCalculator::OnTracePointChanged(Point newPoint)

View File

@ -121,7 +121,7 @@ namespace GraphControl
m_renderMain->BackgroundColor = GraphBackground; m_renderMain->BackgroundColor = GraphBackground;
} }
TryUpdateGraph(); TryUpdateGraph(false);
} }
void Grapher::OnEquationsPropertyChanged(EquationCollection ^ oldValue, EquationCollection ^ newValue) void Grapher::OnEquationsPropertyChanged(EquationCollection ^ oldValue, EquationCollection ^ newValue)
@ -157,7 +157,7 @@ namespace GraphControl
ref new EquationChangedEventHandler(this, &Grapher::OnEquationLineEnabledChanged); ref new EquationChangedEventHandler(this, &Grapher::OnEquationLineEnabledChanged);
} }
PlotGraph(); PlotGraph(false);
} }
void Grapher::OnEquationChanged(Equation ^ equation) void Grapher::OnEquationChanged(Equation ^ equation)
@ -169,7 +169,7 @@ namespace GraphControl
equation->HasGraphError = false; equation->HasGraphError = false;
equation->IsValidated = false; equation->IsValidated = false;
TryPlotGraph(shouldRetry); TryPlotGraph(false, shouldRetry);
} }
void Grapher::OnEquationStyleChanged(Equation ^) void Grapher::OnEquationStyleChanged(Equation ^)
@ -193,7 +193,7 @@ namespace GraphControl
return; return;
} }
PlotGraph(); PlotGraph(true);
} }
KeyGraphFeaturesInfo ^ Grapher::AnalyzeEquation(Equation ^ equation) KeyGraphFeaturesInfo ^ Grapher::AnalyzeEquation(Equation ^ equation)
@ -223,14 +223,14 @@ namespace GraphControl
return KeyGraphFeaturesInfo::Create(CalculatorApp::AnalysisErrorType::AnalysisCouldNotBePerformed); 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(); SetEquationsAsValid();
} }
@ -241,12 +241,12 @@ namespace GraphControl
// If we failed to plot the graph, try again after the bad equations are flagged. // If we failed to plot the graph, try again after the bad equations are flagged.
if (shouldRetry) if (shouldRetry)
{ {
TryUpdateGraph(); TryUpdateGraph(keepCurrentView);
} }
} }
} }
bool Grapher::TryUpdateGraph() bool Grapher::TryUpdateGraph(bool keepCurrentView)
{ {
optional<vector<shared_ptr<IEquation>>> initResult = nullopt; optional<vector<shared_ptr<IEquation>>> initResult = nullopt;
bool successful = false; bool successful = false;
@ -289,7 +289,7 @@ namespace GraphControl
if (graphExpression = m_solver->ParseInput(request)) if (graphExpression = m_solver->ParseInput(request))
{ {
initResult = m_graph->TryInitialize(graphExpression.get()); initResult = TryInitializeGraph(keepCurrentView, graphExpression.get());
if (initResult != nullopt) if (initResult != nullopt)
{ {
@ -318,8 +318,7 @@ namespace GraphControl
// Do not re-initialize the graph to empty if there are still valid equations graphed // Do not re-initialize the graph to empty if there are still valid equations graphed
if (!shouldKeepPreviousGraph) if (!shouldKeepPreviousGraph)
{ {
initResult = m_graph->TryInitialize(); initResult = TryInitializeGraph(keepCurrentView, graphExpression.get());
if (initResult != nullopt) if (initResult != nullopt)
{ {
UpdateGraphOptions(m_graph->GetOptions(), validEqs); UpdateGraphOptions(m_graph->GetOptions(), validEqs);
@ -373,7 +372,7 @@ namespace GraphControl
shared_ptr<IGraph> Grapher::GetGraph(Equation ^ equation) shared_ptr<IGraph> Grapher::GetGraph(Equation ^ equation)
{ {
std::shared_ptr<Graphing::IGraph> graph = m_solver->CreateGrapher(); shared_ptr<Graphing::IGraph> graph = m_solver->CreateGrapher();
wstringstream ss{}; wstringstream ss{};
ss << s_getGraphOpeningTags; ss << s_getGraphOpeningTags;
@ -487,7 +486,7 @@ namespace GraphControl
void Grapher::OnForceProportionalAxesPropertyChanged(bool /*oldValue*/, bool newValue) void Grapher::OnForceProportionalAxesPropertyChanged(bool /*oldValue*/, bool newValue)
{ {
m_calculatedForceProportional = newValue; m_calculatedForceProportional = newValue;
TryUpdateGraph(); TryUpdateGraph(false);
} }
void Grapher::OnPointerEntered(PointerRoutedEventArgs ^ e) void Grapher::OnPointerEntered(PointerRoutedEventArgs ^ e)
@ -646,14 +645,14 @@ namespace GraphControl
{ {
if (auto renderer = m_graph->GetRenderer()) if (auto renderer = m_graph->GetRenderer())
{ {
std::shared_ptr<Graphing::IBitmap> BitmapOut; shared_ptr<Graphing::IBitmap> BitmapOut;
bool hasSomeMissingDataOut = false; bool hasSomeMissingDataOut = false;
HRESULT hr = E_FAIL; HRESULT hr = E_FAIL;
hr = renderer->GetBitmap(BitmapOut, hasSomeMissingDataOut); hr = renderer->GetBitmap(BitmapOut, hasSomeMissingDataOut);
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
// Get the raw date // Get the raw date
std::vector<BYTE> byteVector = BitmapOut->GetData(); vector<BYTE> byteVector = BitmapOut->GetData();
auto arr = ref new Array<BYTE>(&byteVector[0], (unsigned int)byteVector.size()); auto arr = ref new Array<BYTE>(&byteVector[0], (unsigned int)byteVector.size());
// create a memory stream wrapper // create a memory stream wrapper
@ -889,3 +888,19 @@ void Grapher::OnGraphBackgroundPropertyChanged(Windows::UI::Color /*oldValue*/,
m_graph->GetOptions().SetBoxColor(color); m_graph->GetOptions().SetBoxColor(color);
} }
} }
optional<vector<shared_ptr<Graphing::IEquation>>> 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);
}
}

View File

@ -104,7 +104,13 @@ public
event Windows::Foundation::EventHandler<Windows::Foundation::Collections::IMap<Platform::String ^, double> ^> ^ VariablesUpdated; event Windows::Foundation::EventHandler<Windows::Foundation::Collections::IMap<Platform::String ^, double> ^> ^ VariablesUpdated;
void SetVariable(Platform::String ^ variableName, double newValue); void SetVariable(Platform::String ^ variableName, double newValue);
Platform::String ^ ConvertToLinear(Platform::String ^ mmlString); Platform::String ^ ConvertToLinear(Platform::String ^ mmlString);
void PlotGraph();
/// <summary>
/// Draw the graph. Call this method if you add or modify an equation.
/// </summary>
/// <param name="keepCurrentView">Force the graph control to not pan or zoom to adapt the view.</param>
void PlotGraph(bool keepCurrentView);
GraphControl::KeyGraphFeaturesInfo ^ AnalyzeEquation(GraphControl::Equation ^ equation); 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 // 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()) if (value != (int)m_solver->EvalOptions().GetTrigUnitMode())
{ {
m_solver->EvalOptions().SetTrigUnitMode((Graphing::EvalTrigUnitMode)value); m_solver->EvalOptions().SetTrigUnitMode((Graphing::EvalTrigUnitMode)value);
PlotGraph(); PlotGraph(true);
} }
} }
@ -265,8 +271,8 @@ public
void OnEquationChanged(Equation ^ equation); void OnEquationChanged(Equation ^ equation);
void OnEquationStyleChanged(Equation ^ equation); void OnEquationStyleChanged(Equation ^ equation);
void OnEquationLineEnabledChanged(Equation ^ equation); void OnEquationLineEnabledChanged(Equation ^ equation);
bool TryUpdateGraph(); bool TryUpdateGraph(bool keepCurrentView);
void TryPlotGraph(bool shouldRetry); void TryPlotGraph(bool keepCurrentView, bool shouldRetry);
void UpdateGraphOptions(Graphing::IGraphingOptions& options, const std::vector<Equation ^>& validEqs); void UpdateGraphOptions(Graphing::IGraphingOptions& options, const std::vector<Equation ^>& validEqs);
std::vector<Equation ^> GetGraphableEquations(); std::vector<Equation ^> GetGraphableEquations();
void SetGraphArgs(); void SetGraphArgs();
@ -284,7 +290,7 @@ public
void SetEquationsAsValid(); void SetEquationsAsValid();
void SetEquationErrors(); void SetEquationErrors();
std::optional<std::vector<std::shared_ptr<Graphing::IEquation>>> TryInitializeGraph(bool keepCurrentView, _In_ const Graphing::IExpression* graphingExp = nullptr);
private: private:
DX::RenderMain ^ m_renderMain = nullptr; DX::RenderMain ^ m_renderMain = nullptr;