Skip to content

Commit e812da7

Browse files
committed
fix: Rect and Cuboid tool did not correctly position the geometry on creation.
1 parent 60e9dc0 commit e812da7

File tree

5 files changed

+32
-18
lines changed

5 files changed

+32
-18
lines changed

src/Tools/CreateTools/Change/CreateCuboidChange.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,8 @@ class CreateCuboidChange extends CreateGeomChange {
4747
this.cuboid.sizeXParam.value = updateData.baseSize[0]
4848
this.cuboid.sizeYParam.value = updateData.baseSize[1]
4949
}
50-
if (updateData.tr) {
51-
const xfo = this.geomItem.localXfoParam.value
52-
xfo.tr.fromJSON(updateData.tr)
53-
this.geomItem.localXfoParam.value = xfo
50+
if (updateData.xfo) {
51+
this.geomItem.globalXfoParam.value = updateData.xfo
5452
}
5553
if (updateData.height) {
5654
this.cuboid.sizeZParam.value = updateData.height

src/Tools/CreateTools/Change/CreateRectChange.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ class CreateRectChange extends CreateGeomChange {
3030
this.rect = new Rect(0, 0)
3131
const material = new LinesMaterial('circle')
3232
material.baseColorParam.value = this.color
33-
this.geomItem = new CustomGeom('Rect', this.rect, material)
33+
this.geomItem = new CustomGeom('Rect', this.rect, material, this.xfo)
3434
this.geomItem.pickableParam.value = false // At the conclusion of creation, we set selectable to true.
35+
if (this.parentItem) {
36+
this.parentItem.addChild(this.geomItem)
37+
}
3538
}
3639

3740
/**
@@ -40,18 +43,28 @@ class CreateRectChange extends CreateGeomChange {
4043
* @param updateData - The updateData param.
4144
*/
4245
update(updateData: Record<any, any>): void {
43-
if (updateData.baseSize) {
44-
this.rect.sizeXParam.value = updateData.baseSize[0]
45-
this.rect.sizeYParam.value = updateData.baseSize[1]
46+
if (updateData.size) {
47+
this.rect.sizeXParam.value = updateData.size[0]
48+
this.rect.sizeYParam.value = updateData.size[1]
4649
}
47-
if (updateData.tr) {
48-
const xfo = this.geomItem.localXfoParam.value
49-
xfo.tr.fromJSON(updateData.tr)
50-
this.geomItem.localXfoParam.value = xfo
50+
if (updateData.xfo) {
51+
this.geomItem.globalXfoParam.value = updateData.xfo
5152
}
5253

5354
this.emit('updated', updateData)
5455
}
56+
57+
/**
58+
* Serializes change as a JSON object.
59+
*
60+
* @return - The return value.
61+
*/
62+
toJSON(): Record<any, any> {
63+
const j: Record<any, any> = super.toJSON()
64+
j.size = [this.rect.sizeXParam.value, this.rect.sizeYParam.value]
65+
j.xfo = this.geomItem.globalXfoParam.value.toJSON()
66+
return j
67+
}
5568
}
5669

5770
UndoRedoManager.registerChange('CreateRectChange', CreateRectChange)

src/Tools/CreateTools/CreateCuboidTool.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ class CreateCuboidTool extends CreateGeomTool {
5555
if (this.stage == 1) {
5656
const delta = this.invXfo.transformVec3(pt)
5757

58-
// const delta = pt.subtract(this.xfo.tr)
58+
const xfo = this.xfo.clone()
59+
xfo.tr.addInPlace(xfo.ori.rotateVec3(delta.scale(0.5)))
60+
5961
this.change.update({
6062
baseSize: [Math.abs(delta.x), Math.abs(delta.y)],
61-
tr: this.xfo.tr.add(delta.scale(0.5)),
63+
xfo,
6264
})
6365
} else {
6466
const vec = this.invXfo.transformVec3(pt)

src/Tools/CreateTools/CreateRectTool.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ class CreateRectTool extends CreateGeomTool {
5454
const delta = this.invXfo.transformVec3(pt)
5555

5656
this.size = Math.min(Math.abs(delta.x), Math.abs(delta.y))
57+
const xfo = this.xfo.clone()
58+
xfo.tr.addInPlace(xfo.ori.rotateVec3(delta.scale(0.5)))
5759

58-
// const delta = pt.subtract(this.xfo.tr)
5960
this.change.update({
60-
baseSize: [Math.abs(delta.x), Math.abs(delta.y)],
61-
tr: this.xfo.tr.add(this.xfo.ori.rotateVec3(delta.scale(0.5))),
61+
size: [Math.abs(delta.x), Math.abs(delta.y)],
62+
xfo,
6263
})
6364
}
6465
}

testing-e2e/create-tool.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
const $canvas = document.getElementById('renderer')
8282

8383
const scene = new Scene()
84-
scene.setupGrid(100, 100)
84+
scene.setupGrid(10, 10)
8585

8686
const renderer = new GLRenderer($canvas, {
8787
enableFrustumCulling: false,

0 commit comments

Comments
 (0)