Repository navigation
Expand file tree
/
Copy pathpawn_editor.cpp
More file actions
165 lines (140 loc) · 5.09 KB
/
Copy pathpawn_editor.cpp
File metadata and controls
165 lines (140 loc) · 5.09 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
#include "pawn_editor.h"
#include "ui_pawn_editor.h"
#include <QMessageBox>
#include <QPushButton>
// Default Constructor
pawn_editor::pawn_editor(QWidget *parent)
: QDialog(parent)
, ui(new Ui::pawn_editor)
{
ui->setupUi(this);
}
// Main Constructor
pawn_editor::pawn_editor(PathTrackerStruct* Data, PawnInstance* Pawn, QWidget *parent)
: QDialog(parent)
, ui(new Ui::pawn_editor)
, DataPtr(Data)
, PawnPtr(Pawn)
{
ui->setupUi(this);
if (!DataPtr || !PawnPtr) return;
// --- Fill the Combobox with all registered attributes ---
ui->attributeComboBox->clear();
for (const Attribute& attr : PathTrackerStruct::attributes().allAttributes()) {
ui->attributeComboBox->addItem(attr.displayName, attr.key);
}
// --- Setup Table Headers ---
ui->attributesTable->setColumnCount(3);
ui->attributesTable->setHorizontalHeaderLabels({"Attribute", "Base Value", ""});
ui->attributesTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
ui->attributesTable->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
ui->attributesTable->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Fixed);
ui->attributesTable->setColumnWidth(2, 30);
// --- Connect Signals ---
connect(ui->commitButton, &QPushButton::clicked, this, &pawn_editor::onCommitClicked);
connect(ui->attributesTable, &QTableWidget::itemChanged, this, &pawn_editor::onAttributeItemChanged);
// --- Initial Table Load ---
refreshAttributeTable();
}
pawn_editor::~pawn_editor()
{
delete ui;
}
// --- Set base value for the selected attribute ---
void pawn_editor::onCommitClicked()
{
if (!PawnPtr) return;
QString selectedKey = ui->attributeComboBox->currentData().toString();
int value = ui->valueSpinBox->value();
if (selectedKey.isEmpty()) return;
// Update baseValue directly; resolveAttributes() recalculates currentValue.
bool found = false;
for (AttributeValue& av : PawnPtr->attributes) {
if (av.key == selectedKey) {
av.baseValue = value;
found = true;
break;
}
}
if (!found) {
AttributeValue av;
av.key = selectedKey;
av.baseValue = value;
av.currentValue = value;
PawnPtr->attributes.append(av);
}
PawnPtr->resolveAttributes();
clampHp();
refreshAttributeTable();
ui->valueSpinBox->setValue(0);
}
// --- Rebuild the attribute table from the pawn's live data ---
void pawn_editor::refreshAttributeTable()
{
if (!PawnPtr) return;
m_refreshing = true;
ui->attributesTable->setRowCount(0);
for (const AttributeValue& attrVal : PawnPtr->attributes) {
int row = ui->attributesTable->rowCount();
ui->attributesTable->insertRow(row);
QString displayName = getAttributeNameFromID(attrVal.key);
QTableWidgetItem* nameItem = new QTableWidgetItem(displayName);
nameItem->setFlags(nameItem->flags() & ~Qt::ItemIsEditable);
nameItem->setData(Qt::UserRole, attrVal.key);
ui->attributesTable->setItem(row, 0, nameItem);
QTableWidgetItem* valItem = new QTableWidgetItem(QString::number(attrVal.baseValue));
ui->attributesTable->setItem(row, 1, valItem);
auto* deleteButton = new QPushButton("X");
deleteButton->setFixedWidth(30);
ui->attributesTable->setCellWidget(row, 2, deleteButton);
QString keyToDelete = attrVal.key;
connect(deleteButton, &QPushButton::clicked, this, [this, keyToDelete]() {
for (int i = 0; i < PawnPtr->attributes.size(); ++i) {
if (PawnPtr->attributes[i].key == keyToDelete) {
PawnPtr->attributes.removeAt(i);
break;
}
}
PawnPtr->resolveAttributes();
clampHp();
refreshAttributeTable();
});
}
m_refreshing = false;
}
// --- Clamp HP currentValue to its baseValue (max HP) ---
void pawn_editor::clampHp()
{
if (!PawnPtr) return;
for (AttributeValue& av : PawnPtr->attributes) {
if (av.key == HP_KEY && av.currentValue > av.baseValue)
av.currentValue = av.baseValue;
}
}
// --- Persist inline edits from the Base Value column ---
void pawn_editor::onAttributeItemChanged(QTableWidgetItem* item)
{
if (m_refreshing || !PawnPtr) return;
if (item->column() != 1) return;
QTableWidgetItem* nameItem = ui->attributesTable->item(item->row(), 0);
if (!nameItem) return;
QString key = nameItem->data(Qt::UserRole).toString();
bool ok = false;
int value = item->text().toInt(&ok);
if (!ok) return;
for (AttributeValue& av : PawnPtr->attributes) {
if (av.key == key) {
av.baseValue = value;
break;
}
}
PawnPtr->resolveAttributes();
clampHp();
}
// --- Resolve display name from attribute key ---
QString pawn_editor::getAttributeNameFromID(const QString& id)
{
if (PathTrackerStruct::attributes().hasAttribute(id))
return PathTrackerStruct::attributes().getAttribute(id).displayName;
return id;
}