Skip to content

Commit

Permalink
fix: gizmos settings (#596)
Browse files Browse the repository at this point in the history
* fix: gizmo toggle

* fix: rotation gizmo warning

* fix: increase leeway
  • Loading branch information
cazala authored May 23, 2023
1 parent e7ba7f7 commit c718b1c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
10 changes: 7 additions & 3 deletions packages/@dcl/inspector/src/components/Toolbar/Gizmos/Gizmos.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useState } from 'react'
import React, { useCallback, useEffect, useState } from 'react'
import { BsCaretDown } from 'react-icons/bs'
import { BiCheckbox, BiCheckboxChecked } from 'react-icons/bi'
import cx from 'classnames'
Expand Down Expand Up @@ -47,8 +47,12 @@ export const Gizmos = withSdk(({ sdk }) => {

const ref = useOutsideClick(handleClosePanel)

useEffect(() => {
setShowPanel(false)
}, [selection])

return (
<div className="Gizmos">
<div className="Gizmos" ref={ref}>
<ToolbarButton
className={cx('gizmo position', { active: selection?.gizmo === GizmoType.POSITION })}
disabled={disableGizmos}
Expand All @@ -65,7 +69,7 @@ export const Gizmos = withSdk(({ sdk }) => {
onClick={handleScaleGizmo}
/>
<BsCaretDown className="open-panel" onClick={handleTogglePanel} />
<div ref={ref} className={cx('panel', { visible: showPanel })}>
<div className={cx('panel', { visible: showPanel })}>
<div className="title">
<label>Snap</label>
<SnapToggleIcon className="icon" onClick={toggle} />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useCallback, useEffect, useRef, useState } from 'react'
import { CrdtMessageType } from '@dcl/ecs'
import { useSdk } from '../sdk/useSdk'
import { useChange } from '../sdk/useChange'
import { Gizmos } from '../../lib/babylon/decentraland/gizmo-manager'
import { CrdtMessageType } from '@dcl/ecs'

export const useGizmoAlignment = () => {
const gizmosRef = useRef<Gizmos | null>(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('GizmoManager', () => {
entity.scaling = new Vector3(1, 2, 1)
})
afterEach(() => {
entity.scaling = Vector3.Zero()
entity.scaling = new Vector3(1, 1, 1)
})
describe('and the rotation gizmo is not world aligned', () => {
beforeEach(() => {
Expand Down Expand Up @@ -120,6 +120,22 @@ describe('GizmoManager', () => {
})
})
})
describe('and the entity is almost proportionally scaled except for a tiny rounding error', () => {
beforeEach(() => {
entity.scaling = new Vector3(1.0000001192092896, 0.9999998807907104, 1)
})
afterEach(() => {
entity.scaling = new Vector3(1, 1, 1)
})
it('should not force the rotation gizmo to be world aligned', () => {
gizmos.setRotationGizmoWorldAligned(false)
expect(gizmos.isRotationGizmoWorldAligned()).toBe(false)
expect(gizmos.isRotationGizmoAlignmentDisabled()).toBe(false)
gizmos.gizmoManager.gizmos.scaleGizmo?.onDragEndObservable.notifyObservers({} as any)
expect(gizmos.isRotationGizmoWorldAligned()).toBe(false)
expect(gizmos.isRotationGizmoAlignmentDisabled()).toBe(false)
})
})
})
})
describe('When getting the gizmo types', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { snapManager, snapPosition, snapRotation, snapScale } from './snap-manag
import { SceneContext } from './SceneContext'
import { GizmoType } from '../../utils/gizmo'

function areProportional(a: number, b: number) {
// this leeway is here to account for rounding errors due to serializing/deserializing floating point numbers
return Math.abs(a - b) < 1e-5
}

export function createGizmoManager(context: SceneContext) {
// events
const events = mitt<{ change: void }>()
Expand Down Expand Up @@ -47,7 +52,7 @@ export function createGizmoManager(context: SceneContext) {

function fixRotationGizmoAlignment(value: TransformType) {
const isProportional =
Math.abs(value.scale.x) === Math.abs(value.scale.y) && Math.abs(value.scale.y) === value.scale.z
areProportional(value.scale.x, value.scale.y) && areProportional(value.scale.y, value.scale.z)
rotationGizmoAlignmentDisabled = !isProportional
if (!isProportional && !isRotationGizmoWorldAligned()) {
setRotationGizmoWorldAligned(true) // set to world
Expand Down

0 comments on commit c718b1c

Please sign in to comment.