-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphicslayeritem.h
96 lines (79 loc) · 2.11 KB
/
graphicslayeritem.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
/*
* Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
* Joshua Henderson <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef GRAPHICSLAYERITEM_H
#define GRAPHICSLAYERITEM_H
#include <QDebug>
#include <QGraphicsPixmapItem>
#include <QEvent>
#include "planemanager.h"
#include "graphicsplaneitem.h"
/**
* @brief The GraphicsLayerItem class
*
* A GraphicsPlaneItem that is meant for handling a panning layer of a graphics scene.
*
* This expects the image to be 2X the width of the scene, and it will scroll the layer.
*/
class GraphicsLayerItem : public GraphicsPlaneItem
{
public:
GraphicsLayerItem(struct plane_data* plane, const QPixmap& image, int width, int height, int speed)
: GraphicsPlaneItem(plane, image.rect()),
m_image(image),
m_speed(speed),
m_plane(plane),
m_width(width),
m_height(height),
m_x(0)
{
if (!plane)
qFatal("invalid plane pointer");
if (speed < 0)
m_x = m_image.width()/2;
plane_set_pan_size(m_plane, width, height);
plane_set_pan_pos(m_plane, m_x, 0);
plane_apply(m_plane);
}
inline int width() const
{
return m_width;
}
virtual void reverse()
{
m_speed *= -1;
}
virtual void advance(int step) override
{
if (!step)
return;
m_x += m_speed;
if (m_x >= m_image.width()/2)
m_x = 0;
else if (m_x < 0)
m_x = m_image.width()/2;
plane_set_pan_pos(m_plane, m_x, 0);
plane_apply(m_plane);
}
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
{
qDebug() << "GraphicsLayerItem::paint";
draw(m_plane, m_image.toImage());
Q_UNUSED(painter);
Q_UNUSED(option);
Q_UNUSED(widget);
}
virtual ~GraphicsLayerItem()
{}
protected:
QPixmap m_image;
int m_speed;
struct plane_data* m_plane;
int m_width;
int m_height;
int m_x;
};
#endif // GRAPHICSLAYERITEM_H