Added max size support for textinputs

master
thmaillarb 3 years ago
parent 54ab4dd4a7
commit e7ce58c720

@ -33,6 +33,7 @@ typedef struct
bool isActive; bool isActive;
SDL_Color textColor; SDL_Color textColor;
TTF_Font* textFont; TTF_Font* textFont;
size_t maxTextSize;
}TextInput; }TextInput;
bool addStringToInputTextValue(TextInput* textInput, const char* strToAdd); bool addStringToInputTextValue(TextInput* textInput, const char* strToAdd);
@ -82,7 +83,7 @@ bool drawTextInputOnRenderer(SDL_Renderer* renderer, const TextInput* textInput)
* \author Allan POINT * \author Allan POINT
* \date 05/01/2022 * \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 * \brief Free what is needed to be free and clear all data in the TextInput

@ -9,8 +9,19 @@ bool addStringToInputTextValue(TextInput* textInput, const char* strToAdd) {
const size_t lenText = strlen(textInput->value); const size_t lenText = strlen(textInput->value);
const size_t lenStrToAdd = strlen(strToAdd); const size_t lenStrToAdd = strlen(strToAdd);
/*
textInput->value = (char*) realloc(textInput->value, sizeof(char) * (lenText+lenStrToAdd+1)); 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); strcat(textInput->value, strToAdd);
textInput->cursorPosition += lenStrToAdd; textInput->cursorPosition += lenStrToAdd;
return true; return true;
@ -192,15 +203,17 @@ bool drawTextInputOnRenderer(SDL_Renderer* renderer, const TextInput* textInput)
return true; 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) if(textInput == NULL)
{ {
fprintf(stderr, "WARNING: Can't assign value to NULL to create TextInput\n"); fprintf(stderr, "WARNING: Can't assign value to NULL to create TextInput\n");
return false; return false;
} }
textInput->maxTextSize = maxTextSize <= 0 ? 0 : maxTextSize;
textInput->value = (char*) malloc(sizeof(char)); sizeToMalloc = textInput->maxTextSize == 0 ? sizeof(char) : maxTextSize * sizeof(char);
textInput->value = (char*) malloc(sizeToMalloc);
if(textInput->value == NULL) if(textInput->value == NULL)
{ {
fprintf(stderr, "WARNING: Can't allocate memory space to TextInput\n"); fprintf(stderr, "WARNING: Can't allocate memory space to TextInput\n");

@ -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}; SDL_Point colorPos = {.x=w*0.05, .y=h*0.55};
TextLabel colorLabel = createTextLabel(colorText, &colorPos, &black, font, renderer); 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"); fprintf(stderr, "WARNING: Can't init TextInput\n");
return false; return false;

@ -142,7 +142,7 @@ CreateMenuLine createCreateMenuLine(SDL_Renderer* renderer, int xmin, int y, int
// Text input of nickname // Text input of nickname
TextInput pseudoInput; TextInput pseudoInput;
initTextInput(&pseudoInput, &rect, NULL, font); initTextInput(&pseudoInput, &rect, NULL, font, 12);
// Color chooser // Color chooser
ChangeColorParams* params; ChangeColorParams* params;

@ -92,7 +92,7 @@ int testTextInput(void) {
// TextInput // TextInput
SDL_Rect size = {.x=10, .y=10, .w=90, .h=20}; 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"); fprintf(stderr, "WARNING: can't init TextInput\n");
goto Quit; goto Quit;
@ -129,11 +129,11 @@ int testTextInput(void) {
} }
} }
else else
{ {/*
if(!addCharacterToInputTextValueAtCursor(&textInput, writeErina(i))) if(!addCharacterToInputTextValueAtCursor(&textInput, writeErina(i)))
{ {
fprintf(stderr, "WARINING: can't add character in TextInput\n"); fprintf(stderr, "WARINING: can't add character in TextInput\n");
}; };*/
} }
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
SDL_Delay(500); SDL_Delay(500);

@ -52,7 +52,7 @@ void testTextInputProcessor() {
TextInput* textInput = array_TextInput_Last(&inputProcessor.tabTextInput); TextInput* textInput = array_TextInput_Last(&inputProcessor.tabTextInput);
SDL_Rect size = {.x=10, .y=10, .w=90, .h=20}; 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"); fprintf(stderr, "WARNING: can't init TextInput\n");
return; return;

Loading…
Cancel
Save