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