]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/cvarlist.qc
take3: format 903 files
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / cvarlist.qc
1 #include "cvarlist.qh"
2
3 #include "inputbox.qh"
4 #include "../item/checkbox.qh"
5 #include "../item/container.qh"
6 #include "../item/checkbox.qh"
7
8 entity makeXonoticCvarList()
9 {
10         entity me;
11         me = NEW(XonoticCvarList);
12         me.configureXonoticCvarList(me);
13         return me;
14 }
15 void XonoticCvarList_configureXonoticCvarList(entity me)
16 {
17         me.configureXonoticListBox(me);
18         me.handle = buf_create();
19         me.nItems = 0;
20 }
21 void CvarList_Load(entity me, string filter)
22 {
23         if (me.handle < 0) {
24                 return;
25         }
26
27         buf_cvarlist(me.handle, filter, "_");
28         me.nItems = buf_getsize(me.handle);
29         if (autocvar_menu_cvarlist_onlymodified) {
30                 float newbuf = buf_create();
31                 for (int i = 0; i < me.nItems; ++i) {
32                         string k = bufstr_get(me.handle, i);
33                         if (cvar_string(k) != cvar_defstring(k)) {
34                                 bufstr_add(newbuf, k, false);
35                         }
36                 }
37                 buf_del(me.handle);
38                 me.handle = newbuf;
39                 me.nItems = buf_getsize(me.handle);
40         }
41 }
42 void XonoticCvarList_showNotify(entity me)
43 {
44         bool force_initial_selection = false;
45         if (me.handle >= 0 && me.nItems <= 0) { // me.handle not loaded yet?
46                 force_initial_selection = true;
47         }
48         CvarList_Load(me, me.controlledTextbox.text);
49         if (force_initial_selection) {
50                 me.setSelected(me, 0);
51         }
52 }
53 void XonoticCvarList_hideNotify(entity me)
54 {
55         if (me.handle) {
56                 buf_del(me.handle);
57         }
58         me.handle = buf_create();
59         me.nItems = 0;
60 }
61 void XonoticCvarList_destroy(entity me)
62 {
63         if (me.handle) {
64                 buf_del(me.handle);
65         }
66 }
67 string autocvar_menu_forced_saved_cvars;
68 string autocvar_menu_reverted_nonsaved_cvars;
69 float XonoticCvarList_updateCvarType(entity me)
70 {
71         float t;
72         t = cvar_type(me.cvarName);
73         me.cvarType = "";
74         float needsForcing;
75         if (strhasword(autocvar_menu_forced_saved_cvars, me.cvarName)) {
76                 me.cvarType = strcat(me.cvarType, ", ", _("forced to be saved to config.cfg"));
77                 needsForcing = 0;
78         } else if (strhasword(autocvar_menu_reverted_nonsaved_cvars, me.cvarName)) {
79                 // Currently claims to be saved, but won't be on next startup.
80                 me.cvarType = strcat(me.cvarType, ", ", _("will not be saved"));
81                 needsForcing = 1;
82         } else if (t & CVAR_TYPEFLAG_SAVED) {
83                 me.cvarType = strcat(me.cvarType, ", ", _("will be saved to config.cfg"));
84                 needsForcing = 0;
85         } else {
86                 me.cvarType = strcat(me.cvarType, ", ", _("will not be saved"));
87                 needsForcing = 1;
88         }
89         if (t & CVAR_TYPEFLAG_PRIVATE) {
90                 me.cvarType = strcat(me.cvarType, ", ", _("private"));
91         }
92         if (t & CVAR_TYPEFLAG_ENGINE) {
93                 me.cvarType = strcat(me.cvarType, ", ", _("engine setting"));
94         }
95         if (t & CVAR_TYPEFLAG_READONLY) {
96                 me.cvarType = strcat(me.cvarType, ", ", _("read only"));
97         }
98         me.cvarType = strzone(substring(me.cvarType, 2, strlen(me.cvarType) - 2));
99         me.cvarTypeBox.setText(me.cvarTypeBox, me.cvarType);
100         return needsForcing;
101 }
102 void XonoticCvarList_setSelected(entity me, float i)
103 {
104         string s;
105
106         SUPER(XonoticCvarList).setSelected(me, i);
107         if (me.nItems == 0) {
108                 return;
109         }
110
111         if (me.cvarName) {
112                 strunzone(me.cvarName);
113         }
114         if (me.cvarDescription) {
115                 strunzone(me.cvarDescription);
116         }
117         if (me.cvarType) {
118                 strunzone(me.cvarType);
119         }
120         if (me.cvarDefault) {
121                 strunzone(me.cvarDefault);
122         }
123         me.cvarName = strzone(bufstr_get(me.handle, me.selectedItem));
124         me.cvarDescription = strzone(cvar_description(me.cvarName));
125         me.cvarDefault = strzone(cvar_defstring(me.cvarName));
126         me.cvarNameBox.setText(me.cvarNameBox, me.cvarName);
127         me.cvarDescriptionBox.setText(me.cvarDescriptionBox, me.cvarDescription);
128         float needsForcing = me.updateCvarType(me);
129         me.cvarDefaultBox.setText(me.cvarDefaultBox, me.cvarDefault);
130
131         // this one can handle tempstrings
132         s = cvar_string(me.cvarName);
133         me.cvarNeedsForcing = 0;
134         me.cvarValueBox.setText(me.cvarValueBox, s);
135         me.cvarNeedsForcing = needsForcing;
136         me.cvarValueBox.cursorPos = strlen(s);
137 }
138 void CvarList_Filter_Change(entity box, entity me)
139 {
140         CvarList_Load(me, box.text);
141         me.setSelected(me, 0);
142 }
143 void CvarList_Filter_ModifiedCvars(entity box, entity me)
144 {
145         cvar_set("menu_cvarlist_onlymodified", ftos(!autocvar_menu_cvarlist_onlymodified));
146         box.setChecked(box, autocvar_menu_cvarlist_onlymodified);
147         CvarList_Load(me, me.controlledTextbox.text);
148         me.setSelected(me, 0);
149 }
150 void XonoticCvarList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
151 {
152         SUPER(XonoticCvarList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
153
154         me.realFontSize_y = me.fontSize / (absSize.y * me.itemHeight);
155         me.realFontSize_x = me.fontSize / (absSize.x * (1 - me.controlWidth));
156         me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
157
158         me.columnNameOrigin = 0;
159         me.columnValueSize = me.realFontSize.x * 20;
160         me.columnNameSize = 1 - me.columnValueSize - me.realFontSize.x;
161         me.columnValueOrigin = me.columnNameOrigin + me.columnNameSize + me.realFontSize.x;
162 }
163 void XonoticCvarList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
164 {
165         string k, v, d;
166         float t;
167
168         vector theColor;
169         float theAlpha;
170
171         string s;
172
173         if (isSelected) {
174                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
175         } else if (isFocused) {
176                 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
177                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
178         }
179
180         k = bufstr_get(me.handle, i);
181
182         v = cvar_string(k);
183         d = cvar_defstring(k);
184         t = cvar_type(k);
185         if (strhasword(autocvar_menu_forced_saved_cvars, k)) {
186                 theAlpha = SKINALPHA_CVARLIST_SAVED;
187         } else if (strhasword(autocvar_menu_reverted_nonsaved_cvars, k)) {
188                 theAlpha = SKINALPHA_CVARLIST_TEMPORARY;
189         } else if (t & CVAR_TYPEFLAG_SAVED) {
190                 theAlpha = SKINALPHA_CVARLIST_SAVED;
191         } else {
192                 theAlpha = SKINALPHA_CVARLIST_TEMPORARY;
193         }
194         if (v == d) {
195                 theColor = SKINCOLOR_CVARLIST_UNCHANGED;
196         } else {
197                 theColor = SKINCOLOR_CVARLIST_CHANGED;
198         }
199
200         s = draw_TextShortenToWidth(k, me.columnNameSize, 0, me.realFontSize);
201         draw_Text(me.realUpperMargin * eY + me.columnNameOrigin * eX, s, me.realFontSize, theColor, theAlpha, 0);
202         s = draw_TextShortenToWidth(v, me.columnValueSize, 0, me.realFontSize);
203         draw_Text(me.realUpperMargin * eY + me.columnValueOrigin * eX, s, me.realFontSize, theColor, theAlpha, 0);
204 }
205
206 float XonoticCvarList_keyDown(entity me, float scan, float ascii, float shift)
207 {
208         if (scan == K_MOUSE3 || ((shift & S_CTRL) && scan == K_SPACE)) {
209                 CvarList_Revert_Click(NULL, me);
210                 return 1;
211         } else if (scan == K_ENTER) {
212                 me.cvarValueBox.parent.setFocus(me.cvarValueBox.parent, me.cvarValueBox);
213                 return 1;
214         } else if (SUPER(XonoticCvarList).keyDown(me, scan, ascii, shift)) {
215                 return 1;
216         } else if (!me.controlledTextbox) {
217                 return 0;
218         } else {
219                 return me.controlledTextbox.keyDown(me.controlledTextbox, scan, ascii, shift);
220         }
221 }
222
223 float XonoticCvarList_mouseRelease(entity me, vector pos)
224 {
225         if (me.pressed == 2) {
226                 me.cvarValueBox.parent.setFocus(me.cvarValueBox.parent, me.cvarValueBox);
227         }
228         return SUPER(XonoticCvarList).mouseRelease(me, pos);
229 }
230
231 void CvarList_Value_Change(entity box, entity me)
232 {
233         cvar_set(me.cvarNameBox.text, box.text);
234         if (me.cvarNeedsForcing) {
235                 localcmd(sprintf("\nseta %1$s \"$%1$s\"\n", me.cvarName));
236                 cvar_set("menu_reverted_nonsaved_cvars", substring(strreplace(strcat(" ", me.cvarName, " "), " ", strcat(" ", autocvar_menu_reverted_nonsaved_cvars, " ")), 1, -2));
237                 if (autocvar_menu_forced_saved_cvars == "") {
238                         cvar_set("menu_forced_saved_cvars", me.cvarName);
239                 } else {
240                         cvar_set("menu_forced_saved_cvars", strcat(autocvar_menu_forced_saved_cvars, " ", me.cvarName));
241                 }
242                 me.cvarNeedsForcing = 0;
243                 me.updateCvarType(me);
244         }
245 }
246
247 void CvarList_Revert_Click(entity btn, entity me)
248 {
249         me.cvarValueBox.setText(me.cvarValueBox, me.cvarDefault);
250         me.cvarValueBox.cursorPos = strlen(me.cvarDefault);
251         if (strhasword(autocvar_menu_forced_saved_cvars, me.cvarName)) {
252                 cvar_set("menu_forced_saved_cvars", substring(strreplace(strcat(" ", me.cvarName, " "), " ", strcat(" ", autocvar_menu_forced_saved_cvars, " ")), 1, -2));
253                 if (autocvar_menu_reverted_nonsaved_cvars == "") {
254                         cvar_set("menu_reverted_nonsaved_cvars", me.cvarName);
255                 } else {
256                         cvar_set("menu_reverted_nonsaved_cvars", strcat(autocvar_menu_reverted_nonsaved_cvars, " ", me.cvarName));
257                 }
258         }
259         me.cvarNeedsForcing = me.updateCvarType(me);
260 }
261
262 void CvarList_End_Editing(entity box, entity me)
263 {
264         box.parent.setFocus(box.parent, me);
265 }