]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/cvarlist.c
722917f553c8e7876f03ee0a0b4b0799f1bd4890
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / cvarlist.c
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
21         ATTRIB(XonoticCvarList, controlledTextbox, entity, NULL)
22         ATTRIB(XonoticCvarList, cvarNameBox, entity, NULL)
23         ATTRIB(XonoticCvarList, cvarDescriptionBox, entity, NULL)
24         ATTRIB(XonoticCvarList, cvarTypeBox, entity, NULL)
25         ATTRIB(XonoticCvarList, cvarValueBox, entity, NULL)
26         ATTRIB(XonoticCvarList, cvarDefaultBox, entity, NULL)
27
28         ATTRIB(XonoticCvarList, handle, float, -1)
29         ATTRIB(XonoticCvarList, cvarName, string, string_null)
30         ATTRIB(XonoticCvarList, cvarDescription, string, string_null)
31         ATTRIB(XonoticCvarList, cvarType, string, string_null)
32         ATTRIB(XonoticCvarList, cvarDefault, string, string_null)
33 ENDCLASS(XonoticCvarList)
34 entity makeXonoticCvarList();
35 void CvarList_Filter_Change(entity box, entity me);
36 void CvarList_Value_Change(entity box, entity me);
37 void CvarList_Revert_Click(entity btn, entity me);
38 void CvarList_End_Editing(entity box, entity me);
39 #endif
40
41 #ifdef IMPLEMENTATION
42 entity makeXonoticCvarList()
43 {
44         entity me;
45         me = spawnXonoticCvarList();
46         me.configureXonoticCvarList(me);
47         return me;
48 }
49 void XonoticCvarList_configureXonoticCvarList(entity me)
50 {
51         me.configureXonoticListBox(me);
52
53         me.handle = buf_create();
54         buf_cvarlist(me.handle, "", "_");
55         me.nItems = buf_getsize(me.handle);
56 }
57 void XonoticCvarList_destroy(entity me)
58 {
59         buf_del(me.handle);
60 }
61 void XonoticCvarList_setSelected(entity me, float i)
62 {
63         string s;
64
65         SUPER(XonoticCvarList).setSelected(me, i);
66         if(me.nItems == 0)
67                 return;
68
69         if(me.cvarName)
70                 strunzone(me.cvarName);
71         if(me.cvarDescription)
72                 strunzone(me.cvarDescription);
73         if(me.cvarType)
74                 strunzone(me.cvarType);
75         if(me.cvarDefault)
76                 strunzone(me.cvarDefault);
77         me.cvarName = strzone(bufstr_get(me.handle, me.selectedItem));
78         me.cvarDescription = strzone(cvar_description(me.cvarName));
79         me.cvarDefault = strzone(cvar_defstring(me.cvarName));
80
81         float t;
82         t = cvar_type(me.cvarName);
83         me.cvarType = "";
84         if(t & CVAR_TYPEFLAG_SAVED)
85                 me.cvarType = strcat(me.cvarType, ", ", _("will be saved to config.cfg"));
86         else
87                 me.cvarType = strcat(me.cvarType, ", ", _("will not be saved"));
88         if(t & CVAR_TYPEFLAG_PRIVATE)
89                 me.cvarType = strcat(me.cvarType, ", ", _("private"));
90         if(t & CVAR_TYPEFLAG_ENGINE)
91                 me.cvarType = strcat(me.cvarType, ", ", _("engine setting"));
92         if(t & CVAR_TYPEFLAG_READONLY)
93                 me.cvarType = strcat(me.cvarType, ", ", _("read only"));
94         me.cvarType = strzone(substring(me.cvarType, 2, strlen(me.cvarType) - 2));
95
96         me.cvarNameBox.setText(me.cvarNameBox, me.cvarName);
97         me.cvarDescriptionBox.setText(me.cvarDescriptionBox, me.cvarDescription);
98         me.cvarTypeBox.setText(me.cvarTypeBox, me.cvarType);
99         me.cvarDefaultBox.setText(me.cvarDefaultBox, me.cvarDefault);
100
101         // this one can handle tempstrings
102         s = cvar_string(me.cvarName);
103         me.cvarValueBox.setText(me.cvarValueBox, s);
104         me.cvarValueBox.cursorPos = strlen(s);
105 }
106 void CvarList_Filter_Change(entity box, entity me)
107 {
108         buf_cvarlist(me.handle, box.text, "_");
109         me.nItems = buf_getsize(me.handle);
110
111         me.setSelected(me, 0);
112 }
113 void XonoticCvarList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
114 {
115         SUPER(XonoticCvarList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
116
117         me.realFontSize_y = me.fontSize / (absSize_y * me.itemHeight);
118         me.realFontSize_x = me.fontSize / (absSize_x * (1 - me.controlWidth));
119         me.realUpperMargin = 0.5 * (1 - me.realFontSize_y);
120
121         me.columnNameOrigin = 0;
122         me.columnValueSize = me.realFontSize_x * 20;
123         me.columnNameSize = 1 - me.columnValueSize - me.realFontSize_x;
124         me.columnValueOrigin = me.columnNameOrigin + me.columnNameSize + me.realFontSize_x;
125
126         me.setSelected(me, me.selectedItem);
127 }
128 void XonoticCvarList_drawListBoxItem(entity me, float i, vector absSize, float isSelected)
129 {
130         string k, v, d;
131         float t;
132
133         vector theColor;
134         float theAlpha;
135
136         string s;
137
138         if(isSelected)
139                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
140
141         k = bufstr_get(me.handle, i);
142
143         v = cvar_string(k);
144         d = cvar_defstring(k);
145         t = cvar_type(k);
146         if(t & CVAR_TYPEFLAG_SAVED)
147                 theAlpha = SKINALPHA_CVARLIST_SAVED;
148         else
149                 theAlpha = SKINALPHA_CVARLIST_TEMPORARY;
150         if(v == d)
151                 theColor = SKINCOLOR_CVARLIST_UNCHANGED;
152         else
153                 theColor = SKINCOLOR_CVARLIST_CHANGED;
154
155         s = draw_TextShortenToWidth(k, me.columnNameSize, 0, me.realFontSize);
156         draw_Text(me.realUpperMargin * eY + me.columnNameOrigin * eX, s, me.realFontSize, theColor, theAlpha, 0);
157         s = draw_TextShortenToWidth(v, me.columnValueSize, 0, me.realFontSize);
158         draw_Text(me.realUpperMargin * eY + me.columnValueOrigin * eX, s, me.realFontSize, theColor, theAlpha, 0);
159 }
160
161 float XonoticCvarList_keyDown(entity me, float scan, float ascii, float shift)
162 {
163         if (scan == K_MOUSE3 || ((shift & S_CTRL) && scan == K_SPACE))
164         {
165                 CvarList_Revert_Click(world, me);
166                 return 1;
167         }
168         else if(scan == K_ENTER)
169         {
170                 me.cvarValueBox.parent.setFocus(me.cvarValueBox.parent, me.cvarValueBox);
171                 return 1;
172         }
173         else if(SUPER(XonoticCvarList).keyDown(me, scan, ascii, shift))
174                 return 1;
175         else if(!me.controlledTextbox)
176                 return 0;
177         else
178                 return me.controlledTextbox.keyDown(me.controlledTextbox, scan, ascii, shift);
179 }
180
181 float XonoticCvarList_mouseRelease(entity me, vector pos)
182 {
183         if(me.pressed == 2)
184                 me.cvarValueBox.parent.setFocus(me.cvarValueBox.parent, me.cvarValueBox);
185         return SUPER(XonoticCvarList).mouseRelease(me, pos);
186 }
187
188 void CvarList_Value_Change(entity box, entity me)
189 {
190         cvar_set(me.cvarNameBox.text, box.text);
191 }
192
193 void CvarList_Revert_Click(entity btn, entity me)
194 {
195         me.cvarValueBox.setText(me.cvarValueBox, me.cvarDefault);
196         me.cvarValueBox.cursorPos = strlen(me.cvarDefault);
197 }
198
199 void CvarList_End_Editing(entity box, entity me)
200 {
201         box.parent.setFocus(box.parent, me);
202 }
203
204 #endif