]> icculus.org git repositories - divverent/nexuiz.git/blob - menu/mcontrols.qc
Fixed two minor bugs.
[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 != "" && menu_automatedselection == false)
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 != "" && menu_automatedselection == false)
378                         snd_play(self.sound_selected);
379         }*/
380         /*if( self.hold_pressed + self._press_time < time )
381         {
382                 if( menu_selected != self && self._button_state != BUTTON_NORMAL )
383                         self._button_state = BUTTON_NORMAL;
384                 else if( menu_selected == self && self._button_state != BUTTON_SELECTED)
385                 {
386                         self._button_state = BUTTON_SELECTED;
387                         if(self.sound_selected != "" && menu_automatedselection == false)
388                                 snd_play(self.sound_selected);
389                 }
390         }*/
391         if( self._button_state == BUTTON_PRESSED ) {
392                 if( self.hold_pressed + self._press_time < time )
393                         if( menu_selected == self )
394                                 self._button_state = BUTTON_SELECTED;
395                         else
396                                 self._button_state = BUTTON_NORMAL;
397         } else if( self._button_state == BUTTON_NORMAL && menu_selected == self ) {
398                 self._button_state = BUTTON_SELECTED;
399                 if( self.sound_selected != "" && menu_automatedselection == false )
400                         snd_play( self.sound_selected );
401         } else if( menu_selected != self )
402                 self._button_state = BUTTON_NORMAL;
403
404         def_refresh();
405         ctcall_refresh();
406 };
407
408 void(void) ITEM_TEXTBUTTON_DRAW =
409 {
410         if(self.text == "")
411                 return;
412
413         // align to the rect pos - (pos + size)
414         vector alignpos;
415         // now check the alignement
416         if(self.alignment & TEXT_ALIGN_CENTER)
417                 alignpos_x = self.pos_x + (self.size_x - strlen(self.text) * self.font_size_x) / 2;
418         else if(self.alignment & TEXT_ALIGN_RIGHT)
419                 alignpos_x = self.pos_x + self.size_x - strlen(self.text) * self.font_size_x;
420         else
421                 alignpos_x = self.pos_x;
422                 alignpos_y = self.pos_y;
423
424         if(self.style == TEXTBUTTON_STYLE_OUTLINE && self._button_state != BUTTON_NORMAL)
425         {
426                 vector p,s;
427                 // left
428                 p_x = self.pos_x;
429                 p_y = self.pos_y;
430                 s_x = TEXTBUTTON_OUTLINE_WIDTH;
431                 s_y = self.size_y;
432                 if(self._button_state == BUTTON_PRESSED)
433                 {
434                         menu_fillarea(p, s, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
435                 }
436                 else if(self._button_state == BUTTON_SELECTED)
437                 {
438                         menu_fillarea(p, s, self.color_selected, self.alpha_selected, self.drawflag_selected);
439                 }
440                 // right
441                 p_x = self.pos_x + self.size_x - TEXTBUTTON_OUTLINE_WIDTH;
442                 p_y = self.pos_y;
443                 s_x = TEXTBUTTON_OUTLINE_WIDTH;
444                 s_y = self.size_y;
445                 if(self._button_state == BUTTON_PRESSED)
446                 {
447                         menu_fillarea(p, s, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
448                 }
449                 else if(self._button_state == BUTTON_SELECTED)
450                 {
451                         menu_fillarea(p, s, self.color_selected, self.alpha_selected, self.drawflag_selected);
452                 }
453                 // top
454                 p_x = self.pos_x;
455                 p_y = self.pos_y;
456                 s_y = TEXTBUTTON_OUTLINE_WIDTH;
457                 s_x = self.size_x;
458                 if(self._button_state == BUTTON_PRESSED)
459                 {
460                         menu_fillarea(p, s, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
461                 }
462                 else if(self._button_state == BUTTON_SELECTED)
463                 {
464                         menu_fillarea(p, s, self.color_selected, self.alpha_selected, self.drawflag_selected);
465                 }
466                 // bottom
467                 p_x = self.pos_x;
468                 p_y = self.pos_y + self.size_y - TEXTBUTTON_OUTLINE_WIDTH;
469                 s_y = TEXTBUTTON_OUTLINE_WIDTH;
470                 s_x = self.size_x;
471                 if(self._button_state == BUTTON_PRESSED)
472                 {
473                         menu_fillarea(p, s, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
474                 }
475                 else if(self._button_state == BUTTON_SELECTED)
476                 {
477                         menu_fillarea(p, s, self.color_selected, self.alpha_selected, self.drawflag_selected);
478                 }
479         } else  if(self.style == TEXTBUTTON_STYLE_BOX)
480         {
481                 if(self._button_state == BUTTON_PRESSED)
482                 {
483                         menu_fillarea(alignpos, self.size, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
484                 }
485                 else if(self._button_state == BUTTON_SELECTED)
486                 {
487                         menu_fillarea(alignpos, self.size, self.color_selected, self.alpha_selected, self.drawflag_selected);
488                 }
489         }
490
491         if(self._button_state == BUTTON_NORMAL || self.style == TEXTBUTTON_STYLE_BOX || self.style == TEXTBUTTON_STYLE_OUTLINE)
492                 menu_drawstring(alignpos, self.text, self.font_size, self.color, self.alpha, self.drawflag);
493
494         if(self.style == TEXTBUTTON_STYLE_TEXT)
495         {
496                 if(self._button_state == BUTTON_PRESSED)
497                 {
498                         menu_drawstring(alignpos, self.text, self.font_size, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
499                 }
500                 else if(self._button_state == BUTTON_SELECTED)
501                 {
502                         menu_drawstring(alignpos, self.text, self.font_size, self.color_selected, self.alpha_selected, self.drawflag_selected);
503                 }
504         }
505
506         ctcall_draw();
507 };
508
509 void(void) ITEM_TEXTBUTTON_ACTION =
510 {
511         self._press_time = time;
512         self._button_state = BUTTON_PRESSED;
513         if(self.sound_pressed)
514                 snd_play(self.sound_pressed);
515
516         ctcall_action();
517 };
518
519 void(float keynr, float ascii) ITEM_TEXTBUTTON_KEY =
520 {
521         if(ctcall_key(keynr, ascii))
522                 return;
523
524         if(keynr == K_ENTER || keynr == K_MOUSE1)
525         {
526                 self._action();
527         } else
528                 def_keyevent(keynr, ascii);
529 };
530
531 void(void) ITEM_TEXTBUTTON_REINIT =
532 {
533         self._button_state = BUTTON_NORMAL;
534
535         ctcall_reinit();
536 };
537
538 void(void) ITEM_TEXTBUTTON =
539 {
540         if(self.flag == 0)
541                 self.flag = FLAG_AUTOSETCLICK;
542
543         if(self.color == '0 0 0')
544                 self.color = ITEM_TEXT_NORMAL_COLOR;
545         if(self.alpha == 0)
546                 self.alpha = ITEM_TEXT_NORMAL_ALPHA;
547         if(self.color_selected == '0 0 0')
548                 self.color_selected = ITEM_TEXT_SELECTED_COLOR;
549         if(self.alpha_selected == 0)
550                 self.alpha_selected = ITEM_TEXT_SELECTED_ALPHA;
551         if(self.color_pressed == '0 0 0')
552                 self.color_pressed = ITEM_TEXT_PRESSED_COLOR;
553         if(self.alpha_pressed == 0)
554                 self.alpha_pressed = ITEM_TEXT_PRESSED_ALPHA;
555
556         if(self.hold_pressed == 0)
557                 self.hold_pressed = ITEM_BUTTON_HOLD_PRESSED;
558
559         if(self.sound_selected != "")
560                 snd_loadsound(self.sound_selected, SOUND_ENFORCELOADING);
561         else
562                 self.sound_selected = SOUND_SELECT;
563
564         if(self.sound_pressed != "")
565                 snd_loadsound(self.sound_pressed, SOUND_ENFORCELOADING);
566         else
567                 self.sound_pressed = SOUND_ACTION;
568
569         ITEM_TEXTBUTTON_REFRESH();
570         if(self.alignment & TEXT_ALIGN_CENTERPOS)
571         {
572                 self.pos_x = self.pos_x - self.size_x / 2;
573         } else  if(self.alignment & TEXT_ALIGN_LEFTPOS)
574         {
575                 self.pos_x = self.pos_x - self.size_x;
576         }
577
578         item_init(
579                 ITEM_TEXTBUTTON_REINIT,
580                 defct_destroy,
581                 ITEM_TEXTBUTTON_KEY,
582                 ITEM_TEXTBUTTON_DRAW,
583                 defct_mouse_enter,
584                 defct_mouse_leave,
585                 ITEM_TEXTBUTTON_ACTION,
586                 ITEM_TEXTBUTTON_REFRESH);
587
588         ctcall_init();
589 };
590
591 // ITEM_SLIDER
592
593 void(void) ITEM_SLIDER_DRAW =
594 {
595         vector slider_pos;
596         string t;
597
598         // draw the bar
599         if(self.picture_bar != "")
600         {
601                 menu_drawpic(self.pos, self.picture_bar, self.size, self.color, self.alpha, self.drawflag);
602         }
603         else
604         {
605                 menu_fillarea(self.pos, self.size, self.color, self.alpha, self.drawflag);
606         }
607
608         // draw the slider
609         slider_pos = self.pos;
610         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);
611         if(self.picture != "")
612         {
613                 menu_drawpic(slider_pos, self.picture, self.slider_size, self.color, self.alpha, self.drawflag);
614         }
615         else
616         {
617                 menu_fillarea(slider_pos, self.slider_size, self.color + ITEM_SLIDER_BAR_COLOR_DELTA, self.alpha, self.drawflag);
618         }
619 };
620
621 void(void) ITEM_SLIDER_UPDATESLIDER =
622 {
623         self.value = bound(self.min_value, self.value, self.max_value);
624         if(self.slidermove)
625                 self.slidermove();
626 };
627
628 void(float keynr, float ascii) ITEM_SLIDER_KEY =
629 {
630         float old;
631         old = self.value;
632
633         if(ctcall_key(keynr, ascii))
634                 return;
635
636         if(keynr == K_LEFTARROW)
637         {
638                 self.value = (rint(self.value / self.step) - 1) * self.step;
639         }
640         else if(keynr == K_RIGHTARROW)
641         {
642                 self.value = (rint(self.value / self.step) + 1)* self.step;
643         }
644         else if(keynr == K_MOUSE1)
645         {
646                 if(inrect(menu_cursor, self.pos, self.size))
647                 {
648                         if(menu_cursor_x - self.pos_x <= self.left_margin)
649                         {
650                                 self.value = (rint(self.value / self.step) - 1)* self.step;
651                         }
652                         else if(menu_cursor_x - self.pos_x >= self.size_x - self.right_margin)
653                         {
654                                 self.value = (rint(self.value / self.step) + 1)* self.step;
655                         }
656                         else
657                         {
658                                 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));
659                         }
660                 }
661         }
662         else
663         {
664                 def_keyevent(keynr, ascii);
665                 return;
666         }
667         // play sound
668         if(old < self.value)
669         {
670                 snd_play(self.sound_increase);
671         }
672         else if(self.value < old)
673         {
674                 snd_play(self.sound_decrease);
675         }
676
677         ITEM_SLIDER_UPDATESLIDER();
678         ctcall_action();
679 };
680
681 void(void) ITEM_SLIDER_DESTROY =
682 {
683         if(self.picture != "" && self.picture != ITEM_SLIDER_DEFAULT_SLIDER)
684                 gfx_unloadpic(self.picture);
685         if(self.picture_bar != "" && self.picture_bar != ITEM_SLIDER_DEFAULT_BAR)
686                 gfx_unloadpic(self.picture);
687         if(self.sound_increase != SOUND_INCREASE)
688                 snd_unloadsound(self.sound_decrease);
689         if(self.sound_increase != SOUND_INCREASE)
690                 snd_unloadsound(self.sound_increase);
691
692         ctcall_destroy();
693 };
694
695 void(void) ITEM_SLIDER =
696 {
697         if(self.picture != "")
698                 self.picture = gfx_loadpic(self.picture, MENU_ENFORCELOADING);
699         if(self.picture == "")
700                 self.picture = gfx_loadpic(ITEM_SLIDER_DEFAULT_SLIDER, MENU_ENFORCELOADING);
701         if(self.picture_bar != "")
702                 self.picture_bar = gfx_loadpic(self.picture_bar, MENU_ENFORCELOADING);
703         if(self.picture_bar == "")
704                 self.picture_bar = gfx_loadpic(ITEM_SLIDER_DEFAULT_BAR, MENU_ENFORCELOADING);
705         if(self.sound_decrease == "")
706                 self.sound_decrease = SOUND_DECREASE;
707         else
708                 snd_loadsound(self.sound_decrease, MENU_ENFORCELOADING);
709         if(self.sound_increase == "")
710                 self.sound_increase = SOUND_INCREASE;
711         else
712                 snd_loadsound(self.sound_increase, MENU_ENFORCELOADING);
713
714         if(self.color == '0 0 0')
715                 self.color = ITEM_SLIDER_COLOR;
716         if(self.alpha == 0)
717                 self.alpha = ITEM_SLIDER_ALPHA;
718         if(self.step == 0)
719                 self.step = ITEM_SLIDER_STEP;
720         if(self.slider_size == '0 0 0')
721         {
722                 /*if(self.picture != "")
723                         self.slider_size = gfx_getimagesize(self.picture);
724                 else*/
725                         self.slider_size = ITEM_SLIDER_SIZE;
726         }
727         if(self.left_margin == 0)
728                 self.left_margin = ITEM_SLIDER_LEFT_MARGIN;
729         if(self.right_margin == 0)
730                 self.right_margin = ITEM_SLIDER_RIGHT_MARGIN;
731
732         item_init(
733                 defct_reinit,
734                 defct_destroy,
735                 ITEM_SLIDER_KEY,
736                 ITEM_SLIDER_DRAW,
737                 defct_mouse_enter,
738                 defct_mouse_leave,
739                 defct_action,
740                 defct_refresh);
741
742         ctcall_init();
743 };
744
745 // ITEM_TEXTSWITCH
746
747 void(void) ITEM_TEXTSWITCH_DRAW =
748 {
749         string temp;
750
751         // get the current text
752         temp = self.text;
753         self.text = getaltstring(self.value, self.text);
754         // call ITEM_TEXT
755         ITEM_TEXT_DRAW();
756         self.text = temp;
757 };
758
759 void(void) ITEM_TEXTSWITCH_REFRESH =
760 {
761         string temp;
762
763         temp = self.text;
764         self.text = getaltstring(self.value, self.text);
765
766         ITEM_TEXT_REFRESH();
767
768         self.text = temp;
769 };
770
771 void(float keynr, float ascii)  ITEM_TEXTSWITCH_KEY =
772 {
773         float dir;
774         if(ctcall_key(keynr, ascii))
775                 return;
776
777         if(keynr == K_LEFTARROW || keynr == K_MOUSE2)
778         {
779                 dir = 1;
780                 self.value = self.value - 1;
781                 if(self.value < 0)
782                         self.value = getaltstringcount(self.text) - 1;
783         }
784         else if(keynr == K_RIGHTARROW || keynr == K_MOUSE1 || keynr == K_ENTER)
785         {
786                 dir = -1;
787                 self.value = self.value + 1;
788                 if(self.value > getaltstringcount(self.text) - 1)
789                         self.value = 0;
790         } else
791         {
792                 def_keyevent(keynr, ascii);
793                 return;
794         }
795         // play sound
796         if( dir == -1 )
797                 snd_play(self.sound_increase);
798         else if( dir == 1 )
799                 snd_play(self.sound_decrease);
800
801         if(self.switchchange)
802                 self.switchchange();
803 };
804
805 void(void) ITEM_TEXTSWITCH_DESTROY =
806 {
807         if(self.sound_increase != SOUND_INCREASE)
808                 snd_unloadsound(self.sound_decrease);
809         if(self.sound_increase != SOUND_INCREASE)
810                 snd_unloadsound(self.sound_increase);
811 }
812
813 void(void) ITEM_TEXTSWITCH =
814 {
815         string temp;
816
817         if(self.sound_decrease == "")
818                 self.sound_decrease = SOUND_DECREASE;
819         else
820                 snd_loadsound(self.sound_decrease, MENU_ENFORCELOADING);
821         if(self.sound_increase == "")
822                 self.sound_increase = SOUND_INCREASE;
823         else
824                 snd_loadsound(self.sound_increase, MENU_ENFORCELOADING);
825
826         temp = self.text;
827         self.text = getaltstring(self.value, self.text);
828         ITEM_TEXT();
829         self.text = temp;
830
831         item_init(
832                 defct_reinit,
833                 ITEM_TEXTSWITCH_DESTROY,
834                 ITEM_TEXTSWITCH_KEY,
835                 ITEM_TEXTSWITCH_DRAW,
836                 defct_mouse_enter,
837                 defct_mouse_leave,
838                 defct_action,
839                 ITEM_TEXTSWITCH_REFRESH);
840
841         ctcall_init();
842 };
843
844 // ITEM_EDITBOX
845
846 void(void) ITEM_EDITBOX_REINIT =
847 {
848         self._cursorpos = 0;
849         self._button_state = BUTTON_NORMAL;
850 };
851
852 void(void) ITEM_EDITBOX_DRAW =
853 {
854         vector csize, cpos;
855
856         if(self.text != "")
857                 ITEM_TEXT_DRAW();
858
859         if(self._button_state == BUTTON_NORMAL || mod(time * 1000, 1000 / EDITBOX_CURSOR_FREQ) > 500 / EDITBOX_CURSOR_FREQ)
860                 return;
861
862         csize_x = 1.5;
863         csize_y = self.font_size_y;
864         csize_z = 0;
865         cpos = self.pos;
866         cpos_x = cpos_x + self._cursorpos * self.font_size_x;
867
868         menu_fillarea(cpos, csize, self.color_selected, self.alpha_selected, self.drawflag_selected);
869 };
870
871 void(float ascii) ITEM_EDITBOX_INSERT =
872 {
873         string firsthalf, secondhalf;
874         float len;
875
876         if(self._cursorpos > strlen(self.text))
877                 return;
878
879         if(self.format == "")
880         {
881                 // no format stuff, so lets have fun
882                 firsthalf = substring(self.text, 0, self._cursorpos);
883                 secondhalf = substring(self.text, self._cursorpos, strlen(self.text) - self._cursorpos);
884                 strunzone(self.text);
885                 self.text = strcat(firsthalf, chr(ascii));
886                 self.text = strcat(self.text, secondhalf);
887                 if(self.maxlen >= 0 && strlen(self.text) > self.maxlen)
888                 {
889                         // remove the last char(s)
890                         self.text = substring(self.text, 0, self.maxlen);
891                 }
892                 // move the cursor forward
893                 if(strlen(self.text) >= self._cursorpos)
894                         self._cursorpos = self._cursorpos + 1;
895
896                 self.text = strzone(self.text);
897         }
898         else // here begins the funny part
899         {
900                 // TODO: add wildcard support :-/
901                 // move the cursor to the next space
902                 len = strlen(self.format);
903                 while(substring(self.format, self._cursorpos, 1) != " " && len > self._cursorpos)
904                 {
905                         self._cursorpos = self._cursorpos + 1;
906                 }
907                 if(self._cursorpos >= len)
908                         return;
909
910                 firsthalf = substring(self.text, 0, self._cursorpos);
911                 secondhalf = substring(self.text, self._cursorpos + 1 , strlen(self.text) - self._cursorpos - 1);
912                 strunzone(self.text);
913                 self.text = strcat(firsthalf, chr(ascii), secondhalf);
914                 self.text = strzone(self.text);
915         }
916 };
917
918 void(void) ITEM_EDITBOX_LEFT =
919 {
920         if(self._cursorpos > 0)
921                 self._cursorpos = self._cursorpos - 1;
922 };
923
924 void(void) ITEM_EDITBOX_RIGHT =
925 {
926         if(self._cursorpos < strlen(self.text))
927                 self._cursorpos = self._cursorpos + 1;
928 };
929
930 void(float keynr, float ascii) ITEM_EDITBOX_KEY =
931 {
932         if(ctcall_key(keynr, ascii))
933                 return;
934
935         if(ascii >= 30 && ascii <= 126)
936         {
937                 ITEM_EDITBOX_INSERT(ascii);
938         } else if(keynr == K_BACKSPACE)
939         {
940                 if(self.format == "")
941                 {
942                         string firsthalf, secondhalf;
943
944                         firsthalf = substring(self.text,0, self._cursorpos - 1);
945                         secondhalf = substring(self.text,self._cursorpos, strlen(self.text) - self._cursorpos);
946                         strunzone(self.text);
947                         self.text = strcat(firsthalf, secondhalf);
948                         self.text = strzone(self.text);
949
950                         ITEM_EDITBOX_LEFT();
951                 }
952         } else if(keynr == K_ENTER)
953         {
954                 self._action();
955                 //snd_play(self.sound_action);
956         } else if(keynr == K_LEFTARROW)
957         {
958                 ITEM_EDITBOX_LEFT();
959         }
960         else if(keynr == K_RIGHTARROW)
961         {
962                 ITEM_EDITBOX_RIGHT();
963         }
964         else
965                 def_keyevent(keynr, ascii);
966
967         snd_play(self.sound_pressed);
968 };
969
970 void(void) ITEM_EDITBOX_REFRESH =
971 {
972         if(menu_selected != self && menu_selected != self._parent)
973                 self._button_state = BUTTON_NORMAL;
974         else if(self._button_state == BUTTON_NORMAL)
975         {
976                 self._button_state = BUTTON_SELECTED;
977
978                 if(self.sound_selected != "" && menu_automatedselection == false)
979                         snd_play(self.sound_selected);
980         }
981
982         if(self._cursorpos > strlen(self.text))
983                 self._cursorpos = strlen(self.text);
984
985         // hack: we want the text to scroll if possible
986         // self.clip_pos holds the normal value
987         if(self._cursorpos >= self.size_x / self.font_size_x - 1.5 )
988                 self.pos_x = self.clip_pos_x -(self._cursorpos - floor( self.size_x / self.font_size_x - 1.5)) * self.font_size_x;
989         else
990                 self.pos_x = self.clip_pos_x;
991
992         ITEM_TEXT_REFRESH();
993 };
994
995 void(void) ITEM_EDITBOX =
996 {
997         if(self.sound_selected != "")
998                 snd_loadsound(self.sound_selected, SOUND_ENFORCELOADING);
999         else
1000                 self.sound_selected = SOUND_SELECT;
1001
1002         if(self.sound_pressed != "")
1003                 snd_loadsound(self.sound_pressed, SOUND_ENFORCELOADING);
1004         else
1005                 self.sound_pressed = SOUND_ACTION;
1006
1007         // if flag wasnt set yet, then set it to FLAG_DRAWONLY
1008         if(self.flag == 0)
1009                 self.flag = FLAG_AUTOSETCLICK;
1010
1011         if(self.color == '0 0 0')
1012                 self.color = ITEM_PICTURE_NORMAL_COLOR;
1013         if(self.alpha == 0)
1014                 self.alpha = ITEM_PICTURE_NORMAL_ALPHA;
1015         if(self.color_selected == '0 0 0')
1016                 self.color_selected = ITEM_PICTURE_SELECTED_COLOR;
1017         if(self.alpha_selected == 0)
1018                 self.alpha_selected = ITEM_PICTURE_SELECTED_ALPHA;
1019
1020         self.text = strzone(self.text);
1021
1022         ITEM_TEXT_REFRESH();
1023         if(self.alignment & TEXT_ALIGN_CENTERPOS)
1024         {
1025                 self.pos_x = self.pos_x - self.size_x / 2;
1026         } else  if(self.alignment & TEXT_ALIGN_LEFTPOS)
1027         {
1028                 self.pos_x = self.pos_x - self.size_x;
1029         }
1030
1031         //init clip_pos
1032         self.clip_pos = self.pos;
1033
1034         item_init(
1035                 defct_reinit,
1036                 defct_destroy,
1037                 ITEM_EDITBOX_KEY,
1038                 ITEM_EDITBOX_DRAW,
1039                 defct_mouse_enter,
1040                 defct_mouse_leave,
1041                 defct_action,
1042                 ITEM_EDITBOX_REFRESH);
1043
1044         ctcall_init();
1045 };
1046
1047 /*
1048 /////////////////////////////////////////////////////////////////////////
1049 // The "actual" funtions
1050
1051 void(void) ctinit_picture =
1052 {
1053
1054 };
1055
1056 // draws a text (uses the std. way)
1057 void(string text, vector pos, vector size, float alignment, float style, float state) ctdrawtext =
1058 {
1059         vector alignpos;
1060
1061         if(text == "")
1062                 return;
1063
1064         // align to the rect pos - (pos + size)
1065
1066         // now check the alignement
1067         if(alignment & TEXT_ALIGN_CENTER)
1068                 alignpos_x = pos_x + (size_x - strlen(text) * self.font_size_x) / 2;
1069         else if(alignment & TEXT_ALIGN_RIGHT)
1070                 alignpos_x = pos_x + size_x - strlen(text) * self.font_size_x;
1071         else
1072                 alignpos_x = pos_x;
1073                 alignpos_y = pos_y;
1074
1075         if(style == TEXTBUTTON_STYLE_OUTLINE && state != BUTTON_NORMAL)
1076         {
1077                 vector p,s;
1078                 // left
1079                 p_x = pos_x;
1080                 p_y = pos_y;
1081                 s_x = TEXTBUTTON_OUTLINE_WIDTH;
1082                 s_y = size_y;
1083                 if(state == BUTTON_PRESSED)
1084                 {
1085                         menu_fillarea(p, s, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
1086                 }
1087                 else if(state == BUTTON_SELECTED)
1088                 {
1089                         menu_fillarea(p, s, self.color_selected, self.alpha_selected, self.drawflag_selected);
1090                 }
1091                 // right
1092                 p_x = pos_x + size_x - TEXTBUTTON_OUTLINE_WIDTH;
1093                 p_y = pos_y;
1094                 s_x = TEXTBUTTON_OUTLINE_WIDTH;
1095                 s_y = size_y;
1096                 if(state == BUTTON_PRESSED)
1097                 {
1098                         menu_fillarea(p, s, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
1099                 }
1100                 else if(state == BUTTON_SELECTED)
1101                 {
1102                         menu_fillarea(p, s, self.color_selected, self.alpha_selected, self.drawflag_selected);
1103                 }
1104                 // top
1105                 p_x = pos_x;
1106                 p_y = pos_y;
1107                 s_y = TEXTBUTTON_OUTLINE_WIDTH;
1108                 s_x = size_x;
1109                 if(state == BUTTON_PRESSED)
1110                 {
1111                         menu_fillarea(p, s, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
1112                 }
1113                 else if(self._button_state == BUTTON_SELECTED)
1114                 {
1115                         menu_fillarea(p, s, self.color_selected, self.alpha_selected, self.drawflag_selected);
1116                 }
1117                 // bottom
1118                 p_x = pos_x;
1119                 p_y = pos_y + size_y - TEXTBUTTON_OUTLINE_WIDTH;
1120                 s_y = TEXTBUTTON_OUTLINE_WIDTH;
1121                 s_x = size_x;
1122                 if(state == BUTTON_PRESSED)
1123                 {
1124                         menu_fillarea(p, s, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
1125                 }
1126                 else if(state == BUTTON_SELECTED)
1127                 {
1128                         menu_fillarea(p, s, self.color_selected, self.alpha_selected, self.drawflag_selected);
1129                 }
1130         } else  if(style == TEXTBUTTON_STYLE_BOX)
1131         {
1132                 if(state == BUTTON_PRESSED)
1133                 {
1134                         menu_fillarea(alignpos, size, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
1135                 }
1136                 else if(self._button_state == BUTTON_SELECTED)
1137                 {
1138                         menu_fillarea(alignpos, size, self.color_selected, self.alpha_selected, self.drawflag_selected);
1139                 }
1140         }
1141
1142         if(state == BUTTON_NORMAL || style == TEXTBUTTON_STYLE_BOX || style == TEXTBUTTON_STYLE_OUTLINE)
1143                 menu_drawstring(alignpos, text, self.font_size, self.color, self.alpha, self.drawflag);
1144
1145         if(style == TEXTBUTTON_STYLE_TEXT)
1146         {
1147                 if(state == BUTTON_PRESSED)
1148                 {
1149                         menu_drawstring(alignpos, text, self.font_size, self.color_pressed, self.alpha_pressed, self.drawflag_pressed);
1150                 }
1151                 else if(state == BUTTON_SELECTED)
1152                 {
1153                         menu_drawstring(alignpos, text, self.font_size, self.color_selected, self.alpha_selected, self.drawflag_selected);
1154                 }
1155         }
1156 };*/