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