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