-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDragControls.js
More file actions
203 lines (140 loc) · 4.68 KB
/
DragControls.js
File metadata and controls
203 lines (140 loc) · 4.68 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
/*
* DragControls dragging of three.js objects in any scene easily.
*
* Usage: new DragControls(camera, objects, renderer.domElement);
*
* feature requests:
* 1. add rotation?
* 2. axis lock
* 3. inertia dragging
* 4. activate/deactivate? prevent propagation?
*
* issues:
* deal with object parent's matrix?
*
* @author zz85 from https://github.com/zz85
* follow on http://twitter.com/blurspline
*/
THREE.DragControls = function(_camera, _objects, _domElement) {
if (_objects instanceof THREE.Scene) {
_objects = _objects.children;
}
var _projector = new THREE.Projector();
var _mouse = new THREE.Vector3(),
_offset = new THREE.Vector3();
var _selected;
var me = this;
_domElement.addEventListener('mousemove', onDocumentMouseMove, false);
_domElement.addEventListener('mousedown', onDocumentMouseDown, false);
_domElement.addEventListener('mouseup', onDocumentMouseUp, false);
// Enable Drag
this.enabled = true;
/* Custom Event Handling */
var _listeners = {
};
this.on = function(event, handler) {
if (!_listeners[event]) _listeners[event] = [];
_listeners[event].push(handler);
return me;
};
this.off = function(event, handler) {
var l = _listeners[event];
if (!l) return me;
if (l.indexOf(handler)>-1) {
l.splice(handler, 1);
}
return me;
};
var notify = function(event, data, member) {
var l = _listeners[event];
if (!l) return;
if (!member) {
for (var i=0;i<l.length;i++) {
l[i](data);
}
}
};
this.setObjects = function(objects) {
if (objects instanceof THREE.Scene) {
_objects = objects.children;
} else {
_objects = objects;
}
};
// Drag constrains (eg. move along x-axis only, y only, x and y, or default xyz)
var moveX, moveY, moveZ;
moveX = moveY = moveZ = true;
this.constrains = function(xyz) {
if (xyz === undefined)
xyz = 'xyz';
moveX = moveY = moveZ = false;
if (xyz.indexOf('x') > -1) {
moveX = true;
}
if (xyz.indexOf('y') > -1) {
moveY = true;
}
if (xyz.indexOf('z') > -1) {
moveZ = true;
}
return this;
};
function onDocumentMouseMove(event) {
if (!me.enabled) return;
event.preventDefault();
_mouse.x = (event.clientX / _domElement.width) * 2 - 1;
_mouse.y = -(event.clientY / _domElement.height) * 2 + 1;
var ray = _projector.pickingRay(_mouse, _camera);
if (me.enabled && _selected) {
var targetPos = ray.ray.direction.clone().multiplyScalar(_selected.distance).add(ray.ray.origin);
targetPos.sub(_offset);
// _selected.object.position.copy(targetPos.subSelf(_offset));
// Reverse Matrix?
// Hmm, quick hack - should do some kind of planar projection..
if (moveX) _selected.object.position.x = targetPos.x;
if (moveY) _selected.object.position.y = targetPos.y;
if (moveZ) _selected.object.position.z = targetPos.z;
notify('drag', _selected);
return;
}
var intersects = ray.intersectObjects(_objects);
if (intersects.length > 0) {
_domElement.style.cursor = 'pointer';
} else {
_domElement.style.cursor = 'auto';
}
}
function onDocumentMouseDown(event) {
if (!me.enabled) return;
event.preventDefault();
_mouse.x = (event.clientX / _domElement.width) * 2 - 1;
_mouse.y = -(event.clientY / _domElement.height) * 2 + 1;
var ray = _projector.pickingRay(_mouse, _camera);
var intersects = ray.intersectObjects(_objects);
var hit = intersects.length > 0;
if (hit) {
_selected = intersects[0];
_offset.copy(_selected.point).subSelf(_selected.object.position);
_domElement.style.cursor = 'move';
_selected.hit = hit;
notify('dragstart', _selected);
notify('mousedown', _selected);
} else {
notify('mousedown', { hit: hit });
}
}
function onDocumentMouseUp(event) {
if (!me.enabled) return;
event.preventDefault();
var dragged = false;
if (_selected) {
dragged = true;
notify('dragend', _selected);
_selected = null;
}
_domElement.style.cursor = 'auto';
notify('mouseup', {
dragged: dragged
});
}
}