|
| 1 | +import { InstrType } from 'js-slang/dist/ec-evaluator/types'; |
| 2 | +import { Easings } from 'konva/lib/Tween'; |
| 3 | + |
| 4 | +import { Animatable } from './animationComponents/AnimationComponents'; |
| 5 | +import { BinaryOperationAnimation } from './animationComponents/BinaryOperationAnimation'; |
| 6 | +import { BlockAnimation } from './animationComponents/BlockAnimation'; |
| 7 | +import { LiteralAnimation } from './animationComponents/LiteralAnimation'; |
| 8 | +import { PopAnimation } from './animationComponents/PopAnimation'; |
| 9 | +import { UnaryOperationAnimation } from './animationComponents/UnaryOperationAnimation'; |
| 10 | +import { isInstr } from './compactComponents/ControlStack'; |
| 11 | +import EnvVisualizer from './EnvVisualizer'; |
| 12 | +import { Layout } from './EnvVisualizerLayout'; |
| 13 | + |
| 14 | +export class CSEAnimation { |
| 15 | + private static animationEnabled = false; |
| 16 | + static readonly animationComponents: Animatable[] = []; |
| 17 | + static readonly defaultDuration = 0.3; |
| 18 | + static readonly defaultEasing = Easings.StrongEaseInOut; |
| 19 | + |
| 20 | + static enableAnimations(): void { |
| 21 | + CSEAnimation.animationEnabled = true; |
| 22 | + } |
| 23 | + |
| 24 | + static disableAnimations(): void { |
| 25 | + CSEAnimation.animationEnabled = false; |
| 26 | + } |
| 27 | + |
| 28 | + private static clearAnimationComponents(): void { |
| 29 | + CSEAnimation.animationComponents.length = 0; |
| 30 | + } |
| 31 | + |
| 32 | + static updateAnimation() { |
| 33 | + CSEAnimation.animationComponents.forEach(a => a.destroy()); |
| 34 | + CSEAnimation.clearAnimationComponents(); |
| 35 | + |
| 36 | + if (!Layout.previousControlComponent) return; |
| 37 | + const lastControlItem = Layout.previousControlComponent.control.peek(); |
| 38 | + const lastControlComponent = Layout.previousControlComponent.stackItemComponents.at(-1); |
| 39 | + if ( |
| 40 | + !CSEAnimation.animationEnabled || |
| 41 | + !lastControlItem || |
| 42 | + !lastControlComponent || |
| 43 | + !EnvVisualizer.getControlStash() // TODO: handle cases where there are environment animations |
| 44 | + ) { |
| 45 | + return; |
| 46 | + } |
| 47 | + let animation: Animatable | undefined; |
| 48 | + if (!isInstr(lastControlItem)) { |
| 49 | + switch (lastControlItem.type) { |
| 50 | + case 'Literal': |
| 51 | + animation = new LiteralAnimation( |
| 52 | + lastControlComponent, |
| 53 | + Layout.stashComponent.stashItemComponents.at(-1)! |
| 54 | + ); |
| 55 | + break; |
| 56 | + case 'Program': |
| 57 | + case 'ExpressionStatement': |
| 58 | + case 'VariableDeclaration': |
| 59 | + const currentControlSize = Layout.controlComponent.control.size(); |
| 60 | + const previousControlSize = Layout.previousControlComponent.control.size(); |
| 61 | + const numOfItems = currentControlSize - previousControlSize + 1; |
| 62 | + if (numOfItems <= 0) break; |
| 63 | + const targetItems = Array.from({ length: numOfItems }, (_, i) => { |
| 64 | + return Layout.controlComponent.stackItemComponents[previousControlSize + i - 1]; |
| 65 | + }); |
| 66 | + animation = new BlockAnimation(lastControlComponent, targetItems); |
| 67 | + break; |
| 68 | + } |
| 69 | + } else { |
| 70 | + switch (lastControlItem.instrType) { |
| 71 | + case InstrType.RESET: |
| 72 | + case InstrType.WHILE: |
| 73 | + case InstrType.FOR: |
| 74 | + case InstrType.ASSIGNMENT: |
| 75 | + break; |
| 76 | + case InstrType.UNARY_OP: |
| 77 | + animation = new UnaryOperationAnimation( |
| 78 | + lastControlComponent, |
| 79 | + Layout.previousStashComponent.stashItemComponents.at(-1)!, |
| 80 | + Layout.stashComponent.stashItemComponents.at(-1)! |
| 81 | + ); |
| 82 | + break; |
| 83 | + case InstrType.BINARY_OP: |
| 84 | + animation = new BinaryOperationAnimation( |
| 85 | + lastControlComponent, |
| 86 | + Layout.previousStashComponent.stashItemComponents.at(-2)!, |
| 87 | + Layout.previousStashComponent.stashItemComponents.at(-1)!, |
| 88 | + Layout.stashComponent.stashItemComponents.at(-1)! |
| 89 | + ); |
| 90 | + break; |
| 91 | + case InstrType.POP: |
| 92 | + animation = new PopAnimation( |
| 93 | + lastControlComponent, |
| 94 | + Layout.previousStashComponent.stashItemComponents.at(-1)! |
| 95 | + ); |
| 96 | + break; |
| 97 | + case InstrType.APPLICATION: |
| 98 | + case InstrType.BRANCH: |
| 99 | + case InstrType.ENVIRONMENT: |
| 100 | + case InstrType.ARRAY_LITERAL: |
| 101 | + case InstrType.ARRAY_ACCESS: |
| 102 | + case InstrType.ARRAY_ASSIGNMENT: |
| 103 | + case InstrType.ARRAY_LENGTH: |
| 104 | + case InstrType.CONTINUE_MARKER: |
| 105 | + case InstrType.BREAK: |
| 106 | + case InstrType.BREAK_MARKER: |
| 107 | + case InstrType.MARKER: |
| 108 | + } |
| 109 | + } |
| 110 | + if (animation) CSEAnimation.animationComponents.push(animation); |
| 111 | + } |
| 112 | + |
| 113 | + static playAnimation(): void { |
| 114 | + if (!CSEAnimation.animationEnabled) { |
| 115 | + CSEAnimation.disableAnimations(); |
| 116 | + return; |
| 117 | + } |
| 118 | + CSEAnimation.disableAnimations(); |
| 119 | + for (const animationComponent of this.animationComponents) { |
| 120 | + animationComponent.animate(); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments