Implement standard behavior of #_hitTest() for items without children, based on #_contains().

This should cover the minimum of what's needed for PointText.
This commit is contained in:
Jürg Lehni 2013-06-12 19:30:23 -07:00
parent 463a00bd1e
commit fe10c99d1b
5 changed files with 18 additions and 4 deletions

View file

@ -1322,6 +1322,10 @@ var Item = Base.extend(Callback, /** @lends Item# */{
}
// We only implement it here for items with rectangular content,
// for anything else we need to override #contains()
// TODO: There currently is no caching for the results of direct calls
// to this._getBounds('getBounds') (without the application of the
// internal matrix). Performance improvements could be achieved if
// these were cached too. See #_getCachedBounds().
return point.isInside(this._getBounds('getBounds'));
},
@ -1420,6 +1424,8 @@ var Item = Base.extend(Callback, /** @lends Item# */{
var res = this._children[i].hitTest(point, options);
if (res) return res;
}
} else if (this.hasFill() && this._contains(point)) {
return new HitResult('fill', this);
}
},

View file

@ -81,10 +81,9 @@ var Shape = Item.extend(/** @lends Shape# */{
return matrix ? matrix._transformBounds(rect) : rect;
},
_hitTest: function(point, options) {
if (this.hasFill() && this.contains(point))
return new HitResult('fill', this);
_hitTest: function _hitTest(point, options) {
// TODO: Implement stroke!
return _hitTest.base.apply(this, arguments);
},
statics: {

View file

@ -331,6 +331,7 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
if (item._boundsSelected) {
// We need to call the internal _getBounds, to get non-
// transformed bounds.
// TODO: Implement caching for these too!
var coords = mx._transformCorners(
item._getBounds('getBounds'));
// Now draw a rectangle that connects the transformed

View file

@ -12,6 +12,8 @@
// Register a jsDump parser for Base.
QUnit.jsDump.setParser('Base', function (obj, stack) {
// Just compare the string representation of classes inheriting from Base,
// since they hide the internal values.
return obj.toString();
});

View file

@ -14,9 +14,15 @@ module('TextItem');
test('PointText', function() {
var text = new PointText({
font: 'Arial',
fontSize: 14,
point: [100, 100],
content: 'Hello World!'
});
equals(text.point, { x: 100, y: 100 });
equals(text.fillColor, { red: 0, green: 0, blue: 0 }, 'text.fillColor should be black by default');
equals(text.point, { x: 100, y: 100 });
equals(text.bounds.point, { x: 100, y: 87.4 });
equals(function() {
return text.hitTest(text.bounds.center) != null;
}, true);
});