From 120c3791cf1c0a6dee49cc55315c28d4e1ad85d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Sun, 28 May 2023 18:33:52 +0200 Subject: [PATCH] =?UTF-8?q?on=5Fscreen=5Fkeyboard:=20Ya=20se=20pueden=20a?= =?UTF-8?q?=C3=B1adir=20y=20quitar=20caracteres=20al=20texto=20de=20salida?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 2 +- units/on_screen_keyboard.cpp | 45 +++++++++++++++++++++++++----------- units/on_screen_keyboard.h | 11 +++++++-- 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/main.cpp b/main.cpp index 6d4ba52..b0ef3e7 100644 --- a/main.cpp +++ b/main.cpp @@ -243,7 +243,7 @@ void initText() // Inicializa el teclado en pantalla 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("smb2.png"), asset->get("smb2.txt"), options, options->screen.nativeWidth - 30, options->screen.nativeHeight - 80, "JailDesigner"); osk->setBgColor({123, 99, 63}); osk->setCaption("JAILER_ID"); osk->setChars(USE_UPPER | USE_LOWER | USE_NUMBER); diff --git a/units/on_screen_keyboard.cpp b/units/on_screen_keyboard.cpp index 30dfb97..6d1c2c4 100644 --- a/units/on_screen_keyboard.cpp +++ b/units/on_screen_keyboard.cpp @@ -2,7 +2,7 @@ // #include // 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 diff --git a/units/on_screen_keyboard.h b/units/on_screen_keyboard.h index adc6d0b..c9a353e 100644 --- a/units/on_screen_keyboard.h +++ b/units/on_screen_keyboard.h @@ -20,7 +20,6 @@ class OnScreenKeyboard private: // Objetos y punteros SDL_Renderer *renderer; // El renderizador de la ventana - Texture *iconTexture; // Textura para los gráficos Text *text; // Objeto para dibujar texto Input *input; // Gestor de eventos de entrada de teclado o gamepad options_t *options; // Variable con todas las opciones del programa @@ -61,6 +60,8 @@ private: SDL_Rect dest; // Coordenadas donde se dibuja el objeto en pantalla vector layout; // Contiene la disposición del teclado cursor_t cursor; // Variable para gestionar el cursor + string output; // Texto de salida + SDL_Rect outputDest; // Rectangulo con el texto de salida // Rellena la textura de fondo con el color y el texto void fillTexture(); @@ -95,9 +96,15 @@ private: // Actualiza la posición de la textura del cursor void updateCursor(); + // Añade un caracter a la salida + void addChar(); + + // Borra el último caracter de la salida + void removeChar(); + public: // 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 bitmapFile, string textFile, options_t *options, int width, int height, string output = "", color_t color = {0, 0, 0}); // Destructor ~OnScreenKeyboard();