]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/common/util.qc
get rid of 'local' prefixes (does nothing)
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / util.qc
index 23d4f3431941958bbf188467aa90ab8ede62061e..d02fa1e2cfd65c652aba8bc3a89012875f18e5cf 100644 (file)
@@ -41,8 +41,8 @@ void wordwrap_sprint(string s, float l)
 
 string unescape(string in)
 {
-       local float i, len;
-       local string str, s;
+       float i, len;
+       string str, s;
 
        // but it doesn't seem to be necessary in my tests at least
        in = strzone(in);
@@ -72,8 +72,8 @@ string unescape(string in)
 
 void wordwrap_cb(string s, float l, void(string) callback)
 {
-       local string c;
-       local float lleft, i, j, wlen;
+       string c;
+       float lleft, i, j, wlen;
 
        s = strzone(s);
        lleft = l;
@@ -1153,11 +1153,6 @@ float boxinsidebox(vector smins, vector smaxs, vector bmins, vector bmaxs) {retu
 
 float textLengthUpToWidth(string theText, float maxWidth, vector theSize, textLengthUpToWidth_widthFunction_t w)
 {
-       float ICanHasKallerz;
-
-       // detect color codes support in the width function
-       ICanHasKallerz = (w("^7", theSize) == 0);
-
        // STOP.
        // The following function is SLOW.
        // For your safety and for the protection of those around you...
@@ -1181,7 +1176,7 @@ float textLengthUpToWidth(string theText, float maxWidth, vector theSize, textLe
        }
        while(left < right - 1);
 
-       if(ICanHasKallerz)
+       if(w("^7", theSize) == 0) // detect color codes support in the width function
        {
                // NOTE: when color codes are involved, this binary search is,
                // mathematically, BROKEN. However, it is obviously guaranteed to
@@ -1222,11 +1217,6 @@ float textLengthUpToWidth(string theText, float maxWidth, vector theSize, textLe
 
 float textLengthUpToLength(string theText, float maxWidth, textLengthUpToLength_lenFunction_t w)
 {
-       float ICanHasKallerz;
-
-       // detect color codes support in the width function
-       ICanHasKallerz = (w("^7") == 0);
-
        // STOP.
        // The following function is SLOW.
        // For your safety and for the protection of those around you...
@@ -1250,7 +1240,7 @@ float textLengthUpToLength(string theText, float maxWidth, textLengthUpToLength_
        }
        while(left < right - 1);
 
-       if(ICanHasKallerz)
+       if(w("^7") == 0) // detect color codes support in the width function
        {
                // NOTE: when color codes are involved, this binary search is,
                // mathematically, BROKEN. However, it is obviously guaranteed to
@@ -1289,6 +1279,42 @@ float textLengthUpToLength(string theText, float maxWidth, textLengthUpToLength_
        return left;
 }
 
+string find_last_color_code(string s)
+{
+       float start, len, i, carets;
+       start = strstrofs(s, "^", 0);
+       if (start == -1) // no caret found
+               return "";
+       len = strlen(s)-1;
+       for(i = len; i >= start; --i)
+       {
+               if(substring(s, i, 1) != "^")
+                       continue;
+
+               carets = 1;
+               while (i-carets >= start && substring(s, i-carets, 1) == "^")
+                       ++carets;
+
+               // check if carets aren't all escaped
+               if (carets == 1 || mod(carets, 2) == 1) // first check is just an optimization
+               {
+                       if(i+1 <= len)
+                       if(strstrofs("0123456789", substring(s, i+1, 1), 0) >= 0)
+                               return substring(s, i, 2);
+
+                       if(i+4 <= len)
+                       if(substring(s, i+1, 1) == "x")
+                       if(strstrofs("0123456789abcdefABCDEF", substring(s, i+2, 1), 0) >= 0)
+                       if(strstrofs("0123456789abcdefABCDEF", substring(s, i+3, 1), 0) >= 0)
+                       if(strstrofs("0123456789abcdefABCDEF", substring(s, i+4, 1), 0) >= 0)
+                               return substring(s, i, 5);
+               }
+               i -= carets; // this also skips one char before the carets
+       }
+
+       return "";
+}
+
 string getWrappedLine(float w, vector theFontSize, textLengthUpToWidth_widthFunction_t tw)
 {
        float cantake;
@@ -1308,6 +1334,8 @@ string getWrappedLine(float w, vector theFontSize, textLengthUpToWidth_widthFunc
                        getWrappedLine_remaining = substring(s, cantake, strlen(s) - cantake);
                        if(getWrappedLine_remaining == "")
                                getWrappedLine_remaining = string_null;
+                       else if (tw("^7", theFontSize) == 0)
+                               getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, cantake)), getWrappedLine_remaining);
                        return substring(s, 0, cantake);
                }
                else
@@ -1315,6 +1343,8 @@ string getWrappedLine(float w, vector theFontSize, textLengthUpToWidth_widthFunc
                        getWrappedLine_remaining = substring(s, take + 1, strlen(s) - take);
                        if(getWrappedLine_remaining == "")
                                getWrappedLine_remaining = string_null;
+                       else if (tw("^7", theFontSize) == 0)
+                               getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, take)), getWrappedLine_remaining);
                        return substring(s, 0, take);
                }
        }
@@ -1344,6 +1374,8 @@ string getWrappedLineLen(float w, textLengthUpToLength_lenFunction_t tw)
                        getWrappedLine_remaining = substring(s, cantake, strlen(s) - cantake);
                        if(getWrappedLine_remaining == "")
                                getWrappedLine_remaining = string_null;
+                       else if (tw("^7") == 0)
+                               getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, cantake)), getWrappedLine_remaining);
                        return substring(s, 0, cantake);
                }
                else
@@ -1351,6 +1383,8 @@ string getWrappedLineLen(float w, textLengthUpToLength_lenFunction_t tw)
                        getWrappedLine_remaining = substring(s, take + 1, strlen(s) - take);
                        if(getWrappedLine_remaining == "")
                                getWrappedLine_remaining = string_null;
+                       else if (tw("^7") == 0)
+                               getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, take)), getWrappedLine_remaining);
                        return substring(s, 0, take);
                }
        }