on_screen_keyboard: Ya se dibuja el cursor
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
#include "on_screen_keyboard.h"
|
#include "on_screen_keyboard.h"
|
||||||
//#include <iostream>
|
// #include <iostream>
|
||||||
|
|
||||||
// Constructor
|
// 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 iconFile, string bitmapFile, string textFile, options_t *options, int width, int height, color_t color)
|
||||||
@@ -15,6 +15,8 @@ OnScreenKeyboard::OnScreenKeyboard(SDL_Renderer *renderer, Input *input, string
|
|||||||
iconTexture = new Texture(renderer, iconFile);
|
iconTexture = new Texture(renderer, iconFile);
|
||||||
text = new Text(textFile, bitmapFile, renderer);
|
text = new Text(textFile, bitmapFile, renderer);
|
||||||
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, width, height);
|
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
|
// Inicializa variables
|
||||||
char_upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
char_upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
@@ -28,9 +30,18 @@ OnScreenKeyboard::OnScreenKeyboard(SDL_Renderer *renderer, Input *input, string
|
|||||||
setLayout();
|
setLayout();
|
||||||
caption = "";
|
caption = "";
|
||||||
dest = {(options->screen.nativeWidth - width) / 2, (options->screen.nativeHeight - height) / 2, width, height};
|
dest = {(options->screen.nativeWidth - width) / 2, (options->screen.nativeHeight - height) / 2, width, height};
|
||||||
|
cursor.col = 0;
|
||||||
|
cursor.row = 0;
|
||||||
|
cursor.dest = {0, 0, 16, 16};
|
||||||
|
|
||||||
// Rellena la textura de fondo con el color y el texto
|
// Rellena la textura de fondo con el color y el texto
|
||||||
fillTexture();
|
fillTexture();
|
||||||
|
|
||||||
|
// Rellena la textura del cursor
|
||||||
|
SDL_SetRenderTarget(renderer, textureCursor);
|
||||||
|
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 128);
|
||||||
|
SDL_RenderClear(renderer);
|
||||||
|
SDL_SetRenderTarget(renderer, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
@@ -86,19 +97,19 @@ void OnScreenKeyboard::fillTexture()
|
|||||||
SDL_RenderFillRect(renderer, &rect);
|
SDL_RenderFillRect(renderer, &rect);
|
||||||
|
|
||||||
// Dibuja los caracteres que conformaran el teclado
|
// Dibuja los caracteres que conformaran el teclado
|
||||||
int dist = text->getCharacterSize() * 2;
|
const int dist = text->getCharacterSize() * 2;
|
||||||
int offset_x = text->getCharacterSize();
|
const int offset_x = text->getCharacterSize();
|
||||||
int offset_y = text->getCharacterSize() * 3;
|
const int offset_y = text->getCharacterSize() * 3;
|
||||||
for (int j = 0; j < rows; ++j)
|
for (int j = 0; j < rows; ++j)
|
||||||
for (int i = 0; i < columns; ++i)
|
for (int i = 0; i < columns; ++i)
|
||||||
{
|
{
|
||||||
int pos = i + (j * columns);
|
const int pos = i + (j * columns);
|
||||||
if (pos < (int)layout.size())
|
if (pos < (int)layout.size())
|
||||||
{
|
{
|
||||||
color_t color = randColor();
|
//color_t color = randColor();
|
||||||
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 255);
|
//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_Rect rect = {offset_x + (layout.at(pos).col * dist), offset_y + (layout.at(pos).row * dist), dist, dist};
|
||||||
SDL_RenderFillRect(renderer, &rect);
|
//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);
|
text->write(offset_x + (layout.at(pos).col * dist) + 4, offset_y + (layout.at(pos).row * dist) + 4, layout.at(pos).caption);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -130,7 +141,7 @@ int OnScreenKeyboard::getColumns()
|
|||||||
// Calcula cuantas filas necesita el teclado
|
// Calcula cuantas filas necesita el teclado
|
||||||
int OnScreenKeyboard::getRows()
|
int OnScreenKeyboard::getRows()
|
||||||
{
|
{
|
||||||
return (totalChars / getColumns())+1;
|
return (totalChars / getColumns()) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Establece la disposición del teclado
|
// Establece la disposición del teclado
|
||||||
@@ -158,7 +169,7 @@ void OnScreenKeyboard::setLayout()
|
|||||||
key.caption = allChars.substr(i, 1);
|
key.caption = allChars.substr(i, 1);
|
||||||
layout.push_back(key);
|
layout.push_back(key);
|
||||||
|
|
||||||
//std::cout << key.caption << " " << key.col << " " << key.row << " " << std::endl;
|
// std::cout << key.caption << " " << key.col << " " << key.row << " " << std::endl;
|
||||||
|
|
||||||
++index_col;
|
++index_col;
|
||||||
if (index_col == columns)
|
if (index_col == columns)
|
||||||
@@ -169,15 +180,71 @@ void OnScreenKeyboard::setLayout()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Comprueba las entradas
|
||||||
|
void OnScreenKeyboard::checkInput()
|
||||||
|
{
|
||||||
|
if (input->checkInput(INPUT_LEFT, REPEAT_FALSE))
|
||||||
|
decCursorCol();
|
||||||
|
|
||||||
|
if (input->checkInput(INPUT_RIGHT, REPEAT_FALSE))
|
||||||
|
incCursorCol();
|
||||||
|
|
||||||
|
if (input->checkInput(INPUT_UP, REPEAT_FALSE))
|
||||||
|
decCursorRow();
|
||||||
|
|
||||||
|
if (input->checkInput(INPUT_DOWN, REPEAT_FALSE))
|
||||||
|
incCursorRow();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disminuye en una columna la posición del cursor
|
||||||
|
void OnScreenKeyboard::decCursorCol()
|
||||||
|
{
|
||||||
|
cursor.col = cursor.col > 0 ? --cursor.col : columns - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Incrementa en una columna la posición del cursor
|
||||||
|
void OnScreenKeyboard::incCursorCol()
|
||||||
|
{
|
||||||
|
++cursor.col %= columns;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disminuye en una fila la posición del cursor
|
||||||
|
void OnScreenKeyboard::decCursorRow()
|
||||||
|
{
|
||||||
|
cursor.row = cursor.row > 0 ? --cursor.row : rows - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Aumenta en una fila la posición del cursor
|
||||||
|
void OnScreenKeyboard::incCursorRow()
|
||||||
|
{
|
||||||
|
++cursor.row %= rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualiza la posición de la textura del cursor
|
||||||
|
void OnScreenKeyboard::updateCursor()
|
||||||
|
{
|
||||||
|
const int offset_x = text->getCharacterSize();
|
||||||
|
const int offset_y = text->getCharacterSize() * 3;
|
||||||
|
|
||||||
|
cursor.dest.x = dest.x + offset_x + (cursor.col * cursor.dest.w);
|
||||||
|
cursor.dest.y = dest.y + offset_y + (cursor.row * cursor.dest.h);
|
||||||
|
}
|
||||||
|
|
||||||
// Actualiza la lógica del objeto
|
// Actualiza la lógica del objeto
|
||||||
void OnScreenKeyboard::update()
|
void OnScreenKeyboard::update()
|
||||||
{
|
{
|
||||||
|
// Comprueba las entradas
|
||||||
|
checkInput();
|
||||||
|
|
||||||
|
// Actualiza la posición de la textura del cursor
|
||||||
|
updateCursor();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dibuja el objeto en pantalla
|
// Dibuja el objeto en pantalla
|
||||||
void OnScreenKeyboard::render()
|
void OnScreenKeyboard::render()
|
||||||
{
|
{
|
||||||
SDL_RenderCopy(renderer, texture, nullptr, &dest);
|
SDL_RenderCopy(renderer, texture, nullptr, &dest);
|
||||||
|
SDL_RenderCopy(renderer, textureCursor, nullptr, &cursor.dest);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Establece el color de fondo
|
// Establece el color de fondo
|
||||||
|
|||||||
@@ -19,12 +19,13 @@ class OnScreenKeyboard
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
// Objetos y punteros
|
// Objetos y punteros
|
||||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||||
Texture *iconTexture; // Textura para los gráficos
|
Texture *iconTexture; // Textura para los gráficos
|
||||||
Text *text; // Objeto para dibujar texto
|
Text *text; // Objeto para dibujar texto
|
||||||
Input *input; // Gestor de eventos de entrada de teclado o gamepad
|
Input *input; // Gestor de eventos de entrada de teclado o gamepad
|
||||||
options_t *options; // Variable con todas las opciones del programa
|
options_t *options; // Variable con todas las opciones del programa
|
||||||
SDL_Texture *texture; // Textura donde dibujar el objeto
|
SDL_Texture *texture; // Textura donde dibujar el objeto
|
||||||
|
SDL_Texture *textureCursor; // Textura para dibujar el cursor
|
||||||
|
|
||||||
// Estructuras
|
// Estructuras
|
||||||
struct key_t
|
struct key_t
|
||||||
@@ -34,6 +35,13 @@ private:
|
|||||||
string caption; // Texto de la tecla
|
string caption; // Texto de la tecla
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct cursor_t
|
||||||
|
{
|
||||||
|
int col;
|
||||||
|
int row;
|
||||||
|
SDL_Rect dest;
|
||||||
|
};
|
||||||
|
|
||||||
// Variables
|
// Variables
|
||||||
string char_upper; // Cadena de texto con las letras en mayúscula
|
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_lower; // Cadena de texto con las letras en minuscula
|
||||||
@@ -52,6 +60,7 @@ private:
|
|||||||
int height; // Altura del objeto
|
int height; // Altura del objeto
|
||||||
SDL_Rect dest; // Coordenadas donde se dibuja el objeto en pantalla
|
SDL_Rect dest; // Coordenadas donde se dibuja el objeto en pantalla
|
||||||
vector<key_t> layout; // Contiene la disposición del teclado
|
vector<key_t> layout; // Contiene la disposición del teclado
|
||||||
|
cursor_t cursor; // Variable para gestionar el cursor
|
||||||
|
|
||||||
// Rellena la textura de fondo con el color y el texto
|
// Rellena la textura de fondo con el color y el texto
|
||||||
void fillTexture();
|
void fillTexture();
|
||||||
@@ -68,6 +77,24 @@ private:
|
|||||||
// Establece la disposición del teclado
|
// Establece la disposición del teclado
|
||||||
void setLayout();
|
void setLayout();
|
||||||
|
|
||||||
|
// Comprueba las entradas
|
||||||
|
void checkInput();
|
||||||
|
|
||||||
|
// Disminuye en una columna la posición del cursor
|
||||||
|
void decCursorCol();
|
||||||
|
|
||||||
|
// Incrementa en una columna la posición del cursor
|
||||||
|
void incCursorCol();
|
||||||
|
|
||||||
|
// Disminuye en una fila la posición del cursor
|
||||||
|
void decCursorRow();
|
||||||
|
|
||||||
|
// Aumenta en una columna la posición del cursor
|
||||||
|
void incCursorRow();
|
||||||
|
|
||||||
|
// Actualiza la posición de la textura del cursor
|
||||||
|
void updateCursor();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// 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 iconFile, string bitmapFile, string textFile, options_t *options, int width, int height, color_t color = {0, 0, 0});
|
||||||
|
|||||||
Reference in New Issue
Block a user