-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathmouse-actions.ts
277 lines (251 loc) · 9.02 KB
/
mouse-actions.ts
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* @file Mouse Actions
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @private
*/
import PickingProxy from './picking-proxy'
import { almostIdentity } from '../math/math-utils'
import Stage from '../stage/stage'
import StructureComponent from '../component/structure-component'
import SurfaceRepresentation from '../representation/surface-representation'
type ScrollCallback = (stage: Stage, delta: number) => void
type DragCallback = (stage: Stage, dx: number, dy: number) => void
type PickCallback = (stage: Stage, pickingProxy: PickingProxy) => void
export type MouseActionCallback = ScrollCallback | DragCallback | PickCallback
/**
* Mouse actions provided as static methods
*/
class MouseActions {
/**
* Zoom scene based on scroll-delta
* @param {Stage} stage - the stage
* @param {Number} delta - amount to zoom
* @return {undefined}
*/
static zoomScroll (stage: Stage, delta: number) {
stage.trackballControls.zoom(delta)
}
/**
* Move near clipping plane based on scroll-delta
* @param {Stage} stage - the stage
* @param {Number} delta - amount to move clipping plane
* @return {undefined}
*/
static clipNearScroll (stage: Stage, delta: number) {
const sp = stage.getParameters()
stage.setParameters({ clipNear: sp.clipNear + delta / 10 })
}
/**
* Move focus planes based on scroll-delta
* @param {Stage} stage - the stage
* @param {Number} delta - amount to move focus planes
* @return {undefined}
*/
static focusScroll (stage: Stage, delta: number) {
const sp = stage.getParameters()
const focus = sp.clipNear * 2
const sign = Math.sign(delta)
const step = sign * almostIdentity((100 - focus) / 10, 5, 0.2)
stage.setFocus(focus + step)
}
/**
* Zoom scene based on scroll-delta and
* move focus planes based on camera position (zoom)
* @param {Stage} stage - the stage
* @param {Number} delta - amount to move focus planes and zoom
* @return {undefined}
*/
static zoomFocusScroll (stage: Stage, delta: number) {
stage.trackballControls.zoom(delta)
const z = stage.viewer.camera.position.z
stage.setFocus(100 - Math.abs(z / 8))
}
/**
* Change isolevel of volume surfaces based on scroll-delta
* @param {Stage} stage - the stage
* @param {Number} delta - amount to change isolevel
* @return {undefined}
*/
static isolevelScroll (stage: Stage, delta: number) {
const d = Math.sign(delta) / 10
stage.eachRepresentation((reprElem, comp) => {
if (reprElem.repr instanceof SurfaceRepresentation) {
const p = reprElem.getParameters() as any // TODO
if (p.isolevelScroll) {
reprElem.setParameters({ isolevel: p.isolevel + d })
}
}
})
}
/**
* Pan scene based on mouse coordinate changes
* @param {Stage} stage - the stage
* @param {Number} dx - amount to pan in x direction
* @param {Number} dy - amount to pan in y direction
* @return {undefined}
*/
static panDrag (stage: Stage, dx: number, dy: number) {
stage.trackballControls.pan(dx, dy)
}
/**
* Rotate scene based on mouse coordinate changes
* @param {Stage} stage - the stage
* @param {Number} dx - amount to rotate in x direction
* @param {Number} dy - amount to rotate in y direction
* @return {undefined}
*/
static rotateDrag (stage: Stage, dx: number, dy: number) {
stage.trackballControls.rotate(dx, dy)
}
/**
* Rotate scene around z axis based on mouse coordinate changes
* @param {Stage} stage - the stage
* @param {Number} dx - amount to rotate in x direction
* @param {Number} dy - amount to rotate in y direction
* @return {undefined}
*/
static zRotateDrag (stage: Stage, dx: number, dy: number) {
stage.trackballControls.zRotate(dx, dy)
}
/**
* Zoom scene based on mouse coordinate changes
* @param {Stage} stage - the stage
* @param {Number} dx - amount to zoom
* @param {Number} dy - amount to zoom
* @return {undefined}
*/
static zoomDrag (stage: Stage, dx: number, dy: number) {
stage.trackballControls.zoom((dx + dy) / -2)
}
/**
* Zoom scene based on mouse coordinate changes and
* move focus planes based on camera position (zoom)
* @param {Stage} stage - the stage
* @param {Number} dx - amount to zoom and focus
* @param {Number} dy - amount to zoom and focus
* @return {undefined}
*/
static zoomFocusDrag (stage: Stage, dx: number, dy: number) {
stage.trackballControls.zoom((dx + dy) / -2)
const z = stage.viewer.camera.position.z
stage.setFocus(100 - Math.abs(z / 8))
}
/**
* Pan picked component based on mouse coordinate changes
* @param {Stage} stage - the stage
* @param {Number} dx - amount to pan in x direction
* @param {Number} dy - amount to pan in y direction
* @return {undefined}
*/
static panComponentDrag (stage: Stage, dx: number, dy: number) {
stage.trackballControls.panComponent(dx, dy)
}
/**
* Pan picked atom based on mouse coordinate changes
* @param {Stage} stage - the stage
* @param {Number} dx - amount to pan in x direction
* @param {Number} dy - amount to pan in y direction
* @return {undefined}
*/
static panAtomDrag (stage: Stage, dx: number, dy: number) {
stage.trackballControls.panAtom(dx, dy)
}
/**
* Rotate picked component based on mouse coordinate changes
* @param {Stage} stage - the stage
* @param {Number} dx - amount to rotate in x direction
* @param {Number} dy - amount to rotate in y direction
* @return {undefined}
*/
static rotateComponentDrag (stage: Stage, dx: number, dy: number) {
stage.trackballControls.rotateComponent(dx, dy)
}
/**
* Move picked element to the center of the screen
* @param {Stage} stage - the stage
* @param {PickingProxy} pickingProxy - the picking data object
* @return {undefined}
*/
static movePick (stage: Stage, pickingProxy: PickingProxy) {
if (pickingProxy) {
stage.animationControls.move(pickingProxy.position.clone())
}
}
/**
* Show tooltip with information of picked element
* @param {Stage} stage - the stage
* @param {PickingProxy} pickingProxy - the picking data object
* @return {undefined}
*/
static tooltipPick (stage: Stage, pickingProxy: PickingProxy) {
const tt = stage.tooltip
const sp = stage.getParameters() as any
if (sp.tooltip && pickingProxy) {
const mp = pickingProxy.mouse.position
tt.innerText = pickingProxy.getLabel()
tt.style.bottom = (window.innerHeight - mp.y + 3) + 'px'
tt.style.left = (mp.x + 3) + 'px'
tt.style.display = 'block'
} else {
tt.style.display = 'none'
}
}
static measurePick (stage: Stage, pickingProxy: PickingProxy) {
if (pickingProxy && (pickingProxy.atom || pickingProxy.bond)) {
const atom = pickingProxy.atom || pickingProxy.closestBondAtom
const sc = pickingProxy.component as StructureComponent
sc.measurePick(atom)
} else {
stage.measureClear()
}
}
}
type MouseActionPreset = [ string, MouseActionCallback ][]
export const MouseActionPresets = {
default: [
[ 'scroll', MouseActions.zoomScroll ],
[ 'scroll-shift', MouseActions.focusScroll ],
[ 'scroll-ctrl', MouseActions.isolevelScroll ],
[ 'scroll-shift-ctrl', MouseActions.zoomFocusScroll ],
[ 'drag-left', MouseActions.rotateDrag ],
[ 'drag-right', MouseActions.panDrag ],
[ 'drag-ctrl-left', MouseActions.panDrag ],
[ 'drag-ctrl-right', MouseActions.zRotateDrag ],
[ 'drag-shift-left', MouseActions.zoomDrag ],
[ 'drag-middle', MouseActions.zoomFocusDrag ],
[ 'drag-ctrl-shift-right', MouseActions.panComponentDrag ],
[ 'drag-ctrl-shift-left', MouseActions.rotateComponentDrag ],
[ 'clickPick-right', MouseActions.measurePick ],
[ 'clickPick-ctrl-left', MouseActions.measurePick ],
[ 'clickPick-middle', MouseActions.movePick ],
[ 'clickPick-left', MouseActions.movePick ],
[ 'hoverPick', MouseActions.tooltipPick ]
] as MouseActionPreset,
pymol: [
[ 'drag-left', MouseActions.rotateDrag ],
[ 'drag-middle', MouseActions.panDrag ],
[ 'drag-right', MouseActions.zoomDrag ],
[ 'drag-shift-right', MouseActions.focusScroll ],
[ 'clickPick-ctrl+shift-middle', MouseActions.movePick ],
[ 'hoverPick', MouseActions.tooltipPick ]
] as MouseActionPreset,
coot: [
[ 'scroll', MouseActions.isolevelScroll ],
[ 'drag-left', MouseActions.rotateDrag ],
[ 'drag-middle', MouseActions.panDrag ],
[ 'drag-ctrl-left', MouseActions.panDrag ],
[ 'drag-right', MouseActions.zoomFocusDrag ],
[ 'drag-ctrl-right', MouseActions.focusScroll ],
[ 'clickPick-middle', MouseActions.movePick ],
[ 'hoverPick', MouseActions.tooltipPick ]
] as MouseActionPreset,
astexviewer: [
[ 'drag-left', MouseActions.rotateDrag ],
[ 'drag-ctrl-left', MouseActions.panDrag ],
[ 'drag-shift-left', MouseActions.zoomDrag ],
[ 'scroll', MouseActions.focusScroll ],
[ 'clickPick-middle', MouseActions.movePick ],
[ 'hoverPick', MouseActions.tooltipPick ]
] as MouseActionPreset
}
export default MouseActions