diff --git a/apps/sequence/Makefile b/apps/sequence/Makefile index 1dff36fec..c7422d182 100644 --- a/apps/sequence/Makefile +++ b/apps/sequence/Makefile @@ -5,6 +5,7 @@ app_objs += $(addprefix apps/sequence/,\ graph/curve_view_range.o\ graph/graph_controller.o\ graph/graph_view.o\ + graph/term_sum_controller.o\ graph/vertical_cursor_view.o\ list/list_controller.o\ list/list_parameter_controller.o\ diff --git a/apps/sequence/graph/term_sum_controller.cpp b/apps/sequence/graph/term_sum_controller.cpp new file mode 100644 index 000000000..2a95ebb04 --- /dev/null +++ b/apps/sequence/graph/term_sum_controller.cpp @@ -0,0 +1,147 @@ +#include "term_sum_controller.h" +#include "../../shared/text_field_delegate.h" +#include +#include + +using namespace Shared; + +namespace Sequence { + +TermSumController::TermSumController(Responder * parentResponder, GraphView * graphView, CurveViewRange * graphRange, CurveViewCursor * cursor) : + ViewController(parentResponder), + m_contentView(ContentView(graphView)), + m_graphRange(graphRange), + m_sequence(nullptr), + m_cursor(cursor), + m_cursorView(VerticalCursorView()) +{ +} + +const char * TermSumController::title() const { + return "Sommes des termes"; +} + +View * TermSumController::view() { + return &m_contentView; +} + +void TermSumController::viewWillAppear() { + m_contentView.graphView()->setVerticalCursor(true); + m_contentView.graphView()->setCursorView(&m_cursorView); + m_contentView.graphView()->setBannerView(nullptr); + m_contentView.graphView()->selectMainView(true); + m_contentView.graphView()->reload(); + m_contentView.layoutSubviews(); +} + +bool TermSumController::handleEvent(Ion::Events::Event event) { + if (event == Ion::Events::Left) { + if (step > 0) { + return false; + } + if (moveCursorHorizontallyToPosition(roundf(m_cursor->x()-1.0f))) { + m_contentView.graphView()->reload(); + return true; + } + return false; + } + if (event == Ion::Events::Right) { + if (moveCursorHorizontallyToPosition(roundf(m_cursor->x()+1.0f))) { + m_contentView.graphView()->reload(); + return true; + } + return false; + } + if (event.hasText() && event.text()[0] >= '0' && event.text()[0] <= '9') { + if (step > 0 && event.text()[0]-'0' < m_cursor->x()) { + return false; + } + if (moveCursorHorizontallyToPosition(event.text()[0]-'0')) { + m_contentView.graphView()->reload(); + return true; + } + return false; + } + + return false; +} + +bool TermSumController::moveCursorHorizontallyToPosition(int position) { + if (position < 0) { + return false; + } + float x = position; + TextFieldDelegateApp * myApp = (TextFieldDelegateApp *)app(); + if (m_sequence == nullptr) { + return false; + } + float y = m_sequence->evaluateAtAbscissa(x, myApp->localContext()); + m_cursor->moveTo(x, y); + m_graphRange->panToMakePointVisible(x, y, k_cursorTopMarginRatio, k_cursorRightMarginRatio, k_cursorBottomMarginRatio, k_cursorLeftMarginRatio); + return true; +} + +void TermSumController::setSequence(Sequence * sequence) { + m_sequence = sequence; +} + +/* Content View */ + +TermSumController::ContentView::ContentView(GraphView * graphView) : + m_graphView(graphView) +{ +} + +int TermSumController::ContentView::numberOfSubviews() const { + return 2; +} + +View * TermSumController::ContentView::subviewAtIndex(int index) { + assert(index >= 0 && index < 2); + if (index == 0) { + return m_graphView; + } + return &m_legendView; +} + +void TermSumController::ContentView::layoutSubviews() { + m_graphView->setFrame(bounds()); + m_legendView.setFrame(KDRect(0, bounds().height() - k_legendHeight, bounds().width(), k_legendHeight)); +} + +GraphView * TermSumController::ContentView::graphView() { + return m_graphView; +} + +/* Legend View */ + +TermSumController::ContentView::LegendView::LegendView() : + m_sum(ExpressionView(0.0f, 0.5f, KDColorBlack, Palette::GreyBright)), + m_legend(PointerTextView(KDText::FontSize::Small, "SELECTIONNER LE PREMIER TERME", 0.0f, 0.5f, KDColorBlack, Palette::GreyBright)) +{ +} + +void TermSumController::ContentView::LegendView::drawRect(KDContext * ctx, KDRect rect) const { + ctx->fillRect(KDRect(0, bounds().height() - k_legendHeight, bounds().width(), k_legendHeight), Palette::GreyBright); +} + +int TermSumController::ContentView::LegendView::numberOfSubviews() const { + return 2; +} + +View * TermSumController::ContentView::LegendView::subviewAtIndex(int index) { + assert(index >= 0 && index < 2); + if (index == 0) { + return &m_sum; + } + return &m_legend; +} + +void TermSumController::ContentView::LegendView::layoutSubviews() { + KDCoordinate width = bounds().width(); + KDCoordinate heigth = bounds().height(); + m_sum.setFrame(KDRect(0, 0, width/2, heigth)); + m_legend.setFrame(KDRect(width/2, 0, width/2, heigth)); +} + +} diff --git a/apps/sequence/graph/term_sum_controller.h b/apps/sequence/graph/term_sum_controller.h new file mode 100644 index 000000000..0acba1a81 --- /dev/null +++ b/apps/sequence/graph/term_sum_controller.h @@ -0,0 +1,59 @@ +#ifndef SEQUENCE_TERM_SUM_CONTROLLER_H +#define SEQUENCE_TERM_SUM_CONTROLLER_H + +#include +#include "graph_view.h" +#include "curve_view_range.h" +#include "vertical_cursor_view.h" +#include "../../shared/curve_view_cursor.h" + +namespace Sequence { + +class TermSumController : public ViewController { +public: + TermSumController(Responder * parentResponder, GraphView * graphView, CurveViewRange * graphRange, Shared::CurveViewCursor * cursor); + const char * title() const override; + View * view() override; + void viewWillAppear() override; + bool handleEvent(Ion::Events::Event event) override; + bool moveCursorHorizontallyToPosition(int position); + void setSequence(Sequence * sequence); +private: + constexpr static float k_cursorTopMarginRatio = 0.07f; // (cursorHeight/2)/graphViewHeight + constexpr static float k_cursorRightMarginRatio = 0.04f; // (cursorWidth/2)/graphViewWidth + constexpr static float k_cursorBottomMarginRatio = 0.15f; // (cursorHeight/2+bannerHeigh)/graphViewHeight + constexpr static float k_cursorLeftMarginRatio = 0.04f; // (cursorWidth/2)/graphViewWidth + class ContentView : public View { + public: + ContentView(GraphView * graphView); + void layoutSubviews() override; + GraphView * graphView(); + private: + class LegendView : public View { + public: + LegendView(); + void drawRect(KDContext * ctx, KDRect rect) const override; + private: + void layoutSubviews() override; + int numberOfSubviews() const override; + View * subviewAtIndex(int index) override; + ExpressionView m_sum; + PointerTextView m_legend; + }; + int numberOfSubviews() const override; + View * subviewAtIndex(int index) override; + GraphView * m_graphView; + LegendView m_legendView; + constexpr static KDCoordinate k_legendHeight = 28; + }; + ContentView m_contentView; + CurveViewRange * m_graphRange; + Sequence * m_sequence; + Shared::CurveViewCursor * m_cursor; + VerticalCursorView m_cursorView; + int step; +}; + +} + +#endif