1 #include "hudskinlist.qh"
5 entity makeXonoticHUDSkinList()
8 me = NEW(XonoticHUDSkinList);
9 me.configureXonoticHUDSkinList(me);
13 void XonoticHUDSkinList_configureXonoticHUDSkinList(entity me)
15 me.configureXonoticListBox(me);
19 const float HUDSKINPARM_NAME = 0;
20 const float HUDSKINPARM_PATH = 1;
21 const float HUDSKINPARM_TITLE = 2;
22 const float HUDSKINPARM_AUTHOR = 3;
23 const float HUDSKINPARM_COUNT = 4;
24 string XonoticHUDSkinList_hudskinName(entity me, float i)
26 return bufstr_get(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_NAME);
28 string XonoticHUDSkinList_hudskinPath(entity me, float i)
30 return bufstr_get(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_PATH);
32 string XonoticHUDSkinList_hudskinTitle(entity me, float i)
34 return bufstr_get(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_TITLE);
36 string XonoticHUDSkinList_hudskinAuthor(entity me, float i)
38 return bufstr_get(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_AUTHOR);
41 // subdir can be a regular expression
42 void getHUDSkinFiles(entity me, int sortbuf, string subdir)
45 if (me.filterString) {
50 s = strcat(subdir, "hud_", s, ".cfg");
52 int list = search_begin(s, false, true);
54 int n = search_getsize(list);
55 for (int i = 0; i < n; ++i) {
56 string s = search_getfilename(list, i);
57 int subdir_ofs = strstrofs(s, "/", 0);
58 if (subdir_ofs >= 0) {
62 ofs = strstrofs(s, "/", subdir_ofs + 1);
66 if (subdir_ofs == -1) {
67 bufstr_add(sortbuf, s, true);
69 subdir = substring(s, 0, subdir_ofs);
70 string filename = substring(s, subdir_ofs + 1, -1);
71 // invert path and filename position so we can sort sortbuf by filename
72 bufstr_add(sortbuf, strcat(filename, "/", subdir), true);
79 void getAllHUDSkins(entity me, int sortbuf)
81 int n = buf_getsize(sortbuf);
82 for (int i = 0; i < n; ++i) {
83 string entry = bufstr_get(sortbuf, i);
84 int ofs = strstrofs(entry, "/", 0);
86 string filename = entry;
88 s = substring(entry, ofs + 1, -1); // skip initial "/"
90 bufstr_set(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_PATH, s);
91 filename = strcat(s, substring(entry, 0, ofs));
95 s = substring(entry, 4, ofs - 4 - 4); // remove "hud_" prefix and ".cfg" suffix
96 bufstr_set(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_NAME, s);
98 int fh = fopen(filename, FILE_READ);
102 while ((s = fgets(fh)) && substring(s, 0, 2) == "//") {
103 tokenize_console(substring(s, 2, -1));
104 if (argv(0) == "title") {
105 bufstr_set(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_TITLE, argv(1));
106 } else if (argv(0) == "author") {
107 bufstr_set(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_AUTHOR, argv(1));
114 void XonoticHUDSkinList_getHUDSkins(entity me)
116 if (me.listHUDSkin >= 0) {
117 buf_del(me.listHUDSkin);
119 me.listHUDSkin = buf_create();
120 if (me.listHUDSkin < 0) {
124 int sortbuf = buf_create();
125 getHUDSkinFiles(me, sortbuf, "");
126 getHUDSkinFiles(me, sortbuf, "data/");
127 buf_sort(sortbuf, 128, 0);
128 getAllHUDSkins(me, sortbuf);
130 me.nItems = buf_getsize(me.listHUDSkin) / HUDSKINPARM_COUNT;
133 void XonoticHUDSkinList_destroy(entity me)
136 buf_del(me.listHUDSkin);
140 void XonoticHUDSkinList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
142 me.itemAbsSize = '0 0 0';
143 SUPER(XonoticHUDSkinList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
145 me.realFontSize_y = me.fontSize / (me.itemAbsSize_y = (absSize.y * me.itemHeight));
146 me.realFontSize_x = me.fontSize / (me.itemAbsSize_x = (absSize.x * (1 - me.controlWidth)));
147 me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
149 me.columnNameOrigin = me.realFontSize.x;
150 me.columnNameSize = 1 - 2 * me.realFontSize.x;
153 void XonoticHUDSkinList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
157 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
158 } else if (isFocused) {
159 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
160 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
163 s = me.hudskinTitle(me, i);
165 s = me.hudskinName(me, i);
167 s2 = me.hudskinAuthor(me, i);
169 s = strcat(s, " (", s2, ")");
171 s = draw_TextShortenToWidth(s, me.columnNameSize, 0, me.realFontSize);
172 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);
175 void XonoticHUDSkinList_showNotify(entity me)
180 void HUDSkinList_Refresh_Click(entity btn, entity me)
183 me.setSelected(me, 0); // always select the first element after a list update
186 void HUDSkinList_SavedName_Change(entity box, entity me)
189 strunzone(me.savedName);
192 if (box.text != "") {
193 me.savedName = strzone(box.text);
195 me.savedName = string_null;
199 void HUDSkinList_Filter_Change(entity box, entity me)
201 if (me.filterString) {
202 strunzone(me.filterString);
205 if (box.text != "") {
206 if (strstrofs(box.text, "*", 0) >= 0 || strstrofs(box.text, "?", 0) >= 0) {
207 me.filterString = strzone(box.text);
209 me.filterString = strzone(strcat("*", box.text, "*"));
212 me.filterString = string_null;
218 void SaveHUDSkin_Click(entity btn, entity me)
220 string s = me.savedName;
224 localcmd(sprintf("hud save \"%s\"\n", s));
225 me.delayedRefreshTime = time + 1;
228 void XonoticHUDSkinList_draw(entity me)
230 if (me.delayedRefreshTime > 0 && me.delayedRefreshTime < time) {
231 HUDSkinList_Refresh_Click(NULL, me);
232 me.delayedRefreshTime = 0;
234 SUPER(XonoticHUDSkinList).draw(me);
237 void XonoticHUDSkinList_setHUDSkin(entity me)
239 string cfg = strcat(me.hudskinPath(me, me.selectedItem), "hud_", me.hudskinName(me, me.selectedItem), ".cfg");
240 localcmd("exec \"", cfg, "\"\n");
243 void SetHUDSkin_Click(entity btn, entity me)
248 void XonoticHUDSkinList_doubleClickListBoxItem(entity me, float i, vector where)
250 m_play_click_sound(MENU_SOUND_EXECUTE);
254 float XonoticHUDSkinList_keyDown(entity me, float scan, float ascii, float shift)
256 if (scan == K_ENTER || scan == K_KP_ENTER) {
260 return SUPER(XonoticHUDSkinList).keyDown(me, scan, ascii, shift);