From 57bd6590239095510fb8d4332db994a2dc5aacca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Wed, 16 Nov 2011 13:12:41 +0100 Subject: [PATCH] Move Item base mouse handling code to CanvasView. --- node.js/index.js | 4 +++- src/item/Item.js | 18 +++++++++++++----- src/ui/CanvasView.js | 32 ++++++++++++++++++++++++++++++++ src/ui/View.js | 37 ++++++------------------------------- 4 files changed, 54 insertions(+), 37 deletions(-) diff --git a/node.js/index.js b/node.js/index.js index a619f911..82e7669b 100644 --- a/node.js/index.js +++ b/node.js/index.js @@ -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/'); diff --git a/src/item/Item.js b/src/item/Item.js index d235eda0..d816f209 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -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; + } } }; diff --git a/src/ui/CanvasView.js b/src/ui/CanvasView.js index 485bdca8..335be296 100644 --- a/src/ui/CanvasView.js +++ b/src/ui/CanvasView.js @@ -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 diff --git a/src/ui/View.js b/src/ui/View.js index 09400633..e4eadfd9 100644 --- a/src/ui/View.js +++ b/src/ui/View.js @@ -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) {