on_screen_keyboard: Se empiezan a dibujar algunas teclas en pantalla

This commit is contained in:
2023-05-28 10:38:04 +02:00
parent 2b5a2cb588
commit 5d58d24cb9
3 changed files with 118 additions and 9 deletions

View File

@@ -246,6 +246,7 @@ void initOnScreenKeyboard()
osk = new OnScreenKeyboard(renderer, input, asset->get("notify.png"), asset->get("smb2.png"), asset->get("smb2.txt"), options, options->screen.nativeWidth - 30, options->screen.nativeHeight - 80); osk = new OnScreenKeyboard(renderer, input, asset->get("notify.png"), asset->get("smb2.png"), asset->get("smb2.txt"), options, options->screen.nativeWidth - 30, options->screen.nativeHeight - 80);
osk->setBgColor({123, 99, 63}); osk->setBgColor({123, 99, 63});
osk->setCaption("JAILER_ID"); osk->setCaption("JAILER_ID");
osk->setChars(USE_UPPER | USE_NUMBER);
} }
// Inicializa el sprite // Inicializa el sprite

View File

@@ -20,6 +20,14 @@ OnScreenKeyboard::OnScreenKeyboard(SDL_Renderer *renderer, Input *input, string
char_lower = "abcdefghijklmnopqrstuvwxyz"; char_lower = "abcdefghijklmnopqrstuvwxyz";
char_numbers = "0123456789"; char_numbers = "0123456789";
char_symbol = " !\"#$%&'()*+,-./:;<=>?@[]"; char_symbol = " !\"#$%&'()*+,-./:;<=>?@[]";
use_char_upper = true;
use_char_lower = false;
use_char_numbers = false;
use_char_symbol = false;
totalChars = getTotalChars();
columns = getColumns();
rows = getRows();
setLayout();
caption = ""; caption = "";
dest = {(options->screen.nativeWidth - width) / 2, (options->screen.nativeHeight - height) / 2, width, height}; dest = {(options->screen.nativeWidth - width) / 2, (options->screen.nativeHeight - height) / 2, width, height};
@@ -70,10 +78,76 @@ void OnScreenKeyboard::fillTexture()
SDL_Rect rect = {x_rect, y_rect, w_rect, h_rect}; SDL_Rect rect = {x_rect, y_rect, w_rect, h_rect};
SDL_RenderFillRect(renderer, &rect); SDL_RenderFillRect(renderer, &rect);
// Dibuja los caracteres que conformaran el teclado
int dist = text->getCharacterSize() * 2;
for (int i = 0; i < columns; ++i)
for (int j = 0; j < rows; ++j)
{
int pos = i + (j * columns);
if (pos < (int)layout.size())
text->write(i * dist, j * dist, layout.at(i + (j * columns)));
}
// Deja de dibujar en la textura // Deja de dibujar en la textura
SDL_SetRenderTarget(renderer, nullptr); SDL_SetRenderTarget(renderer, nullptr);
} }
// Calcula cuantos caracteres se utilizaran para crear el teclado
int OnScreenKeyboard::getTotalChars()
{
int total = 0;
total += use_char_upper ? char_upper.length() : 0;
total += use_char_lower ? char_lower.length() : 0;
total += use_char_numbers ? char_numbers.length() : 0;
total += use_char_symbol ? char_symbol.length() : 0;
return total;
}
// Calcula cuantas columnas necesita el teclado
int OnScreenKeyboard::getColumns()
{
const int charSize = text->getCharacterSize();
int w = width - (charSize * 2);
int total = 0;
while (w > 0)
{
w -= (charSize * 2);
++total;
}
return total;
}
// Calcula cuantas filas necesita el teclado
int OnScreenKeyboard::getRows()
{
return totalChars / getColumns();
}
// Establece la disposición del teclado
void OnScreenKeyboard::setLayout()
{
totalChars = getTotalChars();
columns = getColumns();
rows = getRows();
layout.clear();
if (use_char_upper)
for (int i = 0; i < char_upper.length(); ++i)
layout.push_back(char_upper.substr(i, 1));
if (use_char_lower)
for (int i = 0; i < char_lower.length(); ++i)
layout.push_back(char_lower.substr(i, 1));
if (use_char_numbers)
for (int i = 0; i < char_numbers.length(); ++i)
layout.push_back(char_numbers.substr(i, 1));
if (use_char_symbol)
for (int i = 0; i < char_symbol.length(); ++i)
layout.push_back(char_symbol.substr(i, 1));
}
// Actualiza la lógica del objeto // Actualiza la lógica del objeto
void OnScreenKeyboard::update() void OnScreenKeyboard::update()
{ {
@@ -98,3 +172,14 @@ void OnScreenKeyboard::setCaption(string text)
caption = text; caption = text;
fillTexture(); fillTexture();
} }
// Establece qué caracteres ofrecerá el objeto
void OnScreenKeyboard::setChars(Uint8 mode)
{
use_char_upper = ((mode & USE_UPPER) == USE_UPPER);
use_char_lower = ((mode & USE_LOWER) == USE_LOWER);
use_char_numbers = ((mode & USE_NUMBER) == USE_NUMBER);
use_char_symbol = ((mode & USE_SYMBOL) == USE_SYMBOL);
setLayout();
fillTexture();
}

View File

@@ -31,15 +31,35 @@ private:
string char_lower; // Cadena de texto con las letras en minuscula string char_lower; // Cadena de texto con las letras en minuscula
string char_numbers; // Cadena de texto con los números string char_numbers; // Cadena de texto con los números
string char_symbol; // Cadena de texto con los símbolos string char_symbol; // Cadena de texto con los símbolos
bool use_char_upper; // Indica si se utilizará ese set de caracteres
bool use_char_lower; // Indica si se utilizará ese set de caracteres
bool use_char_numbers; // Indica si se utilizará ese set de caracteres
bool use_char_symbol; // Indica si se utilizará ese set de caracteres
int totalChars; // Cantidad de caracteres a utilizar
int columns; // Cantidad de caracteres que hay en una fila del teclado
int rows; // Cantidad de filas de caracteres que tendrá el teclado
color_t bgColor; // Color usado para el fondo color_t bgColor; // Color usado para el fondo
string caption; // Texto a mostrar junto al texto a introducir string caption; // Texto a mostrar junto al texto a introducir
int width; // Ancho del objeto int width; // Ancho del objeto
int height; // Altura del objeto int height; // Altura del objeto
SDL_Rect dest; // Coordenadas donde se dibuja el objeto en pantalla SDL_Rect dest; // Coordenadas donde se dibuja el objeto en pantalla
vector<string> layout; // Contiene la disposición del teclado
// Rellena la textura de fondo con el color y el texto // Rellena la textura de fondo con el color y el texto
void fillTexture(); void fillTexture();
// Calcula cuantos caracteres se utilizaran para crear el teclado
int getTotalChars();
// Calcula cuantas columnas necesita el teclado
int getColumns();
// Calcula cuantas filas necesita el teclado
int getRows();
// Establece la disposición del teclado
void setLayout();
public: public:
// Constructor // Constructor
OnScreenKeyboard(SDL_Renderer *renderer, Input *input, string iconFile, string bitmapFile, string textFile, options_t *options, int width, int height, color_t color = {0, 0, 0}); OnScreenKeyboard(SDL_Renderer *renderer, Input *input, string iconFile, string bitmapFile, string textFile, options_t *options, int width, int height, color_t color = {0, 0, 0});
@@ -58,6 +78,9 @@ public:
// Establece el texto a mostrar junto al texto a introducir // Establece el texto a mostrar junto al texto a introducir
void setCaption(string text); void setCaption(string text);
// Establece qué caracteres ofrecerá el objeto
void setChars(Uint8 mode);
}; };
#endif #endif