]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/cvarlist.qc
Create cvar list when it is displayed rather then on menu start
[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 }
59 void XonoticCvarList_showNotify(entity me)
60 {
61         buf_cvarlist(me.handle, "", "_");
62         me.nItems = buf_getsize(me.handle);
63 }
64 void XonoticCvarList_destroy(entity me)
65 {
66         buf_del(me.handle);
67 }
68 string autocvar_menu_forced_saved_cvars;
69 string autocvar_menu_reverted_nonsaved_cvars;
70 float XonoticCvarList_updateCvarType(entity me)
71 {
72         float t;
73         t = cvar_type(me.cvarName);
74         me.cvarType = "";
75         float needsForcing;
76         if(strhasword(autocvar_menu_forced_saved_cvars, me.cvarName))
77         {
78                 me.cvarType = strcat(me.cvarType, ", ", _("forced to be saved to config.cfg"));
79                 needsForcing = 0;
80         }
81         else if(strhasword(autocvar_menu_reverted_nonsaved_cvars, me.cvarName))
82         {
83                 // Currently claims to be saved, but won't be on next startup.
84                 me.cvarType = strcat(me.cvarType, ", ", _("will not be saved"));
85                 needsForcing = 1;
86         }
87         else if(t & CVAR_TYPEFLAG_SAVED)
88         {
89                 me.cvarType = strcat(me.cvarType, ", ", _("will be saved to config.cfg"));
90                 needsForcing = 0;
91         }
92         else
93         {
94                 me.cvarType = strcat(me.cvarType, ", ", _("will not be saved"));
95                 needsForcing = 1;
96         }
97         if(t & CVAR_TYPEFLAG_PRIVATE)
98                 me.cvarType = strcat(me.cvarType, ", ", _("private"));
99         if(t & CVAR_TYPEFLAG_ENGINE)
100                 me.cvarType = strcat(me.cvarType, ", ", _("engine setting"));
101         if(t & CVAR_TYPEFLAG_READONLY)
102                 me.cvarType = strcat(me.cvarType, ", ", _("read only"));
103         me.cvarType = strzone(substring(me.cvarType, 2, strlen(me.cvarType) - 2));
104         me.cvarTypeBox.setText(me.cvarTypeBox, me.cvarType);
105         return needsForcing;
106 }
107 void XonoticCvarList_setSelected(entity me, float i)
108 {
109         string s;
110
111         SUPER(XonoticCvarList).setSelected(me, i);
112         if(me.nItems == 0)
113                 return;
114
115         if(me.cvarName)
116                 strunzone(me.cvarName);
117         if(me.cvarDescription)
118                 strunzone(me.cvarDescription);
119         if(me.cvarType)
120                 strunzone(me.cvarType);
121         if(me.cvarDefault)
122                 strunzone(me.cvarDefault);
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         buf_cvarlist(me.handle, box.text, "_");
141         me.nItems = buf_getsize(me.handle);
142
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         me.setSelected(me, me.selectedItem);
159 }
160 void XonoticCvarList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
161 {
162         string k, v, d;
163         float t;
164
165         vector theColor;
166         float theAlpha;
167
168         string s;
169
170         if(isSelected)
171                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
172         else if(isFocused)
173         {
174                 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
175                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
176         }
177
178         k = bufstr_get(me.handle, i);
179
180         v = cvar_string(k);
181         d = cvar_defstring(k);
182         t = cvar_type(k);
183         if(strhasword(autocvar_menu_forced_saved_cvars, k))
184                 theAlpha = SKINALPHA_CVARLIST_SAVED;
185         else if(strhasword(autocvar_menu_reverted_nonsaved_cvars, k))
186                 theAlpha = SKINALPHA_CVARLIST_TEMPORARY;
187         else if(t & CVAR_TYPEFLAG_SAVED)
188                 theAlpha = SKINALPHA_CVARLIST_SAVED;
189         else
190                 theAlpha = SKINALPHA_CVARLIST_TEMPORARY;
191         if(v == d)
192                 theColor = SKINCOLOR_CVARLIST_UNCHANGED;
193         else
194                 theColor = SKINCOLOR_CVARLIST_CHANGED;
195
196         s = draw_TextShortenToWidth(k, me.columnNameSize, 0, me.realFontSize);
197         draw_Text(me.realUpperMargin * eY + me.columnNameOrigin * eX, s, me.realFontSize, theColor, theAlpha, 0);
198         s = draw_TextShortenToWidth(v, me.columnValueSize, 0, me.realFontSize);
199         draw_Text(me.realUpperMargin * eY + me.columnValueOrigin * eX, s, me.realFontSize, theColor, theAlpha, 0);
200 }
201
202 float XonoticCvarList_keyDown(entity me, float scan, float ascii, float shift)
203 {
204         if (scan == K_MOUSE3 || ((shift & S_CTRL) && scan == K_SPACE))
205         {
206                 CvarList_Revert_Click(NULL, me);
207                 return 1;
208         }
209         else if(scan == K_ENTER)
210         {
211                 me.cvarValueBox.parent.setFocus(me.cvarValueBox.parent, me.cvarValueBox);
212                 return 1;
213         }
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 float XonoticCvarList_mouseRelease(entity me, vector pos)
223 {
224         if(me.pressed == 2)
225                 me.cvarValueBox.parent.setFocus(me.cvarValueBox.parent, me.cvarValueBox);
226         return SUPER(XonoticCvarList).mouseRelease(me, pos);
227 }
228
229 void CvarList_Value_Change(entity box, entity me)
230 {
231         cvar_set(me.cvarNameBox.text, box.text);
232         if(me.cvarNeedsForcing)
233         {
234                 localcmd(sprintf("\nseta %1$s \"$%1$s\"\n", me.cvarName));
235                 cvar_set("menu_reverted_nonsaved_cvars", substring(strreplace(strcat(" ", me.cvarName, " "), " ", strcat(" ", autocvar_menu_reverted_nonsaved_cvars, " ")), 1, -2));
236                 if (autocvar_menu_forced_saved_cvars == "")
237                         cvar_set("menu_forced_saved_cvars", me.cvarName);
238                 else
239                         cvar_set("menu_forced_saved_cvars", strcat(autocvar_menu_forced_saved_cvars, " ", me.cvarName));
240                 me.cvarNeedsForcing = 0;
241                 me.updateCvarType(me);
242         }
243 }
244
245 void CvarList_Revert_Click(entity btn, entity me)
246 {
247         me.cvarValueBox.setText(me.cvarValueBox, me.cvarDefault);
248         me.cvarValueBox.cursorPos = strlen(me.cvarDefault);
249         if(strhasword(autocvar_menu_forced_saved_cvars, me.cvarName))
250         {
251                 cvar_set("menu_forced_saved_cvars", substring(strreplace(strcat(" ", me.cvarName, " "), " ", strcat(" ", autocvar_menu_forced_saved_cvars, " ")), 1, -2));
252                 if (autocvar_menu_reverted_nonsaved_cvars == "")
253                         cvar_set("menu_reverted_nonsaved_cvars", me.cvarName);
254                 else
255                         cvar_set("menu_reverted_nonsaved_cvars", strcat(autocvar_menu_reverted_nonsaved_cvars, " ", me.cvarName));
256         }
257         me.cvarNeedsForcing = me.updateCvarType(me);
258 }
259
260 void CvarList_End_Editing(entity box, entity me)
261 {
262         box.parent.setFocus(box.parent, me);
263 }
264
265 #endif