mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-19 00:37:25 +01:00
206 lines
5.1 KiB
C++
206 lines
5.1 KiB
C++
#include "values_controller.h"
|
|
#include <assert.h>
|
|
|
|
ValuesController::ValuesController(Responder * parentResponder, Graph::FunctionStore * functionStore) :
|
|
HeaderViewController(parentResponder, &m_tableView),
|
|
m_tableView(TableView(this, k_topMargin, k_rightMargin, k_bottomMargin, k_leftMargin)),
|
|
m_activeCellX(0),
|
|
m_activeCellY(-1),
|
|
m_functionStore(functionStore),
|
|
m_interval(Graph::Interval(-1.0f, 1.0f, 0.25f))
|
|
{
|
|
setButtonTitles("Regler l'intervalle", nullptr, nullptr);
|
|
}
|
|
|
|
const char * ValuesController::title() const {
|
|
return "Valeurs";
|
|
}
|
|
|
|
Responder * ValuesController::tabController() const{
|
|
return (parentResponder()->parentResponder());
|
|
}
|
|
|
|
int ValuesController::numberOfRows() {
|
|
return 1 + m_interval.numberOfElements();
|
|
};
|
|
|
|
int ValuesController::numberOfColumns() {
|
|
return 1 + m_functionStore->numberOfActiveFunctions();
|
|
};
|
|
|
|
KDCoordinate ValuesController::rowHeight(int j) {
|
|
return k_cellHeight;
|
|
}
|
|
|
|
KDCoordinate ValuesController::columnWidth(int i) {
|
|
switch (i) {
|
|
case 0:
|
|
return k_abscissaCellWidth;
|
|
default:
|
|
return k_ordinateCellWidth;
|
|
}
|
|
}
|
|
|
|
KDCoordinate ValuesController::cumulatedWidthFromIndex(int i) {
|
|
if (i == 0) {
|
|
return 0;
|
|
} else {
|
|
return k_abscissaCellWidth + (i-1)*k_ordinateCellWidth;
|
|
}
|
|
}
|
|
|
|
KDCoordinate ValuesController::cumulatedHeightFromIndex(int j) {
|
|
return j*k_cellHeight;
|
|
}
|
|
|
|
int ValuesController::indexFromCumulatedWidth(KDCoordinate offsetX) {
|
|
if (offsetX <= k_abscissaCellWidth) {
|
|
return 0;
|
|
} else {
|
|
int index = 0;
|
|
while ((k_abscissaCellWidth+index*k_ordinateCellWidth) <= offsetX) {
|
|
index++;
|
|
}
|
|
return index;
|
|
}
|
|
}
|
|
|
|
int ValuesController::indexFromCumulatedHeight(KDCoordinate offsetY) {
|
|
int index = 0;
|
|
while (index*k_cellHeight <= offsetY) {
|
|
index++;
|
|
}
|
|
return index-1;
|
|
}
|
|
|
|
void ValuesController::setActiveCell(int i, int j) {
|
|
if (i < 0 || i >= numberOfColumns()) {
|
|
return;
|
|
}
|
|
if (j < -1 || j >= numberOfRows()) {
|
|
return;
|
|
}
|
|
|
|
if (m_activeCellY >= 0) {
|
|
EvenOddCell * previousCell = (EvenOddCell *)(m_tableView.cellAtLocation(m_activeCellX, m_activeCellY));
|
|
previousCell->setHighlighted(false);
|
|
}
|
|
|
|
m_activeCellX = i;
|
|
m_activeCellY = j;
|
|
if (m_activeCellY >= 0) {
|
|
m_tableView.scrollToCell(i, j);
|
|
EvenOddCell * cell = (EvenOddCell *)(m_tableView.cellAtLocation(i, j));
|
|
cell->setHighlighted(true);
|
|
}
|
|
}
|
|
|
|
void ValuesController::didBecomeFirstResponder() {
|
|
if (m_activeCellY == -1) {
|
|
setActiveCell(0,0);
|
|
} else {
|
|
if (m_activeCellX < m_functionStore->numberOfActiveFunctions()) {
|
|
setActiveCell(m_activeCellX, m_activeCellY);
|
|
} else {
|
|
setActiveCell(m_functionStore->numberOfActiveFunctions(), m_activeCellY);
|
|
}
|
|
}
|
|
}
|
|
|
|
bool ValuesController::handleEvent(Ion::Events::Event event) {
|
|
if (m_activeCellY == -1) {
|
|
switch (event) {
|
|
case Ion::Events::Event::DOWN_ARROW:
|
|
setSelectedButton(-1);
|
|
setActiveCell(m_activeCellX, m_activeCellY+1);
|
|
return true;
|
|
case Ion::Events::Event::UP_ARROW:
|
|
setSelectedButton(-1);
|
|
app()->setFirstResponder(tabController());
|
|
default:
|
|
return HeaderViewController::handleEvent(event);
|
|
}
|
|
} else {
|
|
switch (event) {
|
|
case Ion::Events::Event::DOWN_ARROW:
|
|
setActiveCell(m_activeCellX, m_activeCellY+1);
|
|
return true;
|
|
case Ion::Events::Event::UP_ARROW:
|
|
setActiveCell(m_activeCellX, m_activeCellY-1);
|
|
if (m_activeCellY == -1) {
|
|
setSelectedButton(0);
|
|
}
|
|
return true;
|
|
case Ion::Events::Event::LEFT_ARROW:
|
|
setActiveCell(m_activeCellX-1, m_activeCellY);
|
|
return true;
|
|
case Ion::Events::Event::RIGHT_ARROW:
|
|
setActiveCell(m_activeCellX+1, m_activeCellY);
|
|
return true;
|
|
case Ion::Events::Event::ENTER:
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
int ValuesController::typeAtLocation(int i, int j) {
|
|
if (j == 0) {
|
|
return 0;
|
|
} else {
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
View * ValuesController::reusableCell(int index, int type) {
|
|
assert(index >= 0);
|
|
switch (type) {
|
|
case 0:
|
|
assert(index < k_maxNumberOfColumns);
|
|
return &m_titleCells[index];
|
|
case 1:
|
|
assert(index < k_maxNumberOfCells);
|
|
return &m_floatCells[index];
|
|
default:
|
|
assert(false);
|
|
break;
|
|
}
|
|
}
|
|
|
|
int ValuesController::reusableCellCount(int type) {
|
|
switch (type) {
|
|
case 0:
|
|
return k_maxNumberOfColumns;
|
|
case 1:
|
|
return k_maxNumberOfCells;
|
|
default:
|
|
assert(false);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void ValuesController::willDisplayCellAtLocation(View * cell, int i, int j) {
|
|
if (j == 0) {
|
|
TitleCell * myCell = (TitleCell *)cell;
|
|
myCell->setEven(j%2 == 0);
|
|
if (i == 0) {
|
|
myCell->setText("x");
|
|
} else {
|
|
Graph::Function * function = m_functionStore->activeFunctionAtIndex(i-1);
|
|
myCell->setText(function->name());
|
|
}
|
|
} else {
|
|
ValueCell * myCell = (ValueCell *)cell;
|
|
if (i == 0){
|
|
myCell->setFloat(m_interval.element(j-1));
|
|
} else {
|
|
Graph::Function * function = m_functionStore->activeFunctionAtIndex(i-1);
|
|
float x = m_interval.element(j-1);
|
|
myCell->setFloat(function->evaluateAtAbscissa(x));
|
|
}
|
|
myCell->setEven(j%2 == 0);
|
|
}
|
|
}
|
|
|