From 3e06c0869bfbc00f81b0140be86a3d2fbcc0f41b Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Tue, 14 Jun 2022 13:24:55 +0200 Subject: [PATCH] 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