mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-20 22:39:50 -05:00
Move Item base mouse handling code to CanvasView.
This commit is contained in:
parent
577c884a70
commit
57bd659023
4 changed files with 54 additions and 37 deletions
|
@ -1,7 +1,9 @@
|
||||||
var fs = require('fs'),
|
var fs = require('fs'),
|
||||||
vm = require('vm'),
|
vm = require('vm'),
|
||||||
path = require('path'),
|
path = require('path'),
|
||||||
Canvas = require('canvas');
|
// Have HTMLCanvasElement reference Canvas too, so we do not handle browser
|
||||||
|
// and server differently in some places of our code.
|
||||||
|
Canvas = HTMLCanvasElement =require('canvas');
|
||||||
|
|
||||||
__dirname = path.resolve(__dirname, '../src/');
|
__dirname = path.resolve(__dirname, '../src/');
|
||||||
|
|
||||||
|
|
|
@ -54,17 +54,25 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{
|
||||||
// Entry for all mouse events in the _events list
|
// Entry for all mouse events in the _events list
|
||||||
var mouseEvent = {
|
var mouseEvent = {
|
||||||
install: function(type) {
|
install: function(type) {
|
||||||
|
// If the view requires counting of installed mouse events,
|
||||||
|
// increase the counters now according to mouseFlags
|
||||||
var counters = this._project.view._eventCounters;
|
var counters = this._project.view._eventCounters;
|
||||||
|
if (counters) {
|
||||||
for (var key in mouseFlags) {
|
for (var key in mouseFlags) {
|
||||||
counters[key] = (counters[key] || 0)
|
counters[key] = (counters[key] || 0)
|
||||||
+ (mouseFlags[key][type] || 0);
|
+ (mouseFlags[key][type] || 0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
uninstall: function(type) {
|
uninstall: function(type) {
|
||||||
|
// If the view requires counting of installed mouse events,
|
||||||
|
// decrease the counters now according to mouseFlags
|
||||||
var counters = this._project.view._eventCounters;
|
var counters = this._project.view._eventCounters;
|
||||||
|
if (counters) {
|
||||||
for (var key in mouseFlags)
|
for (var key in mouseFlags)
|
||||||
counters[key] -= mouseFlags[key][type] || 0;
|
counters[key] -= mouseFlags[key][type] || 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var onFrameItems = [];
|
var onFrameItems = [];
|
||||||
|
|
|
@ -36,6 +36,8 @@ var CanvasView = View.extend(/** @lends CanvasView# */{
|
||||||
canvas = CanvasProvider.getCanvas(size);
|
canvas = CanvasProvider.getCanvas(size);
|
||||||
}
|
}
|
||||||
this._context = canvas.getContext('2d');
|
this._context = canvas.getContext('2d');
|
||||||
|
// Have Item count installed mouse events.
|
||||||
|
this._eventCounters = {};
|
||||||
this.base(canvas);
|
this.base(canvas);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -63,10 +65,40 @@ var CanvasView = View.extend(/** @lends CanvasView# */{
|
||||||
ctx.restore();
|
ctx.restore();
|
||||||
this._redrawNeeded = false;
|
this._redrawNeeded = false;
|
||||||
return true;
|
return true;
|
||||||
|
},
|
||||||
|
|
||||||
|
// Item based mouse handling:
|
||||||
|
|
||||||
|
_hitOptions: {
|
||||||
|
fill: true,
|
||||||
|
stroke: true,
|
||||||
|
tolerance: 0
|
||||||
|
},
|
||||||
|
|
||||||
|
_callEvent: function(item, event, bubble) {
|
||||||
|
var called = false;
|
||||||
|
while (item) {
|
||||||
|
called = item.fire(event.type, event) || called;
|
||||||
|
if (called && (!bubble || event._stopped))
|
||||||
|
break;
|
||||||
|
item = item.getParent();
|
||||||
|
}
|
||||||
|
return called;
|
||||||
|
},
|
||||||
|
|
||||||
|
_onMouseDown: function(event, point) {
|
||||||
|
if (this._eventCounters.mousedown) {
|
||||||
|
var hit = this._project.hitTest(point, this._hitOptions);
|
||||||
|
if (hit && hit.item) {
|
||||||
|
this._callEvent(hit.item, new MouseEvent('mousedown', point,
|
||||||
|
hit.item, event), false);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
/*#*/ if (options.server) {
|
/*#*/ if (options.server) {
|
||||||
|
// Node.js server based image exporting code.
|
||||||
CanvasView.inject(new function() {
|
CanvasView.inject(new function() {
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
// Utility function that converts a number to a string with
|
// Utility function that converts a number to a string with
|
||||||
|
|
|
@ -147,7 +147,6 @@ var View = this.View = Base.extend(Callback, /** @lends View# */{
|
||||||
size.width, size.height);
|
size.width, size.height);
|
||||||
this._matrix = new Matrix();
|
this._matrix = new Matrix();
|
||||||
this._zoom = 1;
|
this._zoom = 1;
|
||||||
this._eventCounters = {};
|
|
||||||
// Make sure the first view is focused for keyboard input straight away
|
// Make sure the first view is focused for keyboard input straight away
|
||||||
if (!View._focused)
|
if (!View._focused)
|
||||||
View._focused = this;
|
View._focused = this;
|
||||||
|
@ -439,23 +438,6 @@ var View = this.View = Base.extend(Callback, /** @lends View# */{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var hitOptions = {
|
|
||||||
fill: true,
|
|
||||||
stroke: true,
|
|
||||||
tolerance: 0
|
|
||||||
};
|
|
||||||
|
|
||||||
function callEvent(item, event, bubble) {
|
|
||||||
var called = false;
|
|
||||||
while (item) {
|
|
||||||
called = item.fire(event.type, event) || called;
|
|
||||||
if (called && (!bubble || event._stopped))
|
|
||||||
break;
|
|
||||||
item = item.getParent();
|
|
||||||
}
|
|
||||||
return called;
|
|
||||||
}
|
|
||||||
|
|
||||||
function mousedown(event) {
|
function mousedown(event) {
|
||||||
// Get the view from the event, and store a reference to the view that
|
// Get the view from the event, and store a reference to the view that
|
||||||
// should receive keyboard input.
|
// should receive keyboard input.
|
||||||
|
@ -463,21 +445,14 @@ var View = this.View = Base.extend(Callback, /** @lends View# */{
|
||||||
curPoint = viewToProject(view, event);
|
curPoint = viewToProject(view, event);
|
||||||
dragging = true;
|
dragging = true;
|
||||||
|
|
||||||
var update = false;
|
if (view._onMouseDown)
|
||||||
// TODO: Move this to CanvasView soon!
|
view._onMouseDown(event, curPoint);
|
||||||
if (view._eventCounters.mousedown) {
|
|
||||||
var hit = view._project.hitTest(curPoint, hitOptions);
|
|
||||||
if (hit && hit.item) {
|
|
||||||
update = callEvent(hit.item, new MouseEvent('mousedown',
|
|
||||||
curPoint, hit.item, event), false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tool = view._scope.tool)
|
if (tool = view._scope.tool)
|
||||||
update = tool._onHandleEvent('mousedown', curPoint, event)
|
tool._onHandleEvent('mousedown', curPoint, event);
|
||||||
|| update;
|
|
||||||
|
|
||||||
if (update)
|
// Always call draw(), but set checkRedraw = true, so we only redraw the
|
||||||
|
// view if anything has changed in the above calls
|
||||||
view.draw(true);
|
view.draw(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue