From cc421e56eadc610a5d91814678d901c5171218da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Sun, 28 May 2023 12:58:02 +0200 Subject: [PATCH] on_screen_keyboard: Ya se dibuja el teclado en la pantalla --- data/8bithud.png | Bin 0 -> 1094 bytes data/8bithud.txt | 194 +++++++++++++++++++++++++++++++++++ main.cpp | 2 +- units/on_screen_keyboard.cpp | 79 ++++++++------ units/on_screen_keyboard.h | 10 +- 5 files changed, 254 insertions(+), 31 deletions(-) create mode 100644 data/8bithud.png create mode 100644 data/8bithud.txt diff --git a/data/8bithud.png b/data/8bithud.png new file mode 100644 index 0000000000000000000000000000000000000000..2ac5e4c8d74520242b2fb7a2bcf709bd8fc9a0e7 GIT binary patch literal 1094 zcmV-M1iAZ(P)Px(07*naRCt{2o7t9PF$hJ~<^TWk?H5xE5jcmCsMlscl|l#^7*f@Jxd4s_NXPow zvfhpT_qdRiy5p+oojAhM0dTk`RJWapbNLHU3$m*>sV z8)eDlcgJxYTF3hA9fUjpZOij%CFp`j-@k+M0Y`_+vMPctdpi$T+qdRIrHPS!bB1_w zFmhIYZ>3o&i!MZVBbd#LbgB_tJv=lXMsU>ul%lIxDLUgXss(-(ua zvAff1^8voP&YYN+#zaStx9O3d*YH^5SvmS#wZB{LWhEr9a%UBz1x1mEE)092SpTx} z?1i3{SDC#C_sZZw)Yvc!UZIn{IN7yk=V#|({D{uS%bWu`f~!OeiBkHi3{Z_QZ~Tf3 zG`fFJX3gWxuet560slLwc|;e2dB#O^&=Fj%E#A#vZMUkI9lxUHGHHAqqSo!eY?@z& z3h&j84SFXt_M;TP@*U33uk|zbCEA|#+49Y^cSj1=1bP9?FCn}QKytPT%-BccCDkT0 zyN-yc+xECe1zbDCbc4fQ_W9slhE*W@)%jO6Tgd*5U-!!;?$YE0Ll-$>eV<9ovx|wC zjbM$p4{RXJ;sp-kCLM|fUmq}zWE?mftJjyg4C9b9_F)ayj_f)>FGb4iBK4yZfKsrh zW8uxuqQ`kn8EcO%fkVFJu(F+E(=>~?>eR+Ob-cHj*0W?P?z?CRwoQoEMQur?PGbx&vIiRfX)zY%;e>TYU zI8$So#=^o%&9n1+{*TYm#q(;c_GvaqPzgF?=4zS-g1SzoGp%&4om%z+n=Di_Dh-6R zftnZTk0-O_NC%C#)*g+|`lxHPSHnHq);v-N{ylg%nF};NGtG`SKJtUHkH#bav&j9~ z{!H>5uzsGk;ek?lbtBpNcOB%eQbqs4e-_X2>^$fRg(9%ZYy@kV43zN;trte{S(+d( zg5>3u-?8JhJkqhS=FcFYa zH?6qa;aRNc(#&KKFLGBQQ!NT2YyWi$&d&exPj);bhafviL}mlf_?b=6>#EEjMVV*Y z^8cGMNYgY;vkkLmbq)AYb~I7VXL +4 +# 63 ? +6 +# 64 @ +8 +# 65 A +6 +# 66 B +6 +# 67 C +6 +# 68 D +6 +# 69 E +6 +# 70 F +6 +# 71 G +6 +# 72 H +6 +# 73 I +6 +# 74 J +6 +# 75 K +6 +# 76 L +6 +# 77 M +6 +# 78 N +6 +# 79 O +6 +# 80 P +6 +# 81 Q +6 +# 82 R +6 +# 83 S +6 +# 84 T +6 +# 85 U +6 +# 86 V +5 +# 87 W +6 +# 88 X +6 +# 89 Y +6 +# 90 Z +6 +# 91 [ +3 +# 92 \ +5 +# 93 ] +3 +# 94 ^ +4 +# 95 _ +6 +# 96 ` +2 +# 97 a +5 +# 98 b +5 +# 99 c +5 +# 100 d +5 +# 101 e +5 +# 102 f +5 +# 103 g +5 +# 104 h +5 +# 105 i +4 +# 106 j +5 +# 107 k +5 +# 108 l +5 +# 109 m +6 +# 110 n +5 +# 111 o +5 +# 112 p +5 +# 113 q +5 +# 114 r +5 +# 115 s +5 +# 116 t +4 +# 117 u +5 +# 118 v +5 +# 119 w +6 +# 120 x +4 +# 121 y +4 +# 122 z +5 +# 123 { +3 +# 124 | +2 +# 125 } +3 +# 126 ~ +3 \ No newline at end of file diff --git a/main.cpp b/main.cpp index 4988611..6d4ba52 100644 --- a/main.cpp +++ b/main.cpp @@ -246,7 +246,7 @@ 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->setBgColor({123, 99, 63}); osk->setCaption("JAILER_ID"); - osk->setChars(USE_UPPER | USE_NUMBER); + osk->setChars(USE_UPPER | USE_LOWER | USE_NUMBER); } // Inicializa el sprite diff --git a/units/on_screen_keyboard.cpp b/units/on_screen_keyboard.cpp index 82257e0..18d208d 100644 --- a/units/on_screen_keyboard.cpp +++ b/units/on_screen_keyboard.cpp @@ -1,4 +1,5 @@ #include "on_screen_keyboard.h" +//#include // Constructor OnScreenKeyboard::OnScreenKeyboard(SDL_Renderer *renderer, Input *input, string iconFile, string bitmapFile, string textFile, options_t *options, int width, int height, color_t color) @@ -24,9 +25,6 @@ OnScreenKeyboard::OnScreenKeyboard(SDL_Renderer *renderer, Input *input, string use_char_lower = false; use_char_numbers = false; use_char_symbol = false; - totalChars = getTotalChars(); - columns = getColumns(); - rows = getRows(); setLayout(); caption = ""; dest = {(options->screen.nativeWidth - width) / 2, (options->screen.nativeHeight - height) / 2, width, height}; @@ -58,6 +56,15 @@ OnScreenKeyboard::~OnScreenKeyboard() // Rellena la textura de fondo con el color y el texto void OnScreenKeyboard::fillTexture() { + // Destruye la textura si la hubiera + if (texture) + SDL_DestroyTexture(texture); + + // Crea la textura + height = (text->getCharacterSize() * 4) + (text->getCharacterSize() * rows * 2); + texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, width, height); + dest = {(options->screen.nativeWidth - width) / 2, (options->screen.nativeHeight - height) / 2, width, height}; + // Empieza a dibujar en la textura SDL_SetRenderTarget(renderer, texture); @@ -80,12 +87,20 @@ void OnScreenKeyboard::fillTexture() // Dibuja los caracteres que conformaran el teclado int dist = text->getCharacterSize() * 2; - for (int i = 0; i < columns; ++i) - for (int j = 0; j < rows; ++j) + int offset_x = text->getCharacterSize(); + 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); if (pos < (int)layout.size()) - text->write(i * dist, j * dist, layout.at(i + (j * columns))); + { + 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); + } } // Deja de dibujar en la textura @@ -106,21 +121,16 @@ int OnScreenKeyboard::getTotalChars() // Calcula cuantas columnas necesita el teclado int OnScreenKeyboard::getColumns() { - const int charSize = text->getCharacterSize(); - int w = width - (charSize * 2); - int total = 0; - while (w > 0) - { - w -= (charSize * 2); - ++total; - } - return total; + const int keyWidth = text->getCharacterSize() * 2; + int rowWidth = width - text->getCharacterSize(); + + return rowWidth / keyWidth; } // Calcula cuantas filas necesita el teclado int OnScreenKeyboard::getRows() { - return totalChars / getColumns(); + return (totalChars / getColumns())+1; } // Establece la disposición del teclado @@ -129,23 +139,34 @@ void OnScreenKeyboard::setLayout() totalChars = getTotalChars(); columns = getColumns(); rows = getRows(); + int index_col = 0; + int index_row = 0; + + // Establece los caracteres a escribir + string allChars = ""; + allChars = use_char_upper ? allChars + char_upper : allChars; + allChars = use_char_lower ? allChars + char_lower : allChars; + allChars = use_char_numbers ? allChars + char_numbers : allChars; + allChars = use_char_symbol ? allChars + char_symbol : allChars; layout.clear(); - if (use_char_upper) - for (int i = 0; i < char_upper.length(); ++i) - layout.push_back(char_upper.substr(i, 1)); + for (int i = 0; i < allChars.length(); ++i) + { + key_t key; + key.col = index_col; + key.row = index_row; + key.caption = allChars.substr(i, 1); + layout.push_back(key); - if (use_char_lower) - for (int i = 0; i < char_lower.length(); ++i) - layout.push_back(char_lower.substr(i, 1)); + //std::cout << key.caption << " " << key.col << " " << key.row << " " << std::endl; - if (use_char_numbers) - for (int i = 0; i < char_numbers.length(); ++i) - layout.push_back(char_numbers.substr(i, 1)); - - if (use_char_symbol) - for (int i = 0; i < char_symbol.length(); ++i) - layout.push_back(char_symbol.substr(i, 1)); + ++index_col; + if (index_col == columns) + { + index_col = 0; + ++index_row; + } + } } // Actualiza la lógica del objeto diff --git a/units/on_screen_keyboard.h b/units/on_screen_keyboard.h index 684ff8d..98a242d 100644 --- a/units/on_screen_keyboard.h +++ b/units/on_screen_keyboard.h @@ -26,6 +26,14 @@ private: options_t *options; // Variable con todas las opciones del programa SDL_Texture *texture; // Textura donde dibujar el objeto + // Estructuras + struct key_t + { + int col; // Posición horizontal de la tecla + int row; // Posición vertical de la tecla + string caption; // Texto de la tecla + }; + // Variables string char_upper; // Cadena de texto con las letras en mayúscula string char_lower; // Cadena de texto con las letras en minuscula @@ -43,7 +51,7 @@ private: int width; // Ancho del objeto int height; // Altura del objeto SDL_Rect dest; // Coordenadas donde se dibuja el objeto en pantalla - vector layout; // Contiene la disposición del teclado + vector layout; // Contiene la disposición del teclado // Rellena la textura de fondo con el color y el texto void fillTexture();