-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #676 from 3DStreet/undo
Undo/redo position/rotation/scale
- Loading branch information
Showing
15 changed files
with
373 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/editor/components/components/UndoRedo/UndoRedo.component.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { faRotateLeft, faRotateRight } from '@fortawesome/free-solid-svg-icons'; | ||
import posthog from 'posthog-js'; | ||
import { AwesomeIcon } from '../AwesomeIcon'; | ||
import { Button } from '../Button'; | ||
import Events from '../../../lib/Events'; | ||
|
||
export const UndoRedo = () => { | ||
const [undoDisabled, setUndoDisabled] = useState( | ||
AFRAME.INSPECTOR.history.undos.length === 0 | ||
); | ||
const [redoDisabled, setRedoDisabled] = useState( | ||
AFRAME.INSPECTOR.history.redos.length === 0 | ||
); | ||
const handleUndoClick = () => { | ||
AFRAME.INSPECTOR.undo(); | ||
posthog.capture('undo_clicked'); | ||
}; | ||
|
||
const handleRedoClick = () => { | ||
AFRAME.INSPECTOR.redo(); | ||
posthog.capture('redo_clicked'); | ||
}; | ||
|
||
useEffect(() => { | ||
const listener = () => { | ||
setUndoDisabled(AFRAME.INSPECTOR.history.undos.length === 0); | ||
setRedoDisabled(AFRAME.INSPECTOR.history.redos.length === 0); | ||
}; | ||
Events.on('historychanged', listener); | ||
return () => { | ||
Events.off('historychanged', listener); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<Button | ||
variant="toolbtn" | ||
onClick={handleUndoClick} | ||
leadingIcon={<AwesomeIcon icon={faRotateLeft} />} | ||
disabled={undoDisabled} | ||
> | ||
<div className="hideInLowResolution">Undo</div> | ||
</Button> | ||
<Button | ||
variant="toolbtn" | ||
onClick={handleRedoClick} | ||
leadingIcon={<AwesomeIcon icon={faRotateRight} />} | ||
disabled={redoDisabled} | ||
> | ||
<div className="hideInLowResolution">Redo</div> | ||
</Button> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { UndoRedo } from './UndoRedo.component.jsx'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* @param editor pointer to main editor object used to initialize | ||
* each command object with a reference to the editor | ||
* @constructor | ||
*/ | ||
|
||
export class Command { | ||
constructor(editor) { | ||
this.id = -1; | ||
this.updatable = false; | ||
this.type = ''; | ||
this.name = ''; | ||
this.editor = editor; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import Events from '../Events'; | ||
import { Command } from '../command.js'; | ||
|
||
function updateEntity(entity, component, property, value) { | ||
if (property) { | ||
if (value === null || value === undefined) { | ||
// Remove property. | ||
entity.removeAttribute(component, property); | ||
} else { | ||
// Set property. | ||
entity.setAttribute(component, property, value); | ||
} | ||
} else { | ||
if (value === null || value === undefined) { | ||
// Remove component. | ||
entity.removeAttribute(component); | ||
} else { | ||
// Set component. | ||
entity.setAttribute(component, value); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param editor Editor | ||
* @param payload: entity, component, property, value. | ||
* @constructor | ||
*/ | ||
export class EntityUpdateCommand extends Command { | ||
constructor(editor, payload) { | ||
super(editor); | ||
|
||
this.type = 'EntityUpdateCommand'; | ||
this.name = 'Update Entity'; | ||
this.updatable = | ||
payload.component === 'position' || | ||
payload.component === 'rotation' || | ||
payload.component === 'scale'; | ||
|
||
this.entity = payload.entity; | ||
this.component = payload.component; | ||
this.property = payload.property; | ||
|
||
const component = AFRAME.components[payload.component]; | ||
if (component) { | ||
if (payload.property) { | ||
if (component.schema[payload.property]) { | ||
this.newValue = component.schema[payload.property].stringify( | ||
payload.value | ||
); | ||
this.oldValue = component.schema[payload.property].stringify( | ||
payload.entity.getAttribute(payload.component, payload.property) | ||
); | ||
if (this.editor.debugUndoRedo) { | ||
console.log(this.component, this.oldValue, this.newValue); | ||
} | ||
} | ||
} else { | ||
this.newValue = component.schema.stringify(payload.value); | ||
this.oldValue = component.schema.stringify( | ||
payload.entity.getAttribute(payload.component) | ||
); | ||
if (this.editor.debugUndoRedo) { | ||
console.log(this.component, this.oldValue, this.newValue); | ||
} | ||
} | ||
} | ||
} | ||
|
||
execute() { | ||
if (this.editor.debugUndoRedo) { | ||
console.log( | ||
'execute', | ||
this.entity, | ||
this.component, | ||
this.property, | ||
this.newValue | ||
); | ||
} | ||
updateEntity(this.entity, this.component, this.property, this.newValue); | ||
Events.emit('entityupdate', { | ||
entity: this.entity, | ||
component: this.component, | ||
property: this.property, | ||
value: this.newValue | ||
}); | ||
} | ||
|
||
undo() { | ||
updateEntity(this.entity, this.component, this.property, this.oldValue); | ||
Events.emit('entityupdate', { | ||
entity: this.entity, | ||
component: this.component, | ||
property: this.property, | ||
value: this.oldValue | ||
}); | ||
} | ||
|
||
update(command) { | ||
if (this.editor.debugUndoRedo) { | ||
console.log('update', command); | ||
} | ||
this.newValue = command.newValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { EntityUpdateCommand } from './EntityUpdateCommand.js'; |
Oops, something went wrong.