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 Draws a rectangle into the Scene. To create a Rectangle, you need only supply
 30  * it's location and size, along with at least 1 of 3 rendering instructions.
 31  * The 3 rendering instructions are "fill", "stroke", and "clear".
 32  * The fill rendering instruction tells this rectangle to fill the rectangle
 33  * with the given color.
 34  * The stroke rendering instruction tells this rectangle to stroke (outline) the
 35  * rectangle with the given color. You can also specify a stroke_width, which
 36  * is used to define the line thickness used while outlining the rectangle.
 37  * The clear rendering instruction actually is set to "true" or "false", and it
 38  * tells the rectangle to clear/remove the area behind it.
 39  * 
 40  * @example
 41  * To draw a red rectangle with a blue outline, you might define it as such:
 42  * Rectangle({
 43  *    x: 25, y: 25, w: 50, h: 50,
 44  *    fill: "red", stroke: "blue"
 45  * })
 46  * This creates a red square at (0,0) with width and height of 50, and a blue
 47  * outline.
 48  * <div id="rectangle_example_1"></div>
 49  * <script type="text/javascript">
 50  *    Stage({
 51  *       container_id: "rectangle_example_1",
 52  *       width: 100, height: 100,
 53  *       update_time: 10000,
 54  *       scene: Scene({
 55  *          content: [
 56  *             Rectangle({
 57  *                x: 25, y: 25, w: 50, h: 50,
 58  *                fill: "red", stroke: "blue"
 59  *             })
 60  *          ]
 61  *       })
 62  *    });
 63  * </script>
 64  * @extends SceneContent
 65  * @param {Object} _details A JSON Object with the following properties:
 66  * <table cellpadding="0" cellspacing="1" border="0" class="constructor_details">
 67  *    <tr><th>Property</th>     <th>Required</th> <th>Default</th></tr>
 68  *    <tr><td>clear</td>        <td>no</td>       <td>false</td></tr>
 69  *    <tr><td>fill</td>         <td>no</td>       <td>transparent</td></tr>
 70  *    <tr><td>stroke</td>       <td>no</td>       <td>#000000</td></tr>
 71  *    <tr><td>stroke_width</td> <td>no</td>       <td>1</td></tr>
 72  * </table>
 73  */
 74 function Rectangle(_details) { // {{{
 75 	/** @constructs */
 76 	function _Rectangle(_details) { // {{{
 77 		// Private Members {{{
 78 		var clear = check(_details.clear, false);
 79 		var fill = check(_details.fill, "transparent");
 80 		var stroke = check(_details.stroke, "#000000");
 81 		var stroke_width = check(_details.stroke_width, 1);
 82 		// }}} Private Members
 83 		
 84 		// Public Members {{{
 85 		this.draw = function(_context) { // {{{
 86 			var bounds = this.getBounds();
 87 			if(exists(fill)) {
 88 				_context.fillStyle = fill;
 89 				_context.fillRect(bounds.x, bounds.y, bounds.w, bounds.h);
 90 			}
 91 			if(exists(stroke)) {
 92 				_context.strokeStyle = stroke;
 93 				if(typeof stroke_width != 'undefined') {
 94 					_context.lineWidth = stroke_width;
 95 				}
 96 				_context.strokeRect(bounds.x, bounds.y, bounds.w, bounds.h);
 97 			}
 98 			if(clear) {
 99 				_context.clearRect(bounds.x, bounds.y, bounds.w, bounds.h);
100 			}
101 		} // }}} draw
102 		// }}} Public Members
103 		
104 	} // }}} _Rectangle
105 	SceneContent(_details).extend(_Rectangle);
106 	var theRectangle = new _Rectangle(_details);
107 	return theRectangle;
108 } // }}}
109 // These properties are for jEdit - Programmer's Text Editor.
110 // Load this file in jEdit to see what they do.
111 // ::folding=explicit:mode=javascript:noTabs=true:collapseFolds=4::
112