on_screen_keyboard: Ya se pueden añadir y quitar caracteres al texto de salida
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
// #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)
|
||||
OnScreenKeyboard::OnScreenKeyboard(SDL_Renderer *renderer, Input *input, string bitmapFile, string textFile, options_t *options, int width, int height, string output, color_t color)
|
||||
{
|
||||
// Inicializa variables
|
||||
this->renderer = renderer;
|
||||
@@ -12,13 +12,13 @@ OnScreenKeyboard::OnScreenKeyboard(SDL_Renderer *renderer, Input *input, string
|
||||
this->height = height;
|
||||
|
||||
// Crea objetos
|
||||
iconTexture = new Texture(renderer, iconFile);
|
||||
text = new Text(textFile, bitmapFile, renderer);
|
||||
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, width, height);
|
||||
textureCursor = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, text->getCharacterSize() * 2, text->getCharacterSize() * 2);
|
||||
SDL_SetTextureBlendMode(textureCursor, SDL_BLENDMODE_BLEND);
|
||||
SDL_SetTextureBlendMode(textureCursor, SDL_BLENDMODE_BLEND);
|
||||
|
||||
// Inicializa variables
|
||||
this->output = output;
|
||||
char_upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
char_lower = "abcdefghijklmnopqrstuvwxyz";
|
||||
char_numbers = "0123456789";
|
||||
@@ -48,11 +48,6 @@ OnScreenKeyboard::OnScreenKeyboard(SDL_Renderer *renderer, Input *input, string
|
||||
OnScreenKeyboard::~OnScreenKeyboard()
|
||||
{
|
||||
// Libera la memoria de los objetos
|
||||
if (iconTexture != nullptr)
|
||||
{
|
||||
delete iconTexture;
|
||||
}
|
||||
|
||||
if (text != nullptr)
|
||||
{
|
||||
delete text;
|
||||
@@ -93,8 +88,8 @@ void OnScreenKeyboard::fillTexture()
|
||||
const int y_rect = text->getCharacterSize();
|
||||
const int w_rect = width - text->getCharacterSize() - x_rect;
|
||||
const int h_rect = text->getCharacterSize();
|
||||
SDL_Rect rect = {x_rect, y_rect, w_rect, h_rect};
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
outputDest = {x_rect, y_rect, w_rect, h_rect};
|
||||
SDL_RenderFillRect(renderer, &outputDest);
|
||||
|
||||
// Dibuja los caracteres que conformaran el teclado
|
||||
const int dist = text->getCharacterSize() * 2;
|
||||
@@ -106,10 +101,10 @@ void OnScreenKeyboard::fillTexture()
|
||||
const int pos = i + (j * columns);
|
||||
if (pos < (int)layout.size())
|
||||
{
|
||||
//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);
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
@@ -194,6 +189,12 @@ void OnScreenKeyboard::checkInput()
|
||||
|
||||
if (input->checkInput(INPUT_DOWN, REPEAT_FALSE))
|
||||
incCursorRow();
|
||||
|
||||
if (input->checkInput(INPUT_ACCEPT, REPEAT_FALSE))
|
||||
addChar();
|
||||
|
||||
if (input->checkInput(INPUT_CANCEL, REPEAT_FALSE))
|
||||
removeChar();
|
||||
}
|
||||
|
||||
// Disminuye en una columna la posición del cursor
|
||||
@@ -230,6 +231,21 @@ void OnScreenKeyboard::updateCursor()
|
||||
cursor.dest.y = dest.y + offset_y + (cursor.row * cursor.dest.h);
|
||||
}
|
||||
|
||||
// Añade un caracter a la salida
|
||||
void OnScreenKeyboard::addChar()
|
||||
{
|
||||
const int pos = cursor.col + cursor.row * columns;
|
||||
if (pos < layout.size())
|
||||
output = output + layout.at(pos).caption;
|
||||
}
|
||||
|
||||
// Borra el último caracter de la salida
|
||||
void OnScreenKeyboard::removeChar()
|
||||
{
|
||||
if (output.length() > 0)
|
||||
output = output.substr(0, output.length() - 1);
|
||||
}
|
||||
|
||||
// Actualiza la lógica del objeto
|
||||
void OnScreenKeyboard::update()
|
||||
{
|
||||
@@ -245,6 +261,7 @@ void OnScreenKeyboard::render()
|
||||
{
|
||||
SDL_RenderCopy(renderer, texture, nullptr, &dest);
|
||||
SDL_RenderCopy(renderer, textureCursor, nullptr, &cursor.dest);
|
||||
text->write(dest.x + outputDest.x, dest.y + outputDest.y, output);
|
||||
}
|
||||
|
||||
// Establece el color de fondo
|
||||
|
||||
Reference in New Issue
Block a user