Skip to content

Commit

Permalink
feat(troika-3d-text): add events fired on text sync start and completion
Browse files Browse the repository at this point in the history
  • Loading branch information
lojjic committed May 12, 2020
1 parent 669b256 commit 3e7d4e0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
22 changes: 19 additions & 3 deletions packages/troika-3d-text/src/facade/Text3DFacade.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,38 @@ class Text3DFacade extends Object3DFacade {

this.selectable = false
this.selectionStart = this.selectionEnd = -1
this.onSyncStart = null
this.onSyncComplete = null

this._afterSync = () => {
mesh.addEventListener('syncstart', e => {
this.notifyWorld('text3DSyncStart')
if (this.onSyncStart) {
this.onSyncStart()
}
})
mesh.addEventListener('synccomplete', e => {
if (!this.isDestroying) {
mesh.geometry.boundingSphere.version++
this.afterUpdate()
this.notifyWorld('text3DSyncComplete')
this.notifyWorld('needsRender')
if (this.onSyncComplete) {
this.onSyncComplete()
}
}
}
})
}

get textRenderInfo() {
return this.threeObject.textRenderInfo
}

afterUpdate() {
const textMesh = this.threeObject
TEXT_MESH_PROPS.forEach(prop => {
textMesh[prop] = this[prop]
})
textMesh.sync(this._afterSync)
textMesh.sync()

super.afterUpdate()

Expand Down
5 changes: 5 additions & 0 deletions packages/troika-3d-text/src/three/TextMesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const raycastMesh = new Mesh(
defaultMaterial
)

const syncStartEvent = {type: 'syncstart'}
const syncCompleteEvent = {type: 'synccomplete'}



/**
Expand Down Expand Up @@ -204,6 +207,7 @@ class TextMesh extends Mesh {
(this._queuedSyncs || (this._queuedSyncs = [])).push(callback)
} else {
this._isSyncing = true
this.dispatchEvent(syncStartEvent)

getTextRenderInfo({
text: this.text,
Expand Down Expand Up @@ -242,6 +246,7 @@ class TextMesh extends Mesh {
})
}

this.dispatchEvent(syncCompleteEvent)
if (callback) {
callback()
}
Expand Down

0 comments on commit 3e7d4e0

Please sign in to comment.