From 8957cb9fea084918895489ba27d12d0996743928 Mon Sep 17 00:00:00 2001 From: Ruben Dashyan Date: Thu, 13 Jun 2019 16:01:01 +0200 Subject: [PATCH] [apps/shared/curve_view_range] Define and use x|yCenter accessors --- apps/sequence/graph/curve_view_range.cpp | 6 +++--- apps/shared/curve_view_range.h | 2 ++ apps/shared/function_graph_controller.cpp | 2 +- apps/shared/interactive_curve_view_range.cpp | 22 +++++++------------- apps/shared/zoom_parameter_controller.cpp | 8 ++----- 5 files changed, 16 insertions(+), 24 deletions(-) diff --git a/apps/sequence/graph/curve_view_range.cpp b/apps/sequence/graph/curve_view_range.cpp index 07dfdafbd..a644efa6a 100644 --- a/apps/sequence/graph/curve_view_range.cpp +++ b/apps/sequence/graph/curve_view_range.cpp @@ -15,7 +15,7 @@ CurveViewRange::CurveViewRange(InteractiveCurveViewRangeDelegate * delegate) : } void CurveViewRange::roundAbscissa() { - int roundedXMean = std::round((m_xMin+m_xMax)/2); + int roundedXMean = std::round(xCenter()); float halfScreenWidth = ((float)Ion::Display::Width)/2.0f; float newXMin = clipped(roundedXMean - halfScreenWidth, false); float newXMax = clipped(roundedXMean + halfScreenWidth - 1.0f, true); @@ -36,8 +36,8 @@ void CurveViewRange::roundAbscissa() { } void CurveViewRange::normalize() { - float xMean = (m_xMin+m_xMax)/2; - float yMean = (m_yMin+m_yMax)/2; + float xMean = xCenter(); + float yMean = yCenter(); // Compute the X float newXMin = clipped(xMean - NormalizedXHalfRange(), false); diff --git a/apps/shared/curve_view_range.h b/apps/shared/curve_view_range.h index c5358b151..eeea3d28d 100644 --- a/apps/shared/curve_view_range.h +++ b/apps/shared/curve_view_range.h @@ -17,6 +17,8 @@ public: virtual float xMax() const = 0; virtual float yMin() const = 0; virtual float yMax() const = 0; + const float xCenter() const { return (xMin() + xMax()) / 2; } + const float yCenter() const { return (yMin() + yMax()) / 2; } virtual float xGridUnit() const { return computeGridUnit(Axis::X, xMax() - xMin()); } virtual float yGridUnit() const { return 0.0f; } float computeGridUnit(Axis axis, float range) const; diff --git a/apps/shared/function_graph_controller.cpp b/apps/shared/function_graph_controller.cpp index b125899dd..e5b91532d 100644 --- a/apps/shared/function_graph_controller.cpp +++ b/apps/shared/function_graph_controller.cpp @@ -118,7 +118,7 @@ InteractiveCurveViewRangeDelegate::Range FunctionGraphController::computeYRange( } double FunctionGraphController::defaultCursorT() { - return 0.0; //TODO LEA (interactiveCurveViewRange()->xMin()+interactiveCurveViewRange()->xMax())/2.0f; + return 0.0; //TODO LEA interactiveCurveViewRange()->xCenter(); } FunctionStore * FunctionGraphController::functionStore() const { diff --git a/apps/shared/interactive_curve_view_range.cpp b/apps/shared/interactive_curve_view_range.cpp index b418f3ae6..12c9417d6 100644 --- a/apps/shared/interactive_curve_view_range.cpp +++ b/apps/shared/interactive_curve_view_range.cpp @@ -71,8 +71,8 @@ void InteractiveCurveViewRange::zoom(float ratio, float x, float y) { if (ratio*std::fabs(xMax-xMin) < k_minFloat || ratio*std::fabs(yMax-yMin) < k_minFloat) { return; } - float centerX = std::isnan(x) || std::isinf(x) ? (xMax+xMin)/2: x; - float centerY = std::isnan(y) || std::isinf(y) ? (yMax+yMin)/2: y; + float centerX = std::isnan(x) || std::isinf(x) ? xCenter() : x; + float centerY = std::isnan(y) || std::isinf(y) ? yCenter() : y; float newXMin = clipped(centerX*(1.0f-ratio)+ratio*xMin, false); float newXMax = clipped(centerX*(1.0f-ratio)+ratio*xMax, true); m_yAuto = false; @@ -101,10 +101,8 @@ void InteractiveCurveViewRange::panWithVector(float x, float y) { void InteractiveCurveViewRange::roundAbscissa() { // Set x range - float xMin = m_xMin; - float xMax = m_xMax; - float newXMin = clipped(std::round((xMin+xMax)/2) - (float)Ion::Display::Width/2.0f, false); - float newXMax = clipped(std::round((xMin+xMax)/2) + (float)Ion::Display::Width/2.0f-1.0f, true); + float newXMin = clipped(std::round(xCenter()) - (float)Ion::Display::Width/2.0f, false); + float newXMax = clipped(std::round(xCenter()) + (float)Ion::Display::Width/2.0f-1.0f, true); if (std::isnan(newXMin) || std::isnan(newXMax)) { return; } @@ -117,21 +115,17 @@ void InteractiveCurveViewRange::roundAbscissa() { void InteractiveCurveViewRange::normalize() { /* We center the ranges on the current range center, and put each axis so that * 1cm = 2 units. */ - float xMin = m_xMin; - float xMax = m_xMax; - float yMin = m_yMin; - float yMax = m_yMax; m_yAuto = false; // Set x range - float newXMin = clipped((xMin+xMax)/2 - NormalizedXHalfRange(), false); - float newXMax = clipped((xMin+xMax)/2 + NormalizedXHalfRange(), true); + float newXMin = clipped(xCenter() - NormalizedXHalfRange(), false); + float newXMax = clipped(xCenter() + NormalizedXHalfRange(), true); if (!std::isnan(newXMin) && !std::isnan(newXMax)) { m_xMax = newXMax; MemoizedCurveViewRange::setXMin(newXMin); } // Set y range - float newYMin = clipped((yMin+yMax)/2 - NormalizedYHalfRange(), false); - float newYMax = clipped((yMin+yMax)/2 + NormalizedYHalfRange(), true); + float newYMin = clipped(yCenter() - NormalizedYHalfRange(), false); + float newYMax = clipped(yCenter() + NormalizedYHalfRange(), true); if (!std::isnan(newYMin) && !std::isnan(newYMax)) { m_yMax = newYMax; MemoizedCurveViewRange::setYMin(newYMin); diff --git a/apps/shared/zoom_parameter_controller.cpp b/apps/shared/zoom_parameter_controller.cpp index d47e81251..29ecc8c92 100644 --- a/apps/shared/zoom_parameter_controller.cpp +++ b/apps/shared/zoom_parameter_controller.cpp @@ -21,16 +21,12 @@ View * ZoomParameterController::view() { bool ZoomParameterController::handleEvent(Ion::Events::Event event) { if (event == Ion::Events::Plus) { - float meanX = (m_interactiveRange->xMin()+m_interactiveRange->xMax())/2.0f; - float meanY = (m_interactiveRange->yMin()+m_interactiveRange->yMax())/2.0f; - m_interactiveRange->zoom(2.0f/3.0f, meanX, meanY); + m_interactiveRange->zoom(2.0f/3.0f, m_interactiveRange->xCenter(), m_interactiveRange->yCenter()); m_contentView.curveView()->reload(); return true; } if (event == Ion::Events::Minus) { - float meanX = (m_interactiveRange->xMin()+m_interactiveRange->xMax())/2.0f; - float meanY = (m_interactiveRange->yMin()+m_interactiveRange->yMax())/2.0f; - m_interactiveRange->zoom(3.0f/2.0f, meanX, meanY); + m_interactiveRange->zoom(3.0f/2.0f, m_interactiveRange->xCenter(), m_interactiveRange->yCenter()); m_contentView.curveView()->reload(); return true; }