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));
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')
29 ATTRIB(XonoticHUDSkinList, filterString, string, string_null)
30 ATTRIB(XonoticHUDSkinList, delayedRefreshTime, float, 0)
31 ATTRIB(XonoticHUDSkinList, savedName, string, string_null)
32 ENDCLASS(XonoticHUDSkinList)
34 #ifndef IMPLEMENTATION
37 entity makeXonoticHUDSkinList();
38 void SaveHUDSkin_Click(entity btn, entity me);
39 void SetHUDSkin_Click(entity btn, entity me);
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);
48 entity makeXonoticHUDSkinList()
51 me = NEW(XonoticHUDSkinList);
52 me.configureXonoticHUDSkinList(me);
56 void XonoticHUDSkinList_configureXonoticHUDSkinList(entity me)
58 me.configureXonoticListBox(me);
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)
69 return bufstr_get(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_NAME);
71 string XonoticHUDSkinList_hudskinPath(entity me, float i)
73 return bufstr_get(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_PATH);
75 string XonoticHUDSkinList_hudskinTitle(entity me, float i)
77 return bufstr_get(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_TITLE);
79 string XonoticHUDSkinList_hudskinAuthor(entity me, float i)
81 return bufstr_get(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_AUTHOR);
84 // subdir can be a regular expression
85 void getHUDSkinFiles(entity me, int sortbuf, string subdir)
92 s = strcat(subdir, "hud_", s, ".cfg");
94 int list = search_begin(s, false, true);
97 int n = search_getsize(list);
98 for(int i = 0; i < n; ++i)
100 string s = search_getfilename(list, i);
101 int subdir_ofs = strstrofs(s, "/", 0);
104 int ofs = subdir_ofs;
108 ofs = strstrofs(s, "/", subdir_ofs + 1);
113 bufstr_add(sortbuf, s, true);
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);
126 void getAllHUDSkins(entity me, int sortbuf)
128 int n = buf_getsize(sortbuf);
129 for(int i = 0; i < n; ++i)
131 string entry = bufstr_get(sortbuf, i);
132 int ofs = strstrofs(entry, "/", 0);
134 string filename = entry;
137 s = substring(entry, ofs + 1, -1); // skip initial "/"
139 bufstr_set(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_PATH, s);
140 filename = strcat(s, substring(entry, 0, ofs));
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);
147 int fh = fopen(filename, FILE_READ);
150 while((s = fgets(fh)) && substring(s, 0, 2) == "//")
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));
162 void XonoticHUDSkinList_getHUDSkins(entity me)
164 if (me.listHUDSkin >= 0)
165 buf_del(me.listHUDSkin);
166 me.listHUDSkin = buf_create();
167 if (me.listHUDSkin < 0)
172 int sortbuf = buf_create();
173 getHUDSkinFiles(me, sortbuf, "");
174 getHUDSkinFiles(me, sortbuf, "data/");
175 buf_sort(sortbuf, 128, 0);
176 getAllHUDSkins(me, sortbuf);
178 me.nItems = buf_getsize(me.listHUDSkin) / HUDSKINPARM_COUNT;
181 void XonoticHUDSkinList_destroy(entity me)
184 buf_del(me.listHUDSkin);
187 void XonoticHUDSkinList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
189 me.itemAbsSize = '0 0 0';
190 SUPER(XonoticHUDSkinList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
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);
196 me.columnNameOrigin = me.realFontSize.x;
197 me.columnNameSize = 1 - 2 * me.realFontSize.x;
200 void XonoticHUDSkinList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
204 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
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);
211 s = me.hudskinTitle(me, i);
213 s = me.hudskinName(me, i);
214 s2 = me.hudskinAuthor(me, i);
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);
221 void XonoticHUDSkinList_showNotify(entity me)
226 void HUDSkinList_Refresh_Click(entity btn, entity me)
229 me.setSelected(me, 0); //always select the first element after a list update
232 void HUDSkinList_SavedName_Change(entity box, entity me)
235 strunzone(me.savedName);
238 me.savedName = strzone(box.text);
240 me.savedName = string_null;
243 void HUDSkinList_Filter_Change(entity box, entity me)
246 strunzone(me.filterString);
250 if (strstrofs(box.text, "*", 0) >= 0 || strstrofs(box.text, "?", 0) >= 0)
251 me.filterString = strzone(box.text);
253 me.filterString = strzone(strcat("*", box.text, "*"));
256 me.filterString = string_null;
261 void SaveHUDSkin_Click(entity btn, entity me)
263 string s = me.savedName;
266 localcmd(sprintf("hud save \"%s\"\n", s));
267 me.delayedRefreshTime = time + 1;
270 void XonoticHUDSkinList_draw(entity me)
272 if(me.delayedRefreshTime > 0 && me.delayedRefreshTime < time)
274 HUDSkinList_Refresh_Click(NULL, me);
275 me.delayedRefreshTime = 0;
277 SUPER(XonoticHUDSkinList).draw(me);
280 void XonoticHUDSkinList_setHUDSkin(entity me)
282 string cfg = strcat(me.hudskinPath(me, me.selectedItem), "hud_", me.hudskinName(me, me.selectedItem), ".cfg");
283 localcmd("exec \"", cfg, "\"\n");
286 void SetHUDSkin_Click(entity btn, entity me)
291 void XonoticHUDSkinList_doubleClickListBoxItem(entity me, float i, vector where)
293 m_play_click_sound(MENU_SOUND_EXECUTE);
297 float XonoticHUDSkinList_keyDown(entity me, float scan, float ascii, float shift)
299 if(scan == K_ENTER || scan == K_KP_ENTER)
306 return SUPER(XonoticHUDSkinList).keyDown(me, scan, ascii, shift);