]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/hudskinlist.qc
Merge branch 'master' into Mario/hagar_notfixed
[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         s = strcat(subdir, "hud_", s, ".cfg");
50
51         int list = search_begin(s, false, true);
52         if(list >= 0)
53         {
54                 int n = search_getsize(list);
55                 for(int i = 0; i < n; ++i)
56                 {
57                         string s = search_getfilename(list, i);
58                         int subdir_ofs = strstrofs(s, "/", 0);
59                         if(subdir_ofs >= 0)
60                         {
61                                 int ofs = subdir_ofs;
62                                 while(ofs != -1)
63                                 {
64                                         subdir_ofs = ofs;
65                                         ofs = strstrofs(s, "/", subdir_ofs + 1);
66                                 }
67                         }
68
69                         if(subdir_ofs == -1)
70                                 bufstr_add(sortbuf, s, true);
71                         else
72                         {
73                                 subdir = substring(s, 0, subdir_ofs);
74                                 string filename = substring(s, subdir_ofs + 1, -1);
75                                 // invert path and filename position so we can sort sortbuf by filename
76                                 bufstr_add(sortbuf, strcat(filename, "/", subdir), true);
77                         }
78                 }
79                 search_end(list);
80         }
81 }
82
83 void getAllHUDSkins(entity me, int sortbuf)
84 {
85         int n = buf_getsize(sortbuf);
86         for(int i = 0; i < n; ++i)
87         {
88                 string entry = bufstr_get(sortbuf, i);
89                 int ofs = strstrofs(entry, "/", 0);
90                 string s = "";
91                 string filename = entry;
92                 if(ofs >= 0)
93                 {
94                         s = substring(entry, ofs + 1, -1); // skip initial "/"
95                         s = strcat(s, "/");
96                         bufstr_set(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_PATH, s);
97                         filename = strcat(s, substring(entry, 0, ofs));
98                 }
99                 else
100                         ofs = strlen(entry);
101                 s = substring(entry, 4, ofs - 4 - 4); // remove "hud_" prefix and ".cfg" suffix
102                 bufstr_set(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_NAME, s);
103
104                 int fh = fopen(filename, FILE_READ);
105                 if(fh < 0)
106                         continue;
107                 while((s = fgets(fh)) && substring(s, 0, 2) == "//")
108                 {
109                         tokenize_console(substring(s, 2, -1));
110                         if(argv(0) == "title")
111                                 bufstr_set(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_TITLE, argv(1));
112                         else if(argv(0) == "author")
113                                 bufstr_set(me.listHUDSkin, i * HUDSKINPARM_COUNT + HUDSKINPARM_AUTHOR, argv(1));
114                 }
115                 fclose(fh);
116         }
117 }
118
119 void XonoticHUDSkinList_getHUDSkins(entity me)
120 {
121         if (me.listHUDSkin >= 0)
122                 buf_del(me.listHUDSkin);
123         me.listHUDSkin = buf_create();
124         if (me.listHUDSkin < 0)
125         {
126                 me.nItems = 0;
127                 return;
128         }
129         int sortbuf = buf_create();
130         getHUDSkinFiles(me, sortbuf, "");
131         getHUDSkinFiles(me, sortbuf, "data/");
132         buf_sort(sortbuf, 128, 0);
133         getAllHUDSkins(me, sortbuf);
134         buf_del(sortbuf);
135         me.nItems = buf_getsize(me.listHUDSkin) / HUDSKINPARM_COUNT;
136 }
137
138 void XonoticHUDSkinList_destroy(entity me)
139 {
140         if(me.nItems > 0)
141                 buf_del(me.listHUDSkin);
142 }
143
144 void XonoticHUDSkinList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
145 {
146         me.itemAbsSize = '0 0 0';
147         SUPER(XonoticHUDSkinList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
148
149         me.realFontSize_y = me.fontSize / (me.itemAbsSize_y = (absSize.y * me.itemHeight));
150         me.realFontSize_x = me.fontSize / (me.itemAbsSize_x = (absSize.x * (1 - me.controlWidth)));
151         me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
152
153         me.columnNameOrigin = me.realFontSize.x;
154         me.columnNameSize = 1 - 2 * me.realFontSize.x;
155 }
156
157 void XonoticHUDSkinList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
158 {
159         string s, s2;
160         if(isSelected)
161                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
162         else if(isFocused)
163         {
164                 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
165                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
166         }
167
168         s = me.hudskinTitle(me, i);
169         if(s == "")
170                 s = me.hudskinName(me, i);
171         s2 = me.hudskinAuthor(me, i);
172         if(s2 != "")
173                 s = strcat(s, " (", s2, ")");
174         s = draw_TextShortenToWidth(s, me.columnNameSize, 0, me.realFontSize);
175         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);
176 }
177
178 void XonoticHUDSkinList_showNotify(entity me)
179 {
180         me.getHUDSkins(me);
181 }
182
183 void HUDSkinList_Refresh_Click(entity btn, entity me)
184 {
185         me.getHUDSkins(me);
186         me.setSelected(me, 0); //always select the first element after a list update
187 }
188
189 void HUDSkinList_SavedName_Change(entity box, entity me)
190 {
191         if(me.savedName)
192                 strunzone(me.savedName);
193
194         if(box.text != "")
195                 me.savedName = strzone(box.text);
196         else
197                 me.savedName = string_null;
198 }
199
200 void HUDSkinList_Filter_Change(entity box, entity me)
201 {
202         if(me.filterString)
203                 strunzone(me.filterString);
204
205         if(box.text != "")
206         {
207                 if (strstrofs(box.text, "*", 0) >= 0 || strstrofs(box.text, "?", 0) >= 0)
208                         me.filterString = strzone(box.text);
209                 else
210                         me.filterString = strzone(strcat("*", box.text, "*"));
211         }
212         else
213                 me.filterString = string_null;
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         localcmd(sprintf("hud save \"%s\"\n", s));
224         me.delayedRefreshTime = time + 1;
225 }
226
227 void XonoticHUDSkinList_draw(entity me)
228 {
229         if(me.delayedRefreshTime > 0 && me.delayedRefreshTime < time)
230         {
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         {
258                 me.setHUDSkin(me);
259                 return 1;
260         }
261         else
262         {
263                 return SUPER(XonoticHUDSkinList).keyDown(me, scan, ascii, shift);
264         }
265 }