Skip to content

Commit e4586cf

Browse files
committed
Commit beginning of item based mouse events. So far only onMousedown / 'mousedown' is supported.
1 parent 18d4468 commit e4586cf

6 files changed

Lines changed: 132 additions & 9 deletions

File tree

src/core/Callback.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var Callback = {
3232
// See if this is the first handler that we're attaching, and
3333
// call install if defined.
3434
if (entry.install && handlers.length == 1)
35-
entry.install.call(this);
35+
entry.install.call(this, type);
3636
}
3737
return this;
3838
},
@@ -53,7 +53,7 @@ var Callback = {
5353
if (!func || (index = handlers.indexOf(func)) != -1
5454
&& handlers.length == 1) {
5555
if (entry.uninstall)
56-
entry.uninstall.call(this);
56+
entry.uninstall.call(this, type);
5757
delete this._handlers[type];
5858
} else if (index != -1) {
5959
// Just remove this one handler

src/item/Item.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,58 @@
2626
*/
2727
var Item = this.Item = Base.extend(Callback, /** @lends Item# */{
2828
_events: new function() {
29+
30+
// Flags defining which native events are required by which Paper events
31+
// as required for counting amount of necessary natives events.
32+
// The mapping is native -> virtual
33+
var mouseFlags = {
34+
mousedown: {
35+
mousedown: 1,
36+
mousedrag: 1,
37+
click: 1,
38+
doubleclick: 1
39+
},
40+
mouseup: {
41+
mouseup: 1,
42+
mousedrag: 1,
43+
click: 1,
44+
doubleclick: 1
45+
},
46+
mousemove: {
47+
mousedrag: 1,
48+
mousemove: 1,
49+
mouseenter: 1,
50+
mouseleave: 1
51+
}
52+
};
53+
54+
// Entry for all mouse events in the _events list
55+
var mouseEvent = {
56+
install: function(type) {
57+
var counters = this._project.view._eventCounters;
58+
for (var key in mouseFlags) {
59+
counters[key] = (counters[key] || 0)
60+
+ (mouseFlags[key][type] || 0);
61+
}
62+
},
63+
uninstall: function(type) {
64+
var counters = this._project.view._eventCounters;
65+
for (var key in mouseFlags)
66+
counters[key] -= mouseFlags[key][type] || 0;
67+
}
68+
};
69+
2970
var onFrameItems = [];
3071
function onFrame(event) {
3172
for (var i = 0, l = onFrameItems.length; i < l; i++)
3273
onFrameItems[i].fire('frame', event);
3374
}
3475

35-
return {
76+
return Base.each(['onMouseDown', 'onMouseUp', 'onMouseDrag', 'onClick',
77+
'onDoubleClick', 'onMouseMove', 'onMouseEnter', 'onMouseLeave'],
78+
function(name) {
79+
this[name] = mouseEvent;
80+
}, {
3681
onFrame: {
3782
install: function() {
3883
if (!onFrameItems.length)
@@ -45,7 +90,7 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{
4590
this._project.view.detach('frame', onFrame);
4691
}
4792
}
48-
};
93+
});
4994
},
5095

5196
initialize: function() {

src/paper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ var paper = new function() {
114114
/*#*/ include('ui/Event.js');
115115
/*#*/ include('ui/KeyEvent.js');
116116
/*#*/ include('ui/Key.js');
117+
/*#*/ include('ui/MouseEvent.js');
117118

118119
/*#*/ include('tool/ToolEvent.js');
119120
/*#*/ include('tool/Tool.js');

src/ui/Event.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,19 @@ var Event = this.Event = Base.extend(/** @lends Event# */{
2525

2626
// PORT: Add to Scriptographer
2727
preventDefault: function() {
28+
this._prevented = true;
2829
DomEvent.preventDefault(this.event);
30+
return this;
2931
},
3032

3133
stopPropagation: function() {
34+
this._stopped = true;
3235
DomEvent.stopPropagation(this.event);
36+
return this;
3337
},
3438

3539
stop: function() {
36-
DomEvent.stop(this.event);
40+
return this.stopPropagation().preventDefault();
3741
},
3842

3943
// DOCS: Document Event#modifiers

src/ui/MouseEvent.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Paper.js
3+
*
4+
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
5+
* based on Scriptographer.org and designed to be largely API compatible.
6+
* http://paperjs.org/
7+
* http://scriptographer.org/
8+
*
9+
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
10+
* http://lehni.org/ & http://jonathanpuckey.com/
11+
*
12+
* Distributed under the MIT license. See LICENSE file for details.
13+
*
14+
* All rights reserved.
15+
*/
16+
17+
/**
18+
* @name MouseEvent
19+
*
20+
* @extends Event
21+
*/
22+
var MouseEvent = this.MouseEvent = Event.extend(new function() {
23+
return /** @lends MouseEvent# */{
24+
initialize: function(type, point, target, event) {
25+
this.base(event);
26+
this.type = type;
27+
this.point = point;
28+
this.target = target;
29+
},
30+
31+
/**
32+
* @return {String} A string representation of the key event.
33+
*/
34+
toString: function() {
35+
return '{ type: ' + this.type
36+
+ ', point: ' + this.point
37+
+ ', target: ' + this.target
38+
+ ', modifiers: ' + this.getModifiers()
39+
+ ' }';
40+
}
41+
};
42+
});

src/ui/View.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ var View = this.View = Base.extend(Callback, /** @lends View# */{
173173
this._context = this._canvas.getContext('2d');
174174
this._matrix = new Matrix();
175175
this._zoom = 1;
176+
this._eventCounters = {};
176177
// Make sure the first view is focused for keyboard input straight away
177178
if (!View._focused)
178179
View._focused = this;
@@ -553,19 +554,49 @@ var View = this.View = Base.extend(Callback, /** @lends View# */{
553554
load: updateFocus
554555
});
555556

557+
var hitOptions = {
558+
fill: true,
559+
stroke: true,
560+
tolerance: 0
561+
};
562+
563+
function callEvent(item, event, bubble) {
564+
var called = false;
565+
while (item) {
566+
called = item.fire(event.type, event) || called;
567+
if (called && (!bubble || event._stopped))
568+
break;
569+
item = item.getParent();
570+
}
571+
return called;
572+
}
573+
556574
return {
557575
_createHandlers: function() {
558576
var view = this;
559577

560578
function mousedown(event) {
561579
// Tell the Key class which view should receive keyboard input.
562580
View._focused = view;
563-
if (!(tool = view._scope.tool))
564-
return;
565581
curPoint = viewToProject(view, event);
566-
if (tool.onHandleEvent('mousedown', curPoint, event))
567-
view.draw(true);
568582
dragging = true;
583+
584+
var update = false;
585+
// TODO: Move this to CanvasView soon!
586+
if (view._eventCounters.mousedown) {
587+
var hit = view._project.hitTest(curPoint, hitOptions);
588+
if (hit && hit.item) {
589+
update = callEvent(hit.item, new MouseEvent('mousedown',
590+
curPoint, hit.item, event), false);
591+
}
592+
}
593+
594+
if (tool = view._scope.tool)
595+
update = tool.onHandleEvent('mousedown', curPoint, event)
596+
|| update;
597+
598+
if (update)
599+
view.draw(true);
569600
}
570601

571602
return {

0 commit comments

Comments
 (0)