Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
Use event classes for better flexibility and type hinting
Browse files Browse the repository at this point in the history
  • Loading branch information
stil committed Oct 14, 2016
1 parent 7685384 commit 3d63f7c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/Decoder.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace GIFEndec;

use GIFEndec\Events\FrameDecodedEvent;
use GIFEndec\Geometry\Point;
use GIFEndec\Geometry\Rectangle;
use GIFEndec\IO\PhpStream;
Expand Down Expand Up @@ -96,7 +97,11 @@ public function decode(callable $onFrameDecoded)
break;
case 0x2C:
$this->readImageDescriptor();
$onFrameDecoded($this->currentFrame, $frameIndex++);

$event = new FrameDecodedEvent();
$event->frameIndex = $frameIndex++;
$event->decodedFrame = $this->currentFrame;
$onFrameDecoded($event);
break;
case 0x3B:
$cycle = false;
Expand Down
17 changes: 17 additions & 0 deletions src/Events/FrameDecodedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace GIFEndec\Events;

use GIFEndec\Frame;

class FrameDecodedEvent
{
/**
* @var int
*/
public $frameIndex;

/**
* @var Frame
*/
public $decodedFrame;
}
22 changes: 22 additions & 0 deletions src/Events/FrameRenderedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace GIFEndec\Events;

use GIFEndec\Frame;

class FrameRenderedEvent
{
/**
* @var int
*/
public $frameIndex;

/**
* @var Frame
*/
public $decodedFrame;

/**
* @var resource
*/
public $renderedFrame;
}
11 changes: 9 additions & 2 deletions src/Renderer.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace GIFEndec;

use GIFEndec\Events\FrameDecodedEvent;
use GIFEndec\Events\FrameRenderedEvent;

class Renderer
{
/**
Expand All @@ -26,8 +29,12 @@ public function __construct(Decoder $decoder)
*/
public function start(callable $onFrameRendered)
{
$this->decoder->decode(function (Frame $frame, $index) use ($onFrameRendered) {
$onFrameRendered($this->render($frame, $index), $index);
$this->decoder->decode(function (FrameDecodedEvent $e) use ($onFrameRendered) {
$event = new FrameRenderedEvent();
$event->frameIndex = $e->frameIndex;
$event->decodedFrame = $e->decodedFrame;
$event->renderedFrame = $this->render($e->decodedFrame, $e->frameIndex);
$onFrameRendered($event);
});
}

Expand Down

0 comments on commit 3d63f7c

Please sign in to comment.