on_screen_keyboard: Ya se dibuja el cursor
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
#include "on_screen_keyboard.h"
|
||||
//#include <iostream>
|
||||
// #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)
|
||||
@@ -15,6 +15,8 @@ OnScreenKeyboard::OnScreenKeyboard(SDL_Renderer *renderer, Input *input, string
|
||||
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
|
||||
char_upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
@@ -28,9 +30,18 @@ OnScreenKeyboard::OnScreenKeyboard(SDL_Renderer *renderer, Input *input, string
|
||||
setLayout();
|
||||
caption = "";
|
||||
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
|
||||
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
|
||||
@@ -86,19 +97,19 @@ void OnScreenKeyboard::fillTexture()
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
|
||||
// Dibuja los caracteres que conformaran el teclado
|
||||
int dist = text->getCharacterSize() * 2;
|
||||
int offset_x = text->getCharacterSize();
|
||||
int offset_y = text->getCharacterSize() * 3;
|
||||
const int dist = text->getCharacterSize() * 2;
|
||||
const int offset_x = text->getCharacterSize();
|
||||
const int offset_y = text->getCharacterSize() * 3;
|
||||
for (int j = 0; j < rows; ++j)
|
||||
for (int i = 0; i < columns; ++i)
|
||||
{
|
||||
int pos = i + (j * columns);
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -130,7 +141,7 @@ int OnScreenKeyboard::getColumns()
|
||||
// Calcula cuantas filas necesita el teclado
|
||||
int OnScreenKeyboard::getRows()
|
||||
{
|
||||
return (totalChars / getColumns())+1;
|
||||
return (totalChars / getColumns()) + 1;
|
||||
}
|
||||
|
||||
// Establece la disposición del teclado
|
||||
@@ -158,7 +169,7 @@ void OnScreenKeyboard::setLayout()
|
||||
key.caption = allChars.substr(i, 1);
|
||||
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;
|
||||
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
|
||||
void OnScreenKeyboard::update()
|
||||
{
|
||||
// Comprueba las entradas
|
||||
checkInput();
|
||||
|
||||
// Actualiza la posición de la textura del cursor
|
||||
updateCursor();
|
||||
}
|
||||
|
||||
// Dibuja el objeto en pantalla
|
||||
void OnScreenKeyboard::render()
|
||||
{
|
||||
SDL_RenderCopy(renderer, texture, nullptr, &dest);
|
||||
SDL_RenderCopy(renderer, textureCursor, nullptr, &cursor.dest);
|
||||
}
|
||||
|
||||
// Establece el color de fondo
|
||||
|
||||
@@ -19,12 +19,13 @@ 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
|
||||
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
|
||||
SDL_Texture *textureCursor; // Textura para dibujar el cursor
|
||||
|
||||
// Estructuras
|
||||
struct key_t
|
||||
@@ -34,6 +35,13 @@ private:
|
||||
string caption; // Texto de la tecla
|
||||
};
|
||||
|
||||
struct cursor_t
|
||||
{
|
||||
int col;
|
||||
int row;
|
||||
SDL_Rect dest;
|
||||
};
|
||||
|
||||
// Variables
|
||||
string char_upper; // Cadena de texto con las letras en mayúscula
|
||||
string char_lower; // Cadena de texto con las letras en minuscula
|
||||
@@ -52,6 +60,7 @@ private:
|
||||
int height; // Altura del objeto
|
||||
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
|
||||
|
||||
// Rellena la textura de fondo con el color y el texto
|
||||
void fillTexture();
|
||||
@@ -68,6 +77,24 @@ private:
|
||||
// Establece la disposición del teclado
|
||||
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:
|
||||
// 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});
|
||||
|
||||
Reference in New Issue
Block a user