-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
813 lines (685 loc) · 25.3 KB
/
map.js
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
/** Main logic: construct and manage scene, handle events, ... */
import collect from 'collect.js'
import { DragControls } from 'three/addons/controls/DragControls.js'
import { MapControls } from 'three/addons/controls/MapControls.js'
import Stats from 'stats.js'
import * as THREE from 'three'
import * as statsLogs from './utils/statsLogs.js'
import { ClassCategoryEntity, ClassEntity, FieldEntity, ObjectEntity, PackageEntity, TraceEntity } from './graph.js'
import { Player, Timeline } from './player.js'
import { TraceReader } from './trace.js'
const MouseButtonFlags = {
LEFT: 1,
MIDDLE: 2,
RIGHT: 4,
BACK: 8,
FORWARD: 16
}
export class EntityBuilder {
static forStyle(style = undefined) {
if (style == undefined) return this.forStyle('flatFDG')
switch (style) {
case 'hierarchical': return HierarchicalEntityBuilder
case 'flatFDG': return FlatFDGEntityBuilder
}
throw new Error(`Unknown style: ${style}`)
}
static newForStyle(style, trace = undefined) {
return new (this.forStyle(style))(trace)
}
constructor(trace) {
this.trace = trace
}
buildAllFieldEntities(object, parentEntity) {
let fields = collect(object.fields)
const maxFields = 20
fields = fields.take(maxFields)
return fields.map((field, name) => {
return this.buildFieldEntities(name, field, parentEntity)
})
}
buildFieldEntities(name, field, parentEntity) {
let primary = null
const fieldEntities = ['front', 'left', 'back', 'right'].map(direction => {
const fieldEntity = new FieldEntity(name, field)
fieldEntity.side = direction
if (primary === null) {
primary = fieldEntity
} else {
fieldEntity.primary = primary
}
parentEntity.addChild(fieldEntity)
return fieldEntity
})
fieldEntities.forEach(fieldEntity => {
fieldEntity.twins = fieldEntities.filter(otherFieldEntity => otherFieldEntity !== fieldEntity)
})
return fieldEntities
}
}
export class HierarchicalEntityBuilder extends EntityBuilder {
allFieldEntities = new Map()
objectEntities = new Map()
classEntities = new Map()
classCategoryEntities = new Map()
packageEntities = new Map()
traceEntity = null
build(traceMap) {
collect(this.trace.classes).each($class => {
this.getClassEntity($class)
})
collect(this.trace.objects).each(object => {
this.getObjectEntity(object)
const maxFields = 20
collect(object.fields).take(maxFields).each((field, name) => {
this.getFieldEntities(name, field, object)
})
})
const traceEntity = this.getTraceEntity()
traceEntity.sortAllChildren()
return traceEntity.build(traceMap)
}
getFieldEntities(name, field, object) {
let fieldEntities = this.allFieldEntities.get(field)
if (fieldEntities) return fieldEntities
const objectEntity = this.getObjectEntity(object)
this.buildFieldEntities(name, field, objectEntity)
this.allFieldEntities.set(field, fieldEntities)
return fieldEntities
}
getObjectEntity(object) {
let objectEntity = this.objectEntities.get(object)
if (objectEntity) return objectEntity
objectEntity = new ObjectEntity(object)
this.getClassEntity(object.class).addChild(objectEntity)
this.objectEntities.set(object, objectEntity)
return objectEntity
}
getClassEntity($class) {
let classEntity = this.classEntities.get($class)
if (classEntity) return classEntity
classEntity = new ClassEntity($class)
this.getClassCategoryEntity($class.category).addChild(classEntity)
this.classEntities.set($class, classEntity)
return classEntity
}
getClassCategoryEntity(category) {
let classCategoryEntity = this.classCategoryEntities.get(category)
if (classCategoryEntity) return classCategoryEntity
classCategoryEntity = new ClassCategoryEntity(category)
this.getPackageEntity(category.package).addChild(classCategoryEntity)
this.classCategoryEntities.set(category, classCategoryEntity)
return classCategoryEntity
}
getPackageEntity($package) {
let packageEntity = this.packageEntities.get($package)
if (packageEntity) return packageEntity
packageEntity = new PackageEntity($package)
this.getTraceEntity().addChild(packageEntity)
this.packageEntities.set($package, packageEntity)
return packageEntity
}
getTraceEntity() {
if (this.traceEntity) return this.traceEntity
this.traceEntity = new TraceEntity(this.trace)
return this.traceEntity
}
}
export class FlatFDGEntityBuilder extends EntityBuilder {
excludedObjectNames = []
excludedClassNames = [
'Boolean', 'True', 'False',
'UndefinedObject',
'SmallInteger', 'LargePositiveInteger', 'LargeNegativeInteger', 'SmallFloat64',
'FullBlockClosure', 'CompiledBlock', 'CompiledMethod', 'CompiledMethodTrailer',
'Array', 'OrderedCollection',
'Bitmap', 'Point', 'Rectangle',
'ByteSymbol',
'Process', 'ProcessorScheduler', 'Mutex',
'Association', 'DependentsArray', 'Heap', 'IdentityDictionary', 'IdentitySet', 'Interval', 'SparseLargeTable', 'WeakArray', 'WeakIdentityKeyDictionary', 'WeakKeyAssociation', 'WeakSet', 'WeakValueAssociation',
'Environment'
]
excludeClasses = true
/** all values may be a factor, a function, or undefined */
forceWeights = {
references: 1,
organization: {
/** will be applied to all organization forces */
force: .005,
sameClass: 2,
sameHierarchy: 1,
sameCategory: .01,
samePackage: .001
},
communication: 0.0001,
globalFactor: 1,
repulsion: .2
}
build(traceMap) {
const objectEntities = this.trace.objects
.filter(object => this.shouldShowObject(object))
.map(object => this.buildObjectEntity(object))
const traceEntity = new TraceEntity(this.trace)
objectEntities.forEach(objectEntity => traceEntity.addChild(objectEntity))
traceEntity.sortAllChildren()
this.addConnections(objectEntities)
const plane = traceEntity.build(traceMap)
traceEntity.layoutFDG(traceMap, this.computeForces.bind(this))
return plane
}
shouldShowObject(object) {
if (this.excludedClassNames.includes(object.class.name)) return false
if (this.excludedObjectNames.includes(object.name)) return false
if (this.excludeClasses && object.class.name.endsWith(' class')) return false
return true
}
buildObjectEntity(object) {
const entity = new ObjectEntity(object)
this.buildAllFieldEntities(object, entity)
return entity
}
addConnections(objectEntities) {
objectEntities.forEach(objectEntity => {
objectEntities.forEach(otherObjectEntity => {
collect(objectEntity.object.fields).each((field, name) => {
if (field === otherObjectEntity.object) {
objectEntity.addConnection(name, otherObjectEntity, 1)
}
})
})
})
}
computeForces(objectEntities, addForce) {
const force = (forceWeight, $default) => forceWeight === undefined
? force($default)
: forceWeight instanceof Function
? (value) => forceWeight(value ?? 1) * (this.forceWeights['globalFactor'] ?? 1)
: (value) => (value ?? 1) * forceWeight * (this.forceWeights['globalFactor'] ?? 1)
// references
if (this.forceWeights['references']) {
const referenceForce = force(this.forceWeights['references'])
objectEntities.forEach(objectEntity => {
const object = objectEntity.object
objectEntities.forEach(otherObjectEntity => {
const otherObject = otherObjectEntity.object
const referenceCount = collect(object.fields).filter(field => field === otherObject).count()
if (referenceCount > 0) {
addForce(objectEntity, otherObjectEntity, referenceForce(referenceCount))
}
})
})
}
// organization
if (this.forceWeights['organization']) {
const organizationForce = force(this.forceWeights['organization']['force'], 1)
const sameClassForce = force(this.forceWeights['organization']['sameClass'])
const sameHierarchyForce = force(this.forceWeights['organization']['sameHierarchy'])
const sameCategoryForce = force(this.forceWeights['organization']['sameCategory'])
const samePackageForce = force(this.forceWeights['organization']['samePackage'])
objectEntities.forEach(objectEntity => {
const object = objectEntity.object
objectEntities.forEach(otherObjectEntity => {
const otherObject = otherObjectEntity.object
let force = 0
if (sameClassForce) {
if (object.class === otherObject.class) {
force += sameClassForce()
}
}
if (sameCategoryForce) {
if (object.class.category === otherObject.class.category) {
force += sameCategoryForce()
}
}
if (sameHierarchyForce) {
if (object.class.category === otherObject.class.category) { // optimization
// TODO HACK: regex-specific! implement common superclasses force instead.
const isRegexAST = /^Rxs[A-Z]/.test(object.class.name)
const otherIsRegexAST = /^Rxs[A-Z]/.test(otherObject.class.name)
if (isRegexAST && otherIsRegexAST) {
force += sameHierarchyForce()
}
const isRegexNFA = /^Rxm[A-Z]/.test(object.class.name)
const otherIsRegexNFA = /^Rxm[A-Z]/.test(otherObject.class.name)
if (isRegexNFA && otherIsRegexNFA) {
force += sameHierarchyForce()
}
}
}
if (samePackageForce) {
if (object.class.category.package === otherObject.class.category.package) {
force += samePackageForce()
}
}
if (force > 0) {
addForce(objectEntity, otherObjectEntity, organizationForce(force))
}
})
})
}
// communication
if (this.forceWeights['communication']) {
const communicationForce = force(this.forceWeights['communication'])
const visitFrame = (frame) => {
const senderEntity = objectEntities.find(objectEntity => objectEntity.object === frame.receiver)
frame.children.forEach(childFrame => {
const receiverEntity = objectEntities.find(objectEntity => objectEntity.object === childFrame.receiver)
if (senderEntity != null && receiverEntity != null) {
addForce(senderEntity, receiverEntity, communicationForce())
}
visitFrame(childFrame)
})
}
visitFrame(this.trace.rootFrame)
}
}
}
export class TraceMap {
constructor(options = {}) {
this.options = options
}
defaultStyle = 'flatFDG'
//#region building
buildMap(domElement) {
this.scene = new THREE.Scene()
this.camera = new THREE.PerspectiveCamera()
// Bird's eye view
this.camera.position.set(0, 50, 100)
// increase the far clipping plane to see the whole plane - TODO: don't hardcode
this.camera.far = 10000
this.renderer = new THREE.WebGLRenderer()
this.window = domElement.ownerDocument.defaultView || domElement.ownerDocument.parentWindow
this.window.traceMap = this
const threeElement = domElement.querySelector('#three')
threeElement.appendChild(this.renderer.domElement)
new ResizeObserver(() => this.updateViewport()).observe(threeElement)
this.buildConsoleInterface()
this.buildControls()
this.buildScene()
const playerElement = domElement.querySelector('#player')
this.buildPlayer(playerElement)
this.renderer.setAnimationLoop(() => this.render())
}
buildConsoleInterface() {
;[FlatFDGEntityBuilder].forEach($class => {
this.window[$class.name] = $class
})
console.log("%ctrace4d", 'color: #0000ff; font-size: larger; font-weight: bold')
console.log("%cAdjust the map like this:", 'color: #0000ff')
console.log(`
traceMap.entityBuilder.forceWeights.references = 0.5
traceMap.entityBuilder.forceWeights.organization.sameClass = 0.1
traceMap.entityBuilder.forceWeights.globalFactor = 0.5
traceMap.entityBuilder.forceWeights.repulsion = 0.3
traceMap.entityBuilder.excludedObjectNames.push("'an Object'")
traceMap.entityBuilder.excludedClassNames.push('ByteString')
traceMap.entityBuilder.excludeClasses = false
traceMap.reloadTrace()
traceMap.player.stepsPerSecond = 100
`) // , "color: #0000ff" // WORKAROUND: formatted newlines are not copyable in Chrome Dev Tools
}
buildControls() {
if ((this.options.countFPS ?? false) !== false) {
this.buildFPSCounter()
}
this.buildMapControls()
this.buildDragControls()
this.buildMouseHandler()
this.buildKeyboardHandler()
}
buildFPSCounter() {
statsLogs.logging(((this.options.measureFPS ?? false) !== false))
this.stats = new Stats()
this.stats.showPanel(0) // FPS
this.window.document.body.appendChild(this.stats.dom)
this.stats.logs = statsLogs.logs
}
buildMapControls() {
this.mapControls = new MapControls(this.camera, this.renderer.domElement)
this.mapControls.enableDamping = false
this.mapControls.enablePan = true
this.mapControls.minDistance = 20
//this.mapControls.maxDistance = 200
//this.mapControls.maxDistance = 2000
this.mapControls.enableRotate = true
// Enforce the camera to be above the ground plane
this.mapControls.maxPolarAngle = Math.PI / 2 - 0.1
this.mapControls.zoomToCursor = true
this.mapControls.addEventListener('change', () => this.updateScene())
this.mapControls.keyPanSpeed = 20
this.mapControls.listenToKeyEvents(this.window.document)
}
buildDragControls() {
// BUG: Should not be able to drag children of draggable, but DragControls always passes recursive=true to raycaster.
this.dragControls = new DragControls([], this.camera, this.renderer.domElement)
this.dragControls.addEventListener('hoveron', (event) => {
event.object.entity?.onDragStart?.(event)
})
this.dragControls.addEventListener('hoveroff', (event) => {
this.dragEntity = null
event.object.entity?.onDragEnd?.(event)
})
this.dragControls.addEventListener('drag', (event) => {
this.dragEntity = event.object.entity
this.lastDragEvent = event
event.object.entity?.onDrag?.(event)
this.updateScene()
})
this.dragControls.enabled = false
}
registerDraggable(object3d) {
this.dragControls.getObjects().push(object3d)
}
unregisterDraggable(object3d) {
const index = this.dragControls.getObjects().indexOf(object3d)
if (index >= 0) {
this.dragControls.getObjects().splice(index, 1)
}
}
buildMouseHandler() {
const raycaster = new THREE.Raycaster()
const mouse = new THREE.Vector2()
this.mouseOverEntities = []
this.focusEntity = null
this.renderer.domElement.addEventListener('pointermove', event => {
// required for updating DragControls, see references
this.lastPointerMoveEvent = event
mouse.x = (event.clientX / this.window.innerWidth) * 2 - 1
mouse.y = -(event.clientY / this.window.innerHeight) * 2 + 1
raycaster.setFromCamera(mouse, this.camera)
const intersects = raycaster.intersectObjects(this.hoverableObject3ds ?? [], false)
const oldFocusEntity = this.focusEntity
const oldMouseOverEntities = this.mouseOverEntities.slice()
this.mouseOverEntities = intersects.slice().reverse()
.map(intersect => intersect.object.entity)
.filter(entity => entity?.wantsMouse?.(event))
oldMouseOverEntities.forEach(entity => {
if (!this.mouseOverEntities.includes(entity)) {
entity.onMouseLeave?.(event)
}
})
this.mouseOverEntities.forEach(entity => {
if (!oldMouseOverEntities.includes(entity)) {
entity.onMouseEnter?.(event)
}
})
this.focusEntity = this.mouseOverEntities[this.mouseOverEntities.length - 1]
if (this.focusEntity !== oldFocusEntity) {
oldFocusEntity?.onHoverEnd?.(event)
if (!(event.buttons & MouseButtonFlags.LEFT)) {
this.focusEntity?.onHoverStart?.(event)
}
}
intersects.some(intersect => {
intersect.object.entity?.onHover?.(event) !== false
})
if (this.dragControls.enabled && !this.dragEntity) {
// fix dragEntity (might be null when positioning cursor over entity before pressing shift)
this.dragEntity = this.focusEntity
this.dragEntity?.onDragStart?.(event)
}
this.updateCursor()
}, { capture: true })
this.renderer.domElement.parentElement.addEventListener('mousemove', event => {
// required for updating mouseOverEntities, see references
this.lastMouseMoveEvent = event
}, { capture: true })
let mouseBeforeClick = null
let isPlainClick = null
this.renderer.domElement.addEventListener('mousedown', event => {
mouseBeforeClick = mouse.clone()
isPlainClick = true
}, false)
this.renderer.domElement.addEventListener('mousemove', event => {
isPlainClick = isPlainClick && mouseBeforeClick.distanceTo(mouse) < 0.01
}, false)
this.renderer.domElement.addEventListener('click', event => {
if (!isPlainClick) return
isPlainClick = null
mouseBeforeClick = null
if (!this.dragEntity && this.focusEntity?.wantsClick?.(event)) {
this.focusEntity.onClick?.(event)
}
}, false)
this.window.addEventListener('keydown', event => {
if (event.key === 'Shift') {
this.mapControls.enabled = false
this.dragControls.enabled = true
if (this.dragEntity !== null) return
if (!this.focusEntity?.wantsDrag?.(event)) return
this.dragEntity = this.focusEntity
this.dragEntity?.onDragStart?.(event)
this.updateCursor(event)
}
}, false)
this.window.addEventListener('keyup', event => {
if (event.key === 'Shift') {
this.mapControls.enabled = true
this.dragControls.enabled = false
this.dragEntity?.onDragEnd?.(event)
this.dragEntity = null
this.updateCursor(event)
}
}, false)
}
buildKeyboardHandler() {
this.window.addEventListener('keydown', event => {
const action = {
' ': () => this.togglePlaying(),
Escape: () => this.stopForceSimulation(),
Home: () => this.resetPlayer()
}[event.key]
if (!action) return
event.preventDefault()
action()
}, false)
}
buildScene() {
// add lights
const directionalLight1 = new THREE.DirectionalLight(0xffffee, 4)
// TODO: don't hardcode sizes
directionalLight1.position.set(250, 300, 200)
directionalLight1.castShadow = true
directionalLight1.shadow.camera.left = -400
directionalLight1.shadow.camera.right = 400
directionalLight1.shadow.camera.top = 400
directionalLight1.shadow.camera.bottom = -400
//directionalLight1.shadow.bias = -0.0000001
directionalLight1.shadow.mapSize.width = 4096
directionalLight1.shadow.mapSize.height = 4096
this.scene.add(directionalLight1)
const skyColor = 0x87ceeb
const skyLightColor = 0xeaeafa
const groundColor = 0xbadea6
const groundLightColor = 0xbababa
const hemisphereLight = new THREE.HemisphereLight(skyLightColor, groundLightColor, 2.2)
this.scene.add(hemisphereLight);
// add ground
const ground = new THREE.Mesh(
new THREE.CircleGeometry(100000, 64),
new THREE.MeshStandardMaterial({ color: groundColor, roughness: 1, metalness: 0, flatShading: true })
)
ground.rotation.x = -Math.PI / 2
ground.position.y = -10
this.scene.add(ground)
this.renderer.setClearColor(skyColor)
this.renderer.shadowMap.enabled = true
this.renderer.shadowMap.type = THREE.PCFSoftShadowMap
this.updateScene()
}
buildPlayer(domElement) {
this.player = new Player(domElement)
const timelineElement = domElement.querySelector('#timeline')
this.timeline = new Timeline(timelineElement)
this.player.on('step', () => this.timeline.time = this.player.cursor.currentTime)
{
let wasPlaying = false
this.timeline.on('startDrag', () => {
wasPlaying = this.player.isPlaying
this.player.pause()
})
this.timeline.on('endDrag', () => {
if (wasPlaying) {
this.player.resume()
}
})
}
// NB: if this gets too slow, debounce updates
this.timeline.on('time', () => this.player.currentTime = this.timeline.time)
// Hardcoded: On mobile devices, stop force simulation when starting to play (there is not yet a mobile-friendly way to stop it manually)
if (/Mobi|Android/i.test(navigator.userAgent)) {
this.player.once('isPlaying', () => {
this.stopForceSimulation()
})
}
}
buildTrace(traceObject3d) {
if (this.traceEntity) {
this.stopForceSimulation()
this.scene.remove(this.traceEntity.object3d)
this.player.off('isPlaying', this.traceEntity.mapSyncTrailToPlayer)
}
this.traceEntity = traceObject3d.entity
this.scene.add(traceObject3d)
this.player.trail = this.traceEntity.trail
this.player.on('isPlaying', this.traceEntity.mapSyncTrailToPlayer = isPlaying => {
this.traceEntity.trail.deferUpdates = /* !isPlaying */ false
// TODO: reenabling deferUpdates would be great but is only possible when hiding the trail
})
this.initHoverableObjects()
this.updateScene()
}
initHoverableObjects() {
const pending = [...this.scene.children]
this.hoverableObject3ds = []
while (pending.length > 0) {
const object = pending.pop()
if (object.visible === false) continue
if (!object.entity) continue
if (object.entity.wantsMouse && !object.entity.wantsMouse()) continue
this.hoverableObject3ds.push(object)
pending.push(...object.children)
}
}
//#endregion
//#region loading
async loadTraceFromServerFile(serverFile, style) {
return await this.loadTrace(TraceReader.readTraceFromServerFile(serverFile), style)
}
async loadTraceFromString(jsonString, style) {
return await this.loadTrace(TraceReader.readTraceFromString(jsonString), style)
}
loadTrace(trace, style = undefined) {
return this.beBusyDuring(async () => {
this.trace = await trace
style ??= this.defaultStyle
this.entityBuilder = EntityBuilder.newForStyle(style, this.trace)
await this.reloadTrace()
})
}
reloadTrace() {
return this.beBusyDuring(async () => {
if (this.player) {
this.player.reset()
}
const traceObject3d = this.entityBuilder.build(this)
this.buildTrace(traceObject3d)
this.reloadPlayer()
this.traceEntity.trail.deferUpdates = true
if (this.options.autoStart ?? false) {
setTimeout(() => this.player.start(), 3000)
}
})
}
reloadPlayer() {
this.player.setToTrace(this.trace, this.traceEntity)
this.timeline.minTime = this.player.cursor.startTime
this.timeline.maxTime = this.player.cursor.endTime
}
async beBusyDuring(fn) {
if (this.mouseCursor === 'wait') return await fn()
const previousMouseCursor = this.mouseCursor
this.mouseCursor = 'wait'
const previousCursor = this.renderer.domElement.style.cursor
this.renderer.domElement.style.cursor = this.mouseCursor
try {
return await fn()
} finally {
if (this.mouseCursor === 'wait') {
this.mouseCursor = 'progress'
this.renderer.domElement.style.cursor = this.mouseCursor
setTimeout(() => {
if (this.mouseCursor === 'progress') {
this.mouseCursor = previousMouseCursor
this.renderer.domElement.style.cursor = previousCursor
}
}, 0)
} else {
}
}
}
//#endregion
//#region actions
stopForceSimulation() {
this.traceEntity.stopForceSimulation()
}
resetPlayer() {
this.player.reset()
}
togglePlaying() {
this.player.togglePlaying()
}
//#endregion
//#region updating
updateScene() {
this.renderRequired = true
}
render() {
if (this.renderRequired) {
this.renderer.render(this.scene, this.camera)
this.renderRequired = false
this.renderedNow()
}
this.mapControls?.update()
this.stats?.update()
}
renderedNow() {
if ((this.options.measureStartTime ?? false) !== false && window.loadStartTime && this.player?.trail) {
const startTime = window.loadStartTime
delete window.loadStartTime
// to appear after current frame
setTimeout(() => {
const timeToLoad = performance.now() - startTime
alert("time to load: " + timeToLoad)
}, 0)
}
}
updateCursor(event) {
if (this.mouseCursor) return this.mouseCursor
let cursor = 'auto'
if (this.dragControls.enabled) {
this.mouseOverEntities.forEach(entity => {
if (this.dragControls.getObjects().some(object => object === entity.object3d)) {
cursor = 'move'
return
}
})
} else {
this.mouseOverEntities.forEach(entity => {
if (entity.wantsClick(event)) {
cursor = 'pointer'
return
}
})
}
this.renderer.domElement.style.cursor = cursor
}
updateViewport() {
// NOTE: if this becomes to slow, use throttle
this.renderer.setSize(this.renderer.domElement.parentElement.clientWidth, this.renderer.domElement.parentElement.clientHeight)
this.camera.aspect = this.renderer.domElement.clientWidth / this.renderer.domElement.clientHeight
this.camera.updateProjectionMatrix()
this.updateScene()
}
//#endregion
}