[apps/home] Cell selection

Change-Id: Iebfd7d6ba2c4ec2ce94929dd039fcbcba324218b
This commit is contained in:
Romain Goyet
2016-09-30 18:42:07 +02:00
parent e1d8289bea
commit e6d3f8a9dc
4 changed files with 73 additions and 3 deletions

View File

@@ -4,11 +4,21 @@
namespace Home {
AppCell::AppCell() :
ChildlessView()
ChildlessView(),
m_active(false)
{
}
void AppCell::setActive(bool active) {
if (m_active != active) {
m_active = active;
markRectAsDirty(bounds());
}
}
void AppCell::drawRect(KDContext * ctx, KDRect rect) const {
KDColor c = m_active ? KDColorRed : KDColorBlack;
ctx->fillRect(KDRect(0,0, 20, 20), c);
/*
KDColor * pixels = (KDColor *)apps_picview_image_raw;
assert(apps_picview_image_raw_len == bounds().width() * bounds().height() * sizeof(KDColor));

View File

@@ -9,6 +9,9 @@ class AppCell : public ChildlessView {
public:
AppCell();
void drawRect(KDContext * ctx, KDRect rect) const override;
void setActive(bool active);
private:
bool m_active;
};
}

View File

@@ -11,16 +11,44 @@ Controller::Controller(Responder * parentResponder) :
{
}
bool Controller::handleEvent(Ion::Events::Event event) {
int nextActiveIndex = 0;
switch (event) {
case Ion::Events::Event::DOWN_ARROW:
nextActiveIndex = m_activeIndex+k_numberOfColumns;
break;
case Ion::Events::Event::UP_ARROW:
nextActiveIndex = m_activeIndex-k_numberOfColumns;
break;
case Ion::Events::Event::LEFT_ARROW:
nextActiveIndex = m_activeIndex-1;
break;
case Ion::Events::Event::RIGHT_ARROW:
nextActiveIndex = m_activeIndex+1;
break;
default:
return false;
}
if (nextActiveIndex < 0) {
nextActiveIndex = 0;
}
if (nextActiveIndex >= k_numberOfApps) {
nextActiveIndex = k_numberOfApps-1;
}
setActiveIndex(nextActiveIndex);
return true;
}
View * Controller::view() {
return &m_tableView;
}
int Controller::numberOfRows() {
return 10;
return ((k_numberOfApps-1)/k_numberOfColumns)+1;
}
int Controller::numberOfColumns() {
return 3;
return k_numberOfColumns;
}
KDCoordinate Controller::cellHeight() {
@@ -39,4 +67,26 @@ int Controller::reusableCellCount() {
return k_maxNumberOfCells;
}
void Controller::setActiveIndex(int index) {
if (m_activeIndex < 0 && m_activeIndex >= k_numberOfApps) {
return;
}
if (index == m_activeIndex) {
return;
}
int j = m_activeIndex/k_numberOfColumns;
int i = m_activeIndex-j*k_numberOfColumns; // Avoid modulo
AppCell * previousCell = (AppCell *)m_tableView.cellAtLocation(i, j);
previousCell->setActive(false);
m_activeIndex = index;
j = m_activeIndex/k_numberOfColumns;
i = m_activeIndex-j*k_numberOfColumns; // Avoid modulo
AppCell * nextCell = (AppCell *)m_tableView.cellAtLocation(i, j);
nextCell->setActive(true);
}
}

View File

@@ -9,8 +9,11 @@ namespace Home {
class Controller : public ViewController, public SimpleTableViewDataSource {
public:
Controller(Responder * parentResponder);
View * view() override;
bool handleEvent(Ion::Events::Event event) override;
virtual int numberOfRows() override;
virtual int numberOfColumns() override;
virtual KDCoordinate cellHeight() override;
@@ -18,9 +21,13 @@ public:
virtual View * reusableCell(int index) override;
virtual int reusableCellCount() override;
private:
void setActiveIndex(int index);
TableView m_tableView;
static constexpr int k_numberOfColumns = 3;
static constexpr int k_numberOfApps = 10;
static constexpr int k_maxNumberOfCells = 16;
AppCell m_cells[k_maxNumberOfCells];
int m_activeIndex;
};
}