1 /*
  2  *Copyright (c) 2009, TellurianRing.com
  3  *All rights reserved.
  4  *
  5  *Redistribution and use in source and binary forms, with or without modification,
  6  *are permitted provided that the following conditions are met:
  7  *
  8  *   Redistributions of source code must retain the above copyright notice, this
  9  *   list of conditions and the following disclaimer.
 10  *   Redistributions in binary form must reproduce the above copyright notice,
 11  *   this list of conditions and the following disclaimer in the documentation
 12  *   and/or other materials provided with the distribution.
 13  *   Neither the name of the Organization (TellurianRing.com) nor the names of
 14  *   its contributors may be used to endorse or promote products derived from
 15  *   this software without specific prior written permission.
 16  *
 17  *THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 18  *ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 19  *WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 20  *DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 21  *ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 22  *(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 23  *LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 24  *ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 25  *(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 26  *SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 27  */
 28 /**
 29  * @class Rotate's a group around a given pivot point.
 30  * @extends Group
 31  * @param {Object} _details A JSON Object describing the rotation of a group
 32  * of content. The object may have these properties:
 33  * <table cellpadding="0" cellspacing="1" border="0" class="constructor_details">
 34  *    <tr><th>Property</th> <th>Required</th> <th>Default</th></tr>
 35  *    <tr><td>angle</td>    <td>no</td>       <td>0 (Radians)</td></tr>
 36  *    <tr><td>pivot</td>    <td>no</td>       <td>The middle of this group, based on cumulative bounds.</td></tr>
 37  * </table>
 38  * @example // Simple Example - rotating an image 45 deg.
 39  * Rotate({ x: 25, y: 20.5, angle: toRadians(45),
 40  *    content: [
 41  *       Graphic({ url: "./images/beetleship.png" })
 42  *    ]
 43  * })
 44  * <div id="rotate_example_1"></div>
 45  * <script type="text/javascript">
 46  *    Stage({
 47  *       container_id: "rotate_example_1",
 48  *       width: 100, height: 100,
 49  *       update_time: 1000,
 50  *       scene: Scene({
 51  *          content: [
 52  *             Rotate({ x: 25, y: 20.5, angle: toRadians(45),
 53  *                content: [
 54  *                   Graphic({ url: "./images/beetleship.png" })
 55  *                ]
 56  *             })
 57  *          ]
 58  *       })
 59  *    });
 60  * </script>
 61  */
 62 function Rotate(_details) { // {{{
 63    /** @constructs */
 64    function _Rotate(_details) { // {{{
 65       // Private Members {{{
 66       var angle = check(_details.angle, 0);
 67       var pivot = _details.pivot;
 68       // }}} Private Members
 69 
 70       // Public Members {{{
 71       this.draw = function(_context) { // {{{
 72          // draw the groups contents to a different canvas.
 73          var bounds = this.getBounds();
 74          var content = this.getContent();
 75          _context.save();
 76          // translate the context to the center of where the group would be
 77          // drawn.
 78          var pivot_point = {x: (bounds.w / 2), y: (bounds.h / 2)};
 79          if(exists(pivot)) {
 80             pivot_point = pivot;
 81          }
 82          _context.translate(bounds.x + pivot_point.x, bounds.y + pivot_point.y);
 83          // then rotate it.
 84          _context.rotate(angle);
 85          // then translate it back to where the group will begin to be drawn.
 86          _context.translate( -pivot_point.x, -pivot_point.y);
 87          for(var index in content) {
 88             var component = content[index];
 89             component.draw(_context);
 90          }
 91          _context.restore();
 92       } // }}} draw
 93 
 94       /**
 95        * Sets this rotation's angle to the given amount.
 96        * @param _angle The angle this Rotation group starts at.
 97        */
 98       this.rotate = function(_angle) { // {{{
 99          angle = _angle;
100       } // }}} rotate
101       /**
102        * Rotates this group by the given amount. This method simply adds the
103        * given delta angle to the starting angle.
104        * @param _dangle The amount to rotate this group by.
105        */
106       this.rotateBy = function(_dangle) { // {{{
107          angle += _dangle;
108       } // }}} rotateBy
109       // }}} public Members
110    } // }}} _Rotate
111 
112    Group(_details).extend(_Rotate);
113    var theRotation = new _Rotate(_details);
114    return theRotation;
115 } // }}} Rotate
116 // These properties are for jEdit - Programmer's Text Editor.
117 // Load this file in jEdit to see what they do.
118 // ::folding=explicit:mode=javascript:noTabs=true:collapseFolds=4::
119