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