From: terencehill Date: Tue, 5 Jan 2021 19:08:20 +0000 (+0100) Subject: Use checkColorCode in textLengthUpToWidth and get rid of skipIncompleteTag X-Git-Tag: xonotic-v0.8.5~608^2~2 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=3d26fc0c5b264d9564d5756884737b743012e7e8;p=xonotic%2Fxonotic-data.pk3dir.git Use checkColorCode in textLengthUpToWidth and get rid of skipIncompleteTag --- diff --git a/qcsrc/common/util.qc b/qcsrc/common/util.qc index 8f37802ae..96ae0b66c 100644 --- a/qcsrc/common/util.qc +++ b/qcsrc/common/util.qc @@ -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 diff --git a/qcsrc/lib/string.qh b/qcsrc/lib/string.qh index 69ecc7b4a..7f3443d6c 100644 --- a/qcsrc/lib/string.qh +++ b/qcsrc/lib/string.qh @@ -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'; } diff --git a/qcsrc/menu/xonotic/colorpicker.qc b/qcsrc/menu/xonotic/colorpicker.qc index c4dfa2b6b..16b71e04e 100644 --- a/qcsrc/menu/xonotic/colorpicker.qc +++ b/qcsrc/menu/xonotic/colorpicker.qc @@ -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) == "^")