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