]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/hudskinlist.qc
take3: format 903 files
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / hudskinlist.qc
1 #include "hudskinlist.qh"
2
3 #include "inputbox.qh"
4
5 entity makeXonoticHUDSkinList()
6 {
7         entity me;
8         me = NEW(XonoticHUDSkinList);
9         me.configureXonoticHUDSkinList(me);
10         return me;
11 }
12
13 void XonoticHUDSkinList_configureXonoticHUDSkinList(entity me)
14 {
15         me.configureXonoticListBox(me);
16         me.nItems = 0;
17 }
18
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)
25 {
26         return bufstr_get(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_NAME);
27 }
28 string XonoticHUDSkinList_hudskinPath(entity me, float i)
29 {
30         return bufstr_get(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_PATH);
31 }
32 string XonoticHUDSkinList_hudskinTitle(entity me, float i)
33 {
34         return bufstr_get(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_TITLE);
35 }
36 string XonoticHUDSkinList_hudskinAuthor(entity me, float i)
37 {
38         return bufstr_get(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_AUTHOR);
39 }
40
41 // subdir can be a regular expression
42 void getHUDSkinFiles(entity me, int sortbuf, string subdir)
43 {
44         string s;
45         if (me.filterString) {
46                 s = me.filterString;
47         } else {
48                 s = "*";
49         }
50         s = strcat(subdir, "hud_", s, ".cfg");
51
52         int list = search_begin(s, false, true);
53         if (list >= 0) {
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) {
59                                 int ofs = subdir_ofs;
60                                 while (ofs != -1) {
61                                         subdir_ofs = ofs;
62                                         ofs = strstrofs(s, "/", subdir_ofs + 1);
63                                 }
64                         }
65
66                         if (subdir_ofs == -1) {
67                                 bufstr_add(sortbuf, s, true);
68                         } else {
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);
73                         }
74                 }
75                 search_end(list);
76         }
77 }
78
79 void getAllHUDSkins(entity me, int sortbuf)
80 {
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);
85                 string s = "";
86                 string filename = entry;
87                 if (ofs >= 0) {
88                         s = substring(entry, ofs + 1, -1); // skip initial "/"
89                         s = strcat(s, "/");
90                         bufstr_set(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_PATH, s);
91                         filename = strcat(s, substring(entry, 0, ofs));
92                 } else {
93                         ofs = strlen(entry);
94                 }
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);
97
98                 int fh = fopen(filename, FILE_READ);
99                 if (fh < 0) {
100                         continue;
101                 }
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));
108                         }
109                 }
110                 fclose(fh);
111         }
112 }
113
114 void XonoticHUDSkinList_getHUDSkins(entity me)
115 {
116         if (me.listHUDSkin >= 0) {
117                 buf_del(me.listHUDSkin);
118         }
119         me.listHUDSkin = buf_create();
120         if (me.listHUDSkin < 0) {
121                 me.nItems = 0;
122                 return;
123         }
124         int sortbuf = buf_create();
125         getHUDSkinFiles(me, sortbuf, "");
126         getHUDSkinFiles(me, sortbuf, "data/");
127         buf_sort(sortbuf, 128, 0);
128         getAllHUDSkins(me, sortbuf);
129         buf_del(sortbuf);
130         me.nItems = buf_getsize(me.listHUDSkin) / HUDSKINPARM_COUNT;
131 }
132
133 void XonoticHUDSkinList_destroy(entity me)
134 {
135         if (me.nItems > 0) {
136                 buf_del(me.listHUDSkin);
137         }
138 }
139
140 void XonoticHUDSkinList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
141 {
142         me.itemAbsSize = '0 0 0';
143         SUPER(XonoticHUDSkinList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
144
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);
148
149         me.columnNameOrigin = me.realFontSize.x;
150         me.columnNameSize = 1 - 2 * me.realFontSize.x;
151 }
152
153 void XonoticHUDSkinList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
154 {
155         string s, s2;
156         if (isSelected) {
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);
161         }
162
163         s = me.hudskinTitle(me, i);
164         if (s == "") {
165                 s = me.hudskinName(me, i);
166         }
167         s2 = me.hudskinAuthor(me, i);
168         if (s2 != "") {
169                 s = strcat(s, " (", s2, ")");
170         }
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);
173 }
174
175 void XonoticHUDSkinList_showNotify(entity me)
176 {
177         me.getHUDSkins(me);
178 }
179
180 void HUDSkinList_Refresh_Click(entity btn, entity me)
181 {
182         me.getHUDSkins(me);
183         me.setSelected(me, 0); // always select the first element after a list update
184 }
185
186 void HUDSkinList_SavedName_Change(entity box, entity me)
187 {
188         if (me.savedName) {
189                 strunzone(me.savedName);
190         }
191
192         if (box.text != "") {
193                 me.savedName = strzone(box.text);
194         } else {
195                 me.savedName = string_null;
196         }
197 }
198
199 void HUDSkinList_Filter_Change(entity box, entity me)
200 {
201         if (me.filterString) {
202                 strunzone(me.filterString);
203         }
204
205         if (box.text != "") {
206                 if (strstrofs(box.text, "*", 0) >= 0 || strstrofs(box.text, "?", 0) >= 0) {
207                         me.filterString = strzone(box.text);
208                 } else {
209                         me.filterString = strzone(strcat("*", box.text, "*"));
210                 }
211         } else {
212                 me.filterString = string_null;
213         }
214
215         me.getHUDSkins(me);
216 }
217
218 void SaveHUDSkin_Click(entity btn, entity me)
219 {
220         string s = me.savedName;
221         if (s == "") {
222                 s = "myconfig";
223         }
224         localcmd(sprintf("hud save \"%s\"\n", s));
225         me.delayedRefreshTime = time + 1;
226 }
227
228 void XonoticHUDSkinList_draw(entity me)
229 {
230         if (me.delayedRefreshTime > 0 && me.delayedRefreshTime < time) {
231                 HUDSkinList_Refresh_Click(NULL, me);
232                 me.delayedRefreshTime = 0;
233         }
234         SUPER(XonoticHUDSkinList).draw(me);
235 }
236
237 void XonoticHUDSkinList_setHUDSkin(entity me)
238 {
239         string cfg = strcat(me.hudskinPath(me, me.selectedItem), "hud_", me.hudskinName(me, me.selectedItem), ".cfg");
240         localcmd("exec \"", cfg, "\"\n");
241 }
242
243 void SetHUDSkin_Click(entity btn, entity me)
244 {
245         me.setHUDSkin(me);
246 }
247
248 void XonoticHUDSkinList_doubleClickListBoxItem(entity me, float i, vector where)
249 {
250         m_play_click_sound(MENU_SOUND_EXECUTE);
251         me.setHUDSkin(me);
252 }
253
254 float XonoticHUDSkinList_keyDown(entity me, float scan, float ascii, float shift)
255 {
256         if (scan == K_ENTER || scan == K_KP_ENTER) {
257                 me.setHUDSkin(me);
258                 return 1;
259         } else {
260                 return SUPER(XonoticHUDSkinList).keyDown(me, scan, ascii, shift);
261         }
262 }