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

View file

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