]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/menu/item/inputbox.c
properly check the player name length as utf-8 octet count
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / item / inputbox.c
index 2d64590306139cc97f1b860ac56c5e7a72de836a..750bc83b890453eb917fcaaac3aa804a54142727 100644 (file)
@@ -25,7 +25,7 @@ CLASS(InputBox) EXTENDS(Label)
        ATTRIB(InputBox, forbiddenCharacters, string, "")
        ATTRIB(InputBox, color, vector, '1 1 1')
        ATTRIB(InputBox, colorF, vector, '1 1 1')
-       ATTRIB(InputBox, maxLength, float, 255)
+       ATTRIB(InputBox, maxLength, float, 255) // if negative, it counts bytes, not chars
 ENDCLASS(InputBox)
 void InputBox_Clear_Click(entity btn, entity me);
 #endif
@@ -79,8 +79,16 @@ void InputBox_enterText(entity me, string ch)
        for(i = 0; i < strlen(ch); ++i)
                if(strstrofs(me.forbiddenCharacters, substring(ch, i, 1), 0) > -1)
                        return;
-       if(strlen(ch) + strlen(me.text) > me.maxLength)
-               return;
+       if(me.maxLength > 0)
+       {
+               if(strlen(ch) + strlen(me.text) > me.maxLength)
+                       return;
+       }
+       else if(me.maxLength < 0)
+       {
+               if(u8_strsize(ch) + u8_strsize(me.text) > -me.maxLength)
+                       return;
+       }
        me.setText(me, strcat(substring(me.text, 0, me.cursorPos), ch, substring(me.text, me.cursorPos, strlen(me.text) - me.cursorPos)));
        me.cursorPos += strlen(ch);
 }