23 #include "playersdiagram.h" 25 #include "serverapi/playerslist.h" 26 #include "serverapi/server.h" 27 #include "datapaths.h" 29 #include "strings.hpp" 35 QImage PlayersDiagram::openImage;
36 QImage PlayersDiagram::openSpecImage;
37 QImage PlayersDiagram::botImage;
38 QImage PlayersDiagram::playerImage;
39 QImage PlayersDiagram::spectatorImage;
41 const QString PlayersDiagram::DEFAULT_STYLE =
"blocks";
42 QString PlayersDiagram::currentlyLoadedStyle;
44 PlayersDiagram::PlayersDiagram(ServerCPtr server)
47 if(openImage.isNull())
52 obtainPlayerNumbers();
56 PlayersDiagram::~PlayersDiagram()
60 QList<PlayersDiagramStyle> PlayersDiagram::availableSlotStyles()
62 QList<PlayersDiagramStyle> list;
69 QStringList knownNames;
70 foreach (QDir dir, stylePaths())
72 QStringList extraSlots = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
73 foreach (QString slotsName, extraSlots)
75 if (!knownNames.contains(slotsName, Qt::CaseInsensitive))
77 knownNames << slotsName;
88 QVector<QRgb> colors = image.colorTable();
89 QColor destinationColor = color.toHsv();
90 for(
int i = 0; i < colors.size(); ++i)
93 if(qRed(colors[i]) != 0 || qAlpha(colors[i]) == 0)
101 destinationColor.getHsv(&hue, &saturation, &value);
102 destinationColor.setHsv(hue, saturation, QColor(colors[i]).toHsv().value());
103 colors[i] = destinationColor.rgb();
105 image.setColorTable(colors);
110 void PlayersDiagram::draw()
113 if(server->numTotalSlots() == 0)
116 diagram = QPixmap(server->numTotalSlots() * playerImage.width(), playerImage.height());
117 diagram.fill(Qt::transparent);
119 slotSize = playerImage.width();
120 position = diagram.width() - slotSize;
121 painter =
new QPainter(&diagram);
123 for (
int team = 0; team < MAX_TEAMS; ++team)
125 drawTeam(Human, team, numHumansOnTeam[team]);
126 drawTeam(Bot, team, numBotsOnTeam[team]);
129 drawTeam(Human, Player::TEAM_NONE, numHumansWithoutTeam);
130 drawTeam(Bot, Player::TEAM_NONE, numBotsWithoutTeam);
132 if (numSpectators > 0)
134 drawPictures(spectatorImage, numSpectators);
137 if (numFreeJoinSlots > 0)
139 drawPictures(openImage, numFreeJoinSlots);
142 if (numFreeSpectatorSlots > 0)
144 drawPictures(openSpecImage, numFreeSpectatorSlots);
150 void PlayersDiagram::drawTeam(PlayerType playerType,
int team,
int howMany)
159 baseImage = botImage;
163 baseImage = playerImage;
167 gLog <<
"Error inside PlayersDiagram::drawTeam(): unknown PlayerType";
171 const QImage picture = colorizePlayer(baseImage, QColor(server->teamColor(team)));
172 drawPictures(picture, howMany);
176 void PlayersDiagram::drawPictures(
const QImage &image,
int howMany)
178 for (; howMany > 0; --howMany)
180 painter->drawImage(position, 0, image);
181 position -= slotSize;
185 bool PlayersDiagram::isNumericStyle(
const QString &style)
187 return style ==
"numeric";
192 if (style == currentlyLoadedStyle)
194 if (isNumericStyle(style))
197 openImage = loadImage(style,
"open");
198 openSpecImage = loadImage(style,
"specopen");
199 botImage = loadImage(style,
"bot");
200 playerImage = loadImage(style,
"player");
201 spectatorImage = loadImage(style,
"spectator");
202 currentlyLoadedStyle = style;
205 QImage PlayersDiagram::loadImage(
const QString &style,
const QString &name)
208 if (style != DEFAULT_STYLE)
210 QString resourcePath;
211 foreach(
const QString &dir, stylePaths())
220 image = QImage(
":/slots/" + DEFAULT_STYLE +
"/" + name);
225 void PlayersDiagram::obtainPlayerNumbers()
227 memset(numBotsOnTeam, 0,
sizeof(
int) * MAX_TEAMS);
228 memset(numHumansOnTeam, 0,
sizeof(
int) * MAX_TEAMS);
232 numBotsWithoutTeam = players.numBotsWithoutTeam();
233 numFreeJoinSlots = server->numFreeJoinSlots();
234 numFreeSpectatorSlots = server->numFreeSpectatorSlots();
235 numHumansWithoutTeam = players.numHumansWithoutTeam();
236 numSpectators = players.numSpectators();
238 for(
int i = 0; i < MAX_TEAMS; ++i)
240 numBotsOnTeam[i] = players.numBotsOnTeam(i);
241 numHumansOnTeam[i] = players.numHumansOnTeam(i);
245 QStringList PlayersDiagram::stylePaths()
247 return gDefaultDataPaths->staticDataSearchDirs(
"theme/slots");
static QString combinePaths(QString pathFront, QString pathEnd)
static void loadImages(const QString &style)
QImage colorizePlayer(QImage image, const QColor &color)