on_screen_keyboard: Ya dibuja el objeto básico en pantalla

This commit is contained in:
2023-05-28 09:09:26 +02:00
parent 54947dce2d
commit 73849a2fef
3 changed files with 90 additions and 12 deletions

View File

@@ -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

View File

@@ -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();
}

View File

@@ -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