]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Use checkColorCode in textLengthUpToWidth and get rid of skipIncompleteTag
authorterencehill <piuntn@gmail.com>
Tue, 5 Jan 2021 19:08:20 +0000 (20:08 +0100)
committerterencehill <piuntn@gmail.com>
Tue, 5 Jan 2021 19:08:20 +0000 (20:08 +0100)
qcsrc/common/util.qc
qcsrc/lib/string.qh
qcsrc/menu/xonotic/colorpicker.qc

index 8f37802ae70ed6b9c1f96ded44553bc60ab720fa..96ae0b66c5f8b50855388dd7db0d50ef159e34c8 100644 (file)
@@ -770,49 +770,6 @@ int cvar_settemp_restore()
        return j;
 }
 
-int skipIncompleteTag(string theText, float pos, int len)
-{
-       int tag_start = -1;
-
-       if(substring(theText, pos - 1, 1) == "^")
-       {
-               if(isCaretEscaped(theText, pos - 1) || pos >= len)
-                       return 0;
-
-               int ch = str2chr(theText, pos);
-               if(ch >= '0' && ch <= '9')
-                       return 1; // ^[0-9] color code found
-               else if (ch == 'x')
-                       tag_start = pos - 1; // ^x tag found
-               else
-                       return 0;
-       }
-       else
-       {
-               for(int i = 2; pos - i >= 0 && i <= 4; ++i)
-               {
-                       if(substring(theText, pos - i, 2) == "^x")
-                       {
-                               tag_start = pos - i; // ^x tag found
-                               break;
-                       }
-               }
-       }
-
-       if(tag_start >= 0)
-       {
-               if(tag_start + 5 < len)
-               if(IS_HEXDIGIT(substring(theText, tag_start + 2, 1)))
-               if(IS_HEXDIGIT(substring(theText, tag_start + 3, 1)))
-               if(IS_HEXDIGIT(substring(theText, tag_start + 4, 1)))
-               {
-                       if(!isCaretEscaped(theText, tag_start))
-                               return 5 - (pos - tag_start); // ^xRGB color code found
-               }
-       }
-       return 0;
-}
-
 float textLengthUpToWidth(string theText, float maxWidth, vector theSize, textLengthUpToWidth_widthFunction_t w)
 {
        // STOP.
@@ -834,7 +791,11 @@ float textLengthUpToWidth(string theText, float maxWidth, vector theSize, textLe
        {
                middle = floor((left + right) / 2);
                if(colors)
-                       ofs = skipIncompleteTag(theText, middle, len);
+               {
+                       vector res = checkColorCode(theText, len, middle, false);
+                       ofs = (res.x) ? res.x - res.y : 0;
+               }
+
                if(w(substring(theText, 0, middle + ofs), theSize) <= maxWidth)
                        left = middle + ofs;
                else
@@ -866,7 +827,11 @@ float textLengthUpToLength(string theText, float maxWidth, textLengthUpToLength_
        {
                middle = floor((left + right) / 2);
                if(colors)
-                       ofs = skipIncompleteTag(theText, middle, len);
+               {
+                       vector res = checkColorCode(theText, len, middle, true);
+                       ofs = (!res.x) ? 0 : res.x - res.y;
+               }
+
                if(w(substring(theText, 0, middle + ofs)) <= maxWidth)
                        left = middle + ofs;
                else
index 69ecc7b4a7dfc3da9322acfb48f8518b24810fb8..7f3443d6c04fc5cdb54b33a37498366ee4438d0e 100644 (file)
@@ -462,29 +462,33 @@ bool isValidColorCodeValue(string theText, int cc_len, int tag_start)
 }
 
 // it returns 0 if pos is NOT in the middle or at the end of a color code
-// otherwise it returns a 2-digit number with cc_len as the first digit
-// and the offset from '^' position to pos as the second digit
+// otherwise it returns a vector with color code length as the first component
+// and the offset from '^' position to pos as the second component
 // e.g.:
-// "a^2xy" | returns 0 if pos == 0 or 1 or 4
-//    ^^   | returns 21 or 22 if pos == 2 or 3
+// "j^2kl" | returns 0 if pos == 0 or 1 or 4
+//    ^^   | returns '2 1' or '2 2' if pos == 2 or 3
 ERASEABLE
-int checkColorCode(string theText, int pos)
+vector checkColorCode(string theText, int text_len, int pos, bool check_at_the_end)
 {
-       int text_len = strlen(theText);
+       if (text_len == 0)
+               text_len = strlen(theText);
        string tag_type = "^";
        int cc_len = 2;
        int tag_len = 1;
 
        LABEL(check_color_tag)
 
-       for (int ofs = cc_len; ofs >= 1; ofs--)
+       int ofs = cc_len;
+       if (!check_at_the_end)
+               ofs--;
+       for (; ofs >= 1; ofs--)
        {
                if (!(pos >= ofs && text_len >= pos + (cc_len - ofs)))
                        continue;
                if(substring(theText, pos - ofs, tag_len) == tag_type)
                {
                        if (!isCaretEscaped(theText, pos - ofs) && isValidColorCodeValue(theText, cc_len, pos - ofs))
-                               return cc_len * 10 + ofs;
+                               return eX * cc_len + eY * ofs;
                }
        }
        if (cc_len == 2)
@@ -494,5 +498,5 @@ int checkColorCode(string theText, int pos)
                tag_len = 2;
                goto check_color_tag;
        }
-       return 0;
+       return '0 0 0';
 }
index c4dfa2b6b3b8fb4fad24ccb6d4786866f6060f68..16b71e04e900a7bdb43de661f1a85ca1f893ee73 100644 (file)
@@ -62,20 +62,17 @@ float XonoticColorpicker_mouseDrag(entity me, vector coords)
        for (;;)
        {
                i = me.controlledTextbox.cursorPos;
-
-               int res = checkColorCode(me.controlledTextbox.text, i);
-               if (res)
-               {
-                       int tag_length = floor(res / 10);
-                       int ofs = res % 10;
-                       for (int j = tag_length - ofs; j > 0; j--)
-                               me.controlledTextbox.keyDown(me.controlledTextbox, K_RIGHTARROW, 8, 0);
-                       for (int j = tag_length; j > 0; j--)
-                               me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
-                       continue;
-               }
-
-               break;
+               string theText = me.controlledTextbox.text;
+               vector res = checkColorCode(theText, strlen(theText), i, true);
+               if (!res.x)
+                       break;
+
+               int cc_len = res.x;
+               int ofs = res.y;
+               for (int j = cc_len - ofs; j > 0; j--)
+                       me.controlledTextbox.keyDown(me.controlledTextbox, K_RIGHTARROW, 8, 0);
+               for (int j = cc_len; j > 0; j--)
+                       me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
        }
 
        if(substring(me.controlledTextbox.text, i-1, 1) == "^")