]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/screenshotlist.qc
Avoid loading lists of demos, screenshots and music tracks when the menu starts,...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / screenshotlist.qc
1 #ifndef SCREENSHOTLIST_H
2 #define SCREENSHOTLIST_H
3 #include "listbox.qc"
4 CLASS(XonoticScreenshotList, XonoticListBox)
5         METHOD(XonoticScreenshotList, configureXonoticScreenshotList, void(entity));
6         ATTRIB(XonoticScreenshotList, rowsPerItem, float, 1)
7         METHOD(XonoticScreenshotList, resizeNotify, void(entity, vector, vector, vector, vector));
8         METHOD(XonoticScreenshotList, setSelected, void(entity, float));
9         METHOD(XonoticScreenshotList, draw, void(entity));
10         METHOD(XonoticScreenshotList, drawListBoxItem, void(entity, int, vector, bool, bool));
11         METHOD(XonoticScreenshotList, getScreenshots, void(entity));
12         METHOD(XonoticScreenshotList, previewScreenshot, void(entity));
13         METHOD(XonoticScreenshotList, startScreenshot, void(entity));
14         METHOD(XonoticScreenshotList, screenshotName, string(entity, float));
15         METHOD(XonoticScreenshotList, doubleClickListBoxItem, void(entity, float, vector));
16         METHOD(XonoticScreenshotList, keyDown, float(entity, float, float, float));
17         METHOD(XonoticScreenshotList, destroy, void(entity));
18         METHOD(XonoticScreenshotList, showNotify, void(entity));
19         ATTRIB(XonoticScreenshotList, listScreenshot, float, -1)
20         ATTRIB(XonoticScreenshotList, realFontSize, vector, '0 0 0')
21         ATTRIB(XonoticScreenshotList, columnNameOrigin, float, 0)
22         ATTRIB(XonoticScreenshotList, columnNameSize, float, 0)
23         ATTRIB(XonoticScreenshotList, realUpperMargin, float, 0)
24         ATTRIB(XonoticScreenshotList, origin, vector, '0 0 0')
25         ATTRIB(XonoticScreenshotList, itemAbsSize, vector, '0 0 0')
26         ATTRIB(XonoticScreenshotList, filterString, string, string_null)
27         ATTRIB(XonoticScreenshotList, filterBox, entity, NULL)
28         ATTRIB(XonoticScreenshotList, filterTime, float, 0)
29
30         ATTRIB(XonoticScreenshotList, newScreenshotTime, float, 0)
31         ATTRIB(XonoticScreenshotList, newSlideShowScreenshotTime, float, 0)
32
33         ATTRIB(XonoticScreenshotList, screenshotBrowserDialog, entity, NULL)
34         ATTRIB(XonoticScreenshotList, screenshotPreview, entity, NULL)
35         ATTRIB(XonoticScreenshotList, screenshotViewerDialog, entity, NULL)
36         METHOD(XonoticScreenshotList, goScreenshot, void(entity, float));
37         METHOD(XonoticScreenshotList, startSlideShow, void(entity));
38         METHOD(XonoticScreenshotList, stopSlideShow, void(entity));
39 ENDCLASS(XonoticScreenshotList)
40
41 entity makeXonoticScreenshotList();
42 void StartScreenshot_Click(entity btn, entity me);
43 void ScreenshotList_Refresh_Click(entity btn, entity me);
44 void ScreenshotList_Filter_Would_Change(entity box, entity me);
45 void ScreenshotList_Filter_Change(entity box, entity me);
46 #endif
47
48 #ifdef IMPLEMENTATION
49
50 entity makeXonoticScreenshotList()
51 {
52         entity me;
53         me = NEW(XonoticScreenshotList);
54         me.configureXonoticScreenshotList(me);
55         return me;
56 }
57
58 void XonoticScreenshotList_configureXonoticScreenshotList(entity me)
59 {
60         me.configureXonoticListBox(me);
61 }
62
63 string XonoticScreenshotList_screenshotName(entity me, float i)
64 {
65         string s;
66         s = bufstr_get(me.listScreenshot, i);
67
68         if(substring(s, 0, 1) == "/")
69                 s = substring(s, 1, strlen(s) - 1);  // remove the first forward slash
70
71         return s;
72 }
73
74 // if subdir is true look in subdirectories too (1 level)
75 void getScreenshots_for_ext(entity me, string ext, float subdir)
76 {
77         string s;
78         if (subdir)
79                 s="screenshots/*/";
80         else
81                 s="screenshots/";
82         if(me.filterString)
83                 s=strcat(s, me.filterString, ext);
84         else
85                 s=strcat(s, "*", ext);
86
87         float list, i, n;
88         list = search_begin(s, false, true);
89         if(list >= 0)
90         {
91                 n = search_getsize(list);
92                 for(i = 0; i < n; ++i)
93                 {
94                         s = search_getfilename(list, i); // get initial full file name
95                         s = substring(s, 12, (strlen(s) - 12 - 4)); // remove "screenshots/" prefix and ".<ext>" suffix
96                         s = strdecolorize(s); // remove any pre-existing colors
97                         if(subdir)
98                         {
99                                 s = strreplace("/", "^7/", s); // clear colors at the forward slash
100                                 s = strcat("/", rgb_to_hexcolor(SKINCOLOR_SCREENSHOTLIST_SUBDIR), s); // add a forward slash for sorting, then color
101                                 bufstr_add(me.listScreenshot, s, true);
102                         }
103                         else { bufstr_add(me.listScreenshot, s, true); }
104                 }
105                 search_end(list);
106         }
107
108         if (subdir)
109                 getScreenshots_for_ext(me, ext, false);
110 }
111
112 void XonoticScreenshotList_getScreenshots(entity me)
113 {
114         if (me.listScreenshot >= 0)
115                 buf_del(me.listScreenshot);
116         me.listScreenshot = buf_create();
117         if (me.listScreenshot < 0)
118         {
119                 me.nItems = 0;
120                 return;
121         }
122         getScreenshots_for_ext(me, ".jpg", true);
123         getScreenshots_for_ext(me, ".tga", true);
124         getScreenshots_for_ext(me, ".png", true);
125         me.nItems = buf_getsize(me.listScreenshot);
126         if(me.nItems > 0)
127                 buf_sort(me.listScreenshot, 128, false);
128 }
129
130 void XonoticScreenshotList_destroy(entity me)
131 {
132         if(me.nItems > 0)
133                 buf_del(me.listScreenshot);
134 }
135
136 void XonoticScreenshotList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
137 {
138         me.itemAbsSize = '0 0 0';
139         SUPER(XonoticScreenshotList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
140
141         me.realFontSize_y = me.fontSize / (me.itemAbsSize_y = (absSize.y * me.itemHeight));
142         me.realFontSize_x = me.fontSize / (me.itemAbsSize_x = (absSize.x * (1 - me.controlWidth)));
143         me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
144
145         me.columnNameOrigin = me.realFontSize.x;
146         me.columnNameSize = 1 - 2 * me.realFontSize.x;
147 }
148
149 void XonoticScreenshotList_setSelected(entity me, float i)
150 {
151         if (me.newSlideShowScreenshotTime)
152                 me.startSlideShow(me);
153         float selectedItem_save = me.selectedItem;
154         SUPER(XonoticScreenshotList).setSelected(me, i);
155         if (me.pressed && me.selectedItem != selectedItem_save)
156         {
157                 // avoid immediate image loading on quick repeated selection changes
158                 me.newScreenshotTime = time + 0.22;
159         }
160         else if (time > me.newScreenshotTime)
161         {
162                 me.newScreenshotTime = 0;
163                 me.previewScreenshot(me); // load the preview on selection change
164         }
165 }
166
167 void XonoticScreenshotList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
168 {
169         string s;
170         if(isSelected)
171                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
172         else if(isFocused)
173         {
174                 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
175                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
176         }
177
178         s = me.screenshotName(me,i);
179         s = draw_TextShortenToWidth(s, me.columnNameSize, 0, me.realFontSize);
180         draw_Text(me.realUpperMargin * eY + (me.columnNameOrigin + 0.00 * (me.columnNameSize - draw_TextWidth(s, 0, me.realFontSize))) * eX, s, me.realFontSize, '1 1 1', SKINALPHA_TEXT, 1);
181 }
182
183 void XonoticScreenshotList_showNotify(entity me)
184 {
185         me.getScreenshots(me);
186         me.previewScreenshot(me);
187 }
188
189 void ScreenshotList_Refresh_Click(entity btn, entity me)
190 {
191         me.getScreenshots(me);
192         me.setSelected(me, 0); //always select the first element after a list update
193 }
194
195 void ScreenshotList_Filter_Change(entity box, entity me)
196 {
197         if(me.filterString)
198                 strunzone(me.filterString);
199
200         if(box.text != "")
201         {
202                 if (strstrofs(box.text, "*", 0) >= 0 || strstrofs(box.text, "?", 0) >= 0)
203                         me.filterString = strzone(box.text);
204                 else
205                         me.filterString = strzone(strcat("*", box.text, "*"));
206         }
207         else
208                 me.filterString = string_null;
209
210         ScreenshotList_Refresh_Click(NULL, me);
211 }
212
213 void ScreenshotList_Filter_Would_Change(entity box, entity me)
214 {
215         me.filterBox = box;
216         me.filterTime = time + 0.5;
217 }
218
219 void XonoticScreenshotList_draw(entity me)
220 {
221         if (me.filterTime && time > me.filterTime)
222         {
223                 ScreenshotList_Filter_Change(me.filterBox, me);
224                 me.filterTime = 0;
225         }
226         if (me.newScreenshotTime && time > me.newScreenshotTime)
227         {
228                 me.previewScreenshot(me);
229                 me.newScreenshotTime = 0;
230         }
231         else if (me.newSlideShowScreenshotTime && time > me.newSlideShowScreenshotTime)
232         {
233                 if (me.selectedItem == me.nItems - 1) //last screenshot?
234                 {
235                         // restart from the first screenshot
236                         me.setSelected(me, 0);
237                         me.goScreenshot(me, +0);
238                 }
239                 else
240                         me.goScreenshot(me, +1);
241         }
242         SUPER(XonoticScreenshotList).draw(me);
243 }
244
245 void XonoticScreenshotList_startSlideShow(entity me)
246 {
247         me.newSlideShowScreenshotTime = time + 3;
248 }
249
250 void XonoticScreenshotList_stopSlideShow(entity me)
251 {
252         me.newSlideShowScreenshotTime = 0;
253 }
254
255 void XonoticScreenshotList_goScreenshot(entity me, float d)
256 {
257         if(!me.screenshotViewerDialog)
258                 return;
259         me.setSelected(me, me.selectedItem + d);
260         me.screenshotViewerDialog.loadScreenshot(me.screenshotViewerDialog, strcat("/screenshots/", strdecolorize(me.screenshotName(me,me.selectedItem))));
261 }
262
263 void XonoticScreenshotList_startScreenshot(entity me)
264 {
265         me.screenshotViewerDialog.loadScreenshot(me.screenshotViewerDialog, strcat("/screenshots/", strdecolorize(me.screenshotName(me,me.selectedItem))));
266         // pop up screenshot
267         DialogOpenButton_Click_withCoords(NULL, me.screenshotViewerDialog, me.origin + eX * (me.columnNameOrigin * me.size.x) + eY * ((me.itemHeight * me.selectedItem - me.scrollPos) * me.size.y), eY * me.itemAbsSize.y + eX * (me.itemAbsSize.x * me.columnNameSize));
268 }
269
270 void XonoticScreenshotList_previewScreenshot(entity me)
271 {
272         if(!me.screenshotBrowserDialog)
273                 return;
274         if (me.nItems <= 0)
275                 me.screenshotBrowserDialog.loadPreviewScreenshot(me.screenshotBrowserDialog, "");
276         else
277                 me.screenshotBrowserDialog.loadPreviewScreenshot(me.screenshotBrowserDialog, strcat("/screenshots/", strdecolorize(me.screenshotName(me,me.selectedItem))));
278 }
279
280 void StartScreenshot_Click(entity btn, entity me)
281 {
282         me.startScreenshot(me);
283 }
284
285 void XonoticScreenshotList_doubleClickListBoxItem(entity me, float i, vector where)
286 {
287         me.startScreenshot(me);
288 }
289
290 float XonoticScreenshotList_keyDown(entity me, float scan, float ascii, float shift)
291 {
292         if(scan == K_ENTER || scan == K_KP_ENTER || scan == K_MOUSE2 || scan == K_SPACE) {
293                 me.startScreenshot(me);
294                 return 1;
295         }
296         return SUPER(XonoticScreenshotList).keyDown(me, scan, ascii, shift);
297 }
298 #endif