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