Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / util / Point.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.util.Point
17  * @extends Ext.util.Region
18  *
19  * Represents a 2D point with x and y properties, useful for comparison and instantiation
20  * from an event:
21  * <pre><code>
22  * var point = Ext.util.Point.fromEvent(e);
23  * </code></pre>
24  */
25
26 Ext.define('Ext.util.Point', {
27
28     /* Begin Definitions */
29     extend: 'Ext.util.Region',
30
31     statics: {
32
33         /**
34          * Returns a new instance of Ext.util.Point base on the pageX / pageY values of the given event
35          * @static
36          * @param {Event} e The event
37          * @returns Ext.util.Point
38          */
39         fromEvent: function(e) {
40             e = (e.changedTouches && e.changedTouches.length > 0) ? e.changedTouches[0] : e;
41             return new this(e.pageX, e.pageY);
42         }
43     },
44
45     /* End Definitions */
46
47     constructor: function(x, y) {
48         this.callParent([y, x, y, x]);
49     },
50
51     /**
52      * Returns a human-eye-friendly string that represents this point,
53      * useful for debugging
54      * @return {String}
55      */
56     toString: function() {
57         return "Point[" + this.x + "," + this.y + "]";
58     },
59
60     /**
61      * Compare this point and another point
62      * @param {Ext.util.Point/Object} The point to compare with, either an instance
63      * of Ext.util.Point or an object with left and top properties
64      * @return {Boolean} Returns whether they are equivalent
65      */
66     equals: function(p) {
67         return (this.x == p.x && this.y == p.y);
68     },
69
70     /**
71      * Whether the given point is not away from this point within the given threshold amount.
72      * TODO: Rename this isNear.
73      * @param {Ext.util.Point/Object} The point to check with, either an instance
74      * of Ext.util.Point or an object with left and top properties
75      * @param {Object/Number} threshold Can be either an object with x and y properties or a number
76      * @return {Boolean}
77      */
78     isWithin: function(p, threshold) {
79         if (!Ext.isObject(threshold)) {
80             threshold = {
81                 x: threshold,
82                 y: threshold
83             };
84         }
85
86         return (this.x <= p.x + threshold.x && this.x >= p.x - threshold.x &&
87                 this.y <= p.y + threshold.y && this.y >= p.y - threshold.y);
88     },
89
90     /**
91      * Compare this point with another point when the x and y values of both points are rounded. E.g:
92      * [100.3,199.8] will equals to [100, 200]
93      * @param {Ext.util.Point/Object} The point to compare with, either an instance
94      * of Ext.util.Point or an object with x and y properties
95      * @return {Boolean}
96      */
97     roundedEquals: function(p) {
98         return (Math.round(this.x) == Math.round(p.x) && Math.round(this.y) == Math.round(p.y));
99     }
100 }, function() {
101     /**
102      * Translate this region by the given offset amount. TODO: Either use translate or translateBy!
103      * @param {Ext.util.Offset/Object} offset Object containing the <code>x</code> and <code>y</code> properties.
104      * Or the x value is using the two argument form.
105      * @param {Number} The y value unless using an Offset object.
106      * @return {Ext.util.Region} this This Region
107      * @method
108      */
109     this.prototype.translate = Ext.util.Region.prototype.translateBy;
110 });
111