Clean up and shorten code.

This commit is contained in:
Jürg Lehni 2011-05-08 15:45:40 +01:00
parent af8fd22ae2
commit 585e3b6254
2 changed files with 31 additions and 51 deletions

View file

@ -44,12 +44,8 @@ var ToolEvent = this.ToolEvent = Base.extend({
* Convenience method to allow local overrides of point values. * Convenience method to allow local overrides of point values.
* See application below. * See application below.
*/ */
choosePoint: function(point, toolPoint) { _choosePoint: function(point, toolPoint) {
if (point) return point ? point : toolPoint ? toolPoint.clone() : null;
return point;
if (toolPoint)
return new Point(toolPoint);
return null;
}, },
/** /**
@ -70,7 +66,7 @@ var ToolEvent = this.ToolEvent = Base.extend({
* </code> * </code>
*/ */
getPoint: function() { getPoint: function() {
return this.choosePoint(this._point, this.tool.point); return this._choosePoint(this._point, this.tool.point);
}, },
setPoint: function(point) { setPoint: function(point) {
@ -82,7 +78,7 @@ var ToolEvent = this.ToolEvent = Base.extend({
* event was fired. * event was fired.
*/ */
getLastPoint: function() { getLastPoint: function() {
return this.choosePoint(this._lastPoint, this.tool.lastPoint); return this._choosePoint(this._lastPoint, this.tool.lastPoint);
}, },
setLastPoint: function(lastPoint) { setLastPoint: function(lastPoint) {
@ -94,7 +90,7 @@ var ToolEvent = this.ToolEvent = Base.extend({
* was last clicked. * was last clicked.
*/ */
getDownPoint: function() { getDownPoint: function() {
return this.choosePoint(this._downPoint, this.tool.downPoint); return this._choosePoint(this._downPoint, this.tool.downPoint);
}, },
setDownPoint: function(downPoint) { setDownPoint: function(downPoint) {
@ -109,7 +105,7 @@ var ToolEvent = this.ToolEvent = Base.extend({
*/ */
getMiddlePoint: function() { getMiddlePoint: function() {
// For explanations, see getDelta() // For explanations, see getDelta()
if (this._middlePoint == null && this.tool.lastPoint != null) { if (!this._middlePoint && this.tool.lastPoint) {
// (point + lastPoint) / 2 // (point + lastPoint) / 2
return this.tool.point.add(this.tool.lastPoint).divide(2); return this.tool.point.add(this.tool.lastPoint).divide(2);
} }
@ -131,10 +127,9 @@ var ToolEvent = this.ToolEvent = Base.extend({
// Instead, keep calculating the delta each time, so the result can be // Instead, keep calculating the delta each time, so the result can be
// directly modified by the script without changing the internal values. // directly modified by the script without changing the internal values.
// We could cache this and use clone, but this is almost as fast... // We could cache this and use clone, but this is almost as fast...
if (this._delta == null && this.tool.lastPoint != null) { return this._delta && this.tool.lastPoint
return this.tool.point.subtract(this.tool.lastPoint); ? this.tool.point.subtract(this.tool.lastPoint)
} : this._delta;
return this._delta;
}, },
setDelta: function(delta) { setDelta: function(delta) {
@ -160,27 +155,16 @@ var ToolEvent = this.ToolEvent = Base.extend({
* </code> * </code>
*/ */
getCount: function() { getCount: function() {
switch (this.type) { // Return downCount for both mouse down and up, since
case 'mousedown': // the count is the same.
case 'mouseup': return /^mouse(down|up)$/.test(this.type)
// Return downCount for both mouse down and up, since ? this.tool.downCount
// the count is the same. : this.tool.count;
return this.tool.downCount;
default:
return this.tool.count;
}
}, },
setCount: function(count) { setCount: function(count) {
switch (this.type) { this.tool[/^mouse(down|up)$/.test(this.type) ? 'downCount' : 'count']
case 'mousedown': = count;
case 'mouseup':
this.tool.downCount = count;
break;
default:
this.tool.count = count;
break;
}
}, },
getModifiers: function() { getModifiers: function() {

View file

@ -35,8 +35,8 @@ var ToolHandler = this.ToolHandler = Base.extend({
* *
* Sample code: * Sample code:
* <code> * <code>
* // Fire the onMouseDrag event after the user has dragged * // Fire the onMouseDrag event after the user has dragged more then 5
* // more then 5 points from the last onMouseDrag event: * // points from the last onMouseDrag event:
* tool.minDistance = 5; * tool.minDistance = 5;
* </code> * </code>
*/ */
@ -104,16 +104,12 @@ var ToolHandler = this.ToolHandler = Base.extend({
this.downCount++; this.downCount++;
break; break;
case 'mouseup': case 'mouseup':
// Mouse up events return the down point for last point, // Mouse up events return the down point for last point, so delta is
// so delta is spanning over the whole drag. // spanning over the whole drag.
this.lastPoint = this.downPoint; this.lastPoint = this.downPoint;
break; break;
} }
if (start) { this.count = start ? 0 : this.count + 1;
this.count = 0;
} else {
this.count++;
}
return true; return true;
}, },
@ -125,15 +121,15 @@ var ToolHandler = this.ToolHandler = Base.extend({
this.onMouseDown(new ToolEvent(this, type, event)); this.onMouseDown(new ToolEvent(this, type, event));
break; break;
case 'mousedrag': case 'mousedrag':
// In order for idleInterval drag events to work, we need to // In order for idleInterval drag events to work, we need to not
// not check the first call for a change of position. // check the first call for a change of position. Subsequent calls
// Subsequent calls required by min/maxDistance functionality // required by min/maxDistance functionality will require it,
// will require it, otherwise this might loop endlessly. // otherwise this might loop endlessly.
var needsChange = false, var needsChange = false,
// If the mouse is moving faster than maxDistance, do not // If the mouse is moving faster than maxDistance, do not produce
// produce events for what is left after the first event is // events for what is left after the first event is generated in
// generated in case it is shorter than maxDistance, as this // case it is shorter than maxDistance, as this would produce weird
// would produce weird results. matchMaxDistance controls this. // results. matchMaxDistance controls this.
matchMaxDistance = false; matchMaxDistance = false;
while (this.updateEvent(type, pt, this.minDistance, while (this.updateEvent(type, pt, this.minDistance,
this.maxDistance, false, needsChange, matchMaxDistance)) { this.maxDistance, false, needsChange, matchMaxDistance)) {
@ -144,8 +140,8 @@ var ToolHandler = this.ToolHandler = Base.extend({
} }
break; break;
case 'mouseup': case 'mouseup':
// If the last mouse drag happened in a different place, call // If the last mouse drag happened in a different place, call mouse
// mouse drag first, then mouse up. // drag first, then mouse up.
if ((this.point.x != pt.x || this.point.y != pt.y) if ((this.point.x != pt.x || this.point.y != pt.y)
&& this.updateEvent('mousedrag', pt, this.minDistance, && this.updateEvent('mousedrag', pt, this.minDistance,
this.maxDistance, false, false, false)) { this.maxDistance, false, false, false)) {