diff --git a/apps/graph/Makefile b/apps/graph/Makefile index d9f7b559d..2cb2d88e3 100644 --- a/apps/graph/Makefile +++ b/apps/graph/Makefile @@ -10,6 +10,7 @@ app_objs += $(addprefix apps/graph/,\ graph/calculation_parameter_controller.o\ graph/curve_parameter_controller.o\ graph/graph_controller.o\ + graph/graph_controller_helper.o\ graph/graph_view.o\ list/list_controller.o\ values/derivative_parameter_controller.o\ diff --git a/apps/graph/graph/graph_controller.cpp b/apps/graph/graph/graph_controller.cpp index 6ffa7a135..cdce83cbf 100644 --- a/apps/graph/graph/graph_controller.cpp +++ b/apps/graph/graph/graph_controller.cpp @@ -85,21 +85,10 @@ void GraphController::reloadBannerView() { } CartesianFunction * f = m_functionStore->activeFunctionAtIndex(m_indexFunctionSelectedByCursor); TextFieldDelegateApp * myApp = (TextFieldDelegateApp *)app(); - char buffer[k_maxNumberOfCharacters+PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)]; - const char * space = " "; - int spaceLength = strlen(space); - if (m_displayDerivativeInBanner || type() == GraphView::Type::Tangent) { - const char * legend = "00(x)="; - int legendLength = strlen(legend); - int numberOfChar = strlcpy(buffer, legend, legendLength+1); - buffer[0] = f->name()[0]; - buffer[1] = '\''; - double y = f->approximateDerivative(m_cursor->x(), myApp->localContext()); - numberOfChar += Complex::convertFloatToText(y, buffer + legendLength, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::MediumNumberOfSignificantDigits), Constant::MediumNumberOfSignificantDigits); - strlcpy(buffer+numberOfChar, space, spaceLength+1); - buffer[k_maxLegendLength] = 0; - m_bannerView.setLegendAtIndex(buffer, 2); - } + reloadDerivativeInBannerViewForCursorOnFunction(m_cursor, f, myApp); + + + char buffer[FunctionBannerDelegate::k_maxNumberOfCharacters+PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)]; if (type() == GraphView::Type::Tangent) { const char * legend = "a="; int legendLength = strlen(legend); @@ -118,15 +107,9 @@ void GraphController::reloadBannerView() { } bool GraphController::moveCursorHorizontally(int direction) { - double xCursorPosition = m_cursor->x(); - double x = direction > 0 ? xCursorPosition + m_graphRange->xGridUnit()/k_numberOfCursorStepsInGradUnit : - xCursorPosition - m_graphRange->xGridUnit()/k_numberOfCursorStepsInGradUnit; CartesianFunction * f = m_functionStore->activeFunctionAtIndex(m_indexFunctionSelectedByCursor); TextFieldDelegateApp * myApp = (TextFieldDelegateApp *)app(); - double y = f->evaluateAtAbscissa(x, myApp->localContext()); - m_cursor->moveTo(x, y); - m_graphRange->panToMakePointVisible(x, y, k_cursorTopMarginRatio, k_cursorRightMarginRatio, k_cursorBottomMarginRatio, k_cursorLeftMarginRatio); - return true; + return privateMoveCursorHorizontally(m_cursor, direction, m_graphRange, k_numberOfCursorStepsInGradUnit, f, myApp, k_cursorTopMarginRatio, k_cursorRightMarginRatio, k_cursorBottomMarginRatio, k_cursorLeftMarginRatio); } void GraphController::initCursorParameters() { diff --git a/apps/graph/graph/graph_controller.h b/apps/graph/graph/graph_controller.h index 3ab7cabe7..75213a816 100644 --- a/apps/graph/graph/graph_controller.h +++ b/apps/graph/graph/graph_controller.h @@ -2,6 +2,7 @@ #define GRAPH_GRAPH_CONTROLLER_H #include "graph_view.h" +#include "graph_controller_helper.h" #include "banner_view.h" #include "curve_parameter_controller.h" #include "../../shared/function_graph_controller.h" @@ -11,7 +12,7 @@ namespace Graph { -class GraphController : public Shared::FunctionGraphController { +class GraphController : public Shared::FunctionGraphController, public GraphControllerHelper { public: GraphController(Responder * parentResponder, CartesianFunctionStore * functionStore, Shared::InteractiveCurveViewRange * curveViewRange, Shared::CurveViewCursor * cursor, uint32_t * modelVersion, uint32_t * rangeVersion, Poincare::Expression::AngleUnit * angleUnitVersion, ButtonRowController * header); const char * title() override; diff --git a/apps/graph/graph/graph_controller_helper.cpp b/apps/graph/graph/graph_controller_helper.cpp new file mode 100644 index 000000000..faa280942 --- /dev/null +++ b/apps/graph/graph/graph_controller_helper.cpp @@ -0,0 +1,34 @@ +#include "graph_controller_helper.h" +#include "../../constant.h" + +using namespace Shared; +using namespace Poincare; + +namespace Graph { + +bool GraphControllerHelper::privateMoveCursorHorizontally(Shared::CurveViewCursor * cursor, int direction, Shared::InteractiveCurveViewRange * range, int numberOfStepsInGradUnit, Shared::Function * function, Shared::TextFieldDelegateApp * app, float cursorTopMarginRatio, float cursorRightMarginRatio, float cursorBottomMarginRatio, float cursorLeftMarginRatio) { + double xCursorPosition = cursor->x(); + double x = direction > 0 ? xCursorPosition + range->xGridUnit()/numberOfStepsInGradUnit : xCursorPosition - range->xGridUnit()/numberOfStepsInGradUnit; + double y = function->evaluateAtAbscissa(x, app->localContext()); + cursor->moveTo(x, y); + range->panToMakePointVisible(x, y, cursorTopMarginRatio, cursorRightMarginRatio, cursorBottomMarginRatio, cursorLeftMarginRatio); + return true; +} + +void GraphControllerHelper::reloadDerivativeInBannerViewForCursorOnFunction(Shared::CurveViewCursor * cursor, CartesianFunction * function, TextFieldDelegateApp * app) { + char buffer[FunctionBannerDelegate::k_maxNumberOfCharacters+PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)]; + const char * space = " "; + int spaceLength = strlen(space); + const char * legend = "00(x)="; + int legendLength = strlen(legend); + int numberOfChar = strlcpy(buffer, legend, legendLength+1); + buffer[0] = function->name()[0]; + buffer[1] = '\''; + double y = function->approximateDerivative(cursor->x(), app->localContext()); + numberOfChar += Complex::convertFloatToText(y, buffer + legendLength, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::MediumNumberOfSignificantDigits), Constant::MediumNumberOfSignificantDigits); + strlcpy(buffer+numberOfChar, space, spaceLength+1); + buffer[Shared::FunctionBannerDelegate::k_maxLegendLength] = 0; + bannerView()->setLegendAtIndex(buffer, 2); +} + +} diff --git a/apps/graph/graph/graph_controller_helper.h b/apps/graph/graph/graph_controller_helper.h new file mode 100644 index 000000000..2fc0960e0 --- /dev/null +++ b/apps/graph/graph/graph_controller_helper.h @@ -0,0 +1,20 @@ +#ifndef GRAPH_GRAPH_CONTROLLER_HELPER_H +#define GRAPH_GRAPH_CONTROLLER_HELPER_H + +#include "../../shared/function_banner_delegate.h" +#include "../../shared/text_field_delegate_app.h" +#include "../../shared/interactive_curve_view_range.h" +#include "../cartesian_function_store.h" + +namespace Graph { + +class GraphControllerHelper { +protected: + bool privateMoveCursorHorizontally(Shared::CurveViewCursor * cursor, int direction, Shared::InteractiveCurveViewRange * range, int numberOfStepsInGradUnit, Shared::Function * function, Shared::TextFieldDelegateApp * app, float cursorTopMarginRatio, float cursorRightMarginRatio, float cursorBottomMarginRatio, float cursorLeftMarginRatio); + void reloadDerivativeInBannerViewForCursorOnFunction(Shared::CurveViewCursor * cursor, CartesianFunction * function, Shared::TextFieldDelegateApp * app); + virtual Shared::BannerView * bannerView() = 0; +}; + +} + +#endif diff --git a/apps/shared/function_banner_delegate.h b/apps/shared/function_banner_delegate.h index 7811f9074..d557110c8 100644 --- a/apps/shared/function_banner_delegate.h +++ b/apps/shared/function_banner_delegate.h @@ -8,9 +8,10 @@ namespace Shared { class FunctionBannerDelegate { -protected: +public: constexpr static int k_maxLegendLength = 14; constexpr static int k_maxNumberOfCharacters = 50; +protected: void reloadBannerViewForCursorOnFunction(CurveViewCursor * cursor, Function * function, char symbol); virtual BannerView * bannerView() = 0; };