]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/demolist.qc
Listbox / Picker: Implement item fading in a different way so that it gets influenced...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / demolist.qc
1 #ifdef INTERFACE
2 CLASS(XonoticDemoList) EXTENDS(XonoticListBox)
3         METHOD(XonoticDemoList, configureXonoticDemoList, void(entity))
4         ATTRIB(XonoticDemoList, rowsPerItem, float, 1)
5         METHOD(XonoticDemoList, resizeNotify, void(entity, vector, vector, vector, vector))
6         METHOD(XonoticDemoList, drawListBoxItem, void(entity, int, vector, bool, bool))
7         METHOD(XonoticDemoList, getDemos, void(entity))
8         METHOD(XonoticDemoList, startDemo, void(entity))
9         METHOD(XonoticDemoList, timeDemo, void(entity))
10         METHOD(XonoticDemoList, demoName, string(entity, float))
11         METHOD(XonoticDemoList, doubleClickListBoxItem, void(entity, float, vector))
12         METHOD(XonoticDemoList, keyDown, float(entity, float, float, float))
13         METHOD(XonoticDemoList, destroy, void(entity))
14         METHOD(XonoticDemoList, showNotify, void(entity))
15
16         ATTRIB(XonoticDemoList, listDemo, float, -1)
17         ATTRIB(XonoticDemoList, realFontSize, vector, '0 0 0')
18         ATTRIB(XonoticDemoList, columnNameOrigin, float, 0)
19         ATTRIB(XonoticDemoList, columnNameSize, float, 0)
20         ATTRIB(XonoticDemoList, realUpperMargin, float, 0)
21         ATTRIB(XonoticDemoList, origin, vector, '0 0 0')
22         ATTRIB(XonoticDemoList, itemAbsSize, vector, '0 0 0')
23
24         ATTRIB(XonoticDemoList, filterString, string, string_null)
25 ENDCLASS(XonoticDemoList)
26
27 #ifndef IMPLEMENTATION
28 // public:
29 entity demolist; // for reference elsewhere
30 entity makeXonoticDemoList();
31 #endif
32 void DemoList_Refresh_Click(entity btn, entity me);
33 void DemoList_Filter_Change(entity box, entity me);
34 #endif
35
36 #ifdef IMPLEMENTATION
37
38 entity makeXonoticDemoList()
39 {
40         entity me;
41         me = spawnXonoticDemoList();
42         me.configureXonoticDemoList(me);
43         return me;
44 }
45
46 void XonoticDemoList_configureXonoticDemoList(entity me)
47 {
48         me.configureXonoticListBox(me);
49         me.getDemos(me);
50 }
51
52 string XonoticDemoList_demoName(entity me, float i)
53 {
54         string s;
55         s = bufstr_get(me.listDemo, i);
56
57         if(substring(s, 0, 1) == "/")
58                 s = substring(s, 1, strlen(s) - 1);  // remove the first forward slash
59
60         return s;
61 }
62
63 // if subdir is true look in subdirectories too (1 level)
64 void getDemos_for_ext(entity me, string ext, float subdir)
65 {
66         string s;
67         if (subdir)
68                 s="demos/*/";
69         else
70                 s="demos/";
71         if(me.filterString)
72                 s=strcat(s, me.filterString, ext);
73         else
74                 s=strcat(s, "*", ext);
75
76         float list, i, n;
77         list = search_begin(s, false, true);
78         if(list >= 0)
79         {
80                 n = search_getsize(list);
81                 for(i = 0; i < n; ++i)
82                 {
83                         s = search_getfilename(list, i); // get initial full file name
84                         s = substring(s, 6, (strlen(s) - 6 - 4)); // remove "demos/" prefix and ".dem" suffix
85                         s = strdecolorize(s); // remove any pre-existing colors
86                         if(subdir)
87                         {
88                                 s = strreplace("/", "^7/", s); // clear colors at the forward slash
89                                 s = strcat("/", rgb_to_hexcolor(SKINCOLOR_DEMOLIST_SUBDIR), s); // add a forward slash for sorting, then color
90                                 bufstr_add(me.listDemo, s, true);
91                         }
92                         else { bufstr_add(me.listDemo, s, true); }
93                 }
94                 search_end(list);
95         }
96
97         if (subdir)
98                 getDemos_for_ext(me, ext, false);
99 }
100
101 void XonoticDemoList_getDemos(entity me)
102 {
103         if (me.listDemo >= 0)
104                 buf_del(me.listDemo);
105         me.listDemo = buf_create();
106         if (me.listDemo < 0)
107         {
108                 me.nItems = 0;
109                 return;
110         }
111         getDemos_for_ext(me, ".dem", true);
112         me.nItems = buf_getsize(me.listDemo);
113         if(me.nItems > 0)
114                 buf_sort(me.listDemo, 128, false);
115 }
116
117 void XonoticDemoList_destroy(entity me)
118 {
119         if(me.nItems > 0)
120                 buf_del(me.listDemo);
121 }
122
123 void XonoticDemoList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
124 {
125         me.itemAbsSize = '0 0 0';
126         SUPER(XonoticDemoList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
127
128         me.realFontSize_y = me.fontSize / (me.itemAbsSize_y = (absSize.y * me.itemHeight));
129         me.realFontSize_x = me.fontSize / (me.itemAbsSize_x = (absSize.x * (1 - me.controlWidth)));
130         me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
131
132         me.columnNameOrigin = me.realFontSize.x;
133         me.columnNameSize = 1 - 2 * me.realFontSize.x;
134 }
135
136 void XonoticDemoList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
137 {
138         string s;
139         if(isSelected)
140                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
141         else if(isFocused)
142         {
143                 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
144                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
145         }
146
147         s = me.demoName(me,i);
148         s = draw_TextShortenToWidth(s, me.columnNameSize, 0, me.realFontSize);
149         draw_Text(me.realUpperMargin * eY + (me.columnNameOrigin + 0.00 * (me.columnNameSize - draw_TextWidth(s, 0, me.realFontSize))) * eX, s, me.realFontSize, SKINCOLOR_TEXT, SKINALPHA_TEXT, 1);
150 }
151
152 void XonoticDemoList_showNotify(entity me)
153 {
154         me.getDemos(me);
155 }
156
157 void DemoList_Refresh_Click(entity btn, entity me)
158 {
159         me.getDemos(me);
160         me.setSelected(me, 0); //always select the first element after a list update
161 }
162
163 void DemoList_Filter_Change(entity box, entity me)
164 {
165         if(me.filterString)
166                 strunzone(me.filterString);
167
168         if(box.text != "")
169         {
170                 if (strstrofs(box.text, "*", 0) >= 0 || strstrofs(box.text, "?", 0) >= 0)
171                         me.filterString = strzone(box.text);
172                 else
173                         me.filterString = strzone(strcat("*", box.text, "*"));
174         }
175         else
176                 me.filterString = string_null;
177
178         me.getDemos(me);
179 }
180
181 void XonoticDemoList_startDemo(entity me)
182 {
183         string s;
184         s = me.demoName(me, me.selectedItem);
185         s = strdecolorize(s);
186
187         localcmd("playdemo \"demos/", s, ".dem\" \nwait \ntogglemenu\n");
188 }
189
190 void XonoticDemoList_timeDemo(entity me)
191 {
192         string s;
193         s = me.demoName(me, me.selectedItem);
194         s = strdecolorize(s);
195
196         localcmd("timedemo \"demos/", s, ".dem\" \nwait \ntogglemenu\n");
197 }
198
199 void DemoConfirm_ListClick_Check_Gamestatus(entity me)
200 {
201         if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER))) // we're not in a match, lets watch the demo
202         {
203                 me.startDemo(me);
204         }
205         else // already in a match, player has to confirm
206         {
207                 DialogOpenButton_Click_withCoords(
208                         me,
209                         main.demostartconfirmDialog,
210                         boxToGlobal(eY * (me.selectedItem * me.itemHeight - me.scrollPos), me.origin, me.size),
211                         boxToGlobalSize(eY * me.itemHeight + eX * (1 - me.controlWidth), me.size)
212                 );
213         }
214 }
215
216 void XonoticDemoList_doubleClickListBoxItem(entity me, float i, vector where)
217 {
218         DemoConfirm_ListClick_Check_Gamestatus(me);
219 }
220
221 float XonoticDemoList_keyDown(entity me, float scan, float ascii, float shift)
222 {
223         if(scan == K_ENTER || scan == K_KP_ENTER)
224         {
225                 DemoConfirm_ListClick_Check_Gamestatus(me);
226                 return 1;
227         }
228         else
229         {
230                 return SUPER(XonoticDemoList).keyDown(me, scan, ascii, shift);
231         }
232 }
233 #endif
234