Refactor
This commit is contained in:
@ -34,62 +34,44 @@ 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;
|
||||||
|
|
||||||
@ -98,7 +80,8 @@ public:
|
|||||||
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;
|
||||||
|
|
||||||
@ -107,54 +90,18 @@ public:
|
|||||||
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)
|
||||||
if (m_char_index == 0)
|
m_current_screen = Screen::SCREEN_1;
|
||||||
m_char_index = (m_char_length + 4) - 1;
|
updateCharLength();
|
||||||
else
|
start();
|
||||||
m_char_index--;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void moveSelectorRight()
|
template<typename TDisplay>
|
||||||
{
|
void Keyboard<TDisplay>::drawKeyboard(bool dont_draw_string)
|
||||||
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};
|
||||||
@ -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});
|
std::string tmpstr;
|
||||||
if (m_current_screen >= Screen::SCREEN_MAX)
|
|
||||||
m_current_screen = Screen::SCREEN_1;
|
switch (m_current_screen)
|
||||||
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};
|
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
|
||||||
|
Reference in New Issue
Block a user