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
|
package AnimatedPixmapItem;
use strict;
use warnings;
use QtCore4;
use QtGui4;
use QtCore4::isa qw( Qt::GraphicsItem );
sub frame
{
return this->{currentFrame};
}
sub frameCount
{
return scalar @{this->{frames}};
}
sub image
{
my ($frame) = @_;
return scalar @{this->{frames}} == 0 ? Qt::Pixmap() : this->{frames}->[$frame % scalar @{this->{frames}}]->{pixmap};
}
sub setVelocity
{
my ($xvel, $yvel) = @_;
this->{vx} = $xvel;
this->{vy} = $yvel;
}
sub xVelocity
{
return this->{vx};
}
sub yVelocity
{
return this->{vy};
}
#struct Frame {
#Qt::Pixmap pixmap;
#Qt::PainterPath shape;
#Qt::RectF boundingRect;
#};
sub currentFrame() {
return this->{currentFrame};
}
sub frames() {
return this->{frames};
}
sub vx() {
return this->{vx};
}
sub vy() {
return this->{vy};
}
sub NEW
{
my ($class, $animation, $scene) = @_;
$class->SUPER::NEW(undef, $scene);
this->{vx} = 0;
this->{vy} = 0;
this->{currentFrame} = 0;
this->{frames} = [];
for (my $i = 0; $i < scalar @{$animation}; ++$i) {
my $pixmap = $animation->[$i];
my $frame = {
pixmap => $pixmap,
shape => Qt::PainterPath(),
boundingRect => Qt::RectF($pixmap->rect())
};
push @{this->{frames}}, $frame;
}
}
sub setFrame
{
my ($frame) = @_;
if (scalar @{frames()}) {
prepareGeometryChange();
this->{currentFrame} = $frame % scalar @{frames()};
}
}
sub advance
{
my ($phase) = @_;
if ($phase == 1) {
moveBy(vx, vy);
}
}
sub boundingRect
{
return frames()->[currentFrame]->{boundingRect};
}
sub shape
{
my $f = frames()->[currentFrame];
if ($f->{shape}->isEmpty()) {
my $path = Qt::PainterPath();
$path->addRegion(Qt::Region($f->{pixmap}->createHeuristicMask()));
$f->{shape} = $path;
}
return $f->{shape};
}
sub paint
{
my ($painter) = @_;
$painter->drawPixmap(0, 0, frames->[currentFrame]->{pixmap});
}
1;
|