Fix #2727 "New esc-menu shouldn't open a standard settings page"
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / command / menu_cmd.qc
1 #include "menu_cmd.qh"
2
3 #include "../menu.qh"
4 #include "../item.qh"
5
6 #include <menu/mutators/_mod.qh>
7
8 #include <common/command/_mod.qh>
9
10 .void(entity me, int argsbuf) readInputArgs;
11 .entity firstChild, nextSibling;
12
13 string _dumptree_space;
14 void _dumptree_open(entity pass, entity me)
15 {
16         string s;
17         s = me.toString(me);
18         if (s == "") s = me.classname;
19         else s = strcat(me.classname, ": ", s);
20         print(_dumptree_space, etos(me), " (", s, ")");
21         if (me.firstChild)
22         {
23                 print(" {\n");
24                 _dumptree_space = strcat(_dumptree_space, "  ");
25         }
26         else
27         {
28                 print("\n");
29         }
30 }
31 void _dumptree_close(entity pass, entity me)
32 {
33         if (me.firstChild)
34         {
35                 _dumptree_space = substring(_dumptree_space, 0, strlen(_dumptree_space) - 2);
36                 print(_dumptree_space, "}\n");
37         }
38 }
39
40 float updateConwidths(float width, float height, float pixelheight);
41
42 void GameCommand(string theCommand)
43 {
44         int argc = tokenize_console(theCommand);
45         string ss = strtolower(argv(0));
46
47         // TODO port these commands to the command system
48         if (argv(0) == "help" || argc == 0)
49         {
50                 LOG_HELP("Usage:^3 menu_cmd <command> [<item>], where possible commands are:");
51                 LOG_HELP("  'sync' reloads all cvars on the current menu page");
52                 LOG_HELP("  'directmenu' shows the menu window named <item> (or the menu window containing an item named <item>)");
53                 LOG_HELP("   if <item> is not specified it shows the list of available items in the console");
54                 LOG_HELP("  'dumptree' dumps the state of the menu as a tree to the console");
55
56                 LOG_HELP("\nGeneric commands shared by all programs:");
57                 GenericCommand_macro_help();
58
59                 return;
60         }
61
62         if (GenericCommand(theCommand)) return;
63
64         if (argv(0) == "sync")
65         {
66                 m_sync();
67                 return;
68         }
69
70         if (argv(0) == "update_conwidths_before_vid_restart")
71         {
72                 updateConwidths(cvar("vid_width"), cvar("vid_height"), cvar("vid_pixelheight"));
73                 return;
74         }
75
76         if (argv(0) == "directmenu" || argv(0) == "directpanelhudmenu")
77         {
78                 string filter = string_null;
79                 if (argv(0) == "directpanelhudmenu") filter = "HUD";
80
81                 if (argc == 1)
82                 {
83                         LOG_HELP("Available items:");
84
85                         FOREACH_ENTITY_ORDERED(it.name != "", {
86                                 if (it.classname == "vtbl") continue;
87                                 string s = it.name;
88                                 if (filter)
89                                 {
90                                         if (!startsWith(s, filter)) continue;
91                                         s = substring(s, strlen(filter), strlen(s) - strlen(filter));
92                                 }
93                                 LOG_HELP(" ", s);
94                         });
95                 }
96                 else if (argc == 2 && (!isdemo() || argv(1) == "Welcome")) // don't allow this command in demos
97                 {
98                         m_play_click_sound(MENU_SOUND_OPEN);
99                         m_goto(strcat(filter, argv(1))); // switch to a menu item
100                 }
101                 else if(argc > 2 && (!isdemo() || argv(1) == "Welcome"))
102                 {
103                         entity e = NULL;
104                         float argsbuf = 0;
105                         string s = strzone(argv(1)); // dialog name
106                         for(int i = 0; (e = nextent(e)); )
107                                 if(e.classname != "vtbl" && e.name == strcat(filter, s))
108                                 {
109                                         argsbuf = buf_create();
110                                         if(argsbuf >= 0)
111                                         if(e.readInputArgs)
112                                         {
113                                                 for(i = 2; i < argc; ++i)
114                                                         bufstr_add(argsbuf, argv(i), 1);
115                                                 e.readInputArgs(e, argsbuf);
116                                                 m_goto(strcat(filter, s));
117                                         }
118                                         if(argsbuf >= 0)
119                                                 buf_del(argsbuf);
120                                 }
121                 }
122                 return;
123         }
124
125         if (argv(0) == "nexposee")
126         {
127                 m_goto("nexposee");
128                 return;
129         }
130
131         if (argv(0) == "servers")
132         {
133                 m_goto("servers");
134                 return;
135         }
136
137         if (argv(0) == "profile")
138         {
139                 m_goto("profile");
140                 return;
141         }
142
143         if (argv(0) == "skinselect")
144         {
145                 m_goto("skinselector");
146                 return;
147         }
148
149         if (argv(0) == "languageselect")
150         {
151                 m_goto("languageselector");
152                 return;
153         }
154
155         if (argv(0) == "settings")
156         {
157                 m_goto("settings");
158                 return;
159         }
160
161         if (argv(0) == "inputsettings")
162         {
163                 m_goto("inputsettings");
164                 return;
165         }
166
167         if (argv(0) == "videosettings")
168         {
169                 m_goto("videosettings");
170                 return;
171         }
172
173         if (argv(0) == "dumptree")
174         {
175                 _dumptree_space = "";
176                 depthfirst(main, parent, firstChild, nextSibling, _dumptree_open, _dumptree_close, NULL);
177                 return;
178         }
179
180         if(MUTATOR_CALLHOOK(Menu_ConsoleCommand, ss, argc, theCommand)) // handled by a mutator
181                 return;
182
183         LOG_INFO("Invalid command. For a list of supported commands, try menu_cmd help.");
184 }