]> icculus.org git repositories - divverent/nexuiz.git/blob - menu/mcontrols.qc
Fixing the CVS:
[divverent/nexuiz.git] / menu / mcontrols.qc
1 ///////////////////////////////////////////////
2 // Controls/Item Source File
3 ///////////////////////
4 // This file belongs to dpmod/darkplaces
5 // AK contains all item specific stuff
6 ////////////////////////////////////////////////
7
8 ////////////////
9 // ITEM_WINDOW
10 //
11
12 void(void) ITEM_WINDOW =
13 {
14         self.flag = self.flag | FLAG_NOSELECT | FLAG_DRAWONLY;
15
16         item_init(
17                 defct_reinit,
18                 defct_destroy,
19                 defct_key,
20                 defct_draw,
21                 defct_mouse_enter,
22                 defct_mouse_leave,
23                 defct_action,
24                 defct_refresh);
25
26         ctcall_init();
27 };
28
29 //////////////////
30 // ITEM_REFERENCE
31 ///
32
33 void(void) ITEM_REFERENCE =
34 {
35         self.flag = self.flag | FLAG_NOSELECT | FLAG_DRAWONLY;
36
37         // if there is no link, this is a dynamic reference
38         if(self.link != "")
39         {
40                 self._child = menu_getitem(self.link);
41                 if(self._child == null_entity)
42                 {
43                         print(self.name, " removed, cause link ", self.link, " not found\n");
44                         remove(self);   // no need to call terminate
45                         return;
46                 }
47         }
48
49         item_init(
50                 defct_reinit,
51                 defct_destroy,
52                 defct_key,
53                 defct_draw,
54                 defct_mouse_enter,
55                 defct_mouse_leave,
56                 defct_action,
57                 defct_refresh);
58
59         ctcall_init();
60 };
61
62 ////////////////
63 // ITEM_CUSTOM
64 ////
65
66 void(void) ITEM_CUSTOM =
67 {
68         item_init(defct_reinit, defct_destroy, defct_key, defct_draw, defct_mouse_enter, defct_mouse_leave, defct_action, defct_refresh);
69         ctcall_init();
70 };
71
72 /////////////////
73 // ITEM_PICTURE
74 ///
75
76 // ITEM_PICTURE has a special draw function
77 void(void) ITEM_PICTURE_DRAW =
78 {
79         menu_drawpic(self.pos, self.picture, self.size, self.color, self.alpha, self.drawflag);
80
81         ctcall_draw();
82 };
83
84 void(void) ITEM_PICTURE_DESTROY =
85 {
86         gfx_unloadpic(self.picture);
87
88         ctcall_destroy();
89 };
90
91 void(void) ITEM_PICTURE =
92 {
93         if(self.picture == "")
94                 // a picture has to have a picture
95                 remove(self);           // no need to call terminate
96
97         // load the picture if it isnt loaded already
98         gfx_loadpic(self.picture, MENU_ENFORCELOADING);
99
100         // if flag wasnt set yet, then set it to FLAG_DRAWONLY
101         if(self.flag == 0)
102                 self.flag = FLAG_DRAWONLY;
103
104         if(self.color == '0 0 0')
105                 self.color = ITEM_PICTURE_NORMAL_COLOR;
106         if(self.alpha == 0)
107                 self.alpha = ITEM_PICTURE_NORMAL_ALPHA;
108
109         item_init(
110                 defct_reinit,
111                 ITEM_PICTURE_DESTROY,
112                 defct_key,
113                 ITEM_PICTURE_DRAW,
114                 defct_mouse_enter,
115                 defct_mouse_leave,
116                 defct_action,
117                 defct_refresh);
118
119         ctcall_init();
120 };
121
122 /////////////
123 // ITEM_TEXT
124 ///
125
126 void(void) ITEM_TEXT_REFRESH =
127 {
128         // first do own refresh, *then* call the default refresh !
129         if(self.size == '0 0 0')
130         {
131                 if(self.font_size == '0 0 0')
132                         self.font_size = ITEM_TEXT_FONT_SIZE;
133
134                 self.size_x = self.font_size_x * strlen(self.text);
135                 self.size_y = self.font_size_y;
136         } else if(self.font_size == '0 0 0')
137         {
138                         self.font_size_x = self.size_x / strlen(self.text);
139                         self.font_size_y = self.size_y;
140         }
141
142         def_refresh();
143         ctcall_refresh();
144 };
145
146 void(void) ITEM_TEXT_DRAW =
147 {
148         if(self.text != "")
149         {
150                 // align to the rect pos - (pos + size)
151                 vector alignpos;
152                 // now check the alignement
153                 if(self.alignment & TEXT_ALIGN_CENTER)
154                         alignpos_x = self.pos_x + (self.size_x - strlen(self.text) * self.font_size_x) / 2;
155                 else if(self.alignment & TEXT_ALIGN_RIGHT)
156                         alignpos_x = self.pos_x + self.size_x - strlen(self.text) * self.font_size_x;
157                 else
158                         alignpos_x = self.pos_x;
159                 alignpos_y = self.pos_y;
160
161                 menu_drawstring(alignpos, self.text, self.font_size, self.color, self.alpha, self.drawflag);
162         }
163         ctcall_draw();
164 };
165
166 void(void) ITEM_TEXT =
167 {
168         if(self.flag == 0)
169                 self.flag = FLAG_DRAWONLY;
170
171         if(self.color == '0 0 0')
172                 self.color = ITEM_TEXT_NORMAL_COLOR;
173         if(self.alpha == 0)
174                 self.alpha = ITEM_TEXT_NORMAL_ALPHA;
175
176         ITEM_TEXT_REFRESH();
177         if(self.alignment & TEXT_ALIGN_CENTERPOS)
178         {
179                 self.pos_x = self.pos_x - self.size_x / 2;
180         } else  if(self.alignment & TEXT_ALIGN_LEFTPOS)
181         {
182                 self.pos_x = self.pos_x - self.size_x;
183         }
184
185         item_init(
186                 defct_reinit,
187                 defct_destroy,
188                 defct_key,
189                 ITEM_TEXT_DRAW,
190                 defct_mouse_enter,
191                 defct_mouse_leave,
192                 defct_action,
193                 ITEM_TEXT_REFRESH);
194
195         ctcall_init();
196 };
197
198 /////////////////
199 // ITEM_RECTANLE
200 ///
201
202 void(void) ITEM_RECTANGLE_DRAW =
203 {
204         menu_fillarea(self.pos, self.size, self.color, self.alpha, self.drawflag);
205 };
206
207 void(void) ITEM_RECTANGLE =
208 {
209         if(self.flag == 0)
210                 self.flag = FLAG_DRAWONLY;
211
212         item_init(
213                 defct_reinit,
214                 defct_destroy,
215                 defct_key,
216                 ITEM_RECTANGLE_DRAW,
217                 defct_mouse_enter,
218                 defct_mouse_leave,
219                 defct_action,
220                 defct_refresh);
221
222         ctcall_init();
223 };
224
225 ////////////////
226 // ITEM_BUTTON
227 ///
228
229 void(void) ITEM_BUTTON_DRAW =
230 {
231         if(self._button_state == BUTTON_NORMAL)
232                 menu_drawpic(self.pos, self.picture, self.size, self.color, self.alpha, self.drawflag);
233         else if(self._button_state == BUTTON_SELECTED)
234                 menu_drawpic(self.pos, self.picture_selected, self.size, self.color_selected, self.alpha_selected, self.drawflag_selected);
235         else
236                 menu_drawpic(self.pos, self.picture_pressed, self.size, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
237
238         ctcall_draw();
239 };
240
241 void(void) ITEM_BUTTON_REFRESH =
242 {
243         if( self.hold_pressed + self._press_time < time ) 
244         {
245                 if( menu_selected != self && self._button_state != BUTTON_NORMAL )
246                         self._button_state = BUTTON_NORMAL;
247                 else if( menu_selected == self && self._button_state != BUTTON_SELECTED)
248                 {
249                         self._button_state = BUTTON_SELECTED;
250                         if(self.sound_selected)
251                                 snd_play(self.sound_selected);
252                 }
253         }
254         
255         def_refresh();
256         ctcall_refresh();
257 };
258
259 void(float keynr, float ascii) ITEM_BUTTON_KEY =
260 {
261         if(ctcall_key(keynr, ascii))
262                 return;
263         
264         if(keynr == K_ENTER || keynr == K_MOUSE1)
265         {
266                 self._action();
267         } else
268                 def_keyevent(keynr, ascii);
269 };
270
271 void(void) ITEM_BUTTON_ACTION =
272 {
273         self._press_time = time;
274         self._button_state = BUTTON_PRESSED;
275         if(self.sound_pressed)
276                 snd_play(self.sound_pressed);
277
278         ctcall_action();
279 };
280 void(void) ITEM_BUTTON_REINIT =
281 {
282         self._button_state = BUTTON_NORMAL;
283
284         ctcall_reinit();
285 };
286
287 void(void) ITEM_BUTTON_DESTROY =
288 {
289         gfx_unloadpic(self.picture);
290         gfx_unloadpic(self.picture_selected);
291         gfx_unloadpic(self.picture_pressed);
292
293         ctcall_destroy();
294 };
295
296 void(void) ITEM_BUTTON =
297 {
298         if(self.picture == "" || self.picture_selected == "" || self.picture_pressed == "")
299                 // a picture has to have pictures
300                 remove(self);   // no need to call terminate
301
302         // load the picture if it isnt loaded already
303         gfx_loadpic(self.picture, MENU_ENFORCELOADING);
304         gfx_loadpic(self.picture_selected, MENU_ENFORCELOADING);
305         gfx_loadpic(self.picture_pressed, MENU_ENFORCELOADING);
306
307         if(self.sound_selected != "")
308                 snd_loadsound(self.sound_selected, SOUND_ENFORCELOADING);
309         else
310                 self.sound_selected = SOUND_SELECT;
311
312         if(self.sound_pressed != "")
313                 snd_loadsound(self.sound_pressed, SOUND_ENFORCELOADING);
314         else
315                 self.sound_pressed = SOUND_ACTION;
316
317         // if flag wasnt set yet, then set it to FLAG_DRAWONLY
318         if(self.flag == 0)
319                 self.flag = FLAG_AUTOSETCLICK;
320
321         if(self.color == '0 0 0')
322                 self.color = ITEM_PICTURE_NORMAL_COLOR;
323         if(self.alpha == 0)
324                 self.alpha = ITEM_PICTURE_NORMAL_ALPHA;
325         if(self.color_selected == '0 0 0')
326                 self.color_selected = ITEM_PICTURE_SELECTED_COLOR;
327         if(self.alpha_selected == 0)
328                 self.alpha_selected = ITEM_PICTURE_SELECTED_ALPHA;
329         if(self.color_pressed == '0 0 0')
330                 self.color_pressed = ITEM_PICTURE_PRESSED_COLOR;
331         if(self.alpha_pressed == 0)
332                 self.alpha_pressed = ITEM_PICTURE_PRESSED_ALPHA;
333
334         if(self.hold_pressed == 0)
335                 self.hold_pressed = ITEM_BUTTON_HOLD_PRESSED;
336
337         item_init(
338                 ITEM_BUTTON_REINIT,
339                 ITEM_BUTTON_DESTROY,
340                 ITEM_BUTTON_KEY,
341                 ITEM_BUTTON_DRAW,
342                 defct_mouse_enter,
343                 defct_mouse_leave,
344                 ITEM_BUTTON_ACTION,
345                 ITEM_BUTTON_REFRESH);
346
347         ctcall_init();
348 };
349
350 ////////////////////
351 // ITEM_TEXTBUTTON
352 ///
353
354 void(void) ITEM_TEXTBUTTON_REFRESH =
355 {
356         // first do own refresh, *then* call the default refresh !
357         if(self.size == '0 0 0')
358         {
359                 if(self.font_size == '0 0 0')
360                         self.font_size = ITEM_TEXT_FONT_SIZE;
361
362                 self.size_x = self.font_size_x * strlen(self.text);
363                 self.size_y = self.font_size_y;
364         } else if(self.font_size == '0 0 0')
365         {
366                         self.font_size_x = self.size_x / strlen(self.text);
367                         self.font_size_y = self.size_y;
368         }
369
370         if((self.hold_pressed + self._press_time < time && self._button_state == BUTTON_PRESSED) || (menu_selected != self && self._button_state == BUTTON_SELECTED))
371         {
372                 self._button_state = BUTTON_NORMAL;
373         }
374         if(menu_selected == self && self._button_state == BUTTON_NORMAL)
375         {
376                 self._button_state = BUTTON_SELECTED;
377                 if(self.sound_selected)
378                         snd_play(self.sound_selected);
379         }
380
381         def_refresh();
382         ctcall_refresh();
383 };
384
385 void(void) ITEM_TEXTBUTTON_DRAW =
386 {
387         if(self.text == "")
388                 return;
389
390         // align to the rect pos - (pos + size)
391         vector alignpos;
392         // now check the alignement
393         if(self.alignment & TEXT_ALIGN_CENTER)
394                 alignpos_x = self.pos_x + (self.size_x - strlen(self.text) * self.font_size_x) / 2;
395         else if(self.alignment & TEXT_ALIGN_RIGHT)
396                 alignpos_x = self.pos_x + self.size_x - strlen(self.text) * self.font_size_x;
397         else
398                 alignpos_x = self.pos_x;
399                 alignpos_y = self.pos_y;
400
401         if(self.style == TEXTBUTTON_STYLE_OUTLINE && self._button_state != BUTTON_NORMAL)
402         {
403                 vector p,s;
404                 // left
405                 p_x = self.pos_x;
406                 p_y = self.pos_y;
407                 s_x = TEXTBUTTON_OUTLINE_WIDTH;
408                 s_y = self.size_y;
409                 if(self._button_state == BUTTON_PRESSED)
410                 {
411                         menu_fillarea(p, s, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
412                 }
413                 else if(self._button_state == BUTTON_SELECTED)
414                 {
415                         menu_fillarea(p, s, self.color_selected, self.alpha_selected, self.drawflag_selected);
416                 }
417                 // right
418                 p_x = self.pos_x + self.size_x - TEXTBUTTON_OUTLINE_WIDTH;
419                 p_y = self.pos_y;
420                 s_x = TEXTBUTTON_OUTLINE_WIDTH;
421                 s_y = self.size_y;
422                 if(self._button_state == BUTTON_PRESSED)
423                 {
424                         menu_fillarea(p, s, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
425                 }
426                 else if(self._button_state == BUTTON_SELECTED)
427                 {
428                         menu_fillarea(p, s, self.color_selected, self.alpha_selected, self.drawflag_selected);
429                 }
430                 // top
431                 p_x = self.pos_x;
432                 p_y = self.pos_y;
433                 s_y = TEXTBUTTON_OUTLINE_WIDTH;
434                 s_x = self.size_x;
435                 if(self._button_state == BUTTON_PRESSED)
436                 {
437                         menu_fillarea(p, s, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
438                 }
439                 else if(self._button_state == BUTTON_SELECTED)
440                 {
441                         menu_fillarea(p, s, self.color_selected, self.alpha_selected, self.drawflag_selected);
442                 }
443                 // bottom
444                 p_x = self.pos_x;
445                 p_y = self.pos_y + self.size_y - TEXTBUTTON_OUTLINE_WIDTH;
446                 s_y = TEXTBUTTON_OUTLINE_WIDTH;
447                 s_x = self.size_x;
448                 if(self._button_state == BUTTON_PRESSED)
449                 {
450                         menu_fillarea(p, s, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
451                 }
452                 else if(self._button_state == BUTTON_SELECTED)
453                 {
454                         menu_fillarea(p, s, self.color_selected, self.alpha_selected, self.drawflag_selected);
455                 }
456         } else  if(self.style == TEXTBUTTON_STYLE_BOX)
457         {
458                 if(self._button_state == BUTTON_PRESSED)
459                 {
460                         menu_fillarea(alignpos, self.size, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
461                 }
462                 else if(self._button_state == BUTTON_SELECTED)
463                 {
464                         menu_fillarea(alignpos, self.size, self.color_selected, self.alpha_selected, self.drawflag_selected);
465                 }
466         }
467
468         if(self._button_state == BUTTON_NORMAL || self.style == TEXTBUTTON_STYLE_BOX || self.style == TEXTBUTTON_STYLE_OUTLINE)
469                 menu_drawstring(alignpos, self.text, self.font_size, self.color, self.alpha, self.drawflag);
470
471         if(self.style == TEXTBUTTON_STYLE_TEXT)
472         {
473                 if(self._button_state == BUTTON_PRESSED)
474                 {
475                         menu_drawstring(alignpos, self.text, self.font_size, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
476                 }
477                 else if(self._button_state == BUTTON_SELECTED)
478                 {
479                         menu_drawstring(alignpos, self.text, self.font_size, self.color_selected, self.alpha_selected, self.drawflag_selected);
480                 }
481         }
482
483         ctcall_draw();
484 };
485
486 void(void) ITEM_TEXTBUTTON_ACTION =
487 {
488         self._press_time = time;
489         self._button_state = BUTTON_PRESSED;
490         if(self.sound_pressed)
491                 snd_play(self.sound_pressed);
492
493         ctcall_action();
494 };
495
496 void(float keynr, float ascii) ITEM_TEXTBUTTON_KEY =
497 {
498         if(ctcall_key(keynr, ascii))
499                 return;
500
501         if(keynr == K_ENTER || keynr == K_MOUSE1)
502         {
503                 self._action();
504         } else
505                 def_keyevent(keynr, ascii);
506 };
507
508 void(void) ITEM_TEXTBUTTON_REINIT =
509 {
510         self._button_state = BUTTON_NORMAL;
511
512         ctcall_reinit();
513 };
514
515 void(void) ITEM_TEXTBUTTON =
516 {
517         if(self.flag == 0)
518                 self.flag = FLAG_AUTOSETCLICK;
519
520         if(self.color == '0 0 0')
521                 self.color = ITEM_TEXT_NORMAL_COLOR;
522         if(self.alpha == 0)
523                 self.alpha = ITEM_TEXT_NORMAL_ALPHA;
524         if(self.color_selected == '0 0 0')
525                 self.color_selected = ITEM_TEXT_SELECTED_COLOR;
526         if(self.alpha_selected == 0)
527                 self.alpha_selected = ITEM_TEXT_SELECTED_ALPHA;
528         if(self.color_pressed == '0 0 0')
529                 self.color_pressed = ITEM_TEXT_PRESSED_COLOR;
530         if(self.alpha_pressed == 0)
531                 self.alpha_pressed = ITEM_TEXT_PRESSED_ALPHA;
532
533         if(self.hold_pressed == 0)
534                 self.hold_pressed = ITEM_BUTTON_HOLD_PRESSED;
535
536         if(self.sound_selected != "")
537                 snd_loadsound(self.sound_selected, SOUND_ENFORCELOADING);
538         else
539                 self.sound_selected = SOUND_SELECT;
540
541         if(self.sound_pressed != "")
542                 snd_loadsound(self.sound_pressed, SOUND_ENFORCELOADING);
543         else
544                 self.sound_pressed = SOUND_ACTION;
545
546         ITEM_TEXTBUTTON_REFRESH();
547         if(self.alignment & TEXT_ALIGN_CENTERPOS)
548         {
549                 self.pos_x = self.pos_x - self.size_x / 2;
550         } else  if(self.alignment & TEXT_ALIGN_LEFTPOS)
551         {
552                 self.pos_x = self.pos_x - self.size_x;
553         }
554
555         item_init(
556                 ITEM_TEXTBUTTON_REINIT,
557                 defct_destroy,
558                 ITEM_TEXTBUTTON_KEY,
559                 ITEM_TEXTBUTTON_DRAW,
560                 defct_mouse_enter,
561                 defct_mouse_leave,
562                 ITEM_TEXTBUTTON_ACTION,
563                 ITEM_TEXTBUTTON_REFRESH);
564
565         ctcall_init();
566 };
567
568 // ITEM_SLIDER
569
570 void(void) ITEM_SLIDER_DRAW =
571 {
572         vector slider_pos;
573         string t;
574
575         // draw the bar
576         if(self.picture_bar != "")
577         {
578                 menu_drawpic(self.pos, self.picture_bar, self.size, self.color, self.alpha, self.drawflag);
579         }
580         else
581         {
582                 menu_fillarea(self.pos, self.size, self.color, self.alpha, self.drawflag);
583         }
584
585         // draw the slider
586         slider_pos = self.pos;
587         slider_pos_x = slider_pos_x + self.left_margin + ((self.size_x - self.slider_size_x - self.left_margin - self.right_margin) / (self.max_value - self.min_value)) * (self.value - self.min_value);
588         if(self.picture != "")
589         {
590                 menu_drawpic(slider_pos, self.picture, self.slider_size, self.color, self.alpha, self.drawflag);
591         }
592         else
593         {
594                 menu_fillarea(slider_pos, self.slider_size, self.color + ITEM_SLIDER_BAR_COLOR_DELTA, self.alpha, self.drawflag);
595         }
596 };
597
598 void(void) ITEM_SLIDER_UPDATESLIDER =
599 {
600         self.value = bound(self.min_value, self.value, self.max_value);
601         if(self.slidermove)
602                 self.slidermove();
603 };
604
605 void(float keynr, float ascii) ITEM_SLIDER_KEY =
606 {
607         float old;
608         old = self.value;
609
610         if(ctcall_key(keynr, ascii))
611                 return;
612
613         if(keynr == K_LEFTARROW)
614         {
615                 self.value = (rint(self.value / self.step) - 1) * self.step;
616         }
617         else if(keynr == K_RIGHTARROW)
618         {
619                 self.value = (rint(self.value / self.step) + 1)* self.step;
620         }
621         else if(keynr == K_MOUSE1)
622         {
623                 if(inrect(menu_cursor, self.pos, self.size))
624                 {
625                         if(menu_cursor_x - self.pos_x <= self.left_margin)
626                         {
627                                 self.value = (rint(self.value / self.step) - 1)* self.step;
628                         }
629                         else if(menu_cursor_x - self.pos_x >= self.size_x - self.right_margin)
630                         {
631                                 self.value = (rint(self.value / self.step) + 1)* self.step;
632                         }
633                         else
634                         {
635                                 self.value = self.min_value + ((menu_cursor_x - self.slider_size_x / 2) - self.pos_x - self.left_margin) * ((self.max_value - self.min_value) / (self.size_x - self.slider_size_x - self.left_margin - self.right_margin));
636                         }
637                 }
638         }
639         else
640         {
641                 def_keyevent(keynr, ascii);
642                 return;
643         }
644         // play sound
645         if(old < self.value)
646         {
647                 snd_play(self.sound_increase);
648         }
649         else if(self.value < old)
650         {
651                 snd_play(self.sound_decrease);
652         }
653
654         ITEM_SLIDER_UPDATESLIDER();
655         ctcall_action();
656 };
657
658 void(void) ITEM_SLIDER_DESTROY =
659 {
660         if(self.picture != "" && self.picture != ITEM_SLIDER_DEFAULT_SLIDER)
661                 gfx_unloadpic(self.picture);
662         if(self.picture_bar != "" && self.picture_bar != ITEM_SLIDER_DEFAULT_BAR)
663                 gfx_unloadpic(self.picture);
664         if(self.sound_increase != SOUND_INCREASE)
665                 snd_unloadsound(self.sound_decrease);
666         if(self.sound_increase != SOUND_INCREASE)
667                 snd_unloadsound(self.sound_increase);
668
669         ctcall_destroy();
670 };
671
672 void(void) ITEM_SLIDER =
673 {
674         if(self.picture != "")
675                 self.picture = gfx_loadpic(self.picture, MENU_ENFORCELOADING);
676         if(self.picture == "")
677                 self.picture = gfx_loadpic(ITEM_SLIDER_DEFAULT_SLIDER, MENU_ENFORCELOADING);
678         if(self.picture_bar != "")
679                 self.picture_bar = gfx_loadpic(self.picture_bar, MENU_ENFORCELOADING);
680         if(self.picture_bar == "")
681                 self.picture_bar = gfx_loadpic(ITEM_SLIDER_DEFAULT_BAR, MENU_ENFORCELOADING);
682         if(self.sound_decrease == "")
683                 self.sound_decrease = SOUND_DECREASE;
684         else
685                 snd_loadsound(self.sound_decrease, MENU_ENFORCELOADING);
686         if(self.sound_increase == "")
687                 self.sound_increase = SOUND_INCREASE;
688         else
689                 snd_loadsound(self.sound_increase, MENU_ENFORCELOADING);
690
691         if(self.color == '0 0 0')
692                 self.color = ITEM_SLIDER_COLOR;
693         if(self.alpha == 0)
694                 self.alpha = ITEM_SLIDER_ALPHA;
695         if(self.step == 0)
696                 self.step = ITEM_SLIDER_STEP;
697         if(self.slider_size == '0 0 0')
698         {
699                 /*if(self.picture != "")
700                         self.slider_size = gfx_getimagesize(self.picture);
701                 else*/
702                         self.slider_size = ITEM_SLIDER_SIZE;
703         }
704         if(self.left_margin == 0)
705                 self.left_margin = ITEM_SLIDER_LEFT_MARGIN;
706         if(self.right_margin == 0)
707                 self.right_margin = ITEM_SLIDER_RIGHT_MARGIN;
708
709         item_init(
710                 defct_reinit,
711                 defct_destroy,
712                 ITEM_SLIDER_KEY,
713                 ITEM_SLIDER_DRAW,
714                 defct_mouse_enter,
715                 defct_mouse_leave,
716                 defct_action,
717                 defct_refresh);
718
719         ctcall_init();
720 };
721
722 // ITEM_TEXTSWITCH
723
724 void(void) ITEM_TEXTSWITCH_DRAW =
725 {
726         string temp;
727
728         // get the current text
729         temp = self.text;
730         self.text = getaltstring(self.value, self.text);
731         // call ITEM_TEXT
732         ITEM_TEXT_DRAW();
733         self.text = temp;
734 };
735
736 void(void) ITEM_TEXTSWITCH_REFRESH =
737 {
738         string temp;
739
740         temp = self.text;
741         self.text = getaltstring(self.value, self.text);
742
743         ITEM_TEXT_REFRESH();
744
745         self.text = temp;
746 };
747
748 void(float keynr, float ascii)  ITEM_TEXTSWITCH_KEY =
749 {
750         float old;
751         old = self.value;
752
753         if(ctcall_key(keynr, ascii))
754                 return;
755
756         if(keynr == K_LEFTARROW || keynr == K_MOUSE2)
757         {
758                 self.value = self.value - 1;
759                 if(self.value < 0)
760                         self.value = getaltstringcount(self.text) - 1;
761         }
762         else if(keynr == K_RIGHTARROW || keynr == K_MOUSE1 || keynr == K_ENTER)
763         {
764                 self.value = self.value + 1;
765                 if(self.value > getaltstringcount(self.text) - 1)
766                         self.value = 0;
767         } else
768         {
769                 def_keyevent(keynr, ascii);
770                 return;
771         }
772         // play sound
773         if(old < self.value)
774                 snd_play(self.sound_increase);
775         else if(self.value < old)
776                 snd_play(self.sound_decrease);
777
778         if(self.switchchange)
779                 self.switchchange();
780 };
781
782 void(void) ITEM_TEXTSWITCH_DESTROY =
783 {
784         if(self.sound_increase != SOUND_INCREASE)
785                 snd_unloadsound(self.sound_decrease);
786         if(self.sound_increase != SOUND_INCREASE)
787                 snd_unloadsound(self.sound_increase);
788 }
789
790 void(void) ITEM_TEXTSWITCH =
791 {
792         string temp;
793
794         if(self.sound_decrease == "")
795                 self.sound_decrease = SOUND_DECREASE;
796         else
797                 snd_loadsound(self.sound_decrease, MENU_ENFORCELOADING);
798         if(self.sound_increase == "")
799                 self.sound_increase = SOUND_INCREASE;
800         else
801                 snd_loadsound(self.sound_increase, MENU_ENFORCELOADING);
802
803         temp = self.text;
804         self.text = getaltstring(self.value, self.text);
805         ITEM_TEXT();
806         self.text = temp;
807
808         item_init(
809                 defct_reinit,
810                 ITEM_TEXTSWITCH_DESTROY,
811                 ITEM_TEXTSWITCH_KEY,
812                 ITEM_TEXTSWITCH_DRAW,
813                 defct_mouse_enter,
814                 defct_mouse_leave,
815                 defct_action,
816                 ITEM_TEXTSWITCH_REFRESH);
817
818         ctcall_init();
819 };
820
821 // ITEM_EDITBOX
822
823 void(void) ITEM_EDITBOX_REINIT =
824 {
825         self._cursorpos = 0;
826         self._button_state = BUTTON_NORMAL;
827 };
828
829 void(void) ITEM_EDITBOX_DRAW =
830 {
831         vector csize, cpos;
832
833         if(self.text != "")
834                 ITEM_TEXT_DRAW();
835
836         if(self._button_state == BUTTON_NORMAL || mod(time * 1000, 1000 / EDITBOX_CURSOR_FREQ) > 500 / EDITBOX_CURSOR_FREQ)
837                 return;
838
839         csize_x = 1.5;
840         csize_y = self.font_size_y;
841         csize_z = 0;
842         cpos = self.pos;
843         cpos_x = cpos_x + self._cursorpos * self.font_size_x;
844
845         menu_fillarea(cpos, csize, self.color_selected, self.alpha_selected, self.drawflag_selected);
846 };
847
848 void(float ascii) ITEM_EDITBOX_INSERT =
849 {
850         string firsthalf, secondhalf;
851         float len;
852
853         if(self._cursorpos > strlen(self.text))
854                 return;
855
856         if(self.format == "")
857         {
858                 // no format stuff, so lets have fun
859                 firsthalf = substring(self.text, 0, self._cursorpos);
860                 secondhalf = substring(self.text, self._cursorpos, strlen(self.text) - self._cursorpos);
861                 strunzone(self.text);
862                 self.text = strcat(firsthalf, chr(ascii));
863                 self.text = strcat(self.text, secondhalf);
864                 if(self.maxlen >= 0 && strlen(self.text) > self.maxlen)
865                 {
866                         // remove the last char(s)
867                         self.text = substring(self.text, 0, self.maxlen);
868                 }
869                 // move the cursor forward
870                 if(strlen(self.text) >= self._cursorpos)
871                         self._cursorpos = self._cursorpos + 1;
872
873                 self.text = strzone(self.text);
874         }
875         else // here begins the funny part
876         {
877                 // TODO: add wildcard support :-/
878                 // move the cursor to the next space
879                 len = strlen(self.format);
880                 while(substring(self.format, self._cursorpos, 1) != " " && len > self._cursorpos)
881                 {
882                         self._cursorpos = self._cursorpos + 1;
883                 }
884                 if(self._cursorpos >= len)
885                         return;
886
887                 firsthalf = substring(self.text, 0, self._cursorpos);
888                 secondhalf = substring(self.text, self._cursorpos + 1 , strlen(self.text) - self._cursorpos - 1);
889                 strunzone(self.text);
890                 self.text = strcat(firsthalf, chr(ascii), secondhalf);
891                 self.text = strzone(self.text);
892         }
893 };
894
895 void(void) ITEM_EDITBOX_LEFT =
896 {
897         if(self._cursorpos > 0)
898                 self._cursorpos = self._cursorpos - 1;
899 };
900
901 void(void) ITEM_EDITBOX_RIGHT =
902 {
903         if(self._cursorpos < strlen(self.text))
904                 self._cursorpos = self._cursorpos + 1;
905 };
906
907 void(float keynr, float ascii) ITEM_EDITBOX_KEY =
908 {
909         if(ctcall_key(keynr, ascii))
910                 return;
911         
912         if(ascii >= 30 && ascii <= 126)
913         {
914                 ITEM_EDITBOX_INSERT(ascii);
915         } else if(keynr == K_BACKSPACE)
916         {
917                 if(self.format == "")
918                 {
919                         string firsthalf, secondhalf;
920
921                         firsthalf = substring(self.text,0, self._cursorpos - 1);
922                         secondhalf = substring(self.text,self._cursorpos, strlen(self.text) - self._cursorpos);
923                         strunzone(self.text);
924                         self.text = strcat(firsthalf, secondhalf);
925                         self.text = strzone(self.text);
926
927                         ITEM_EDITBOX_LEFT();
928                 }
929         } else if(keynr == K_ENTER)
930         {
931                 if(self._action)
932                         self._action();
933                 //snd_play(self.sound_action);
934         } else if(keynr == K_LEFTARROW)
935         {
936                 ITEM_EDITBOX_LEFT();
937         }
938         else if(keynr == K_RIGHTARROW)
939         {
940                 ITEM_EDITBOX_RIGHT();
941         }
942         else
943                 def_keyevent(keynr, ascii);
944
945         snd_play(self.sound_pressed);
946 };
947
948 void(void) ITEM_EDITBOX_REFRESH =
949 {
950         if(menu_selected != self && menu_selected != self._parent)
951                 self._button_state = BUTTON_NORMAL;
952         else if(self._button_state == BUTTON_NORMAL)
953         {
954                 self._button_state = BUTTON_SELECTED;
955
956                 if(self.sound_selected)
957                         snd_play(self.sound_selected);
958         }
959
960         if(self._cursorpos > strlen(self.text))
961                 self._cursorpos = strlen(self.text);
962
963         ITEM_TEXT_REFRESH();
964 };
965
966 void(void) ITEM_EDITBOX =
967 {
968         if(self.sound_selected != "")
969                 snd_loadsound(self.sound_selected, SOUND_ENFORCELOADING);
970         else
971                 self.sound_selected = SOUND_SELECT;
972
973         if(self.sound_pressed != "")
974                 snd_loadsound(self.sound_pressed, SOUND_ENFORCELOADING);
975         else
976                 self.sound_pressed = SOUND_ACTION;
977
978         // if flag wasnt set yet, then set it to FLAG_DRAWONLY
979         if(self.flag == 0)
980                 self.flag = FLAG_AUTOSETCLICK;
981
982         if(self.color == '0 0 0')
983                 self.color = ITEM_PICTURE_NORMAL_COLOR;
984         if(self.alpha == 0)
985                 self.alpha = ITEM_PICTURE_NORMAL_ALPHA;
986         if(self.color_selected == '0 0 0')
987                 self.color_selected = ITEM_PICTURE_SELECTED_COLOR;
988         if(self.alpha_selected == 0)
989                 self.alpha_selected = ITEM_PICTURE_SELECTED_ALPHA;
990
991         self.text = strzone(self.text);
992
993         ITEM_TEXT_REFRESH();
994         if(self.alignment & TEXT_ALIGN_CENTERPOS)
995         {
996                 self.pos_x = self.pos_x - self.size_x / 2;
997         } else  if(self.alignment & TEXT_ALIGN_LEFTPOS)
998         {
999                 self.pos_x = self.pos_x - self.size_x;
1000         }
1001
1002         item_init(
1003                 defct_reinit,
1004                 defct_destroy,
1005                 ITEM_EDITBOX_KEY,
1006                 ITEM_EDITBOX_DRAW,
1007                 defct_mouse_enter,
1008                 defct_mouse_leave,
1009                 defct_action,
1010                 ITEM_EDITBOX_REFRESH);
1011
1012         ctcall_init();
1013 };
1014
1015 /*
1016 /////////////////////////////////////////////////////////////////////////
1017 // The "actual" funtions
1018
1019 void(void) ctinit_picture =
1020 {
1021
1022 };
1023
1024 // draws a text (uses the std. way)
1025 void(string text, vector pos, vector size, float alignment, float style, float state) ctdrawtext =
1026 {
1027         vector alignpos;
1028
1029         if(text == "")
1030                 return;
1031
1032         // align to the rect pos - (pos + size)
1033
1034         // now check the alignement
1035         if(alignment & TEXT_ALIGN_CENTER)
1036                 alignpos_x = pos_x + (size_x - strlen(text) * self.font_size_x) / 2;
1037         else if(alignment & TEXT_ALIGN_RIGHT)
1038                 alignpos_x = pos_x + size_x - strlen(text) * self.font_size_x;
1039         else
1040                 alignpos_x = pos_x;
1041                 alignpos_y = pos_y;
1042
1043         if(style == TEXTBUTTON_STYLE_OUTLINE && state != BUTTON_NORMAL)
1044         {
1045                 vector p,s;
1046                 // left
1047                 p_x = pos_x;
1048                 p_y = pos_y;
1049                 s_x = TEXTBUTTON_OUTLINE_WIDTH;
1050                 s_y = size_y;
1051                 if(state == BUTTON_PRESSED)
1052                 {
1053                         menu_fillarea(p, s, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
1054                 }
1055                 else if(state == BUTTON_SELECTED)
1056                 {
1057                         menu_fillarea(p, s, self.color_selected, self.alpha_selected, self.drawflag_selected);
1058                 }
1059                 // right
1060                 p_x = pos_x + size_x - TEXTBUTTON_OUTLINE_WIDTH;
1061                 p_y = pos_y;
1062                 s_x = TEXTBUTTON_OUTLINE_WIDTH;
1063                 s_y = size_y;
1064                 if(state == BUTTON_PRESSED)
1065                 {
1066                         menu_fillarea(p, s, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
1067                 }
1068                 else if(state == BUTTON_SELECTED)
1069                 {
1070                         menu_fillarea(p, s, self.color_selected, self.alpha_selected, self.drawflag_selected);
1071                 }
1072                 // top
1073                 p_x = pos_x;
1074                 p_y = pos_y;
1075                 s_y = TEXTBUTTON_OUTLINE_WIDTH;
1076                 s_x = size_x;
1077                 if(state == BUTTON_PRESSED)
1078                 {
1079                         menu_fillarea(p, s, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
1080                 }
1081                 else if(self._button_state == BUTTON_SELECTED)
1082                 {
1083                         menu_fillarea(p, s, self.color_selected, self.alpha_selected, self.drawflag_selected);
1084                 }
1085                 // bottom
1086                 p_x = pos_x;
1087                 p_y = pos_y + size_y - TEXTBUTTON_OUTLINE_WIDTH;
1088                 s_y = TEXTBUTTON_OUTLINE_WIDTH;
1089                 s_x = size_x;
1090                 if(state == BUTTON_PRESSED)
1091                 {
1092                         menu_fillarea(p, s, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
1093                 }
1094                 else if(state == BUTTON_SELECTED)
1095                 {
1096                         menu_fillarea(p, s, self.color_selected, self.alpha_selected, self.drawflag_selected);
1097                 }
1098         } else  if(style == TEXTBUTTON_STYLE_BOX)
1099         {
1100                 if(state == BUTTON_PRESSED)
1101                 {
1102                         menu_fillarea(alignpos, size, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
1103                 }
1104                 else if(self._button_state == BUTTON_SELECTED)
1105                 {
1106                         menu_fillarea(alignpos, size, self.color_selected, self.alpha_selected, self.drawflag_selected);
1107                 }
1108         }
1109
1110         if(state == BUTTON_NORMAL || style == TEXTBUTTON_STYLE_BOX || style == TEXTBUTTON_STYLE_OUTLINE)
1111                 menu_drawstring(alignpos, text, self.font_size, self.color, self.alpha, self.drawflag);
1112
1113         if(style == TEXTBUTTON_STYLE_TEXT)
1114         {
1115                 if(state == BUTTON_PRESSED)
1116                 {
1117                         menu_drawstring(alignpos, text, self.font_size, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
1118                 }
1119                 else if(state == BUTTON_SELECTED)
1120                 {
1121                         menu_drawstring(alignpos, text, self.font_size, self.color_selected, self.alpha_selected, self.drawflag_selected);
1122                 }
1123         }
1124 };*/