mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-29 03:29:58 +02:00
[apps/graph/graph] Handle the cursor moves in the graph view
Change-Id: I5db563e4b88a6dd33dd6fc9ce5a4cf1337d43c31
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "../../constant.h"
|
||||
#include <assert.h>
|
||||
#include <math.h>
|
||||
#include <float.h>
|
||||
|
||||
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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user