]> icculus.org git repositories - divverent/nexuiz.git/blob - menu/mcustom.qc
-Added support to some of Toddd's sounds.
[divverent/nexuiz.git] / menu / mcustom.qc
1 ///////////////////////////////////////////////
2 // Custom Menu Source File
3 ///////////////////////
4 // This file belongs to dpmod/darkplaces
5 // AK contains menu specific stuff that is made especially for dpmod
6 // AK this file is used e.g. for defining some special event functions
7 ////////////////////////////////////////////////
8
9 ////////////////
10 // global stuff
11 ///
12
13 void() nex_setposition =
14 {
15         self.pos_x = 0;
16         self.pos_y = (self.font_size_y + 5) * self.orderpos;
17         self.pos_z = 0;
18         
19         self.origin = self.pos;
20 };
21
22 void(entity item) nex_makeonlyvisible =
23 {
24         local entity node;
25         
26         if( item.flag & FLAG_HIDDEN )
27                 item.flag = item.flag - FLAG_HIDDEN;
28         for( node = item._next ; node ; node = node._next )
29                 node.flag = item.flag | FLAG_HIDDEN;
30         for( node = item._prev ; node ; node = node._prev )
31                 node.flag = item.flag | FLAG_HIDDEN;
32 };
33
34 void(void) nex_linkrelhack =
35 {
36         self._child._parent = self;
37 };
38
39 void(void) nex_maketextzone =
40 {
41         self.text = strzone(self.text);
42 };
43
44 void(void) nex_slidertext =
45 {
46         entity ent;
47         if(self.link == "")
48         {
49                 print("No link specified\n");
50                 eprint(self);
51                 self.init = null_function;
52                 return;
53         }
54
55         ent = menu_getitem(self.link);
56         if(ent == null_entity)
57         {
58                 objerror("No link found for ", self.link,"\n");
59         }
60
61         self._link = ent;
62
63         self.flag = self.flag | FLAG_DRAWREFRESHONLY;
64
65         self.refresh = _nex_slidertext_refresh;
66 };
67
68 void(void) _nex_slidertext_refresh =
69 {
70         self.text = ftos(self._link.value);
71         if(self.maxlen > 0)
72                 self.text = substring(self.text,0, self.maxlen);
73         // reset the size, so its set
74         self.size = '0 0 0';
75 };
76
77 float(float keynr, float ascii) nex_redirect_key =
78 {
79         if((ascii>=20 && ascii <= 126) || keynr == K_BACKSPACE || keynr == K_ENTER || keynr == K_LEFTARROW || keynr == K_RIGHTARROW || (keynr >= K_MOUSE1 && keynr <= K_MOUSE10))
80         {
81                 raise_key(self._child, keynr, ascii);
82                 return true;
83         }
84         return false;
85 };
86
87 void(void) nex_cvar_slider =
88 {
89         self.value = cvar(self.cvarname);
90         self.slidermove = self.switchchange = _nex_cvar_slider;
91         self.refresh = _nex_cvar_slider_refresh;
92 };
93
94 void(void) _nex_cvar_slider_refresh =
95 {
96         if(self.cvartype == CVAR_INT || self.cvartype == CVAR_FLOAT || self.cvartype == CVAR_STEP)
97                 self.value = cvar(self.cvarname);
98 };
99
100 void(void) _nex_cvar_slider =
101 {
102         if(self.cvarname == "")
103                 return;
104         if(self.cvartype == CVAR_INT) // || self.cvartype == CVAR_STRING)
105                 self.value = rint(self.value);
106         if(self.cvartype == CVAR_STEP)
107                 self.value = rint(self.value / self.step) * self.step;
108         if(self.cvartype == CVAR_INT || self.cvartype == CVAR_FLOAT || self.cvartype == CVAR_STEP)
109                 cvar_set(self.cvarname, ftos(self.value));
110         /*if(cvartype == CVAR_STRING)
111         {
112                 string s;
113                 s = getaltstring(self.value, self.cvarvalues);
114                 cvar_set(self.cvarname, s);
115         }
116         */
117 };
118
119 //////////////
120 // main.menu
121 ///
122
123 void(void) nex_makeselfonlyvisible =
124 {
125         nex_makeonlyvisible( self );
126 };
127
128 // quit menu
129
130 void(void) nex_quit_choose =
131 {
132         entity e;
133         // because of the missing support for real array, we have to do it the stupid way
134         // (we also have to use strzone for the text, cause it the temporary strings wont work
135         // for it)
136         if(nex_quitrequest == 0)
137         {
138                 e = menu_getitem("quit_msg_0");
139                 e.text = getaltstring(0,nex_quitmsg_0);
140         }
141         if(nex_quitrequest == 1)
142         {
143                 e = menu_getitem("quit_msg_0");
144                 e.text = getaltstring(0,nex_quitmsg_1);
145         }
146         if(nex_quitrequest == 2)
147         {
148                 e = menu_getitem("quit_msg_0");
149                 e.text = getaltstring(0,nex_quitmsg_2);
150         }
151         if(nex_quitrequest == 3)
152         {
153                 e = menu_getitem("quit_msg_0");
154                 e.text = getaltstring(0,nex_quitmsg_3);
155         }
156         e.text = strzone(e.text);
157
158         if(nex_quitrequest == 0)
159         {
160                 e = menu_getitem("quit_msg_1");
161                 e.text = getaltstring(1,nex_quitmsg_0);
162         }
163         if(nex_quitrequest == 1)
164         {
165                 e = menu_getitem("quit_msg_1");
166                 e.text = getaltstring(1,nex_quitmsg_1);
167         }
168         if(nex_quitrequest == 2)
169         {
170                 e = menu_getitem("quit_msg_1");
171                 e.text = getaltstring(1,nex_quitmsg_2);
172         }
173         if(nex_quitrequest == 3)
174         {
175                 e = menu_getitem("quit_msg_1");
176                 e.text = getaltstring(1,nex_quitmsg_3);
177         }
178         e.text = strzone(e.text);
179
180         nex_quitrequest = nex_quitrequest + 1;
181         if(nex_quitrequest == DPMOD_QUIT_MSG_COUNT)
182                 nex_quitrequest = 0;
183 };
184
185 void(void) nex_quit =
186 {
187 /*  entity ent;
188         // choose a quit message
189         nex_quit_choose();
190
191         // change the flags
192         ent = menu_getitem("main");
193         ent.flag = ent.flag | FLAG_CHILDDRAWONLY;
194         ent = menu_getitem("quit");
195         ent.flag = FLAG_NOSELECT;
196         menu_jumptowindow(ent, false);*/
197         entity ent;
198
199         // change the flags
200         ent = menu_getitem("quitbox_ref");
201         ent._child = menu_activewindow;
202         ent = menu_getitem("quitbox");
203         menu_jumptowindow(ent, true);
204 };
205
206 void(void) nex_quit_yes =
207 {
208         cmd("quit\n");
209 };
210
211 void(void) nex_quit_no =
212 {
213 /*  entity ent;
214
215         ent = menu_getitem("quit_msg_0");
216         strunzone(ent.text);
217
218         ent = menu_getitem("quit_msg_1");
219         strunzone(ent.text);
220
221         ent = menu_getitem("quit");
222         ent.flag = FLAG_HIDDEN;
223         ent = menu_getitem("main");
224         ent.flag = ent.flag - FLAG_CHILDDRAWONLY;
225         menu_selectup();*/
226         menu_selectup();
227 };
228
229 float(float keynr, float ascii) nex_quit_key =
230 {
231         if(keynr == K_LEFTARROW)
232                 return false;
233         if(keynr == K_RIGHTARROW)
234                 return false;
235         if(keynr == K_ENTER)
236                 return false;
237         if(keynr == K_MOUSE1)
238                 return false;
239         if(ascii == 'Y' || ascii == 'y')
240                 nex_quit_yes();
241         if(ascii == 'N' || ascii == 'n' || keynr == K_ESCAPE)
242                 nex_quit_no();
243         return true;
244 };
245
246 // options menu
247
248 void(void) nex_display_options =
249 {
250         entity ent;
251
252         ent = menu_getitem( "options" );
253         
254         nex_makeonlyvisible( ent );
255         menu_jumptowindow( ent, false );
256 };
257
258 /////////////////
259 // video.menu
260
261 void(void) nex_display_video =
262 {
263         nex_makeonlyvisible( menu_getitem( "video" ) );
264         menu_jumptowindow( menu_getitem( "video" ), false );
265 };
266
267 void(void) nex_video_bpp_reinit =
268 {
269         if(cvar("vid_bitsperpixel") == 32)
270                 self.value = 1;
271         else
272                 self.value = 0;
273 };
274
275 void(void) nex_video_fullscreen_reinit =
276 {
277         self.value = cvar("vid_fullscreen");
278 };
279
280 string nex_video_resolutions;
281
282 void(void) nex_video_resolution_switch_reinit =
283 {
284         float c, i;
285         float pos86;
286
287         c = getaltstringcount(nex_video_resolutions);
288         for(i = 0; i < c; i=i+1)
289         {
290                 string s;
291                 vector t;
292                 s = getaltstring(i,nex_video_resolutions);
293                 s = strcat("'",s,"'");
294                 t = stov(s);
295                 if(t_x == cvar("vid_width") && t_y == cvar("vid_height"))
296                 {
297                         self.value = i;
298                         return;
299                 }
300                 if(t == '800 600 0')
301                         pos86 = i;
302         }
303
304         self.value = pos86;
305 };
306
307 void(void) nex_video_resolution_switch =
308 {
309         var float pos86 = 0;
310         var float counter = 0;
311         vector t;
312
313         nex_video_resolutions = "";
314         self.text = "";
315         self.value = -1;
316
317         while((t = getresolution(counter)) != '0 0 0')
318         {
319                 if(t == '800 600 0')
320                         pos86 = counter;
321                 if(t_x == cvar("vid_width") && t_y == cvar("vid_height"))
322                 {
323                         self.value = counter;
324                 }
325                 counter = counter + 1;
326                 self.text = strcat(self.text,"'",ftos(t_x),"x");
327                 self.text = strcat(self.text,ftos(t_y),"'");
328                 nex_video_resolutions = strcat(nex_video_resolutions,vtos(t));
329         }
330
331         if(self.value == -1)
332                 self.value = pos86;
333
334         self.text = strzone(self.text);
335         nex_video_resolutions = strzone(nex_video_resolutions);
336
337         self.reinit = nex_video_resolution_switch;
338 };
339
340 void(void) nex_video_apply =
341 {
342         vector set, res;
343         float changed;
344         entity tmp;
345
346         changed = false;
347
348         // resolution test
349         res_x = cvar("vid_width");
350         res_y = cvar("vid_height");
351         res_z = 0;
352
353         tmp = menu_getitem("video_resolution_switch");
354         set = stov(getaltstring(tmp.value, nex_video_resolutions));
355         if(set != res)
356         {
357                 cvar_set("vid_width",ftos(set_x));
358                 cvar_set("vid_height",ftos(set_y));
359                 changed = true;
360         }
361         // bpp test
362         tmp = menu_getitem("video_bpp_switch");
363         if((tmp.value+1)*16 != cvar("vid_bitsperpixel"))
364         {
365                 cvar_set("vid_bitsperpixel",ftos((tmp.value+1)*16));
366                 changed = true;
367         }
368         // fullscreen changed
369         tmp = menu_getitem("video_fullscreen_switch");
370         if(tmp.value != cvar("vid_fullscreen"))
371         {
372                 cvar_set("vid_fullscreen",ftos(tmp.value));
373                 changed = true;
374         }
375
376         if(changed)
377         {
378                 cmd("vid_restart\n");
379         }
380 };
381
382 /////////////////
383 // xplayer.menu
384 ///
385
386 float maxfrags;
387 float maxtime;
388 float maxbots;
389
390 string hostname;
391 float  maxclients;
392 float  publicserver;
393
394 entity maps_list;
395 entity maps_current;
396
397 // text = info text
398 // picture = picture filename
399 // name = map name
400 void(entity node) _nex_xp_remove =
401 {
402         strunzone( node.picture );
403         strunzone( node.text );
404         strunzone( node.name );
405         remove( node );
406 }
407
408 void(void) _nex_xp_update =
409 {
410         entity item;
411         
412         item = menu_getitem( "multiplayer_map_name" );
413         item.text = maps_current.name;
414         
415         item = menu_getitem( "multiplayer_map_picture" );
416         item.picture = maps_current.picture;
417         
418         item = menu_getitem( "multiplayer_map_info" );
419         item.text = maps_current.text;
420 };
421
422 void(void) nex_xp_enummaps =
423 {
424         float count;
425         float searchhandle;
426         float counter;
427         
428         // remove the old list
429         if( maps_list ) {
430                 entity node;
431                 
432                 node = maps_list;
433                 while( node._next ) {
434                         node = node._next;
435                         _nex_xp_remove( node._prev );
436                 }
437                 _nex_xp_remove( node );
438         }
439         maps_list = maps_current = null_entity;
440         
441         searchhandle = search_begin( "maps/*.bsp", true, true );
442         if( searchhandle == -1 ) {
443                 print( "menu: No maps found!\n" );
444                 return;
445         } else if( searchhandle == -2)
446                 return;
447                 
448         maps_list = maps_current = spawn();
449         count = search_getsize( searchhandle );
450         for( counter = 0 ; counter < count ; counter = counter + 1 ) {
451                 float file;
452                 string fname;
453                 
454                 fname = search_getfilename( searchhandle, counter );
455                 fname = substring( fname, 0, strlen( fname ) -  4 );
456                 fname = strzone( fname );
457                 
458                 //try to find the information text
459                 file = fopen( strcat( fname, ".txt" ), FILE_READ );
460                 if( file == -1 )
461                         maps_current.text = strzone( "--NO INFORMATION AVAILABLE--" );
462                 else {
463                         string temp, old;
464                         maps_current.text = strzone( "" );
465                         do {
466                                 old = maps_current.text;
467                                 temp = fgets( file );
468                                 maps_current.text = strzone( strcat( old, temp, "\n" ) );
469                                 strunzone( old );
470                         } while( temp );
471                         fclose( file );
472                 }
473                 
474                 //try to find the picture
475                 file = search_begin( strcat( fname, ".jpg" ), true, true );
476                 if( file == -1 )
477                         maps_current.picture = strzone( "gfx/m_nomap" );
478                 else {
479                         maps_current.picture = strzone( strcat( fname, ".jpg" ) );
480                         search_end( file );
481                 }
482                 
483                 maps_current.name = strzone( substring( fname, 5, strlen( fname ) - 5 ) ); // remove the 'maps/'
484                 strunzone( fname );
485                 
486                 // create next item and link it with the list
487                 maps_current._next = spawn();
488                 maps_current._next._prev = maps_current;
489                 maps_current = maps_current._next;
490         }
491         // remove the last item
492         maps_current = maps_current._prev;
493         remove( maps_current._next );
494         maps_current._next = null_entity;
495         
496         search_end( searchhandle );
497
498         maps_current = maps_list;
499         _nex_xp_update();                       
500 };
501
502 void(void) nex_xp_prev =
503 {
504         if( maps_current._prev )
505                 maps_current = maps_current._prev;
506         _nex_xp_update();       
507 };
508
509 void(void) nex_xp_next =
510 {
511         if( maps_current._next )
512                 maps_current = maps_current._next;
513         _nex_xp_update();
514 };
515
516
517 void(void) nex_display_multiplayer =
518 {
519         nex_makeonlyvisible( menu_getitem( "multiplayer" ) );
520         menu_jumptowindow( menu_getitem( "multiplayer" ), false );
521 };
522
523 void(void) nex_xp_maxfrags =
524 {
525         float x;
526         x = stof(self.text);
527         maxfrags = rint(x);
528
529         strunzone(self.text);
530         self.text = ftos(x);
531         self.text = strzone(self.text);
532 };
533
534 void(void) nex_xp_maxbots =
535 {
536         float x;
537         x = stof(self.text);
538         maxbots = rint(x);
539
540         strunzone(self.text);
541         self.text = ftos(x);
542         self.text = strzone(self.text);
543 };
544
545 void(void) nex_xp_maxtime =
546 {
547         float x;
548         x = stof(self.text);
549         maxtime = rint(x);
550
551         strunzone(self.text);
552         self.text = ftos(maxtime);
553         self.text = strzone(self.text);
554 };
555
556 void(void) nex_sp_start =
557 {
558         //local float i;
559
560         cvar_set("fraglimit", ftos(maxfrags));
561         cvar_set("timelimit", ftos(maxtime));
562         cvar_set("deathmatch", ftos(1));
563         cvar_set("maxplayers", ftos(maxbots + 1));
564         cmd("map ");
565         cmd(maps_current.name);
566         cmd(";wait;togglemenu;wait;");
567
568         // BLACK: TODO - change this somewhen into something cleaner - see bot/bot.qc
569         //if(maxbots && maxbots <= (99 - 40 + 1))
570         {
571                 string t;
572                 t = strcat("impulse ", ftos(40 + maxbots - 1));
573                 cmd(t);
574                 print(t);
575         }
576
577         cmd("\n");
578 };
579
580 void(void) nex_mp_hostname =
581 {
582         hostname = self.text;
583 };
584
585 void(void) nex_mp_maxclients =
586 {
587         local float i;
588         i = stof(self.text);
589         maxclients = rint(i);
590
591         strunzone(self.text);
592         self.text = strzone(ftos(maxclients));
593 };
594
595 void(void) nex_mp_publicserv =
596 {
597         publicserver = self.value;
598 };
599
600 void(void) nex_mp_start =
601 {
602         cvar_set("fraglimit", ftos(maxfrags));
603         cvar_set("timelimit", ftos(maxtime));
604         cvar_set("deathmatch", ftos(1));
605         cvar_set("maxplayers", ftos(maxclients));
606         cvar_set("sv_public", ftos(publicserver));
607         cvar_set("hostname", hostname);
608         cmd("map ");
609         cmd(maps_current.name);
610         cmd(";wait;togglemenu\n");
611 };
612
613 void(void) nex_goto_createserver =
614 {
615         menu_jumptowindow( menu_getitem( "multiplayer_frame"), false );
616 }
617
618 void(void) nex_draw_text =
619 {
620         local vector drawpos;
621         local float cpos, end;
622         
623         cpos = 0;
624         drawpos = self.pos;
625         drawpos_y = drawpos_y + self.font_size_y; 
626         do {
627                 local string l;
628                 local float i;
629                 
630                 end = strlen( self.text );
631                 i = cpos;
632                 do {
633                         l = substring( self.text, i, 1 );
634                         
635                         if( l == "\n" ) {
636                                 end = i;
637                                 break;
638                         }
639                         
640                         i = i + 1;
641                 } while( l != "" );
642                 
643                 l = substring( self.text, cpos, end - cpos );
644                 if( l != "" )
645                         menu_drawstring( drawpos, l, self.font_size, self.color, self.alpha, self.drawflag );
646                 drawpos_y = drawpos_y + self.font_size_y;
647                 
648                 cpos = end + 1;
649         } while( end != strlen( self.text ) );                  
650 }
651
652 // server list
653 var float slist_selected = 0;
654 var float slist_start   = 0;
655 float slist_estimatedsize;
656
657 void(void) nex_display_serverlist =
658 {
659         if(menu_activewindow.name == "serverlist")
660                 return;
661         menu_jumptowindow(menu_getitem("serverlist"),true);
662         cmd("net_slist\n");
663 };
664
665 // use a ITEM_TEXT as base item for this
666 // we need another item to clip the slist
667 // (another reason for a new menu qc!)
668 void(void) slist_draw =
669 {
670         float c;
671         vector pos;
672         float listsize;
673
674         //print("slist drawing...", ftos(listsize), "\n");
675
676         pos = '0 0 0'; // the upper item sets the origin
677         listsize = gethostcachevalue(SLIST_HOSTCACHECOUNT);
678         if(!listsize)
679         {
680                 menu_drawstring(pos, "No Servers", self.font_size, self.color, self.alpha, self.drawflag);
681                 return;
682         }
683
684         for(c = 0; c < slist_estimatedsize + 1; c = c + 1)
685         {
686                 string line;
687
688                 if(listsize <= slist_start + c)
689                         break;
690
691                 //print("drawing ", ftos(c),"\n");
692
693                 if(slist_selected - slist_start == c)
694                 {
695                         vector s;
696                         s = self.size;
697                         s_y = self.font_size_y * 2;
698
699                         menu_fillarea(pos, s, self.color_selected, self.alpha_selected * (sin(time * 9) + 1) / 2, self.drawflag_selected);
700                 }
701
702                 line = gethostcachestring(SLIST_LINE1, slist_start + c);
703
704                 menu_drawstring(pos, line, self.font_size, self.color, self.alpha, self.drawflag);
705
706                 pos_y = pos_y + self.font_size_y;
707
708                 line = gethostcachestring(SLIST_LINE2, slist_start + c);
709
710                 menu_drawstring(pos, line, self.font_size, self.color, self.alpha, self.drawflag);
711
712                 pos_y = pos_y + self.font_size_y;
713         }
714 };
715
716 void(void) slist_init =
717 {
718         self.size = self._parent.size;
719         slist_estimatedsize = self.size_y / self.font_size_y / 2;
720 };
721
722 void(void) slist_refresh =
723 {
724         float hostcachesize;
725
726         hostcachesize = gethostcachevalue(SLIST_HOSTCACHECOUNT);
727         if(slist_start >= hostcachesize)
728                 slist_start = ceil(hostcachesize - slist_estimatedsize);
729         if(slist_start < 0)
730                 slist_start = 0;
731         if(slist_selected >= hostcachesize)
732                 slist_selected = hostcachesize;
733 };
734
735 void(void) slist_join =
736 {
737         string cname;
738         cname = gethostcachestring(SLIST_CNAME, slist_selected);
739
740         if(cname != "")
741         {
742                 cmd("connect ");
743                 cmd(cname);
744                 cmd(" ;togglemenu\n");
745         }
746 };
747
748 float(float keynr, float ascii ) slist_key =
749 {
750         float listsize;
751
752         if(keynr == K_UPARROW)
753         {
754                 if(slist_selected > 0)
755                         slist_selected = slist_selected - 1;
756                 if(slist_start > slist_selected)
757                         slist_start = slist_selected;
758                 return TRUE;
759         } else if(keynr == K_DOWNARROW)
760         {
761                 listsize = gethostcachevalue(SLIST_HOSTCACHECOUNT);
762                 if(slist_selected < listsize - 1)
763                         slist_selected = slist_selected + 1;
764                 if(slist_start + slist_estimatedsize - 0.5 < slist_selected)
765                         slist_start = ceil(slist_selected - slist_estimatedsize + 0.5);
766                 return TRUE;
767         } else if(keynr == K_ENTER)
768         {
769                 slist_join();
770                 return TRUE;
771         } else if(keynr == K_SPACE)
772         {
773                 cmd("net_slist\n");
774                 return TRUE;
775         } else if(keynr == K_MOUSE1)
776         {
777                 float pos;
778
779                 listsize = gethostcachevalue(SLIST_HOSTCACHECOUNT);
780                 pos = floor(menu_cursor_y / self.font_size_y / 2);
781
782                  if(pos + slist_start == slist_selected)
783                         slist_join();
784                 else if(pos + slist_start < listsize)
785                         slist_selected = pos;
786                 return TRUE;
787         }
788         return FALSE;
789 };
790
791 void(void) slist_info1 =
792 {
793         float query, reply;
794         string temp;
795
796         query = gethostcachevalue(SLIST_MASTERQUERYCOUNT);
797         reply = gethostcachevalue(SLIST_MASTERREPLYCOUNT);
798
799         strunzone(self.text);
800
801         temp = strcat(ftos(reply), "/");
802         temp = strcat(temp, ftos(query), " Master Servers");
803
804         self.text = strzone(temp);
805 };
806
807 void(void) slist_info2 =
808 {
809         float query, reply;
810         string temp;
811
812         query = gethostcachevalue(SLIST_SERVERQUERYCOUNT);
813         reply = gethostcachevalue(SLIST_SERVERREPLYCOUNT);
814
815         strunzone(self.text);
816
817         temp = strcat(ftos(reply), "/");
818         temp = strcat(temp, ftos(query), " Game Servers");
819
820         self.text = strzone(temp);
821 };
822
823 /////////////////
824 // options.menu
825 ///
826
827 void(void) nex_options_alwaysrun_switchchange =
828 {
829         if(self.value)
830         {
831                 cvar_set("cl_forwardspeed","400");
832                 cvar_set("cl_backspeed","400");
833         }
834         else
835         {
836                 cvar_set("cl_forwardspeed","200");
837                 cvar_set("cl_backspeed","200");
838         }
839 };
840
841 void(void) nex_options_alwaysrun_refresh =
842 {
843         if(cvar("cl_forwardspeed") > 200)
844                 self.value = 1;
845         else
846                 self.value = 0;
847 };
848
849 void(void) nex_options_invmouse_switchchange =
850 {
851         float old;
852         old = 0 - cvar("m_pitch");
853         cvar_set("m_pitch",ftos(old));
854 };
855
856 void(void) nex_options_invmouse_refresh =
857 {
858         if(cvar("m_pitch") > 0)
859                 self.value = 0;
860         else
861                 self.value = 1;
862 };
863
864 void(void) nex_snd =
865 {
866         entity ent;
867         ent = menu_getitem("options_sound");
868         nex_makeonlyvisible( ent );
869         menu_jumptowindow(ent, false);
870 }
871
872 void(void) nex_snd_cd_init =
873 {
874         if(cvar("cdaudioinitialized"))
875         {
876                 self.flag = FLAG_AUTOSETCLICK;
877                 self.color = ITEM_TEXT_NORMAL_COLOR;
878         }
879         else
880         {
881                 self.flag = FLAG_NOSELECT | FLAG_DRAWONLY | FLAG_CHILDDRAWONLY;
882                 self.color = '0.5 0.5 0.5';
883         }
884 };
885
886 void(void) nex_snd_snd_init =
887 {
888         if(cvar("snd_initialized"))
889         {
890                 self.flag = FLAG_AUTOSETCLICK;
891                 self.color = ITEM_TEXT_NORMAL_COLOR;
892         }
893         else
894         {
895                 self.flag = FLAG_NOSELECT | FLAG_DRAWONLY | FLAG_CHILDDRAWONLY;
896                 self.color = '0.5 0.5 0.5';
897         }
898 };
899
900 void(void) nex_cc =
901 {
902         entity ent;
903         ent = menu_getitem("options_cc");
904         nex_makeonlyvisible( ent );
905         menu_jumptowindow(ent, false);
906 }
907
908 void(void) nex_cc_reset =
909 {
910         cmd(
911         "v_hwgamma 1;"
912         "v_gamma 1;"
913         "v_contrast 1;"
914         "v_brightness 0;"
915         "v_color_enable 0;"
916         "v_color_black_r 0;"
917         "v_color_black_g 0;"
918         "v_color_black_b 0;"
919         "v_color_grey_r 0;"
920         "v_color_grey_g 0;"
921         "v_color_grey_b 0;"
922         "v_color_white_r 1;"
923         "v_color_white_g 1;"
924         "v_color_white_b 1;"
925         "\n");
926 };
927
928 void(void) nex_cc_check_hwgamma =
929 {
930         if(cvar("vid_hardwaregammasupported"))
931         {
932                 self.flag = FLAG_AUTOSETCLICK;
933                 self.color = ITEM_TEXT_NORMAL_COLOR;
934         }
935         else
936         {
937                 self.flag = FLAG_NOSELECT;
938                 self.color = '0.5 0.5 0.5';
939         }
940 };
941
942 void(void) nex_cc_check_gamma = // used in key -- BADBAD HACKHACK
943 {
944         if(cvar("v_hwgamma") && cvar("vid_hardwaregammasupported") && !cvar("v_color_enable"))
945         {
946                 self.flag = FLAG_AUTOSETCLICK;
947                 self.color = ITEM_TEXT_NORMAL_COLOR;
948         }
949         else
950         {
951                 self.flag = FLAG_NOSELECT;
952                 self.color = '0.5 0.5 0.5';
953         }
954 };
955
956 void(void) nex_cc_check_grey = // used in key -- BADBAD HACKHACK
957 {
958         if(cvar("v_hwgamma") && cvar("vid_hardwaregammasupported") && cvar("v_color_enable"))
959         {
960                 self.flag = FLAG_AUTOSETCLICK;
961                 self.color = ITEM_TEXT_NORMAL_COLOR;
962         }
963         else
964         {
965                 self.flag = FLAG_NOSELECT;
966                 self.color = '0.5 0.5 0.5';
967         }
968 };
969
970 void(void) nex_cc_check_ncolor_enable =
971 {
972         if(!cvar("v_color_enable"))
973         {
974                 self.flag = FLAG_AUTOSETCLICK;
975                 self.color = ITEM_TEXT_NORMAL_COLOR;
976         }
977         else
978         {
979                 self.flag = FLAG_NOSELECT;
980                 self.color = '0.5 0.5 0.5';
981         }
982 };
983
984 void(void) nex_cc_check_color_enable =
985 {
986         if(cvar("v_color_enable"))
987         {
988                 self.flag = FLAG_AUTOSETCLICK;
989                 self.color = ITEM_TEXT_NORMAL_COLOR;
990         }
991         else
992         {
993                 self.flag = FLAG_NOSELECT;
994                 self.color = '0.5 0.5 0.5';
995         }
996 };
997
998 void(void) nex_cc_color_enable =
999 {
1000         cvar_set("v_color_enable","1");
1001 };
1002
1003 void(void) nex_cc_ncolor_enable =
1004 {
1005         cvar_set("v_color_enable","0");
1006 };
1007
1008 void(void) nex_cc_grey_refresh =
1009 {
1010         self.value = cvar(strcat(self.cvarname,"_r"));
1011         self.value = self.value + cvar(strcat(self.cvarname,"_g"));
1012         self.value = self.value + cvar(strcat(self.cvarname,"_b"));
1013         self.value = self.value / 3;
1014 };
1015
1016
1017 void(void) nex_cc_grey_move =
1018 {
1019         string tmp;
1020         tmp = ftos(self.value);
1021         cvar_set(strcat(self.cvarname,"_r"), tmp);
1022         cvar_set(strcat(self.cvarname,"_g"), tmp);
1023         cvar_set(strcat(self.cvarname,"_b"), tmp);
1024 };
1025
1026 void(void) nex_cc_grey =
1027 {
1028         self.refresh = nex_cc_grey_refresh;
1029         self.slidermove = nex_cc_grey_move;
1030 };
1031
1032 // key control stuff
1033 void(void) nex_cntrl =
1034 {
1035         entity ent;
1036         ent = menu_getitem("options_control");
1037         nex_makeonlyvisible( ent );
1038         menu_jumptowindow(ent, false);
1039 }
1040
1041 const float NUMKEYS = 2;        // visible key count (first and secondary key)
1042 string bindcommand;
1043
1044 void(float keynr, float ascii) nex_con_keyhook =
1045 {
1046         entity ent;
1047
1048         //dprint("nex_con_keyhook called !\n");
1049
1050         if(keynr != K_ESCAPE)
1051         {
1052                 string tmp;
1053                 tmp = bind_getkeylist(bindcommand);
1054                 // remove the binds if we need more space
1055                 bind_limitbinds(NUMKEYS - 1, bindcommand);
1056                 // bind the new key
1057                 bind_bindkey(bind_getstringforkey(keynr), bindcommand);
1058         }
1059
1060         ent = menu_getitem("options_control_statemsg1");
1061         ent.value = 0;
1062
1063         ent = menu_getitem("options_control_statemsg2");
1064         ent.value = 0;
1065
1066         menu_keyhook = null_function;
1067 };
1068
1069 void(void) nex_con_action_key =
1070 {
1071         entity ent;
1072
1073         bindcommand = self.link;
1074
1075         //dprint("action called");
1076
1077         menu_keyhook = nex_con_keyhook;
1078
1079         ent = menu_getitem("options_control_statemsg1");
1080         ent.value = 1;
1081
1082         ent = menu_getitem("options_control_statemsg2");
1083         ent.value = 1;
1084 };
1085
1086 float(float keynr, float ascii) nex_con_key =
1087 {
1088         if(keynr == K_DEL)
1089         {
1090                 // the user wants to unbind this action
1091                 //dprint("Removing binds for '", self.link,"'\n");
1092                 bind_limitbinds(0, self.link);
1093                 return true;
1094         }
1095
1096         return false;
1097 };
1098
1099 void(void) nex_con_update_keys =
1100 {
1101         entity ent;
1102         string keystr;
1103         float num;
1104
1105         if(!self._child)
1106                 return;
1107
1108         keystr = strzone(bind_getkeylist(self.link));
1109
1110         num = 0;
1111
1112         for(ent = self._child; ent != null_entity; ent = ent._next)
1113         {
1114                 float keynr;
1115
1116                 keynr = stof(getaltstring(num, keystr));
1117
1118                 strunzone(ent.text);
1119
1120                 if(keynr == -1 )
1121                         ent.text = strzone("-");
1122                 else
1123                         ent.text = strzone(bind_getstringforkey(keynr));
1124
1125                 num = num + 1;
1126         }
1127
1128         strunzone(keystr);
1129 };
1130
1131 var float numkey = 0;
1132 void(void) CONTROL_KEY =
1133 {
1134         entity key1, key2, desc;
1135         entity temp_desc, temp_key;
1136
1137         temp_desc = menu_getitem("control_desc_template");
1138         temp_key = menu_getitem("control_key_template");
1139
1140         desc = spawn();
1141         copyentity(temp_desc, desc);
1142
1143         key1 = spawn();
1144         copyentity(temp_key, key1);
1145
1146         key2 = spawn();
1147         copyentity(temp_key, key2);
1148
1149         desc.name = self.text;
1150         key1.name = strzone(strcat(self.text, " key1"));
1151         key2.name = strzone(strcat(self.text, " key2"));
1152
1153         desc.pos_y = key1.pos_y = key2.pos_y = temp_desc.pos_y * numkey;
1154         key2.pos_x = 2 * key2.pos_x;
1155
1156         desc.text = self.text;
1157         desc.link = self.link;
1158
1159         key1.text = strzone("-");
1160         key2.text = strzone("-");
1161
1162         desc.alignment = 0;
1163
1164         // set the parents
1165         desc.parent = "options_control";
1166         desc._parent = menu_getitem("options_control");
1167
1168         key1.parent = key2.parent = desc.name;
1169         key1._parent = key2._parent = desc;
1170
1171         //eprint(key1);
1172         //eprint(key2);
1173         //eprint(desc);
1174
1175         numkey = numkey + 1;
1176 };
1177
1178 ////////////////////////////////////////////////////
1179 // Test Stuff
1180 ///
1181 void(void) initbrightness =
1182 {
1183         self.value = cvar("scr_conbrightness");
1184 };
1185
1186 void(void) setbrightness =
1187 {
1188         cvar_set("scr_conbrightness",ftos(self.value));
1189 };
1190
1191 void(void)  nex_main_exit =
1192 {
1193         entity e;
1194         e = menu_getitem("MAIN_MENU");
1195         e.flag = FLAG_NOSELECT + FLAG_CHILDDRAWONLY;
1196         e = menu_getitem("MAIN_EXIT_MENU");
1197         e.flag = FLAG_NOSELECT;
1198         menu_jumptowindow(e, false);
1199 };
1200
1201 void(void) nex_main_exit_no =
1202 {
1203         entity e;
1204         e = menu_getitem("MAIN_EXIT_MENU");
1205         e.flag = FLAG_NOSELECT + FLAG_HIDDEN;
1206         e = menu_getitem("MAIN_MENU");
1207         e.flag = FLAG_NOSELECT;
1208         menu_selectup();
1209 };
1210
1211 void(void) nex_main_exit_yes =
1212 {
1213         cmd("quit\n");
1214 };
1215
1216 float(float keynr, float ascii) nex_main_exit_key =
1217 {
1218         if(keynr == K_ESCAPE)
1219         {
1220                 nex_main_exit_no();
1221                 return true;
1222         }
1223         return false;
1224 }
1225
1226 void(void)  dorestart =
1227 {
1228         cmd("menu_restart\n");
1229 };
1230
1231 void(void) nex_text_cur_x =
1232 {
1233         self.text = ftos(rint(cursor_x));
1234 };
1235
1236 void(void) nex_text_cur_y =
1237 {
1238         self.text = ftos(rint(cursor_y));
1239 };