forked from paperjs/paper.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolEvent.js
More file actions
211 lines (192 loc) · 5.13 KB
/
Copy pathToolEvent.js
File metadata and controls
211 lines (192 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* http://paperjs.org/
*
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
* http://lehni.org/ & http://jonathanpuckey.com/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
/**
* @name ToolEvent
*
* @class ToolEvent The ToolEvent object is received by the {@link Tool}'s mouse
* event handlers {@link Tool#onMouseDown}, {@link Tool#onMouseDrag},
* {@link Tool#onMouseMove} and {@link Tool#onMouseUp}. The ToolEvent
* object is the only parameter passed to these functions and contains
* information about the mouse event.
*
* @extends Event
*/
var ToolEvent = Event.extend(/** @lends ToolEvent# */{
_class: 'ToolEvent',
// Have ToolEvent#item fall back to returning null, not undefined.
_item: null,
initialize: function ToolEvent(tool, type, event) {
this.tool = tool;
this.type = type;
this.event = event;
},
/**
* Convenience method to allow local overrides of point values.
* See application below.
*/
_choosePoint: function(point, toolPoint) {
return point ? point : toolPoint ? toolPoint.clone() : null;
},
/**
* The type of tool event.
*
* @name ToolEvent#type
* @type String('mousedown', 'mouseup', 'mousemove', 'mousedrag')
*/
/**
* The position of the mouse in project coordinates when the event was
* fired.
*
* @example
* function onMouseDrag(event) {
* // the position of the mouse when it is dragged
* console.log(event.point);
* }
*
* function onMouseUp(event) {
* // the position of the mouse when it is released
* console.log(event.point);
* }
*
* @type Point
* @bean
*/
getPoint: function() {
return this._choosePoint(this._point, this.tool._point);
},
setPoint: function(point) {
this._point = point;
},
/**
* The position of the mouse in project coordinates when the previous
* event was fired.
*
* @type Point
* @bean
*/
getLastPoint: function() {
return this._choosePoint(this._lastPoint, this.tool._lastPoint);
},
setLastPoint: function(lastPoint) {
this._lastPoint = lastPoint;
},
/**
* The position of the mouse in project coordinates when the mouse button
* was last clicked.
*
* @type Point
* @bean
*/
getDownPoint: function() {
return this._choosePoint(this._downPoint, this.tool._downPoint);
},
setDownPoint: function(downPoint) {
this._downPoint = downPoint;
},
/**
* The point in the middle between {@link #lastPoint} and
* {@link #point}. This is a useful position to use when creating
* artwork based on the moving direction of the mouse, as returned by
* {@link #delta}.
*
* @type Point
* @bean
*/
getMiddlePoint: function() {
// For explanations, see getDelta()
if (!this._middlePoint && this.tool._lastPoint) {
// (point + lastPoint) / 2
return this.tool._point.add(this.tool._lastPoint).divide(2);
}
return this._middlePoint;
},
setMiddlePoint: function(middlePoint) {
this._middlePoint = middlePoint;
},
/**
* The difference between the current position and the last position of the
* mouse when the event was fired. In case of the mouseup event, the
* difference to the mousedown position is returned.
*
* @type Point
* @bean
*/
getDelta: function() {
// Do not put the calculated delta into delta, since this only reserved
// for overriding event.delta.
// Instead, keep calculating the delta each time, so the result can be
// directly modified by the script without changing the internal values.
// We could cache this and use clone, but this is almost as fast...
return !this._delta && this.tool._lastPoint
? this.tool._point.subtract(this.tool._lastPoint)
: this._delta;
},
setDelta: function(delta) {
this._delta = delta;
},
/**
* The number of times the mouse event was fired.
*
* @type Number
* @bean
*/
getCount: function() {
// Return downCount for both mouse down and up, since
// the count is the same.
return /^mouse(down|up)$/.test(this.type)
? this.tool._downCount
: this.tool._count;
},
setCount: function(count) {
this.tool[/^mouse(down|up)$/.test(this.type) ? 'downCount' : 'count']
= count;
},
/**
* The item at the position of the mouse (if any).
*
* If the item is contained within one or more {@link Group} or
* {@link CompoundPath} items, the most top level group or compound path
* that it is contained within is returned.
*
* @type Item
* @bean
*/
getItem: function() {
if (!this._item) {
var result = this.tool._scope.project.hitTest(this.getPoint());
if (result) {
var item = result.item,
// Find group parent, but exclude layers
parent = item._parent;
while (/^(group|compound-path)$/.test(parent._type)) {
item = parent;
parent = parent._parent;
}
this._item = item;
}
}
return this._item;
},
setItem: function(item) {
this._item = item;
},
/**
* @return {String} a string representation of the tool event
*/
toString: function() {
return '{ type: ' + this.type
+ ', point: ' + this.getPoint()
+ ', count: ' + this.getCount()
+ ', modifiers: ' + this.getModifiers()
+ ' }';
}
});