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