]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/hudskinlist.qc
Add a HUD skin selector in the HUD Setup window (that one that appears pressing ESC...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / hudskinlist.qc
1 #ifndef HUDSKINLIST_H
2 #define HUDSKINLIST_H
3 #include "listbox.qc"
4 CLASS(XonoticHUDSkinList, XonoticListBox)
5         METHOD(XonoticHUDSkinList, configureXonoticHUDSkinList, void(entity));
6         ATTRIB(XonoticHUDSkinList, rowsPerItem, float, 1)
7         METHOD(XonoticHUDSkinList, resizeNotify, void(entity, vector, vector, vector, vector));
8         METHOD(XonoticHUDSkinList, drawListBoxItem, void(entity, int, vector, bool, bool));
9         METHOD(XonoticHUDSkinList, getHUDSkins, void(entity));
10         METHOD(XonoticHUDSkinList, setHUDSkin, void(entity));
11         METHOD(XonoticHUDSkinList, hudskinName, string(entity, float));
12         METHOD(XonoticHUDSkinList, doubleClickListBoxItem, void(entity, float, vector));
13         METHOD(XonoticHUDSkinList, keyDown, float(entity, float, float, float));
14         METHOD(XonoticHUDSkinList, destroy, void(entity));
15         METHOD(XonoticHUDSkinList, showNotify, void(entity));
16
17         ATTRIB(XonoticHUDSkinList, listHUDSkin, float, -1)
18         ATTRIB(XonoticHUDSkinList, realFontSize, vector, '0 0 0')
19         ATTRIB(XonoticHUDSkinList, columnNameOrigin, float, 0)
20         ATTRIB(XonoticHUDSkinList, columnNameSize, float, 0)
21         ATTRIB(XonoticHUDSkinList, realUpperMargin, float, 0)
22         ATTRIB(XonoticHUDSkinList, origin, vector, '0 0 0')
23         ATTRIB(XonoticHUDSkinList, itemAbsSize, vector, '0 0 0')
24
25         ATTRIB(XonoticHUDSkinList, filterString, string, string_null)
26 ENDCLASS(XonoticHUDSkinList)
27
28 #ifndef IMPLEMENTATION
29 // public:
30 entity hudskinlist;
31 entity makeXonoticHUDSkinList();
32 void SetHUDSkin_Click(entity btn, entity me);
33 #endif
34 void HUDSkinList_Refresh_Click(entity btn, entity me);
35 void HUDSkinList_Filter_Change(entity box, entity me);
36 #endif
37
38 #ifdef IMPLEMENTATION
39
40 entity makeXonoticHUDSkinList()
41 {
42         entity me;
43         me = NEW(XonoticHUDSkinList);
44         me.configureXonoticHUDSkinList(me);
45         return me;
46 }
47
48 void XonoticHUDSkinList_configureXonoticHUDSkinList(entity me)
49 {
50         me.configureXonoticListBox(me);
51         me.getHUDSkins(me);
52 }
53
54 string XonoticHUDSkinList_hudskinName(entity me, float i)
55 {
56         string s;
57         s = bufstr_get(me.listHUDSkin, i);
58
59         if(substring(s, 0, 1) == "/")
60                 s = substring(s, 1, strlen(s) - 1);  // remove the first forward slash
61
62         return s;
63 }
64
65 void getAllHUDSkins(entity me)
66 {
67         string s;
68         if(me.filterString)
69                 s = me.filterString;
70         else
71                 s = "*";
72         s = strcat("hud_", s, ".cfg");
73
74         float list, i, n;
75         list = search_begin(s, false, true);
76         if(list >= 0)
77         {
78                 n = search_getsize(list);
79                 for(i = 0; i < n; ++i)
80                 {
81                         s = search_getfilename(list, i); // get initial full file name
82                         s = substring(s, 4, (strlen(s) - 4 - 4)); // remove "hud_" prefix and ".cfg" suffix
83                         bufstr_add(me.listHUDSkin, s, true);
84                 }
85                 search_end(list);
86         }
87 }
88
89 void XonoticHUDSkinList_getHUDSkins(entity me)
90 {
91         if (me.listHUDSkin >= 0)
92                 buf_del(me.listHUDSkin);
93         me.listHUDSkin = buf_create();
94         if (me.listHUDSkin < 0)
95         {
96                 me.nItems = 0;
97                 return;
98         }
99         getAllHUDSkins(me);
100         me.nItems = buf_getsize(me.listHUDSkin);
101         if(me.nItems > 0)
102                 buf_sort(me.listHUDSkin, 128, false);
103 }
104
105 void XonoticHUDSkinList_destroy(entity me)
106 {
107         if(me.nItems > 0)
108                 buf_del(me.listHUDSkin);
109 }
110
111 void XonoticHUDSkinList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
112 {
113         me.itemAbsSize = '0 0 0';
114         SUPER(XonoticHUDSkinList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
115
116         me.realFontSize_y = me.fontSize / (me.itemAbsSize_y = (absSize.y * me.itemHeight));
117         me.realFontSize_x = me.fontSize / (me.itemAbsSize_x = (absSize.x * (1 - me.controlWidth)));
118         me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
119
120         me.columnNameOrigin = me.realFontSize.x;
121         me.columnNameSize = 1 - 2 * me.realFontSize.x;
122 }
123
124 void XonoticHUDSkinList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
125 {
126         string s;
127         if(isSelected)
128                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
129         else if(isFocused)
130         {
131                 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
132                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
133         }
134
135         s = me.hudskinName(me,i);
136         s = draw_TextShortenToWidth(s, me.columnNameSize, 0, me.realFontSize);
137         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);
138 }
139
140 void XonoticHUDSkinList_showNotify(entity me)
141 {
142         me.getHUDSkins(me);
143 }
144
145 void HUDSkinList_Refresh_Click(entity btn, entity me)
146 {
147         me.getHUDSkins(me);
148         me.setSelected(me, 0); //always select the first element after a list update
149 }
150
151 void HUDSkinList_Filter_Change(entity box, entity me)
152 {
153         if(me.filterString)
154                 strunzone(me.filterString);
155
156         if(box.text != "")
157         {
158                 if (strstrofs(box.text, "*", 0) >= 0 || strstrofs(box.text, "?", 0) >= 0)
159                         me.filterString = strzone(box.text);
160                 else
161                         me.filterString = strzone(strcat("*", box.text, "*"));
162         }
163         else
164                 me.filterString = string_null;
165
166         me.getHUDSkins(me);
167 }
168
169 void XonoticHUDSkinList_setHUDSkin(entity me)
170 {
171         string s = me.hudskinName(me, me.selectedItem);
172
173         localcmd("exec \"hud_", s, ".cfg\"\n");
174 }
175
176 void SetHUDSkin_Click(entity btn, entity me)
177 {
178         me.setHUDSkin(me);
179 }
180
181 void XonoticHUDSkinList_doubleClickListBoxItem(entity me, float i, vector where)
182 {
183         m_play_click_sound(MENU_SOUND_EXECUTE);
184         me.setHUDSkin(me);
185 }
186
187 float XonoticHUDSkinList_keyDown(entity me, float scan, float ascii, float shift)
188 {
189         if(scan == K_ENTER || scan == K_KP_ENTER)
190         {
191                 me.setHUDSkin(me);
192                 return 1;
193         }
194         else
195         {
196                 return SUPER(XonoticHUDSkinList).keyDown(me, scan, ascii, shift);
197         }
198 }
199 #endif
200