Location: PHPKode > scripts > LampoWeb > lampoweb/lampoweb.inc
<?php

    /* ---------------------------------------------------------------------------
     # Author: Alessandro Pasotti v. 0.02
     # Worst Code Award Winner 2003, candidate for 2004 too :)
     #
     # in- and out-functs were stolen from http://ming.sourceforge.net
     # and probably written by dave at opaque.net, creator of ming library,
     # many thanks to him and the other developers of ming.
     # 
     # This library contains several functions to create and manipulate
     # flash shape and menus. An useful function loads shapes exported from
     # "The Gimp".
     #
     # License: GPL.
     #
     # ---------------------------------------------------------------------------
     # VECLOAD
     # Loads a path exported from the gimp into a shape, optionally centered and
     # resized
     #
     # ---------------------------------------------------------------------------
     # SWF[VERTICAL|HORIZONTAL]MENU
     # Create a menu with buttons and place it on the screen, accept several
     # configuration parameters, dimensions are adjusted automatically to fit
     # the given space
     #
     # --------------------------------------------------------------------------
     # SHAPEANIMATEFROMTO (and other manipulating functions)
     # These group of function will create a sprite with the given
     # parameters a and insert it in the movie
     #
     # --------------------------------------------------------------------------
     # IN and OUTFUNCTIONS
     # Provide several animations with transparency effect
     # This are not my own effort: they've been grabbed from ming.sourceforge.net
     # 
     # ---------------------------------------------------------------------------
     */

     
    // set to >0 for debug output
    $debug = 0;


    /////////////////////////////////////////
    //
    // load a web-safe colorlist
    //

    require "colorlist.inc";


    //////////////////////////////////////////////////////////////////////
    //
    // "in" functions leave one instance on the display list
    // (though they can use more than one), and return that instance

    function fadein($movie, $shape , $cp) {
        $i = $movie->add($shape);
        $i->moveTo($cp->x, $cp->y);

        for($j = 0; $j <= 20; ++$j) {
            $i->multColor(1.0, 1.0, 1.0, $j/20);
            $movie->nextFrame();
        }

        return $i;
    }

    function slideleftin($movie, $shape , $cp) {
        $i = $movie->add($shape);
         
        for($j = 0; $j <= 20; ++$j) {
            $i->moveTo($cp->x-($j-20) * ($j-20), $cp->y);
            $i->multColor(1.0, 1.0, 1.0, $j/20);
            $movie->nextFrame();
        }
         
        return $i;
    }
     
    function sliderightin($movie, $shape , $cp) {
        $i = $movie->add($shape);
         
        for($j = 0; $j <= 20; ++$j) {
            $i->moveTo($cp->x+($j-20) * ($j-20), $cp->y);
            $i->multColor(1.0, 1.0, 1.0, $j/20);
            $movie->nextFrame();
        }
         
        return $i;
    }

    function zoomin($movie, $shape , $cp) {
        $i = $movie->add($shape);
        $i->moveTo($cp->x , $cp->y);
         
        for($j = 0; $j <= 20; ++$j) {
            $i->scaleTo(sqrt(sqrt($j/20)));
            $i->multColor(1.0, 1.0, 1.0, $j/20);
            $movie->nextFrame();
        }
         
        return $i;
    }
     
    function skewin($movie, $shape , $cp) {
        $i = $movie->add($shape);
        $i->moveTo($cp->x , $cp->y);
         
        for($j = 0; $j <= 20; ++$j) {
            $i->skewXTo((20-$j) * (20-$j)/200);
            $i->multColor(1.0, 1.0, 1.0, $j/20);
            $movie->nextFrame();
        }
         
        return $i;
    }
     
    function rotatein($movie, $shape, $cp) {
        $i = $movie->add($shape);
        $i->moveTo($cp->x , $cp->y);
         
        for($j = 0; $j <= 20; ++$j) {
            $i->rotateTo((20-$j) * (20-$j)/30);
            $i->multColor(1.0, 1.0, 1.0, $j/20);
            $movie->nextFrame();
        }
         
        return $i;
    }
     
    function stretchdownin($movie, $shape, $cp) {
        $i = $movie->add($shape);
         
        for($j = 0; $j <= 20; ++$j) {
            $i->moveTo($cp->x , $cp->y * $j/20);
            $i->scaleTo(1.0, $j/20);
            $movie->nextFrame();
        }
         
        return $i;
    }
     
    function stretchupin($movie, $shape , $cp) {
        $i = $movie->add($shape);
         
        for($j = 0; $j <= 20; ++$j) {
            $i->moveTo($cp->x, $cp->y+$cp->y * (20-$j)/20);
            $i->scaleTo(1.0, $j/20);
            $movie->nextFrame();
        }
         
        return $i;
    }
     
    function doubleslidein($movie, $shape , $cp) {
        $i1 = $movie->add($shape);
        $i2 = $movie->add($shape);
         
        for($j = 0; $j <= 20; ++$j) {
            $i1->moveTo($cp->x-($j-20) * ($j-20)/2, $cp->y);
            $i2->moveTo($cp->x+($j-20) * ($j-20)/2, $cp->y);
            $i1->multColor(1.0, 1.0, 1.0, $j * $j/400);
            $i2->multColor(1.0, 1.0, 1.0, $j * $j/400);
            $movie->nextFrame();
        }

        $movie->remove($i2);
        return $i1;
    }
     
    $infuncs = array(fadein, sliderightin, slideleftin, zoomin, doubleslidein,
        skewin, rotatein, stretchupin, stretchdownin);

    // "out" functions are required to remove all instances of the shape,
    // including the one that's passed in
     
    function fadeout($movie, $shape, $instance , $cp) {
        for($j = 0; $j <= 20; ++$j) {
            $instance->multColor(1.0, 1.0, 1.0, (20-$j)/20);
            $movie->nextFrame();
        }
         
        $movie->remove($instance);
    }
     
    function sliderightout($movie, $shape, $instance, $cp) {
        for($j = 0; $j <= 20; ++$j) {
            $instance->moveTo($cp->x+$j * $j, $cp->y);
            $instance->multColor(1.0, 1.0, 1.0, (20-$j)/20);
            $movie->nextFrame();
        }
         
        $movie->remove($instance);
    }

    function slideleftout($movie, $shape, $instance, $cp) {
        for($j = 0; $j <= 20; ++$j) {
            $instance->moveTo($cp->x-$j * $j, $cp->y);
            $instance->multColor(1.0, 1.0, 1.0, (20-$j)/20);
            $movie->nextFrame();
        }

        $movie->remove($instance);
    }
     
    function zoomout($movie, $shape, $instance, $cp) {
        for($j = 0; $j <= 20; ++$j) {
            $instance->scaleTo(1+sqrt($j/20));
            $instance->multColor(1.0, 1.0, 1.0, (20-$j)/20);
            $movie->nextFrame();
        }
         
        $movie->remove($instance);
    }
     
    function skewout($movie, $shape, $instance, $cp) {
        for($j = 0; $j <= 20; ++$j) {
            $instance->skewXTo(-$j * $j/200);
            $instance->multColor(1.0, 1.0, 1.0, (20-$j)/20);
            $movie->nextFrame();
        }
         
        $movie->remove($instance);
    }
     
    function stretchdownout($movie, $shape, $instance, $cp) {
        for($j = 0; $j <= 20; ++$j) {
            $instance->moveTo($cp->x, $cp->y+$cp->y * $j/20);
            $instance->scaleTo(1.0, (20-$j)/20);
            $movie->nextFrame();
        }
         
        $movie->remove($instance);
    }
     
    function stretchupout($movie, $shape, $instance, $cp) {
        for($j = 0; $j <= 20; ++$j) {
            $instance->moveTo($cp->x, $cp->y * (20-$j)/20);
            $instance->scaleTo(1.0, (20-$j)/20);
            $movie->nextFrame();
        }

        $movie->remove($instance);
    }
     
    function rotateout($movie, $shape, $instance, $cp) {
        for($j = 0; $j <= 20; ++$j) {
            $instance->rotateTo(-$j * $j/$cp->y);
            $instance->multColor(1.0, 1.0, 1.0, (20-$j)/20);
            $movie->nextFrame();
        }
         
        $movie->remove($instance);
    }
     
    function doubleslideout($movie, $shape, $i1, $cp) {
        $i2 = $movie->add($shape);
         
        for($j = 0; $j <= 20; ++$j) {
            $i1->moveTo($cp->x-$j * $j/2, $cp->y);
            $i2->moveTo($cp->x+$j * $j/2, $cp->y);
            $i1->multColor(1.0, 1.0, 1.0, (20-$j) * (20-$j)/400);
            $i2->multColor(1.0, 1.0, 1.0, (20-$j) * (20-$j)/400);
            $movie->nextFrame();
        }
         
        $movie->remove($i1);
        $movie->remove($i2);
    }
     
    $outfuncs = array(fadeout, sliderightout, slideleftout, zoomout,
        doubleslideout, skewout, rotateout, stretchupout,
        stretchdownout);

    //////////////////////////////////////////////////////////////////////
    //
    // Some classes to be used in function below
    //


     
    // color in hex RGB
    class color {
        var $r;
        var $g;
        var $b;
         
        function color ($r = '00' , $g = '00' , $b = '00') {
            $this->r = $r;
            $this->g = $g;
            $this->b = $b;
        }
        function getColor() {
            //echo $this->r.$this->g.$this->b . "<br>";
            return $this->r . $this->g . $this->b;
        }
        function getDecR() {
            return base_convert($this->r, 16 , 10);
        }
        function getDecG() {
            return base_convert($this->g, 16 , 10);
        }
        function getDecB() {
            return base_convert($this->b, 16 , 10);
        }
    }


    class point {
        var $x;
        var $y; // coords
        function setPoint ($xi = 0 , $yi = 0) {
            // constructor
            $this->x = $xi;
            $this->y = $yi;
        }
        function getPoint () {
            return $this;
        }
        function getPointX () {
            return $this->x;
        }
        function getPointY () {
            return $this->y;
        }
         
        function point ($xi = 0 , $yi = 0) {
            // constructor
            $this->setPoint($xi, $yi);
        }
         
        function switchCoords () {
            $tmp = $this->x;
            $this->x = $this->y;
            $this->y = $tmp;
        }
         
    }
     
    class vecPoint extends point {
         
        var $type = ''; //
    }
     
     
    function ShapeAnimateFromTo ($shape , $movie , $from , $to , $steps ) {
        $xInc = ($to->x - $from->x)/$steps;
        $yInc = ($to->y - $from->y)/$steps;
        global $debug;
        if ($debug > 1) {
            echo "to->x $to->x , to->y $to->y , xInc $xInc , yInc $yInc , steps $steps \n";
        }
        $mySpr = new SWFSprite();
        $myHandle = $mySpr->add($shape);
        for ($i = 0 ; $i < $steps; $i++, $from->x += $xInc, $from->y += $yInc ) {
            $myHandle->moveTo($from->x , $from->y);
            $mySpr->nextFrame();
        }
        return $movie->add($mySpr);
    }
     
    function ShapeAnimateFromToRot ($shape , $movie , $from , $to , $deg , $steps ) {
        $xInc = ($to->x - $from->x)/$steps;
        $yInc = ($to->y - $from->y)/$steps;
        global $debug;
        if ($debug > 1) {
            echo "to->x $to->x , to->y $to->y , xInc $xInc , yInc $yInc , steps $steps \n";
        }
         
        $mySpr = new SWFSprite();
        $myHandle = $mySpr->add($shape);
         
        for ($i = 0 ; $i < $steps; $i++, $from->x += $xInc, $from->y += $yInc ) {
            $myHandle->rotate($deg/$steps);
            $myHandle->moveTo($from->x , $from->y);
            $mySpr->nextFrame();
        }
        return $movie->add($mySpr);
    }
     
     
    function ShapeRotateAt ($shape , $movie , $at , $deg , $steps ) {
        global $debug;
        // debug code
        if ($debug > 1) {
            echo "to->x $to->x , to->y $to->y , xInc $xInc , yInc $yInc , steps $steps \n";
        }
        $mySpr = new SWFSprite();
        //$mySpr->add( new SWFAction("stop();"));
        $myHandle = $mySpr->add($shape);
        $myHandle->moveTo($at->x , $at->y);
         
        for ($i = 0 ; $i < abs($deg); $i += abs($deg/$steps)) {
            $myHandle->rotate($deg/$steps);
            $mySpr->nextFrame();
        }
        return $movie->add($mySpr);

    }
     
    ///////////////////////////////////////////////////////////////////////
    //
    // Reads a path file exported from "The Gimp" and draws a flash shape
    //  using php-ming bezier instructions.
    //  Shape is optionally centered and resized.
    //  vecLoad ($vecFileName , $Shape , $center , $resize)
    //  $vecFileName : name of the file containig the "path" exported
    //    from The Gimp
    //  $Shape: reference to a ming SWFShape
    //  $center: if <>0  the shape is centered
    //  $resize: if = x the maximum size (height or width) will be x
    //
     
     
    function vecLoad ($vecFileName , $Shape , $center , $resize) {
        $bezPoints = array();
        // store bezier anchors and points
        global $debug;
         
        $xOfs = 0;
        $yOfs = 0;
        $xMax = -10e90;
        $yMax = -10e90;
        $xMin = +10e90;
        $yMin = +10e90;
        $Scale = 1;
        $vecP = new vecPoint;
         
        file_exists($vecFileName) || die ("ERROR: file not found $vecFileName\n");
        $vec = file($vecFileName);
         
        // get bounds and compute center coords
        $Scale = 1;
        for ($i = 6 ; $i < count($vec) ; $i++ ) {
            getPoint($vecP, $vec, $i , $xOfs , $yOfs, $Scale );
            if ($debug > 1) {
                print var_dump($vecP);
            };
            if (($vecP->type == 1 ) || ($vecP->type == 3 ) ) {
                if ($xMax < $vecP->x ) {
                    $xMax = $vecP->x;
                }
                if ($yMax < $vecP->y ) {
                    $yMax = $vecP->y;
                }
                if ($xMin > $vecP->x ) {
                    $xMin = $vecP->x;
                }
                if ($yMin > $vecP->y ) {
                    $yMin = $vecP->y;
                }
            }
        }
        if ($resize) {
            if (abs($xMax - $xMin ) > abs($yMax - $yMin ) ) {
                $Scale = $resize / abs($xMax - $xMin );
            } else {
                $Scale = $resize / abs($yMax - $yMin );
            }
        }
         
        $xOfs = -($xMax + $xMin ) / 2;
        $yOfs = -($yMax + $yMin ) / 2;
         
        if (!$center ) {
            $xOfs = 0;
            $yOfs = 0;
        }
         
        // number of points
        $vecString = explode (':', $vec[1] );
        $points = $vecString[1];
         
        // if path is closed
        $closed = strstr ($vec[2], '1' );
         
        if ($debug) {
            print " resize : $resize \n";
            print " closed= $closed \t points= $points \n";
            print " xMax: $xMax ,  xMin: $xMin\n";
            print " yMax: $yMax ,  yMin: $yMin\n";
            print " dim : " . ($xMax - $xMin ) . " X " . ($yMax - $yMin ) . " pixel \n";
            print " xOfs: $xOfs , yOfs: $yOfs , Scale: $Scale \n";
        }
        // read 1st point
        getPoint($firstPoint, $vec, 5 , $xOfs , $yOfs, $Scale);
        $lastPoint = $firstPoint;
         
        if ($debug ) {
            print " 1st point: $firstPoint\n";
        }
         
        $Shape->movePenTo($firstPoint->x , $firstPoint->y );
         
        // main loop, start from line 6 of the vpath file:
        $i = 6;
         
        do {
            getpoint($vecP , $vec, $i , $xOfs , $yOfs, $Scale);
            switch ($vecP->type) {
                case '1':
                // it is an anchor point
                if (count($bezPoints) == 4 ) {
                    //it is an end point
                    if (($bezPoints[0] == $lastPoint->x) && ($bezPoints[1] == $lastPoint->y)
                        && ($bezPoints[2] == $vecP->x) && ($bezPoints[3] == $vecP->y)) {
                        $Shape->drawLineTo($vecP->x , $vecP->y);
                    } else {
                        $Shape->drawCubicTo($bezPoints[0] , $bezPoints[1] ,
                            $bezPoints[2] , $bezPoints[3],
                            $vecP->x , $vecP->y );
                        if ($debug) {
                            print "1 - drawCubicTo( {$bezPoints[0]} , {$bezPoints[1]} , {$bezPoints[2]} ,{$bezPoints[3]}, {$vecP->x} , {$vecP->y} );\n";
                        }
                    }
                    $lastPoint = $vecP;
                    $bezPoints = array();
                } else {
                    print " ERROR: bez shift 1 \n";
                }
                break;
                case '2' :
                // control point
                if ((count($bezPoints) == 2 ) || (count($bezPoints) == 0 ) ) {
                    // is a control point
                    array_push ($bezPoints, $vecP->x , $vecP->y );
                } else {
                     print " ERROR: bez shift 2 \n" ;
                }
                break;
                case '3': // anchor point + new closed curve
                if (count($bezPoints) == 4 ) {
                    // is an end point
                    $Shape->drawCubicTo($bezPoints[0] , $bezPoints[1] ,
                        $bezPoints[2] , $bezPoints[3] ,
                        $firstPoint->x , $firstPoint->y);
                    if ($debug) {
                        print "3 - drawCubicTo( {$bezPoints[0]} , {$bezPoints[1]} , {$bezPoints[2]} ,{$bezPoints[3]}, {$vecP->x} , {$vecP->y} );\n";
                    }
                    $firstPoint = $vecP;
                    $bezPoints = array();
                    //print " -------------- new curve --------------------\n";
                    $Shape->movePenTo ($firstPoint->x , $firstPoint->y );
                } else {
                    print " ERROR: bez shift 3 \n";
                }
                break;
                default:
                {
                    print " ERROR: type unknown\n";
                }
            }
             
            $i++;
        }
        while ($i < count($vec) );
         
        // if curve is closed draw the last
        if ($closed) {
            $Shape->drawCubicTo($bezPoints[0] , $bezPoints[1] ,
                $bezPoints[2] , $bezPoints[3] ,
                $firstPoint->x , $firstPoint->y );
             
        }
        if ($debug) {
	    print " ------------- end curve --------------\n";
	}
    }
     
     
    // read point at position $pos and determine its type, stores in $vecP
    function getPoint (&$vecP , $vec, $pos , $xOfs , $yOfs, $Scale) {
        global $debug ;
         
        preg_match_all('/[0-9]+/', $vec[$pos], $val );
         
        $vecP->x = ($val[0][1] + $xOfs) * $Scale;
        $vecP->y = ($val[0][2] + $yOfs) * $Scale;
        $vecP->type = (int)$val[0][0];
        if ($debug > 1) {
            print $vec[$pos] . "\t" . $val[0][0] . "\t" . $val[0][1] . "\t" . $val[0][2]. "\n";
            print var_dump($vecP);
        }
    }

     

    function rect($x , $y , $color , $border = 0) {
        $s = new SWFShape();
        if ($border) {
            $s->setLine($border, 0, 0, 0);
        }
        //echo "<br>RECT<br>";
        //var_dump($color);
        $s->setRightFill($s->addFill($color->getDecR(), $color->getDecG(), $color->getDecB()));
        $s->movePenTo(-$x/2, -$y/2);
        $s->drawLineTo(-$x/2, $y/2);
        $s->drawLineTo($x/2, $y/2);
        $s->drawLineTo($x/2, -$y/2);
        $s->drawLineTo(-$x/2, -$y/2);
         
        return $s;
    }
     
    function label($text, $fontFile, &$height , $length , $color) {
        $t = new SWFText();
        $font = new SWFFont($fontFile);
         
        $t->setFont($font);

        $height = $height/(1 + $t->getDescent() * $height/1024);
        $t->setHeight($height);
         
        //echo $t->getAscent() . "\t<b> $height x $length </b>\n  ";
         
        if ($t->getWidth($text) > $length) {
            $height = $height * $length/$t->getWidth($text);
            $t->setHeight($height);
            //echo "CENTERED $height <bR>";
        }
        //$height = $t->getDescent();

        $t->setColor($color->getDecR(), $color->getDecG(), $color->getDecB());
        $t->addString($text);
        return $t;
    }
     
     
    //////////////////////////////////////////////
    //
    // create a SWFButton and put it in the movie
    //  $movie : the movie where the button will be
    //  $dimension : point(x,y) stores x and y dimensions
    //  $position : button's upper left corner coords
    //  $text : label of the button
    //  $fontFile: the fdb font file for the label
    //  $action: actioscript triggered when clicked e.g. getUrl('http://www.button.org' , '_top')
    //  $colors = array( 'up' => color( r,g,b) , 'down' =>..., 'hit' ..., 'over' ...)
    //  $halign : horizontal alignment of the label ('left' 'right' 'center')
     
    function putSWFButton ($movie , $dimension , $position , $text , $fontFile ,
        $action , $colors , $halign = 'left' , $border = 0) {
         
        $margin = 0.1 ; // define margins between text and button edges (default 10%)

        $b = new SWFButton();
        $b->setOver(rect($dimension->x, $dimension->y, $colors['over'], $border));
        $b->setDown(rect($dimension->x, $dimension->y, $colors['down'], $border));
        $b->setUp(rect($dimension->x, $dimension->y, $colors['up'], $border));
        $b->setHit(rect($dimension->x, $dimension->y, $colors['hit'], $border));
         
        $b->setAction(new SWFAction($action));
         
        $i = $movie->add($b);
        $i->moveTo($position->x + $dimension->x/2 , $position->y + $dimension->y/2);
         
        $height = $dimension->y * (1 - $margin);
        $t = label($text, $fontFile, $height , $dimension->x * (1 - $margin), $colors['text'], $border);
        $j = $movie->add($t);
        switch ($halign) {
            case 'right':
            $x = $position->x - ($dimension->x * $margin)/2 + $dimension->x - $t->getWidth($text);
            break;
            case 'center':
            $x = $position->x + ($dimension->x - $t->getWidth($text))/2;
            break;
             
            default:
            $x = $position->x + ($dimension->x * $margin)/2;
            break;
        }
        $y = $position->y + $dimension->y/2 + $height/2 - $t->getDescent()/2;
        $j->moveTo($x , $y );
         
    }
     
    ////////////////////////////////////////////////////////////////////////////////////
    //
    // create a Vertical or Horizontal SWF menu and insert it in the movie
    //  $movie : the movie where the button will be
    //  $dimension : point(x,y) stores x and y dimensions
    //  $position : button's upper left corner coords
    //  $span : distance from buttons in % of the button (x for Horiz and y for Vert) dimension
    //  $menuItems[]= array( 'text' => 'Button One' , 'action' => "getUrl('http://www.button.org' , '_top')"
    //  $fontFile: the fdb font file for the label
    //  $action: actioscript triggered when clicked e.g. getUrl('http://www.button.org' , '_top')
    //  $colors = array( 'up' => color( r,g,b) , 'down' =>..., 'hit' ..., 'over' ...)
    //  $halign : 'left' , 'center' , 'right'
    //  $border : if >0 the button will have a border with line width = $border

    function putSWFVerticalMenu ($movie , $dimension , $position , $span , $menuItems ,
        $fontFile, $colors , $halign = 'left' , $border = 0 ) {
         
        $numItems = count($menuItems);
         
        $height = $dimension->y / ($numItems + $span * $numItems - $span);
        $distance = $height * $span;
        $buttonDim = new point($dimension->x , $height);
         
        for ($i = 1; $i <= $numItems ; $i++) {
            putSWFButton ($movie , $buttonDim , $position , $menuItems[$i-1]['text'] ,
                $fontFile , $menuItems[$i-1]['action'] , $colors , $halign , $border);
            $position->y += $height + $distance;
        }
    }
     
    function putSWFHorizontalMenu ($movie , $dimension , $position , $span , $menuItems ,
        $fontFile, $colors , $halign = 'left', $border = 0 ) {
         
        $numItems = count($menuItems);
         
        $length = $dimension->x / ($numItems + $span * $numItems - $span);
        $distance = $length * $span;
        $buttonDim = new point($length , $dimension->y );

        for ($i = 1; $i <= $numItems ; $i++) {
            putSWFButton ($movie , $buttonDim , $position , $menuItems[$i-1]['text'] ,
                $fontFile , $menuItems[$i-1]['action'] , $colors , $halign, $border);
            $position->x += $length + $distance;
        }
    }


?>
Return current item: LampoWeb