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