Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Secondary changes refactor #196

Merged
merged 9 commits into from
Aug 16, 2023
10 changes: 4 additions & 6 deletions src/Handles/AxialRotationHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,8 @@ class AxialRotationHandle extends Handle {
const deltaXfo = new Xfo()
deltaXfo.ori.setFromAxisAndAngle(this.baseXfo.ori.getZaxis(), angle)

if (this.selectionGroup) {
const selectionXfoChange = <SelectionXfoChange>this.change
selectionXfoChange.setDeltaXfo(deltaXfo)
if (this.change instanceof SelectionXfoChange) {
this.change.setDeltaXfo(deltaXfo)
} else {
// Add the values in global space.
const newBase = this.baseXfo.clone()
Expand All @@ -192,9 +191,8 @@ class AxialRotationHandle extends Handle {
* @param event - The event param.
*/
onDragEnd(event: ZeaPointerEvent): void {
if (this.selectionGroup) {
const selectionXfoChange = <SelectionXfoChange>this.change
selectionXfoChange.setDone()
if (this.change instanceof SelectionXfoChange) {
this.change.setDone()
}
this.change = null
}
Expand Down
10 changes: 4 additions & 6 deletions src/Handles/LinearMovementHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,9 @@ class LinearMovementHandle extends BaseLinearMovementHandle {
onDrag(event: ZeaPointerEvent): void {
const dragVec = this.holdPos.subtract(this.grabPos)

if (this.selectionGroup) {
const selectionXfoChange = <SelectionXfoChange>this.change
if (this.change instanceof SelectionXfoChange) {
const deltaXfo = new Xfo(dragVec)
selectionXfoChange.setDeltaXfo(deltaXfo)
this.change.setDeltaXfo(deltaXfo)
} else {
const newXfo = this.baseXfo.clone()
newXfo.tr.addInPlace(dragVec)
Expand All @@ -157,9 +156,8 @@ class LinearMovementHandle extends BaseLinearMovementHandle {
* @param event - The event param.
*/
onDragEnd(event: ZeaPointerEvent): void {
if (this.selectionGroup) {
const selectionXfoChange = <SelectionXfoChange>this.change
selectionXfoChange.setDone()
if (this.change instanceof SelectionXfoChange) {
this.change.setDone()
}
this.change = null
}
Expand Down
10 changes: 4 additions & 6 deletions src/Handles/LinearScaleHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,10 @@ class LinearScaleHandle extends BaseLinearMovementHandle {
this.tmplocalXfo.sc.set(1, 1, sc)
this.localXfoParam.value = this.tmplocalXfo

if (this.selectionGroup) {
const selectionXfoChange = <SelectionXfoChange>this.change
if (this.change instanceof SelectionXfoChange) {
const deltaXfo = new Xfo()
deltaXfo.sc.set(sc, sc, sc)
selectionXfoChange.setDeltaXfo(deltaXfo)
this.change.setDeltaXfo(deltaXfo)
} else {
this.change.update({
value: newXfo,
Expand All @@ -180,9 +179,8 @@ class LinearScaleHandle extends BaseLinearMovementHandle {
* @param event - The event param.
*/
onDragEnd(event: ZeaPointerEvent): void {
if (this.selectionGroup) {
const selectionXfoChange = <SelectionXfoChange>this.change
selectionXfoChange.setDone()
if (this.change instanceof SelectionXfoChange) {
this.change.setDone()
}
this.change = null

Expand Down
10 changes: 4 additions & 6 deletions src/Handles/PlanarMovementHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,9 @@ class PlanarMovementHandle extends Handle {
onDrag(event: ZeaPointerEvent): void {
const dragVec = this.holdPos.subtract(this.grabPos)

if (this.selectionGroup) {
const selectionXfoChange = <SelectionXfoChange>this.change
if (this.change instanceof SelectionXfoChange) {
const deltaXfo = new Xfo(dragVec)
selectionXfoChange.setDeltaXfo(deltaXfo)
this.change.setDeltaXfo(deltaXfo)
} else {
const newXfo = this.baseXfo.clone()
newXfo.tr.addInPlace(dragVec)
Expand All @@ -104,9 +103,8 @@ class PlanarMovementHandle extends Handle {
* @param event - The event param.
*/
onDragEnd(event: ZeaPointerEvent): void {
if (this.selectionGroup) {
const selectionXfoChange = <SelectionXfoChange>this.change
selectionXfoChange.setDone()
if (this.change instanceof SelectionXfoChange) {
this.change.setDone()
}
this.change = null
}
Expand Down
2 changes: 2 additions & 0 deletions src/Measurement/MeasurementChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class MeasurementChange extends Change {
* @param context - The appData param.
*/
fromJSON(j: Record<string, any>, context: Record<string, any>): void {
super.fromJSON(j, context)

const sceneRoot = context.appData.scene.getRoot()
const parentItem = sceneRoot.resolvePath(j.parentItemPath, 1)
if (parentItem) {
Expand Down
10 changes: 0 additions & 10 deletions src/Tools/CreateTools/Change/CreateCircleChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,6 @@ class CreateCircleChange extends CreateGeomChange {
j.radius = this.circle.radiusParam.value
return j
}

/**
* Updates circle with the specified JSON
*
* @param j - The j param.
*/
updateFromJSON(j: Record<any, any>): void {
console.log('CreateCircleChange:', j)
if (j.radius) this.circle.radiusParam.value = j.radius
}
}

UndoRedoManager.registerChange('CreateCircleChange', CreateCircleChange)
Expand Down
18 changes: 7 additions & 11 deletions src/Tools/CreateTools/Change/CreateGeomChange.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Xfo, Color, GeomItem, TreeItem } from '@zeainc/zea-engine'
import Change from '../../../UndoRedo/Change'
import { UndoRedoManager } from '../../..'

/**
* Class representing a create geom change.
Expand All @@ -15,8 +16,7 @@ class CreateGeomChange extends Change {
* @param name - The name value.
*/
constructor(name: string, parentItem: TreeItem, xfo: Xfo) {
super(name)
this.parentItem = parentItem
super(name ? name : 'CreateGeomChange')

this.createGeoItem()

Expand Down Expand Up @@ -46,7 +46,6 @@ class CreateGeomChange extends Change {
* Removes recently created geometry from its parent.
*/
undo(): void {
console.log('this.geomItem', this.geomItem)
this.parentItem.removeChild(this.parentItem.getChildIndex(this.geomItem))
}

Expand All @@ -70,7 +69,7 @@ class CreateGeomChange extends Change {
j.geomItemXfo = this.geomItem.localXfoParam.getValue()

const material = this.geomItem.getParameter('Material').getValue()
j.color = material.getParameter('BaseColor').getValue()
j.color = material.getParameter('BaseColor')?.getValue()
return j
}

Expand All @@ -81,6 +80,8 @@ class CreateGeomChange extends Change {
* @param context - The appData param.
*/
fromJSON(j: Record<any, any>, context: Record<any, any>): void {
super.fromJSON(j, context)

const sceneRoot = context.appData.scene.getRoot()
this.parentItem = sceneRoot.resolvePath(j.parentItemPath, 1)
this.geomItem.setName(this.parentItem.generateUniqueName(j.geomItemName))
Expand All @@ -97,13 +98,6 @@ class CreateGeomChange extends Change {
}
}

// updateFromJSON(j) {
// if (this.__newValue.fromJSON)
// this.__newValue.fromJSON(j.value);
// else
// this.__newValue = j.value;
// }

/**
* Removes geometry item reference from change change.
*/
Expand All @@ -112,5 +106,7 @@ class CreateGeomChange extends Change {
}
}

UndoRedoManager.registerChange('CreateGeomChange', CreateGeomChange)

export default CreateGeomChange
export { CreateGeomChange }
9 changes: 0 additions & 9 deletions src/Tools/CreateTools/Change/CreateSphereChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@ class CreateSphereChange extends CreateGeomChange {
j.radius = this.sphere.radiusParam.getValue()
return j
}

/**
* Updates sphere geometry using a JSON object.
*
* @param j - The j param.
*/
updateFromJSON(j: Record<any, any>): void {
if (j.radius) this.sphere.radiusParam.value = j.radius
}
}

UndoRedoManager.registerChange('CreateSphereChange', CreateSphereChange)
Expand Down
9 changes: 0 additions & 9 deletions src/Tools/VRTools/VRHoldObjectsTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,6 @@ class HoldObjectsChange extends Change {
}
}
}

/**
* Updates the state of an existing identified `Parameter` through replication.
*
* @param j - The j param.
*/
updateFromJSON(j: Record<string, any>): void {
this.update(j)
}
}

UndoRedoManager.registerChange('HoldObjectsChange', HoldObjectsChange)
Expand Down
30 changes: 17 additions & 13 deletions src/UndoRedo/Change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ class Change extends EventEmitter {
addSecondaryChange(secondaryChange: Change) {
const index = this.secondaryChanges.length
this.secondaryChanges.push(secondaryChange)
secondaryChange.setPrimaryChange(this)
return index
}

setPrimaryChange(primaryChange: Change) {}

/**
* Called by the `UndoRedoManager` in the `undo` method, and contains the code you wanna run when the undo action is triggered,
* of course it depends on what you're doing.
Expand Down Expand Up @@ -63,7 +66,11 @@ class Change extends EventEmitter {
* @param context - The appData param.
*/
toJSON(context: Record<any, any>): Record<any, any> {
return {}
return {
name: this.name,
className: this.getClassName(),
secondaryChanges: this.secondaryChanges.map((change) => change.toJSON(context)),
}
}

/**
Expand All @@ -76,19 +83,14 @@ class Change extends EventEmitter {
* @param j - The j param.
* @param context - The context param.
*/
fromJSON(j: Record<any, any>, context: Record<any, any>): void {}
fromJSON(j: Record<any, any>, context: Record<any, any>): void {
this.name = j.name

/**
* Useful method to update the state of an existing identified `Change` through replication.
*
* @note By default it calls the `update` method in the `Change` class, but you can override this if you need to.
*
* @param j - The j param.
*/
updateFromJSON(j: Record<any, any>): void {
// Many change objects can load json directly
// in the update method.
this.update(j)
j.secondaryChanges.forEach((childJson: Record<any, any>) => {
const change = UndoRedoManager.getInstance().constructChange(childJson.className)
change.fromJSON(childJson, context)
this.addSecondaryChange(change)
})
}

/**
Expand All @@ -100,5 +102,7 @@ class Change extends EventEmitter {
destroy(): void {}
}

UndoRedoManager.registerChange('Change', Change)

export default Change
export { Change }
41 changes: 20 additions & 21 deletions src/UndoRedo/Changes/ParameterValueChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ class ParameterValueChange extends Change {
param: Parameter<unknown>
nextValue: any
prevValue: any
supressed: boolean = false
/**
* Creates an instance of ParameterValueChange.
*
* @param param - The Parameter object that is modified by this change.
* @param newValue - The newValue value.
*/
constructor(param?: Parameter<unknown>, newValue?: any) {
super(param ? param.getName() + ' Changed' : 'ParameterValueChange')

if (param) {
super(param ? param.getName() + ' Changed' : 'ParameterValueChange')
this.prevValue = param.getValue()
this.param = param
if (newValue != undefined) {
this.nextValue = newValue
this.param.value = this.nextValue
}
} else {
super()
}
}

Expand Down Expand Up @@ -61,7 +61,14 @@ class ParameterValueChange extends Change {
update(updateData: Record<string, any>): void {
if (!this.param) return
this.nextValue = updateData.value
this.param.value = this.nextValue

// The supressed value is set to true by Zea Platform when the change should
// not be applied on a local computer due to the local user viewing a different
// pose to the original user who created the change.
if (!this.supressed) {
this.param.value = this.nextValue
}

this.emit('updated', updateData)
}

Expand All @@ -72,10 +79,8 @@ class ParameterValueChange extends Change {
* @return {object} The return value.
*/
toJSON(context: Record<any, any>): Record<any, any> {
const j: Record<any, any> = {
name: this.name,
paramPath: this.param.getPath(),
}
const j = super.toJSON(context)
j.paramPath = this.param.getPath()

if (this.nextValue != undefined) {
if (this.nextValue.toJSON) {
Expand All @@ -94,6 +99,8 @@ class ParameterValueChange extends Change {
* @param context - The context param.
*/
fromJSON(j: Record<any, any>, context: Record<any, any>): Record<any, any> {
super.fromJSON(j, context)

const param = context.appData.scene.getRoot().resolvePath(j.paramPath, 1)
if (!param || !(param instanceof Parameter)) {
console.warn('resolvePath is unable to resolve', j.paramPath)
Expand All @@ -104,19 +111,11 @@ class ParameterValueChange extends Change {
if (this.prevValue.clone) this.nextValue = this.prevValue.clone()
else this.nextValue = this.prevValue

this.name = j.name
if (j.value != undefined) this.updateFromJSON(j)
}

/**
* Updates the state of an existing identified `Parameter` through replication.
*
* @param j - The j param.
*/
updateFromJSON(j: Record<any, any>): void {
if (!this.param) return
if (this.nextValue.fromJSON) this.nextValue.fromJSON(j.value)
else this.nextValue = j.value
if (j.value != undefined) {
if (this.nextValue.fromJSON) this.nextValue.fromJSON(j.value)
else this.nextValue = j.value
}
if (this.supressed) return
this.param.value = this.nextValue
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/UndoRedo/Changes/SelectionChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class SelectionChange extends Change {
*/
constructor(selectionManager: SelectionManager, prevSelection: Set<TreeItem>, newSelection: Set<TreeItem>) {
super('SelectionChange')

if (!selectionManager || !prevSelection || !newSelection) return

this.__selectionManager = selectionManager
this.__prevSelection = prevSelection
this.__newSelection = newSelection
Expand Down
3 changes: 3 additions & 0 deletions src/UndoRedo/Changes/SelectionVisibilityChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class SelectionVisibilityChange extends Change {
*/
constructor(selection: Set<TreeItem>, state: boolean) {
super('Selection Visibility Change')

if (!selection || typeof state !== 'boolean') return

this.selection = selection
this.state = state
this._changeItemsVisibility(this.state)
Expand Down
Loading