-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphicsspriteitem.h
166 lines (137 loc) · 3.76 KB
/
graphicsspriteitem.h
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
/*
* Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
* Joshua Henderson <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef GRAPHICSSPRITEITEM_H
#define GRAPHICSSPRITEITEM_H
#include "planemanager.h"
#include "graphicsplaneitem.h"
#include <QObject>
#include <QGraphicsItem>
#include <QPixmap>
#include <QPainter>
#include <QDebug>
#include <QGraphicsSceneMouseEvent>
#include <string>
#include <map>
/**
* @brief The GraphicsSpriteItem class
*
* An enhanced GraphicsPlaneItem that specifically handles sprite sheets
* and animating any number of sequences in those sprite sheets using the
* hardware plane pan functionality.
*/
class GraphicsSpriteItem : public GraphicsPlaneItem
{
Q_OBJECT
Q_PROPERTY(QPointF pos READ pos WRITE setPos)
Q_PROPERTY(int frame READ currentFrame WRITE setFrame)
public:
GraphicsSpriteItem(struct plane_data* plane, const QPixmap &image, int width, int height)
: GraphicsPlaneItem(plane, QRectF(0, 0, width, height)),
m_image(image),
m_frame(0),
m_flipHorizontal(false),
m_flipVertical(false)
{}
struct sequence
{
int m_x;
int m_y;
int m_width;
int m_height;
int m_count;
};
virtual void addSequence(const std::string& name, int x, int y, int width, int height, int count)
{
m_sequences[name] = {x,y,width,height,count};
if (m_sequences.size() == 1)
{
m_sequence = name;
plane_set_pan_pos(m_plane, x, y);
plane_set_pan_size(m_plane, width, height);
plane_apply(m_plane);
}
}
virtual void setSequence(const std::string& name)
{
if (m_sequence != name)
{
m_sequence = name;
m_frame = 0;
}
}
inline int currentFrame() const
{
return m_frame;
}
inline int frameCount()
{
return m_sequences[m_sequence].m_count;
}
inline int frameCount(const std::string& name)
{
return m_sequences[name].m_count;
}
inline int width()
{
return m_sequences[m_sequence].m_width;
}
inline int width(const std::string& name)
{
return m_sequences[name].m_width;
}
inline int height()
{
return m_sequences[m_sequence].m_height;
}
inline int height(const std::string& name)
{
return m_sequences[name].m_height;
}
inline void toggleFlipHorizontal()
{
m_flipHorizontal = !m_flipHorizontal;
}
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
{
qDebug() << "Sprite::paint";
draw(m_plane, m_image.toImage(), m_flipHorizontal, m_flipVertical);
Q_UNUSED(painter);
Q_UNUSED(option);
Q_UNUSED(widget);
}
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) override
{
emit clicked();
event->ignore();
}
virtual ~GraphicsSpriteItem()
{}
signals:
void clicked();
void doubleclicked();
public slots:
virtual void setFrame(int frame)
{
m_frame = frame;
plane_set_pan_pos(m_plane,
m_sequences[m_sequence].m_x + (m_frame * m_sequences[m_sequence].m_width),
m_sequences[m_sequence].m_y);
plane_set_pan_size(m_plane,
m_sequences[m_sequence].m_width,
m_sequences[m_sequence].m_height);
plane_apply(m_plane);
}
protected:
QPixmap m_image;
int m_speed;
int m_frame;
bool m_flipHorizontal;
bool m_flipVertical;
std::map<std::string, sequence> m_sequences;
std::string m_sequence;
};
#endif // GRAPHICSSPRITEITEM_H