]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/skinlist.qc
Header police
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / skinlist.qc
1 #include "skinlist.qh"
2 #ifndef SKINLIST_H
3 #define SKINLIST_H
4 #include "listbox.qc"
5 CLASS(XonoticSkinList, XonoticListBox)
6         METHOD(XonoticSkinList, configureXonoticSkinList, void(entity));
7         ATTRIB(XonoticSkinList, rowsPerItem, float, 4)
8         METHOD(XonoticSkinList, resizeNotify, void(entity, vector, vector, vector, vector));
9         METHOD(XonoticSkinList, drawListBoxItem, void(entity, int, vector, bool, bool));
10         METHOD(XonoticSkinList, getSkins, void(entity));
11         METHOD(XonoticSkinList, setSkin, void(entity));
12         METHOD(XonoticSkinList, loadCvars, void(entity));
13         METHOD(XonoticSkinList, saveCvars, void(entity));
14         METHOD(XonoticSkinList, skinParameter, string(entity, float, float));
15         METHOD(XonoticSkinList, doubleClickListBoxItem, void(entity, float, vector));
16         METHOD(XonoticSkinList, keyDown, float(entity, float, float, float));
17         METHOD(XonoticSkinList, destroy, void(entity));
18
19         ATTRIB(XonoticSkinList, skinlist, float, -1)
20         ATTRIB(XonoticSkinList, realFontSize, vector, '0 0 0')
21         ATTRIB(XonoticSkinList, columnPreviewOrigin, float, 0)
22         ATTRIB(XonoticSkinList, columnPreviewSize, float, 0)
23         ATTRIB(XonoticSkinList, columnNameOrigin, float, 0)
24         ATTRIB(XonoticSkinList, columnNameSize, float, 0)
25         ATTRIB(XonoticSkinList, realUpperMargin1, float, 0)
26         ATTRIB(XonoticSkinList, realUpperMargin2, float, 0)
27         ATTRIB(XonoticSkinList, origin, vector, '0 0 0')
28         ATTRIB(XonoticSkinList, itemAbsSize, vector, '0 0 0')
29
30         ATTRIB(XonoticSkinList, name, string, "skinselector")
31 ENDCLASS(XonoticSkinList)
32
33 entity makeXonoticSkinList();
34 void SetSkin_Click(entity btn, entity me);
35 #endif
36
37 #ifdef IMPLEMENTATION
38
39 const float SKINPARM_NAME = 0;
40 const float SKINPARM_TITLE = 1;
41 const float SKINPARM_AUTHOR = 2;
42 const float SKINPARM_PREVIEW = 3;
43 const float SKINPARM_COUNT = 4;
44
45 entity makeXonoticSkinList()
46 {
47         entity me;
48         me = NEW(XonoticSkinList);
49         me.configureXonoticSkinList(me);
50         return me;
51 }
52
53 void XonoticSkinList_configureXonoticSkinList(entity me)
54 {
55         me.configureXonoticListBox(me);
56         me.getSkins(me);
57         me.loadCvars(me);
58 }
59
60 void XonoticSkinList_loadCvars(entity me)
61 {
62         string s;
63         float i, n;
64         s = cvar_string("menu_skin");
65         n = me.nItems;
66         for(i = 0; i < n; ++i)
67         {
68                 if(me.skinParameter(me, i, SKINPARM_NAME) == s)
69                 {
70                         me.setSelected(me, i);
71                         break;
72                 }
73         }
74 }
75
76 void XonoticSkinList_saveCvars(entity me)
77 {
78         cvar_set("menu_skin", me.skinParameter(me, me.selectedItem, SKINPARM_NAME));
79 }
80
81 string XonoticSkinList_skinParameter(entity me, float i, float key)
82 {
83         return bufstr_get(me.skinlist, i * SKINPARM_COUNT + key);
84 }
85
86 void XonoticSkinList_getSkins(entity me)
87 {
88         float glob, buf, i, n, fh;
89         string s, name;
90
91         buf = buf_create();
92         glob = search_begin("gfx/menu/*/skinvalues.txt", true, true);
93         if(glob < 0)
94         {
95                 me.skinlist = buf;
96                 me.nItems = 0;
97                 return;
98         }
99
100         n = search_getsize(glob);
101         for(i = 0; i < n; ++i)
102         {
103                 s = search_getfilename(glob, i);
104                 name = substring(s, 9, strlen(s) - 24); // the * part
105                 bufstr_set(buf, i * SKINPARM_COUNT + SKINPARM_NAME, name);
106                 bufstr_set(buf, i * SKINPARM_COUNT + SKINPARM_TITLE, _("<TITLE>"));
107                 bufstr_set(buf, i * SKINPARM_COUNT + SKINPARM_AUTHOR, _("<AUTHOR>"));
108                 if(draw_PictureSize(strcat("/gfx/menu/", substring(s, 9, strlen(s) - 24), "/skinpreview")) == '0 0 0')
109                         bufstr_set(buf, i * SKINPARM_COUNT + SKINPARM_PREVIEW, "nopreview_menuskin");
110                 else
111                         bufstr_set(buf, i * SKINPARM_COUNT + SKINPARM_PREVIEW, strcat("/gfx/menu/", substring(s, 9, strlen(s) - 24), "/skinpreview"));
112                 fh = fopen(s, FILE_READ);
113                 if(fh < 0)
114                 {
115                         LOG_INFO("Warning: can't open skinvalues.txt file\n");
116                         continue;
117                 }
118                 while((s = fgets(fh)))
119                 {
120                         // these two are handled by skinlist.qc
121                         if(substring(s, 0, 6) == "title ")
122                         {
123                                 if (name == cvar_defstring("menu_skin"))
124                                         bufstr_set(buf, i * SKINPARM_COUNT + SKINPARM_TITLE, strcat(substring(s, 6, strlen(s) - 6), " (", _("Default"), ")"));
125                                 else
126                                         bufstr_set(buf, i * SKINPARM_COUNT + SKINPARM_TITLE, substring(s, 6, strlen(s) - 6));
127                         }
128                         else if(substring(s, 0, 7) == "author ")
129                                 bufstr_set(buf, i * SKINPARM_COUNT + SKINPARM_AUTHOR, substring(s, 7, strlen(s) - 7));
130                 }
131                 fclose(fh);
132         }
133
134         search_end(glob);
135
136         me.skinlist = buf;
137         me.nItems = n;
138 }
139
140 void XonoticSkinList_destroy(entity me)
141 {
142         buf_del(me.skinlist);
143 }
144
145 void XonoticSkinList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
146 {
147         me.itemAbsSize = '0 0 0';
148         SUPER(XonoticSkinList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
149
150         me.realFontSize_y = me.fontSize / (me.itemAbsSize_y = (absSize.y * me.itemHeight));
151         me.realFontSize_x = me.fontSize / (me.itemAbsSize_x = (absSize.x * (1 - me.controlWidth)));
152         me.realUpperMargin1 = 0.5 * (1 - 2.5 * me.realFontSize.y);
153         me.realUpperMargin2 = me.realUpperMargin1 + 1.5 * me.realFontSize.y;
154
155         me.columnPreviewOrigin = 0;
156         me.columnPreviewSize = me.itemAbsSize.y / me.itemAbsSize.x * 4 / 3;
157         me.columnNameOrigin = me.columnPreviewOrigin + me.columnPreviewSize + me.realFontSize.x;
158         me.columnNameSize = 1 - me.columnPreviewSize - 2 * me.realFontSize.x;
159 }
160
161 void XonoticSkinList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
162 {
163         string s;
164
165         if(isSelected)
166                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
167         else if(isFocused)
168         {
169                 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
170                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
171         }
172
173         s = me.skinParameter(me, i, SKINPARM_PREVIEW);
174         draw_Picture(me.columnPreviewOrigin * eX, s, me.columnPreviewSize * eX + eY, '1 1 1', 1);
175
176         s = me.skinParameter(me, i, SKINPARM_TITLE);
177         s = draw_TextShortenToWidth(s, me.columnNameSize, 0, me.realFontSize);
178         draw_Text(me.realUpperMargin1 * eY + (me.columnNameOrigin + 0.00 * (me.columnNameSize - draw_TextWidth(s, 0, me.realFontSize))) * eX, s, me.realFontSize, SKINCOLOR_SKINLIST_TITLE, SKINALPHA_TEXT, 0);
179
180         s = me.skinParameter(me, i, SKINPARM_AUTHOR);
181         s = draw_TextShortenToWidth(s, me.columnNameSize, 0, me.realFontSize);
182         draw_Text(me.realUpperMargin2 * eY + (me.columnNameOrigin + 1.00 * (me.columnNameSize - draw_TextWidth(s, 0, me.realFontSize))) * eX, s, me.realFontSize, SKINCOLOR_SKINLIST_AUTHOR, SKINALPHA_TEXT, 0);
183 }
184
185 void XonoticSkinList_setSkin(entity me)
186 {
187         me.saveCvars(me);
188         localcmd("\nmenu_restart\nmenu_cmd skinselect\n");
189 }
190
191 void SetSkin_Click(entity btn, entity me)
192 {
193         me.setSkin(me);
194 }
195
196 void XonoticSkinList_doubleClickListBoxItem(entity me, float i, vector where)
197 {
198         m_play_click_sound(MENU_SOUND_EXECUTE);
199         me.setSkin(me);
200 }
201
202 float XonoticSkinList_keyDown(entity me, float scan, float ascii, float shift)
203 {
204         if(scan == K_ENTER || scan == K_KP_ENTER)
205         {
206                 m_play_click_sound(MENU_SOUND_EXECUTE);
207                 me.setSkin(me);
208                 return 1;
209         }
210         else
211                 return SUPER(XonoticSkinList).keyDown(me, scan, ascii, shift);
212 }
213 #endif