diff --git a/apps/regression/go_to_parameter_controller.cpp b/apps/regression/go_to_parameter_controller.cpp index c27d5109a..31d3b05c3 100644 --- a/apps/regression/go_to_parameter_controller.cpp +++ b/apps/regression/go_to_parameter_controller.cpp @@ -8,10 +8,8 @@ using namespace Poincare; namespace Regression { GoToParameterController::GoToParameterController(Responder * parentResponder, Store * store, CurveViewCursor * cursor) : - FloatParameterController(parentResponder), - m_abscisseCell(MessageTableCellWithEditableText(&m_selectableTableView, this, m_draftTextBuffer)), + Shared::GoToParameterController(parentResponder, store, cursor, I18n::Message::X), m_store(store), - m_cursor(cursor), m_xPrediction(true) { } @@ -27,15 +25,6 @@ const char * GoToParameterController::title() { return "Prediction sachant y"; } -int GoToParameterController::numberOfRows() { - return 2; -} - -float GoToParameterController::previousParameterAtIndex(int index) { - assert(index == 0); - return m_previousParameter; -} - float GoToParameterController::parameterAtIndex(int index) { assert(index == 0); if (m_xPrediction) { @@ -50,33 +39,26 @@ bool GoToParameterController::setParameterAtIndex(int parameterIndex, float f) { app()->displayWarning(I18n::Message::ForbiddenValue); return false; } + float x = m_store->xValueForYValue(f); if (m_xPrediction) { - float y = m_store->yValueForXValue(f); - if (fabsf(y) > k_maxDisplayableFloat) { - app()->displayWarning(I18n::Message::ForbiddenValue); - return false; - } - m_cursor->moveTo(f, y); + x = m_store->yValueForXValue(f); + } + if (fabsf(x) > k_maxDisplayableFloat) { + app()->displayWarning(I18n::Message::ForbiddenValue); + return false; + } + if (isnan(x)) { + app()->displayWarning(I18n::Message::ValueNotReachedByRegression); + return false; + } + if (m_xPrediction) { + m_cursor->moveTo(f, x); } else { - float x = m_store->xValueForYValue(f); - if (fabsf(x) > k_maxDisplayableFloat) { - app()->displayWarning(I18n::Message::ForbiddenValue); - return false; - } m_cursor->moveTo(x, f); } return true; } -HighlightCell * GoToParameterController::reusableParameterCell(int index, int type) { - assert(index == 0); - return &m_abscisseCell; -} - -int GoToParameterController::reusableParameterCellCount(int type) { - return 1; -} - void GoToParameterController::willDisplayCellForIndex(HighlightCell * cell, int index) { if (index == numberOfRows()-1) { return; @@ -90,31 +72,4 @@ void GoToParameterController::willDisplayCellForIndex(HighlightCell * cell, int FloatParameterController::willDisplayCellForIndex(cell, index); } -bool GoToParameterController::textFieldDidFinishEditing(TextField * textField, const char * text) { - AppsContainer * appsContainer = ((TextFieldDelegateApp *)app())->container(); - Context * globalContext = appsContainer->globalContext(); - float floatBody = Expression::parse(text)->approximate(*globalContext); - float parameter = m_store->yValueForXValue(floatBody); - if (!m_xPrediction) { - parameter = m_store->xValueForYValue(floatBody); - } - if (isnan(parameter)) { - app()->displayWarning(I18n::Message::ValueNotReachedByRegression); - return false; - } - return FloatParameterController::textFieldDidFinishEditing(textField, text); -} - -void GoToParameterController::viewWillAppear() { - m_previousParameter = parameterAtIndex(0); -} - -void GoToParameterController::buttonAction() { - m_store->centerAxisAround(CurveViewRange::Axis::X, m_cursor->x()); - m_store->centerAxisAround(CurveViewRange::Axis::Y, m_cursor->y()); - StackViewController * stack = (StackViewController *)parentResponder(); - stack->pop(); - stack->pop(); -} - } diff --git a/apps/regression/go_to_parameter_controller.h b/apps/regression/go_to_parameter_controller.h index 42138f06b..6e20bcbb6 100644 --- a/apps/regression/go_to_parameter_controller.h +++ b/apps/regression/go_to_parameter_controller.h @@ -2,34 +2,21 @@ #define REGRESSION_GO_TO_PARAMETER_CONTROLLER_H #include -#include "../shared/float_parameter_controller.h" -#include "../shared/curve_view_cursor.h" +#include "../shared/go_to_parameter_controller.h" #include "store.h" namespace Regression { -class GoToParameterController : public Shared::FloatParameterController { +class GoToParameterController : public Shared::GoToParameterController { public: GoToParameterController(Responder * parentResponder, Store * store, Shared::CurveViewCursor * cursor); void setXPrediction(bool xPrediction); const char * title() override; - int numberOfRows() override; void willDisplayCellForIndex(HighlightCell * cell, int index) override; - bool textFieldDidFinishEditing(TextField * textField, const char * text) override; - void viewWillAppear() override; private: - constexpr static float k_maxDisplayableFloat = 1E8f; - HighlightCell * reusableParameterCell(int index, int type) override; - int reusableParameterCellCount(int type) override; - float previousParameterAtIndex(int index) override; float parameterAtIndex(int index) override; bool setParameterAtIndex(int parameterIndex, float f) override; - void buttonAction() override; - char m_draftTextBuffer[MessageTableCellWithEditableText::k_bufferLength]; - MessageTableCellWithEditableText m_abscisseCell; - float m_previousParameter; Store * m_store; - Shared::CurveViewCursor * m_cursor; bool m_xPrediction; }; diff --git a/apps/shared/Makefile b/apps/shared/Makefile index 9b0423dd8..88ac41cc6 100644 --- a/apps/shared/Makefile +++ b/apps/shared/Makefile @@ -10,6 +10,7 @@ app_objs += $(addprefix apps/shared/,\ float_parameter_controller.o\ function.o\ function_curve_parameter_controller.o\ + function_go_to_parameter_controller.o\ function_graph_view.o\ function_graph_controller.o\ function_store.o\ diff --git a/apps/shared/float_parameter_controller.h b/apps/shared/float_parameter_controller.h index 25f2e4139..414f8765f 100644 --- a/apps/shared/float_parameter_controller.h +++ b/apps/shared/float_parameter_controller.h @@ -30,6 +30,7 @@ public: protected: int activeCell(); StackViewController * stackController(); + virtual float parameterAtIndex(int index) = 0; SelectableTableView m_selectableTableView; private: constexpr static int k_buttonMargin = 6; @@ -38,7 +39,6 @@ private: virtual HighlightCell * reusableParameterCell(int index, int type) = 0; TextFieldDelegateApp * textFieldDelegateApp() override; virtual float previousParameterAtIndex(int index) = 0; - virtual float parameterAtIndex(int index) = 0; virtual bool setParameterAtIndex(int parameterIndex, float f) = 0; ButtonWithSeparator m_okButton; }; diff --git a/apps/shared/function_curve_parameter_controller.cpp b/apps/shared/function_curve_parameter_controller.cpp index c64b4fdc6..a66a17b5f 100644 --- a/apps/shared/function_curve_parameter_controller.cpp +++ b/apps/shared/function_curve_parameter_controller.cpp @@ -8,7 +8,7 @@ FunctionCurveParameterController::FunctionCurveParameterController(InteractiveCu m_goToCell(MessageTableCellWithChevron(I18n::Message::Goto)), m_selectableTableView(SelectableTableView(this, this, 0, 1, Metric::CommonTopMargin, Metric::CommonRightMargin, Metric::CommonBottomMargin, Metric::CommonLeftMargin)), - m_goToParameterController(GoToParameterController(this, graphRange, cursor, symbol)), + m_goToParameterController(FunctionGoToParameterController(this, graphRange, cursor, symbol)), m_function(nullptr) { } diff --git a/apps/shared/function_curve_parameter_controller.h b/apps/shared/function_curve_parameter_controller.h index 3c9318cf2..587bfbd15 100644 --- a/apps/shared/function_curve_parameter_controller.h +++ b/apps/shared/function_curve_parameter_controller.h @@ -2,7 +2,7 @@ #define SHARED_FUNCTION_CURVE_PARAMETER_CONTROLLER_H #include -#include "go_to_parameter_controller.h" +#include "function_go_to_parameter_controller.h" #include "function.h" #include "curve_view_cursor.h" #include "interactive_curve_view_range.h" @@ -21,7 +21,7 @@ protected: MessageTableCellWithChevron m_goToCell; SelectableTableView m_selectableTableView; private: - GoToParameterController m_goToParameterController; + FunctionGoToParameterController m_goToParameterController; Function * m_function; }; diff --git a/apps/shared/function_go_to_parameter_controller.cpp b/apps/shared/function_go_to_parameter_controller.cpp new file mode 100644 index 000000000..ca00a7cc7 --- /dev/null +++ b/apps/shared/function_go_to_parameter_controller.cpp @@ -0,0 +1,44 @@ +#include "function_go_to_parameter_controller.h" +#include "text_field_delegate_app.h" +#include + +using namespace Poincare; + +namespace Shared { + +FunctionGoToParameterController::FunctionGoToParameterController(Responder * parentResponder, InteractiveCurveViewRange * graphRange, CurveViewCursor * cursor, I18n::Message symbol) : + GoToParameterController(parentResponder, graphRange, cursor, symbol), + m_function(nullptr) +{ +} + +const char * FunctionGoToParameterController::title() { + return I18n::translate(I18n::Message::Goto); +} + +float FunctionGoToParameterController::parameterAtIndex(int index) { + assert(index == 0); + return m_cursor->x(); +} + +bool FunctionGoToParameterController::setParameterAtIndex(int parameterIndex, float f) { + assert(parameterIndex == 0); + TextFieldDelegateApp * myApp = (TextFieldDelegateApp *)app(); + float y = m_function->evaluateAtAbscissa(f, myApp->localContext()); + if (fabsf(f) > k_maxDisplayableFloat || fabsf(y) > k_maxDisplayableFloat) { + app()->displayWarning(I18n::Message::ForbiddenValue); + return false; + } + if (isnan(y) || isinf(y)) { + app()->displayWarning(I18n::Message::ValueNotReachedByFunction); + return false; + } + m_cursor->moveTo(f, y); + return true; +} + +void FunctionGoToParameterController::setFunction(Function * function) { + m_function = function; +} + +} diff --git a/apps/shared/function_go_to_parameter_controller.h b/apps/shared/function_go_to_parameter_controller.h new file mode 100644 index 000000000..4d9af0bea --- /dev/null +++ b/apps/shared/function_go_to_parameter_controller.h @@ -0,0 +1,22 @@ +#ifndef SHARED_FUNCTION_GO_TO_PARAMETER_CONTROLLER_H +#define SHARED_FUNCTION_GO_TO_PARAMETER_CONTROLLER_H + +#include "go_to_parameter_controller.h" +#include "function.h" + +namespace Shared { + +class FunctionGoToParameterController : public GoToParameterController { +public: + FunctionGoToParameterController(Responder * parentResponder, InteractiveCurveViewRange * graphRange, CurveViewCursor * cursor, I18n::Message symbol); + const char * title() override; + void setFunction(Function * function); +private: + float parameterAtIndex(int index) override; + bool setParameterAtIndex(int parameterIndex, float f) override; + Function * m_function; +}; + +} + +#endif diff --git a/apps/shared/go_to_parameter_controller.cpp b/apps/shared/go_to_parameter_controller.cpp index 760d4195a..b6183e986 100644 --- a/apps/shared/go_to_parameter_controller.cpp +++ b/apps/shared/go_to_parameter_controller.cpp @@ -6,17 +6,12 @@ namespace Shared { GoToParameterController::GoToParameterController(Responder * parentResponder, InteractiveCurveViewRange * graphRange, CurveViewCursor * cursor, I18n::Message symbol) : FloatParameterController(parentResponder), - m_abscisseCell(MessageTableCellWithEditableText(&m_selectableTableView, this, m_draftTextBuffer, symbol)), - m_graphRange(graphRange), m_cursor(cursor), - m_function(nullptr) + m_abscisseCell(MessageTableCellWithEditableText(&m_selectableTableView, this, m_draftTextBuffer, symbol)), + m_graphRange(graphRange) { } -const char * GoToParameterController::title() { - return I18n::translate(I18n::Message::Goto); -} - void GoToParameterController::viewWillAppear() { m_previousParameter = parameterAtIndex(0); FloatParameterController::viewWillAppear(); @@ -31,23 +26,6 @@ float GoToParameterController::previousParameterAtIndex(int index) { return m_previousParameter; } -float GoToParameterController::parameterAtIndex(int index) { - assert(index == 0); - return m_cursor->x(); -} - -bool GoToParameterController::setParameterAtIndex(int parameterIndex, float f) { - assert(parameterIndex == 0); - TextFieldDelegateApp * myApp = (TextFieldDelegateApp *)app(); - float y = m_function->evaluateAtAbscissa(f, myApp->localContext()); - if (fabsf(f) > k_maxDisplayableFloat || fabsf(y) > k_maxDisplayableFloat) { - app()->displayWarning(I18n::Message::ForbiddenValue); - return false; - } - m_cursor->moveTo(f, y); - return true; -} - HighlightCell * GoToParameterController::reusableParameterCell(int index, int type) { assert(index == 0); return &m_abscisseCell; @@ -57,10 +35,6 @@ int GoToParameterController::reusableParameterCellCount(int type) { return 1; } -void GoToParameterController::setFunction(Function * function) { - m_function = function; -} - void GoToParameterController::buttonAction() { m_graphRange->centerAxisAround(CurveViewRange::Axis::X, m_cursor->x()); m_graphRange->centerAxisAround(CurveViewRange::Axis::Y, m_cursor->y()); diff --git a/apps/shared/go_to_parameter_controller.h b/apps/shared/go_to_parameter_controller.h index 209e85390..3f9bc937e 100644 --- a/apps/shared/go_to_parameter_controller.h +++ b/apps/shared/go_to_parameter_controller.h @@ -5,31 +5,26 @@ #include "float_parameter_controller.h" #include "curve_view_cursor.h" #include "interactive_curve_view_range.h" -#include "function.h" namespace Shared { class GoToParameterController : public FloatParameterController { public: GoToParameterController(Responder * parentResponder, InteractiveCurveViewRange * graphRange, CurveViewCursor * cursor, I18n::Message symbol); - const char * title() override; void viewWillAppear() override; - void setFunction(Function * function); int numberOfRows() override; -private: +protected: constexpr static float k_maxDisplayableFloat = 1E8f; + CurveViewCursor * m_cursor; +private: void buttonAction() override; HighlightCell * reusableParameterCell(int index, int type) override; int reusableParameterCellCount(int type) override; float previousParameterAtIndex(int index) override; - float parameterAtIndex(int index) override; - bool setParameterAtIndex(int parameterIndex, float f) override; char m_draftTextBuffer[MessageTableCellWithEditableText::k_bufferLength]; MessageTableCellWithEditableText m_abscisseCell; float m_previousParameter; InteractiveCurveViewRange * m_graphRange; - CurveViewCursor * m_cursor; - Function * m_function; }; }