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