-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
BuilderGameView.ts
254 lines (225 loc) · 7.93 KB
/
BuilderGameView.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
import { Plane, Vector3 } from 'three';
import Component from 'vue-class-component';
import { Inject, Provide, Prop, Watch } from 'vue-property-decorator';
import {
getFileConfigContexts,
createContextId,
AuxCausalTree,
AuxOp,
createWorkspace,
FilesState,
duplicateFile,
toast,
cleanFile,
pasteState,
PasteStateOptions,
} from '@casual-simulation/aux-common';
import { StoredCausalTree } from '@casual-simulation/causal-trees';
import { appManager } from '../../shared/AppManager';
import { keys } from 'lodash';
import BuilderApp from '../BuilderApp/BuilderApp';
import MiniFile from '../MiniFile/MiniFile';
import { IGameView } from '../../shared/vue-components/IGameView';
import BuilderHome from '../BuilderHome/BuilderHome';
import { isMac, copyFilesFromSimulation } from '../../shared/SharedUtils';
import BaseGameView from '../../shared/vue-components/BaseGameView';
import { BuilderGame } from '../scene/BuilderGame';
import { Game } from '../../shared/scene/Game';
import { SubscriptionLike } from 'rxjs';
import { map, tap } from 'rxjs/operators';
import { Physics } from '../../shared/scene/Physics';
import { ContextGroup3D } from 'aux-web/shared/scene/ContextGroup3D';
import { BuilderInteractionManager } from '../interaction/BuilderInteractionManager';
@Component({
components: {
'mini-file': MiniFile,
},
})
export default class BuilderGameView extends BaseGameView implements IGameView {
_game: BuilderGame = null;
showUploadFiles: boolean = false;
showCameraHome: boolean = false;
@Inject() addSidebarItem: BuilderApp['addSidebarItem'];
@Inject() removeSidebarItem: BuilderApp['removeSidebarItem'];
@Inject() removeSidebarGroup: BuilderApp['removeSidebarGroup'];
@Inject() home: BuilderHome;
@Inject() buildApp: BuilderApp;
@Prop() channelId: string;
@Watch('channelId')
onChannelIdChanged() {
this.rebuildGame();
}
constructor() {
super();
}
protected createGame(): Game {
return new BuilderGame(appManager.simulationManager.primary, this);
}
protected setupCore() {
this._subscriptions.push(
this._game
.watchCameraRigDistanceSquared(this._game.mainCameraRig)
.pipe(
map(distSqr => distSqr >= 75),
tap(visible => (this.showCameraHome = visible))
)
.subscribe()
);
}
onDragEnter(event: DragEvent) {
if (event.dataTransfer.types.indexOf('Files') >= 0) {
this.showUploadFiles = true;
event.dataTransfer.dropEffect = 'copy';
event.preventDefault();
}
}
onDragOver(event: DragEvent) {
if (event.dataTransfer.types.indexOf('Files') >= 0) {
this.showUploadFiles = true;
event.dataTransfer.dropEffect = 'copy';
event.preventDefault();
}
}
onDragLeave(event: DragEvent) {
this.showUploadFiles = false;
}
centerCamera() {
this._game.onCenterCamera(this._game.mainCameraRig);
}
async onDrop(event: DragEvent) {
this.showUploadFiles = false;
event.preventDefault();
let auxFiles: Bot[] = [];
if (event.dataTransfer.items) {
for (let i = 0; i < event.dataTransfer.items.length; i++) {
const item = event.dataTransfer.items[i];
if (item.kind === 'file') {
const file = item.getAsFile();
if (file.name.endsWith('.aux')) {
auxFiles.push(file);
}
}
}
} else {
for (let i = 0; i < event.dataTransfer.files.length; i++) {
const file = event.dataTransfer.files.item(i);
if (file.name.endsWith('.aux')) {
auxFiles.push(file);
}
}
}
if (auxFiles.length > 0) {
console.log(
`[BuilderGameView] Uploading ${auxFiles.length} ${
auxFiles.length === 1 ? 'file' : 'files'
}`
);
await Promise.all(
auxFiles.map(file => appManager.uploadState(file))
);
}
}
copySelectionMac() {
if (isMac()) {
this._copySelection();
}
}
copySelectionNormal() {
if (!isMac()) {
this._copySelection();
}
}
pasteClipboardMac() {
if (isMac()) {
this._pasteClipboard();
}
}
pasteClipboardNormal() {
if (!isMac()) {
this._pasteClipboard();
}
}
private async _copySelection() {
const sim = appManager.simulationManager.primary;
const files = sim.selection.getSelectedFilesForUser(
sim.helper.userFile
);
if (files.length === 0) {
appManager.simulationManager.primary.helper.transaction(
toast('Nothing selected to copy!')
);
return;
}
await copyFilesFromSimulation(sim, files);
appManager.simulationManager.primary.helper.transaction(
toast('Selection Copied!')
);
}
private async _pasteClipboard() {
if (navigator.clipboard) {
try {
const calc = appManager.simulationManager.primary.helper.createContext();
// TODO: Cleanup this function
const json = await navigator.clipboard.readText();
const stored: StoredCausalTree<AuxOp> = JSON.parse(json);
let tree = new AuxCausalTree(stored);
await tree.import(stored);
const fileIds = keys(tree.value);
const interaction = this._game.getInteraction() as BuilderInteractionManager;
const mouseDir = Physics.screenPosToRay(
this._game.getInput().getMouseScreenPos(),
this._game.mainCameraRig.mainCamera
);
const point = Physics.pointOnPlane(
mouseDir,
new Plane(new Vector3(0, 1, 0))
);
let options: PasteStateOptions = {
x: point.x,
y: point.z,
z: point.y,
};
const {
good,
gridPosition,
workspace,
} = interaction.pointOnWorkspaceGrid(
calc,
this._game.getInput().getMousePagePos()
);
if (good) {
options.context = interaction.firstContextInWorkspace(
workspace
);
options.x = gridPosition.x;
options.y = gridPosition.y;
options.z = 0;
}
appManager.simulationManager.primary.helper.transaction(
pasteState(tree.value, options),
toast(
`${fileIds.length} ${
fileIds.length === 1 ? 'file' : 'files'
} pasted!`
)
);
} catch (ex) {
console.error('[BuilderGameView] Paste failed', ex);
appManager.simulationManager.primary.helper.transaction(
toast(
"Couldn't paste your clipboard. Have you copied a selection or worksurface?"
)
);
}
} else {
console.error(
"[BuilderGameView] Browser doesn't support clipboard API!"
);
appManager.simulationManager.primary.helper.transaction(
toast(
"Sorry, but your browser doesn't support pasting files from a selection or worksurface."
)
);
}
}
}