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