From 6d2d6819fa028026fa83a77ccfda3cbe5e69f31b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20Br=C3=BCll?= Date: Sat, 4 Jul 2020 11:30:33 +0200 Subject: [PATCH] GuiTextBoxMulti now will also break lines when encountering a newline character (#97) * Fix for issue #94: infinite loop on space-free sequences in text that are wider than textbox bounds marked no lastSpacePos as -1. check for a space-char had to come earlier than check for width overrun. if no space found in this line but line too wide, wrap sequence and start new line with textWidth of last character. reset lastSpacePos on every new line. * GuiTextBoxMulti now will also break when encountering a newline character Co-authored-by: Ray --- src/gui_textbox_extended.h | 2 ++ src/raygui.h | 14 ++++++-------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/gui_textbox_extended.h b/src/gui_textbox_extended.h index 23c0b01..954a0a0 100644 --- a/src/gui_textbox_extended.h +++ b/src/gui_textbox_extended.h @@ -92,7 +92,9 @@ RAYGUIDEF bool GuiTextBoxEx(Rectangle bounds, char *text, int textSize, bool edi #if defined(GUI_TEXTBOX_EXTENDED_IMPLEMENTATION) +#ifndef RAYGUI_H #include "raygui.h" +#endif //---------------------------------------------------------------------------------- // Defines and Macros diff --git a/src/raygui.h b/src/raygui.h index 3fff7e3..8518837 100644 --- a/src/raygui.h +++ b/src/raygui.h @@ -1768,26 +1768,24 @@ bool GuiTextBoxMulti(Rectangle bounds, char *text, int textSize, bool editMode) // Calculate cursor position considering text char oneCharText[2] = { 0 }; - int lastSpacePos = -1; + int lastBreakingPos = -1; for (int i = 0; i < keyCount && currentLine < keyCount; i++) { oneCharText[0] = text[i]; textWidth += (GetTextWidth(oneCharText) + GuiGetStyle(DEFAULT, TEXT_SPACING)); - if (text[i] == ' ') lastSpacePos = i; + if (text[i] == ' ' || text[i] == '\n') lastBreakingPos = i; - if (textWidth >= textAreaBounds.width) + if ( text[i] == '\n' || textWidth >= textAreaBounds.width) { currentLine++; textWidth = 0; - if(lastSpacePos > 0) - i = lastSpacePos; - else - textWidth += (GetTextWidth(oneCharText) + GuiGetStyle(DEFAULT, TEXT_SPACING)); + if (lastBreakingPos > 0) i = lastBreakingPos; + else textWidth += (GetTextWidth(oneCharText) + GuiGetStyle(DEFAULT, TEXT_SPACING)); - lastSpacePos = -1; + lastBreakingPos = -1; } }