Repository navigation
Expand file tree
/
Copy pathcursor-target.ts
More file actions
195 lines (181 loc) · 5.45 KB
/
Copy pathcursor-target.ts
File metadata and controls
195 lines (181 loc) · 5.45 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
import {Component, Object3D, Emitter} from '@wonderlandengine/api';
import {Cursor, CursorEvents, EventTypes} from './cursor.js';
import {FingerCursor} from './finger-cursor.js';
/**
* Click/hover/move/button target for [cursor](#cursor).
*
* To trigger code when clicking, hovering, unhovering, moving cursor, pressing
* cursor button or releasing cursor button, use `.addClickFunction(f)`,
* `.addHoverFunction(f)`, `.addUnHoverFunction(f)`,
* `.addMoveFunction(f)`, `.addDownFunction(f)` and
* `.addUpFunction(f)` respectively with any `function f() {}`.
*
* To call members on a different component, you can set up a cursor target like
* so:
*
* ```js
* start: function() {
* let target = this.object.addComponent('cursor-target');
* target.onClick.add(this.onClick.bind(this));
* },
* onClick: function() {
* console.log(this.object.name, "was clicked!");
* }
* ```
* **Functions:**
*
* ```js
* const target = this.object.getComponent(CursorTarget);
* const callback = function(object, cursorComponent) {};
*
* target.onHover.add(callback);
* target.onHover.remove(callback);
*
* target.onUnHover.add(callback);
* target.onUnHover.remove(callback);
*
* target.onClick.add(callback);
* target.onClick.remove(callback);
*
* target.onMove.add(callback);
* target.onMove.remove(callback);
*
* target.onDown.add(callback);
* target.onDown.remove(callback);
*
* target.onUp.add(callback);
* target.onUp.remove(callback);
* ```
*
* **Requirements:**
* - a `collision` component to be attached to the same object.
*
* See [Animation Example](/showcase/animation).
*/
export class CursorTarget
extends Component
implements CursorEvents<Object3D, Cursor | FingerCursor>
{
static TypeName = 'cursor-target';
static Properties = {};
/** Emitter for events when the target is hovered */
onHover = new Emitter<[Object3D, Cursor | FingerCursor, EventTypes?]>();
/** Emitter for events when the target is unhovered */
onUnhover = new Emitter<[Object3D, Cursor | FingerCursor, EventTypes?]>();
/** Emitter for events when the target is clicked */
onClick = new Emitter<[Object3D, Cursor | FingerCursor, EventTypes?]>();
/** Emitter for events when the cursor moves on the target */
onMove = new Emitter<[Object3D, Cursor | FingerCursor, EventTypes?]>();
/** Emitter for events when the user pressed the select button on the target */
onDown = new Emitter<[Object3D, Cursor | FingerCursor, EventTypes?]>();
/** Emitter for events when the user unpressed the select button on the target */
onUp = new Emitter<[Object3D, Cursor | FingerCursor, EventTypes?]>();
/**
* @deprecated Use the emitter instead.
*
* @example
* this.onHover.add(f);
*/
addHoverFunction(f: (object?: Object3D, cursor?: Cursor | FingerCursor) => void) {
this.onHover.add(f);
}
/**
* @deprecated Use the emitter instead.
*
* @example
* this.onHover.remove(f);
*/
removeHoverFunction(f: (object?: Object3D, cursor?: Cursor | FingerCursor) => void) {
this.onHover.remove(f);
}
/**
* @deprecated Use the emitter instead.
*
* @example
* this.onUnhover.add(f);
*/
addUnHoverFunction(f: (object?: Object3D, cursor?: Cursor | FingerCursor) => void) {
this.onUnhover.add(f);
}
/**
* @deprecated Use the emitter instead.
*
* @example
* this.onUnhover.remove(f);
*/
removeUnHoverFunction(f: (object?: Object3D, cursor?: Cursor | FingerCursor) => void) {
this.onUnhover.remove(f);
}
/**
* @deprecated Use the emitter instead.
*
* @example
* this.onClick.add(f);
*/
addClickFunction(f: (object?: Object3D, cursor?: Cursor | FingerCursor) => void) {
this.onClick.add(f);
}
/**
* @deprecated Use the emitter instead.
*
* @example
* component.onClick.remove(f);
*/
removeClickFunction(f: (object?: Object3D, cursor?: Cursor | FingerCursor) => void) {
this.onClick.remove(f);
}
/**
* @deprecated Use the emitter instead.
*
* @example
* component.onMove.add(f);
*/
addMoveFunction(f: (object?: Object3D, cursor?: Cursor | FingerCursor) => void) {
this.onMove.add(f);
}
/**
* @deprecated Use the emitter instead.
*
* @example
* component.onMove.remove(f);
*/
removeMoveFunction(f: (object?: Object3D, cursor?: Cursor | FingerCursor) => void) {
this.onMove.remove(f);
}
/**
* @deprecated Use the emitter instead.
*
* @example
* component.onDown.add(f);
*/
addDownFunction(f: (object?: Object3D, cursor?: Cursor | FingerCursor) => void) {
this.onDown.add(f);
}
/**
* @deprecated Use the emitter instead.
*
* @example
* component.onDown.remove(f);
*/
removeDownFunction(f: (object?: Object3D, cursor?: Cursor | FingerCursor) => void) {
this.onDown.remove(f);
}
/**
* @deprecated Use the emitter instead.
*
* @example
* component.onUp.add(f);
*/
addUpFunction(f: (object?: Object3D, cursor?: Cursor | FingerCursor) => void) {
this.onUp.add(f);
}
/**
* @deprecated Use the emitter instead.
*
* @example
* component.onUp.remove(f);
*/
removeUpFunction(f: (object?: Object3D, cursor?: Cursor | FingerCursor) => void) {
this.onUp.remove(f);
}
}