From 73849a2fefaf618c169b5945b459ff366e123f60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Sun, 28 May 2023 09:09:26 +0200 Subject: [PATCH] =?UTF-8?q?on=5Fscreen=5Fkeyboard:=20Ya=20dibuja=20el=20ob?= =?UTF-8?q?jeto=20b=C3=A1sico=20en=20pantalla?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 4 ++- units/on_screen_keyboard.cpp | 62 ++++++++++++++++++++++++++++++++++-- units/on_screen_keyboard.h | 36 ++++++++++++++++----- 3 files changed, 90 insertions(+), 12 deletions(-) diff --git a/main.cpp b/main.cpp index 8be1429..6cc746a 100644 --- a/main.cpp +++ b/main.cpp @@ -243,7 +243,9 @@ 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); + 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->setCaption("JAILER_ID"); } // Inicializa el sprite diff --git a/units/on_screen_keyboard.cpp b/units/on_screen_keyboard.cpp index 3f0b853..a668afc 100644 --- a/units/on_screen_keyboard.cpp +++ b/units/on_screen_keyboard.cpp @@ -1,19 +1,30 @@ #include "on_screen_keyboard.h" // Constructor -OnScreenKeyboard::OnScreenKeyboard(SDL_Renderer *renderer, Input *input, string iconFile, string bitmapFile, string textFile, options_t *options) +OnScreenKeyboard::OnScreenKeyboard(SDL_Renderer *renderer, Input *input, string iconFile, string bitmapFile, string textFile, options_t *options, int width, int height, color_t color) { // Inicializa variables this->renderer = renderer; this->options = options; this->input = input; + this->width = width; + 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); // Inicializa variables - letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + char_upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + char_lower = "abcdefghijklmnopqrstuvwxyz"; + char_numbers = "0123456789"; + char_symbol = " !\"#$%&'()*+,-./:;<=>?@[]"; + caption = ""; + dest = {(options->screen.nativeWidth - width) / 2, (options->screen.nativeHeight - height) / 2, width, height}; + + // Rellena la textura de fondo con el color y el texto + fillTexture(); } // Destructor @@ -29,6 +40,37 @@ OnScreenKeyboard::~OnScreenKeyboard() { delete text; } + + if (texture != nullptr) + { + SDL_DestroyTexture(texture); + } +} + +// Rellena la textura de fondo con el color y el texto +void OnScreenKeyboard::fillTexture() +{ + // Empieza a dibujar en la textura + SDL_SetRenderTarget(renderer, texture); + + // Establece el color de fondo + SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, 255); + SDL_RenderClear(renderer); + + // Escribe el texto que aparece en el objeto + text->write(text->getCharacterSize(), text->getCharacterSize(), caption); + + // Dibuja el cuadro donde va el texto que se escribirá + SDL_SetRenderDrawColor(renderer, bgColor.r - 10, bgColor.g - 10, bgColor.b - 10, 255); + const int x_rect = (text->getCharacterSize() * 2) + text->lenght(caption); + 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); + + // Deja de dibujar en la textura + SDL_SetRenderTarget(renderer, nullptr); } // Actualiza la lógica del objeto @@ -39,5 +81,19 @@ void OnScreenKeyboard::update() // Dibuja el objeto en pantalla void OnScreenKeyboard::render() { - text->write(0,0,"ON_SCREEN_KEYBOARD"); + SDL_RenderCopy(renderer, texture, nullptr, &dest); +} + +// Establece el color de fondo +void OnScreenKeyboard::setBgColor(color_t color) +{ + bgColor = color; + fillTexture(); +} + +// Establece el texto a mostrar junto al texto a introducir +void OnScreenKeyboard::setCaption(string text) +{ + caption = text; + fillTexture(); } \ No newline at end of file diff --git a/units/on_screen_keyboard.h b/units/on_screen_keyboard.h index cc47c9e..aa746cd 100644 --- a/units/on_screen_keyboard.h +++ b/units/on_screen_keyboard.h @@ -8,36 +8,56 @@ #ifndef ON_SCREEN_KEYBOARD_H #define ON_SCREEN_KEYBOARD_H +#define USE_UPPER 1 +#define USE_LOWER 2 +#define USE_NUMBER 4 +#define USE_SYMBOL 8 + using namespace std; 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 + SDL_Texture *texture; // Textura donde dibujar el objeto // Variables - string letters; + string char_upper; // Cadena de texto con las letras en mayúscula + string char_lower; // Cadena de texto con las letras en minuscula + string char_numbers; // Cadena de texto con los números + string char_symbol; // Cadena de texto con los símbolos + color_t bgColor; // Color usado para el fondo + string caption; // Texto a mostrar junto al texto a introducir + int width; // Ancho del objeto + int height; // Altura del objeto + SDL_Rect dest; // Coordenadas donde se dibuja el objeto en pantalla + + // Rellena la textura de fondo con el color y el texto + void fillTexture(); public: - // Constructor - OnScreenKeyboard(SDL_Renderer *renderer, Input *input, string iconFile, string bitmapFile, string textFile, options_t *options); + 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}); // Destructor ~OnScreenKeyboard(); - // Actualiza la lógica del objeto - void update(); + // Actualiza la lógica del objeto + void update(); - // Dibuja el objeto en pantalla - void render(); + // Dibuja el objeto en pantalla + void render(); + // Establece el color de fondo + void setBgColor(color_t color); + + // Establece el texto a mostrar junto al texto a introducir + void setCaption(string text); }; #endif \ No newline at end of file