-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventoryUI.cpp
More file actions
433 lines (366 loc) · 14.9 KB
/
Copy pathInventoryUI.cpp
File metadata and controls
433 lines (366 loc) · 14.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include "InventoryUI.h"
#include <QDebug>
#include <QContextMenuEvent>
#include <QTimer>
#include <QDrag>
#include <QMimeData>
#include <QApplication>
#include <QFileInfo>
#include <QMenu>
#include <QLabel>
#include <QToolTip>
#include <QScreen>
#include "Equipment.h"
#include "Armor.h"
InventoryUI::InventoryUI(QWidget *parent) : QWidget(parent)
{
setWindowTitle("物品栏");
setFixedSize(800, 600);
setAcceptDrops(true);
// 将窗口移动到屏幕中央,并向上调整300像素
QRect screenGeometry = QApplication::primaryScreen()->geometry();
move((screenGeometry.width() - width()) / 2, (screenGeometry.height() - height()) / 2 - 300);
m_layout = new QGridLayout(this);
createInventorySlots();
}
void InventoryUI::createInventorySlots()
{
for (int row = 0; row < INVENTORY_ROWS; ++row)
{
for (int col = 0; col < INVENTORY_COLS; ++col)
{
QPushButton* slot = new QPushButton(this);
slot->setFixedSize(60, 60);
slot->setStyleSheet(
"QPushButton { background-color: #444444; border: 2px solid #666666; }"
"QPushButton:hover { border-color: #888888; background-color: #555555; }"
"QPushButton { icon-size: 40px 40px; }"
"QPushButton { text-align: right; padding-right: 5px; padding-bottom: 2px; color: white; font-weight: bold; }"
);
slot->setProperty("slotIndex", row * INVENTORY_COLS + col);
m_layout->addWidget(slot, row + 2, col);
m_slots.append(slot);
connect(slot, &QPushButton::clicked, this, &InventoryUI::onSlotClicked);
// 启用鼠标追踪以支持悬停效果显示工具提示
slot->setMouseTracking(true);
}
}
}
void InventoryUI::updateInventory(const QList<Item*>& items, const QList<Equipment*>&)
{
// 创建一个映射来存储相同类型物品的数量
QMap<QString, QPair<Item*, int>> itemStacks;
QMap<QString, int> itemSlots;
// 首先统计所有相同类型的物品
for (Item* item : items)
{
if (item)
{
QString itemKey = item->getName();
// 如果已经存在相同类型的物品,累加数量
if (!itemStacks.contains(itemKey)) {
// 如果是新物品类型,直接添加
itemStacks[itemKey] = qMakePair(item, item->getCount());
} else {
// 如果已经存在相同类型的物品,累加数量
itemStacks[itemKey].second += item->getCount();
}
itemSlots[itemKey] = -1;
}
}
// 清空所有槽位
for (int i = 0; i < m_slots.size(); ++i) {
m_slots[i]->setText("");
m_slots[i]->setIcon(QIcon());
m_slots[i]->setToolTip("");
m_slots[i]->setProperty("item", QVariant());// 清空物品指针属性
}
// 更新物品栏,将相同类型的物品堆叠在一起
int currentSlot = 0;
for (auto it = itemStacks.begin(); it != itemStacks.end(); ++it)
{
if (currentSlot < m_slots.size())
{
Item* item = it.value().first;
int count = it.value().second;
item->setCount(count); // 更新物品数量
updateSlot(currentSlot, item);
itemSlots[it.key()] = currentSlot;
currentSlot++;
}
}
}
void InventoryUI::updateSlot(int index, Item* item)
{
if (index >= 0 && index < m_slots.size() && item)
{
QPushButton* slot = m_slots[index];
// 存储物品指针到按钮属性中,以便后续访问
slot->setProperty("item", QVariant::fromValue(static_cast<void*>(item)));
// 设置图标
QString iconPath = item->getIconPath();
qDebug() << "原始图片路径:" << iconPath;
if (!iconPath.isEmpty())
{
// 检查图标路径是否存在,如果不存在尝试修正
QFileInfo fileInfo(iconPath);
if (!fileInfo.exists())
{
// 尝试不同的路径组合
QStringList pathsToTry =
{
QString("img/Item/%1").arg(iconPath),
QString(":/img/Item/%1").arg(iconPath),
QString(":/Item/%1").arg(iconPath),
iconPath
};
for (const QString& path : pathsToTry)
{
qDebug() << "尝试加载图片路径:" << path;
QFileInfo tryInfo(path);
if (tryInfo.exists() || QFile::exists(":" + path))
{
iconPath = path;
qDebug() << "成功找到图片路径:" << iconPath;
break;
}
}
}
QIcon icon(iconPath);
if (!icon.isNull())
{
slot->setIcon(icon);
slot->setIconSize(QSize(60, 60));
// 显示物品数量
if (item->getCount() > 1)
{
slot->setText(QString::number(item->getCount()));
}
else
{
slot->setText("");
}
}
else
{
qDebug() << "无法加载图标:" << iconPath;
// 如果图标加载失败,显示物品名称
slot->setText(item->getName());
}
}
else
{
// 如果没有图标,显示物品名称和数量
if (item->getCount() > 1)
{
slot->setText(QString("%1 x%2").arg(item->getName()).arg(item->getCount()));
}
else
{
slot->setText(item->getName());
}
}
// 设置工具提示显示物品详细信息
QString tooltip = createDetailedTooltip(item);
slot->setToolTip(tooltip);
// 启用鼠标追踪以支持悬停效果
slot->setMouseTracking(true);
}
}
QString InventoryUI::createDetailedTooltip(Item* item)
{
if (!item) return "";
QString tooltip = QString("<div style='background-color: #333333; color: white; padding: 5px; border: 1px solid #555555;'>");
// 物品名称 (使用不同颜色区分不同类型)
QString nameColor;
switch (item->getType()) {
case Item::WEAPON: nameColor = "#ff9900"; break; // 橙色
case Item::ARMOR: nameColor = "#3399ff"; break; // 蓝色
case Item::MATERIAL: nameColor = "#99cc33"; break; // 绿色
case Item::KEY_ITEM: nameColor = "#cc33ff"; break; // 紫色
default: nameColor = "white"; break;
}
tooltip += QString("<div style='font-weight: bold; color: %1; font-size: 12px;'>%2</div>").arg(nameColor).arg(item->getName());
// 物品类型
tooltip += QString("<div style='color: #aaaaaa; font-size: 10px;'>类型: %1</div>").arg(item->getTypeString());
// 物品描述
tooltip += QString("<div style='margin-top: 5px;'>%1</div>").arg(item->getDescription());
// 堆叠数量
if (item->canStack()) {
tooltip += QString("<div style='margin-top: 5px;'>数量: %1/%2</div>").arg(item->getCount()).arg(item->getStackLimit());
}
// 特殊属性 (根据物品类型显示不同信息)
if (item->getType() == Item::WEAPON) {
// 尝试将物品转换为Equipment类型以获取属性修饰符
if (Equipment* weapon = dynamic_cast<Equipment*>(item)) {
QMap<QString, int> modifiers = weapon->getAttributeModifiers();
if (modifiers.contains("attackPower")) {
tooltip += QString("<div style='color: #ff6666;'>伤害: +%1</div>").arg(modifiers["attackPower"]);
}
}
}
else if (item->getType() == Item::ARMOR) {
// 尝试将物品转换为Armor类型以获取防御值
if (Armor* armor = dynamic_cast<Armor*>(item)) {
tooltip += QString("<div style='color: #6699ff;'>防御: +%1</div>").arg(armor->getDefense());
}
}
// 是否可消耗
if (item->isConsumable()) {
tooltip += "<div style='color: #aaaaaa; font-style: italic; margin-top: 5px;'>使用后消耗</div>";
}
tooltip += "</div>";
return tooltip;
}
void InventoryUI::onSlotRightClicked()
{
// 右键点击不再显示菜单,保留函数以便将来可能的扩展
// 物品信息通过鼠标悬停时的工具提示(tooltip)显示
}
void InventoryUI::onSlotClicked()
{
// 获取发送信号的按钮
QPushButton* button = qobject_cast<QPushButton*>(sender());
if (!button) return;
// 获取按钮对应的物品栏位置
int slotIndex = button->property("slotIndex").toInt();
// 只处理中键点击操作,右键点击不再触发任何操作
if (QApplication::mouseButtons() & Qt::MiddleButton)
{
// 中键点击:丢弃物品
emit itemDropped(slotIndex);
}
// 右键点击不做任何处理,鼠标悬停时会自动显示工具提示
}
void InventoryUI::dragEnterEvent(QDragEnterEvent* event)
{
if (event->mimeData()->hasFormat("application/x-item")) {
event->acceptProposedAction();
}
}
void InventoryUI::mousePressEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton) {
QPushButton* slot = qobject_cast<QPushButton*>(childAt(event->pos()));
if (slot && m_slots.contains(slot)) {
// 只有当槽位中有物品时才记录拖拽起始位置
Item* item = static_cast<Item*>(slot->property("item").value<void*>());
if (item)
{
m_dragStartPosition = event->pos();
}
}
}
}
void InventoryUI::mouseMoveEvent(QMouseEvent* event)
{
if (!(event->buttons() & Qt::LeftButton)) {
return;
}
if ((event->pos() - m_dragStartPosition).manhattanLength() < QApplication::startDragDistance()) {
return;
}
QPushButton* sourceSlot = qobject_cast<QPushButton*>(childAt(m_dragStartPosition));
if (!sourceSlot || !m_slots.contains(sourceSlot)) {
return;
}
int slotIndex = sourceSlot->property("slotIndex").toInt();
// 获取物品指针
Item* item = static_cast<Item*>(sourceSlot->property("item").value<void*>());
if (!item)
{
return; // 如果槽位没有物品,不创建拖拽
}
QDrag* drag = new QDrag(this);
QMimeData* mimeData = new QMimeData;
QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
dataStream << slotIndex;
mimeData->setData("application/x-item", itemData);
// 添加物品类型信息
QByteArray typeData;
QDataStream typeStream(&typeData, QIODevice::WriteOnly);
typeStream << item->getType();
mimeData->setData("application/x-item-type", typeData);
// 添加物品名称信息
QByteArray nameData;
QDataStream nameStream(&nameData, QIODevice::WriteOnly);
nameStream << item->getName();
mimeData->setData("application/x-item-name", nameData);
drag->setMimeData(mimeData);
// 设置拖动时的图标
QIcon icon = sourceSlot->icon();
if (!icon.isNull())
{
QPixmap pixmap = icon.pixmap(50, 50);
if (!pixmap.isNull()) {
drag->setPixmap(pixmap);
drag->setHotSpot(QPoint(25, 25));
}
}
// 输出调试信息
qDebug() << "物品栏开始拖动,起始位置:" << m_dragStartPosition << "物品:" << item->getName();
Qt::DropActions supportedActions = Qt::MoveAction;
Qt::DropAction result = drag->exec(supportedActions, Qt::MoveAction);
if (result == Qt::MoveAction)
{
// 如果拖放成功,清空源槽位
sourceSlot->setIcon(QIcon());
sourceSlot->setText("");
sourceSlot->setProperty("item", QVariant());
sourceSlot->setToolTip("");
}
}
void InventoryUI::dropEvent(QDropEvent* event)
{
const QMimeData* mimeData = event->mimeData();
if (mimeData->hasFormat("application/x-item") || mimeData->hasFormat("application/x-crafting-item")) {
QPushButton* targetSlot = qobject_cast<QPushButton*>(childAt(event->pos()));
if (!targetSlot || !m_slots.contains(targetSlot)) {
event->ignore();
return;
}
int targetIndex = targetSlot->property("slotIndex").toInt();
if (mimeData->hasFormat("application/x-item")) {
QByteArray itemData = mimeData->data("application/x-item");
QDataStream dataStream(&itemData, QIODevice::ReadOnly);
int sourceIndex;
dataStream >> sourceIndex;
// 获取源槽位和目标槽位的物品指针
QPushButton* sourceSlot = m_slots[sourceIndex];
Item* sourceItem = static_cast<Item*>(sourceSlot->property("item").value<void*>());
Item* targetItem = static_cast<Item*>(targetSlot->property("item").value<void*>());
// 交换物品
sourceSlot->setProperty("item", QVariant::fromValue(static_cast<void*>(targetItem)));
targetSlot->setProperty("item", QVariant::fromValue(static_cast<void*>(sourceItem)));
// 更新显示
if (sourceItem) updateSlot(targetIndex, sourceItem);
if (targetItem) updateSlot(sourceIndex, targetItem);
event->acceptProposedAction();
}
}
}
void InventoryUI::setupSlotMenu(QPushButton* slot, Item* item)
{
if (!item) return;
// 注意:我们不再在这里创建菜单,而是在右键点击时动态创建
// 这样可以避免内存泄漏和菜单显示问题
}
// 添加箱子物品到物品栏
void InventoryUI::addItemsFromChest(const QList<Item*>& chestItems)
{
if (chestItems.isEmpty()) {
return;
}
// 发出信号通知游戏逻辑处理箱子物品的添加
emit chestItemsReceived(chestItems);
// 显示一个简单的提示
QLabel* notification = new QLabel("获得了箱子中的物品!", this);
notification->setStyleSheet("background-color: rgba(0, 0, 0, 150); color: white; padding: 10px; border-radius: 5px;");
notification->setAlignment(Qt::AlignCenter);
notification->setFixedSize(200, 50);
notification->move((width() - notification->width()) / 2, height() - notification->height() - 20);
notification->show();
// 2秒后自动隐藏提示
QTimer::singleShot(2000, notification, &QLabel::deleteLater);
}