2011-03-03 13:33:41 +00:00
|
|
|
var Tool = ToolHandler.extend(new function() {
|
2011-02-28 18:31:03 +01:00
|
|
|
function viewToArtwork(event, document) {
|
2011-02-28 19:29:32 +01:00
|
|
|
var point = Point.create(event.offset.x, event.offset.y);
|
2011-02-28 18:31:03 +01:00
|
|
|
// TODO: always the active view?
|
|
|
|
return document.activeView.viewToArtwork(point);
|
|
|
|
};
|
2011-02-08 13:15:55 +01:00
|
|
|
|
2011-02-28 18:31:03 +01:00
|
|
|
return {
|
|
|
|
beans: true,
|
|
|
|
|
|
|
|
initialize: function(handlers, doc) {
|
|
|
|
this.base(handlers);
|
2011-03-03 16:21:17 +00:00
|
|
|
if (paper.document)
|
|
|
|
this.document = paper.document;
|
2011-02-28 18:31:03 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
setDocument: function(doc) {
|
|
|
|
if (this._document)
|
|
|
|
$(this._document.canvas).removeEvents();
|
2011-03-03 16:21:17 +00:00
|
|
|
this._document = doc || paper.document;
|
2011-02-28 18:31:03 +01:00
|
|
|
var that = this, curPoint;
|
|
|
|
var dragging = false;
|
|
|
|
var events = {
|
|
|
|
dragstart: function(e) {
|
2011-02-28 19:29:32 +01:00
|
|
|
curPoint = viewToArtwork(e, that._document);
|
2011-02-28 18:31:03 +01:00
|
|
|
that.onHandleEvent('MOUSE_DOWN', curPoint, null, null);
|
|
|
|
if (that.onMouseDown)
|
2011-02-15 00:01:56 +01:00
|
|
|
that._document.redraw();
|
2011-02-28 18:31:03 +01:00
|
|
|
if (that.eventInterval != -1)
|
|
|
|
this.intervalId = setInterval(events.drag, that.eventInterval);
|
|
|
|
dragging = true;
|
|
|
|
},
|
|
|
|
drag: function(e) {
|
2011-02-28 19:29:32 +01:00
|
|
|
if (e) curPoint = viewToArtwork(e, that._document);
|
2011-02-28 18:31:03 +01:00
|
|
|
if (curPoint) {
|
|
|
|
that.onHandleEvent('MOUSE_DRAG', curPoint, null, null);
|
|
|
|
if (that.onMouseDrag)
|
|
|
|
that._document.redraw();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
dragend: function(e) {
|
|
|
|
curPoint = null;
|
|
|
|
if (this.eventInterval != -1)
|
|
|
|
clearInterval(this.intervalId);
|
2011-02-28 19:29:32 +01:00
|
|
|
that.onHandleEvent('MOUSE_UP',
|
|
|
|
viewToArtwork(e, that._document), null, null);
|
2011-02-28 18:31:03 +01:00
|
|
|
if (that.onMouseUp)
|
2011-02-26 13:00:55 +01:00
|
|
|
that._document.redraw();
|
2011-02-28 18:31:03 +01:00
|
|
|
dragging = false;
|
|
|
|
},
|
|
|
|
mousemove: function(e) {
|
|
|
|
if (!dragging) {
|
2011-02-28 19:29:32 +01:00
|
|
|
that.onHandleEvent('MOUSE_MOVE',
|
|
|
|
viewToArtwork(e, that._document), null, null);
|
2011-02-28 18:31:03 +01:00
|
|
|
if (that.onMouseMove)
|
|
|
|
that._document.redraw();
|
|
|
|
}
|
2011-02-26 13:00:55 +01:00
|
|
|
}
|
2011-02-28 18:31:03 +01:00
|
|
|
};
|
|
|
|
$(doc.canvas).addEvents(events);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The fixed time delay between each call to the {@link #onMouseDrag}
|
|
|
|
* event. Setting this to an interval means the {@link #onMouseDrag} event
|
|
|
|
* is called repeatedly after the initial {@link #onMouseDown} until the
|
|
|
|
* user releases the mouse.
|
|
|
|
*
|
|
|
|
* Sample code:
|
|
|
|
* <code>
|
|
|
|
* // Fire the onMouseDrag event once a second,
|
|
|
|
* // while the mouse button is down
|
|
|
|
* tool.eventInterval = 1000;
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @return the interval time in milliseconds
|
|
|
|
*/
|
|
|
|
eventInterval: -1,
|
|
|
|
|
|
|
|
getDocument: function() {
|
|
|
|
return this._document;
|
|
|
|
}
|
|
|
|
};
|
2011-02-14 20:08:19 +01:00
|
|
|
});
|