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