-
Notifications
You must be signed in to change notification settings - Fork 172
CSE Machine: Added animation for spread instruction #3114
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ab18008
spread animation
alsonleej 62a3fdc
spread animation
alsonleej caf48a0
fixed bug with empty array, and animated control instr
alsonleej 08ba90d
fixed bug with empty array, and animated control instr
alsonleej 19d9e35
added animation for array access out of range
alsonleej 68843b6
add highlight to change in call arity
alsonleej bdc4221
remove console log
alsonleej 01711e7
Merge branch 'master' into spreadanim
alsonleej d189779
Merge branch 'master' into spreadanim
martin-henz 2d1a374
Merge branch 'master' into spreadanim
alsonleej 1992dd7
Update index.html
alsonleej 9312d38
Merge branch 'master' into spreadanim
alsonleej 7413d87
Update craco.config.js
alsonleej 017bcea
Update ArrayAccessAnimation.tsx
alsonleej 1f4c517
Update craco.config.js
alsonleej 7782608
Merge branch 'master' into spreadanim
martin-henz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
172 changes: 172 additions & 0 deletions
172
src/features/cseMachine/animationComponents/ArraySpreadAnimation.tsx
This file contains hidden or 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,172 @@ | ||
import { Easings } from 'konva/lib/Tween'; | ||
import React from 'react'; | ||
import { Group } from 'react-konva'; | ||
|
||
import { ControlItemComponent } from '../components/ControlItemComponent'; | ||
import { StashItemComponent } from '../components/StashItemComponent'; | ||
import { Visible } from '../components/Visible'; | ||
import { ControlStashConfig } from '../CseMachineControlStashConfig'; | ||
import { | ||
defaultActiveColor, | ||
defaultDangerColor, | ||
defaultStrokeColor, | ||
getTextWidth | ||
} from '../CseMachineUtils'; | ||
import { Animatable, AnimationConfig } from './base/Animatable'; | ||
import { AnimatedGenericArrow } from './base/AnimatedGenericArrow'; | ||
import { AnimatedTextbox } from './base/AnimatedTextbox'; | ||
import { getNodePosition } from './base/AnimationUtils'; | ||
|
||
/** | ||
* Adapted from InstructionApplicationAnimation, but changed resultAnimation to [], among others | ||
*/ | ||
export class ArraySpreadAnimation extends Animatable { | ||
private controlInstrAnimation: AnimatedTextbox; // the array literal control item | ||
private stashItemAnimation: AnimatedTextbox; | ||
private resultAnimations: AnimatedTextbox[]; | ||
private arrowAnimation?: AnimatedGenericArrow<StashItemComponent, Visible>; | ||
private currCallInstrAnimation: AnimatedTextbox; | ||
private prevCallInstrAnimation: AnimatedTextbox; | ||
|
||
private endX: number; | ||
|
||
constructor( | ||
private controlInstrItem: ControlItemComponent, | ||
private stashItem: StashItemComponent, | ||
private resultItems: StashItemComponent[], | ||
private currCallInstrItem: ControlItemComponent, | ||
private prevCallInstrItem: ControlItemComponent | ||
) { | ||
super(); | ||
|
||
console.log(stashItem); | ||
alsonleej marked this conversation as resolved.
Show resolved
Hide resolved
|
||
console.log(resultItems); | ||
|
||
this.endX = stashItem!.x() + stashItem!.width(); | ||
this.controlInstrAnimation = new AnimatedTextbox( | ||
controlInstrItem.text, | ||
getNodePosition(controlInstrItem), | ||
{ rectProps: { stroke: defaultActiveColor() } } | ||
); | ||
this.stashItemAnimation = new AnimatedTextbox(stashItem.text, getNodePosition(stashItem), { | ||
rectProps: { | ||
stroke: defaultDangerColor() | ||
} | ||
}); | ||
|
||
// call instr above | ||
this.prevCallInstrAnimation = new AnimatedTextbox( | ||
this.prevCallInstrItem.text, | ||
{ ...getNodePosition(this.prevCallInstrItem), opacity: 0 }, | ||
{ rectProps: { stroke: defaultActiveColor() } } | ||
); | ||
|
||
this.currCallInstrAnimation = new AnimatedTextbox( | ||
this.currCallInstrItem.text, | ||
{ ...getNodePosition(this.currCallInstrItem), opacity: 0 }, | ||
{ rectProps: { stroke: defaultActiveColor() } } | ||
); | ||
|
||
this.resultAnimations = resultItems.map(item => { | ||
return new AnimatedTextbox(item.text, { | ||
...getNodePosition(item), | ||
opacity: 0 | ||
}); | ||
}); | ||
if (stashItem.arrow) { | ||
this.arrowAnimation = new AnimatedGenericArrow(stashItem.arrow, { opacity: 0 }); | ||
} | ||
} | ||
|
||
draw(): React.ReactNode { | ||
return ( | ||
<Group ref={this.ref} key={Animatable.key--}> | ||
{this.controlInstrAnimation.draw()} | ||
{this.stashItemAnimation.draw()} | ||
{this.currCallInstrAnimation.draw()} | ||
{this.prevCallInstrAnimation.draw()} | ||
{this.resultAnimations.map(a => a.draw())} | ||
{this.arrowAnimation?.draw()} | ||
</Group> | ||
); | ||
} | ||
|
||
async animate(animationConfig?: AnimationConfig) { | ||
this.resultItems?.map(a => a.ref.current?.hide()); | ||
this.resultItems?.map(a => a.arrow?.ref.current?.hide()); | ||
const minInstrWidth = | ||
getTextWidth(this.controlInstrItem.text) + ControlStashConfig.ControlItemTextPadding * 2; | ||
const resultX = (idx: number) => this.resultItems[idx]?.x() ?? this.stashItem.x(); | ||
const resultY = this.resultItems[0]?.y() ?? this.stashItem.y(); | ||
const startX = resultX(0); | ||
const fadeDuration = ((animationConfig?.duration ?? 1) * 3) / 4; | ||
const fadeInDelay = (animationConfig?.delay ?? 0) + (animationConfig?.duration ?? 1) / 4; | ||
|
||
// Move spread instruction next to stash item (array pointer) | ||
await Promise.all([ | ||
// Show change in call arity | ||
this.prevCallInstrAnimation.animateTo( | ||
{ scaleX: 1.1, scaleY: 1.1, opacity: 0 }, | ||
{ duration: 0.3, easing: Easings.StrongEaseOut } | ||
), | ||
|
||
this.currCallInstrAnimation.animateTo( | ||
{ opacity: 1 }, | ||
{ duration: 0.3, easing: Easings.StrongEaseOut } | ||
), | ||
|
||
...this.resultAnimations.flatMap(a => [ | ||
a.animateTo( | ||
{ x: startX + (this.endX - startX) / 2 - this.resultItems[0]?.width() / 2 }, | ||
{ duration: 0 } | ||
) | ||
]), | ||
this.controlInstrAnimation.animateRectTo({ stroke: defaultStrokeColor() }, animationConfig), | ||
this.controlInstrAnimation.animateTo( | ||
{ | ||
x: startX, | ||
y: resultY + (this.resultItems[0]?.height() ?? this.stashItem.height()), | ||
width: minInstrWidth | ||
}, | ||
animationConfig | ||
), | ||
this.stashItemAnimation.animateRectTo({ stroke: defaultDangerColor() }, animationConfig) | ||
]); | ||
|
||
animationConfig = { ...animationConfig, delay: 0 }; | ||
// Merge all elements together to form the result | ||
await Promise.all([ | ||
this.controlInstrAnimation.animateTo({ x: resultX(0), y: resultY }, animationConfig), | ||
this.controlInstrAnimation.animateTo( | ||
{ opacity: 0 }, | ||
{ ...animationConfig, duration: fadeDuration } | ||
), | ||
this.stashItemAnimation.animateTo({ x: resultX(0) }, animationConfig), | ||
this.stashItemAnimation.animateTo( | ||
{ opacity: 0 }, | ||
{ ...animationConfig, duration: fadeDuration } | ||
), | ||
|
||
...this.resultAnimations.flatMap((a, idx) => [ | ||
a.animateTo({ x: resultX(idx) }, animationConfig), | ||
a.animateRectTo({ stroke: defaultDangerColor() }, animationConfig), | ||
a.animateTo( | ||
{ opacity: 1 }, | ||
{ ...animationConfig, duration: fadeDuration, delay: fadeInDelay } | ||
) | ||
]) | ||
]); | ||
|
||
this.destroy(); | ||
} | ||
|
||
destroy() { | ||
this.ref.current?.hide(); | ||
this.resultItems.map(a => a.ref.current?.show()); | ||
this.resultItems.map(a => a.arrow?.ref.current?.show()); | ||
this.controlInstrAnimation.destroy(); | ||
this.stashItemAnimation.destroy(); | ||
this.resultAnimations.map(a => a.destroy()); | ||
this.arrowAnimation?.destroy(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.