Simplify ToolEvent#modifiers.

This commit is contained in:
Jonathan Puckey 2011-04-25 12:05:18 +02:00
parent 22bc08501e
commit 1e4ae9afb4
6 changed files with 27 additions and 54 deletions

View file

@ -63,8 +63,6 @@ var sources = [
'src/tool/ToolEvent.js',
'src/tool/ToolHandler.js',
'src/tool/Tool.js',
'src/ui/KeyModifiers.js',
'src/util/BlendMode.js',
'src/util/CanvasProvider.js',

View file

@ -111,8 +111,6 @@ Base.inject({
//#include "tool/ToolHandler.js"
//#include "tool/Tool.js"
//#include "ui/KeyModifiers.js"
//#include "util/BlendMode.js"
//#include "util/CanvasProvider.js"
//#include "util/Element.js"

View file

@ -31,9 +31,8 @@ var Tool = this.Tool = ToolHandler.extend(new function() {
var dragging = false;
this.events = {
mousedown: function(event) {
var modifiers = new KeyModifiers(event);
curPoint = viewToArtwork(event, that._document);
that.onHandleEvent('mouse-down', curPoint, modifiers);
that.onHandleEvent('mouse-down', curPoint, event);
if (that.onMouseDown)
that._document.redraw();
if (that.eventInterval != null) {
@ -44,30 +43,28 @@ var Tool = this.Tool = ToolHandler.extend(new function() {
},
mousemove: function(event) {
var modifiers = new KeyModifiers(event);
var point = event && viewToArtwork(event, that._document);
if (dragging) {
curPoint = point || curPoint;
if (curPoint) {
that.onHandleEvent('mouse-drag', curPoint, modifiers);
that.onHandleEvent('mouse-drag', curPoint, event);
if (that.onMouseDrag)
that._document.redraw();
}
} else {
that.onHandleEvent('mouse-move', point, modifiers);
that.onHandleEvent('mouse-move', point, event);
if (that.onMouseMove)
that._document.redraw();
}
},
mouseup: function(event) {
var modifiers = new KeyModifiers(event);
if (dragging) {
curPoint = null;
if (that.eventInterval != null)
clearInterval(this.timer);
that.onHandleEvent('mouse-up',
viewToArtwork(event, that._document), modifiers);
viewToArtwork(event, that._document), event);
if (that.onMouseUp)
that._document.redraw();
dragging = false;

View file

@ -34,12 +34,12 @@
var ToolEvent = this.ToolEvent = Base.extend({
beans: true,
initialize: function(tool, type, modifiers) {
this.modifiers = modifiers;
initialize: function(tool, type, event) {
this.event = event;
this.tool = tool;
this.type = type;
},
toString: function() {
return '{ type: ' + this.type
+ ', point: ' + this.point
@ -189,6 +189,20 @@ var ToolEvent = this.ToolEvent = Base.extend({
this.tool.count = count;
break;
}
},
getModifiers: function() {
if (!this._modifiers) {
var event = this.event;
this._modifiers = {
shift: event.shiftKey,
control: event.ctrlKey,
alt: event.altKey,
command: event.metaKey
// TODO: capslock
};
}
return this._modifiers;
}
// TODO: implement hitTest first

View file

@ -117,12 +117,12 @@ var ToolHandler = this.ToolHandler = Base.extend({
return true;
},
onHandleEvent: function(type, pt, modifiers) {
onHandleEvent: function(type, pt, event) {
switch (type) {
case 'mouse-down':
this.updateEvent(type, pt, null, null, true, false, false);
if (this.onMouseDown)
this.onMouseDown(new ToolEvent(this, type, modifiers));
this.onMouseDown(new ToolEvent(this, type, event));
break;
case 'mouse-drag':
// In order for idleInterval drag events to work, we need to
@ -139,7 +139,7 @@ var ToolHandler = this.ToolHandler = Base.extend({
this.maxDistance, false, this.needsChange,
this.matchMaxDistance)) {
if (this.onMouseDrag)
this.onMouseDrag(new ToolEvent(this, type, modifiers));
this.onMouseDrag(new ToolEvent(this, type, event));
this.needsChange = true;
this.matchMaxDistance = true;
}
@ -151,12 +151,12 @@ var ToolHandler = this.ToolHandler = Base.extend({
&& this.updateEvent('mouse-drag', pt, this.minDistance,
this.maxDistance, false, false, false)) {
if (this.onMouseDrag)
this.onMouseDrag(new ToolEvent(this, type, modifiers));
this.onMouseDrag(new ToolEvent(this, type, event));
}
this.updateEvent(type, pt, null, this.maxDistance, false,
false, false);
if (this.onMouseUp)
this.onMouseUp(new ToolEvent(this, type, modifiers));
this.onMouseUp(new ToolEvent(this, type, event));
// Start with new values for TRACK_CURSOR
this.updateEvent(type, pt, null, null, true, false, false);
this.firstMove = true;
@ -165,7 +165,7 @@ var ToolHandler = this.ToolHandler = Base.extend({
while (this.updateEvent(type, pt, this.minDistance,
this.maxDistance, this.firstMove, true, false)) {
if (this.onMouseMove)
this.onMouseMove(new ToolEvent(this, type, modifiers));
this.onMouseMove(new ToolEvent(this, type, event));
this.firstMove = false;
}
break;

View file

@ -1,34 +0,0 @@
/*
* Paper.js
*
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
* based on Scriptographer.org and designed to be largely API compatible.
* http://paperjs.org/
* http://scriptographer.org/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
* http://lehni.org/ & http://jonathanpuckey.com/
*
* All rights reserved.
*/
var KeyModifiers = Base.extend({
initialize: function(event) {
this.event = event;
}
}, new function() {
var modifiers = {
shift: 'shiftKey',
control: 'ctrlKey',
alt: 'altKey',
command: 'metaKey'
};
return Base.each(modifiers, function(modifier, key) {
this['get' + Base.capitalize(key)] = function() {
return this.event[modifier];
};
}, { beans: true });
});