From e7ce58c7202df36cd00e333c25909ff6859f54cd Mon Sep 17 00:00:00 2001 From: thmaillarb Date: Mon, 7 Mar 2022 21:00:04 +0100 Subject: [PATCH] Added max size support for textinputs --- Pontu/include/engine/TextInput.h | 3 ++- Pontu/src/engine/TextInput.c | 21 +++++++++++++++++---- Pontu/src/view/ConnectionMenu.c.old | 2 +- Pontu/src/view/GameCreationMenu.c | 2 +- Pontu/test/testTextInput.c | 6 +++--- Pontu/test/testTextInputWithProcessor.c | 2 +- 6 files changed, 25 insertions(+), 11 deletions(-) diff --git a/Pontu/include/engine/TextInput.h b/Pontu/include/engine/TextInput.h index 3e790d4..65437ef 100644 --- a/Pontu/include/engine/TextInput.h +++ b/Pontu/include/engine/TextInput.h @@ -33,6 +33,7 @@ typedef struct bool isActive; SDL_Color textColor; TTF_Font* textFont; + size_t maxTextSize; }TextInput; bool addStringToInputTextValue(TextInput* textInput, const char* strToAdd); @@ -82,7 +83,7 @@ bool drawTextInputOnRenderer(SDL_Renderer* renderer, const TextInput* textInput) * \author Allan POINT * \date 05/01/2022 */ -bool initTextInput(TextInput* textInput, const SDL_Rect* size, const SDL_Color* textColor, TTF_Font* textFont); +bool initTextInput(TextInput* textInput, const SDL_Rect* size, const SDL_Color* textColor, TTF_Font* textFont, size_t maxTextSize); /* * \brief Free what is needed to be free and clear all data in the TextInput diff --git a/Pontu/src/engine/TextInput.c b/Pontu/src/engine/TextInput.c index d337b3a..a39f22e 100644 --- a/Pontu/src/engine/TextInput.c +++ b/Pontu/src/engine/TextInput.c @@ -9,8 +9,19 @@ bool addStringToInputTextValue(TextInput* textInput, const char* strToAdd) { const size_t lenText = strlen(textInput->value); const size_t lenStrToAdd = strlen(strToAdd); - + /* textInput->value = (char*) realloc(textInput->value, sizeof(char) * (lenText+lenStrToAdd+1)); + strcat(textInput->value, strToAdd);*/ + + if (textInput->maxTextSize != 0) { + if (lenText + lenStrToAdd > textInput->maxTextSize) { + fprintf(stderr, "WARNING: text to be added is too long"); + return false; + } + } + else { + textInput->value = (char*) realloc(textInput->value, sizeof(char) * (lenText+lenStrToAdd+1)); + } strcat(textInput->value, strToAdd); textInput->cursorPosition += lenStrToAdd; return true; @@ -192,15 +203,17 @@ bool drawTextInputOnRenderer(SDL_Renderer* renderer, const TextInput* textInput) return true; } -bool initTextInput(TextInput* textInput, const SDL_Rect* size, const SDL_Color* textColor, TTF_Font* font) +bool initTextInput(TextInput* textInput, const SDL_Rect* size, const SDL_Color* textColor, TTF_Font* font, size_t maxTextSize) { + size_t sizeToMalloc; if(textInput == NULL) { fprintf(stderr, "WARNING: Can't assign value to NULL to create TextInput\n"); return false; } - - textInput->value = (char*) malloc(sizeof(char)); + textInput->maxTextSize = maxTextSize <= 0 ? 0 : maxTextSize; + sizeToMalloc = textInput->maxTextSize == 0 ? sizeof(char) : maxTextSize * sizeof(char); + textInput->value = (char*) malloc(sizeToMalloc); if(textInput->value == NULL) { fprintf(stderr, "WARNING: Can't allocate memory space to TextInput\n"); diff --git a/Pontu/src/view/ConnectionMenu.c.old b/Pontu/src/view/ConnectionMenu.c.old index 30452f2..406f21e 100644 --- a/Pontu/src/view/ConnectionMenu.c.old +++ b/Pontu/src/view/ConnectionMenu.c.old @@ -26,7 +26,7 @@ bool drawMenuConnection(const SDL_Renderer* renderer, TTF_Font* font, int w, in SDL_Point colorPos = {.x=w*0.05, .y=h*0.55}; TextLabel colorLabel = createTextLabel(colorText, &colorPos, &black, font, renderer); - if(!initTextInput(&pseudoInput, &pseudoInputRect, NULL, font)) + if(!initTextInput(&pseudoInput, &pseudoInputRect, NULL, font, 0)) // TODO set fixed size { fprintf(stderr, "WARNING: Can't init TextInput\n"); return false; diff --git a/Pontu/src/view/GameCreationMenu.c b/Pontu/src/view/GameCreationMenu.c index 767212a..aafe29e 100644 --- a/Pontu/src/view/GameCreationMenu.c +++ b/Pontu/src/view/GameCreationMenu.c @@ -142,7 +142,7 @@ CreateMenuLine createCreateMenuLine(SDL_Renderer* renderer, int xmin, int y, int // Text input of nickname TextInput pseudoInput; - initTextInput(&pseudoInput, &rect, NULL, font); + initTextInput(&pseudoInput, &rect, NULL, font, 12); // Color chooser ChangeColorParams* params; diff --git a/Pontu/test/testTextInput.c b/Pontu/test/testTextInput.c index ee2be5e..b0e4607 100644 --- a/Pontu/test/testTextInput.c +++ b/Pontu/test/testTextInput.c @@ -92,7 +92,7 @@ int testTextInput(void) { // TextInput SDL_Rect size = {.x=10, .y=10, .w=90, .h=20}; - if(!initTextInput(&textInput, &size, NULL, fontHandler.fonts[FONT_retro])) + if(!initTextInput(&textInput, &size, NULL, fontHandler.fonts[FONT_retro], 3)) { fprintf(stderr, "WARNING: can't init TextInput\n"); goto Quit; @@ -129,11 +129,11 @@ int testTextInput(void) { } } else - { + {/* if(!addCharacterToInputTextValueAtCursor(&textInput, writeErina(i))) { fprintf(stderr, "WARINING: can't add character in TextInput\n"); - }; + };*/ } SDL_RenderPresent(renderer); SDL_Delay(500); diff --git a/Pontu/test/testTextInputWithProcessor.c b/Pontu/test/testTextInputWithProcessor.c index e89d009..c9bd7b6 100644 --- a/Pontu/test/testTextInputWithProcessor.c +++ b/Pontu/test/testTextInputWithProcessor.c @@ -52,7 +52,7 @@ void testTextInputProcessor() { TextInput* textInput = array_TextInput_Last(&inputProcessor.tabTextInput); SDL_Rect size = {.x=10, .y=10, .w=90, .h=20}; - if(!initTextInput(textInput, &size, NULL, fontHandler.fonts[FONT_PublicPixel])) + if(!initTextInput(textInput, &size, NULL, fontHandler.fonts[FONT_PublicPixel], 3)) { fprintf(stderr, "WARNING: can't init TextInput\n"); return;