-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPetGridWidget.cpp
98 lines (85 loc) · 3.04 KB
/
PetGridWidget.cpp
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
#include <QWidget>
#include <QScrollArea>
#include <QGridLayout>
#include <QLabel>
#include <QPixmap>
#include "Pet.hpp"
#include "PetGridWidget.hpp"
#include <QScroller>
#include "PetScreen.hpp"
#define ICON_SIZE 100
// #define GRID_LENGTH ((width()>0)?width()/ICON_SIZE:4)
#define GRID_LENGTH 4
PetGridWidget::PetGridWidget(QWidget *parent, Inventory *inventory) : QScrollArea(parent), inventory(inventory), spriteLabels(), petInfo(this,inventory)
{
QScroller::grabGesture(this, QScroller::TouchGesture);
this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// Create a grid layout to arrange the pet icons
QWidget *containerWidget = new QWidget(this);
layout = new QGridLayout(containerWidget);
layout->setSpacing(0);
layout->setContentsMargins(0, 0, 0, 0);
// Add the pet icons to the grid
updatePets();
containerWidget->setLayout(layout);
setWidgetResizable(true);
setWidget(containerWidget);
QTimer *sprite_timer = new QTimer(this);
connect(sprite_timer, &QTimer::timeout, this, &PetGridWidget::updatePets);
sprite_timer->start(100);
QVBoxLayout* layout = new QVBoxLayout(this);
layout->addWidget(&petInfo);
}
void PetGridWidget::updatePets()
{
// Update the sprite labels and add/remove sprite labels from the grid layout
inventory->mutex.lockForRead();
int numPets = inventory->user_list.size();
int numSpriteLabels = spriteLabels.size();
for (int i = 0; i < numPets; i++)
{
if (i >= numSpriteLabels)
{
// Add a new sprite label to the grid layout
spriteLabels.push_back(new PressLabel(this));
spriteLabels.back()->setAlignment(Qt::AlignCenter);
}
else
{
if (numPets != numSpriteLabels){
layout->removeWidget(spriteLabels[i]);
QObject::disconnect(spriteLabels[i], &PressLabel::clicked, this, nullptr);
}
}
// Create a label for the pet's sprite
// inventory->user_list[i].updateSprite();
spriteLabels[i]->setPixmap(inventory->user_list[i].getSprite().scaled(ICON_SIZE, ICON_SIZE, Qt::KeepAspectRatio));
if(numPets!=numSpriteLabels){
layout->addWidget(spriteLabels[i], i / GRID_LENGTH, i % GRID_LENGTH);
QObject::connect(spriteLabels[i], &PressLabel::clicked, this, [this, i]() {
showPetInfo(inventory->user_list[i],i);
});
}
}
inventory->mutex.unlock();
for (int i = numPets; i < numSpriteLabels; i++)
{
// Remove sprite labels from the grid layout
PressLabel *label = spriteLabels.back();
spriteLabels.pop_back();
delete label;
}
update();
}
void PetGridWidget::showPetInfo(const Pet& pet,unsigned i)
{
petInfo.setPet(pet,i);
// Show the pet info widget
petInfo.show();
}
PressLabel::PressLabel(QWidget* parent, Qt::WindowFlags f): QLabel(parent) {
}
void PressLabel::mousePressEvent(QMouseEvent* event) {
emit clicked();
}