From 80e45d368e3fa89136e5ffb52244c67267b965f2 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Mon, 13 Jun 2022 17:58:01 +0200 Subject: [PATCH 1/9] Added isLandscape() --- src/tftinstance.cpp | 2 +- src/tftinstance.h | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/tftinstance.cpp b/src/tftinstance.cpp index 4b6e7b4..9cb17f5 100644 --- a/src/tftinstance.cpp +++ b/src/tftinstance.cpp @@ -2,4 +2,4 @@ namespace espgui { TFT_eSPI tft; -} +} // namespace espgui diff --git a/src/tftinstance.h b/src/tftinstance.h index 87ec58b..07cc6a6 100644 --- a/src/tftinstance.h +++ b/src/tftinstance.h @@ -5,4 +5,8 @@ namespace espgui { extern TFT_eSPI tft; +inline bool isLandscape() +{ + return (tft.getRotation() == 1 || tft.getRotation() == 3); +} } From 0c6f3df4f797359127a6b9089f5c8d771bf3f3ea Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Mon, 13 Jun 2022 17:58:33 +0200 Subject: [PATCH 2/9] Made UI a bit better, fix for landscape --- src/changevaluedisplay.cpp | 19 ++++++++++++++----- src/changevaluedisplay.h | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/changevaluedisplay.cpp b/src/changevaluedisplay.cpp index 0660d78..bd07f64 100644 --- a/src/changevaluedisplay.cpp +++ b/src/changevaluedisplay.cpp @@ -8,15 +8,24 @@ void ChangeValueDisplayInterface::initScreen() { Base::initScreen(); - tft.drawRect(25, 75, 190, 65, TFT_WHITE); + tft.drawRoundRect(35, 65, 190, 65, 8, TFT_WHITE); m_valueLabel.start(); tft.setTextFont(4); tft.setTextColor(TFT_WHITE); - tft.drawString("Change value and", 10, 160); - tft.drawString("press button to", 10, 185); - tft.drawString("confirm and go", 10, 210); - tft.drawString("back.", 10, 235); + if (espgui::isLandscape()) + { + tft.drawString("Change value and press", 10, 152); + tft.drawString("button to confirm and", 10, 177); + tft.drawString("go back", 10, 202); + } + else + { + tft.drawString("Change value and", 10, 160); + tft.drawString("press button to", 10, 185); + tft.drawString("confirm and go", 10, 210); + tft.drawString("back.", 10, 235); + } } template<> diff --git a/src/changevaluedisplay.h b/src/changevaluedisplay.h index ca98003..1988020 100644 --- a/src/changevaluedisplay.h +++ b/src/changevaluedisplay.h @@ -33,7 +33,7 @@ public: virtual void setShownValue(int value) = 0; protected: - Label m_valueLabel{26, 81}; // 188, 53 + Label m_valueLabel{36, 71}; // 188, 53 }; template From 94cb9de981363e841a2c6fbbf3feb8173d05a752 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Mon, 13 Jun 2022 17:59:16 +0200 Subject: [PATCH 3/9] Changed text, added 3 dots, roundRect --- src/changevaluedisplay_ip_address_t.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/changevaluedisplay_ip_address_t.cpp b/src/changevaluedisplay_ip_address_t.cpp index 9e10b64..32c9264 100644 --- a/src/changevaluedisplay_ip_address_t.cpp +++ b/src/changevaluedisplay_ip_address_t.cpp @@ -19,7 +19,7 @@ void ChangeValueDisplay::initScreen() Base::initScreen(); tft.setTextColor(TFT_WHITE); - tft.drawString("Change IP", 0, 50); + tft.drawString("Change IP Address", 0, 50); for(int i = 0; i <= 3; i++) { @@ -30,6 +30,10 @@ void ChangeValueDisplay::initScreen() for (auto &label : m_labels) label.start(); + tft.drawString(".", spacing+boxWidth+spacing/4, y); + tft.drawString(".", spacing*2+boxWidth*2+spacing/4, y); + tft.drawString(".", spacing*3+boxWidth*3+spacing/4, y); + drawRect(m_currentIndex, 1, TFT_YELLOW); drawRect(m_currentIndex, 2, TFT_YELLOW); @@ -108,7 +112,7 @@ void ChangeValueDisplay::buttonReleased(Button button) void ChangeValueDisplay::drawRect(int index, int offset, uint32_t color) const { - tft.drawRect(m_labels[index].x()-offset, m_labels[index].y()-offset, boxWidth+(offset*2), boxHeight+(offset*2), color); + tft.drawRoundRect(m_labels[index].x()-offset, m_labels[index].y()-offset, boxWidth+(offset*2), boxHeight+(offset*2), 3, color); } } // namespace espgui From 35bf2907b34161fa423ca320b8fa06b7d999543d Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Mon, 13 Jun 2022 17:59:44 +0200 Subject: [PATCH 4/9] Changed text, roundRect --- src/changevaluedisplay_chrono.h | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/changevaluedisplay_chrono.h b/src/changevaluedisplay_chrono.h index 96dc67d..6787804 100644 --- a/src/changevaluedisplay_chrono.h +++ b/src/changevaluedisplay_chrono.h @@ -40,7 +40,7 @@ private: T m_value; bool m_pressed{}; - Label m_valueLabel{26, 81}; // 188, 53 + Label m_valueLabel{36, 71}; // 188, 53 }; template @@ -58,15 +58,24 @@ void ChangeValueDisplayChrono::initScreen() { Base::initScreen(); - tft.drawRect(25, 75, 190, 65, TFT_WHITE); + tft.drawRoundRect(32, 65, 190, 34, 8, TFT_WHITE); m_valueLabel.start(); tft.setTextFont(4); tft.setTextColor(TFT_WHITE); - tft.drawString("Change value and", 10, 160); - tft.drawString("press button to", 10, 185); - tft.drawString("confirm and go", 10, 210); - tft.drawString("back.", 10, 235); + if (espgui::isLandscape()) + { + tft.drawString("Change value and press", 10, 152); + tft.drawString("button to confirm and", 10, 177); + tft.drawString("go back", 10, 202); + } + else + { + tft.drawString("Change value and", 10, 160); + tft.drawString("press button to", 10, 185); + tft.drawString("confirm and go", 10, 210); + tft.drawString("back.", 10, 235); + } } template From b372f356acb34d6d1eefd6f83ad4c4a181f796c1 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Mon, 13 Jun 2022 18:00:07 +0200 Subject: [PATCH 5/9] Rounded Rect --- src/menudisplay.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/menudisplay.cpp b/src/menudisplay.cpp index bfdcb68..8e7d39c 100644 --- a/src/menudisplay.cpp +++ b/src/menudisplay.cpp @@ -105,10 +105,11 @@ void MenuDisplay::redraw() int newHighlightedIndex{-1}; const auto drawItemRect = [](const auto &label, const auto color){ - tft.drawRect(5, + tft.drawRoundRect(5, label.y()-1, tft.width() - 10, lineHeight+1, + 5, color); }; From df1ae9ba95dfc7afdda36ba5181c55dd7cd6caf6 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Mon, 13 Jun 2022 18:01:29 +0200 Subject: [PATCH 6/9] Implemented Keyboard; ToDo: Control buttons in vertical mode --- CMakeLists.txt | 1 + src/changevaluedisplay_string.cpp | 64 +++---- src/changevaluedisplay_string.h | 19 +- src/keyboardhelper.h | 285 ++++++++++++++++++++++++++++++ 4 files changed, 331 insertions(+), 38 deletions(-) create mode 100644 src/keyboardhelper.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 96fac73..aa79e03 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,7 @@ set(headers src/graphdisplay.h src/icon.h src/iconinterface.h + src/keyboardhelper.h src/icons/back.h src/icons/checked.h src/icons/unchecked.h diff --git a/src/changevaluedisplay_string.cpp b/src/changevaluedisplay_string.cpp index 82e9f5e..bc0eb4a 100644 --- a/src/changevaluedisplay_string.cpp +++ b/src/changevaluedisplay_string.cpp @@ -1,5 +1,8 @@ #include "changevaluedisplay_string.h" +// 3rdparty lib includes +#include + // local includes #include "tftinstance.h" @@ -8,63 +11,62 @@ void espgui::ChangeValueDisplay::start() Base::start(); m_value = this->getValue(); - - m_pressed = false; } void espgui::ChangeValueDisplay::initScreen() { Base::initScreen(); - tft.drawRect(25, 75, 190, 65, TFT_WHITE); + tft.drawRoundRect(10, 50, tft.width() - 20, 34, 5, TFT_WHITE); m_valueLabel.start(); - tft.setTextFont(4); - tft.setTextColor(TFT_WHITE); - tft.drawString("Change value and", 10, 160); - tft.drawString("press button to", 10, 185); - tft.drawString("confirm and go", 10, 210); - tft.drawString("back.", 10, 235); -} - -void espgui::ChangeValueDisplay::update() -{ - Base::update(); - - if (m_pressed) - { - m_pressed = false; - if (auto result = this->setValue(m_value); result) - confirm(); - else - errorOccured(std::move(result).error()); - } + m_keyboard.start(); } void espgui::ChangeValueDisplay::redraw() { + const auto now = espchrono::millis_clock::now().time_since_epoch().count() / 1000; Base::redraw(); tft.setTextColor(TFT_WHITE, TFT_BLACK); tft.setTextFont(4); m_valueLabel.redraw(m_value); + + tft.drawRect(m_valueLabel.x() + tft.textWidth(m_value) + 3, m_valueLabel.y(), 2, tft.fontHeight(), (now % 1000 < 500) ? TFT_WHITE : TFT_BLACK); +} + +void espgui::ChangeValueDisplay::setShownValue(std::string &&value) +{ + tft.drawRect(m_valueLabel.x() + tft.textWidth(m_value) + 3, m_valueLabel.y(), 2, tft.fontHeight(), TFT_BLACK); + m_value = std::move(value); } void espgui::ChangeValueDisplay::buttonPressed(Button button) { //Base::buttonPressed(button); - - switch (button) - { - case Button::Left: this->back(); break; - case Button::Right: m_pressed = true; break; - default:; - } + m_keyboard.buttonPressed(button); } void espgui::ChangeValueDisplay::buttonReleased(Button button) { //Base::buttonReleased(button); - + m_keyboard.buttonReleased(button); // TODO stop auto scroll } + +void espgui::ChangeValueDisplay::confirmValue() +{ + if (auto result = this->setValue(m_value); result) + confirm(); + else + errorOccured(std::move(result).error()); +} + +void espgui::ChangeValueDisplay::removeLastCharFromShownValue() +{ + if (auto val = this->shownValue(); !val.empty()) + { + val.pop_back(); + this->setShownValue(std::move(val)); + } +} diff --git a/src/changevaluedisplay_string.h b/src/changevaluedisplay_string.h index 803e40f..f968fa6 100644 --- a/src/changevaluedisplay_string.h +++ b/src/changevaluedisplay_string.h @@ -4,11 +4,12 @@ #include // local includes -#include "changevaluedisplay.h" -#include "displaywithtitle.h" -#include "confirminterface.h" #include "backinterface.h" +#include "changevaluedisplay.h" +#include "confirminterface.h" +#include "displaywithtitle.h" #include "errorhandlerinterface.h" +#include "keyboardhelper.h" #include "widgets/label.h" namespace espgui { @@ -29,20 +30,24 @@ public: void start() override; void initScreen() override; - void update() override; void redraw() override; void buttonPressed(Button button) override; void buttonReleased(Button button) override; + void confirmValue(); + const std::string &shownValue() const { return m_value; } - void setShownValue(std::string &&value) { m_value = std::move(value); } + void setShownValue(std::string &&value); + void appendToShownValue(char c) { m_value.push_back(c); } + void appendToShownValue(const std::string &s) { m_value.append(s); } + void removeLastCharFromShownValue(); private: std::string m_value; - bool m_pressed{}; - Label m_valueLabel{26, 81}; // 188, 53 + Label m_valueLabel{12, 55}; // 188, 53 + Keyboard> m_keyboard{*this}; }; } // namespace espgui diff --git a/src/keyboardhelper.h b/src/keyboardhelper.h new file mode 100644 index 0000000..1451504 --- /dev/null +++ b/src/keyboardhelper.h @@ -0,0 +1,285 @@ +#pragma once + +// system includes +#include + +// esp-idf includes +#include + +// 3rdparty lib includes +#include +#include + +// local includes +#include "display.h" +#include "screenmanager.h" +#include "tftinstance.h" + +namespace espgui { +namespace { + constexpr const char * const TAG = "KeyboardHelper"; + // all ascii chars from '!' to '~' + constexpr const char * const KEYBOARD_SCREEN_1 = "\"!$%&/()=?QWERTZUIOPASDFGHJKL YXCVBNM"; + constexpr const char * const KEYBOARD_SCREEN_2 = "1234567890qwertzuiopasdfghjkl yxcvbnm"; + constexpr const char * const KEYBOARD_SCREEN_3 = "#'*+-,.;:_<|>\\@[]^_`{}~\"!$%&/ ()=?+-*"; + constexpr const char * const SHIFT = "Shift"; + constexpr const char * const SPACE = "Space"; + constexpr const char * const BACKSPACE = "Back"; + constexpr const char * const ENTER = "Enter"; +// constexpr const char * const KEYBOARD_SCREEN_3 = "1234567890!\"§$%&/()= ?@€{[]}\\~#+-*_,;.:"; +} // namespace + +template +class Keyboard +{ +public: + explicit Keyboard(TDisplay &display) : m_display(display) {} + + void start() + { + const auto isLandscape = espgui::isLandscape(); + m_keyboard_start_y = isLandscape ? 98 : 120; + + tft.fillRect(1, m_keyboard_start_y - 10, tft.width()-1, tft.height() - m_keyboard_start_y - (isLandscape ? 0 : 50), TFT_BLACK); + tft.drawSunkenRect(1, m_keyboard_start_y - 10, tft.width()-1, tft.height() - m_keyboard_start_y - (isLandscape ? 0 : 50), TFT_WHITE, TFT_GREY, TFT_BLACK); + + updateCharLength(); + drawKeyboard(); + } + + void buttonPressed(Button button) + { + switch (button) + { + case Right: + { + if (m_char_index < m_char_length) + m_display.setShownValue(m_display.shownValue() + m_keyset[m_char_index]); + else if (m_char_index == m_char_length) // shift + { + nextScreen(); + } + else if (m_char_index == m_char_length + 1) // space + { + m_display.setShownValue(m_display.shownValue() + " "); + } + else if (m_char_index == m_char_length + 2) // backspace + { + m_display.removeLastCharFromShownValue(); + } + else if (m_char_index == m_char_length + 3) // enter + { + m_display.confirmValue(); + } + break; + } + case Left: + popScreen(); + return; + case Up: + moveSelectorLeft(); + drawKeyboard(true); + break; + case Down: + moveSelectorRight(); + drawKeyboard(true); + break; + default:; + } + } + + void buttonReleased(Button button) {} + + void moveSelectorUp() + { + m_last_char_index = m_char_index; + + m_char_index -= 10; + if (m_char_index < 0) + m_char_index = (m_char_length + 4) - m_char_index; + } + + void moveSelectorDown() + { + m_last_char_index = m_char_index; + + m_char_index += 10; + if (m_char_index >= (m_char_length + 4)) + m_char_index = m_char_index - (m_char_length + 4); + } + + void moveSelectorLeft() + { + m_last_char_index = m_char_index; + + if (m_char_index == 0) + m_char_index = (m_char_length + 4) - 1; + else + m_char_index--; + } + + void moveSelectorRight() + { + m_last_char_index = m_char_index; + + if (m_char_index == (m_char_length + 4) - 1) + m_char_index = 0; + else + m_char_index++; + } + +private: + void updateCharLength() + { + std::string tmpstr; + + switch (m_current_screen) + { + case Screen::SCREEN_1: + tmpstr = KEYBOARD_SCREEN_1; + break; + case Screen::SCREEN_2: + tmpstr = KEYBOARD_SCREEN_2; + break; + case Screen::SCREEN_3: + tmpstr = KEYBOARD_SCREEN_3; + break; + default: + ESP_LOGE(TAG, "Unknown screen"); + return; + } + + m_keyboard = tmpstr; + cpputils::stringReplaceAll(" ", "", tmpstr); + m_char_length = tmpstr.length(); + m_keyset = tmpstr; + } + + void drawKeyboard(bool dont_draw_string = false) + { + size_t char_index{0}; + std::string keyboard_screen{m_keyboard}; + + std::vector keyboard_lines; + + for (size_t i = 0; i < keyboard_screen.size(); i += 10) + { + std::string line = keyboard_screen.substr(i, 10); + if (cpputils::stringEndsWith(line, " ")) + line.pop_back(); + + keyboard_lines.push_back(line); + } + + const auto datum = tft.getTextDatum(); + tft.setTextDatum(MC_DATUM); + + for (size_t i = 0; i < keyboard_lines.size(); i++) + { tft.setTextColor(TFT_WHITE); + + tft.setTextColor(TFT_GREY); + + const int32_t y = m_keyboard_start_y + (i * tft.fontHeight() + 9); + std::string line = keyboard_lines[i]; + const int16_t x = tft.width() / (line.size() + 1); + for (size_t j = 0; j < line.size(); j++) + { + const std::string _char{line[j]}; + const int32_t x_pos = x * (j + 1); + const int32_t y_pos = y; + + const auto width = tft.textWidth(_char) + 2; + const auto height = tft.fontHeight() + 4; + if (char_index == m_char_index) + tft.drawRoundRect(x_pos-width/2-1, y_pos-height/2, width+2, height-4, 3, TFT_DARKGREY); + + if (char_index == m_last_char_index) + { + tft.drawRoundRect(x_pos-width/2-1, y_pos-height/2, width+2, height-4, 3, TFT_BLACK); + } + + if (!dont_draw_string || char_index == m_char_index || char_index == m_last_char_index) + { + if (char_index == m_char_index || char_index == m_last_char_index) + tft.setTextColor(char_index == m_last_char_index ? TFT_GREY : TFT_WHITE); + + tft.drawString(_char, x_pos, y_pos); + + if (char_index == m_char_index) + tft.setTextColor(TFT_GREY); + } + char_index++; + } + } + + tft.setTextDatum(datum); + + // draw 3 extra buttons, back, space and enter (x=10, x=tft.width()/2, x=tft.width()-10) + const int32_t y = m_keyboard_start_y + (keyboard_lines.size() * tft.fontHeight()); + + // align left (SHIFT, SPACE) + tft.drawRoundRect(15-2, y-1, tft.textWidth(SHIFT)+4, tft.fontHeight()+2, 3, TFT_DARKGREY); + tft.drawRoundRect(30+tft.textWidth(SHIFT)-2, y-1, tft.textWidth(SPACE)+4, tft.fontHeight()+2, 3, TFT_DARKGREY); + + // align right (BACKSPACE, ENTER); align from tft.width() + tft.drawRoundRect(tft.width()-30-tft.textWidth(ENTER)-tft.textWidth(BACKSPACE)-2, y-1, tft.textWidth(BACKSPACE)+4, tft.fontHeight()+2, 3, TFT_DARKGREY); + tft.drawRoundRect(tft.width()-15-tft.textWidth(ENTER)-2, y-1, tft.textWidth(ENTER)+4, tft.fontHeight()+2, 3, TFT_DARKGREY); + + // if (!dont_draw_string) + { + // align left (SHIFT, SPACE) + if (m_char_index == m_char_length) + tft.setTextColor(TFT_BLACK, TFT_WHITE); + else + tft.setTextColor(TFT_WHITE, TFT_BLACK); + tft.drawString(SHIFT, 15, y); + + if (m_char_index == m_char_length + 1) + tft.setTextColor(TFT_BLACK, TFT_WHITE); + else + tft.setTextColor(TFT_WHITE, TFT_BLACK); + tft.drawString(SPACE, 30+tft.textWidth(SHIFT), y); + + // align right (BACKSPACE, ENTER); align from tft.width() + if (m_char_index == m_char_length + 2) + tft.setTextColor(TFT_BLACK, TFT_WHITE); + else + tft.setTextColor(TFT_WHITE, TFT_BLACK); + tft.drawString(BACKSPACE, tft.width()-30-tft.textWidth(ENTER)-tft.textWidth(BACKSPACE), y); + + if (m_char_index == m_char_length + 3) + tft.setTextColor(TFT_BLACK, TFT_WHITE); + else + tft.setTextColor(TFT_WHITE, TFT_BLACK); + tft.drawString(ENTER, tft.width()-15-tft.textWidth(ENTER), y); + } + } + + void nextScreen() + { + m_current_screen = static_cast(static_cast(m_current_screen) + uint8_t{1}); + if (m_current_screen >= Screen::SCREEN_MAX) + m_current_screen = Screen::SCREEN_1; + updateCharLength(); + start(); + } + + int32_t m_char_length{0}; + std::string m_keyboard{KEYBOARD_SCREEN_1}; + std::string m_keyset{}; + + TDisplay &m_display; + int32_t m_keyboard_start_y{0}; + + int32_t m_char_index{10}; + int32_t m_last_char_index{-1}; + + enum class Screen : uint8_t { + SCREEN_1, + SCREEN_2, + SCREEN_3, + SCREEN_MAX + }; + Screen m_current_screen{Screen::SCREEN_2}; +}; +} // namespace espgui From a0890839875aad93264acc9cc5c8a6d7d14bb8ce Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Mon, 13 Jun 2022 23:36:22 +0200 Subject: [PATCH 7/9] Fixed portrait mode --- src/keyboardhelper.h | 113 ++++++++++++++++++++++++++++++------------- 1 file changed, 80 insertions(+), 33 deletions(-) diff --git a/src/keyboardhelper.h b/src/keyboardhelper.h index 1451504..0d06c09 100644 --- a/src/keyboardhelper.h +++ b/src/keyboardhelper.h @@ -26,7 +26,6 @@ namespace { constexpr const char * const SPACE = "Space"; constexpr const char * const BACKSPACE = "Back"; constexpr const char * const ENTER = "Enter"; -// constexpr const char * const KEYBOARD_SCREEN_3 = "1234567890!\"§$%&/()= ?@€{[]}\\~#+-*_,;.:"; } // namespace template @@ -40,8 +39,8 @@ public: const auto isLandscape = espgui::isLandscape(); m_keyboard_start_y = isLandscape ? 98 : 120; - tft.fillRect(1, m_keyboard_start_y - 10, tft.width()-1, tft.height() - m_keyboard_start_y - (isLandscape ? 0 : 50), TFT_BLACK); - tft.drawSunkenRect(1, m_keyboard_start_y - 10, tft.width()-1, tft.height() - m_keyboard_start_y - (isLandscape ? 0 : 50), TFT_WHITE, TFT_GREY, TFT_BLACK); + tft.fillRect(1, m_keyboard_start_y - 10, tft.width()-1, tft.height() - m_keyboard_start_y - (isLandscape ? 0 : 30), TFT_BLACK); + tft.drawSunkenRect(1, m_keyboard_start_y - 10, tft.width()-1, tft.height() - m_keyboard_start_y - (isLandscape ? 0 : 30), TFT_WHITE, TFT_GREY, TFT_BLACK); updateCharLength(); drawKeyboard(); @@ -217,41 +216,89 @@ private: // draw 3 extra buttons, back, space and enter (x=10, x=tft.width()/2, x=tft.width()-10) const int32_t y = m_keyboard_start_y + (keyboard_lines.size() * tft.fontHeight()); - // align left (SHIFT, SPACE) - tft.drawRoundRect(15-2, y-1, tft.textWidth(SHIFT)+4, tft.fontHeight()+2, 3, TFT_DARKGREY); - tft.drawRoundRect(30+tft.textWidth(SHIFT)-2, y-1, tft.textWidth(SPACE)+4, tft.fontHeight()+2, 3, TFT_DARKGREY); - - // align right (BACKSPACE, ENTER); align from tft.width() - tft.drawRoundRect(tft.width()-30-tft.textWidth(ENTER)-tft.textWidth(BACKSPACE)-2, y-1, tft.textWidth(BACKSPACE)+4, tft.fontHeight()+2, 3, TFT_DARKGREY); - tft.drawRoundRect(tft.width()-15-tft.textWidth(ENTER)-2, y-1, tft.textWidth(ENTER)+4, tft.fontHeight()+2, 3, TFT_DARKGREY); - - // if (!dont_draw_string) + if (isLandscape()) { // align left (SHIFT, SPACE) - if (m_char_index == m_char_length) - tft.setTextColor(TFT_BLACK, TFT_WHITE); - else - tft.setTextColor(TFT_WHITE, TFT_BLACK); - tft.drawString(SHIFT, 15, y); - - if (m_char_index == m_char_length + 1) - tft.setTextColor(TFT_BLACK, TFT_WHITE); - else - tft.setTextColor(TFT_WHITE, TFT_BLACK); - tft.drawString(SPACE, 30+tft.textWidth(SHIFT), y); + tft.drawRoundRect(15 - 2, y - 1, tft.textWidth(SHIFT) + 4, tft.fontHeight() + 2, 3, TFT_DARKGREY); + tft.drawRoundRect(30 + tft.textWidth(SHIFT) - 2, y - 1, tft.textWidth(SPACE) + 4, tft.fontHeight() + 2, 3, + TFT_DARKGREY); // align right (BACKSPACE, ENTER); align from tft.width() - if (m_char_index == m_char_length + 2) - tft.setTextColor(TFT_BLACK, TFT_WHITE); - else - tft.setTextColor(TFT_WHITE, TFT_BLACK); - tft.drawString(BACKSPACE, tft.width()-30-tft.textWidth(ENTER)-tft.textWidth(BACKSPACE), y); + tft.drawRoundRect(tft.width() - 30 - tft.textWidth(ENTER) - tft.textWidth(BACKSPACE) - 2, y - 1, + tft.textWidth(BACKSPACE) + 4, tft.fontHeight() + 2, 3, TFT_DARKGREY); + tft.drawRoundRect(tft.width() - 15 - tft.textWidth(ENTER) - 2, y - 1, tft.textWidth(ENTER) + 4, + tft.fontHeight() + 2, 3, TFT_DARKGREY); - if (m_char_index == m_char_length + 3) - tft.setTextColor(TFT_BLACK, TFT_WHITE); - else - tft.setTextColor(TFT_WHITE, TFT_BLACK); - tft.drawString(ENTER, tft.width()-15-tft.textWidth(ENTER), y); + // if (!dont_draw_string) + { + // align left (SHIFT, SPACE) + if (m_char_index == m_char_length) + tft.setTextColor(TFT_BLACK, TFT_WHITE); + else + tft.setTextColor(TFT_WHITE, TFT_BLACK); + tft.drawString(SHIFT, 15, y); + + if (m_char_index == m_char_length + 1) + tft.setTextColor(TFT_BLACK, TFT_WHITE); + else + tft.setTextColor(TFT_WHITE, TFT_BLACK); + tft.drawString(SPACE, 30 + tft.textWidth(SHIFT), y); + + // align right (BACKSPACE, ENTER); align from tft.width() + if (m_char_index == m_char_length + 2) + tft.setTextColor(TFT_BLACK, TFT_WHITE); + else + tft.setTextColor(TFT_WHITE, TFT_BLACK); + tft.drawString(BACKSPACE, tft.width() - 30 - tft.textWidth(ENTER) - tft.textWidth(BACKSPACE), y); + + if (m_char_index == m_char_length + 3) + tft.setTextColor(TFT_BLACK, TFT_WHITE); + else + tft.setTextColor(TFT_WHITE, TFT_BLACK); + tft.drawString(ENTER, tft.width() - 15 - tft.textWidth(ENTER), y); + } + } + else + { + const int32_t y_2 = y + tft.fontHeight() + 4; + // align left (SHIFT, SPACE) + tft.drawRoundRect(15 - 2, y - 1, tft.textWidth(SHIFT) + 4, tft.fontHeight() + 2, 3, TFT_DARKGREY); + tft.drawRoundRect(15 - 2, y_2 - 1, tft.textWidth(SPACE) + 4, tft.fontHeight() + 2, 3, TFT_DARKGREY); + + // align right (BACKSPACE, ENTER); align from tft.width() + tft.drawRoundRect(tft.width() - 15 - tft.textWidth(ENTER) - 2, y - 1, tft.textWidth(ENTER) + 4, + tft.fontHeight() + 2, 3, TFT_DARKGREY); + tft.drawRoundRect(tft.width() - 15 - tft.textWidth(BACKSPACE) - 2, y_2 - 1, tft.textWidth(BACKSPACE) + 4, + tft.fontHeight() + 2, 3, TFT_DARKGREY); + + // if (!dont_draw_string) + { + // align left (SHIFT, SPACE) + if (m_char_index == m_char_length) + tft.setTextColor(TFT_BLACK, TFT_WHITE); + else + tft.setTextColor(TFT_WHITE, TFT_BLACK); + tft.drawString(SHIFT, 15, y); + + if (m_char_index == m_char_length + 1) + tft.setTextColor(TFT_BLACK, TFT_WHITE); + else + tft.setTextColor(TFT_WHITE, TFT_BLACK); + tft.drawString(SPACE, 15, y_2); + + // align right (BACKSPACE, ENTER); align from tft.width() + if (m_char_index == m_char_length + 2) + tft.setTextColor(TFT_BLACK, TFT_WHITE); + else + tft.setTextColor(TFT_WHITE, TFT_BLACK); + tft.drawString(BACKSPACE, tft.width() - 15 - tft.textWidth(BACKSPACE), y_2); + + if (m_char_index == m_char_length + 3) + tft.setTextColor(TFT_BLACK, TFT_WHITE); + else + tft.setTextColor(TFT_WHITE, TFT_BLACK); + tft.drawString(ENTER, tft.width() - 15 - tft.textWidth(ENTER), y); + } } } From 3e06c0869bfbc00f81b0140be86a3d2fbcc0f41b Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 14 Jun 2022 13:24:55 +0200 Subject: [PATCH 8/9] Refactor --- src/keyboardhelper.h | 566 ++++++++++++++++++++++--------------------- 1 file changed, 296 insertions(+), 270 deletions(-) diff --git a/src/keyboardhelper.h b/src/keyboardhelper.h index 0d06c09..f726e42 100644 --- a/src/keyboardhelper.h +++ b/src/keyboardhelper.h @@ -34,282 +34,22 @@ class Keyboard public: explicit Keyboard(TDisplay &display) : m_display(display) {} - void start() - { - const auto isLandscape = espgui::isLandscape(); - m_keyboard_start_y = isLandscape ? 98 : 120; + void start(); - tft.fillRect(1, m_keyboard_start_y - 10, tft.width()-1, tft.height() - m_keyboard_start_y - (isLandscape ? 0 : 30), TFT_BLACK); - tft.drawSunkenRect(1, m_keyboard_start_y - 10, tft.width()-1, tft.height() - m_keyboard_start_y - (isLandscape ? 0 : 30), TFT_WHITE, TFT_GREY, TFT_BLACK); + void buttonPressed(Button button); + void buttonReleased(Button button); - updateCharLength(); - drawKeyboard(); - } - - void buttonPressed(Button button) - { - switch (button) - { - case Right: - { - if (m_char_index < m_char_length) - m_display.setShownValue(m_display.shownValue() + m_keyset[m_char_index]); - else if (m_char_index == m_char_length) // shift - { - nextScreen(); - } - else if (m_char_index == m_char_length + 1) // space - { - m_display.setShownValue(m_display.shownValue() + " "); - } - else if (m_char_index == m_char_length + 2) // backspace - { - m_display.removeLastCharFromShownValue(); - } - else if (m_char_index == m_char_length + 3) // enter - { - m_display.confirmValue(); - } - break; - } - case Left: - popScreen(); - return; - case Up: - moveSelectorLeft(); - drawKeyboard(true); - break; - case Down: - moveSelectorRight(); - drawKeyboard(true); - break; - default:; - } - } - - void buttonReleased(Button button) {} - - void moveSelectorUp() - { - m_last_char_index = m_char_index; - - m_char_index -= 10; - if (m_char_index < 0) - m_char_index = (m_char_length + 4) - m_char_index; - } - - void moveSelectorDown() - { - m_last_char_index = m_char_index; - - m_char_index += 10; - if (m_char_index >= (m_char_length + 4)) - m_char_index = m_char_index - (m_char_length + 4); - } - - void moveSelectorLeft() - { - m_last_char_index = m_char_index; - - if (m_char_index == 0) - m_char_index = (m_char_length + 4) - 1; - else - m_char_index--; - } - - void moveSelectorRight() - { - m_last_char_index = m_char_index; - - if (m_char_index == (m_char_length + 4) - 1) - m_char_index = 0; - else - m_char_index++; - } + void moveSelectorUp(); + void moveSelectorDown(); + void moveSelectorLeft(); + void moveSelectorRight(); private: - void updateCharLength() - { - std::string tmpstr; + void updateCharLength(); - switch (m_current_screen) - { - case Screen::SCREEN_1: - tmpstr = KEYBOARD_SCREEN_1; - break; - case Screen::SCREEN_2: - tmpstr = KEYBOARD_SCREEN_2; - break; - case Screen::SCREEN_3: - tmpstr = KEYBOARD_SCREEN_3; - break; - default: - ESP_LOGE(TAG, "Unknown screen"); - return; - } + void drawKeyboard(bool dont_draw_string = false); - m_keyboard = tmpstr; - cpputils::stringReplaceAll(" ", "", tmpstr); - m_char_length = tmpstr.length(); - m_keyset = tmpstr; - } - - void drawKeyboard(bool dont_draw_string = false) - { - size_t char_index{0}; - std::string keyboard_screen{m_keyboard}; - - std::vector keyboard_lines; - - for (size_t i = 0; i < keyboard_screen.size(); i += 10) - { - std::string line = keyboard_screen.substr(i, 10); - if (cpputils::stringEndsWith(line, " ")) - line.pop_back(); - - keyboard_lines.push_back(line); - } - - const auto datum = tft.getTextDatum(); - tft.setTextDatum(MC_DATUM); - - for (size_t i = 0; i < keyboard_lines.size(); i++) - { tft.setTextColor(TFT_WHITE); - - tft.setTextColor(TFT_GREY); - - const int32_t y = m_keyboard_start_y + (i * tft.fontHeight() + 9); - std::string line = keyboard_lines[i]; - const int16_t x = tft.width() / (line.size() + 1); - for (size_t j = 0; j < line.size(); j++) - { - const std::string _char{line[j]}; - const int32_t x_pos = x * (j + 1); - const int32_t y_pos = y; - - const auto width = tft.textWidth(_char) + 2; - const auto height = tft.fontHeight() + 4; - if (char_index == m_char_index) - tft.drawRoundRect(x_pos-width/2-1, y_pos-height/2, width+2, height-4, 3, TFT_DARKGREY); - - if (char_index == m_last_char_index) - { - tft.drawRoundRect(x_pos-width/2-1, y_pos-height/2, width+2, height-4, 3, TFT_BLACK); - } - - if (!dont_draw_string || char_index == m_char_index || char_index == m_last_char_index) - { - if (char_index == m_char_index || char_index == m_last_char_index) - tft.setTextColor(char_index == m_last_char_index ? TFT_GREY : TFT_WHITE); - - tft.drawString(_char, x_pos, y_pos); - - if (char_index == m_char_index) - tft.setTextColor(TFT_GREY); - } - char_index++; - } - } - - tft.setTextDatum(datum); - - // draw 3 extra buttons, back, space and enter (x=10, x=tft.width()/2, x=tft.width()-10) - const int32_t y = m_keyboard_start_y + (keyboard_lines.size() * tft.fontHeight()); - - if (isLandscape()) - { - // align left (SHIFT, SPACE) - tft.drawRoundRect(15 - 2, y - 1, tft.textWidth(SHIFT) + 4, tft.fontHeight() + 2, 3, TFT_DARKGREY); - tft.drawRoundRect(30 + tft.textWidth(SHIFT) - 2, y - 1, tft.textWidth(SPACE) + 4, tft.fontHeight() + 2, 3, - TFT_DARKGREY); - - // align right (BACKSPACE, ENTER); align from tft.width() - tft.drawRoundRect(tft.width() - 30 - tft.textWidth(ENTER) - tft.textWidth(BACKSPACE) - 2, y - 1, - tft.textWidth(BACKSPACE) + 4, tft.fontHeight() + 2, 3, TFT_DARKGREY); - tft.drawRoundRect(tft.width() - 15 - tft.textWidth(ENTER) - 2, y - 1, tft.textWidth(ENTER) + 4, - tft.fontHeight() + 2, 3, TFT_DARKGREY); - - // if (!dont_draw_string) - { - // align left (SHIFT, SPACE) - if (m_char_index == m_char_length) - tft.setTextColor(TFT_BLACK, TFT_WHITE); - else - tft.setTextColor(TFT_WHITE, TFT_BLACK); - tft.drawString(SHIFT, 15, y); - - if (m_char_index == m_char_length + 1) - tft.setTextColor(TFT_BLACK, TFT_WHITE); - else - tft.setTextColor(TFT_WHITE, TFT_BLACK); - tft.drawString(SPACE, 30 + tft.textWidth(SHIFT), y); - - // align right (BACKSPACE, ENTER); align from tft.width() - if (m_char_index == m_char_length + 2) - tft.setTextColor(TFT_BLACK, TFT_WHITE); - else - tft.setTextColor(TFT_WHITE, TFT_BLACK); - tft.drawString(BACKSPACE, tft.width() - 30 - tft.textWidth(ENTER) - tft.textWidth(BACKSPACE), y); - - if (m_char_index == m_char_length + 3) - tft.setTextColor(TFT_BLACK, TFT_WHITE); - else - tft.setTextColor(TFT_WHITE, TFT_BLACK); - tft.drawString(ENTER, tft.width() - 15 - tft.textWidth(ENTER), y); - } - } - else - { - const int32_t y_2 = y + tft.fontHeight() + 4; - // align left (SHIFT, SPACE) - tft.drawRoundRect(15 - 2, y - 1, tft.textWidth(SHIFT) + 4, tft.fontHeight() + 2, 3, TFT_DARKGREY); - tft.drawRoundRect(15 - 2, y_2 - 1, tft.textWidth(SPACE) + 4, tft.fontHeight() + 2, 3, TFT_DARKGREY); - - // align right (BACKSPACE, ENTER); align from tft.width() - tft.drawRoundRect(tft.width() - 15 - tft.textWidth(ENTER) - 2, y - 1, tft.textWidth(ENTER) + 4, - tft.fontHeight() + 2, 3, TFT_DARKGREY); - tft.drawRoundRect(tft.width() - 15 - tft.textWidth(BACKSPACE) - 2, y_2 - 1, tft.textWidth(BACKSPACE) + 4, - tft.fontHeight() + 2, 3, TFT_DARKGREY); - - // if (!dont_draw_string) - { - // align left (SHIFT, SPACE) - if (m_char_index == m_char_length) - tft.setTextColor(TFT_BLACK, TFT_WHITE); - else - tft.setTextColor(TFT_WHITE, TFT_BLACK); - tft.drawString(SHIFT, 15, y); - - if (m_char_index == m_char_length + 1) - tft.setTextColor(TFT_BLACK, TFT_WHITE); - else - tft.setTextColor(TFT_WHITE, TFT_BLACK); - tft.drawString(SPACE, 15, y_2); - - // align right (BACKSPACE, ENTER); align from tft.width() - if (m_char_index == m_char_length + 2) - tft.setTextColor(TFT_BLACK, TFT_WHITE); - else - tft.setTextColor(TFT_WHITE, TFT_BLACK); - tft.drawString(BACKSPACE, tft.width() - 15 - tft.textWidth(BACKSPACE), y_2); - - if (m_char_index == m_char_length + 3) - tft.setTextColor(TFT_BLACK, TFT_WHITE); - else - tft.setTextColor(TFT_WHITE, TFT_BLACK); - tft.drawString(ENTER, tft.width() - 15 - tft.textWidth(ENTER), y); - } - } - } - - void nextScreen() - { - m_current_screen = static_cast(static_cast(m_current_screen) + uint8_t{1}); - if (m_current_screen >= Screen::SCREEN_MAX) - m_current_screen = Screen::SCREEN_1; - updateCharLength(); - start(); - } + void nextScreen(); int32_t m_char_length{0}; std::string m_keyboard{KEYBOARD_SCREEN_1}; @@ -329,4 +69,290 @@ private: }; Screen m_current_screen{Screen::SCREEN_2}; }; + +template +void Keyboard::moveSelectorUp() +{ + m_last_char_index = m_char_index; + + m_char_index -= 10; + if (m_char_index < 0) + m_char_index = (m_char_length + 4) - m_char_index; +} + +template +void Keyboard::moveSelectorDown() +{ + m_last_char_index = m_char_index; + + m_char_index += 10; + if (m_char_index >= (m_char_length + 4)) + m_char_index = m_char_index - (m_char_length + 4); +} + +template +void Keyboard::nextScreen() +{ + m_current_screen = static_cast(static_cast(m_current_screen) + uint8_t{1}); + if (m_current_screen >= Screen::SCREEN_MAX) + m_current_screen = Screen::SCREEN_1; + updateCharLength(); + start(); +} + +template +void Keyboard::drawKeyboard(bool dont_draw_string) +{ + size_t char_index{0}; + std::string keyboard_screen{m_keyboard}; + + std::vector keyboard_lines; + + for (size_t i = 0; i < keyboard_screen.size(); i += 10) + { + std::string line = keyboard_screen.substr(i, 10); + if (cpputils::stringEndsWith(line, " ")) + line.pop_back(); + + keyboard_lines.push_back(line); + } + + const auto datum = tft.getTextDatum(); + tft.setTextDatum(MC_DATUM); + + for (size_t i = 0; i < keyboard_lines.size(); i++) + { tft.setTextColor(TFT_WHITE); + + tft.setTextColor(TFT_GREY); + + const int32_t y = m_keyboard_start_y + (i * tft.fontHeight() + 9); + std::string line = keyboard_lines[i]; + const int16_t x = tft.width() / (line.size() + 1); + for (size_t j = 0; j < line.size(); j++) + { + const std::string _char{line[j]}; + const int32_t x_pos = x * (j + 1); + const int32_t y_pos = y; + + const auto width = tft.textWidth(_char) + 2; + const auto height = tft.fontHeight() + 4; + if (char_index == m_char_index) + tft.drawRoundRect(x_pos-width/2-1, y_pos-height/2, width+2, height-4, 3, TFT_DARKGREY); + + if (char_index == m_last_char_index) + { + tft.drawRoundRect(x_pos-width/2-1, y_pos-height/2, width+2, height-4, 3, TFT_BLACK); + } + + if (!dont_draw_string || char_index == m_char_index || char_index == m_last_char_index) + { + if (char_index == m_char_index || char_index == m_last_char_index) + tft.setTextColor(char_index == m_last_char_index ? TFT_GREY : TFT_WHITE); + + tft.drawString(_char, x_pos, y_pos); + + if (char_index == m_char_index) + tft.setTextColor(TFT_GREY); + } + char_index++; + } + } + + tft.setTextDatum(datum); + + // draw 3 extra buttons, back, space and enter (x=10, x=tft.width()/2, x=tft.width()-10) + const int32_t y = m_keyboard_start_y + (keyboard_lines.size() * tft.fontHeight()); + + if (isLandscape()) + { + // align left (SHIFT, SPACE) + tft.drawRoundRect(15 - 2, y - 1, tft.textWidth(SHIFT) + 4, tft.fontHeight() + 2, 3, TFT_DARKGREY); + tft.drawRoundRect(30 + tft.textWidth(SHIFT) - 2, y - 1, tft.textWidth(SPACE) + 4, tft.fontHeight() + 2, 3, + TFT_DARKGREY); + + // align right (BACKSPACE, ENTER); align from tft.width() + tft.drawRoundRect(tft.width() - 30 - tft.textWidth(ENTER) - tft.textWidth(BACKSPACE) - 2, y - 1, + tft.textWidth(BACKSPACE) + 4, tft.fontHeight() + 2, 3, TFT_DARKGREY); + tft.drawRoundRect(tft.width() - 15 - tft.textWidth(ENTER) - 2, y - 1, tft.textWidth(ENTER) + 4, + tft.fontHeight() + 2, 3, TFT_DARKGREY); + + // if (!dont_draw_string) + { + // align left (SHIFT, SPACE) + if (m_char_index == m_char_length) + tft.setTextColor(TFT_BLACK, TFT_WHITE); + else + tft.setTextColor(TFT_WHITE, TFT_BLACK); + tft.drawString(SHIFT, 15, y); + + if (m_char_index == m_char_length + 1) + tft.setTextColor(TFT_BLACK, TFT_WHITE); + else + tft.setTextColor(TFT_WHITE, TFT_BLACK); + tft.drawString(SPACE, 30 + tft.textWidth(SHIFT), y); + + // align right (BACKSPACE, ENTER); align from tft.width() + if (m_char_index == m_char_length + 2) + tft.setTextColor(TFT_BLACK, TFT_WHITE); + else + tft.setTextColor(TFT_WHITE, TFT_BLACK); + tft.drawString(BACKSPACE, tft.width() - 30 - tft.textWidth(ENTER) - tft.textWidth(BACKSPACE), y); + + if (m_char_index == m_char_length + 3) + tft.setTextColor(TFT_BLACK, TFT_WHITE); + else + tft.setTextColor(TFT_WHITE, TFT_BLACK); + tft.drawString(ENTER, tft.width() - 15 - tft.textWidth(ENTER), y); + } + } + else + { + const int32_t y_2 = y + tft.fontHeight() + 4; + // align left (SHIFT, SPACE) + tft.drawRoundRect(15 - 2, y - 1, tft.textWidth(SHIFT) + 4, tft.fontHeight() + 2, 3, TFT_DARKGREY); + tft.drawRoundRect(15 - 2, y_2 - 1, tft.textWidth(SPACE) + 4, tft.fontHeight() + 2, 3, TFT_DARKGREY); + + // align right (BACKSPACE, ENTER); align from tft.width() + tft.drawRoundRect(tft.width() - 15 - tft.textWidth(ENTER) - 2, y - 1, tft.textWidth(ENTER) + 4, + tft.fontHeight() + 2, 3, TFT_DARKGREY); + tft.drawRoundRect(tft.width() - 15 - tft.textWidth(BACKSPACE) - 2, y_2 - 1, tft.textWidth(BACKSPACE) + 4, + tft.fontHeight() + 2, 3, TFT_DARKGREY); + + // if (!dont_draw_string) + { + // align left (SHIFT, SPACE) + if (m_char_index == m_char_length) + tft.setTextColor(TFT_BLACK, TFT_WHITE); + else + tft.setTextColor(TFT_WHITE, TFT_BLACK); + tft.drawString(SHIFT, 15, y); + + if (m_char_index == m_char_length + 1) + tft.setTextColor(TFT_BLACK, TFT_WHITE); + else + tft.setTextColor(TFT_WHITE, TFT_BLACK); + tft.drawString(SPACE, 15, y_2); + + // align right (BACKSPACE, ENTER); align from tft.width() + if (m_char_index == m_char_length + 2) + tft.setTextColor(TFT_BLACK, TFT_WHITE); + else + tft.setTextColor(TFT_WHITE, TFT_BLACK); + tft.drawString(BACKSPACE, tft.width() - 15 - tft.textWidth(BACKSPACE), y_2); + + if (m_char_index == m_char_length + 3) + tft.setTextColor(TFT_BLACK, TFT_WHITE); + else + tft.setTextColor(TFT_WHITE, TFT_BLACK); + tft.drawString(ENTER, tft.width() - 15 - tft.textWidth(ENTER), y); + } + } +} + +template +void Keyboard::updateCharLength() +{ + std::string tmpstr; + + switch (m_current_screen) + { + case Screen::SCREEN_1: + tmpstr = KEYBOARD_SCREEN_1; + break; + case Screen::SCREEN_2: + tmpstr = KEYBOARD_SCREEN_2; + break; + case Screen::SCREEN_3: + tmpstr = KEYBOARD_SCREEN_3; + break; + default: + ESP_LOGE(TAG, "Unknown screen"); + return; + } + + m_keyboard = tmpstr; + cpputils::stringReplaceAll(" ", "", tmpstr); + m_char_length = tmpstr.length(); + m_keyset = tmpstr; +} + +template +void Keyboard::moveSelectorRight() +{ + m_last_char_index = m_char_index; + + if (m_char_index == (m_char_length + 4) - 1) + m_char_index = 0; + else + m_char_index++; +} + +template +void Keyboard::moveSelectorLeft() +{ + m_last_char_index = m_char_index; + + if (m_char_index == 0) + m_char_index = (m_char_length + 4) - 1; + else + m_char_index--; +} + +template +void Keyboard::start() +{ + const auto isLandscape = espgui::isLandscape(); + m_keyboard_start_y = isLandscape ? 98 : 120; + + tft.fillRect(1, m_keyboard_start_y - 10, tft.width()-1, tft.height() - m_keyboard_start_y - (isLandscape ? 0 : 30), TFT_BLACK); + tft.drawSunkenRect(1, m_keyboard_start_y - 10, tft.width()-1, tft.height() - m_keyboard_start_y - (isLandscape ? 0 : 30), TFT_WHITE, TFT_GREY, TFT_BLACK); + + updateCharLength(); + drawKeyboard(); +} + +template +void Keyboard::buttonPressed(Button button) +{ + switch (button) + { + case Right: + { + if (m_char_index < m_char_length) + m_display.setShownValue(m_display.shownValue() + m_keyset[m_char_index]); + else if (m_char_index == m_char_length) // shift + { + nextScreen(); + } + else if (m_char_index == m_char_length + 1) // space + { + m_display.setShownValue(m_display.shownValue() + " "); + } + else if (m_char_index == m_char_length + 2) // backspace + { + m_display.removeLastCharFromShownValue(); + } + else if (m_char_index == m_char_length + 3) // enter + { + m_display.confirmValue(); + } + break; + } + case Left: + popScreen(); + return; + case Up: + moveSelectorLeft(); + drawKeyboard(true); + break; + case Down: + moveSelectorRight(); + drawKeyboard(true); + break; + default:; + } +} + +template +void Keyboard::buttonReleased(espgui::Button button) {} } // namespace espgui From 0beee355ed6b5dfc18e6dbdd34b8860b8d3b9704 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 14 Jun 2022 13:51:10 +0200 Subject: [PATCH 9/9] Removed futurecpp.h --- src/keyboardhelper.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/keyboardhelper.h b/src/keyboardhelper.h index f726e42..29de9ad 100644 --- a/src/keyboardhelper.h +++ b/src/keyboardhelper.h @@ -1,8 +1,5 @@ #pragma once -// system includes -#include - // esp-idf includes #include