Move Item base mouse handling code to CanvasView.

This commit is contained in:
Jürg Lehni 2011-11-16 13:12:41 +01:00
parent 577c884a70
commit 57bd659023
4 changed files with 54 additions and 37 deletions

View file

@ -1,7 +1,9 @@
var fs = require('fs'),
vm = require('vm'),
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/');

View file

@ -54,16 +54,24 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{
// Entry for all mouse events in the _events list
var mouseEvent = {
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;
for (var key in mouseFlags) {
counters[key] = (counters[key] || 0)
+ (mouseFlags[key][type] || 0);
if (counters) {
for (var key in mouseFlags) {
counters[key] = (counters[key] || 0)
+ (mouseFlags[key][type] || 0);
}
}
},
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;
for (var key in mouseFlags)
counters[key] -= mouseFlags[key][type] || 0;
if (counters) {
for (var key in mouseFlags)
counters[key] -= mouseFlags[key][type] || 0;
}
}
};

View file

@ -36,6 +36,8 @@ var CanvasView = View.extend(/** @lends CanvasView# */{
canvas = CanvasProvider.getCanvas(size);
}
this._context = canvas.getContext('2d');
// Have Item count installed mouse events.
this._eventCounters = {};
this.base(canvas);
},
@ -63,10 +65,40 @@ var CanvasView = View.extend(/** @lends CanvasView# */{
ctx.restore();
this._redrawNeeded = false;
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) {
// Node.js server based image exporting code.
CanvasView.inject(new function() {
var path = require('path');
// Utility function that converts a number to a string with

View file

@ -147,7 +147,6 @@ var View = this.View = Base.extend(Callback, /** @lends View# */{
size.width, size.height);
this._matrix = new Matrix();
this._zoom = 1;
this._eventCounters = {};
// Make sure the first view is focused for keyboard input straight away
if (!View._focused)
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) {
// Get the view from the event, and store a reference to the view that
// should receive keyboard input.
@ -463,22 +445,15 @@ var View = this.View = Base.extend(Callback, /** @lends View# */{
curPoint = viewToProject(view, event);
dragging = true;
var update = false;
// TODO: Move this to CanvasView soon!
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 (view._onMouseDown)
view._onMouseDown(event, curPoint);
if (tool = view._scope.tool)
update = tool._onHandleEvent('mousedown', curPoint, event)
|| update;
tool._onHandleEvent('mousedown', curPoint, event);
if (update)
view.draw(true);
// Always call draw(), but set checkRedraw = true, so we only redraw the
// view if anything has changed in the above calls
view.draw(true);
}
function mousemove(event) {