2 string draw_mousepointer;
4 string draw_UseSkinFor(string pic)
6 if(substring(pic, 0, 1) == "/")
7 return substring(pic, 1, strlen(pic)-1);
9 return strcat(draw_currentSkin, "/", pic);
12 void draw_setMousePointer(string pic)
14 draw_mousepointer = strzone(draw_UseSkinFor(pic));
17 void draw_drawMousePointer(vector where)
19 drawpic(boxToGlobal(where, draw_shift, draw_scale), draw_mousepointer, '32 32 0', '1 1 1', draw_alpha, 0);
26 draw_scale = '1 0 0' * cvar("vid_conwidth") + '0 1 0' * cvar("vid_conheight");
30 vector globalToBox(vector v, vector theOrigin, vector theScale)
38 vector globalToBoxSize(vector v, vector theScale)
45 vector boxToGlobal(vector v, vector theOrigin, vector theScale)
53 vector boxToGlobalSize(vector v, vector theScale)
60 void draw_Picture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha)
62 pic = draw_UseSkinFor(pic);
63 drawpic(boxToGlobal(theOrigin, draw_shift, draw_scale), pic, boxToGlobalSize(theSize, draw_scale), theColor, theAlpha * draw_alpha, 0);
66 void draw_Fill(vector theOrigin, vector theSize, vector theColor, float theAlpha)
68 drawfill(boxToGlobal(theOrigin, draw_shift, draw_scale), boxToGlobalSize(theSize, draw_scale), theColor, theAlpha * draw_alpha, 0);
71 // a button picture is a texture containing three parts:
72 // 1/4 width: left part
73 // 1/2 width: middle part (stretched)
74 // 1/4 width: right part
75 // it is assumed to be 4x as wide as high for aspect ratio purposes, which
76 // means, the parts are a square, two squares and a square.
77 void draw_ButtonPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha)
82 pic = draw_UseSkinFor(pic);
83 theOrigin = boxToGlobal(theOrigin, draw_shift, draw_scale);
84 theSize = boxToGlobalSize(theSize, draw_scale);
85 theAlpha *= draw_alpha;
86 width = eX * theSize_x;
87 height = eY * theSize_y;
88 if(theSize_x <= theSize_y * 2)
90 // button not wide enough
91 // draw just left and right part then
92 square = eX * theSize_x * 0.5;
93 bW = eX * (0.25 * theSize_x / (theSize_y * 2));
94 drawsubpic(theOrigin, square + height, pic, '0 0 0', eY + bW, theColor, theAlpha, 0);
95 drawsubpic(theOrigin + square, square + height, pic, eX - bW, eY + bW, theColor, theAlpha, 0);
99 square = eX * theSize_y;
100 drawsubpic(theOrigin, height + square, pic, '0 0 0', '0.25 1 0', theColor, theAlpha, 0);
101 drawsubpic(theOrigin + square, theSize - 2 * square, pic, '0.25 0 0', '0.5 1 0', theColor, theAlpha, 0);
102 drawsubpic(theOrigin + width - square, height + square, pic, '0.75 0 0', '0.25 1 0', theColor, theAlpha, 0);
106 // a vertical button picture is a texture containing three parts:
107 // 1/4 height: left part
108 // 1/2 height: middle part (stretched)
109 // 1/4 height: right part
110 // it is assumed to be 4x as high as wide for aspect ratio purposes, which
111 // means, the parts are a square, two squares and a square.
112 void draw_VertButtonPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha)
115 vector width, height;
117 pic = draw_UseSkinFor(pic);
118 theOrigin = boxToGlobal(theOrigin, draw_shift, draw_scale);
119 theSize = boxToGlobalSize(theSize, draw_scale);
120 theAlpha *= draw_alpha;
121 width = eX * theSize_x;
122 height = eY * theSize_y;
123 if(theSize_y <= theSize_x * 2)
125 // button not high enough
126 // draw just upper and lower part then
127 square = eY * theSize_y * 0.5;
128 bH = eY * (0.25 * theSize_y / (theSize_x * 2));
129 drawsubpic(theOrigin, square + width, pic, '0 0 0', eX + bH, theColor, theAlpha, 0);
130 drawsubpic(theOrigin + square, square + width, pic, eY - bH, eX + bH, theColor, theAlpha, 0);
134 square = eY * theSize_x;
135 drawsubpic(theOrigin, width + square, pic, '0 0 0', '1 0.25 0', theColor, theAlpha, 0);
136 drawsubpic(theOrigin + square, theSize - 2 * square, pic, '0 0.25 0', '1 0.5 0', theColor, theAlpha, 0);
137 drawsubpic(theOrigin + height - square, width + square, pic, '0 0.75 0', '1 0.25 0', theColor, theAlpha, 0);
141 // a border picture is a texture containing nine parts:
142 // 1/4 width: left part
143 // 1/2 width: middle part (stretched)
144 // 1/4 width: right part
146 // 1/4 height: top part
147 // 1/2 height: middle part (stretched)
148 // 1/4 height: bottom part
149 void draw_BorderPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha, vector theBorderSize)
152 vector width, height;
154 pic = draw_UseSkinFor(pic);
155 theOrigin = boxToGlobal(theOrigin, draw_shift, draw_scale);
156 theSize = boxToGlobalSize(theSize, draw_scale);
157 theBorderSize = boxToGlobalSize(theBorderSize, draw_scale);
158 theAlpha *= draw_alpha;
159 width = eX * theSize_x;
160 height = eY * theSize_y;
161 if(theSize_x <= theBorderSize_x * 2)
163 // not wide enough... draw just left and right then
164 bW = eX * (0.25 * theSize_x / (theBorderSize_x * 2));
165 if(theSize_y <= theBorderSize_y * 2)
167 // not high enough... draw just corners
168 bH = eY * (0.25 * theSize_y / (theBorderSize_y * 2));
169 drawsubpic(theOrigin, width * 0.5 + height * 0.5, pic, '0 0 0', bW + bH, theColor, theAlpha, 0);
170 drawsubpic(theOrigin + width * 0.5, width * 0.5 + height * 0.5, pic, eX - bW, bW + bH, theColor, theAlpha, 0);
171 drawsubpic(theOrigin + height * 0.5, width * 0.5 + height * 0.5, pic, eY - bH, bW + bH, theColor, theAlpha, 0);
172 drawsubpic(theOrigin + theSize * 0.5, width * 0.5 + height * 0.5, pic, eX + eY - bW - bH, bW + bH, theColor, theAlpha, 0);
176 dY = theBorderSize_x * eY;
177 drawsubpic(theOrigin, width * 0.5 + dY, pic, '0 0 0', '0 0.25 0' + bW, theColor, theAlpha, 0);
178 drawsubpic(theOrigin + width * 0.5, width * 0.5 + dY, pic, '0 0 0' + eX - bW, '0 0.25 0' + bW, theColor, theAlpha, 0);
179 drawsubpic(theOrigin + dY, width * 0.5 + height - 2 * dY, pic, '0 0.25 0', '0 0.5 0' + bW, theColor, theAlpha, 0);
180 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);
181 drawsubpic(theOrigin + height - dY, width * 0.5 + dY, pic, '0 0.75 0', '0 0.25 0' + bW, theColor, theAlpha, 0);
182 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);
187 if(theSize_y <= theBorderSize_y * 2)
189 // not high enough... draw just top and bottom then
190 bH = eY * (0.25 * theSize_y / (theBorderSize_y * 2));
191 dX = theBorderSize_x * eX;
192 drawsubpic(theOrigin, dX + height * 0.5, pic, '0 0 0', '0.25 0 0' + bH, theColor, theAlpha, 0);
193 drawsubpic(theOrigin + dX, width - 2 * dX + height * 0.5, pic, '0.25 0 0', '0.5 0 0' + bH, theColor, theAlpha, 0);
194 drawsubpic(theOrigin + width - dX, dX + height * 0.5, pic, '0.75 0 0', '0.25 0 0' + bH, theColor, theAlpha, 0);
195 drawsubpic(theOrigin + height * 0.5, dX + height * 0.5, pic, '0 0 0' + eY - bH, '0.25 0 0' + bH, theColor, theAlpha, 0);
196 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);
197 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);
201 dX = theBorderSize_x * eX;
202 dY = theBorderSize_x * eY;
203 drawsubpic(theOrigin, dX + dY, pic, '0 0 0', '0.25 0.25 0', theColor, theAlpha, 0);
204 drawsubpic(theOrigin + dX, width - 2 * dX + dY, pic, '0.25 0 0', '0.5 0.25 0', theColor, theAlpha, 0);
205 drawsubpic(theOrigin + width - dX, dX + dY, pic, '0.75 0 0', '0.25 0.25 0', theColor, theAlpha, 0);
206 drawsubpic(theOrigin + dY, dX + height - 2 * dY, pic, '0 0.25 0', '0.25 0.5 0', theColor, theAlpha, 0);
207 drawsubpic(theOrigin + dY + dX, width - 2 * dX + height - 2 * dY, pic, '0.25 0.25 0', '0.5 0.5 0', theColor, theAlpha, 0);
208 drawsubpic(theOrigin + dY + width - dX, dX + height - 2 * dY, pic, '0.75 0.25 0', '0.25 0.5 0', theColor, theAlpha, 0);
209 drawsubpic(theOrigin + height - dY, dX + dY, pic, '0 0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
210 drawsubpic(theOrigin + height - dY + dX, width - 2 * dX + dY, pic, '0.25 0.75 0', '0.5 0.25 0', theColor, theAlpha, 0);
211 drawsubpic(theOrigin + height - dY + width - dX, dX + dY, pic, '0.75 0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
215 void draw_Text(vector theOrigin, string theText, vector theSize, vector theColor, float theAlpha, float ICanHasKallerz)
217 if(theSize_x <= 0 || theSize_y <= 0)
218 error("Drawing zero size text?\n");
220 drawcolorcodedstring(boxToGlobal(theOrigin, draw_shift, draw_scale), theText, boxToGlobalSize(theSize, draw_scale), theAlpha * draw_alpha, 0);
222 drawstring(boxToGlobal(theOrigin, draw_shift, draw_scale), theText, boxToGlobalSize(theSize, draw_scale), theColor, theAlpha * draw_alpha, 0);
225 float draw_TextWidth(string theText, float ICanHasKallerz)
227 //return strlen(theText);
228 //print("draw_TextWidth \"", theText, "\"\n");
229 return stringwidth(theText, ICanHasKallerz);
236 error("Already clipping, no stack implemented here, sorry");
237 drawsetcliparea(draw_shift_x, draw_shift_y, draw_scale_x, draw_scale_y);
241 void draw_SetClipRect(vector theOrigin, vector theScale)
245 error("Already clipping, no stack implemented here, sorry");
246 o = boxToGlobal(theOrigin, draw_shift, draw_scale);
247 s = boxToGlobalSize(theScale, draw_scale);
248 drawsetcliparea(o_x, o_y, s_x, s_y);
252 void draw_ClearClip()
255 error("Not clipping, can't clear it then");
260 string draw_TextShortenToWidth(string theText, float maxWidth, float ICanHasKallerz)
262 if(draw_TextWidth(theText, ICanHasKallerz) <= maxWidth)
265 return strcat(substring(theText, 0, draw_TextLengthUpToWidth(theText, maxWidth - draw_TextWidth("...", ICanHasKallerz), ICanHasKallerz)), "...");
268 float draw_TextLengthUpToWidth(string theText, float maxWidth, float ICanHasKallerz)
271 // The following function is SLOW.
272 // For your safety and for the protection of those around you...
273 // DO NOT CALL THIS AT HOME.
275 if(draw_TextWidth(theText, ICanHasKallerz) <= maxWidth)
276 return strlen(theText); // yeah!
278 // binary search for right place to cut string
279 float left, right, middle; // this always works
281 right = strlen(theText); // this always fails
284 middle = floor((left + right) / 2);
285 if(draw_TextWidth(substring(theText, 0, middle), ICanHasKallerz) <= maxWidth)
290 while(left < right - 1);
292 // NOTE: when color codes are involved, this binary search is,
293 // mathematically, BROKEN. However, it is obviously guaranteed to
294 // terminate, as the range still halves each time - but nevertheless, it is
295 // guaranteed that it finds ONE valid cutoff place (where "left" is in
296 // range, and "right" is outside).