From a8a6791af784e9e9afb1f380dce705f87eeb28a9 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Mon, 26 Dec 2022 23:09:48 +0100 Subject: [PATCH] Fixed and improved keyboard --- src/changevaluedisplay_string.cpp | 16 ++-- src/keyboardhelper.h | 119 +++++++++--------------------- src/tftespiimpl.h | 2 +- src/tftinterface.h | 2 +- 4 files changed, 47 insertions(+), 92 deletions(-) diff --git a/src/changevaluedisplay_string.cpp b/src/changevaluedisplay_string.cpp index d372b6e..2fe3895 100644 --- a/src/changevaluedisplay_string.cpp +++ b/src/changevaluedisplay_string.cpp @@ -7,6 +7,8 @@ #include "tftinterface.h" #include "tftcolors.h" +using namespace std::chrono_literals; + void espgui::ChangeValueDisplay::start() { Base::start(); @@ -28,19 +30,23 @@ void espgui::ChangeValueDisplay::initScreen(TftInterface &tft) void espgui::ChangeValueDisplay::redraw(TftInterface &tft) { - const auto now = espchrono::millis_clock::now().time_since_epoch().count() / 1000; Base::redraw(tft); + const auto now_ts = espchrono::millis_clock::now().time_since_epoch(); + const auto char_width = tft.textWidth("A", 4); + const auto maxChars = (tft.width() - 20) / char_width; + const auto string = m_value.substr(std::max(0, static_cast(m_value.size()) - maxChars)); + + m_valueLabel.redraw(tft, string, TFT_WHITE, TFT_BLACK, 4); + if (m_needsClear) { tft.drawRect(m_valueLabel.x() + tft.textWidth(*m_needsClear, 4) + 3, m_valueLabel.y(), 2, tft.fontHeight(4), TFT_BLACK); - m_needsClear = std::nullopt; } - m_valueLabel.redraw(tft, m_value, TFT_WHITE, TFT_BLACK, 4); - - tft.drawRect(m_valueLabel.x() + tft.textWidth(m_value, 4) + 3, m_valueLabel.y(), 2, tft.fontHeight(4), (now % 1000 < 500) ? TFT_WHITE : TFT_BLACK); + // tft.drawRect(m_valueLabel.x() + tft.textWidth(m_value, 4) + 3, m_valueLabel.y(), 2, tft.fontHeight(4), (now % 1000 < 500) ? TFT_WHITE : TFT_BLACK); + tft.drawRect(m_valueLabel.x() + tft.textWidth(string, 4) + 3, m_valueLabel.y(), 2, tft.fontHeight(4), (now_ts % 1000ms < 500ms) ? TFT_WHITE : TFT_BLACK); m_keyboard.redraw(tft); } diff --git a/src/keyboardhelper.h b/src/keyboardhelper.h index 620bd47..5617fc2 100644 --- a/src/keyboardhelper.h +++ b/src/keyboardhelper.h @@ -106,6 +106,8 @@ void Keyboard::nextScreen() template void Keyboard::drawKeyboard(TftInterface &tft, bool dont_draw_string) { + constexpr const int FONT = 4; + size_t char_index{0}; std::string keyboard_screen{m_keyboard}; @@ -120,15 +122,9 @@ void Keyboard::drawKeyboard(TftInterface &tft, bool dont_draw_string) keyboard_lines.push_back(line); } -#if 0 - const auto datum = tft.getTextDatum(); - tft.setTextDatum(MC_DATUM); - for (size_t i = 0; i < keyboard_lines.size(); i++) { - tft.setTextColor(TFT_GREY); - - const int32_t y = m_keyboard_start_y + (i * tft.fontHeight() + 9); + const int32_t y = m_keyboard_start_y + (i * tft.fontHeight(FONT) + 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++) @@ -137,120 +133,73 @@ void Keyboard::drawKeyboard(TftInterface &tft, bool dont_draw_string) 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); + const auto width = tft.textWidth(_char, FONT) + 2; + const auto height = tft.fontHeight(FONT) + 4; - 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); - } + tft.drawRoundRect(x_pos-width/2-1, y_pos-height/2, width+2, height-4, 3, espgui::TFT_BLACK); + + if (char_index == m_char_index) + tft.drawRoundRect(x_pos-width/2-1, y_pos-height/2, width+2, height-4, 3, espgui::TFT_DARKGREY); 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); + tft.drawString(_char, x_pos, y_pos, (char_index == m_char_index ? espgui::TFT_WHITE : espgui::TFT_GREY), espgui::TFT_BLACK, FONT, 4); // 4 = center } 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(4)); + const int32_t y = m_keyboard_start_y + (keyboard_lines.size() * tft.fontHeight(FONT)); if (isLandscape(tft)) { // align left (SHIFT, SPACE) - tft.drawRoundRect(15 - 2, y - 1, tft.textWidth(SHIFT) + 4, tft.fontHeight(4) + 2, 3, TFT_DARKGREY); - tft.drawRoundRect(30 + tft.textWidth(SHIFT, 4) - 2, y - 1, tft.textWidth(SPACE, 4) + 4, tft.fontHeight() + 2, 3, - TFT_DARKGREY); + tft.drawRoundRect(15 - 2, y - 1, tft.textWidth(SHIFT, FONT) + 4, tft.fontHeight(FONT) + 2, 3, espgui::TFT_DARKGREY); + tft.drawRoundRect(30 + tft.textWidth(SHIFT, FONT) - 2, y - 1, tft.textWidth(SPACE, FONT) + 4, tft.fontHeight(FONT) + 2, 3, + espgui::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); + tft.drawRoundRect(tft.width() - 30 - tft.textWidth(ENTER, FONT) - tft.textWidth(BACKSPACE, FONT) - 2, y - 1, + tft.textWidth(BACKSPACE, FONT) + 4, tft.fontHeight(FONT) + 2, 3, espgui::TFT_DARKGREY); + tft.drawRoundRect(tft.width() - 15 - tft.textWidth(ENTER, FONT) - 2, y - 1, tft.textWidth(ENTER, FONT) + 4, + tft.fontHeight(FONT) + 2, 3, espgui::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); + tft.drawString(SHIFT, 15, y, (m_char_index == m_char_length ? espgui::TFT_BLACK : espgui::TFT_WHITE), (m_char_index == m_char_length ? espgui::TFT_WHITE : espgui::TFT_BLACK), FONT); + tft.drawString(SPACE, 30 + tft.textWidth(SHIFT, FONT), y, (m_char_index == m_char_length + 1 ? espgui::TFT_BLACK : espgui::TFT_WHITE), (m_char_index == m_char_length +1 ? espgui::TFT_WHITE : espgui::TFT_BLACK), FONT); // 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); + tft.drawString(BACKSPACE, tft.width() - 30 - tft.textWidth(ENTER, FONT) - tft.textWidth(BACKSPACE, FONT), y, (m_char_index == m_char_length + 2 ? espgui::TFT_BLACK : espgui::TFT_WHITE), (m_char_index == m_char_length + 2 ? espgui::TFT_WHITE : espgui::TFT_BLACK), FONT); + tft.drawString(ENTER, tft.width() - 15 - tft.textWidth(ENTER, FONT), y, (m_char_index == m_char_length + 3 ? espgui::TFT_BLACK : espgui::TFT_WHITE), (m_char_index == m_char_length + 3 ? espgui::TFT_WHITE : espgui::TFT_BLACK), FONT); } } else { - const int32_t y_2 = y + tft.fontHeight() + 4; + const int32_t y_2 = y + tft.fontHeight(FONT) + 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); + tft.drawRoundRect(15 - 2, y - 1, tft.textWidth(SHIFT, FONT) + 4, tft.fontHeight(FONT) + 2, 3, espgui::TFT_DARKGREY); + tft.drawRoundRect(15 - 2, y_2 - 1, tft.textWidth(SPACE, FONT) + 4, tft.fontHeight(FONT) + 2, 3, espgui::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); + tft.drawRoundRect(tft.width() - 15 - tft.textWidth(ENTER, FONT) - 2, y - 1, tft.textWidth(ENTER, FONT) + 4, + tft.fontHeight(FONT) + 2, 3, espgui::TFT_DARKGREY); + tft.drawRoundRect(tft.width() - 15 - tft.textWidth(BACKSPACE, FONT) - 2, y_2 - 1, tft.textWidth(BACKSPACE, FONT) + 4, + tft.fontHeight(FONT) + 2, 3, espgui::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); + tft.drawString(SHIFT, 15, y, (m_char_index == m_char_length ? espgui::TFT_BLACK : espgui::TFT_WHITE), (m_char_index == m_char_length ? espgui::TFT_WHITE : espgui::TFT_BLACK), FONT); + tft.drawString(SPACE, 15, y_2, (m_char_index == m_char_length + 1 ? espgui::TFT_BLACK : espgui::TFT_WHITE), (m_char_index == m_char_length + 1 ? espgui::TFT_WHITE : espgui::TFT_BLACK), FONT); // 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); + tft.drawString(BACKSPACE, tft.width() - 15 - tft.textWidth(BACKSPACE, FONT), y_2, (m_char_index == m_char_length + 2 ? espgui::TFT_BLACK : espgui::TFT_WHITE), (m_char_index == m_char_length + 2 ? espgui::TFT_WHITE : espgui::TFT_BLACK), FONT); + tft.drawString(ENTER, tft.width() - 15 - tft.textWidth(ENTER, FONT), y, (m_char_index == m_char_length + 3 ? espgui::TFT_BLACK : espgui::TFT_WHITE), (m_char_index == m_char_length + 3 ? espgui::TFT_WHITE : espgui::TFT_BLACK), FONT); } } -#endif } template @@ -327,7 +276,7 @@ void Keyboard::redraw(TftInterface &tft) if (m_needsRedraw) { m_needsRedraw = false; - drawKeyboard(tft, true); + drawKeyboard(tft); } } diff --git a/src/tftespiimpl.h b/src/tftespiimpl.h index 5150a49..7ac8129 100644 --- a/src/tftespiimpl.h +++ b/src/tftespiimpl.h @@ -66,7 +66,7 @@ public: int16_t textWidth(std::string_view string, uint8_t font) override { return m_tft.textWidth(string, font); } int16_t fontHeight(int16_t font) override { return m_tft.fontHeight(font); } - int16_t drawString(std::string_view string, int32_t x, int32_t y, uint16_t color, uint16_t bgcolor, uint8_t font) override { return m_tft.drawString(string, x, y, color, bgcolor, font); } + int16_t drawString(std::string_view string, int32_t x, int32_t y, uint16_t color, uint16_t bgcolor, uint8_t font, uint8_t datum = 0) override { return m_tft.drawString(string, x, y, color, bgcolor, font, datum); } int16_t drawCentreString(std::string_view string, int32_t x, int32_t y, uint16_t color, uint16_t bgcolor, uint8_t font) override { return m_tft.drawCentreString(string, x, y, color, bgcolor, font); } int16_t drawRightString(std::string_view string, int32_t x, int32_t y, uint16_t color, uint16_t bgcolor, uint8_t font) override { return m_tft.drawRightString(string, x, y, color, bgcolor, font); } diff --git a/src/tftinterface.h b/src/tftinterface.h index 6955711..e27d95d 100644 --- a/src/tftinterface.h +++ b/src/tftinterface.h @@ -77,7 +77,7 @@ public: // Handle char arrays // Use with setTextDatum() to position string on TFT, and setTextPadding() to blank old displayed strings - virtual int16_t drawString(std::string_view string, int32_t x, int32_t y, uint16_t color, uint16_t bgcolor, uint8_t font) = 0; // Draw string using specifed font number + virtual int16_t drawString(std::string_view string, int32_t x, int32_t y, uint16_t color, uint16_t bgcolor, uint8_t font, uint8_t datum = 0) = 0; // Draw string using specifed font number virtual int16_t drawCentreString(std::string_view string, int32_t x, int32_t y, uint16_t color, uint16_t bgcolor, uint8_t font) = 0; // Deprecated, use setTextDatum() and drawString() virtual int16_t drawRightString(std::string_view string, int32_t x, int32_t y, uint16_t color, uint16_t bgcolor, uint8_t font) = 0; // Deprecated, use setTextDatum() and drawString()