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