2011-03-04 13:34:31 +00:00
|
|
|
var Tool = this.Tool = ToolHandler.extend(new function() {
|
2011-02-28 18:31:03 +01:00
|
|
|
function viewToArtwork(event, document) {
|
2011-03-04 23:46:16 +00:00
|
|
|
var point = Events.getOffset(event);
|
2011-02-28 18:31:03 +01:00
|
|
|
// TODO: always the active view?
|
|
|
|
return document.activeView.viewToArtwork(point);
|
|
|
|
};
|
2011-03-03 22:45:17 +00:00
|
|
|
|
2011-02-28 18:31:03 +01:00
|
|
|
return {
|
|
|
|
beans: true,
|
|
|
|
|
|
|
|
initialize: function(handlers, doc) {
|
|
|
|
this.base(handlers);
|
2011-03-04 23:32:46 +00:00
|
|
|
// Create events once, so they can be removed easily too.
|
2011-02-28 18:31:03 +01:00
|
|
|
var that = this, curPoint;
|
|
|
|
var dragging = false;
|
2011-03-04 23:32:46 +00:00
|
|
|
this.events = {
|
|
|
|
mousedown: function(event) {
|
|
|
|
curPoint = viewToArtwork(event, that._document);
|
2011-03-03 23:06:53 +00:00
|
|
|
that.onHandleEvent('mouse-down', curPoint, null, null);
|
2011-02-28 18:31:03 +01:00
|
|
|
if (that.onMouseDown)
|
2011-02-15 00:01:56 +01:00
|
|
|
that._document.redraw();
|
2011-03-04 23:32:46 +00:00
|
|
|
if (that.eventInterval != null) {
|
|
|
|
this.timer = setInterval(that.events.mousemove,
|
2011-03-03 22:45:17 +00:00
|
|
|
that.eventInterval);
|
2011-03-04 23:32:46 +00:00
|
|
|
}
|
2011-02-28 18:31:03 +01:00
|
|
|
dragging = true;
|
|
|
|
},
|
2011-03-04 23:32:46 +00:00
|
|
|
|
|
|
|
mousemove: function(event) {
|
|
|
|
var point = event && viewToArtwork(event, that._document);
|
|
|
|
if (dragging) {
|
|
|
|
curPoint = point || curPoint;
|
|
|
|
if (curPoint) {
|
|
|
|
that.onHandleEvent('mouse-drag', curPoint, null, null);
|
|
|
|
if (that.onMouseDrag)
|
|
|
|
that._document.redraw();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
that.onHandleEvent('mouse-move', point, null, null);
|
|
|
|
if (that.onMouseMove)
|
2011-02-28 18:31:03 +01:00
|
|
|
that._document.redraw();
|
|
|
|
}
|
|
|
|
},
|
2011-03-04 23:32:46 +00:00
|
|
|
|
|
|
|
mouseup: function(event) {
|
|
|
|
if (dragging) {
|
|
|
|
curPoint = null;
|
|
|
|
if (this.eventInterval != null)
|
|
|
|
clearInterval(this.timer);
|
|
|
|
that.onHandleEvent('mouse-up',
|
|
|
|
viewToArtwork(event, that._document), null, null);
|
|
|
|
if (that.onMouseUp)
|
2011-02-28 18:31:03 +01:00
|
|
|
that._document.redraw();
|
2011-03-04 23:32:46 +00:00
|
|
|
dragging = false;
|
2011-02-28 18:31:03 +01:00
|
|
|
}
|
2011-02-26 13:00:55 +01:00
|
|
|
}
|
2011-03-04 23:32:46 +00:00
|
|
|
}
|
|
|
|
if (paper.document)
|
|
|
|
this.setDocument(paper.document);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
getDocument: function() {
|
|
|
|
return this._document;
|
|
|
|
},
|
|
|
|
|
|
|
|
setDocument: function(doc) {
|
2011-03-04 23:46:16 +00:00
|
|
|
// Remove old events first.
|
2011-03-04 23:32:46 +00:00
|
|
|
if (this._document)
|
|
|
|
Events.remove(this._document.canvas, this.events);
|
|
|
|
this._document = doc || paper.document;
|
|
|
|
Events.add(doc.canvas, this.events);
|
2011-02-28 18:31:03 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The fixed time delay between each call to the {@link #onMouseDrag}
|
2011-03-03 22:45:17 +00:00
|
|
|
* 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.
|
2011-02-28 18:31:03 +01:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
2011-03-04 23:32:46 +00:00
|
|
|
eventInterval: null
|
2011-02-28 18:31:03 +01:00
|
|
|
};
|
2011-03-03 16:32:55 +00:00
|
|
|
});
|