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);
};
return {
beans: true,
initialize: function(handlers, doc) { initialize: function(handlers, doc) {
this.base(handlers); this.base(handlers);
if (Paper.document) if (Paper.document)
this.document = Paper.document; this.document = Paper.document;
}, },
setDocument: function(doc) { setDocument: function(doc) {
if (this._document) if (this._document)
$(this._document.canvas).removeEvents(); $(this._document.canvas).removeEvents();
this._document = doc || Paper.document; this._document = doc || Paper.document;
var that = this, curPoint; var that = this, curPoint;
var dragging = false; var dragging = false;
var events = { var events = {
dragstart: function(e) { dragstart: function(e) {
curPoint = new Point(e.offset); curPoint = viewToArtwork(e);//new Point(e.offset);
that.onHandleEvent('MOUSE_DOWN', curPoint, null, null); that.onHandleEvent('MOUSE_DOWN', curPoint, null, null);
if (that.onMouseDown) if (that.onMouseDown)
that._document.redraw();
if (that.eventInterval != -1)
this.intervalId = setInterval(events.drag, that.eventInterval);
dragging = true;
},
drag: function(e) {
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; }
} };
}); });