on_screen_keyboard: Ya se pueden añadir y quitar caracteres al texto de salida
This commit is contained in:
2
main.cpp
2
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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
// 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;
|
||||
@@ -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
|
||||
|
||||
@@ -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<key_t> 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();
|
||||
|
||||
Reference in New Issue
Block a user