Skip to content

Commit

Permalink
Merge pull request #32 from decentraland/counter-updates
Browse files Browse the repository at this point in the history
Justification added to counter
  • Loading branch information
dzsunyec committed Sep 13, 2024
2 parents c042ca1 + fb674d6 commit b3900fd
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions src/ui/counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Vector3 } from '@dcl/sdk/math'
import { syncEntity } from '@dcl/sdk/network'
import { getSDK } from '../sdk'

type justification = 'left' | 'right' | 'center'

function separateDigits(value: number): number[] {
const arr: number[] = []
let lastDigit: number
Expand Down Expand Up @@ -30,13 +32,15 @@ export class Counter3D {
spacing: number = 1
currentNumber: number = 0
zeroPadding: boolean
justify: justification = 'center'

constructor(
transform: TransformTypeWithOptionals,
maxDigits: number,
digitSpacing: number,
zeroPadding: boolean,
id: number
id: number,
justify?: justification
) {
const {
engine,
Expand All @@ -49,9 +53,11 @@ export class Counter3D {
this.maxDigits = maxDigits
this.zeroPadding = zeroPadding
this.id = id
this.justify = justify ?? 'center'

this.addDigits()
}

addDigits() {
const {
engine,
Expand All @@ -66,7 +72,44 @@ export class Counter3D {
VisibilityComponent.createOrReplace(numberEntity)
this.digits.push(numberEntity)

syncEntity(numberEntity, [GltfContainer.componentId], this.id * 20000 + i)
syncEntity(numberEntity, [Transform.componentId, GltfContainer.componentId], this.id * 20000 + i)
}
}

justifyNumber() {
const {
components: { Transform }
} = getSDK()
const numberArr = separateDigits(this.currentNumber)
let offset = 0

switch (this.justify) {
case 'left': {
offset = 0
break
}
case 'right': {
if (!this.zeroPadding) {
offset = -(numberArr.length - 1) * this.spacing
} else {
offset = -(this.digits.length - 1) * this.spacing
}

break
}
case 'center': {
if (!this.zeroPadding) {
offset = -((numberArr.length - 1) * this.spacing) / 2
} else {
offset = -((this.digits.length - 1) * this.spacing) / 2
}

break
}
}

for (let i = 0; i < this.digits.length; i++) {
Transform.getMutable(this.digits[i]).position = Vector3.create(i * this.spacing + offset, 0, 0)
}
}

Expand Down Expand Up @@ -94,6 +137,8 @@ export class Counter3D {
}
}
}

this.justifyNumber()
}

reduceNumberBy(_decrement: number) {
Expand Down

0 comments on commit b3900fd

Please sign in to comment.