This commit is contained in:
CommanderRedYT
2022-06-14 13:24:55 +02:00
parent a089083987
commit 3e06c0869b

View File

@ -34,128 +34,75 @@ class Keyboard
public: public:
explicit Keyboard(TDisplay &display) : m_display(display) {} explicit Keyboard(TDisplay &display) : m_display(display) {}
void start() 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 : 30), TFT_BLACK); void buttonPressed(Button button);
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 buttonReleased(Button button);
updateCharLength(); void moveSelectorUp();
drawKeyboard(); void moveSelectorDown();
} void moveSelectorLeft();
void moveSelectorRight();
void buttonPressed(Button button) private:
{ void updateCharLength();
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 drawKeyboard(bool dont_draw_string = false);
void moveSelectorUp() void nextScreen();
{
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};
};
template<typename TDisplay>
void Keyboard<TDisplay>::moveSelectorUp()
{
m_last_char_index = m_char_index; m_last_char_index = m_char_index;
m_char_index -= 10; m_char_index -= 10;
if (m_char_index < 0) if (m_char_index < 0)
m_char_index = (m_char_length + 4) - m_char_index; m_char_index = (m_char_length + 4) - m_char_index;
} }
void moveSelectorDown() template<typename TDisplay>
{ void Keyboard<TDisplay>::moveSelectorDown()
{
m_last_char_index = m_char_index; m_last_char_index = m_char_index;
m_char_index += 10; m_char_index += 10;
if (m_char_index >= (m_char_length + 4)) if (m_char_index >= (m_char_length + 4))
m_char_index = m_char_index - (m_char_length + 4); m_char_index = m_char_index - (m_char_length + 4);
} }
void moveSelectorLeft() template<typename TDisplay>
{ void Keyboard<TDisplay>::nextScreen()
m_last_char_index = m_char_index; {
m_current_screen = static_cast<Screen>(static_cast<uint8_t>(m_current_screen) + uint8_t{1});
if (m_current_screen >= Screen::SCREEN_MAX)
m_current_screen = Screen::SCREEN_1;
updateCharLength();
start();
}
if (m_char_index == 0) template<typename TDisplay>
m_char_index = (m_char_length + 4) - 1; void Keyboard<TDisplay>::drawKeyboard(bool dont_draw_string)
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}; size_t char_index{0};
std::string keyboard_screen{m_keyboard}; std::string keyboard_screen{m_keyboard};
@ -300,33 +247,112 @@ private:
tft.drawString(ENTER, tft.width() - 15 - tft.textWidth(ENTER), y); tft.drawString(ENTER, tft.width() - 15 - tft.textWidth(ENTER), y);
} }
} }
} }
void nextScreen() template<typename TDisplay>
void Keyboard<TDisplay>::updateCharLength()
{
std::string tmpstr;
switch (m_current_screen)
{ {
m_current_screen = static_cast<Screen>(static_cast<uint8_t>(m_current_screen) + uint8_t{1}); case Screen::SCREEN_1:
if (m_current_screen >= Screen::SCREEN_MAX) tmpstr = KEYBOARD_SCREEN_1;
m_current_screen = Screen::SCREEN_1; break;
updateCharLength(); case Screen::SCREEN_2:
start(); tmpstr = KEYBOARD_SCREEN_2;
break;
case Screen::SCREEN_3:
tmpstr = KEYBOARD_SCREEN_3;
break;
default:
ESP_LOGE(TAG, "Unknown screen");
return;
} }
int32_t m_char_length{0}; m_keyboard = tmpstr;
std::string m_keyboard{KEYBOARD_SCREEN_1}; cpputils::stringReplaceAll(" ", "", tmpstr);
std::string m_keyset{}; m_char_length = tmpstr.length();
m_keyset = tmpstr;
}
TDisplay &m_display; template<typename TDisplay>
int32_t m_keyboard_start_y{0}; void Keyboard<TDisplay>::moveSelectorRight()
{
m_last_char_index = m_char_index;
int32_t m_char_index{10}; if (m_char_index == (m_char_length + 4) - 1)
int32_t m_last_char_index{-1}; m_char_index = 0;
else
m_char_index++;
}
enum class Screen : uint8_t { template<typename TDisplay>
SCREEN_1, void Keyboard<TDisplay>::moveSelectorLeft()
SCREEN_2, {
SCREEN_3, m_last_char_index = m_char_index;
SCREEN_MAX
}; if (m_char_index == 0)
Screen m_current_screen{Screen::SCREEN_2}; m_char_index = (m_char_length + 4) - 1;
}; else
m_char_index--;
}
template<typename TDisplay>
void Keyboard<TDisplay>::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<typename TDisplay>
void Keyboard<TDisplay>::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<typename TDisplay>
void Keyboard<TDisplay>::buttonReleased(espgui::Button button) {}
} // namespace espgui } // namespace espgui