Table events

This commit is contained in:
Bertrand Songis
2020-03-04 18:07:15 +01:00
parent 2957e54a04
commit 8002607b66
2 changed files with 98 additions and 17 deletions
+47
View File
@@ -48,3 +48,50 @@ void Table::Body::paint(BitmapBuffer * dc)
index += 1; index += 1;
} }
} }
#if defined(HARDWARE_TOUCH)
bool Table::Body::onTouchEnd(coord_t x, coord_t y)
{
unsigned index = y / TABLE_LINE_HEIGHT;
if (index < lines.size()) {
auto onPress = lines[index].onPress;
if (onPress)
onPress();
}
return true;
}
#endif
#if defined(HARDWARE_KEYS)
void Table::Body::onEvent(event_t event)
{
TRACE_WINDOWS("%s received event 0x%X", getWindowDebugString().c_str(), event);
if (event == EVT_KEY_BREAK(KEY_ENTER)) {
if (selection >= 0) {
auto onPress = lines[selection].onPress;
if (onPress)
onPress();
}
}
if (event == EVT_ROTARY_RIGHT) {
int newSelection = (selection + 1) % lines.size();
select(newSelection, true);
auto onSelect = lines[selection].onSelect;
if (onSelect) {
onSelect();
}
}
else if (event == EVT_ROTARY_LEFT) {
int newSelection = selection == 0 ? lines.size() - 1 : selection - 1;
select(newSelection, true);
auto onSelect = lines[selection].onSelect;
if (onSelect) {
onSelect();
}
}
else {
Window::onEvent(event);
}
}
#endif
+51 -17
View File
@@ -33,6 +33,7 @@ class Table: public Window {
} }
std::vector<std::string> values; std::vector<std::string> values;
std::function<void()> onPress; std::function<void()> onPress;
std::function<void()> onSelect;
}; };
class Header: public Window { class Header: public Window {
@@ -44,6 +45,13 @@ class Table: public Window {
{ {
} }
#if defined(DEBUG_WINDOWS)
std::string getName() override
{
return "Table::Header";
}
#endif
void setLine(const Line & line) void setLine(const Line & line)
{ {
header = line; header = line;
@@ -66,6 +74,13 @@ class Table: public Window {
{ {
} }
#if defined(DEBUG_WINDOWS)
std::string getName() override
{
return "Table::Body";
}
#endif
void addLine(const Line & line) void addLine(const Line & line)
{ {
lines.push_back(line); lines.push_back(line);
@@ -77,19 +92,29 @@ class Table: public Window {
lines.clear(); lines.clear();
} }
void select(int index, bool scroll)
{
selection = index;
if (scroll) {
const rect_t rect = {
0,
index * TABLE_LINE_HEIGHT,
width(),
TABLE_LINE_HEIGHT
};
scrollTo(rect);
}
invalidate();
}
void paint(BitmapBuffer * dc) override; void paint(BitmapBuffer * dc) override;
#if defined(HARDWARE_KEYS)
void onEvent(event_t event) override;
#endif
#if defined(HARDWARE_TOUCH) #if defined(HARDWARE_TOUCH)
bool onTouchEnd(coord_t x, coord_t y) override bool onTouchEnd(coord_t x, coord_t y) override;
{
unsigned index = y / TABLE_LINE_HEIGHT;
if (index < lines.size()) {
auto onPress = lines[index].onPress;
if (onPress)
onPress();
}
return true;
}
#endif #endif
protected: protected:
@@ -108,6 +133,13 @@ class Table: public Window {
{ {
} }
#if defined(DEBUG_WINDOWS)
std::string getName() override
{
return "Table";
}
#endif
~Table() override ~Table() override
{ {
header.detach(); header.detach();
@@ -132,13 +164,14 @@ class Table: public Window {
body.invalidate(); body.invalidate();
} }
void setSelection(int index, bool scroll = false) void setFocus(uint8_t flag) override
{ {
body.selection = index; body.setFocus(flag);
if (scroll) { }
body.setScrollPositionY(index * TABLE_LINE_HEIGHT);
} void select(int index, bool scroll = true)
body.invalidate(); {
body.select(index, scroll);
} }
void setHeader(const char * const values[]) void setHeader(const char * const values[])
@@ -153,13 +186,14 @@ class Table: public Window {
header.setLine(line); header.setLine(line);
} }
void addLine(const char * const values[], std::function<void()> onPress = nullptr) void addLine(const char * const values[], std::function<void()> onPress = nullptr, std::function<void()> onSelect = nullptr)
{ {
Line line(columnsCount); Line line(columnsCount);
for (uint8_t i = 0; i < columnsCount; i++) { for (uint8_t i = 0; i < columnsCount; i++) {
line.values[i] = values[i]; line.values[i] = values[i];
} }
line.onPress = onPress; line.onPress = onPress;
line.onSelect = onSelect;
body.addLine(line); body.addLine(line);
} }