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

View File

@ -34,62 +34,44 @@ 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 moveSelectorUp();
void moveSelectorDown();
void moveSelectorLeft();
void moveSelectorRight();
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:;
}
}
private:
void updateCharLength();
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;
@ -98,7 +80,8 @@ public:
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;
@ -107,54 +90,18 @@ public:
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;
if (m_char_index == 0)
m_char_index = (m_char_length + 4) - 1;
else
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();
}
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)
template<typename TDisplay>
void Keyboard<TDisplay>::drawKeyboard(bool dont_draw_string)
{
size_t char_index{0};
std::string keyboard_screen{m_keyboard};
@ -302,31 +249,110 @@ private:
}
}
void nextScreen()
template<typename TDisplay>
void Keyboard<TDisplay>::updateCharLength()
{
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();
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;
}
int32_t m_char_length{0};
std::string m_keyboard{KEYBOARD_SCREEN_1};
std::string m_keyset{};
m_keyboard = tmpstr;
cpputils::stringReplaceAll(" ", "", tmpstr);
m_char_length = tmpstr.length();
m_keyset = tmpstr;
}
TDisplay &m_display;
int32_t m_keyboard_start_y{0};
template<typename TDisplay>
void Keyboard<TDisplay>::moveSelectorRight()
{
m_last_char_index = m_char_index;
int32_t m_char_index{10};
int32_t m_last_char_index{-1};
if (m_char_index == (m_char_length + 4) - 1)
m_char_index = 0;
else
m_char_index++;
}
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>::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<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