From f7079bf876975dd26f7c461f6d89bcec8bc8c70a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Tue, 29 Nov 2016 16:23:05 +0100 Subject: [PATCH] [apps/graph/graph] Handle the cursor moves in the graph view Change-Id: I5db563e4b88a6dd33dd6fc9ce5a4cf1337d43c31 --- apps/graph/graph/graph_controller.cpp | 22 ++++++++-- apps/graph/graph/graph_view.cpp | 63 ++++++++++++++++++++++++++- apps/graph/graph/graph_view.h | 6 ++- 3 files changed, 84 insertions(+), 7 deletions(-) diff --git a/apps/graph/graph/graph_controller.cpp b/apps/graph/graph/graph_controller.cpp index de1b39db3..97fdce53c 100644 --- a/apps/graph/graph/graph_controller.cpp +++ b/apps/graph/graph/graph_controller.cpp @@ -150,13 +150,27 @@ bool GraphController::handleEvent(Ion::Events::Event event) { m_view.reload(); return true; } - if (event == Ion::Events::OK) { - m_view.moveCursorRight(); + if (event == Ion::Events::Left) { + m_view.moveCursorHorizontally(-2); + return true; + } + if (event == Ion::Events::Right) { + m_view.moveCursorHorizontally(2); return true; } if (event == Ion::Events::Up) { - headerViewController()->setSelectedButton(0); - m_headerSelected = true; + Function * f = m_view.moveCursorUp(); + if (f == nullptr) { + headerViewController()->setSelectedButton(0); + m_headerSelected = true; + } + return true; + } + if (event == Ion::Events::Down) { + Function * f = m_view.moveCursorDown(); + if (f == nullptr) { + return false; + } return true; } return false; diff --git a/apps/graph/graph/graph_view.cpp b/apps/graph/graph/graph_view.cpp index 371bb079e..9f326ca2f 100644 --- a/apps/graph/graph/graph_view.cpp +++ b/apps/graph/graph/graph_view.cpp @@ -2,6 +2,7 @@ #include "../../constant.h" #include #include +#include namespace Graph { @@ -59,6 +60,7 @@ Context * GraphView::context() const { void GraphView::reload() { markRectAsDirty(bounds()); + initCursorPosition(); layoutSubviews(); } @@ -76,12 +78,69 @@ int GraphView::numberOfYLabels() const { return ceilf((max(Axis::Vertical) - min(Axis::Vertical))/(2*m_axisInterval->yScale())); } -void GraphView::moveCursorRight() { - KDPoint offset = KDPoint(2,0); +void GraphView::initCursorPosition() { + float center = (min(Axis::Horizontal)+max(Axis::Horizontal))/2.0f; + m_indexFunctionSelectedByCursor = 0; + Function * firstFunction = m_functionStore->activeFunctionAtIndex(0); + float fCenter = firstFunction->evaluateAtAbscissa(center, m_evaluateContext); + m_cursorPosition = KDPoint(160, floatToPixel(Axis::Vertical, fCenter)); +} + +void GraphView::moveCursorHorizontally(KDCoordinate xOffset) { + KDPoint offset = KDPoint(xOffset,0); m_cursorPosition = m_cursorPosition.translatedBy(offset); + Function * f = m_functionStore->activeFunctionAtIndex(m_indexFunctionSelectedByCursor); + float ordinate = f->evaluateAtAbscissa(pixelToFloat(Axis::Horizontal, m_cursorPosition.x()), m_evaluateContext); + m_cursorPosition = KDPoint(m_cursorPosition.x(), floatToPixel(Axis::Vertical, ordinate)); layoutSubviews(); } +Function * GraphView::moveCursorUp() { + float x = pixelToFloat(Axis::Horizontal, m_cursorPosition.x()); + Function * actualFunction = m_functionStore->activeFunctionAtIndex(m_indexFunctionSelectedByCursor); + float y = actualFunction->evaluateAtAbscissa(x, m_evaluateContext); + Function * nextFunction = actualFunction; + float nextY = FLT_MAX; + for (int i = 0; i < m_functionStore->numberOfActiveFunctions(); i++) { + Function * f = m_functionStore->activeFunctionAtIndex(i); + float newY = f->evaluateAtAbscissa(x, m_evaluateContext); + if (newY > y && newY < nextY) { + m_indexFunctionSelectedByCursor = i; + nextY = newY; + nextFunction = f; + } + } + if (nextFunction == actualFunction) { + return nullptr; + } + m_cursorPosition = KDPoint(m_cursorPosition.x(), floatToPixel(Axis::Vertical, nextY)); + layoutSubviews(); + return nextFunction; +} + +Function * GraphView::moveCursorDown() { + float x = pixelToFloat(Axis::Horizontal, m_cursorPosition.x()); + Function * actualFunction = m_functionStore->activeFunctionAtIndex(m_indexFunctionSelectedByCursor); + float y = actualFunction->evaluateAtAbscissa(x, m_evaluateContext); + Function * nextFunction = actualFunction; + float nextY = -FLT_MAX; + for (int i = 0; i < m_functionStore->numberOfActiveFunctions(); i++) { + Function * f = m_functionStore->activeFunctionAtIndex(i); + float newY = f->evaluateAtAbscissa(x, m_evaluateContext); + if (newY < y && newY > nextY) { + m_indexFunctionSelectedByCursor = i; + nextY = newY; + nextFunction = f; + } + } + if (nextFunction == actualFunction) { + return nullptr; + } + m_cursorPosition = KDPoint(m_cursorPosition.x(), floatToPixel(Axis::Vertical, nextY)); + layoutSubviews(); + return nextFunction; +} + void GraphView::layoutSubviews() { KDRect cursorFrame(m_cursorPosition, 10, 10); m_cursorView.setFrame(cursorFrame); diff --git a/apps/graph/graph/graph_view.h b/apps/graph/graph/graph_view.h index 106845e26..d833a04b3 100644 --- a/apps/graph/graph/graph_view.h +++ b/apps/graph/graph/graph_view.h @@ -30,7 +30,10 @@ public: #endif // void drawRect(KDRect rect) const override; - void moveCursorRight(); + void initCursorPosition(); + void moveCursorHorizontally(KDCoordinate xOffset); + Function * moveCursorUp(); + Function * moveCursorDown(); void setContext(Context * evaluateContext); Context * context() const; void reload(); @@ -79,6 +82,7 @@ private: CursorView m_cursorView; KDPoint m_cursorPosition; + int m_indexFunctionSelectedByCursor; constexpr static KDCoordinate k_labelWidth = 32; constexpr static KDCoordinate k_labelHeight = 12; constexpr static KDCoordinate k_labelMargin = 4;