Repository navigation
Expand file tree
/
Copy pathpawnrow.cpp
More file actions
166 lines (142 loc) · 4.9 KB
/
Copy pathpawnrow.cpp
File metadata and controls
166 lines (142 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "pawnrow.h"
#include "ui_pawnrow.h"
#include "PathTrackerStruct.h"
#include "PictureHelper.h"
#include <QPixmap>
#include <QFileInfo>
#include <QLabel>
PawnRow::PawnRow(QWidget *parent)
: QWidget(parent)
, ui(new Ui::PawnRow)
{
ui->setupUi(this);
// Required for widget-level background-color QSS to paint on a custom widget
setAttribute(Qt::WA_StyledBackground, true);
}
PawnRow::~PawnRow()
{
delete ui;
}
void PawnRow::SetData(PawnInstance* PawnData, int initiative)
{
ui->PriorityLabel->setText(QString::number(initiative));
ui->NameLabel->setText(PawnData->displayName);
setGraphicsEffect(nullptr);
setStyleSheet("");
// Portrait
QString portraitPath;
if (Player* player = dynamic_cast<Player*>(PawnData))
{
portraitPath = player->PictureFilePath;
}
else if (!PawnData->templateId.isEmpty())
{
PawnTemplate* tmpl = PathTrackerStruct::instance().findPawnTemplate(PawnData->templateId);
if (tmpl) portraitPath = tmpl->PictureFilePath;
}
if (!portraitPath.isEmpty())
{
QPixmap pixmap(ResolvePicturePath(portraitPath));
if (!pixmap.isNull())
ui->PortraitLabel->setPixmap(pixmap);
else
ui->PortraitLabel->clear();
}
else
{
ui->PortraitLabel->clear();
}
// Effect icons
QLayoutItem* layoutItem;
while ((layoutItem = ui->IconContainer->takeAt(0)) != nullptr)
{
delete layoutItem->widget();
delete layoutItem;
}
for (const auto& effectInstance : PawnData->effects)
{
EffectTemplate* effectTemplate = PathTrackerStruct::instance().findEffectTemplate(effectInstance->templateId);
if (!effectTemplate || effectTemplate->PictureFilePath.isEmpty()) continue;
QPixmap iconPixmap(ResolvePicturePath(effectTemplate->PictureFilePath));
if (iconPixmap.isNull()) continue;
auto* iconLabel = new QLabel(this);
iconLabel->setFixedSize(24, 24);
iconLabel->setScaledContents(true);
iconLabel->setPixmap(iconPixmap);
ui->IconContainer->addWidget(iconLabel);
}
ui->IconContainer->addStretch();
// HP progress bar + numeric HP/AC labels
int maxHp = 0;
for (const AttributeValue& av : PawnData->attributes)
if (av.key == HP_KEY) { maxHp = av.baseValue; break; }
int currentHp = PawnData->getAttribute(HP_KEY);
if (maxHp > 0) {
ui->progressBar->setMaximum(maxHp);
ui->progressBar->setValue(qMax(0, currentHp));
ui->progressBar->show();
ui->HpLabel->setText(QString("HP %1/%2").arg(currentHp).arg(maxHp));
ui->HpLabel->show();
} else {
ui->progressBar->hide();
ui->HpLabel->hide();
}
int ac = PawnData->getAttribute(AC_KEY);
if (ac != 0) {
ui->AcLabel->setText(QString("AC %1").arg(ac));
ui->AcLabel->show();
} else {
ui->AcLabel->hide();
}
m_pawnData = PawnData;
m_initiative = initiative;
ApplyStylesheet(false);
}
void PawnRow::RefreshIcons()
{
if (!m_pawnData) return;
QLayoutItem* layoutItem;
while ((layoutItem = ui->IconContainer->takeAt(0)) != nullptr)
{
delete layoutItem->widget();
delete layoutItem;
}
for (const auto& effectInstance : m_pawnData->effects)
{
EffectTemplate* effectTemplate = PathTrackerStruct::instance().findEffectTemplate(effectInstance->templateId);
if (!effectTemplate || effectTemplate->PictureFilePath.isEmpty()) continue;
QPixmap iconPixmap(ResolvePicturePath(effectTemplate->PictureFilePath));
if (iconPixmap.isNull()) continue;
auto* iconLabel = new QLabel(this);
iconLabel->setFixedSize(24, 24);
iconLabel->setScaledContents(true);
iconLabel->setPixmap(iconPixmap);
ui->IconContainer->addWidget(iconLabel);
}
ui->IconContainer->addStretch();
}
void PawnRow::ApplyStylesheet(bool selected)
{
if (!m_pawnData) return;
const AppTheme& t = PathTrackerStruct::instance().theme();
QColor bg;
switch (m_pawnData->state) {
case PawnState::Active: bg = t.pawnActiveColor; break;
case PawnState::Spent: bg = t.pawnSpentColor; break;
case PawnState::Unconscious: bg = t.pawnUnconsciousColor; break;
case PawnState::Dead: bg = t.pawnDeadColor; break;
default: bg = t.pawnWaitingColor; break;
}
QString border = selected
? QString("border-left: 4px solid %1;").arg(t.accentColor.name())
: "border-left: none;";
setStyleSheet(QString(
"PawnRow { background-color: %1; %2 }"
"PawnRow QLabel { background-color: transparent; color: %3; }"
"PawnRow QLabel#PortraitLabel { border: 2px solid %4; border-radius: 2px; }"
).arg(bg.name(), border, t.textColor.name(), t.accentColor.name()));
}
void PawnRow::SetSelected(bool selected)
{
ApplyStylesheet(selected);
}