Skip to content

Commit

Permalink
feat: add basic rendering for text displays
Browse files Browse the repository at this point in the history
  • Loading branch information
Phoenix616 committed Nov 19, 2024
1 parent 0506d9d commit 2592d50
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
15 changes: 12 additions & 3 deletions prismarine-viewer/viewer/lib/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,25 @@ function getUsernameTexture (username: string, { fontFamily = 'sans-serif' }: an
const padding = 5
ctx.font = `${fontSize}px ${fontFamily}`

const textWidth = ctx.measureText(username).width + padding * 2
const lines = String(username).split("\n")

Check failure on line 32 in prismarine-viewer/viewer/lib/entities.ts

View workflow job for this annotation

GitHub Actions / build-and-deploy

Strings must use singlequote

let textWidth = 0
for (let line of lines) {

Check failure on line 35 in prismarine-viewer/viewer/lib/entities.ts

View workflow job for this annotation

GitHub Actions / build-and-deploy

'line' is never reassigned. Use 'const' instead
const width = ctx.measureText(line).width + padding * 2
if (width > textWidth) textWidth = width
}

canvas.width = textWidth
canvas.height = fontSize + padding * 2
canvas.height = (fontSize + padding * 2) * lines.length

ctx.fillStyle = 'rgba(0, 0, 0, 0.3)'
ctx.fillRect(0, 0, canvas.width, canvas.height)

ctx.font = `${fontSize}px ${fontFamily}`
ctx.fillStyle = 'white'
ctx.fillText(username, padding, fontSize)
for (let i = 0; i < lines.length; i++) {

Check failure on line 48 in prismarine-viewer/viewer/lib/entities.ts

View workflow job for this annotation

GitHub Actions / build-and-deploy

Use a `for-of` loop instead of this `for` loop
ctx.fillText(lines[i], padding + (textWidth - ctx.measureText(lines[i]).width) / 2, fontSize + fontSize * i)
}

return canvas
}
Expand Down Expand Up @@ -454,6 +462,7 @@ export class Entities extends EventEmitter {
// ---
// not player
const displayText = entity.metadata?.[3] && this.parseEntityLabel(entity.metadata[2])
|| entity.metadata?.[23] && this.parseEntityLabel(entity.metadata[23]) // text displays

Check failure on line 465 in prismarine-viewer/viewer/lib/entities.ts

View workflow job for this annotation

GitHub Actions / build-and-deploy

Expected indentation of 6 spaces
if (entity.name !== 'player' && displayText) {
addNametag({ ...entity, username: displayText }, this.entitiesOptions, this.entities[entity.id].children.find(c => c.name === 'mesh'))
}
Expand Down
4 changes: 2 additions & 2 deletions prismarine-viewer/viewer/lib/entity/EntityMesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ export const knownNotHandled = [
'item_display', 'item_frame',
'lightning_bolt', 'marker',
'painting', 'spawner_minecart',
'spectral_arrow', 'text_display',
'tnt', 'trader_llama', 'zombie_horse'
'spectral_arrow', 'tnt',
'trader_llama', 'zombie_horse'
]

export const temporaryMap = {
Expand Down
4 changes: 4 additions & 0 deletions prismarine-viewer/viewer/lib/entity/entities.json
Original file line number Diff line number Diff line change
Expand Up @@ -16390,6 +16390,10 @@
},
"render_controllers": ["controller.render.strider"]
},
"text_display": {
"identifier": "minecraft:text_display",
"geometry": {}
},
"trident": {
"identifier": "minecraft:thrown_trident",
"textures": {
Expand Down

0 comments on commit 2592d50

Please sign in to comment.