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