From 5251885d4d01adfe9a43cfd92e378ec5d2b63ece Mon Sep 17 00:00:00 2001 From: div0 Date: Thu, 25 Dec 2008 18:03:28 +0000 Subject: [PATCH] terrencehill's color codes patch. Please test thoroughly! git-svn-id: svn://svn.icculus.org/nexuiz/trunk@5309 f962a42d-fe04-0410-a3ab-8c8b0445ebaa --- data/qcsrc/client/miscfunctions.qc | 21 +++-- data/qcsrc/menu/draw.qc | 29 ++++++- data/qcsrc/menu/item/inputbox.c | 125 +++++++++++++++++++++++++++-- 3 files changed, 162 insertions(+), 13 deletions(-) diff --git a/data/qcsrc/client/miscfunctions.qc b/data/qcsrc/client/miscfunctions.qc index b5388d9a9..89b85178e 100644 --- a/data/qcsrc/client/miscfunctions.qc +++ b/data/qcsrc/client/miscfunctions.qc @@ -136,11 +136,22 @@ float stringwidth_oldfont(string text, float handleColors) ch = str2chr(text, i+1); if(ch >= '0' && ch <= '9') ++i; - else - ++width; - } - else - ++width; + else if(i+4 < len && ch == 'x') + { + ch = str2chr(text, i+2); + if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) + { + ch = str2chr(text, i+3); + if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) + { + ch = str2chr(text, i+4); + if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) + i+=4; + else ++width; + } else ++width; + } else ++width; + } else ++width; + } else ++width; } return width; } diff --git a/data/qcsrc/menu/draw.qc b/data/qcsrc/menu/draw.qc index f8765e154..ac53c0e0e 100644 --- a/data/qcsrc/menu/draw.qc +++ b/data/qcsrc/menu/draw.qc @@ -295,6 +295,7 @@ float draw_TextLengthUpToWidth(string theText, float maxWidth, float ICanHasKall return strlen(theText); // yeah! // binary search for right place to cut string + float ch; float left, right, middle; // this always works left = 0; right = strlen(theText); // this always fails @@ -313,6 +314,32 @@ float draw_TextLengthUpToWidth(string theText, float maxWidth, float ICanHasKall // terminate, as the range still halves each time - but nevertheless, it is // guaranteed that it finds ONE valid cutoff place (where "left" is in // range, and "right" is outside). - + + // terencehill: the following code detects truncated ^xrgb tags (e.g. ^x or ^x4) + // and decrease left on the basis of the chars detected of the truncated tag + // Even if the ^xrgb tag is not complete/correct, left is decreased + // (sometimes too much but with a correct result) + // it fixes also ^[0-9] + if (left >= 1 && substring(theText, left-1, 1) == "^") // * it doesn't consider one or more ^ before the current ^ + left-=1; + else if (left >= 2 && substring(theText, left-2, 2) == "^x") // ^x/ + left-=2; + else if (left >= 3 && substring(theText, left-3, 2) == "^x") + { + ch = str2chr(theText, left-1); + if( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) // ^xr/ + left-=3; + } + else if (left >= 4 && substring(theText, left-4, 2) == "^x") + { + ch = str2chr(theText, left-2); + if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) + { + ch = str2chr(theText, left-1); + if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') ) // ^xrg/ + left-=4; + } + } + return left; } diff --git a/data/qcsrc/menu/item/inputbox.c b/data/qcsrc/menu/item/inputbox.c index 00f03350e..becfc40b9 100644 --- a/data/qcsrc/menu/item/inputbox.c +++ b/data/qcsrc/menu/item/inputbox.c @@ -163,13 +163,17 @@ void drawInputBox(entity me) string ch, ch2; float i; vector theColor; - float theAlpha; + float theAlpha; //float theVariableAlpha; vector p; float brightness; + vector theTempColor; + float component; + brightness = cvar("r_textbrightness"); p = me.realOrigin - eX * me.scrollPos; theColor = '1 1 1'; - theAlpha = 1; + theAlpha = 1; //theVariableAlpha = 1; // changes when ^ax found + for(i = 0; i < strlen(me.text); ++i) { ch = substring(me.text, i, 1); @@ -180,7 +184,7 @@ void drawInputBox(entity me) w = draw_TextWidth(strcat(ch, ch2), 0) * me.realFontSize_x; if(ch2 == "^") { - draw_Fill(p, eX * w + eY * me.realFontSize_y, '0 0 1', 0.5); + draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5); draw_Text(p + eX * 0.25 * w, "^", me.realFontSize, theColor, theAlpha, 0); } else if(ch2 == "0" || stof(ch2)) // digit? @@ -201,19 +205,126 @@ void drawInputBox(entity me) theColor = theColor * (1 - brightness) + brightness * '1 1 1'; draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5); draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0); - draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0); } + else if(ch2 == "x") // ^x found + { + theColor = '1 1 1' * (1 - brightness) + brightness * '1 1 1'; + theTempColor = '0 0 0'; + + component = str2chr(me.text, i+2); + if (component >= '0' && component <= '9') + component = component - '0'; + else if (component >= 'a' && component <= 'f') + component = component - 87; + else if (component >= 'A' && component <= 'F') + component = component - 55; + else + component = -1; + + if (component >= 0) // ^xr found + { + theTempColor_x = component/15; + + component = str2chr(me.text, i+3); + if (component >= '0' && component <= '9') + component = component - '0'; + else if (component >= 'a' && component <= 'f') + component = component - 87; + else if (component >= 'A' && component <= 'F') + component = component - 55; + else + component = -1; + + if (component >= 0) // ^xrg found + { + theTempColor_y = component/15; + + component = str2chr(me.text, i+4); + if (component >= '0' && component <= '9') + component = component - '0'; + else if (component >= 'a' && component <= 'f') + component = component - 87; + else if (component >= 'A' && component <= 'F') + component = component - 55; + else + component = -1; + + if (component >= 0) // ^xrgb found + { + theTempColor_z = component/15; + theColor = '0 0 0' + theTempColor; + w = draw_TextWidth(substring(me.text, i, 5), 0) * me.realFontSize_x; + + draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5); + draw_Text(p, substring(me.text, i, 5), me.realFontSize, theColor, 1, 0); // theVariableAlpha instead of 1 using alpha tags ^ax + i += 3; + } + else + { + // blue missing + w = draw_TextWidth(substring(me.text, i, 4), 0) * me.realFontSize_x; + draw_Fill(p, eX * w + eY * me.realFontSize_y, eZ, 0.5); + draw_Text(p, substring(me.text, i, 4), me.realFontSize, '1 1 1', theAlpha, 0); + i += 2; + } + } + else + { + // green missing + w = draw_TextWidth(substring(me.text, i, 3), 0) * me.realFontSize_x; + draw_Fill(p, eX * w + eY * me.realFontSize_y, eY, 0.5); + draw_Text(p, substring(me.text, i, 3), me.realFontSize, '1 1 1', theAlpha, 0); + i += 1; + } + } + else + { + // red missing + //w = draw_TextWidth(substring(me.text, i, 2), 0) * me.realFontSize_x; + draw_Fill(p, eX * w + eY * me.realFontSize_y, eX, 0.5); + draw_Text(p, substring(me.text, i, 2), me.realFontSize, '1 1 1', theAlpha, 0); + } + } + /*else if(ch2 == "a") // ^a found + { + draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5); + draw_Text(p, substring(me.text, i, 2), me.realFontSize, theColor, 0.8, 0); + + component = str2chr(me.text, i+2); + if (component >= '0' && component <= '9') + component = component - '0'; + else if (component >= 'a' && component <= 'f') + component = component - 87; + else if (component >= 'A' && component <= 'F') + component = component - 55; + else + component = -1; + + if (component >= 0) // ^ah found + { + // FIX ME: overflow here + if (component == 20 && theVariableAlpha <= 0.97) + theVariableAlpha = theVariableAlpha + 0.0625; + else if (component == 30 && theVariableAlpha >= 0.03) + theVariableAlpha = theVariableAlpha - 0.0625; + else + theVariableAlpha = component*0.0625; + + draw_Fill(p, eX * draw_TextWidth(substring(me.text, i, 3), 0) * me.realFontSize_x + eY * me.realFontSize_y, '0.8 0.8 0.8', 0.5); + draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, 0.8, 0); + } + }*/ else { - draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 0 0', 0.5); - draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0); + draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5); draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0); } p += w * eX; ++i; continue; } - draw_Text(p, ch, me.realFontSize, theColor, theAlpha, 0); p += eX * draw_TextWidth(ch, 0) * me.realFontSize_x; + draw_Text(p, ch, me.realFontSize, theColor, theAlpha, 0); // TODO theVariableAlpha + p += eX * draw_TextWidth(ch, 0) * me.realFontSize_x; } } else -- 2.39.2