]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/hudskinlist.qc
736ee0a6edbdd4ad1f920c0df176193076c64767
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / hudskinlist.qc
1 #ifndef HUDSKINLIST_H
2 #define HUDSKINLIST_H
3 #include "listbox.qc"
4 CLASS(XonoticHUDSkinList, XonoticListBox)
5         METHOD(XonoticHUDSkinList, configureXonoticHUDSkinList, void(entity));
6         ATTRIB(XonoticHUDSkinList, rowsPerItem, float, 1)
7         METHOD(XonoticHUDSkinList, resizeNotify, void(entity, vector, vector, vector, vector));
8         METHOD(XonoticHUDSkinList, draw, void(entity));
9         METHOD(XonoticHUDSkinList, drawListBoxItem, void(entity, int, vector, bool, bool));
10         METHOD(XonoticHUDSkinList, getHUDSkins, void(entity));
11         METHOD(XonoticHUDSkinList, setHUDSkin, void(entity));
12         METHOD(XonoticHUDSkinList, hudskinName, string(entity, float));
13         METHOD(XonoticHUDSkinList, hudskinPath, string(entity, float));
14         METHOD(XonoticHUDSkinList, hudskinTitle, string(entity, float));
15         METHOD(XonoticHUDSkinList, hudskinAuthor, string(entity, float));
16         METHOD(XonoticHUDSkinList, doubleClickListBoxItem, void(entity, float, vector));
17         METHOD(XonoticHUDSkinList, keyDown, float(entity, float, float, float));
18         METHOD(XonoticHUDSkinList, destroy, void(entity));
19         METHOD(XonoticHUDSkinList, showNotify, void(entity));
20
21         ATTRIB(XonoticHUDSkinList, listHUDSkin, float, -1)
22         ATTRIB(XonoticHUDSkinList, realFontSize, vector, '0 0 0')
23         ATTRIB(XonoticHUDSkinList, columnNameOrigin, float, 0)
24         ATTRIB(XonoticHUDSkinList, columnNameSize, float, 0)
25         ATTRIB(XonoticHUDSkinList, realUpperMargin, float, 0)
26         ATTRIB(XonoticHUDSkinList, origin, vector, '0 0 0')
27         ATTRIB(XonoticHUDSkinList, itemAbsSize, vector, '0 0 0')
28
29         ATTRIB(XonoticHUDSkinList, filterString, string, string_null)
30         ATTRIB(XonoticHUDSkinList, delayedRefreshTime, float, 0)
31         ATTRIB(XonoticHUDSkinList, savedName, string, string_null)
32 ENDCLASS(XonoticHUDSkinList)
33
34 #ifndef IMPLEMENTATION
35 // public:
36 entity hudskinlist;
37 entity makeXonoticHUDSkinList();
38 void SaveHUDSkin_Click(entity btn, entity me);
39 void SetHUDSkin_Click(entity btn, entity me);
40 #endif
41 void HUDSkinList_Refresh_Click(entity btn, entity me);
42 void HUDSkinList_Filter_Change(entity box, entity me);
43 void HUDSkinList_SavedName_Change(entity box, entity me);
44 #endif
45
46 #ifdef IMPLEMENTATION
47
48 entity makeXonoticHUDSkinList()
49 {
50         entity me;
51         me = NEW(XonoticHUDSkinList);
52         me.configureXonoticHUDSkinList(me);
53         return me;
54 }
55
56 void XonoticHUDSkinList_configureXonoticHUDSkinList(entity me)
57 {
58         me.configureXonoticListBox(me);
59         me.getHUDSkins(me);
60 }
61
62 const float HUDSKINPARM_NAME = 0;
63 const float HUDSKINPARM_PATH = 1;
64 const float HUDSKINPARM_TITLE = 2;
65 const float HUDSKINPARM_AUTHOR = 3;
66 const float HUDSKINPARM_COUNT = 4;
67 string XonoticHUDSkinList_hudskinName(entity me, float i)
68 {
69         return bufstr_get(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_NAME);
70 }
71 string XonoticHUDSkinList_hudskinPath(entity me, float i)
72 {
73         return bufstr_get(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_PATH);
74 }
75 string XonoticHUDSkinList_hudskinTitle(entity me, float i)
76 {
77         return bufstr_get(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_TITLE);
78 }
79 string XonoticHUDSkinList_hudskinAuthor(entity me, float i)
80 {
81         return bufstr_get(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_AUTHOR);
82 }
83
84 // subdir can be a regular expression
85 void getHUDSkinFiles(entity me, int sortbuf, string subdir)
86 {
87         string s;
88         if(me.filterString)
89                 s = me.filterString;
90         else
91                 s = "*";
92         s = strcat(subdir, "hud_", s, ".cfg");
93
94         int list = search_begin(s, false, true);
95         if(list >= 0)
96         {
97                 int n = search_getsize(list);
98                 for(int i = 0; i < n; ++i)
99                 {
100                         string s = search_getfilename(list, i);
101                         int subdir_ofs = strstrofs(s, "/", 0);
102                         if(subdir_ofs >= 0)
103                         {
104                                 int ofs = subdir_ofs;
105                                 while(ofs != -1)
106                                 {
107                                         subdir_ofs = ofs;
108                                         ofs = strstrofs(s, "/", subdir_ofs + 1);
109                                 }
110                         }
111
112                         if(subdir_ofs == -1)
113                                 bufstr_add(sortbuf, s, true);
114                         else
115                         {
116                                 subdir = substring(s, 0, subdir_ofs);
117                                 string filename = substring(s, subdir_ofs + 1, -1);
118                                 // invert path and filename position so we can sort sortbuf by filename
119                                 bufstr_add(sortbuf, strcat(filename, "/", subdir), true);
120                         }
121                 }
122                 search_end(list);
123         }
124 }
125
126 void getAllHUDSkins(entity me, int sortbuf)
127 {
128         int n = buf_getsize(sortbuf);
129         for(int i = 0; i < n; ++i)
130         {
131                 string entry = bufstr_get(sortbuf, i);
132                 int ofs = strstrofs(entry, "/", 0);
133                 string s = "";
134                 string filename = entry;
135                 if(ofs >= 0)
136                 {
137                         s = substring(entry, ofs + 1, -1); // skip initial "/"
138                         s = strcat(s, "/");
139                         bufstr_set(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_PATH, s);
140                         filename = strcat(s, substring(entry, 0, ofs));
141                 }
142                 else
143                         ofs = strlen(entry);
144                 s = substring(entry, 4, ofs - 4 - 4); // remove "hud_" prefix and ".cfg" suffix
145                 bufstr_set(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_NAME, s);
146
147                 int fh = fopen(filename, FILE_READ);
148                 if(fh < 0)
149                         continue;
150                 while((s = fgets(fh)) && substring(s, 0, 2) == "//")
151                 {
152                         tokenize_console(substring(s, 2, -1));
153                         if(argv(0) == "title")
154                                 bufstr_set(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_TITLE, argv(1));
155                         else if(argv(0) == "author")
156                                 bufstr_set(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_AUTHOR, argv(1));
157                 }
158                 fclose(fh);
159         }
160 }
161
162 void XonoticHUDSkinList_getHUDSkins(entity me)
163 {
164         if (me.listHUDSkin >= 0)
165                 buf_del(me.listHUDSkin);
166         me.listHUDSkin = buf_create();
167         if (me.listHUDSkin < 0)
168         {
169                 me.nItems = 0;
170                 return;
171         }
172         int sortbuf = buf_create();
173         getHUDSkinFiles(me, sortbuf, "");
174         getHUDSkinFiles(me, sortbuf, "data/");
175         buf_sort(sortbuf, 128, 0);
176         getAllHUDSkins(me, sortbuf);
177         buf_del(sortbuf);
178         me.nItems = buf_getsize(me.listHUDSkin) / HUDSKINPARM_COUNT;
179 }
180
181 void XonoticHUDSkinList_destroy(entity me)
182 {
183         if(me.nItems > 0)
184                 buf_del(me.listHUDSkin);
185 }
186
187 void XonoticHUDSkinList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
188 {
189         me.itemAbsSize = '0 0 0';
190         SUPER(XonoticHUDSkinList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
191
192         me.realFontSize_y = me.fontSize / (me.itemAbsSize_y = (absSize.y * me.itemHeight));
193         me.realFontSize_x = me.fontSize / (me.itemAbsSize_x = (absSize.x * (1 - me.controlWidth)));
194         me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
195
196         me.columnNameOrigin = me.realFontSize.x;
197         me.columnNameSize = 1 - 2 * me.realFontSize.x;
198 }
199
200 void XonoticHUDSkinList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
201 {
202         string s, s2;
203         if(isSelected)
204                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
205         else if(isFocused)
206         {
207                 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
208                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
209         }
210
211         s = me.hudskinTitle(me, i);
212         if(s == "")
213                 s = me.hudskinName(me, i);
214         s2 = me.hudskinAuthor(me, i);
215         if(s2 != "")
216                 s = strcat(s, " (", s2, ")");
217         s = draw_TextShortenToWidth(s, me.columnNameSize, 0, me.realFontSize);
218         draw_Text(me.realUpperMargin * eY + (me.columnNameOrigin + 0.00 * (me.columnNameSize - draw_TextWidth(s, 0, me.realFontSize))) * eX, s, me.realFontSize, SKINCOLOR_TEXT, SKINALPHA_TEXT, 1);
219 }
220
221 void XonoticHUDSkinList_showNotify(entity me)
222 {
223         me.getHUDSkins(me);
224 }
225
226 void HUDSkinList_Refresh_Click(entity btn, entity me)
227 {
228         me.getHUDSkins(me);
229         me.setSelected(me, 0); //always select the first element after a list update
230 }
231
232 void HUDSkinList_SavedName_Change(entity box, entity me)
233 {
234         if(me.savedName)
235                 strunzone(me.savedName);
236
237         if(box.text != "")
238                 me.savedName = strzone(box.text);
239         else
240                 me.savedName = string_null;
241 }
242
243 void HUDSkinList_Filter_Change(entity box, entity me)
244 {
245         if(me.filterString)
246                 strunzone(me.filterString);
247
248         if(box.text != "")
249         {
250                 if (strstrofs(box.text, "*", 0) >= 0 || strstrofs(box.text, "?", 0) >= 0)
251                         me.filterString = strzone(box.text);
252                 else
253                         me.filterString = strzone(strcat("*", box.text, "*"));
254         }
255         else
256                 me.filterString = string_null;
257
258         me.getHUDSkins(me);
259 }
260
261 void SaveHUDSkin_Click(entity btn, entity me)
262 {
263         string s = me.savedName;
264         if(s == "")
265                 s = "myconfig";
266         localcmd(sprintf("hud save \"%s\"\n", s));
267         me.delayedRefreshTime = time + 1;
268 }
269
270 void XonoticHUDSkinList_draw(entity me)
271 {
272         if(me.delayedRefreshTime > 0 && me.delayedRefreshTime < time)
273         {
274                 HUDSkinList_Refresh_Click(NULL, me);
275                 me.delayedRefreshTime = 0;
276         }
277         SUPER(XonoticHUDSkinList).draw(me);
278 }
279
280 void XonoticHUDSkinList_setHUDSkin(entity me)
281 {
282         string cfg = strcat(me.hudskinPath(me, me.selectedItem), "hud_", me.hudskinName(me, me.selectedItem), ".cfg");
283         localcmd("exec \"", cfg, "\"\n");
284 }
285
286 void SetHUDSkin_Click(entity btn, entity me)
287 {
288         me.setHUDSkin(me);
289 }
290
291 void XonoticHUDSkinList_doubleClickListBoxItem(entity me, float i, vector where)
292 {
293         m_play_click_sound(MENU_SOUND_EXECUTE);
294         me.setHUDSkin(me);
295 }
296
297 float XonoticHUDSkinList_keyDown(entity me, float scan, float ascii, float shift)
298 {
299         if(scan == K_ENTER || scan == K_KP_ENTER)
300         {
301                 me.setHUDSkin(me);
302                 return 1;
303         }
304         else
305         {
306                 return SUPER(XonoticHUDSkinList).keyDown(me, scan, ascii, shift);
307         }
308 }
309 #endif
310