]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/playerlist.qc
Header police
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / playerlist.qc
1 #include "playerlist.qh"
2 #ifndef PLAYERLIST_H
3 #define PLAYERLIST_H
4 #include "listbox.qc"
5 CLASS(XonoticPlayerList, XonoticListBox)
6         ATTRIB(XonoticPlayerList, rowsPerItem, float, 1)
7         METHOD(XonoticPlayerList, resizeNotify, void(entity, vector, vector, vector, vector));
8         METHOD(XonoticPlayerList, drawListBoxItem, void(entity, int, vector, bool, bool));
9         ATTRIB(XonoticPlayerList, allowFocusSound, float, 0)
10         ATTRIB(XonoticPlayerList, realFontSize, vector, '0 0 0')
11         ATTRIB(XonoticPlayerList, columnNameOrigin, float, 0)
12         ATTRIB(XonoticPlayerList, columnNameSize, float, 0)
13         ATTRIB(XonoticPlayerList, columnScoreOrigin, float, 0)
14         ATTRIB(XonoticPlayerList, columnScoreSize, float, 0)
15         ATTRIB(XonoticPlayerList, realUpperMargin, float, 0)
16         ATTRIB(XonoticPlayerList, origin, vector, '0 0 0')
17         ATTRIB(XonoticPlayerList, itemAbsSize, vector, '0 0 0')
18         METHOD(XonoticPlayerList, setPlayerList, void(entity, string));
19         METHOD(XonoticPlayerList, getPlayerList, string(entity, float, float));
20         ATTRIB(XonoticPlayerList, playerList, float, -1)
21         ATTRIB(XonoticPlayerList, selectionDoesntMatter, bool, true)
22 ENDCLASS(XonoticPlayerList)
23 entity makeXonoticPlayerList();
24 #endif
25
26 #ifdef IMPLEMENTATION
27
28 const float PLAYERPARM_SCORE = 0;
29 const float PLAYERPARM_PING = 1;
30 const float PLAYERPARM_TEAM = 2;
31 const float PLAYERPARM_NAME = 3;
32 const float PLAYERPARM_COUNT = 4;
33
34 entity makeXonoticPlayerList()
35 {
36         entity me;
37         me = NEW(XonoticPlayerList);
38         me.configureXonoticListBox(me);
39         return me;
40 }
41
42 void XonoticPlayerList_setPlayerList(entity me, string plist)
43 {
44         int buf,i,n;
45         string s;
46
47         buf = buf_create();
48         me.nItems = tokenizebyseparator(plist, "\n");
49         for(i = 0; i < me.nItems; ++i)
50         {
51                 bufstr_set(buf, i * PLAYERPARM_COUNT + PLAYERPARM_NAME, argv(i)); // -666 100 "^4Nex ^2Player"
52         }
53
54         for(i = 0; i < me.nItems; ++i)
55         {
56                 s = bufstr_get(buf, i * PLAYERPARM_COUNT + PLAYERPARM_NAME);
57                 n = tokenize_console(s);
58
59                 if(n == 4)
60                 {
61                         bufstr_set(buf, i * PLAYERPARM_COUNT + PLAYERPARM_SCORE, argv(0)); // -666
62                         bufstr_set(buf, i * PLAYERPARM_COUNT + PLAYERPARM_PING,  argv(1)); // 100
63                         bufstr_set(buf, i * PLAYERPARM_COUNT + PLAYERPARM_TEAM,  argv(2)); // 0 for spec, else 1, 2, 3, 4
64                         bufstr_set(buf, i * PLAYERPARM_COUNT + PLAYERPARM_NAME,  argv(3)); // ^4Nex ^2Player
65                 }
66                 else
67                 {
68                         bufstr_set(buf, i * PLAYERPARM_COUNT + PLAYERPARM_SCORE, argv(0)); // -666
69                         bufstr_set(buf, i * PLAYERPARM_COUNT + PLAYERPARM_PING,  argv(1)); // 100
70                         bufstr_set(buf, i * PLAYERPARM_COUNT + PLAYERPARM_TEAM,  "-1");
71                         bufstr_set(buf, i * PLAYERPARM_COUNT + PLAYERPARM_NAME,  argv(2)); // ^4Nex ^2Player
72                 }
73         }
74         me.playerList = buf;
75 }
76
77 string XonoticPlayerList_getPlayerList(entity me, float i, float key)
78 {
79         return bufstr_get(me.playerList, i * PLAYERPARM_COUNT + key);
80 }
81
82 void XonoticPlayerList_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
83 {
84         me.itemAbsSize = '0 0 0';
85         SUPER(XonoticPlayerList).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
86
87         me.realFontSize_y = me.fontSize / (me.itemAbsSize_y = (absSize.y * me.itemHeight));
88         me.realFontSize_x = me.fontSize / (me.itemAbsSize_x = (absSize.x * (1 - me.controlWidth)));
89         me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
90
91         // this list does 1 char left and right margin
92         me.columnScoreSize = 5 * me.realFontSize.x;
93         me.columnNameSize = 1 - 3 * me.realFontSize.x - me.columnScoreSize;
94
95         me.columnNameOrigin = me.realFontSize.x;
96         me.columnScoreOrigin = me.columnNameOrigin + me.columnNameSize + me.realFontSize.x;
97 }
98
99 void XonoticPlayerList_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
100 {
101         string s;
102         string score;
103         float t;
104         vector rgb;
105
106         t = stof(me.getPlayerList(me, i, PLAYERPARM_TEAM));
107         if(t == 1)
108                 rgb = colormapPaletteColor(4, 0);
109         else if(t == 2)
110                 rgb = colormapPaletteColor(13, 0);
111         else if(t == 3)
112                 rgb = colormapPaletteColor(12, 0);
113         else if(t == 4)
114                 rgb = colormapPaletteColor(9, 0);
115         else
116                 rgb = SKINCOLOR_TEXT;
117
118         s = me.getPlayerList(me, i, PLAYERPARM_NAME);
119         score = me.getPlayerList(me, i, PLAYERPARM_SCORE);
120
121         if(substring(score, strlen(score) - 10, 10) == ":spectator")
122         {
123                 score = _("spectator");
124         }
125         else
126         {
127                 if((t = strstrofs(score, ":", 0)) >= 0)
128                         score = substring(score, 0, t);
129                 if((t = strstrofs(score, ",", 0)) >= 0)
130                         score = substring(score, 0, t);
131
132                 if(stof(score) == FRAGS_SPECTATOR)
133                         score = _("spectator");
134         }
135
136         s = draw_TextShortenToWidth(s, me.columnNameSize, 1, me.realFontSize);
137         draw_Text(me.realUpperMargin2 * eY + (me.columnNameOrigin + 0.00 * (me.columnNameSize - draw_TextWidth(s, 1, me.realFontSize))) * eX, s, me.realFontSize, '1 1 1', 1, 1);
138
139         score = draw_TextShortenToWidth(score, me.columnScoreSize, 0, me.realFontSize);
140         draw_Text(me.realUpperMargin2 * eY + (me.columnScoreOrigin + 1.00 * (me.columnScoreSize - draw_TextWidth(score, 1, me.realFontSize))) * eX, score, me.realFontSize, rgb, 1, 0);
141 }
142
143 #endif