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 /**
 30  * @extends Point
 31  * @class Represents a Rectangular area with x,y coordinates as well as width,
 32  *    and height.
 33  * @author <a href="mailto:elberry@tellurianring.com">Eric Berry</a>
 34  * @example
 35  * // Creates a new Bounds object using standard JSON.
 36  * var myBounds = Bounds({x: 10, y: 10, w: 100, h: 100});
 37  * @example Alerts true
 38  * alert(myBounds.contains(Bounds({x: 15, y: 15})));
 39  * @example Alerts true
 40  * alert(myBounds.contains({x: 20, y: 20, w: 20, h: 20}));
 41  * @example Alerts false
 42  * alert(myBounds.contains({x: 20, y: 20, w: 100, h: 100}));
 43  * This example alerts false, because when width and height are supplied, the
 44  * given bounds must be completely within the 'myBounds' area.
 45  * @example // Using a Bounds object.
 46  * var myBounds = Bounds({x: 10, y: 10, w: 100, h: 100});
 47  * // alerts 10
 48  * alert(myBounds.x);
 49  * // alerts 200
 50  * alerts(myBounds.w * myBounds.h);
 51  * @param {Bounds,Point,Object} _bounds Another Bounds object, or Point object. Can be created
 52  *    using this function or created with a JSON Object containing the
 53  *    following attributes:
 54  * <table cellpadding="0" cellspacing="1" border="0" class="constructor_details">
 55  *    <tr><th>Property</th> <th>Required</th> <th>Default</th></tr>
 56  *    <tr><td>x</td>        <td>yes</td>      <td></td></tr>
 57  *    <tr><td>y</td>        <td>yes</td>      <td></td></tr>
 58  *    <tr><td>w</td>        <td>yes</td>      <td></td></tr>
 59  *    <tr><td>h</td>        <td>yes</td>      <td></td></tr>
 60  * </table>
 61  */
 62 function Bounds(_details) { // {{{
 63    /** @constructs */
 64    function _Bounds(_details) { // {{{
 65       /** 
 66        * Height
 67        * @field
 68        */
 69       this.h = _details.h;
 70       /**
 71        * Width
 72        * @field
 73        */
 74       this.w = _details.w;
 75       
 76       /**
 77        * Checks to see if the given Bounds object is within this 
 78        * bounds object. This function works with Point objects as well.
 79        * Meaning that if you pass it an object with only x, and y
 80        * coordinates, it only checks to see if they are within this bounds.
 81        * @function 
 82        */
 83       this.contains = function(_bounds) {
 84          // my maximum x and y coords.
 85          var mx = this.x + this.w;
 86          var my = this.y + this.h;
 87          
 88          // other bounds x and y coords.
 89          var ox = _bounds.x;
 90          var oy = _bounds.y;
 91          // initial checks to make sure other x, and y coords are within these
 92          // bounds.
 93          if(ox < this.x || ox > mx || oy < this.y || oy > my) {
 94             return false;
 95          }
 96          
 97          // if there's a width and height in the given bounds, then test to see
 98          // if entire bounds fits within this bounds.
 99          if(exists(_bounds.w, _bounds.h)) {
100             
101             // we only need to check that the max x, and y coords of the other
102             // bounds is less than our max x and y coords because the other
103             // x and y values have already been checked, and wouldn't have 
104             // reached here if they weren't inside this bounds.
105             ox = ox + _bounds.w;
106             oy = oy + _bounds.h;
107             if(ox > mx) {
108                return false;
109             }
110             if(oy > my) {
111                return false;
112             }
113          }
114          // if we've gotten this far, then the other bounds is within these
115          // bounds
116          return true;
117       }
118    } // }}} _Bounds
119    Point(_details).extend(_Bounds);
120    var theBounds = new _Bounds(_details);
121    return theBounds;
122 } // }}} Bounds
123 // These properties are for jEdit - Programmer's Text Editor.
124 // Load this file in jEdit to see what they do.
125 // ::folding=explicit:mode=javascript:noTabs=true:collapseFolds=4::
126