paper.js/src/tool/Tool.js

74 lines
1.9 KiB
JavaScript
Raw Normal View History

2011-02-08 13:15:55 +01:00
Tool = ToolHandler.extend({
2011-02-11 14:40:36 +01:00
beans: true,
2011-02-13 18:52:17 +00:00
2011-02-08 13:15:55 +01:00
initialize: function(handlers, doc) {
this.base(handlers);
2011-02-21 03:32:39 +01:00
if (Paper.document)
this.document = Paper.document;
2011-02-08 13:15:55 +01:00
},
setDocument: function(doc) {
2011-02-21 03:32:39 +01:00
if (this._document)
$(this._document.canvas).removeEvents();
this._document = doc || Paper.document;
var that = this, curPoint;
var dragging = false;
var events = {
dragstart: function(e) {
curPoint = new Point(e.offset);
that.onHandleEvent('MOUSE_DOWN', curPoint, null, null);
2011-02-21 03:32:39 +01:00
if (that.onMouseDown)
that._document.redraw();
2011-02-21 03:32:39 +01:00
if (that.eventInterval != -1)
this.intervalId = setInterval(events.drag, that.eventInterval);
dragging = true;
2011-02-08 13:15:55 +01:00
},
drag: function(e) {
2011-02-21 03:32:39 +01:00
if (e) curPoint = new Point(e.offset);
if (curPoint) {
that.onHandleEvent('MOUSE_DRAG', curPoint, null, null);
2011-02-21 03:32:39 +01:00
if (that.onMouseDrag)
that._document.redraw();
}
2011-02-08 13:15:55 +01:00
},
dragend: function(e) {
curPoint = null;
2011-02-21 03:32:39 +01:00
if (this.eventInterval != -1)
clearInterval(this.intervalId);
2011-02-08 13:15:55 +01:00
that.onHandleEvent('MOUSE_UP', new Point(e.offset), null, null);
2011-02-21 03:32:39 +01:00
if (that.onMouseUp)
that._document.redraw();
dragging = false;
},
mousemove: function(e) {
if(!dragging) {
that.onHandleEvent('MOUSE_MOVE', new Point(e.offset), null, null);
if (that.onMouseMove)
that._document.redraw();
}
2011-02-08 13:15:55 +01:00
}
};
$(doc.canvas).addEvents(events);
2011-02-08 13:15:55 +01:00
},
/**
2011-02-11 14:40:36 +01:00
* The fixed time delay between each call to the {@link #onMouseDrag}
2011-02-08 13:15:55 +01: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.
*
* 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-02-11 14:40:36 +01:00
eventInterval: -1,
2011-02-08 13:15:55 +01:00
2011-02-11 14:40:36 +01:00
getDocument: function() {
return this._document;
2011-02-08 13:15:55 +01:00
}
});