-
Notifications
You must be signed in to change notification settings - Fork 12
/
table_controller.cpp
175 lines (152 loc) · 4.75 KB
/
table_controller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "table_controller.h"
#include "app.h"
#include "../apps_container.h"
extern "C" {
#include <assert.h>
}
namespace Atomic {
TableController::ContentView::ContentView(TableController * controller, SelectableTableViewDataSource * selectionDataSource) :
m_selectableTableView(controller, controller, selectionDataSource, controller)
{
m_selectableTableView.setVerticalCellOverlap(0);
m_selectableTableView.setMargins(k_sideMargin, k_sideMargin, k_sideMargin, k_sideMargin);
m_selectableTableView.setBackgroundColor(KDColorWhite);
}
SelectableTableView * TableController::ContentView::selectableTableView() {
return &m_selectableTableView;
}
void TableController::ContentView::drawRect(KDContext * ctx, KDRect rect) const {
ctx->fillRect(bounds(), KDColorWhite);
}
int TableController::ContentView::numberOfSubviews() const {
return 4;
}
View * TableController::ContentView::subviewAtIndex(int index) {
switch(index) {
case 0:
return &m_selectableTableView;
case 1:
return &m_ok;
case 2:
return &m_info;
case 3:
return &m_lines;
default:
assert(false);
return nullptr;
}
}
void TableController::ContentView::layoutSubviews(bool force) {
m_selectableTableView.setFrame(bounds(), force);
m_ok.setFrame(KDRect(295,200, m_ok.minimalSizeForOptimalDisplay()), force);
m_info.setFrame(KDRect(KDPoint(48,15),m_info.minimalSizeForOptimalDisplay()), force);
m_lines.setFrame(KDRect(KDPoint(48, 99), m_lines.minimalSizeForOptimalDisplay()), force);
}
TableController::TableController(Responder * parentResponder, SelectableTableViewDataSource * selectionDataSource) :
ViewController(parentResponder),
m_view(this, selectionDataSource),
m_list(this)
{
}
bool TableController::handleEvent(Ion::Events::Event event) {
if (event == Ion::Events::Right && m_cursor < static_cast<int>(sizeof(atomsdefs) / sizeof(AtomDef) - 1)) {
AtomDef atom = atomsdefs[++m_cursor];
setSelection(atom);
return true;
}
if (event == Ion::Events::Left && m_cursor > 0) {
AtomDef atom = atomsdefs[--m_cursor];
setSelection(atom);
return true;
}
if (event == Ion::Events::Up) {
int row = selectionDataSource()->selectedRow();
int column = selectionDataSource()->selectedColumn();
if (row > 0) {
if (row == 8) {
row--;
}
for(size_t i = 0; i < (sizeof(atomsdefs) / sizeof(AtomDef)); i++) {
AtomDef atom = atomsdefs[i];
if (atom.x == column && atom.y == row-1) {
m_cursor = i;
setSelection(atom);
return true;
}
}
}
return true;
}
if (event == Ion::Events::Down) {
int row = selectionDataSource()->selectedRow();
int column = selectionDataSource()->selectedColumn();
if (row < 9) {
if (row == 6) {row++;}
for(size_t i = 0; i < (sizeof(atomsdefs) / sizeof(AtomDef)); i++) {
AtomDef atom = atomsdefs[i];
if (atom.x == column && atom.y == row+1) {
m_cursor = i;
setSelection(atom);
return true;
}
}
}
}
if (event == Ion::Events::OK || event == Ion::Events::EXE) {
Container::activeApp()->displayModalViewController(&m_list, 0.f, 0.f, Metric::CommonTopMargin, Metric::PopUpLeftMargin, 0, Metric::PopUpRightMargin);
return true;
}
return false;
}
void TableController::didBecomeFirstResponder() {
if (selectionDataSource()->selectedRow() == -1) {
setSelection(atomsdefs[0]);
} else {
setSelection(atomsdefs[m_cursor]);
}
}
View * TableController::view() {
return &m_view;
}
int TableController::numberOfRows() const {
return ((k_maxNumberOfCells - 1) / k_numberOfColumns) + 1;
}
int TableController::numberOfColumns() const {
return k_numberOfColumns;
}
KDCoordinate TableController::cellHeight() {
return k_cellHeight;
}
KDCoordinate TableController::cellWidth() {
return k_cellWidth;
}
HighlightCell * TableController::reusableCell(int index) {
assert(index < k_maxNumberOfCells);
return &m_cells[index];
}
int TableController::reusableCellCount() const {
return k_maxNumberOfCells;
}
void TableController::willDisplayCellAtLocation(HighlightCell * cell, int i, int j) {
AtomicCell* c = static_cast<AtomicCell*>(cell);
for(AtomDef atom : atomsdefs) {
if(atom.x == i && atom.y == j) {
c->setVisible(true);
c->setAtom(atom);
return;
}
}
c->setVisible(false);
}
SelectableTableViewDataSource * TableController::selectionDataSource() const {
return App::app()->snapshot();
}
void TableController::setSelection(AtomDef atom) {
m_view.selectableTableView()->selectCellAtLocation(atom.x,atom.y,false);
m_view.setAtom(atom);
m_list.setAtom(atomsdefs[m_cursor]);
}
StackViewController * TableController::stackController() const {
return (StackViewController *)parentResponder();
}
}