Skip to content

Adding animations #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/PhpPresentation/Slide.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ class Slide implements ComparableInterface, ShapeContainerInterface
* @var \PhpOffice\PhpPresentation\Slide\Transition
*/
private $slideTransition;

/**
*
* @var \PhpOffice\PhpPresentation\Slide\Animation
*/
private $animations = array();

/**
* Hash index
Expand Down Expand Up @@ -540,4 +546,27 @@ public function setIsVisible($value = true)
$this->isVisible = (bool)$value;
return $this;
}

/**
* Add an animation to the slide
*
* @param \PhpOffice\PhpPresentation\Slide\Animation
* @return \PhpOffice\PhpPresentation\Slide\Animation
*/
public function addAnimation($animation)
{
$this->animations[] = $animation;
return $animation;
}

/**
* Get collection of animations
*
* @return \PhpOffice\PhpPresentation\Slide\Animation
*/

public function getAnimations()
{
return $this->animations;
}
}
24 changes: 24 additions & 0 deletions src/PhpPresentation/Slide/Animation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace PhpOffice\PhpPresentation\Slide;

class Animation
{
private $shapeCollection = array();

public function __construct()
{

}

public function addShape($shape)
{
$this->shapeCollection[] = $shape;

return $shape;
}

public function getShapeCollection()
{
return $this->shapeCollection;
}
}
268 changes: 267 additions & 1 deletion src/PhpPresentation/Writer/PowerPoint2007/PptSlides.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ public function writeSlide(Slide $pSlide)
$objWriter->endElement();

// Loop shapes
$shapeId = 0;
$shapeId = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JewrassicPark Why do you modify this value ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I answered this in the last fork.

It seemed like powerpoint was treating shape 0 as the slide itself. IIRC I couldn't get the animations to work when the shape index started at 0.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I forgot it :)

$shapes = $pSlide->getShapeCollection();
foreach ($shapes as $shape) {
// Increment $shapeId
Expand Down Expand Up @@ -514,6 +514,272 @@ public function writeSlide(Slide $pSlide)
if (!is_null($pSlide->getTransition())) {
$this->writeTransition($objWriter, $pSlide->getTransition());
}
$shapeId = 1;
$shapes = $pSlide->getShapeCollection();

$animations = $pSlide->getAnimations();

if (count($animations) > 0) {

$hashToIdMap = array();

foreach ($shapes as $shape) {
$shapeId += 1;
$hash = $shape->getHashCode();
$hashToIdMap[$hash] = $shapeId;
}

$animationIds = array();

foreach ($animations as $animation) {
$shapesInAnimation = $animation->getShapeCollection();
foreach ($shapesInAnimation as $shape) {
$hash = $shape->getHashCode();
$animationIds[] = $hashToIdMap[$hash];
}
}

$idCount = 1;

$objWriter->startElement('p:timing');

$objWriter->startElement('p:tnLst');

$objWriter->startElement('p:par');

$objWriter->startElement('p:cTn');
$objWriter->writeAttribute('id', $idCount);
$idCount += 1;
$objWriter->writeAttribute('dur', 'indefinite');
$objWriter->writeAttribute('restart', 'never');
$objWriter->writeAttribute('nodeType', 'tmRoot');

$objWriter->startElement('p:childTnLst');

$objWriter->startElement('p:seq');
$objWriter->writeAttribute('concurrent', '1');
$objWriter->writeAttribute('nextAc', 'seek');

$objWriter->startElement('p:cTn');
$objWriter->writeAttribute('id', $idCount);
$idCount += 1;
$objWriter->writeAttribute('dur', 'indefinite');
$objWriter->writeAttribute('nodeType', 'mainSeq');

$objWriter->startElement("p:childTnLst");

//each animation has multiple shapes
foreach ($animations as $animation) {

$objWriter->startElement("p:par");

$objWriter->startElement("p:cTn");
$objWriter->writeAttribute("id", $idCount);
$idCount += 1;
$objWriter->writeAttribute("fill", "hold");

$objWriter->startElement('p:stCondLst');

$objWriter->startElement('p:cond');
$objWriter->writeAttribute("delay", "indefinite");
$objWriter->endElement();

$objWriter->endElement();

$objWriter->startElement("p:childTnLst");

$objWriter->startElement("p:par");

$objWriter->startElement("p:cTn");
$objWriter->writeAttribute("id", $idCount);
$idCount += 1;
$objWriter->writeAttribute("fill", "hold");

$objWriter->startElement('p:stCondLst');

$objWriter->startElement('p:cond');
$objWriter->writeAttribute("delay", "0");
$objWriter->endElement();

$objWriter->endElement();

$objWriter->startElement("p:childTnLst");

$shapesInAnimation = $animation->getShapeCollection();

$firstInAni = true;

foreach ($shapesInAnimation as $shape) {
$hash = $shape->getHashCode();
$shapeId = $hashToIdMap[$hash];

$objWriter->startElement("p:par");

$objWriter->startElement("p:cTn");
$objWriter->writeAttribute("id", $idCount);
$idCount += 1;
$objWriter->writeAttribute("presetID", "1");
$objWriter->writeAttribute("presetClass", "entr");
$objWriter->writeAttribute("fill", "hold");
$objWriter->writeAttribute("presetSubtype", "0");
$objWriter->writeAttribute("grpId", "0");

$nodeType = $firstInAni ? "clickEffect" : "withEffect";

$objWriter->writeAttribute("nodeType", $nodeType);

$objWriter->startElement("p:stCondLst");

$objWriter->startElement("p:cond");
$objWriter->writeAttribute("delay", "0");
$objWriter->endElement();

$objWriter->endElement();

$objWriter->startElement("p:childTnLst");

$objWriter->startElement("p:set");

$objWriter->startElement("p:cBhvr");

$objWriter->startElement("p:cTn");
$objWriter->writeAttribute("id", $idCount);
$idCount += 1;
$objWriter->writeAttribute("dur", "1");
$objWriter->writeAttribute("fill", "hold");

$objWriter->startElement("p:stCondLst");

$objWriter->startElement("p:cond");
$objWriter->writeAttribute("delay", "0");
$objWriter->endElement();

$objWriter->endElement();



$objWriter->endElement();

$objWriter->startElement("p:tgtEl");

$objWriter->startElement("p:spTgt");
//shape id
$objWriter->writeAttribute("spid", $shapeId);
$objWriter->endElement();

$objWriter->endElement();

$objWriter->startElement("p:attrNameLst");

$objWriter->writeElement("p:attrName", "style.visibility");

$objWriter->endElement();

$objWriter->endElement();

$objWriter->startElement("p:to");

$objWriter->startElement("p:strVal");
$objWriter->writeAttribute("val", "visible");
$objWriter->endElement();

$objWriter->endElement();

$objWriter->endElement();

$objWriter->endElement(); //end child tn lst

$objWriter->endElement(); //end ctn

$objWriter->endElement();

$firstInAni = false;
}


$objWriter->endElement(); //end child lst

$objWriter->endElement(); //end ctn

$objWriter->endElement(); //end par


$objWriter->endElement(); //end childtnlist

$objWriter->endElement(); //end ctn

$objWriter->endElement(); //end par

}


$objWriter->endElement();

$objWriter->endElement();

///prevCondLst
$objWriter->startElement("p:prevCondLst");

$objWriter->startElement('p:cond');
$objWriter->writeAttribute('evt', 'onPrev');
$objWriter->writeAttribute('delay', '0');

$objWriter->startElement('p:tgtEl');

$objWriter->writeElement('p:sldTgt', null);

$objWriter->endElement();

$objWriter->endElement();

$objWriter->endElement();

//nextCondLst
$objWriter->startElement("p:nextCondLst");

$objWriter->startElement('p:cond');
$objWriter->writeAttribute('evt', 'onNext');
$objWriter->writeAttribute('delay', '0');

$objWriter->startElement('p:tgtEl');

$objWriter->writeElement('p:sldTgt', null);

$objWriter->endElement();

$objWriter->endElement();

$objWriter->endElement();

$objWriter->endElement(); //end p:seq

$objWriter->endElement(); //end p:childTnLst

$objWriter->endElement(); //end ctn

$objWriter->endElement(); //end par

$objWriter->endElement(); //end tnLst

$objWriter->startElement('p:bldLst');

//add in ids of all shapes in this slides animations

foreach ($animationIds as $id) {
$objWriter->startElement('p:bldP');
$objWriter->writeAttribute("spid", $id);
$objWriter->writeAttribute('grpId', 0);
$objWriter->endELement();
}


$objWriter->endElement(); // end bldLst

$objWriter->endElement(); //end timing

$objWriter->endElement();
}


$objWriter->endElement();

Expand Down