From 1e4ae9afb41a2e915d6399ca465b9c9e7d13db3e Mon Sep 17 00:00:00 2001 From: Jonathan Puckey Date: Mon, 25 Apr 2011 12:05:18 +0200 Subject: [PATCH] Simplify ToolEvent#modifiers. --- src/load.js | 2 -- src/paper.js | 2 -- src/tool/Tool.js | 11 ++++------- src/tool/ToolEvent.js | 20 +++++++++++++++++--- src/tool/ToolHandler.js | 12 ++++++------ src/ui/KeyModifiers.js | 34 ---------------------------------- 6 files changed, 27 insertions(+), 54 deletions(-) delete mode 100644 src/ui/KeyModifiers.js diff --git a/src/load.js b/src/load.js index 820e9094..7593512e 100644 --- a/src/load.js +++ b/src/load.js @@ -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', diff --git a/src/paper.js b/src/paper.js index 0e68d122..c16ae7cf 100644 --- a/src/paper.js +++ b/src/paper.js @@ -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" diff --git a/src/tool/Tool.js b/src/tool/Tool.js index a1fa2a45..27ed50d8 100644 --- a/src/tool/Tool.js +++ b/src/tool/Tool.js @@ -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; diff --git a/src/tool/ToolEvent.js b/src/tool/ToolEvent.js index 646e5474..2b42a4f8 100644 --- a/src/tool/ToolEvent.js +++ b/src/tool/ToolEvent.js @@ -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 diff --git a/src/tool/ToolHandler.js b/src/tool/ToolHandler.js index 4af9a062..44e48d8a 100644 --- a/src/tool/ToolHandler.js +++ b/src/tool/ToolHandler.js @@ -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; diff --git a/src/ui/KeyModifiers.js b/src/ui/KeyModifiers.js deleted file mode 100644 index 9b6cd612..00000000 --- a/src/ui/KeyModifiers.js +++ /dev/null @@ -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 }); -});