r2932 - in trunk/data/qcsrc/menu-div0test: . item nexuiz
DONOTREPLY at icculus.org
DONOTREPLY at icculus.org
Sat Nov 10 09:30:13 EST 2007
Author: div0
Date: 2007-11-10 09:30:13 -0500 (Sat, 10 Nov 2007)
New Revision: 2932
Added:
trunk/data/qcsrc/menu-div0test/item/inputbox.c
Modified:
trunk/data/qcsrc/menu-div0test/classes.c
trunk/data/qcsrc/menu-div0test/draw.qc
trunk/data/qcsrc/menu-div0test/draw.qh
trunk/data/qcsrc/menu-div0test/item/label.c
trunk/data/qcsrc/menu-div0test/item/listbox.c
trunk/data/qcsrc/menu-div0test/nexuiz/dialog_multiplayer_join.c
trunk/data/qcsrc/menu-div0test/nexuiz/serverlist.c
Log:
inputboxing!
Modified: trunk/data/qcsrc/menu-div0test/classes.c
===================================================================
--- trunk/data/qcsrc/menu-div0test/classes.c 2007-11-09 23:07:34 UTC (rev 2931)
+++ trunk/data/qcsrc/menu-div0test/classes.c 2007-11-10 14:30:13 UTC (rev 2932)
@@ -14,6 +14,7 @@
#include "item/tab.c"
#include "item/textslider.c"
#include "item/listbox.c"
+#include "item/inputbox.c"
#include "nexuiz/dialog.c"
#include "nexuiz/tab.c"
#include "nexuiz/mainwindow.c"
Modified: trunk/data/qcsrc/menu-div0test/draw.qc
===================================================================
--- trunk/data/qcsrc/menu-div0test/draw.qc 2007-11-09 23:07:34 UTC (rev 2931)
+++ trunk/data/qcsrc/menu-div0test/draw.qc 2007-11-10 14:30:13 UTC (rev 2932)
@@ -200,18 +200,21 @@
}
}
}
-void draw_Text(vector theOrigin, string theText, vector theSize, vector theColor, float theAlpha)
+void draw_Text(vector theOrigin, string theText, vector theSize, vector theColor, float theAlpha, float ICanHasKallerz)
{
if(theSize_x <= 0 || theSize_y <= 0)
error("Drawing zero size text?\n");
- drawstring(boxToGlobal(theOrigin, draw_shift, draw_scale), theText, boxToGlobalSize(theSize, draw_scale), theColor, theAlpha * draw_alpha, 0);
+ if(ICanHasKallerz)
+ drawcolorcodedstring(boxToGlobal(theOrigin, draw_shift, draw_scale), theText, boxToGlobalSize(theSize, draw_scale), theAlpha * draw_alpha, 0);
+ else
+ drawstring(boxToGlobal(theOrigin, draw_shift, draw_scale), theText, boxToGlobalSize(theSize, draw_scale), theColor, theAlpha * draw_alpha, 0);
}
-float draw_TextWidth(string theText)
+float draw_TextWidth(string theText, float ICanHasKallerz)
{
//return strlen(theText);
//print("draw_TextWidth \"", theText, "\"\n");
- return stringwidth(theText, 0);
+ return stringwidth(theText, ICanHasKallerz);
}
float draw_clipSet;
@@ -223,6 +226,17 @@
draw_clipSet = 1;
}
+void draw_SetClipRect(vector theOrigin, vector theScale)
+{
+ vector o, s;
+ if(draw_clipSet)
+ error("Already clipping, no stack implemented here, sorry");
+ o = boxToGlobal(theOrigin, draw_shift, draw_scale);
+ s = boxToGlobalSize(theScale, draw_scale);
+ drawsetcliparea(o_x, o_y, s_x, s_y);
+ draw_clipSet = 1;
+}
+
void draw_ClearClip()
{
if(!draw_clipSet)
@@ -231,15 +245,23 @@
draw_clipSet = 0;
}
-string draw_TextShortenToWidth(string theText, float maxWidth)
+string draw_TextShortenToWidth(string theText, float maxWidth, float ICanHasKallerz)
{
+ if(draw_TextWidth(theText, ICanHasKallerz) <= maxWidth)
+ return theText;
+ else
+ return strcat(substring(theText, 0, draw_TextLengthUpToWidth(theText, maxWidth = draw_TextWidth("...", ICanHasKallerz), ICanHasKallerz)), "...");
+}
+
+float draw_TextLengthUpToWidth(string theText, float maxWidth, float ICanHasKallerz)
+{
// STOP.
// The following function is SLOW.
// For your safety and for the protection of those around you...
// DO NOT CALL THIS AT HOME.
// No really, don't.
- if(draw_TextWidth(theText) <= maxWidth)
- return theText; // yeah!
+ if(draw_TextWidth(theText, ICanHasKallerz) <= maxWidth)
+ return strlen(theText); // yeah!
// binary search for right place to cut string
float left, right, middle; // this always works
@@ -248,7 +270,7 @@
do
{
middle = floor((left + right) / 2);
- if(draw_TextWidth(strcat(substring(theText, 0, middle), "...")) <= maxWidth)
+ if(draw_TextWidth(substring(theText, 0, middle), ICanHasKallerz) <= maxWidth)
left = middle;
else
right = middle;
@@ -261,5 +283,5 @@
// guaranteed that it finds ONE valid cutoff place (where "left" is in
// range, and "right" is outside).
- return strcat(substring(theText, 0, left), "...");
+ return left;
}
Modified: trunk/data/qcsrc/menu-div0test/draw.qh
===================================================================
--- trunk/data/qcsrc/menu-div0test/draw.qh 2007-11-09 23:07:34 UTC (rev 2931)
+++ trunk/data/qcsrc/menu-div0test/draw.qh 2007-11-10 14:30:13 UTC (rev 2932)
@@ -11,11 +11,13 @@
void draw_BorderPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha, vector theBorderSize);
void draw_Picture(vector origin, string pic, vector size, vector color, float alpha);
void draw_Fill(vector theOrigin, vector theSize, vector theColor, float theAlpha);
-void draw_Text(vector origin, string text, vector size, vector color, float alpha);
-float draw_TextWidth(string text);
-string draw_TextShortenToWidth(string text, float maxWidth);
+void draw_Text(vector origin, string text, vector size, vector color, float alpha, float allowColorCodes);
+float draw_TextWidth(string text, float allowColorCodes);
+string draw_TextShortenToWidth(string text, float maxWidth, float allowColorCodes);
+float draw_TextLengthUpToWidth(string text, float maxWidth, float allowColorCodes);
void draw_SetClip();
+void draw_SetClipRect(vector theOrigin, vector theScale);
void draw_ClearClip();
vector boxToGlobal(vector v, vector shift, vector scale);
Added: trunk/data/qcsrc/menu-div0test/item/inputbox.c
===================================================================
--- trunk/data/qcsrc/menu-div0test/item/inputbox.c (rev 0)
+++ trunk/data/qcsrc/menu-div0test/item/inputbox.c 2007-11-10 14:30:13 UTC (rev 2932)
@@ -0,0 +1,200 @@
+#ifdef INTERFACE
+CLASS(InputBox) EXTENDS(Label)
+ METHOD(InputBox, configureInputBox, void(entity, string, float, float, string))
+ METHOD(InputBox, draw, void(entity))
+ METHOD(InputBox, setText, void(entity, string))
+ METHOD(InputBox, keyDown, float(entity, float, float, float))
+ METHOD(InputBox, mouseRelease, float(entity, vector))
+ METHOD(InputBox, mousePress, float(entity, vector))
+ METHOD(InputBox, mouseDrag, float(entity, vector))
+
+ ATTRIB(InputBox, src, string, string_null)
+
+ ATTRIB(InputBox, cursorPos, float, 0) // characters
+ ATTRIB(InputBox, scrollPos, float, 0) // widths
+
+ ATTRIB(InputBox, focusable, float, 1)
+ ATTRIB(InputBox, lastChangeTime, float, 0)
+ ATTRIB(InputBox, dragScrollTimer, float, 0)
+ ATTRIB(InputBox, dragScrollPos, vector, '0 0 0')
+ ATTRIB(InputBox, pressed, float, 0)
+ ATTRIB(InputBox, editColorCodes, float, 1)
+ENDCLASS(InputBox)
+#endif
+
+#ifdef IMPLEMENTATION
+void configureInputBoxInputBox(entity me, string theText, float theCursorPos, float theFontSize, string gfx)
+{
+ configureLabelLabel(me, theText, theFontSize, 0.0);
+ me.src = gfx;
+ me.cursorPos = theCursorPos;
+}
+
+void setTextInputBox(entity me, string txt)
+{
+ if(me.text)
+ strunzone(me.text);
+ setTextLabel(me, strzone(txt));
+}
+
+float mouseDragInputBox(entity me, vector pos)
+{
+ float p;
+ me.dragScrollPos = pos;
+ p = me.scrollPos + pos_x - me.keepspaceLeft;
+ me.cursorPos = draw_TextLengthUpToWidth(me.text, p / me.realFontSize_x, 0);
+ me.lastChangeTime = time;
+ return 1;
+}
+
+float mousePressInputBox(entity me, vector pos)
+{
+ me.dragScrollTimer = 0;
+ me.pressed = 1;
+ return mouseDragInputBox(me, pos);
+}
+
+float mouseReleaseInputBox(entity me, vector pos)
+{
+ me.pressed = 0;
+ return mouseDragInputBox(me, pos);
+}
+
+float keyDownInputBox(entity me, float key, float ascii, float shift)
+{
+ me.lastChangeTime = time;
+ me.dragScrollTimer = 0;
+ if(ascii >= 32 && ascii != 127)
+ {
+ me.setText(me, strcat(substring(me.text, 0, me.cursorPos), chr(ascii), substring(me.text, me.cursorPos, strlen(me.text) - me.cursorPos)));
+ me.cursorPos += 1;
+ return 1;
+ }
+ switch(key)
+ {
+ case K_LEFTARROW:
+ me.cursorPos -= 1;
+ return 1;
+ case K_RIGHTARROW:
+ me.cursorPos += 1;
+ return 1;
+ case K_HOME:
+ me.cursorPos = 0;
+ return 1;
+ case K_END:
+ me.cursorPos = strlen(me.text);
+ return 1;
+ case K_BACKSPACE:
+ if(me.cursorPos > 0)
+ {
+ me.cursorPos -= 1;
+ me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
+ }
+ return 1;
+ case K_DEL:
+ me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
+ return 1;
+ }
+ return 0;
+}
+
+void drawInputBox(entity me)
+{
+#define CURSOR "_"
+ float cursorPosInWidths, totalSizeInWidths;
+
+ if(me.pressed)
+ me.mouseDrag(me, me.dragScrollPos); // simulate mouseDrag event
+
+ if(me.src)
+ {
+ if(me.focused)
+ draw_ButtonPicture('0 0 0', strcat(me.src, "_f"), '1 1 0', '1 1 1', 1);
+ else
+ draw_ButtonPicture('0 0 0', strcat(me.src, "_n"), '1 1 0', '1 1 1', 1);
+ }
+
+ me.cursorPos = bound(0, me.cursorPos, strlen(me.text));
+ cursorPosInWidths = draw_TextWidth(substring(me.text, 0, me.cursorPos), 0) * me.realFontSize_x;
+ totalSizeInWidths = draw_TextWidth(strcat(me.text, CURSOR), 0) * me.realFontSize_x;
+
+ me.dragScrollTimer -= frametime;
+ if(me.dragScrollTimer < 0)
+ {
+ float save;
+ save = me.scrollPos;
+ me.scrollPos = bound(cursorPosInWidths - (0.875 - me.keepspaceLeft - me.keepspaceRight), me.scrollPos, cursorPosInWidths - 0.125);
+ if(me.scrollPos != save)
+ me.dragScrollTimer = 0.2;
+ }
+ me.scrollPos = min(me.scrollPos, totalSizeInWidths - (1 - me.keepspaceRight - me.keepspaceLeft));
+ me.scrollPos = max(0, me.scrollPos);
+
+ draw_SetClipRect(eX * me.keepspaceLeft, eX * (1 - me.keepspaceLeft - me.keepspaceRight) + eY);
+ if(me.editColorCodes)
+ {
+ string out, ch, ch2;
+ float i;
+ vector theColor;
+ float theAlpha;
+ vector p;
+ float brightness;
+ brightness = cvar("r_textbrightness");
+ p = me.realOrigin - eX * me.scrollPos;
+ theColor = '1 1 1';
+ theAlpha = 1;
+ for(i = 0; i < strlen(me.text); ++i)
+ {
+ ch = substring(me.text, i, 1);
+ if(ch == "^")
+ {
+ float w;
+ ch2 = substring(me.text, i+1, 1);
+ 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_Text(p + eX * 0.25 * w, "^", me.realFontSize, theColor, theAlpha, 0);
+ }
+ else if(ch2 == "0" || stof(ch2)) // digit?
+ {
+ switch(stof(ch2))
+ {
+ case 0: theColor = '0 0 0'; theAlpha = 1; break;
+ case 1: theColor = '1 0 0'; theAlpha = 1; break;
+ case 2: theColor = '0 1 0'; theAlpha = 1; break;
+ case 3: theColor = '1 1 0'; theAlpha = 1; break;
+ case 4: theColor = '0 0 1'; theAlpha = 1; break;
+ case 5: theColor = '0 1 1'; theAlpha = 1; break;
+ case 6: theColor = '1 0 1'; theAlpha = 1; break;
+ case 7: theColor = '1 1 1'; theAlpha = 1; break;
+ case 8: theColor = '1 1 1'; theAlpha = 0.5; break;
+ case 9: theColor = '0.5 0.5 0.5'; theAlpha = 1; break;
+ }
+ theColor = theColor * (1 - brightness) + brightness * '1 1 1';
+ draw_Fill(p, eX * w + eY * me.realFontSize_y, '0 0 0', 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
+ {
+ 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_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;
+ }
+ }
+ else
+ draw_Text(me.realOrigin - eX * me.scrollPos, me.text, me.realFontSize, '1 1 1', 1, 0);
+ // skipping drawLabel(me);
+ draw_ClearClip();
+
+ if(!me.focused || (time - me.lastChangeTime) < floor(time - me.lastChangeTime) + 0.5)
+ draw_Text(me.realOrigin + eX * (cursorPosInWidths - me.scrollPos), CURSOR, me.realFontSize, '1 1 1', 1, 0);
+}
+#endif
Modified: trunk/data/qcsrc/menu-div0test/item/label.c
===================================================================
--- trunk/data/qcsrc/menu-div0test/item/label.c 2007-11-09 23:07:34 UTC (rev 2931)
+++ trunk/data/qcsrc/menu-div0test/item/label.c 2007-11-10 14:30:13 UTC (rev 2932)
@@ -5,7 +5,7 @@
METHOD(Label, resizeNotify, void(entity, vector, vector, vector, vector))
METHOD(Label, setText, void(entity, string))
METHOD(Label, toString, string(entity))
- ATTRIB(Label, text, string, "Big Red Button")
+ ATTRIB(Label, text, string, string_null)
ATTRIB(Label, fontSize, float, 8)
ATTRIB(Label, align, float, 0.5)
ATTRIB(Label, keepspaceLeft, float, 0) // for use by subclasses (radiobuttons for example)
@@ -24,14 +24,14 @@
void setTextLabel(entity me, string txt)
{
me.text = txt;
- me.realOrigin_x = me.align * (1 - me.keepspaceLeft - me.keepspaceRight - me.realFontSize_x * draw_TextWidth(me.text)) + me.keepspaceLeft;
+ me.realOrigin_x = me.align * (1 - me.keepspaceLeft - me.keepspaceRight - me.realFontSize_x * draw_TextWidth(me.text, 0)) + me.keepspaceLeft;
}
void resizeNotifyLabel(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
{
// absSize_y is height of label
me.realFontSize_y = me.fontSize / absSize_y;
me.realFontSize_x = me.fontSize / absSize_x;
- me.realOrigin_x = me.align * (1 - me.keepspaceLeft - me.keepspaceRight - me.realFontSize_x * draw_TextWidth(me.text)) + me.keepspaceLeft;
+ me.realOrigin_x = me.align * (1 - me.keepspaceLeft - me.keepspaceRight - me.realFontSize_x * draw_TextWidth(me.text, 0)) + me.keepspaceLeft;
me.realOrigin_y = 0.5 * (1 - me.realFontSize_y);
}
void configureLabelLabel(entity me, string txt, float sz, float algn)
@@ -42,7 +42,8 @@
}
void drawLabel(entity me)
{
- if(me.text && me.fontSize)
- draw_Text(me.realOrigin, me.text, me.realFontSize, '1 1 1', me.alpha);
+ if(me.fontSize)
+ if(me.text)
+ draw_Text(me.realOrigin, me.text, me.realFontSize, '1 1 1', me.alpha, 0);
}
#endif
Modified: trunk/data/qcsrc/menu-div0test/item/listbox.c
===================================================================
--- trunk/data/qcsrc/menu-div0test/item/listbox.c 2007-11-09 23:07:34 UTC (rev 2931)
+++ trunk/data/qcsrc/menu-div0test/item/listbox.c 2007-11-10 14:30:13 UTC (rev 2932)
@@ -253,6 +253,6 @@
void drawListBoxItemListBox(entity me, float i, vector absSize, float selected)
{
- draw_Text('0 0 0', strcat("Item ", ftos(i)), eX * (8 / absSize_x) + eY * (8 / absSize_y), (selected ? '0 1 0' : '1 1 1'), 1);
+ draw_Text('0 0 0', strcat("Item ", ftos(i)), eX * (8 / absSize_x) + eY * (8 / absSize_y), (selected ? '0 1 0' : '1 1 1'), 1, 0);
}
#endif
Modified: trunk/data/qcsrc/menu-div0test/nexuiz/dialog_multiplayer_join.c
===================================================================
--- trunk/data/qcsrc/menu-div0test/nexuiz/dialog_multiplayer_join.c 2007-11-09 23:07:34 UTC (rev 2931)
+++ trunk/data/qcsrc/menu-div0test/nexuiz/dialog_multiplayer_join.c 2007-11-10 14:30:13 UTC (rev 2932)
@@ -10,6 +10,16 @@
#endif
#ifdef IMPLEMENTATION
+entity makeNexuizTextBox()
+{
+ entity e;
+ e = spawnInputBox();
+ e.configureInputBox(e, "hello world", 3, 12, "qcsrc/menu-div0test/basebutton");
+ e.keepspaceLeft = 0.1;
+ e.keepspaceRight = 0.2;
+ return e;
+}
+
entity makeNexuizServerListTab()
{
entity me;
@@ -24,7 +34,8 @@
slist = makeNexuizServerList();
me.TR(me);
- me.TD(me, 1, me.columns - 1.5, e = makeNexuizTextLabel(0, "Filter:"));
+ me.TD(me, 1, 0.5, e = makeNexuizTextLabel(0, "Filter:"));
+ me.TD(me, 1, me.columns - 2, e = makeNexuizTextBox());
me.TD(me, 1, 0.75, e = makeNexuizCheckBox(0, string_null, "Empty"));
e.checked = slist.filterShowEmpty;
e.onClickEntity = slist;
Modified: trunk/data/qcsrc/menu-div0test/nexuiz/serverlist.c
===================================================================
--- trunk/data/qcsrc/menu-div0test/nexuiz/serverlist.c 2007-11-09 23:07:34 UTC (rev 2931)
+++ trunk/data/qcsrc/menu-div0test/nexuiz/serverlist.c 2007-11-10 14:30:13 UTC (rev 2932)
@@ -328,12 +328,12 @@
}
s = ftos(p);
- draw_Text(me.realUpperMargin * eY + (me.columnPingSize - draw_TextWidth(s) * me.realFontSize_x) * eX, s, me.realFontSize, theColor, theAlpha);
- s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_NAME, i), me.columnNameSize / me.realFontSize_x);
- draw_Text(me.realUpperMargin * eY + me.columnNameOrigin * eX, s, me.realFontSize, theColor, theAlpha);
- s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_MAP, i), me.columnMapSize / me.realFontSize_x);
- draw_Text(me.realUpperMargin * eY + (me.columnMapOrigin + (me.columnMapSize - draw_TextWidth(s) * me.realFontSize_x) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha);
+ draw_Text(me.realUpperMargin * eY + (me.columnPingSize - draw_TextWidth(s, 0) * me.realFontSize_x) * eX, s, me.realFontSize, theColor, theAlpha, 0);
+ s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_NAME, i), me.columnNameSize / me.realFontSize_x, 0);
+ draw_Text(me.realUpperMargin * eY + me.columnNameOrigin * eX, s, me.realFontSize, theColor, theAlpha, 0);
+ s = draw_TextShortenToWidth(gethostcachestring(SLIST_FIELD_MAP, i), me.columnMapSize / me.realFontSize_x, 0);
+ draw_Text(me.realUpperMargin * eY + (me.columnMapOrigin + (me.columnMapSize - draw_TextWidth(s, 0) * me.realFontSize_x) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
s = strcat(ftos(gethostcachenumber(SLIST_FIELD_NUMHUMANS, i)), "/", ftos(gethostcachenumber(SLIST_FIELD_MAXPLAYERS, i)));
- draw_Text(me.realUpperMargin * eY + (me.columnPlayersOrigin + (me.columnPlayersSize - draw_TextWidth(s) * me.realFontSize_x) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha);
+ draw_Text(me.realUpperMargin * eY + (me.columnPlayersOrigin + (me.columnPlayersSize - draw_TextWidth(s, 0) * me.realFontSize_x) * 0.5) * eX, s, me.realFontSize, theColor, theAlpha, 0);
}
#endif
More information about the nexuiz-commits
mailing list