23 #include "playersdiagram.h"
25 #include "datapaths.h"
27 #include "serverapi/playerslist.h"
28 #include "serverapi/server.h"
29 #include "strings.hpp"
36 QImage PlayersDiagram::openImage;
37 QImage PlayersDiagram::openSpecImage;
38 QImage PlayersDiagram::botImage;
39 QImage PlayersDiagram::playerImage;
40 QImage PlayersDiagram::spectatorImage;
42 const QString PlayersDiagram::DEFAULT_STYLE =
"blocks";
43 QString PlayersDiagram::currentlyLoadedStyle;
45 PlayersDiagram::PlayersDiagram(ServerCPtr server)
46 : server(std::move(server))
48 if (openImage.isNull())
51 obtainPlayerNumbers();
55 PlayersDiagram::~PlayersDiagram()
59 QList<PlayersDiagramStyle> PlayersDiagram::availableSlotStyles()
61 QList<PlayersDiagramStyle> list;
68 QStringList knownNames;
69 for (QDir dir : stylePaths())
71 QStringList extraSlots = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
72 for (QString slotsName : extraSlots)
74 if (!knownNames.contains(slotsName, Qt::CaseInsensitive))
76 knownNames << slotsName;
87 QVector<QRgb> colors = image.colorTable();
88 QColor destinationColor = color.toHsv();
89 for (QRgb &color : colors)
92 if (qRed(color) != 0 || qAlpha(color) == 0)
98 destinationColor.getHsv(&hue, &saturation, &value);
99 destinationColor.setHsv(hue, saturation, QColor(color).toHsv().value());
100 color = destinationColor.rgb();
102 image.setColorTable(colors);
107 void PlayersDiagram::draw()
110 if (server->numTotalSlots() == 0)
113 diagram = QPixmap(server->numTotalSlots() * playerImage.width(), playerImage.height());
114 diagram.fill(Qt::transparent);
116 slotSize = playerImage.width();
117 position = diagram.width() - slotSize;
118 painter =
new QPainter(&diagram);
120 for (
int team = 0; team < MAX_TEAMS; ++team)
122 drawTeam(Human, team, numHumansOnTeam[team]);
123 drawTeam(Bot, team, numBotsOnTeam[team]);
126 drawTeam(Human, Player::TEAM_NONE, numHumansWithoutTeam);
127 drawTeam(Bot, Player::TEAM_NONE, numBotsWithoutTeam);
129 if (numSpectators > 0)
130 drawPictures(spectatorImage, numSpectators);
132 if (numFreeJoinSlots > 0)
133 drawPictures(openImage, numFreeJoinSlots);
135 if (numFreeSpectatorSlots > 0)
136 drawPictures(openSpecImage, numFreeSpectatorSlots);
141 void PlayersDiagram::drawTeam(PlayerType playerType,
int team,
int howMany)
150 baseImage = botImage;
154 baseImage = playerImage;
158 gLog <<
"Error inside PlayersDiagram::drawTeam(): unknown PlayerType";
162 const QImage picture =
colorizePlayer(baseImage, QColor(server->teamColor(team)));
163 drawPictures(picture, howMany);
167 void PlayersDiagram::drawPictures(
const QImage &image,
int howMany)
169 for (; howMany > 0; --howMany)
171 painter->drawImage(position, 0, image);
172 position -= slotSize;
176 bool PlayersDiagram::isNumericStyle(
const QString &style)
178 return style ==
"numeric";
183 if (style == currentlyLoadedStyle)
185 if (isNumericStyle(style))
188 openImage = loadImage(style,
"open");
189 openSpecImage = loadImage(style,
"specopen");
190 botImage = loadImage(style,
"bot");
191 playerImage = loadImage(style,
"player");
192 spectatorImage = loadImage(style,
"spectator");
193 currentlyLoadedStyle = style;
196 QImage PlayersDiagram::loadImage(
const QString &style,
const QString &name)
199 if (style != DEFAULT_STYLE)
201 QString resourcePath;
202 for (
const QString &dir : stylePaths())
210 image = QImage(
":/slots/" + DEFAULT_STYLE +
"/" + name);
214 void PlayersDiagram::obtainPlayerNumbers()
216 memset(numBotsOnTeam, 0,
sizeof(
int) * MAX_TEAMS);
217 memset(numHumansOnTeam, 0,
sizeof(
int) * MAX_TEAMS);
221 numBotsWithoutTeam = players.numBotsWithoutTeam();
222 numFreeJoinSlots = server->numFreeJoinSlots();
223 numFreeSpectatorSlots = server->numFreeSpectatorSlots();
224 numHumansWithoutTeam = players.numHumansWithoutTeam();
225 numSpectators = players.numSpectators();
227 for (
int i = 0; i < MAX_TEAMS; ++i)
229 numBotsOnTeam[i] = players.numBotsOnTeam(i);
230 numHumansOnTeam[i] = players.numHumansOnTeam(i);
234 QStringList PlayersDiagram::stylePaths()
236 return gDefaultDataPaths->staticDataSearchDirs(
"theme/slots");