From 8002607b66fb843d1c32ad3ce121f2f5f0ac8806 Mon Sep 17 00:00:00 2001 From: Bertrand Songis Date: Wed, 4 Mar 2020 18:07:15 +0100 Subject: [PATCH] Table events --- radio/src/thirdparty/libopenui/src/table.cpp | 47 ++++++++++++++ radio/src/thirdparty/libopenui/src/table.h | 68 +++++++++++++++----- 2 files changed, 98 insertions(+), 17 deletions(-) diff --git a/radio/src/thirdparty/libopenui/src/table.cpp b/radio/src/thirdparty/libopenui/src/table.cpp index 8427e5d2c..7966d4847 100644 --- a/radio/src/thirdparty/libopenui/src/table.cpp +++ b/radio/src/thirdparty/libopenui/src/table.cpp @@ -48,3 +48,50 @@ void Table::Body::paint(BitmapBuffer * dc) 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 diff --git a/radio/src/thirdparty/libopenui/src/table.h b/radio/src/thirdparty/libopenui/src/table.h index b2a55a182..06f1cf992 100644 --- a/radio/src/thirdparty/libopenui/src/table.h +++ b/radio/src/thirdparty/libopenui/src/table.h @@ -33,6 +33,7 @@ class Table: public Window { } std::vector values; std::function onPress; + std::function onSelect; }; 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) { 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) { lines.push_back(line); @@ -77,19 +92,29 @@ class Table: public Window { 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; +#if defined(HARDWARE_KEYS) + void onEvent(event_t event) override; +#endif + #if defined(HARDWARE_TOUCH) - 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; - } + bool onTouchEnd(coord_t x, coord_t y) override; #endif protected: @@ -108,6 +133,13 @@ class Table: public Window { { } +#if defined(DEBUG_WINDOWS) + std::string getName() override + { + return "Table"; + } +#endif + ~Table() override { header.detach(); @@ -132,13 +164,14 @@ class Table: public Window { body.invalidate(); } - void setSelection(int index, bool scroll = false) + void setFocus(uint8_t flag) override { - body.selection = index; - if (scroll) { - body.setScrollPositionY(index * TABLE_LINE_HEIGHT); - } - body.invalidate(); + body.setFocus(flag); + } + + void select(int index, bool scroll = true) + { + body.select(index, scroll); } void setHeader(const char * const values[]) @@ -153,13 +186,14 @@ class Table: public Window { header.setLine(line); } - void addLine(const char * const values[], std::function onPress = nullptr) + void addLine(const char * const values[], std::function onPress = nullptr, std::function onSelect = nullptr) { Line line(columnsCount); for (uint8_t i = 0; i < columnsCount; i++) { line.values[i] = values[i]; } line.onPress = onPress; + line.onSelect = onSelect; body.addLine(line); }