2 string draw_mousepointer;
4 void draw_setMousePointer(string pic)
6 draw_mousepointer = pic;
9 void draw_drawMousePointer(vector where)
11 drawpic(boxToGlobal(where, draw_shift, draw_scale), draw_mousepointer, '32 32 0', '1 1 1', draw_alpha, 0);
18 draw_scale = '1 0 0' * cvar("vid_conwidth") + '0 1 0' * cvar("vid_conheight");
22 vector globalToBox(vector v, vector theOrigin, vector theScale)
30 vector globalToBoxSize(vector v, vector theScale)
37 vector boxToGlobal(vector v, vector theOrigin, vector theScale)
45 vector boxToGlobalSize(vector v, vector theScale)
52 void draw_Picture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha)
54 drawpic(boxToGlobal(theOrigin, draw_shift, draw_scale), pic, boxToGlobalSize(theSize, draw_scale), theColor, theAlpha * draw_alpha, 0);
57 void draw_Fill(vector theOrigin, vector theSize, vector theColor, float theAlpha)
59 drawfill(boxToGlobal(theOrigin, draw_shift, draw_scale), boxToGlobalSize(theSize, draw_scale), theColor, theAlpha * draw_alpha, 0);
62 // a button picture is a texture containing three parts:
63 // 1/4 width: left part
64 // 1/2 width: middle part (stretched)
65 // 1/4 width: right part
66 // it is assumed to be 4x as wide as high for aspect ratio purposes, which
67 // means, the parts are a square, two squares and a square.
68 void draw_ButtonPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha)
73 theOrigin = boxToGlobal(theOrigin, draw_shift, draw_scale);
74 theSize = boxToGlobalSize(theSize, draw_scale);
75 theAlpha *= draw_alpha;
76 width = eX * theSize_x;
77 height = eY * theSize_y;
78 if(theSize_x <= theSize_y * 2)
80 // button not wide enough
81 // draw just left and right part then
82 square = eX * theSize_x * 0.5;
83 bW = eX * (0.25 * theSize_x / (theSize_y * 2));
84 drawsubpic(theOrigin, square + height, pic, '0 0 0', eY + bW, theColor, theAlpha, 0);
85 drawsubpic(theOrigin + square, square + height, pic, eX - bW, eY + bW, theColor, theAlpha, 0);
89 square = eX * theSize_y;
90 drawsubpic(theOrigin, height + square, pic, '0 0 0', '0.25 1 0', theColor, theAlpha, 0);
91 drawsubpic(theOrigin + square, theSize - 2 * square, pic, '0.25 0 0', '0.5 1 0', theColor, theAlpha, 0);
92 drawsubpic(theOrigin + width - square, height + square, pic, '0.75 0 0', '0.25 1 0', theColor, theAlpha, 0);
96 // a vertical button picture is a texture containing three parts:
97 // 1/4 height: left part
98 // 1/2 height: middle part (stretched)
99 // 1/4 height: right part
100 // it is assumed to be 4x as high as wide for aspect ratio purposes, which
101 // means, the parts are a square, two squares and a square.
102 void draw_VertButtonPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha)
105 vector width, height;
107 theOrigin = boxToGlobal(theOrigin, draw_shift, draw_scale);
108 theSize = boxToGlobalSize(theSize, draw_scale);
109 theAlpha *= draw_alpha;
110 width = eX * theSize_x;
111 height = eY * theSize_y;
112 if(theSize_y <= theSize_x * 2)
114 // button not high enough
115 // draw just upper and lower part then
116 square = eY * theSize_y * 0.5;
117 bH = eY * (0.25 * theSize_y / (theSize_x * 2));
118 drawsubpic(theOrigin, square + width, pic, '0 0 0', eX + bH, theColor, theAlpha, 0);
119 drawsubpic(theOrigin + square, square + width, pic, eY - bH, eX + bH, theColor, theAlpha, 0);
123 square = eY * theSize_x;
124 drawsubpic(theOrigin, width + square, pic, '0 0 0', '1 0.25 0', theColor, theAlpha, 0);
125 drawsubpic(theOrigin + square, theSize - 2 * square, pic, '0 0.25 0', '1 0.5 0', theColor, theAlpha, 0);
126 drawsubpic(theOrigin + height - square, width + square, pic, '0 0.75 0', '1 0.25 0', theColor, theAlpha, 0);
130 // a border picture is a texture containing nine parts:
131 // 1/4 width: left part
132 // 1/2 width: middle part (stretched)
133 // 1/4 width: right part
135 // 1/4 height: top part
136 // 1/2 height: middle part (stretched)
137 // 1/4 height: bottom part
138 void draw_BorderPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha, vector theBorderSize)
141 vector width, height;
143 theOrigin = boxToGlobal(theOrigin, draw_shift, draw_scale);
144 theSize = boxToGlobalSize(theSize, draw_scale);
145 theBorderSize = boxToGlobalSize(theBorderSize, draw_scale);
146 theAlpha *= draw_alpha;
147 width = eX * theSize_x;
148 height = eY * theSize_y;
149 if(theSize_x <= theBorderSize_x * 2)
151 // not wide enough... draw just left and right then
152 bW = eX * (0.25 * theSize_x / (theBorderSize_x * 2));
153 if(theSize_y <= theBorderSize_y * 2)
155 // not high enough... draw just corners
156 bH = eY * (0.25 * theSize_y / (theBorderSize_y * 2));
157 drawsubpic(theOrigin, width * 0.5 + height * 0.5, pic, '0 0 0', bW + bH, theColor, theAlpha, 0);
158 drawsubpic(theOrigin + width * 0.5, width * 0.5 + height * 0.5, pic, eX - bW, bW + bH, theColor, theAlpha, 0);
159 drawsubpic(theOrigin + height * 0.5, width * 0.5 + height * 0.5, pic, eY - bH, bW + bH, theColor, theAlpha, 0);
160 drawsubpic(theOrigin + theSize * 0.5, width * 0.5 + height * 0.5, pic, eX + eY - bW - bH, bW + bH, theColor, theAlpha, 0);
164 dY = theBorderSize_x * eY;
165 drawsubpic(theOrigin, width * 0.5 + dY, pic, '0 0 0', '0 0.25 0' + bW, theColor, theAlpha, 0);
166 drawsubpic(theOrigin + width * 0.5, width * 0.5 + dY, pic, '0 0 0' + eX - bW, '0 0.25 0' + bW, theColor, theAlpha, 0);
167 drawsubpic(theOrigin + dY, width * 0.5 + height - 2 * dY, pic, '0 0.25 0', '0 0.5 0' + bW, theColor, theAlpha, 0);
168 drawsubpic(theOrigin + width * 0.5 + dY, width * 0.5 + height - 2 * dY, pic, '0 0.25 0' + eX - bW, '0 0.5 0' + bW, theColor, theAlpha, 0);
169 drawsubpic(theOrigin + height - dY, width * 0.5 + dY, pic, '0 0.75 0', '0 0.25 0' + bW, theColor, theAlpha, 0);
170 drawsubpic(theOrigin + width * 0.5 + height - dY, width * 0.5 + dY, pic, '0 0.75 0' + eX - bW, '0 0.25 0' + bW, theColor, theAlpha, 0);
175 if(theSize_y <= theBorderSize_y * 2)
177 // not high enough... draw just top and bottom then
178 bH = eY * (0.25 * theSize_y / (theBorderSize_y * 2));
179 dX = theBorderSize_x * eX;
180 drawsubpic(theOrigin, dX + height * 0.5, pic, '0 0 0', '0.25 0 0' + bH, theColor, theAlpha, 0);
181 drawsubpic(theOrigin + dX, width - 2 * dX + height * 0.5, pic, '0.25 0 0', '0.5 0 0' + bH, theColor, theAlpha, 0);
182 drawsubpic(theOrigin + width - dX, dX + height * 0.5, pic, '0.75 0 0', '0.25 0 0' + bH, theColor, theAlpha, 0);
183 drawsubpic(theOrigin + height * 0.5, dX + height * 0.5, pic, '0 0 0' + eY - bH, '0.25 0 0' + bH, theColor, theAlpha, 0);
184 drawsubpic(theOrigin + dX + height * 0.5, width - 2 * dX + height * 0.5, pic, '0.25 0 0' + eY - bH, '0.5 0 0' + bH, theColor, theAlpha, 0);
185 drawsubpic(theOrigin + width - dX + height * 0.5, dX + height * 0.5, pic, '0.75 0 0' + eY - bH, '0.25 0 0' + bH, theColor, theAlpha, 0);
189 dX = theBorderSize_x * eX;
190 dY = theBorderSize_x * eY;
191 drawsubpic(theOrigin, dX + dY, pic, '0 0 0', '0.25 0.25 0', theColor, theAlpha, 0);
192 drawsubpic(theOrigin + dX, width - 2 * dX + dY, pic, '0.25 0 0', '0.5 0.25 0', theColor, theAlpha, 0);
193 drawsubpic(theOrigin + width - dX, dX + dY, pic, '0.75 0 0', '0.25 0.25 0', theColor, theAlpha, 0);
194 drawsubpic(theOrigin + dY, dX + height - 2 * dY, pic, '0 0.25 0', '0.25 0.5 0', theColor, theAlpha, 0);
195 drawsubpic(theOrigin + dY + dX, width - 2 * dX + height - 2 * dY, pic, '0.25 0.25 0', '0.5 0.5 0', theColor, theAlpha, 0);
196 drawsubpic(theOrigin + dY + width - dX, dX + height - 2 * dY, pic, '0.75 0.25 0', '0.25 0.5 0', theColor, theAlpha, 0);
197 drawsubpic(theOrigin + height - dY, dX + dY, pic, '0 0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
198 drawsubpic(theOrigin + height - dY + dX, width - 2 * dX + dY, pic, '0.25 0.75 0', '0.5 0.25 0', theColor, theAlpha, 0);
199 drawsubpic(theOrigin + height - dY + width - dX, dX + dY, pic, '0.75 0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
203 void draw_Text(vector theOrigin, string theText, vector theSize, vector theColor, float theAlpha, float ICanHasKallerz)
205 if(theSize_x <= 0 || theSize_y <= 0)
206 error("Drawing zero size text?\n");
208 drawcolorcodedstring(boxToGlobal(theOrigin, draw_shift, draw_scale), theText, boxToGlobalSize(theSize, draw_scale), theAlpha * draw_alpha, 0);
210 drawstring(boxToGlobal(theOrigin, draw_shift, draw_scale), theText, boxToGlobalSize(theSize, draw_scale), theColor, theAlpha * draw_alpha, 0);
213 float draw_TextWidth(string theText, float ICanHasKallerz)
215 //return strlen(theText);
216 //print("draw_TextWidth \"", theText, "\"\n");
217 return stringwidth(theText, ICanHasKallerz);
224 error("Already clipping, no stack implemented here, sorry");
225 drawsetcliparea(draw_shift_x, draw_shift_y, draw_scale_x, draw_scale_y);
229 void draw_SetClipRect(vector theOrigin, vector theScale)
233 error("Already clipping, no stack implemented here, sorry");
234 o = boxToGlobal(theOrigin, draw_shift, draw_scale);
235 s = boxToGlobalSize(theScale, draw_scale);
236 drawsetcliparea(o_x, o_y, s_x, s_y);
240 void draw_ClearClip()
243 error("Not clipping, can't clear it then");
248 string draw_TextShortenToWidth(string theText, float maxWidth, float ICanHasKallerz)
250 if(draw_TextWidth(theText, ICanHasKallerz) <= maxWidth)
253 return strcat(substring(theText, 0, draw_TextLengthUpToWidth(theText, maxWidth - draw_TextWidth("...", ICanHasKallerz), ICanHasKallerz)), "...");
256 float draw_TextLengthUpToWidth(string theText, float maxWidth, float ICanHasKallerz)
259 // The following function is SLOW.
260 // For your safety and for the protection of those around you...
261 // DO NOT CALL THIS AT HOME.
263 if(draw_TextWidth(theText, ICanHasKallerz) <= maxWidth)
264 return strlen(theText); // yeah!
266 // binary search for right place to cut string
267 float left, right, middle; // this always works
269 right = strlen(theText); // this always fails
272 middle = floor((left + right) / 2);
273 if(draw_TextWidth(substring(theText, 0, middle), ICanHasKallerz) <= maxWidth)
278 while(left < right - 1);
280 // NOTE: when color codes are involved, this binary search is,
281 // mathematically, BROKEN. However, it is obviously guaranteed to
282 // terminate, as the range still halves each time - but nevertheless, it is
283 // guaranteed that it finds ONE valid cutoff place (where "left" is in
284 // range, and "right" is outside).