Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Consolidate applying redo and undo button logic with shortcut logic

Scott Petrovic9fe4392f295e
diff --git a/src/lib/EventListeners.ts b/src/lib/EventListeners.ts
index 187f624..c2bf096 100644
--- a/src/lib/EventListeners.ts
+++ b/src/lib/EventListeners.ts
@@ -152,43 +152,20 @@ export class EventListeners {
return
}
- // Prevent default if we're handling the event
- const is_ctrl_or_cmd = event.ctrlKey || event.metaKey
+ // Define undo/redo shortcut conditions
+ // Ctrl+Z or Cmd+Z for undo
+ // Ctrl+Y, Cmd+Y, Ctrl+Shift+Z, or Cmd+Shift+Z for redo
+ const is_undo_shortcut_pressed = (event.ctrlKey || event.metaKey) && event.key === 'z' && !event.shiftKey
+ const is_redo_shortcut_pressed = (event.ctrlKey || event.metaKey) && (event.key === 'y' || (event.key === 'z' && event.shiftKey))
- if (is_ctrl_or_cmd && event.key === 'z' && !event.shiftKey) {
- // Ctrl+Z or Cmd+Z for undo
+ if (is_undo_shortcut_pressed) {
event.preventDefault()
- const success = this.bootstrap.edit_skeleton_step.undo_bone_transformation()
- if (success) {
- console.log('Undo successful')
-
- // Refresh weight painting if in weight painted mode
- if (this.bootstrap.mesh_preview_display_type === ModelPreviewDisplay.WeightPainted) {
- this.bootstrap.regenerate_weight_painted_preview_mesh()
- }
-
- // Update skeleton helper if it exists
- if (this.bootstrap.skeleton_helper !== undefined) {
- this.bootstrap.regenerate_skeleton_helper(this.bootstrap.edit_skeleton_step.skeleton(), 'Skeleton Helper')
- }
- }
- } else if (is_ctrl_or_cmd && (event.key === 'y' || (event.key === 'z' && event.shiftKey))) {
- // Ctrl+Y, Cmd+Y, Ctrl+Shift+Z, or Cmd+Shift+Z for redo
+ this.bootstrap.edit_skeleton_step.undo_bone_transformation()
+ }
+
+ if (is_redo_shortcut_pressed) {
event.preventDefault()
- const success = this.bootstrap.edit_skeleton_step.redo_bone_transformation()
- if (success) {
- console.log('Redo successful')
- // Update skeleton helper if it exists
- if (this.bootstrap.skeleton_helper !== undefined) {
- this.bootstrap.regenerate_skeleton_helper(this.bootstrap.edit_skeleton_step.skeleton(), 'Skeleton Helper')
- }
- // Refresh weight painting if in weight painted mode
- if (this.bootstrap.mesh_preview_display_type === ModelPreviewDisplay.WeightPainted) {
- this.bootstrap.regenerate_weight_painted_preview_mesh()
- }
- } else {
- console.log('No redo states available')
- }
+ this.bootstrap.edit_skeleton_step.redo_bone_transformation()
}
})
}
diff --git a/src/lib/processes/StepEditSkeleton.ts b/src/lib/processes/StepEditSkeleton.ts
index 23c16f8..c8be5a3 100644
--- a/src/lib/processes/StepEditSkeleton.ts
+++ b/src/lib/processes/StepEditSkeleton.ts
@@ -53,14 +53,28 @@ export class StepEditSkeleton extends EventTarget {
* Undo the last bone transformation
*/
public undo_bone_transformation (): boolean {
- return this.undo_redo_system.undo()
+ const result = this.undo_redo_system.undo()
+ if (result) {
+ // Update skeleton helper and any UI elements that depend on bone positions
+ this.dispatchEvent(new CustomEvent('skeletonTransformed'))
+ console.log('Undo successful')
+ }
+ return result
}
/**
* Redo the last undone bone transformation
*/
public redo_bone_transformation (): boolean {
- return this.undo_redo_system.redo()
+ const result = this.undo_redo_system.redo()
+ if (result) {
+ // Update skeleton helper and any UI elements that depend on bone positions
+ this.dispatchEvent(new CustomEvent('skeletonTransformed'))
+ console.log('Redo successful')
+ } else {
+ console.log('No redo states available')
+ }
+ return result
}
public setup_scene (main_scene: Scene): void {
diff --git a/src/script.ts b/src/script.ts
index 60c300b..fcef8f4 100644
--- a/src/script.ts
+++ b/src/script.ts
@@ -71,6 +71,19 @@ export class Bootstrap {
this.eventListeners = new EventListeners(this)
// helps resolve requestAnimationFrame calling animate() with wrong context
this.animate = this.animate.bind(this)
+
+ // Listen for skeleton transformation events to update UI and visuals
+ this.edit_skeleton_step.addEventListener('skeletonTransformed', () => {
+ // Update skeleton helper if it exists
+ if (this.skeleton_helper !== undefined) {
+ this.regenerate_skeleton_helper(this.edit_skeleton_step.skeleton(), 'Skeleton Helper')
+ }
+
+ // Refresh weight painting if in weight painted mode
+ if (this.mesh_preview_display_type === ModelPreviewDisplay.WeightPainted) {
+ this.regenerate_weight_painted_preview_mesh()
+ }
+ })
}
private inject_build_version (): void {