Have Tool convert points from view coordinates to artwork coordinates.

This commit is contained in:
Jonathan Puckey 2011-02-28 18:31:03 +01:00
parent 74f797a9dd
commit 4fa293ec6d

View file

@ -1,74 +1,82 @@
Tool = ToolHandler.extend({ Tool = ToolHandler.extend(new function() {
beans: true, function viewToArtwork(event, document) {
var point = Point.read(event.offset.x, event.offset.y);
// TODO: always the active view?
return document.activeView.viewToArtwork(point);
};
initialize: function(handlers, doc) { return {
this.base(handlers); beans: true,
if (Paper.document)
this.document = Paper.document;
},
setDocument: function(doc) { initialize: function(handlers, doc) {
if (this._document) this.base(handlers);
$(this._document.canvas).removeEvents(); if (Paper.document)
this._document = doc || Paper.document; this.document = Paper.document;
var that = this, curPoint; },
var dragging = false;
var events = { setDocument: function(doc) {
dragstart: function(e) { if (this._document)
curPoint = new Point(e.offset); $(this._document.canvas).removeEvents();
that.onHandleEvent('MOUSE_DOWN', curPoint, null, null); this._document = doc || Paper.document;
if (that.onMouseDown) var that = this, curPoint;
that._document.redraw(); var dragging = false;
if (that.eventInterval != -1) var events = {
this.intervalId = setInterval(events.drag, that.eventInterval); dragstart: function(e) {
dragging = true; curPoint = viewToArtwork(e);//new Point(e.offset);
}, that.onHandleEvent('MOUSE_DOWN', curPoint, null, null);
drag: function(e) { if (that.onMouseDown)
if (e) curPoint = new Point(e.offset);
if (curPoint) {
that.onHandleEvent('MOUSE_DRAG', curPoint, null, null);
if (that.onMouseDrag)
that._document.redraw(); that._document.redraw();
} if (that.eventInterval != -1)
}, this.intervalId = setInterval(events.drag, that.eventInterval);
dragend: function(e) { dragging = true;
curPoint = null; },
if (this.eventInterval != -1) drag: function(e) {
clearInterval(this.intervalId); if (e) curPoint = viewToArtwork(e);//new Point(e.offset);
that.onHandleEvent('MOUSE_UP', new Point(e.offset), null, null); if (curPoint) {
if (that.onMouseUp) that.onHandleEvent('MOUSE_DRAG', curPoint, null, null);
that._document.redraw(); if (that.onMouseDrag)
dragging = false; that._document.redraw();
}, }
mousemove: function(e) { },
if(!dragging) { dragend: function(e) {
that.onHandleEvent('MOUSE_MOVE', new Point(e.offset), null, null); curPoint = null;
if (that.onMouseMove) if (this.eventInterval != -1)
clearInterval(this.intervalId);
that.onHandleEvent('MOUSE_UP', viewToArtwork(e), null, null);
if (that.onMouseUp)
that._document.redraw(); that._document.redraw();
dragging = false;
},
mousemove: function(e) {
if (!dragging) {
that.onHandleEvent('MOUSE_MOVE', viewToArtwork(e), null, null);
if (that.onMouseMove)
that._document.redraw();
}
} }
} };
}; $(doc.canvas).addEvents(events);
$(doc.canvas).addEvents(events); },
},
/** /**
* The fixed time delay between each call to the {@link #onMouseDrag} * The fixed time delay between each call to the {@link #onMouseDrag}
* event. Setting this to an interval means the {@link #onMouseDrag} event * event. Setting this to an interval means the {@link #onMouseDrag} event
* is called repeatedly after the initial {@link #onMouseDown} until the * is called repeatedly after the initial {@link #onMouseDown} until the
* user releases the mouse. * user releases the mouse.
* *
* Sample code: * Sample code:
* <code> * <code>
* // Fire the onMouseDrag event once a second, * // Fire the onMouseDrag event once a second,
* // while the mouse button is down * // while the mouse button is down
* tool.eventInterval = 1000; * tool.eventInterval = 1000;
* </code> * </code>
* *
* @return the interval time in milliseconds * @return the interval time in milliseconds
*/ */
eventInterval: -1, eventInterval: -1,
getDocument: function() { getDocument: function() {
return this._document; return this._document;
} }
};
}); });