|
| 1 | +/* |
| 2 | + * BreakHack - A dungeone crawler RPG |
| 3 | + * Copyright (C) 2025 Linus Probert <linus.probert@gmail.com> |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "text_input.h" |
| 20 | +#include "SDL3/SDL_clipboard.h" |
| 21 | +#include "sprite.h" |
| 22 | +#include "defines.h" |
| 23 | + |
| 24 | +static char seedInputBuffer[16] = {0}; |
| 25 | +static Sprite *seedInputSprite = NULL; |
| 26 | +static Sprite *seedHeadingSprite = NULL; |
| 27 | +static Sprite *seedHintSprite = NULL; |
| 28 | +static SDL_Renderer *gRenderer = NULL; |
| 29 | +static bool gInputConfirmed = false; |
| 30 | + |
| 31 | +void |
| 32 | +text_input_init(SDL_Window *window, SDL_Renderer *renderer, const char *title) |
| 33 | +{ |
| 34 | + gRenderer = renderer; |
| 35 | + gInputConfirmed = false; |
| 36 | + |
| 37 | + // Clear the buffer |
| 38 | + memset(seedInputBuffer, '\0', sizeof(seedInputBuffer)); |
| 39 | + SDL_StartTextInput(window); |
| 40 | + |
| 41 | + if (seedHeadingSprite == NULL) { |
| 42 | + seedHeadingSprite = sprite_create(); |
| 43 | + sprite_load_text_texture(seedHeadingSprite, "GUI/SDS_8x8.ttf", 0, 18, 1); |
| 44 | + texture_load_from_text(seedHeadingSprite->textures[0], title, C_BLUE, C_BLACK, renderer); |
| 45 | + seedHeadingSprite->dim = seedHeadingSprite->textures[0]->dim; |
| 46 | + seedHeadingSprite->pos = |
| 47 | + (Position){(SCREEN_WIDTH - seedHeadingSprite->dim.width) >> 1, SCREEN_HEIGHT / 2 - 40}; |
| 48 | + seedHeadingSprite->fixed = true; |
| 49 | + } |
| 50 | + |
| 51 | + if (seedInputSprite == NULL) { |
| 52 | + seedInputSprite = sprite_create(); |
| 53 | + sprite_load_text_texture(seedInputSprite, "GUI/SDS_8x8.ttf", 0, 18, 1); |
| 54 | + seedInputSprite->fixed = true; |
| 55 | + } |
| 56 | + |
| 57 | + if (seedHintSprite == NULL) { |
| 58 | + seedHintSprite = sprite_create(); |
| 59 | + sprite_load_text_texture(seedHintSprite, "GUI/SDS_8x8.ttf", 0, 10, 1); |
| 60 | + texture_load_from_text(seedHintSprite->textures[0], "ENTER to confirm | ESC to cancel", C_WHITE, |
| 61 | + C_BLACK, renderer); |
| 62 | + seedHintSprite->dim = seedHintSprite->textures[0]->dim; |
| 63 | + seedHintSprite->pos = (Position){15, SCREEN_HEIGHT - 25}; |
| 64 | + seedHintSprite->fixed = true; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +static void |
| 69 | +parseBufferToInput(const char *buffer) |
| 70 | +{ |
| 71 | + for (const char *c = buffer; *c; c++) { |
| 72 | + if (*c >= '0' && *c <= '9') { |
| 73 | + size_t len = SDL_strlen(seedInputBuffer); |
| 74 | + if (len < sizeof(seedInputBuffer) - 1) { |
| 75 | + seedInputBuffer[len] = *c; |
| 76 | + seedInputBuffer[len + 1] = '\0'; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +void |
| 83 | +text_input_update(Input *input) |
| 84 | +{ |
| 85 | + if (input->textInput[0] != '\0') { |
| 86 | + parseBufferToInput(input->textInput); |
| 87 | + } else if (input_key_is_pressed(input, KEY_BACKSPACE)) { |
| 88 | + size_t len = SDL_strlen(seedInputBuffer); |
| 89 | + if (len > 0) { |
| 90 | + seedInputBuffer[len - 1] = '\0'; |
| 91 | + } |
| 92 | + } else if (input_key_is_pressed(input, KEY_ENTER)) { |
| 93 | + gInputConfirmed = true; |
| 94 | + } else if (input_modkey_is_pressed(input, KEY_CTRL_V)) { |
| 95 | + char *cb_text = SDL_GetClipboardText(); |
| 96 | + parseBufferToInput(cb_text); |
| 97 | + SDL_free(cb_text); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +bool |
| 102 | +text_input_is_confirmed(void) |
| 103 | +{ |
| 104 | + return gInputConfirmed; |
| 105 | +} |
| 106 | + |
| 107 | +const char * |
| 108 | +text_input_get_value(void) |
| 109 | +{ |
| 110 | + return seedInputBuffer; |
| 111 | +} |
| 112 | + |
| 113 | +void |
| 114 | +text_input_render(Camera *cam) |
| 115 | +{ |
| 116 | + SDL_FRect bg = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT}; |
| 117 | + SDL_SetRenderDrawColor(cam->renderer, 0, 0, 0, 175); |
| 118 | + SDL_RenderFillRect(cam->renderer, &bg); |
| 119 | + |
| 120 | + sprite_render(seedHeadingSprite, cam); |
| 121 | + |
| 122 | + char displayBuf[20]; |
| 123 | + SDL_snprintf(displayBuf, sizeof(displayBuf), "%s_", seedInputBuffer); |
| 124 | + texture_load_from_text(seedInputSprite->textures[0], displayBuf, C_YELLOW, C_BLACK, gRenderer); |
| 125 | + seedInputSprite->dim = seedInputSprite->textures[0]->dim; |
| 126 | + seedInputSprite->pos = (Position){(SCREEN_WIDTH - seedInputSprite->dim.width) >> 1, SCREEN_HEIGHT / 2}; |
| 127 | + sprite_render(seedInputSprite, cam); |
| 128 | + |
| 129 | + sprite_render(seedHintSprite, cam); |
| 130 | +} |
| 131 | + |
| 132 | +void |
| 133 | +text_input_close(SDL_Window *window) |
| 134 | +{ |
| 135 | + SDL_StopTextInput(window); |
| 136 | + |
| 137 | + if (seedHeadingSprite) { |
| 138 | + sprite_destroy(seedHeadingSprite); |
| 139 | + seedHeadingSprite = NULL; |
| 140 | + } |
| 141 | + if (seedInputSprite) { |
| 142 | + sprite_destroy(seedInputSprite); |
| 143 | + seedInputSprite = NULL; |
| 144 | + } |
| 145 | + if (seedHintSprite) { |
| 146 | + sprite_destroy(seedHintSprite); |
| 147 | + seedHintSprite = NULL; |
| 148 | + } |
| 149 | +} |
0 commit comments