Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / util / Point.js
1 /**
2  * @class Ext.util.Point
3  * @extends Ext.util.Region
4  *
5  * Represents a 2D point with x and y properties, useful for comparison and instantiation
6  * from an event:
7  * <pre><code>
8  * var point = Ext.util.Point.fromEvent(e);
9  * </code></pre>
10  */
11
12 Ext.define('Ext.util.Point', {
13
14     /* Begin Definitions */
15     extend: 'Ext.util.Region',
16
17     statics: {
18
19         /**
20          * Returns a new instance of Ext.util.Point base on the pageX / pageY values of the given event
21          * @static
22          * @param {Event} e The event
23          * @returns Ext.util.Point
24          */
25         fromEvent: function(e) {
26             e = (e.changedTouches && e.changedTouches.length > 0) ? e.changedTouches[0] : e;
27             return new this(e.pageX, e.pageY);
28         }
29     },
30
31     /* End Definitions */
32
33     constructor: function(x, y) {
34         this.callParent([y, x, y, x]);
35     },
36
37     /**
38      * Returns a human-eye-friendly string that represents this point,
39      * useful for debugging
40      * @return {String}
41      */
42     toString: function() {
43         return "Point[" + this.x + "," + this.y + "]";
44     },
45
46     /**
47      * Compare this point and another point
48      * @param {Ext.util.Point/Object} The point to compare with, either an instance
49      * of Ext.util.Point or an object with left and top properties
50      * @return {Boolean} Returns whether they are equivalent
51      */
52     equals: function(p) {
53         return (this.x == p.x && this.y == p.y);
54     },
55
56     /**
57      * Whether the given point is not away from this point within the given threshold amount.
58      * TODO: Rename this isNear.
59      * @param {Ext.util.Point/Object} The point to check with, either an instance
60      * of Ext.util.Point or an object with left and top properties
61      * @param {Object/Number} threshold Can be either an object with x and y properties or a number
62      * @return {Boolean}
63      */
64     isWithin: function(p, threshold) {
65         if (!Ext.isObject(threshold)) {
66             threshold = {
67                 x: threshold,
68                 y: threshold
69             };
70         }
71
72         return (this.x <= p.x + threshold.x && this.x >= p.x - threshold.x &&
73                 this.y <= p.y + threshold.y && this.y >= p.y - threshold.y);
74     },
75
76     /**
77      * Compare this point with another point when the x and y values of both points are rounded. E.g:
78      * [100.3,199.8] will equals to [100, 200]
79      * @param {Ext.util.Point/Object} The point to compare with, either an instance
80      * of Ext.util.Point or an object with x and y properties
81      * @return {Boolean}
82      */
83     roundedEquals: function(p) {
84         return (Math.round(this.x) == Math.round(p.x) && Math.round(this.y) == Math.round(p.y));
85     }
86 }, function() {
87     /**
88      * Translate this region by the given offset amount. TODO: Either use translate or translateBy!
89      * @param {Ext.util.Offset/Object} offset Object containing the <code>x</code> and <code>y</code> properties.
90      * Or the x value is using the two argument form.
91      * @param {Number} The y value unless using an Offset object.
92      * @return {Ext.util.Region} this This Region
93      */
94     this.prototype.translate = Ext.util.Region.prototype.translateBy;
95 });