From 66d920a2803e1aa5fc3e671f6d4616559f207dfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Fri, 6 Jul 2018 13:46:12 +0200 Subject: [PATCH] [shared] Factorize GraphController::initCursorParameters of Graph and Sequence --- apps/graph/graph/graph_controller.cpp | 15 --------------- apps/graph/graph/graph_controller.h | 1 - apps/sequence/graph/graph_controller.cpp | 14 ++------------ apps/sequence/graph/graph_controller.h | 2 +- apps/shared/function_graph_controller.cpp | 19 +++++++++++++++++++ apps/shared/function_graph_controller.h | 2 ++ 6 files changed, 24 insertions(+), 29 deletions(-) diff --git a/apps/graph/graph/graph_controller.cpp b/apps/graph/graph/graph_controller.cpp index 1425a7369..086a7ad6f 100644 --- a/apps/graph/graph/graph_controller.cpp +++ b/apps/graph/graph/graph_controller.cpp @@ -79,21 +79,6 @@ bool GraphController::moveCursorHorizontally(int direction) { return privateMoveCursorHorizontally(m_cursor, direction, m_graphRange, k_numberOfCursorStepsInGradUnit, f, myApp, k_cursorTopMarginRatio, k_cursorRightMarginRatio, k_cursorBottomMarginRatio, k_cursorLeftMarginRatio); } -void GraphController::initCursorParameters() { - double x = (interactiveCurveViewRange()->xMin()+interactiveCurveViewRange()->xMax())/2.0f; - TextFieldDelegateApp * myApp = (TextFieldDelegateApp *)app(); - int functionIndex = 0; - double y = 0; - do { - CartesianFunction * firstFunction = functionStore()->activeFunctionAtIndex(functionIndex++); - y = firstFunction->evaluateAtAbscissa(x, myApp->localContext()); - } while ((std::isnan(y) || std::isinf(y)) && functionIndex < functionStore()->numberOfActiveFunctions()); - m_cursor->moveTo(x, y); - functionIndex = (std::isnan(y) || std::isinf(y)) ? 0 : functionIndex - 1; - selectFunctionWithCursor(functionIndex); - interactiveCurveViewRange()->panToMakePointVisible(x, y, k_cursorTopMarginRatio, k_cursorRightMarginRatio, k_cursorBottomMarginRatio, k_cursorLeftMarginRatio); -} - InteractiveCurveViewRange * GraphController::interactiveCurveViewRange() { return m_graphRange; } diff --git a/apps/graph/graph/graph_controller.h b/apps/graph/graph/graph_controller.h index 00f193d4d..47721bb09 100644 --- a/apps/graph/graph/graph_controller.h +++ b/apps/graph/graph/graph_controller.h @@ -26,7 +26,6 @@ private: BannerView * bannerView() override; void reloadBannerView() override; bool moveCursorHorizontally(int direction) override; - void initCursorParameters() override; Shared::InteractiveCurveViewRange * interactiveCurveViewRange() override; CartesianFunctionStore * functionStore() const override; GraphView * functionGraphView() override; diff --git a/apps/sequence/graph/graph_controller.cpp b/apps/sequence/graph/graph_controller.cpp index 6cb709f81..d0ba85018 100644 --- a/apps/sequence/graph/graph_controller.cpp +++ b/apps/sequence/graph/graph_controller.cpp @@ -61,18 +61,8 @@ bool GraphController::moveCursorHorizontally(int direction) { return true; } -void GraphController::initCursorParameters() { - double x = std::round((interactiveCurveViewRange()->xMin()+interactiveCurveViewRange()->xMax())/2.0); - selectFunctionWithCursor(0); - TextFieldDelegateApp * myApp = (TextFieldDelegateApp *)app(); - int functionIndex = 0; - double y = 0; - do { - Sequence * firstFunction = m_sequenceStore->activeFunctionAtIndex(functionIndex++); - y = firstFunction->evaluateAtAbscissa(x, myApp->localContext()); - } while (std::isnan(y) && functionIndex < m_sequenceStore->numberOfActiveFunctions()); - m_cursor->moveTo(x, y); - m_graphRange->panToMakePointVisible(x, y, k_cursorTopMarginRatio, k_cursorRightMarginRatio, k_cursorBottomMarginRatio, k_cursorLeftMarginRatio); +double GraphController::defaultCursorAbscissa() { + return std::round(Shared::FunctionGraphController::defaultCursorAbscissa()); } CurveViewRange * GraphController::interactiveCurveViewRange() { diff --git a/apps/sequence/graph/graph_controller.h b/apps/sequence/graph/graph_controller.h index 0cafb0f07..4ce0dca25 100644 --- a/apps/sequence/graph/graph_controller.h +++ b/apps/sequence/graph/graph_controller.h @@ -21,7 +21,7 @@ private: BannerView * bannerView() override; bool handleEnter() override; bool moveCursorHorizontally(int direction) override; - void initCursorParameters() override; + double defaultCursorAbscissa() override; CurveViewRange * interactiveCurveViewRange() override; SequenceStore * functionStore() const override; GraphView * functionGraphView() override; diff --git a/apps/shared/function_graph_controller.cpp b/apps/shared/function_graph_controller.cpp index f880a53a4..af03faebf 100644 --- a/apps/shared/function_graph_controller.cpp +++ b/apps/shared/function_graph_controller.cpp @@ -108,6 +108,25 @@ void FunctionGraphController::initRangeParameters() { selectFunctionWithCursor(0); } +double FunctionGraphController::defaultCursorAbscissa() { + return (interactiveCurveViewRange()->xMin()+interactiveCurveViewRange()->xMax())/2.0f; +} + +void FunctionGraphController::initCursorParameters() { + double x = defaultCursorAbscissa(); + TextFieldDelegateApp * myApp = (TextFieldDelegateApp *)app(); + int functionIndex = 0; + double y = 0; + do { + Function * firstFunction = functionStore()->activeFunctionAtIndex(functionIndex++); + y = firstFunction->evaluateAtAbscissa(x, myApp->localContext()); + } while ((std::isnan(y) || std::isinf(y)) && functionIndex < functionStore()->numberOfActiveFunctions()); + m_cursor->moveTo(x, y); + functionIndex = (std::isnan(y) || std::isinf(y)) ? 0 : functionIndex - 1; + selectFunctionWithCursor(functionIndex); + interactiveCurveViewRange()->panToMakePointVisible(x, y, k_displayTopMarginRatio, k_cursorRightMarginRatio, k_displayBottomMarginRatio, k_cursorLeftMarginRatio); +} + bool FunctionGraphController::moveCursorVertically(int direction) { Function * actualFunction = functionStore()->activeFunctionAtIndex(indexFunctionSelectedByCursor()); TextFieldDelegateApp * myApp = (TextFieldDelegateApp *)app(); diff --git a/apps/shared/function_graph_controller.h b/apps/shared/function_graph_controller.h index 11840d97e..09eaffdf9 100644 --- a/apps/shared/function_graph_controller.h +++ b/apps/shared/function_graph_controller.h @@ -26,6 +26,7 @@ protected: return *m_indexFunctionSelectedByCursor; } virtual void selectFunctionWithCursor(int functionIndex); + virtual double defaultCursorAbscissa(); private: /* When y auto is ticked, we use a display margin to be ensure that the user * can move the cursor along the curve without panning the window */ @@ -36,6 +37,7 @@ private: float addMargin(float x, float range, bool isMin) override; void initRangeParameters() override; + void initCursorParameters() override; bool moveCursorVertically(int direction) override; CurveView * curveView() override; uint32_t modelVersion() override;