21 Kasım 2009 Cumartesi

swfmill XML Creator Class


SWFMillXMLCreator Class'ı swfmill aracı ile kullanılabilecek XML dosyaları hazırlamaya yarayan bir sınıftır. Bu sınıf sayesinde kolaylıkla SWF dosyaları yaratılabilir, resim, font, animasyon ve ses dosyaları import edilebilir halde XML şemaları hazırlanabiliyor. Sınıfın kaynak kodu aşağıdaki gibi:


/**
 * This class is used to create an xml file for creating or editing swf documents by the tool swfmill.
 * It uses DOMDocument Class. swfmill version is swfmill-0.2.12
 *
 * @author Behçet MUTLU
 
* @email behcetmutlu@b-prodesign.com
 
* @copyright 2009 AdresGezgini.Com
 */

class SWFMillXMLCreator extends DOMDocument
{

    private $movie = null;

    private function addSimpleElement($name, $parent)
    {
        return $this->addElement($name, is_null($parent) ? $this->movie : $parent);
    }

    private function addArrayElement($name, $parent, $vars)
    {
        return $this->addElement($name, is_null($parent) ? $this->movie : $parent, $vars);
    }
   
    //
    /**
     * Importing a Sound
     *
     * We can import sounds with a similar method, where “id” is the linkage
     * id, and we put the following tag inside of the tag:
     *
     *
     *
     * and later in our actionscript (compiled prior via Mtasc, etc). we’d put:
     *
     *         var snd:Sound = new Sound();
     *         snd.attachSound("myID");
     *         snd.start();
     *
     * Note: Requires pre-release 0.2.11.18 or later, and sound is not streamed.
     * Note: Works with 56, 64, 128, 192 and 256 kbps MP3s. Note: joint stereo
     * MP3s, WAV etc don’t play at all.
     *
     * SWFMillXMLCreator::addSound()
     *
     * @param string $id
     * @param string $import
     * @param mixed $parent
     * @return
     */
    public function addSound($id = "foo", $import = "", $parent = null)
    {
        return $this->addArrayElement("sound", $parent, array("id" => $id, "import" => $import));
    }
   
   
    /**
     * Note : That the font tag must exist inside a library tag for the linkage
     * id to work with TextFormats and dynamically created TextFields, or that
     * it is Exported for runtime sharing.
     *
     * The font node get two attributes “id” and “name”. id will be to be used
     * for the format attribute of textfields :
     *
     *
     *
     *
     * name attribute is used in order to get an actionscript reference to the
     * embedded font. When using the TextField’s embedFonts property, you’ll be
     * able to use the TextFormat.font property with this name. This attribute
     * is also a way to group different font by families and retrieve them from
     * actionscript (Flash 8 only).
     *
     * Last thing to note, the id and attributes can be used together as the
     * serve different purposes.
     *
     * SWFMillXMLCreator::placeElement()
     *
     * @param string $id
     * @param string $name
     * @param string $depth
     * @param mixed $parent
     * @return
     */
    public function placeElement($id = "hellobox", $name = "", $depth = "10", $parent = null)
    {
        return $this->addArrayElement("place", $parent, array("id" => $id, "name" => $name, "depth" => $depth));
    }
   
    //
    /**
     * Note : That the font tag must exist inside a library tag for the linkage
     * id to work with TextFormats and dynamically created TextFields, or that
     * it is Exported for runtime sharing.
     *
     * The font node get two attributes “id” and “name”. id will be to be used
     * for the format attribute of textfields :
     *
     *
     *
     *
     * name attribute is used in order to get an actionscript reference to the
     * embedded font. When using the TextField’s embedFonts property, you’ll be
     * able to use the TextFormat.font property with this name. This attribute
     * is also a way to group different font by families and retrieve them from
     * actionscript (Flash 8 only).
     *
     * Last thing to note, the id and attributes can be used together as the
     * serve different purposes.
     *
     * SWFMillXMLCreator::addTextField()
     *
     * @param string $id
     * @param string $width
     * @param string $height
     * @param string $size
     * @param string $font
     * @param string $text
     * @param mixed $parent
     * @return
     */
    public function addTextField($id = "hellobox", $width = "200", $height="50", $size="10", $font="font", $text="hello world!", $parent = null )
    {
        return $this->addArrayElement("textfield", $parent, array("id" => $id, "width" => $width, "height" => $height, "size" => $size, "font" => $font, "text" => $text));
    }
   

    /**
     * Importing Fonts
     *
     * Imoporting TTF fonts works almost like importing images or SWFs,
     * but the tag is used and you can specify the characters you
     * want included with the glyphs attribute. The example imports the
     * numerical characters of vera.ttf.
     *
     *
     *
     * Note : In order to use special characters, you’d use the equivalent
     * xml entity in the glyph attribute. For example é in place of é.
     *
     * SWFMillXMLCreator::addFont()
     *
     * @param string $id
     * @param string $import
     * @param string $glyphs
     * @param mixed $parent
     * @return
     */
    public function addFont($id = "font", $import="", $glyphs="0123456789", $parent = null)
    {
        return $this->addArrayElement("font", $parent, array("id" => $id, "import" => $import, "glyphs" => $glyphs));
           
    }

    /**
     * A simple Library
     *
     * The tag, like almost all others, lives in a tag.
     * You can determine in which frame assets are included into your SWF by
     * using multiple frames and putting it into the one you want your assets
     *  to be available in. You don’t have to import assets into the library,
     * but then they won’t be available to ActionScript. Sometimes you don’t
     * need an asset to be in the library, because you only want to use it as
     * a part of another clip you define, or you place it onto the stage directly
     * with the tag. Of course, you can have several and
     * tags in one . The example will import the same image as above,
     * but this time into the library. The ID also serves as the linkage name if
     * you import into the library.
     *
     *  
     *    
     *  

     *
     *
     *
     * SWFMillXMLCreator::addLibrary()
     *
     * @param mixed $parent
     * @return
     */
    public function addLibrary($parent = null)
    {
        return $this->addSimpleElement("library", $parent);
    }


    /**
     * The most basic SWF
     *
     * That’s the most basic SWF you can make. It’s 320 by 240 pixels, tries
     *  to run at 12 frames per second, and has a white background. Note the
     * tag, except of the and tags, all other
     * tags belong into one. For simplicity’s sake, most examples below will
     * assume you know that, and won’t include the header explicitly.
     *
     *
     *  
     *  
     *

     *
     * If you save this XML code into a file sample.swfml (it could also be
     * sample.xml or in fact anything else), you can compile it into a binary
     * SWF with the following commandline:
     *
     * swfmill simple sample.swfml sample.swf
     *
     *
     *
     * SWFMillXMLCreator::createMovie()
     *
     * @param string $width
     * @param string $height
     * @param string $framerate
     * @return
     */
    public function createMovie($width = "320", $height = "240", $framerate = "12", $version="")
    {
        $this->movie = $this->addArrayElement("movie", null, array("width" => $width,
            "height" => $height, "framerate" => $framerate, "version" => $version));
        return $this->movie;
    }

    /**
     * Creating a frame.
     * SWFMillXMLCreator::addFrame()
     *
     * @param mixed $parent
     * @return
     */
    public function addFrame($name = "", $parent = null)
    {
        return $this->addArrayElement("frame", $parent, array("name" =>$name));
    }
   
     /**
     * Creating a frame.
     * SWFMillXMLCreator::addFrame()
     *
     * @param mixed $parent
     * @return
     */
    public function stop($parent = null)
    {
        return $this->addSimpleElement("stop", $parent);
    }

    /**
     * Setting background property
     * SWFMillXMLCreator::addBackground()
     *
     * @param mixed $color
     * @param mixed $parent
     * @return
     */
    public function addBackground($color, $parent = null)
    {
        return $this->addArrayElement("background", $parent, array("color" => $color));
    }

    /**
     * The tag lets you import JPGs, PNGs (including alpha), SWFs and SVGs (somewhat experimental). 
     * They will be available as MovieClips. This example will import library/foo.jpg
     * and give it the ID foo. swfmill can also import TrueType fonts, but the syntax is different.
     *
     *
     *
     *
     * SWFMillXMLCreator::addClip()
     *
     * @param string $id
     * @param string $import
     * @param mixed $parent
     * @return
     */
    public function addClip($id = "foo", $import = "", $class="", $parent = null)
    {
        return $this->addArrayElement("clip", $parent, array("id" => $id, "import" => $import, "class" => $class));
    }
   
   
    /**
     * Metadata that search engine could index :
     *
     *  < meta title="my title" description="To be indexed" />
     *
     * SWFMillXMLCreator::addMetaData()
     *
     * @param string $title
     * @param string $description
     * @param mixed $parent
     * @return
     */
    public function addMetaData($title="my title", $description="To be indexed", $parent = null)
    {
        return $this->addArrayElement("meta", $parent, array("title" => $title, "description" => $description));
    }
   
    //
    /**
     * Importing a Shared Library
     *
     * You can use shared libraries, too. SWFs created with swfmill always
     * are available for runtime sharing, to import another SWF as a library
     * keep a local copy for testing purposes and import it like this:
     *
     * SWFMillXMLCreator::import()
     *
     * @param string $file
     * @param string $url
     * @param mixed $parent
     * @return
     */
    public function import($file = "library/library.swf", $url = "http://foo.com/library.swf", $parent = null)
    {
        return $this->addArrayElement("import", $parent, array("file" => $file, "url" => $url));
    }
   
    //
    /**
     * Using components
     *
     * As for version 0.2.9 you are able to use the new component tag.
     * No need to use a library and a call tag. Here is a simple example.
     * The combo is instantiated later by actionscript injected using MTASC.
     *
     *  
     *  
     *    
     *  
     *
     * There are still 2 problems with components:
     *
     * SWF extracted from a SWC can be big because they contain data for
     * the Flash IDE, classes and the visual elements that we need.
     * 
     * If you add a couple of components from the same family, swfmill will
     * not check if some classes already exist and you will end up with duplicates.
     * Here is a comparison with 3 components of the same family, an Alert window,
     * a Label and a ProgressBar:
     *
     *  Compiled with the Flash IDE: 12k
     *      
     *  Compiled with swfmill: 99k
     *
     *
     * SWFMillXMLCreator::addComponent()
     *
     * @param string $id
     * @param string $file
     * @param mixed $parent
     * @return
     */
    public function addComponent($id = "ComboBox", $file = "library/library.swf", $parent = null)
    {
        return $this->addArrayElement("component", $parent, array("id" => $id, "file" => $file));
    }
   
    private function addElement($name, $parent, $vars = array())
    {

        $currentElement = $this->createElement($name);
       

        if (sizeof($vars) > 0)
        {
            if(isset($vars[0]) && is_array($vars[0]))
            {
                  foreach ($vars as $element)
                  {
                        $currentElement->appendChild($element);               
                  }
                           
            }
            else
            {
                foreach ($vars as $var => $value) {
                    if(isset($value) && !is_null($value) && $value != "")
                        $currentElement->setAttribute($var, $value);
                }               
            }
        }
       
        if (!is_null($parent))
            $parent->appendChild($currentElement);

        return $currentElement;
    }

    /**
     * SWFMillXMLCreator::toArray()
     * @author Yarg Dahc
     * @link  http://php.net/manual/en/class.domdocument.php
     * @param mixed $oDomNode
     * @return
     */
    public function toArray(DOMNode$oDomNode = null)
    {
        // return empty array if dom is blank
        if (is_null($oDomNode) && !$this->hasChildNodes()) {
            return array();
        }
        $oDomNode = (is_null($oDomNode)) ? $this->documentElement : $oDomNode;
        if (!$oDomNode->hasChildNodes()) {
            $mResult = $oDomNode->nodeValue;
        } else {
            $mResult = array();
            foreach ($oDomNode->childNodes as $oChildNode) {
                // how many of these child nodes do we have?
                // this will give us a clue as to what the result structure should be
                $oChildNodeList = $oDomNode->getElementsByTagName($oChildNode->nodeName);
                $iChildCount = 0;
                // there are x number of childs in this node that have the same tag name
                // however, we are only interested in the # of siblings with the same tag name
                foreach ($oChildNodeList as $oNode) {
                    if ($oNode->parentNode->isSameNode($oChildNode->parentNode)) {
                        $iChildCount++;
                    }
                }
                $mValue = $this->toArray($oChildNode);
                $sKey = ($oChildNode->nodeName {
                    0}
                == '#') ? 0 : $oChildNode->nodeName;
                $mValue = is_array($mValue) ? $mValue[$oChildNode->nodeName] : $mValue;
                // how many of thse child nodes do we have?
                if ($iChildCount > 1) { // more than 1 child - make numeric array
                    $mResult[$sKey][] = $mValue;
                } else {
                    $mResult[$sKey] = $mValue;
                }
            }
            // if the child is bar, the result will be array(bar)
            // make the result just 'bar'
            if (count($mResult) == 1 && isset($mResult[0]) && !is_array($mResult[0])) {
                $mResult = $mResult[0];
            }
        }
        // get our attributes if we have any
        $arAttributes = array();
        if ($oDomNode->hasAttributes()) {
            foreach ($oDomNode->attributes as $sAttrName => $oAttrNode) {
                // retain namespace prefixes
                $arAttributes["@{$oAttrNode->nodeName}"] = $oAttrNode->nodeValue;
            }
        }
        // check for namespace attribute - Namespaces will not show up in the attributes list
        if ($oDomNode instanceof DOMElement && $oDomNode->getAttribute('xmlns')) {
            $arAttributes["@xmlns"] = $oDomNode->getAttribute('xmlns');
        }
        if (count($arAttributes)) {
            if (!is_array($mResult)) {
                $mResult = (trim($mResult)) ? array($mResult) : array();
            }
            $mResult = array_merge($mResult, $arAttributes);
        }
        $arResult = array($oDomNode->nodeName => $mResult);
        return $arResult;
    }
}


?>


 Bu sınıfın test / örnek kodu da aşağıdaki gibi:


/**
 * The tester of the SWFMillCreator class.
 *
 
* @author Behçet MUTLU
 
* @email behcetmutlu@b-prodesign.com
 
* @copyright 2009 AdresGezgini.Com
 */
 

 require_once("SWFMillXMLCreator.class.php");

$xml = new SWFMillXMLCreator("1.0", "utf-8");

$movie = $xml->createMovie("320", "240", "12", "7");

$xml->addClip("upState",         "up.png");
$xml->addClip("downState",         "down.png");
$xml->addClip("overState",         "over.png");
$xml->addClip("disabledState",     "disabled.png");

$frame = $xml->addFrame();

$library = $xml->addLibrary($frame);

$clip = $xml->addClip("testButton","","",$library);

$xml->placeElement("upState", "", "1", $up = $xml->addFrame("Up", $clip));
$xml->placeElement("downState", "", "2", $down = $xml->addFrame("Down", $clip));
$xml->placeElement("overState", "", "3", $over = $xml->addFrame("Over", $clip));
$xml->placeElement("disabledState", "", "4", $disabled = $xml->addFrame("Disabled", $clip));
$xml->stop($up);
$xml->stop($down);
$xml->stop($over);
$xml->stop($disabled);


$xml->appendChild($movie);

header( "content-type: application/xml; charset=utf-8" );

$xml->preserveWhiteSpace = false;
$xml->formatOutput   = true;

print($xml->saveXML());

?> 


0 yorum:

Yorum Gönder