]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/nexuiz/cvarlist.c
initial checkin from nexuiz svn r8756
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / nexuiz / cvarlist.c
1 #ifdef INTERFACE
2 CLASS(NexuizCvarList) EXTENDS(NexuizListBox)
3         METHOD(NexuizCvarList, configureNexuizCvarList, void(entity))
4         ATTRIB(NexuizCvarList, rowsPerItem, float, 1)
5         METHOD(NexuizCvarList, drawListBoxItem, void(entity, float, vector, float))
6         METHOD(NexuizCvarList, resizeNotify, void(entity, vector, vector, vector, vector))
7         METHOD(NexuizCvarList, keyDown, float(entity, float, float, float))
8
9         METHOD(NexuizCvarList, destroy, void(entity))
10
11         ATTRIB(NexuizCvarList, realFontSize, vector, '0 0 0')
12         ATTRIB(NexuizCvarList, realUpperMargin, float, 0)
13         ATTRIB(NexuizCvarList, columnNameOrigin, float, 0)
14         ATTRIB(NexuizCvarList, columnNameSize, float, 0)
15         ATTRIB(NexuizCvarList, columnValueOrigin, float, 0)
16         ATTRIB(NexuizCvarList, columnValueSize, float, 0)
17
18         METHOD(NexuizCvarList, setSelected, void(entity, float))
19         ATTRIB(NexuizCvarList, controlledTextbox, entity, NULL)
20         ATTRIB(NexuizCvarList, cvarNameBox, entity, NULL)
21         ATTRIB(NexuizCvarList, cvarDescriptionBox, entity, NULL)
22         ATTRIB(NexuizCvarList, cvarTypeBox, entity, NULL)
23         ATTRIB(NexuizCvarList, cvarValueBox, entity, NULL)
24         ATTRIB(NexuizCvarList, cvarDefaultBox, entity, NULL)
25
26         ATTRIB(NexuizCvarList, handle, float, -1)
27         ATTRIB(NexuizCvarList, cvarName, string, string_null)
28         ATTRIB(NexuizCvarList, cvarDescription, string, string_null)
29         ATTRIB(NexuizCvarList, cvarType, string, string_null)
30         ATTRIB(NexuizCvarList, cvarDefault, string, string_null)
31 ENDCLASS(NexuizCvarList)
32 entity makeNexuizCvarList();
33 void CvarList_Filter_Change(entity box, entity me);
34 void CvarList_Value_Change(entity box, entity me);
35 void CvarList_Revert_Click(entity btn, entity me);
36 #endif
37
38 #ifdef IMPLEMENTATION
39 entity makeNexuizCvarList()
40 {
41         entity me;
42         me = spawnNexuizCvarList();
43         me.configureNexuizCvarList(me);
44         return me;
45 }
46 void configureNexuizCvarListNexuizCvarList(entity me)
47 {
48         me.configureNexuizListBox(me);
49
50         me.handle = buf_create();
51         buf_cvarlist(me.handle, "", "_");
52         me.nItems = buf_getsize(me.handle);
53 }
54 void destroyNexuizCvarList(entity me)
55 {
56         buf_del(me.handle);
57 }
58 void setSelectedNexuizCvarList(entity me, float i)
59 {
60         string s;
61
62         setSelectedListBox(me, i);
63         if(me.nItems == 0)
64                 return;
65         
66         if(me.cvarName)
67                 strunzone(me.cvarName);
68         if(me.cvarDescription)
69                 strunzone(me.cvarDescription);
70         if(me.cvarType)
71                 strunzone(me.cvarType);
72         if(me.cvarDefault)
73                 strunzone(me.cvarDefault);
74         me.cvarName = strzone(bufstr_get(me.handle, me.selectedItem));
75         me.cvarDescription = strzone(cvar_description(me.cvarName));
76         me.cvarDefault = strzone(cvar_defstring(me.cvarName));
77
78         float t;
79         t = cvar_type(me.cvarName);
80         me.cvarType = "";
81         if(t & CVAR_TYPEFLAG_SAVED)
82                 me.cvarType = strcat(me.cvarType, ", will be saved to config.cfg");
83         else
84                 me.cvarType = strcat(me.cvarType, ", will not be saved");
85         if(t & CVAR_TYPEFLAG_PRIVATE)
86                 me.cvarType = strcat(me.cvarType, ", private");
87         if(t & CVAR_TYPEFLAG_ENGINE)
88                 me.cvarType = strcat(me.cvarType, ", engine setting");
89         if(t & CVAR_TYPEFLAG_READONLY)
90                 me.cvarType = strcat(me.cvarType, ", read only");
91         me.cvarType = strzone(substring(me.cvarType, 2, strlen(me.cvarType) - 2));
92
93         me.cvarNameBox.setText(me.cvarNameBox, me.cvarName);
94         me.cvarDescriptionBox.setText(me.cvarDescriptionBox, me.cvarDescription);
95         me.cvarTypeBox.setText(me.cvarTypeBox, me.cvarType);
96         me.cvarDefaultBox.setText(me.cvarDefaultBox, me.cvarDefault);
97
98         // this one can handle tempstrings
99         s = cvar_string(me.cvarName);
100         me.cvarValueBox.setText(me.cvarValueBox, s);
101         me.cvarValueBox.cursorPos = strlen(s);
102 }
103 void CvarList_Filter_Change(entity box, entity me)
104 {
105         buf_cvarlist(me.handle, box.text, "_");
106         me.nItems = buf_getsize(me.handle);
107
108         me.setSelected(me, 0);
109 }
110 void resizeNotifyNexuizCvarList(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
111 {
112         resizeNotifyNexuizListBox(me, relOrigin, relSize, absOrigin, absSize);
113
114         me.realFontSize_y = me.fontSize / (absSize_y * me.itemHeight);
115         me.realFontSize_x = me.fontSize / (absSize_x * (1 - me.controlWidth));
116         me.realUpperMargin = 0.5 * (1 - me.realFontSize_y);
117
118         me.columnNameOrigin = 0;
119         me.columnValueSize = me.realFontSize_x * 20;
120         me.columnNameSize = 1 - me.columnValueSize - me.realFontSize_x;
121         me.columnValueOrigin = me.columnNameOrigin + me.columnNameSize + me.realFontSize_x;
122
123         me.setSelected(me, me.selectedItem);
124 }
125 void drawListBoxItemNexuizCvarList(entity me, float i, vector absSize, float isSelected)
126 {
127         string k, v, d;
128         float t;
129
130         vector theColor;
131         float theAlpha;
132
133         string s;
134
135         if(isSelected)
136                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
137         
138         k = bufstr_get(me.handle, i);
139
140         v = cvar_string(k);
141         d = cvar_defstring(k);
142         t = cvar_type(k);
143         if(t & CVAR_TYPEFLAG_SAVED)
144                 theAlpha = SKINALPHA_CVARLIST_SAVED;
145         else
146                 theAlpha = SKINALPHA_CVARLIST_TEMPORARY;
147         if(v == d)
148                 theColor = SKINCOLOR_CVARLIST_UNCHANGED;
149         else
150                 theColor = SKINCOLOR_CVARLIST_CHANGED;
151
152         s = draw_TextShortenToWidth(k, me.columnNameSize, 0, me.realFontSize);
153         draw_Text(me.realUpperMargin * eY + me.columnNameOrigin * eX, s, me.realFontSize, theColor, theAlpha, 0);
154         s = draw_TextShortenToWidth(v, me.columnValueSize, 0, me.realFontSize);
155         draw_Text(me.realUpperMargin * eY + me.columnValueOrigin * eX, s, me.realFontSize, theColor, theAlpha, 0);
156 }
157
158 float keyDownNexuizCvarList(entity me, float scan, float ascii, float shift)
159 {
160         if(keyDownListBox(me, scan, ascii, shift))
161                 return 1;
162         else if(!me.controlledTextbox)
163                 return 0;
164         else
165                 return me.controlledTextbox.keyDown(me.controlledTextbox, scan, ascii, shift);
166 }
167
168 void CvarList_Value_Change(entity box, entity me)
169 {
170         cvar_set(me.cvarNameBox.text, box.text);
171 }
172
173 void CvarList_Revert_Click(entity btn, entity me)
174 {
175         me.cvarValueBox.setText(me.cvarValueBox, me.cvarDefault);
176         me.cvarValueBox.cursorPos = strlen(me.cvarDefault);
177 }
178 #endif