Refactor
This commit is contained in:
@ -34,128 +34,75 @@ 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;
|
||||
|
||||
m_char_index -= 10;
|
||||
if (m_char_index < 0)
|
||||
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_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;
|
||||
template<typename TDisplay>
|
||||
void Keyboard<TDisplay>::nextScreen()
|
||||
{
|
||||
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)
|
||||
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)
|
||||
{
|
||||
template<typename TDisplay>
|
||||
void Keyboard<TDisplay>::drawKeyboard(bool dont_draw_string)
|
||||
{
|
||||
size_t char_index{0};
|
||||
std::string keyboard_screen{m_keyboard};
|
||||
|
||||
@ -300,33 +247,112 @@ private:
|
||||
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});
|
||||
if (m_current_screen >= Screen::SCREEN_MAX)
|
||||
m_current_screen = Screen::SCREEN_1;
|
||||
updateCharLength();
|
||||
start();
|
||||
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
|
||||
|
Reference in New Issue
Block a user