on_screen_keyboard: Ya se dibuja el teclado en la pantalla

This commit is contained in:
2023-05-28 12:58:02 +02:00
parent ad9a3fb594
commit cc421e56ea
5 changed files with 254 additions and 31 deletions

View File

@@ -1,4 +1,5 @@
#include "on_screen_keyboard.h"
//#include <iostream>
// Constructor
OnScreenKeyboard::OnScreenKeyboard(SDL_Renderer *renderer, Input *input, string iconFile, string bitmapFile, string textFile, options_t *options, int width, int height, color_t color)
@@ -24,9 +25,6 @@ OnScreenKeyboard::OnScreenKeyboard(SDL_Renderer *renderer, Input *input, string
use_char_lower = false;
use_char_numbers = false;
use_char_symbol = false;
totalChars = getTotalChars();
columns = getColumns();
rows = getRows();
setLayout();
caption = "";
dest = {(options->screen.nativeWidth - width) / 2, (options->screen.nativeHeight - height) / 2, width, height};
@@ -58,6 +56,15 @@ OnScreenKeyboard::~OnScreenKeyboard()
// Rellena la textura de fondo con el color y el texto
void OnScreenKeyboard::fillTexture()
{
// Destruye la textura si la hubiera
if (texture)
SDL_DestroyTexture(texture);
// Crea la textura
height = (text->getCharacterSize() * 4) + (text->getCharacterSize() * rows * 2);
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, width, height);
dest = {(options->screen.nativeWidth - width) / 2, (options->screen.nativeHeight - height) / 2, width, height};
// Empieza a dibujar en la textura
SDL_SetRenderTarget(renderer, texture);
@@ -80,12 +87,20 @@ void OnScreenKeyboard::fillTexture()
// 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 offset_x = text->getCharacterSize();
int offset_y = text->getCharacterSize() * 3;
for (int j = 0; j < rows; ++j)
for (int i = 0; i < columns; ++i)
{
int pos = i + (j * columns);
if (pos < (int)layout.size())
text->write(i * dist, j * dist, layout.at(i + (j * columns)));
{
color_t color = randColor();
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 255);
SDL_Rect rect = {offset_x + (layout.at(pos).col * dist), offset_y + (layout.at(pos).row * dist), dist, dist};
SDL_RenderFillRect(renderer, &rect);
text->write(offset_x + (layout.at(pos).col * dist) + 4, offset_y + (layout.at(pos).row * dist) + 4, layout.at(pos).caption);
}
}
// Deja de dibujar en la textura
@@ -106,21 +121,16 @@ int OnScreenKeyboard::getTotalChars()
// 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;
const int keyWidth = text->getCharacterSize() * 2;
int rowWidth = width - text->getCharacterSize();
return rowWidth / keyWidth;
}
// Calcula cuantas filas necesita el teclado
int OnScreenKeyboard::getRows()
{
return totalChars / getColumns();
return (totalChars / getColumns())+1;
}
// Establece la disposición del teclado
@@ -129,23 +139,34 @@ void OnScreenKeyboard::setLayout()
totalChars = getTotalChars();
columns = getColumns();
rows = getRows();
int index_col = 0;
int index_row = 0;
// Establece los caracteres a escribir
string allChars = "";
allChars = use_char_upper ? allChars + char_upper : allChars;
allChars = use_char_lower ? allChars + char_lower : allChars;
allChars = use_char_numbers ? allChars + char_numbers : allChars;
allChars = use_char_symbol ? allChars + char_symbol : allChars;
layout.clear();
if (use_char_upper)
for (int i = 0; i < char_upper.length(); ++i)
layout.push_back(char_upper.substr(i, 1));
for (int i = 0; i < allChars.length(); ++i)
{
key_t key;
key.col = index_col;
key.row = index_row;
key.caption = allChars.substr(i, 1);
layout.push_back(key);
if (use_char_lower)
for (int i = 0; i < char_lower.length(); ++i)
layout.push_back(char_lower.substr(i, 1));
//std::cout << key.caption << " " << key.col << " " << key.row << " " << std::endl;
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));
++index_col;
if (index_col == columns)
{
index_col = 0;
++index_row;
}
}
}
// Actualiza la lógica del objeto

View File

@@ -26,6 +26,14 @@ private:
options_t *options; // Variable con todas las opciones del programa
SDL_Texture *texture; // Textura donde dibujar el objeto
// Estructuras
struct key_t
{
int col; // Posición horizontal de la tecla
int row; // Posición vertical de la tecla
string caption; // Texto de la tecla
};
// Variables
string char_upper; // Cadena de texto con las letras en mayúscula
string char_lower; // Cadena de texto con las letras en minuscula
@@ -43,7 +51,7 @@ private:
int width; // Ancho del objeto
int height; // Altura del objeto
SDL_Rect dest; // Coordenadas donde se dibuja el objeto en pantalla
vector<string> layout; // Contiene la disposición del teclado
vector<key_t> layout; // Contiene la disposición del teclado
// Rellena la textura de fondo con el color y el texto
void fillTexture();