X-Git-Url: http://git.ithinksw.org/extjs.git/blobdiff_plain/0494b8d9b9bb03ab6c22b34dae81261e3cd7e3e6..7a654f8d43fdb43d78b63d90528bed6e86b608cc:/src/util/Point.js?ds=sidebyside diff --git a/src/util/Point.js b/src/util/Point.js new file mode 100644 index 00000000..e89caa5a --- /dev/null +++ b/src/util/Point.js @@ -0,0 +1,95 @@ +/** + * @class Ext.util.Point + * @extends Ext.util.Region + * + * Represents a 2D point with x and y properties, useful for comparison and instantiation + * from an event: + *
+ * var point = Ext.util.Point.fromEvent(e);
+ *
+ */
+
+Ext.define('Ext.util.Point', {
+
+ /* Begin Definitions */
+ extend: 'Ext.util.Region',
+
+ statics: {
+
+ /**
+ * Returns a new instance of Ext.util.Point base on the pageX / pageY values of the given event
+ * @static
+ * @param {Event} e The event
+ * @returns Ext.util.Point
+ */
+ fromEvent: function(e) {
+ e = (e.changedTouches && e.changedTouches.length > 0) ? e.changedTouches[0] : e;
+ return new this(e.pageX, e.pageY);
+ }
+ },
+
+ /* End Definitions */
+
+ constructor: function(x, y) {
+ this.callParent([y, x, y, x]);
+ },
+
+ /**
+ * Returns a human-eye-friendly string that represents this point,
+ * useful for debugging
+ * @return {String}
+ */
+ toString: function() {
+ return "Point[" + this.x + "," + this.y + "]";
+ },
+
+ /**
+ * Compare this point and another point
+ * @param {Ext.util.Point/Object} The point to compare with, either an instance
+ * of Ext.util.Point or an object with left and top properties
+ * @return {Boolean} Returns whether they are equivalent
+ */
+ equals: function(p) {
+ return (this.x == p.x && this.y == p.y);
+ },
+
+ /**
+ * Whether the given point is not away from this point within the given threshold amount.
+ * TODO: Rename this isNear.
+ * @param {Ext.util.Point/Object} The point to check with, either an instance
+ * of Ext.util.Point or an object with left and top properties
+ * @param {Object/Number} threshold Can be either an object with x and y properties or a number
+ * @return {Boolean}
+ */
+ isWithin: function(p, threshold) {
+ if (!Ext.isObject(threshold)) {
+ threshold = {
+ x: threshold,
+ y: threshold
+ };
+ }
+
+ return (this.x <= p.x + threshold.x && this.x >= p.x - threshold.x &&
+ this.y <= p.y + threshold.y && this.y >= p.y - threshold.y);
+ },
+
+ /**
+ * Compare this point with another point when the x and y values of both points are rounded. E.g:
+ * [100.3,199.8] will equals to [100, 200]
+ * @param {Ext.util.Point/Object} The point to compare with, either an instance
+ * of Ext.util.Point or an object with x and y properties
+ * @return {Boolean}
+ */
+ roundedEquals: function(p) {
+ return (Math.round(this.x) == Math.round(p.x) && Math.round(this.y) == Math.round(p.y));
+ }
+}, function() {
+ /**
+ * Translate this region by the given offset amount. TODO: Either use translate or translateBy!
+ * @param {Ext.util.Offset/Object} offset Object containing the x
and y
properties.
+ * Or the x value is using the two argument form.
+ * @param {Number} The y value unless using an Offset object.
+ * @return {Ext.util.Region} this This Region
+ */
+ this.prototype.translate = Ext.util.Region.prototype.translateBy;
+});