]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu-div0test/draw.qc
added start of a skin subsystem; added Xaw-like skin :P
[divverent/nexuiz.git] / data / qcsrc / menu-div0test / draw.qc
1 float drawfont;
2 string draw_mousepointer;
3
4 string draw_UseSkinFor(string pic)
5 {
6         if(substring(pic, 0, 1) == "/")
7                 return substring(pic, 1, strlen(pic)-1);
8         else
9                 return strcat(draw_currentSkin, "/", pic);
10 }
11
12 void draw_setMousePointer(string pic)
13 {
14         draw_mousepointer = strzone(draw_UseSkinFor(pic));
15 }
16
17 void draw_drawMousePointer(vector where)
18 {
19         drawpic(boxToGlobal(where, draw_shift, draw_scale), draw_mousepointer, '32 32 0', '1 1 1', draw_alpha, 0);
20 }
21
22 void draw_reset()
23 {
24         drawfont = 8;
25         draw_shift = '0 0 0';
26         draw_scale = '1 0 0' * cvar("vid_conwidth") + '0 1 0' * cvar("vid_conheight");
27         draw_alpha = 1;
28 }
29
30 vector globalToBox(vector v, vector theOrigin, vector theScale)
31 {
32         v -= theOrigin;
33         v_x /= theScale_x;
34         v_y /= theScale_y;
35         return v;
36 }
37
38 vector globalToBoxSize(vector v, vector theScale)
39 {
40         v_x /= theScale_x;
41         v_y /= theScale_y;
42         return v;
43 }
44
45 vector boxToGlobal(vector v, vector theOrigin, vector theScale)
46 {
47         v_x *= theScale_x;
48         v_y *= theScale_y;
49         v += theOrigin;
50         return v;
51 }
52
53 vector boxToGlobalSize(vector v, vector theScale)
54 {
55         v_x *= theScale_x;
56         v_y *= theScale_y;
57         return v;
58 }
59
60 void draw_Picture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha)
61 {
62         pic = draw_UseSkinFor(pic);
63         drawpic(boxToGlobal(theOrigin, draw_shift, draw_scale), pic, boxToGlobalSize(theSize, draw_scale), theColor, theAlpha * draw_alpha, 0);
64 }
65
66 void draw_Fill(vector theOrigin, vector theSize, vector theColor, float theAlpha)
67 {
68         drawfill(boxToGlobal(theOrigin, draw_shift, draw_scale), boxToGlobalSize(theSize, draw_scale), theColor, theAlpha * draw_alpha, 0);
69 }
70
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)
78 {
79         vector square;
80         vector width, height;
81         vector bW;
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)
89         {
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);
96         }
97         else
98         {
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);
103         }
104 }
105
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)
113 {
114         vector square;
115         vector width, height;
116         vector bH;
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)
124         {
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);
131         }
132         else
133         {
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);
138         }
139 }
140
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
145 // divided into
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)
150 {
151         vector dX, dY;
152         vector width, height;
153         vector bW, bH;
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)
162         {
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)
166                 {
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);
173                 }
174                 else
175                 {
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);
183                 }
184         }
185         else
186         {
187                 if(theSize_y <= theBorderSize_y * 2)
188                 {
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);
198                 }
199                 else
200                 {
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);
212                 }
213         }
214 }
215 void draw_Text(vector theOrigin, string theText, vector theSize, vector theColor, float theAlpha, float ICanHasKallerz)
216 {
217         if(theSize_x <= 0 || theSize_y <= 0)
218                 error("Drawing zero size text?\n");
219         if(ICanHasKallerz)
220                 drawcolorcodedstring(boxToGlobal(theOrigin, draw_shift, draw_scale), theText, boxToGlobalSize(theSize, draw_scale), theAlpha * draw_alpha, 0);
221         else
222                 drawstring(boxToGlobal(theOrigin, draw_shift, draw_scale), theText, boxToGlobalSize(theSize, draw_scale), theColor, theAlpha * draw_alpha, 0);
223 }
224
225 float draw_TextWidth(string theText, float ICanHasKallerz)
226 {
227         //return strlen(theText);
228         //print("draw_TextWidth \"", theText, "\"\n");
229         return stringwidth(theText, ICanHasKallerz);
230 }
231
232 float draw_clipSet;
233 void draw_SetClip()
234 {
235         if(draw_clipSet)
236                 error("Already clipping, no stack implemented here, sorry");
237         drawsetcliparea(draw_shift_x, draw_shift_y, draw_scale_x, draw_scale_y);
238         draw_clipSet = 1;
239 }
240
241 void draw_SetClipRect(vector theOrigin, vector theScale)
242 {
243         vector o, s;
244         if(draw_clipSet)
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);
249         draw_clipSet = 1;
250 }
251
252 void draw_ClearClip()
253 {
254         if(!draw_clipSet)
255                 error("Not clipping, can't clear it then");
256         drawresetcliparea();
257         draw_clipSet = 0;
258 }
259
260 string draw_TextShortenToWidth(string theText, float maxWidth, float ICanHasKallerz)
261 {
262         if(draw_TextWidth(theText, ICanHasKallerz) <= maxWidth)
263                 return theText;
264         else
265                 return strcat(substring(theText, 0, draw_TextLengthUpToWidth(theText, maxWidth - draw_TextWidth("...", ICanHasKallerz), ICanHasKallerz)), "...");
266 }
267
268 float draw_TextLengthUpToWidth(string theText, float maxWidth, float ICanHasKallerz)
269 {
270         // STOP.
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.
274         // No really, don't.
275         if(draw_TextWidth(theText, ICanHasKallerz) <= maxWidth)
276                 return strlen(theText); // yeah!
277
278         // binary search for right place to cut string
279         float left, right, middle; // this always works
280         left = 0;
281         right = strlen(theText); // this always fails
282         do
283         {
284                 middle = floor((left + right) / 2);
285                 if(draw_TextWidth(substring(theText, 0, middle), ICanHasKallerz) <= maxWidth)
286                         left = middle;
287                 else
288                         right = middle;
289         }
290         while(left < right - 1);
291
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).
297
298         return left;
299 }