1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419 |
- import * as React from "react";
- import { Animation } from "babylonjs/Animations/animation";
- import { Vector2, Vector3, Quaternion } from "babylonjs/Maths/math.vector";
- import { Color3, Color4 } from "babylonjs/Maths/math.color";
- import { Size } from "babylonjs/Maths/math.size";
- import { EasingFunction } from "babylonjs/Animations/easing";
- import { IAnimationKey } from "babylonjs/Animations/animationKey";
- import { IKeyframeSvgPoint } from "./keyframeSvgPoint";
- import { SvgDraggableArea } from "./svgDraggableArea";
- import { Timeline } from "./timeline";
- import { Notification } from "./notification";
- import { GraphActionsBar } from "./graphActionsBar";
- import { Scene } from "babylonjs/scene";
- import { IAnimatable } from "babylonjs/Animations/animatable.interface";
- import { Animatable } from "babylonjs/Animations/animatable";
- import { TargetedAnimation } from "babylonjs/Animations/animationGroup";
- import { EditorControls } from "./editorControls";
- import { SelectedCoordinate } from "./animationListTree";
- import { LockObject } from "../lockObject";
- import { GlobalState } from "../../../../globalState";
- import { Nullable } from "babylonjs/types";
- import { Observer } from "babylonjs/Misc/observable";
- import { ScaleLabel } from "./scale-label";
- import { KeyframeSvgPoint } from "./keyframeSvgPoint";
- require("./curveEditor.scss");
- interface IAnimationCurveEditorComponentProps {
- playOrPause?: () => void;
- scene: Scene;
- entity: IAnimatable | TargetedAnimation;
- lockObject: LockObject;
- globalState: GlobalState;
- }
- interface ICanvasAxis {
- value: number;
- label: number;
- }
- export enum CurveScale {
- float,
- radians,
- degrees,
- integers,
- default,
- }
- export interface IActionableKeyFrame {
- frame?: number | string;
- value?: any;
- }
- interface ICurveData {
- pathData: string;
- pathLength: number;
- domCurve: React.RefObject<SVGPathElement>;
- color: string;
- id: string;
- }
- export class AnimationCurveEditorComponent extends React.Component<
- IAnimationCurveEditorComponentProps,
- {
- isOpen: boolean;
- selected: Animation | null;
- svgKeyframes: IKeyframeSvgPoint[] | undefined;
- currentFrame: number;
- currentValue: number;
- frameAxisLength: ICanvasAxis[];
- valueAxisLength: ICanvasAxis[];
- isFlatTangentMode: boolean;
- isTangentMode: boolean;
- isBrokenMode: boolean;
- lerpMode: boolean;
- scale: number;
- playheadOffset: number;
- notification: string;
- currentPoint: SVGPoint | undefined;
- playheadPos: number;
- isPlaying: boolean;
- selectedPathData: ICurveData[] | undefined;
- selectedCoordinate: number;
- animationLimit: number;
- fps: number;
- isLooping: boolean;
- panningY: number;
- panningX: number;
- repositionCanvas: boolean;
- actionableKeyframe: IActionableKeyFrame;
- valueScaleType: CurveScale;
- valueScale: number;
- canvasLength: number;
- lastKeyframeCreated: Nullable<string>;
- canvasWidthScale: number;
- valuesPositionResize: number;
- framesInCanvasView: { from: number; to: number };
- maxFrame: number | undefined;
- minFrame: number | undefined;
- }
- > {
- private _snippetUrl = "https://snippet.babylonjs.com";
- // Height scale *Review this functionaliy
- private _heightScale: number = 100;
- private _scaleFactor: number = 2;
- private _currentScale: number = 10;
- // Canvas Length *Review this functionality
- readonly _entityName: string;
- //private _canvasLength: number;
- private _svgKeyframes: IKeyframeSvgPoint[] = [];
- private _isPlaying: boolean = false;
- private _graphCanvas: React.RefObject<HTMLDivElement>;
- private _editor: React.RefObject<HTMLDivElement>;
- private _editorWindow: Window;
- //private _selectedCurve: React.RefObject<SVGPathElement>;
- private _svgCanvas: React.RefObject<SvgDraggableArea>;
- private _isTargetedAnimation: boolean;
- private _pixelFrameUnit: number;
- private _resizedTimeline: number;
- private _onBeforeRenderObserver: Nullable<Observer<Scene>>;
- private _mainAnimatable: Nullable<Animatable>;
- constructor(props: IAnimationCurveEditorComponentProps) {
- super(props);
- this._entityName = (this.props.entity as any).id;
- this._editor = React.createRef();
- this._graphCanvas = React.createRef();
- this._svgCanvas = React.createRef();
- this._pixelFrameUnit = 10;
- let initialSelection;
- let initialPathData;
- let initialLerpMode;
- if (this.props.entity instanceof TargetedAnimation) {
- this._isTargetedAnimation = true;
- initialSelection = this.props.entity.animation;
- initialLerpMode = initialSelection !== undefined ? this.analizeAnimationForLerp(initialSelection) : false;
- initialPathData = initialSelection !== undefined ? this.getPathData(initialSelection) : undefined;
- } else {
- this._isTargetedAnimation = false;
- let hasAnimations =
- this.props.entity.animations !== undefined || this.props.entity.animations !== null
- ? this.props.entity.animations
- : false;
- initialSelection = hasAnimations !== false ? hasAnimations && hasAnimations[0] : null;
- initialLerpMode =
- initialSelection !== undefined
- ? this.analizeAnimationForLerp(this.props.entity.animations && initialSelection)
- : false;
- initialPathData = initialSelection && this.getPathData(initialSelection);
- initialPathData = initialPathData === null || initialPathData === undefined ? undefined : initialPathData;
- }
- const _canvasLength = 240;
- this.stopAnimation();
- // will update this until we have a top scroll/zoom feature
- let valueInd = [2, 1.8, 1.6, 1.4, 1.2, 1, 0.8, 0.6, 0.4, 0.2, 0];
- this.state = {
- selected: initialSelection,
- isOpen: true,
- svgKeyframes: this._svgKeyframes,
- currentFrame: 0,
- currentValue: 1,
- isFlatTangentMode: false,
- isTangentMode: false,
- isBrokenMode: false,
- lerpMode: initialLerpMode,
- playheadOffset: this._graphCanvas.current
- ? this._graphCanvas.current.children[0].clientWidth / (_canvasLength * 10)
- : 0,
- frameAxisLength: this.setFrameAxis(_canvasLength),
- valueAxisLength: new Array(10).fill(0).map((s, i) => {
- return { value: i * 10, label: valueInd[i] };
- }),
- notification: "",
- currentPoint: undefined,
- scale: 1,
- playheadPos: 0,
- isPlaying: false,
- selectedPathData: initialPathData,
- selectedCoordinate: 0,
- animationLimit: _canvasLength / 2,
- canvasLength: _canvasLength,
- fps: 60,
- isLooping: true,
- panningY: 0,
- panningX: 0,
- repositionCanvas: false,
- actionableKeyframe: { frame: undefined, value: undefined },
- valueScaleType: CurveScale.default,
- valueScale: 2,
- lastKeyframeCreated: null,
- canvasWidthScale: 200,
- valuesPositionResize: 2,
- framesInCanvasView: { from: 0, to: 20 },
- maxFrame: undefined,
- minFrame: undefined,
- };
- }
- componentDidMount() {
- this.state.selected && this.selectAnimation(this.state.selected);
- if (
- this._editor.current &&
- this._editor.current.ownerDocument &&
- this._editor.current.ownerDocument.defaultView
- ) {
- this._editorWindow = this._editor.current.ownerDocument.defaultView;
- this._editorWindow.addEventListener("resize", this.onWindowResizeWidth.bind(this));
- }
- }
- componentDidUpdate(prevProps: IAnimationCurveEditorComponentProps, prevState: any) {
- if (prevState.currentFrame !== this.state.currentFrame) {
- this.onCurrentFrameChangeChangeScene(this.state.currentFrame);
- }
- }
- onCurrentFrameChangeChangeScene(value: number) {
- if (!this._mainAnimatable) {
- return;
- }
- this._mainAnimatable.goToFrame(value);
- }
- /**
- * Notifications
- * To add notification we set the state and clear to make the notification bar hide.
- */
- clearNotification = () => {
- this.setState({ notification: "" });
- };
- /**
- * Zoom and Scroll
- * This section handles zoom and scroll
- * of the graph area.
- */
- zoom = (e: React.WheelEvent<HTMLDivElement>) => {
- e.nativeEvent.stopImmediatePropagation();
- let scaleX = 1;
- if (Math.sign(e.deltaY) === -1) {
- scaleX = this.state.scale; //- 0.01; //+ 0.01;
- }
- this.setState({ scale: scaleX });
- };
- setFrameAxis(currentLength: number) {
- //const factor = 10 / this._pixelFrameUnit;
- let halfNegative = new Array(currentLength).fill(0).map((s, i) => {
- return { value: -i * 10, label: -i };
- });
- let halfPositive = new Array(currentLength).fill(0).map((s, i) => {
- return { value: i * 10, label: i };
- });
- return [...halfNegative, ...halfPositive];
- }
- setValueLines() {
- const lineV = this._heightScale / 10;
- const initialValues = new Array(this._currentScale).fill(0).map((_, i) => {
- return {
- value: i * lineV,
- label: (this._scaleFactor * ((this._currentScale - i) / this._currentScale)).toFixed(2),
- };
- });
- initialValues.shift();
- const valueHeight = Math.abs(Math.round(this.state.panningY / this._currentScale));
- const sign = Math.sign(this.state.panningY);
- const pannedValues = new Array(valueHeight).fill(0).map((s, i) => {
- return sign === -1
- ? {
- value: -i * lineV,
- label: ((i + this._currentScale) / (this._currentScale / this._scaleFactor)).toFixed(2),
- }
- : {
- value: (i + lineV) * this._currentScale,
- label: ((i * -1) / (this._currentScale / this._scaleFactor)).toFixed(2),
- };
- });
- return [...initialValues, ...pannedValues];
- }
- getValueLabel(i: number) {
- // Need to update this when Y axis grows
- let label = 0;
- if (i === 0) {
- label = 2;
- }
- if (i === 50) {
- label = 1;
- } else {
- label = (100 - i * 2) * 0.01 + 1;
- }
- return label;
- }
- resetPlayheadOffset() {
- if (this._graphCanvas && this._graphCanvas.current) {
- this.setState({
- playheadOffset:
- this._graphCanvas.current.children[0].clientWidth /
- (this.state.canvasLength * 10 * this.state.scale),
- });
- }
- }
- encodeCurveId(animationName: string, keyframeIndex: number) {
- return animationName + "_" + keyframeIndex;
- }
- decodeCurveId(id: string) {
- const order = parseInt(id.split("_")[3]);
- const coordinate = parseInt(id.split("_")[2]);
- return { order, coordinate };
- }
- getKeyframeValueFromAnimation(id: string) {
- const animation = this.state.selected as Animation;
- const { order, coordinate } = this.decodeCurveId(id);
- const keys = [...animation.getKeys()];
- const key = keys.find((_, i) => i === order);
- if (key) {
- const valueAsArray = this.getValueAsArray(animation.dataType, key.value);
- return { frame: key?.frame, value: valueAsArray[coordinate] };
- } else {
- return undefined;
- }
- }
- /**
- * Keyframe Manipulation
- * This section handles events from SvgDraggableArea.
- */
- selectKeyframe = (id: string, multiselect: boolean) => {
- let frameValue: IActionableKeyFrame | undefined;
- const selectedKeyframe = this.state.svgKeyframes?.find((kf) => kf.id === id);
- const isKeyFrameSelected = selectedKeyframe?.selected;
- const hasCollinearPoints = this.hasCollinearPoints(selectedKeyframe);
- if (!multiselect) {
- frameValue = this.getKeyframeValueFromAnimation(id);
- this.deselectKeyframes();
- } else {
- frameValue = { frame: undefined, value: undefined };
- }
- if (isKeyFrameSelected) {
- frameValue = { frame: undefined, value: undefined };
- }
- const updatedKeyframes = this.state.svgKeyframes?.map((kf) => {
- if (kf.id === id) {
- kf.selected = !isKeyFrameSelected;
- }
- return kf;
- });
- let maxFrame = undefined;
- let minFrame = undefined;
- if (frameValue && typeof frameValue.frame === "number") {
- let { prev, next } = this.getPreviousAndNextKeyframe(frameValue.frame);
- maxFrame = next;
- minFrame = prev;
- }
- this.setState({
- svgKeyframes: updatedKeyframes,
- actionableKeyframe: frameValue ?? this.state.actionableKeyframe,
- maxFrame: maxFrame,
- minFrame: minFrame,
- isBrokenMode: !hasCollinearPoints,
- });
- };
- hasCollinearPoints = (kf: IKeyframeSvgPoint | undefined) => {
- const left = kf?.leftControlPoint;
- const right = kf?.rightControlPoint;
- if (left === undefined || right === undefined || left === null || right === null) {
- return false;
- } else {
- if (left.y === right.y) {
- return true;
- } else {
- return false;
- }
- }
- };
- getPreviousAndNextKeyframe = (frame: number) => {
- let prev,
- next = undefined;
- const animation = this.state.selected;
- if (animation) {
- const keys = animation.getKeys();
- if (keys) {
- const index = keys.findIndex((x) => x.frame === frame);
- prev = keys[index - 1] && keys[index - 1].frame + 1;
- next = keys[index + 1] && keys[index + 1].frame - 1;
- }
- }
- return { prev, next };
- };
- selectKeyframeFromId = (id: string, actionableKeyframe: IActionableKeyFrame) => {
- this.deselectKeyframes();
- const updatedKeyframes = this.state.svgKeyframes?.map((kf) => {
- if (kf.id === id) {
- kf.selected = true;
- }
- return kf;
- });
- let { prev, next } = this.getPreviousAndNextKeyframe(actionableKeyframe.frame as number);
- this.setState({
- svgKeyframes: updatedKeyframes,
- actionableKeyframe: actionableKeyframe ?? this.state.actionableKeyframe,
- maxFrame: next,
- minFrame: prev,
- });
- };
- resetActionableKeyframe = () => {
- this.setState({
- actionableKeyframe: { frame: undefined, value: undefined },
- maxFrame: undefined,
- minFrame: undefined,
- });
- };
- selectedControlPoint = (type: string, id: string) => {
- const controlPoint = this.state.svgKeyframes?.find((x) => x.id === id);
- if (controlPoint) {
- let isSelected;
- if (type === "left") {
- isSelected = controlPoint.isLeftActive;
- controlPoint.isLeftActive = !isSelected;
- controlPoint.isRightActive = false;
- }
- if (type === "right") {
- isSelected = controlPoint.isRightActive;
- controlPoint.isRightActive = !isSelected;
- controlPoint.isLeftActive = false;
- }
- }
- let updatedKeyframes = this.state.svgKeyframes?.map((kf) => {
- if (kf.id === id) {
- kf === controlPoint;
- }
- return kf;
- });
- this.setState({ svgKeyframes: updatedKeyframes });
- };
- deselectKeyframes = () => {
- let updatedKeyframes = this.state.svgKeyframes?.map((kf) => {
- kf.isLeftActive = false;
- kf.isRightActive = false;
- kf.selected = false;
- return kf;
- });
- this.setState({
- svgKeyframes: updatedKeyframes,
- actionableKeyframe: { frame: undefined, value: undefined },
- maxFrame: undefined,
- minFrame: undefined,
- });
- };
- updateValuePerCoordinate(
- dataType: number,
- value: number | Vector2 | Vector3 | Color3 | Color4 | Size | Quaternion,
- newValue: number,
- coordinate?: number
- ) {
- if (dataType === Animation.ANIMATIONTYPE_FLOAT) {
- value = newValue;
- }
- if (dataType === Animation.ANIMATIONTYPE_VECTOR2) {
- switch (coordinate) {
- case SelectedCoordinate.x:
- (value as Vector2).x = newValue;
- break;
- case SelectedCoordinate.y:
- (value as Vector2).y = newValue;
- break;
- }
- }
- if (dataType === Animation.ANIMATIONTYPE_VECTOR3) {
- switch (coordinate) {
- case SelectedCoordinate.x:
- (value as Vector3).x = newValue;
- break;
- case SelectedCoordinate.y:
- (value as Vector3).y = newValue;
- break;
- case SelectedCoordinate.z:
- (value as Vector3).z = newValue;
- break;
- }
- }
- if (dataType === Animation.ANIMATIONTYPE_QUATERNION) {
- switch (coordinate) {
- case SelectedCoordinate.x:
- (value as Quaternion).x = newValue;
- break;
- case SelectedCoordinate.y:
- (value as Quaternion).y = newValue;
- break;
- case SelectedCoordinate.z:
- (value as Quaternion).z = newValue;
- break;
- case SelectedCoordinate.w:
- (value as Quaternion).w = newValue;
- break;
- }
- }
- if (dataType === Animation.ANIMATIONTYPE_COLOR3) {
- switch (coordinate) {
- case SelectedCoordinate.r:
- (value as Color3).r = newValue;
- break;
- case SelectedCoordinate.g:
- (value as Color3).g = newValue;
- break;
- case SelectedCoordinate.b:
- (value as Color3).b = newValue;
- break;
- }
- }
- if (dataType === Animation.ANIMATIONTYPE_COLOR4) {
- switch (coordinate) {
- case SelectedCoordinate.r:
- (value as Color4).r = newValue;
- break;
- case SelectedCoordinate.g:
- (value as Color4).g = newValue;
- break;
- case SelectedCoordinate.b:
- (value as Color4).b = newValue;
- break;
- case SelectedCoordinate.a:
- (value as Color4).a = newValue;
- break;
- }
- }
- if (dataType === Animation.ANIMATIONTYPE_SIZE) {
- switch (coordinate) {
- case SelectedCoordinate.width:
- (value as Size).width = newValue;
- break;
- case SelectedCoordinate.g:
- (value as Size).height = newValue;
- break;
- }
- }
- return value;
- }
- forceFrameZeroToExist(keys: IAnimationKey[]) {
- const zeroFrame = keys.find((x) => Math.abs(x.frame) === 0);
- if (zeroFrame === undefined) {
- const prevToZero = keys.filter((x) => Math.sign(x.frame) === -1).sort((a, b) => b.frame - a.frame);
- let value;
- if (prevToZero.length !== 0) {
- value = prevToZero[0].value;
- } else {
- value = 1;
- }
- const frame: IAnimationKey = { frame: 0, value };
- keys.push(frame);
- keys.sort((a, b) => a.frame - b.frame);
- }
- }
- renderPoints = (updatedSvgKeyFrame: IKeyframeSvgPoint, id: string) => {
- let animation = this.state.selected as Animation;
- const { order: index, coordinate } = this.decodeCurveId(id);
- let keys = [...animation.getKeys()];
- let newFrame = 0;
- if (updatedSvgKeyFrame.keyframePoint.x !== 0) {
- if (updatedSvgKeyFrame.keyframePoint.x > 0 && updatedSvgKeyFrame.keyframePoint.x < 1) {
- newFrame = 0;
- } else {
- newFrame = Math.round(updatedSvgKeyFrame.keyframePoint.x / this._pixelFrameUnit);
- }
- }
- if (newFrame > keys[index].frame) {
- if (index === keys.length - 1) {
- keys[index].frame = newFrame;
- } else {
- const nextKf = keys[index + 1];
- if (nextKf) {
- if (nextKf.frame <= newFrame) {
- keys[index].frame = keys[index].frame;
- } else {
- keys[index].frame = newFrame;
- }
- }
- }
- }
- if (newFrame < keys[index].frame) {
- if (index === 0) {
- keys[index].frame = newFrame;
- } else {
- const prevKf = keys[index - 1];
- if (prevKf) {
- if (prevKf.frame >= newFrame) {
- keys[index].frame = keys[index].frame;
- } else {
- keys[index].frame = newFrame;
- }
- }
- }
- }
- let updatedValue =
- ((this._heightScale - updatedSvgKeyFrame.keyframePoint.y) / this._heightScale) * this._scaleFactor;
- const updatedValueInCoordinate = this.updateValuePerCoordinate(
- animation.dataType,
- keys[index].value,
- updatedValue,
- coordinate
- );
- keys[index].value = updatedValueInCoordinate;
- this.updateLeftControlPoint(updatedSvgKeyFrame, keys[index], animation.dataType, coordinate);
- this.updateRightControlPoint(updatedSvgKeyFrame, keys[index], animation.dataType, coordinate);
- this.forceFrameZeroToExist(keys);
- animation.setKeys(keys);
- let { prev, next } = this.getPreviousAndNextKeyframe(newFrame);
- this.setState({
- actionableKeyframe: { frame: newFrame, value: updatedValueInCoordinate },
- maxFrame: next,
- minFrame: prev,
- });
- this.selectAnimation(animation, coordinate);
- };
- updateLeftControlPoint(
- updatedSvgKeyFrame: IKeyframeSvgPoint,
- key: IAnimationKey,
- dataType: number,
- coordinate: number
- ) {
- if (updatedSvgKeyFrame.isLeftActive) {
- if (updatedSvgKeyFrame.leftControlPoint !== null) {
- // Rotate Control Points
- // Get the previous svgKeyframe and measure distance between these two points
- let distanceWithPreviousKeyframe = this.getControlPointWeight(updatedSvgKeyFrame);
- // Get a quarter of that value for amplitude
- let distanceAmplitudeOfX = updatedSvgKeyFrame.leftControlPoint.x - distanceWithPreviousKeyframe;
- let slope =
- (updatedSvgKeyFrame.leftControlPoint.y - updatedSvgKeyFrame.keyframePoint.y) /
- (updatedSvgKeyFrame.leftControlPoint.x - updatedSvgKeyFrame.keyframePoint.x);
- let newValueOfY =
- (distanceAmplitudeOfX - updatedSvgKeyFrame.leftControlPoint.x) * slope +
- updatedSvgKeyFrame.keyframePoint.y;
- let updatedValue =
- ((newValueOfY - updatedSvgKeyFrame.keyframePoint.y) * this._scaleFactor) / this._heightScale;
- if (updatedValue > -100 && updatedValue < 100) {
- key.inTangent = slope; //this.updateValuePerCoordinate(dataType, key.inTangent, updatedValue, coordinate);
- if (!this.state.isBrokenMode) {
- if (updatedSvgKeyFrame.rightControlPoint !== null) {
- // Sets opposite value
- // get angle between control points and keep angle...
- key.outTangent = key.inTangent * -1;
- }
- }
- }
- }
- }
- }
- updateRightControlPoint(
- updatedSvgKeyFrame: IKeyframeSvgPoint,
- key: IAnimationKey,
- dataType: number,
- coordinate: number
- ) {
- if (updatedSvgKeyFrame.isRightActive) {
- if (updatedSvgKeyFrame.rightControlPoint !== null) {
- // Rotate Control Points
- // Get the next svgKeyframe and measure distance between these two points
- let distanceWithNextKeyframe = this.getControlPointWeight(updatedSvgKeyFrame);
- let distanceAmplitudeOfX = updatedSvgKeyFrame.rightControlPoint.x + distanceWithNextKeyframe;
- let slope =
- (updatedSvgKeyFrame.rightControlPoint.y - updatedSvgKeyFrame.keyframePoint.y) /
- (updatedSvgKeyFrame.rightControlPoint.x - updatedSvgKeyFrame.keyframePoint.x);
- let newValueOfY =
- (distanceAmplitudeOfX - updatedSvgKeyFrame.rightControlPoint.x) * slope +
- updatedSvgKeyFrame.keyframePoint.y;
- let updatedValue =
- ((newValueOfY - updatedSvgKeyFrame.keyframePoint.y) * this._scaleFactor) / this._heightScale;
- if (updatedValue > -100 && updatedValue < 100) {
- key.outTangent = slope * -1; //this.updateValuePerCoordinate(dataType, key.outTangent, updatedValue, coordinate);
- if (!this.state.isBrokenMode) {
- if (updatedSvgKeyFrame.leftControlPoint !== null) {
- // Sets opposite value
- key.inTangent = key.outTangent * -1;
- }
- }
- }
- }
- }
- }
- getControlPointWeight(updatedSvgKeyFrame: IKeyframeSvgPoint) {
- let distanceWithPreviousKeyframe = this.state.canvasWidthScale / 4;
- if (this.state.svgKeyframes) {
- let indexOfKeyframe = this.state.svgKeyframes.indexOf(updatedSvgKeyFrame);
- let previousKeyframe = this.state.svgKeyframes[indexOfKeyframe - 1];
- if (previousKeyframe?.keyframePoint) {
- distanceWithPreviousKeyframe =
- Vector2.Distance(updatedSvgKeyFrame.keyframePoint, previousKeyframe.keyframePoint) / 2;
- }
- }
- let distanceWithNextKeyframe = this.state.canvasWidthScale / 4;
- if (this.state.svgKeyframes) {
- let indexOfKeyframe = this.state.svgKeyframes.indexOf(updatedSvgKeyFrame);
- let nextKeyframe = this.state.svgKeyframes[indexOfKeyframe + 1];
- if (nextKeyframe?.keyframePoint) {
- distanceWithNextKeyframe =
- Vector2.Distance(nextKeyframe.keyframePoint, updatedSvgKeyFrame.keyframePoint) / 2;
- }
- }
- if (distanceWithPreviousKeyframe < distanceWithNextKeyframe) {
- return distanceWithPreviousKeyframe;
- } else {
- return distanceWithNextKeyframe;
- }
- }
- handleFrameChange = (event: React.ChangeEvent<HTMLInputElement>) => {
- event.preventDefault();
- let frame;
- let maxFrame = undefined;
- let minFrame = undefined;
- if (event.target.value === "") {
- frame = "";
- } else {
- frame = parseInt(event.target.value);
- const { prev, next } = this.getPreviousAndNextKeyframe(frame);
- maxFrame = next;
- minFrame = prev;
- }
- this.setState({
- actionableKeyframe: {
- frame: frame,
- value: this.state.actionableKeyframe.value,
- },
- maxFrame,
- minFrame,
- });
- };
- handleValueChange = (event: React.ChangeEvent<HTMLInputElement>) => {
- event.preventDefault();
- let value;
- if (event.target.value !== undefined) {
- if (event.target.value !== "") {
- value = parseFloat(event.target.value);
- } else {
- value = "";
- }
- this.setState({
- actionableKeyframe: {
- frame: this.state.actionableKeyframe.frame,
- value: value,
- },
- maxFrame: undefined,
- minFrame: undefined,
- });
- }
- };
- setKeyframeValueFromInput = (actionableKeyframe: IActionableKeyFrame) => {
- this.setState(
- {
- actionableKeyframe,
- },
- this.setKeyframeValue
- );
- };
- setKeyframeValue = () => {
- if (
- this.state.actionableKeyframe.frame !== "" &&
- this.state.actionableKeyframe.frame !== undefined &&
- this.state.actionableKeyframe.value !== "" &&
- this.state.actionableKeyframe.value !== undefined
- ) {
- if (this.state.selected !== null) {
- let currentSelected = this.state.svgKeyframes?.find((kf) => kf.selected);
- if (currentSelected) {
- let { order, coordinate } = this.decodeCurveId(currentSelected.id);
- let animation = this.state.selected;
- let keys = animation.getKeys();
- let isKeyframe = keys.find((_, i) => i === order);
- if (isKeyframe) {
- let updatedKeys = keys.map((k, i) => {
- if (i === order) {
- k.frame = this.state.actionableKeyframe.frame as number;
- const currentValue = this.getValueAsArray(animation.dataType, k.value);
- currentValue[coordinate] = this.state.actionableKeyframe.value;
- k.value = currentValue.length === 1 ? currentValue[0] : currentValue;
- }
- return k;
- });
- this.forceFrameZeroToExist(updatedKeys);
- this.state.selected.setKeys(updatedKeys);
- this.selectAnimation(animation);
- this.setCanvasPosition({
- frame: this.state.actionableKeyframe.frame as number,
- value: this.state.actionableKeyframe.value,
- });
- const { prev, next } = this.getPreviousAndNextKeyframe(
- this.state.actionableKeyframe.frame as number
- );
- this.setState({ maxFrame: next, minFrame: prev });
- }
- }
- }
- }
- };
- setFlatTangent = () => {
- if (this.state.selected !== null) {
- const keyframes = this.state.svgKeyframes?.filter((kf) => kf.selected).map((k) => this.decodeCurveId(k.id));
- const currentAnimation = this.state.selected;
- const keys = currentAnimation.getKeys();
- if (this.state.isBrokenMode) {
- const keyframeWithControlPointSelected = this.state.svgKeyframes?.find((kf) => kf.selected);
- if (keyframeWithControlPointSelected) {
- keyframes?.forEach((k) => {
- const keyframe = keys[k.order];
- if (keyframeWithControlPointSelected.isLeftActive) {
- keyframe.inTangent = this.returnZero(currentAnimation.dataType);
- }
- if (keyframeWithControlPointSelected.isRightActive) {
- keyframe.outTangent = this.returnZero(currentAnimation.dataType);
- }
- });
- }
- } else {
- keyframes?.forEach((k) => {
- const keyframe = keys[k.order];
- keyframe.inTangent = this.returnZero(currentAnimation.dataType);
- keyframe.outTangent = this.returnZero(currentAnimation.dataType);
- });
- }
- currentAnimation.setKeys(keys);
- this.selectAnimation(currentAnimation, this.state.selectedCoordinate);
- }
- };
- setBrokenMode = () => {
- if (this.state.selected !== null) {
- let animation = this.state.selected;
- this.setState({ isBrokenMode: !this.state.isBrokenMode }, () =>
- this.selectAnimation(animation, this.state.selectedCoordinate)
- );
- }
- };
- setLerpToActiveControlPoint = () => {
- const animation = this.state.selected;
- if (this.state.svgKeyframes && animation) {
- const keys = animation.getKeys();
- const selectedKeyframe = this.state.svgKeyframes.find(
- (keyframe: IKeyframeSvgPoint) => keyframe.selected && (keyframe.isLeftActive || keyframe.isRightActive)
- );
- if (selectedKeyframe !== null && selectedKeyframe) {
- const { order, coordinate } = this.decodeCurveId(selectedKeyframe.id);
- const key = keys[order];
- if (selectedKeyframe.isLeftActive && selectedKeyframe.leftControlPoint !== null) {
- const start = new Vector2(key.frame, key.value);
- const prev = new Vector2(keys[order - 1].frame, keys[order - 1].value);
- //const pointHalf = Vector2.Lerp(prev, start, 0.5); // Explore the distance of the point
- //const inTangent = pointHalf.y - start.y;
- let slope = (start.y - prev.y) / (start.x - prev.x);
- key.inTangent = slope * -1;
- } else if (selectedKeyframe.isRightActive && selectedKeyframe.rightControlPoint !== null) {
- const start = new Vector2(key.frame, key.value);
- const next = new Vector2(keys[order + 1].frame, keys[order + 1].value);
- //const pointHalf = Vector2.Lerp(start, next, 0.5); // Explore the distance of the point
- let slope = (next.y - start.y) / (next.x - start.x);
- // const outTangent = pointHalf.y - start.y;
- key.outTangent = slope;
- }
- this.setState({ isBrokenMode: true }, () => {
- this.selectAnimation(animation, coordinate);
- });
- }
- }
- };
- addKeyframeClick = () => {
- if (this.state.selected !== null) {
- let currentAnimation = this.state.selected;
- let keys = currentAnimation.getKeys();
- let x = this.state.currentFrame;
- let existValue = keys.find((k) => k.frame === x);
- if (existValue === undefined) {
- let y = this.state.actionableKeyframe.value ?? 1;
- // check if value exists...
- let arrayValue: any = [];
- let emptyValue = this.returnZero(currentAnimation.dataType);
- if (emptyValue) {
- arrayValue = this.getValueAsArray(currentAnimation.dataType, emptyValue);
- }
- // calculate point between prevkeyframe and nextkeyframe.
- const previousKFs = keys.filter((kf) => kf.frame < x);
- const nextKFs = keys.filter((kf) => kf.frame > x);
- const prev = previousKFs.slice(-1)[0];
- const next = nextKFs[0];
- if (prev === undefined && next) {
- y = next.value;
- }
- if (prev && next === undefined) {
- y = prev.value;
- }
- if (prev && next) {
- const value1 = new Vector2(prev.frame, prev.value);
- const tangent1 = new Vector2(prev.outTangent, prev.outTangent);
- const value2 = new Vector2(next.frame, next.value);
- const tangent2 = new Vector2(next.inTangent, next.inTangent);
- const amount = (x - prev.frame) / (next.frame - prev.frame);
- const newV = Vector2.Hermite(value1, tangent1, value2, tangent2, amount);
- y = newV.y;
- }
- arrayValue[this.state.selectedCoordinate] = y;
- let actualValue = this.setValueAsType(currentAnimation.dataType, arrayValue);
- const recentlyCreated = {
- frame: x,
- value: actualValue,
- inTangent: this.state.isFlatTangentMode ? 0 : 0, // check if flat mode can be turn off
- outTangent: this.state.isFlatTangentMode ? 0 : 0, // check if flat mode can be turn off
- };
- keys.push(recentlyCreated);
- keys.sort((a, b) => a.frame - b.frame);
- const newIndex = keys.findIndex((kf) => kf.frame === x);
- const id = `${currentAnimation.name}_${currentAnimation.targetProperty}_${this.state.selectedCoordinate}`;
- const curvedId = this.encodeCurveId(id, newIndex);
- this.setState({ lastKeyframeCreated: curvedId });
- this.forceFrameZeroToExist(keys);
- currentAnimation.setKeys(keys);
- this.selectAnimation(currentAnimation, this.state.selectedCoordinate);
- }
- }
- };
- removeKeyframeClick = () => {
- if (this.state.selected !== null) {
- let currentAnimation = this.state.selected;
- if (currentAnimation.dataType === Animation.ANIMATIONTYPE_FLOAT) {
- let keys = currentAnimation.getKeys();
- let x = this.state.currentFrame;
- let filteredKeys = keys.filter((kf) => kf.frame !== x);
- currentAnimation.setKeys(filteredKeys);
- this.selectAnimation(currentAnimation, this.state.selectedCoordinate);
- }
- }
- };
- removeKeyframes = (points: IKeyframeSvgPoint[]) => {
- if (this.state.selected !== null) {
- let currentAnimation = this.state.selected;
- const indexesToRemove = points.map((p) => {
- return {
- index: parseInt(p.id.split("_")[3]),
- coordinate: parseInt(p.id.split("_")[2]),
- };
- });
- if (currentAnimation.dataType === Animation.ANIMATIONTYPE_FLOAT) {
- let keys = currentAnimation.getKeys();
- let filteredKeys = keys.filter((_, i) => {
- return !indexesToRemove.find((x) => x.index === i);
- });
- currentAnimation.setKeys(filteredKeys);
- this.deselectKeyframes();
- this.selectAnimation(currentAnimation, this.state.selectedCoordinate);
- }
- }
- };
- addKeyFrame(event: React.MouseEvent<SVGSVGElement>) {
- event.preventDefault();
- if (this.state.selected !== null) {
- var svg = event.target as SVGSVGElement;
- var pt = svg.createSVGPoint();
- pt.x = event.clientX;
- pt.y = event.clientY;
- var inverse = svg.getScreenCTM()?.inverse();
- var cursorpt = pt.matrixTransform(inverse);
- var currentAnimation = this.state.selected;
- var keys = currentAnimation.getKeys();
- var height = 100;
- var middle = height / 2;
- var keyValue;
- if (cursorpt.y < middle) {
- keyValue = 1 + (100 / cursorpt.y) * 0.1;
- }
- if (cursorpt.y > middle) {
- keyValue = 1 - (100 / cursorpt.y) * 0.1;
- }
- keys.push({ frame: cursorpt.x, value: keyValue });
- currentAnimation.setKeys(keys);
- this.selectAnimation(currentAnimation);
- }
- }
- /**
- * Curve Rendering Functions
- * This section handles how to render curves.
- */
- setKeyframePointLinear(point: Vector2, index: number) {
- // here set the ID to a unique id
- let svgKeyframe = {
- keyframePoint: point,
- rightControlPoint: null,
- leftControlPoint: null,
- id: index.toString(),
- selected: false,
- isLeftActive: false,
- isRightActive: false,
- };
- this._svgKeyframes.push(svgKeyframe);
- }
- flatTangents(keyframes: IAnimationKey[], dataType: number) {
- // Checks if Flat Tangent is active (tangents are set to zero)
- // only flat the selected control point
- // if multiple selected then flat all...
- let flattened;
- if (this.state && this.state.isFlatTangentMode) {
- flattened = keyframes.map((kf) => {
- if (kf.inTangent !== undefined) {
- kf.inTangent = this.returnZero(dataType);
- }
- if (kf.outTangent !== undefined) {
- kf.outTangent = this.returnZero(dataType);
- }
- return kf;
- });
- } else {
- flattened = keyframes;
- }
- this.setState({ isFlatTangentMode: false });
- return flattened;
- }
- returnZero(dataType: number) {
- switch (dataType) {
- case Animation.ANIMATIONTYPE_FLOAT:
- return 0;
- case Animation.ANIMATIONTYPE_VECTOR3:
- return Vector3.Zero();
- case Animation.ANIMATIONTYPE_VECTOR2:
- return Vector2.Zero();
- case Animation.ANIMATIONTYPE_QUATERNION:
- return Quaternion.Zero();
- case Animation.ANIMATIONTYPE_COLOR3:
- return new Color3(0, 0, 0);
- case Animation.ANIMATIONTYPE_COLOR4:
- return new Color4(0, 0, 0, 0);
- case Animation.ANIMATIONTYPE_SIZE:
- return new Size(0, 0);
- default:
- return 0;
- }
- }
- getValueAsArray(valueType: number, value: number | Vector2 | Vector3 | Color3 | Color4 | Size | Quaternion) {
- switch (valueType) {
- case Animation.ANIMATIONTYPE_FLOAT:
- return [value as number];
- case Animation.ANIMATIONTYPE_VECTOR3:
- return (value as Vector3).asArray();
- case Animation.ANIMATIONTYPE_VECTOR2:
- return (value as Vector2).asArray();
- case Animation.ANIMATIONTYPE_QUATERNION:
- return (value as Quaternion).asArray();
- case Animation.ANIMATIONTYPE_COLOR3:
- return (value as Color3).asArray();
- case Animation.ANIMATIONTYPE_COLOR4:
- return (value as Color4).asArray();
- case Animation.ANIMATIONTYPE_SIZE:
- return [(value as Size).width, (value as Size).height];
- default:
- return [];
- }
- }
- setValueAsType(valueType: number, arrayValue: number[]) {
- switch (valueType) {
- case Animation.ANIMATIONTYPE_FLOAT:
- return arrayValue[0];
- case Animation.ANIMATIONTYPE_VECTOR3:
- return new Vector3(arrayValue[0], arrayValue[1], arrayValue[2]);
- case Animation.ANIMATIONTYPE_VECTOR2:
- return new Vector2(arrayValue[0], arrayValue[1]);
- case Animation.ANIMATIONTYPE_QUATERNION:
- return new Quaternion(arrayValue[0], arrayValue[1], arrayValue[2], arrayValue[3]);
- case Animation.ANIMATIONTYPE_COLOR3:
- return new Color3(arrayValue[0], arrayValue[1], arrayValue[2]);
- case Animation.ANIMATIONTYPE_COLOR4:
- return new Color4(arrayValue[0], arrayValue[1], arrayValue[2], arrayValue[3]);
- case Animation.ANIMATIONTYPE_SIZE:
- return new Size(arrayValue[0], arrayValue[1]);
- default:
- return arrayValue[0];
- }
- }
- getPathData(animation: Animation | null) {
- if (animation === null) {
- return undefined;
- }
- var keyframes = animation.getKeys();
- if (keyframes === undefined || keyframes.length === 0) {
- return undefined;
- } else {
- const {
- easingMode,
- easingType,
- usesTangents,
- valueType,
- highestFrame,
- name,
- targetProperty,
- } = this.getAnimationData(animation);
- //keyframes = this.flatTangents(keyframes, valueType); // This will break because we are using setState before mounted...
- const startKey = keyframes[0];
- let middle = this._heightScale / this._scaleFactor; //?
- let collection: ICurveData[] = [];
- const colors = ["red", "green", "blue", "white", "#7a4ece"];
- const startValue = this.getValueAsArray(valueType, startKey.value);
- for (var d = 0; d < startValue.length; d++) {
- const id = `${name}_${targetProperty}_${d}`;
- const curveColor = valueType === Animation.ANIMATIONTYPE_FLOAT ? colors[4] : colors[d];
- // START OF LINE/CURVE
- let data: string | undefined = `M${startKey.frame * this._pixelFrameUnit}, ${
- this._heightScale - startValue[d] * middle
- }`; //
- if (this.state) {
- if (usesTangents) {
- data = this.curvePathWithTangents(keyframes, data, middle, valueType, d, id);
- } else {
- if (easingType !== undefined && easingMode !== undefined) {
- let easingFunction = animation.getEasingFunction();
- data = this.curvePath(keyframes, data, middle, easingFunction as EasingFunction);
- } else {
- if (this.state !== undefined) {
- data = this.curvePathWithoutTangents(keyframes, data, middle, valueType, d, id);
- }
- }
- }
- }
- collection.push({
- pathData: data,
- pathLength: highestFrame,
- domCurve: React.createRef(),
- color: curveColor,
- id: id,
- });
- }
- return collection;
- }
- }
- getAnimationData(animation: Animation) {
- // General Props
- let loopMode = animation.loopMode;
- let name = animation.name;
- let blendingSpeed = animation.blendingSpeed;
- let targetProperty = animation.targetProperty;
- let targetPropertyPath = animation.targetPropertyPath;
- let framesPerSecond = animation.framePerSecond;
- let highestFrame = animation.getHighestFrame();
- //let serialized = animation.serialize();
- let usesTangents =
- animation.getKeys().find((kf) => kf.hasOwnProperty("inTangent") || kf.hasOwnProperty("outTangent")) !==
- undefined
- ? true
- : false;
- let valueType = animation.dataType;
- // easing properties
- let easingType, easingMode;
- let easingFunction: EasingFunction = animation.getEasingFunction() as EasingFunction;
- if (easingFunction === undefined) {
- easingType = undefined;
- easingMode = undefined;
- } else {
- easingType = easingFunction.constructor.name;
- easingMode = easingFunction.getEasingMode();
- }
- return {
- loopMode,
- name,
- blendingSpeed,
- targetPropertyPath,
- targetProperty,
- framesPerSecond,
- highestFrame,
- usesTangents,
- easingType,
- easingMode,
- valueType,
- };
- }
- calculateLinearTangents(keyframes: IAnimationKey[]) {
- const updatedKeyframes: IAnimationKey[] = keyframes.map((kf, i) => {
- if (keyframes[i + 1] !== undefined) {
- const start = new Vector2(keyframes[i].frame, keyframes[i].value);
- const next = new Vector2(keyframes[i + 1].frame, keyframes[i + 1].value);
- let slope = (next.y - start.y) / (next.x - start.x);
- kf.outTangent = slope;
- }
- if (keyframes[i - 1] !== undefined) {
- const start = new Vector2(keyframes[i].frame, keyframes[i].value);
- const prev = new Vector2(keyframes[i - 1].frame, keyframes[i - 1].value);
- let slope = (prev.y - start.y) / (prev.x - start.x);
- kf.inTangent = slope * -1;
- }
- if (i === keyframes.length - 1) {
- kf.outTangent = null;
- }
- return kf;
- });
- return updatedKeyframes;
- }
- curvePathWithoutTangents(
- keyframes: IAnimationKey[],
- data: string,
- middle: number,
- type: number,
- coordinate: number,
- animationName: string
- ) {
- const updatedKeyframes = this.calculateLinearTangents(keyframes);
- return this.curvePathWithTangents(updatedKeyframes, data, middle, type, coordinate, animationName);
- }
- curvePathWithTangents(
- keyframes: IAnimationKey[],
- data: string,
- middle: number,
- type: number,
- coordinate: number,
- animationName: string
- ) {
- keyframes.forEach((key, i) => {
- // Create a unique id for curve
- const curveId = this.encodeCurveId(animationName, i);
- // identify type of value and split...
- const keyframe_valueAsArray = this.getValueAsArray(type, key.value)[coordinate];
- let svgKeyframe;
- let outTangent;
- let inTangent;
- let defaultWeight = this.state.canvasWidthScale / 2; // Get weight depending on prev and next frame distance
- //distanceWithPreviousKeyframe = Vector2.Distance(updatedSvgKeyFrame.keyframePoint, previousKeyframe.keyframePoint) / 4;
- // For inTangent
- // has prev frame?
- let weightIn = 0;
- if (keyframes[i - 1] !== undefined) {
- let prevIn = new Vector2(keyframes[i - 1].frame, keyframes[i - 1].value);
- let currIn = new Vector2(key.frame, key.value);
- weightIn = (Vector2.Distance(prevIn, currIn) / 2) * this._pixelFrameUnit;
- }
- // For outTangent
- // has next frame?
- let weightOut = 0;
- if (keyframes[i + 1] !== undefined) {
- let prevOut = new Vector2(keyframes[i + 1].frame, keyframes[i + 1].value);
- let currOut = new Vector2(key.frame, key.value);
- weightOut = (Vector2.Distance(prevOut, currOut) / 2) * this._pixelFrameUnit;
- }
- if (weightIn !== 0 && weightOut !== 0) {
- if (weightIn < weightOut) {
- defaultWeight = weightIn > defaultWeight ? defaultWeight : weightIn;
- } else {
- defaultWeight = weightOut > defaultWeight ? defaultWeight : weightOut;
- }
- }
- if (weightIn === 0 && weightOut !== 0) {
- defaultWeight = weightOut > defaultWeight ? defaultWeight : weightOut;
- }
- if (weightIn !== 0 && weightOut === 0) {
- defaultWeight = weightIn > defaultWeight ? defaultWeight : weightIn;
- }
- // if curve doesnt have tangents then must be null to have linear
- // right now has 0 then the linear will show a slight curve as flat tangents...
- let defaultTangent: number | null = null;
- if (i !== 0 || i !== keyframes.length - 1) {
- defaultTangent = null;
- }
- // defaultTangent = 0; Zero or if linear get linear formula (slope of next, prev point)
- var inT =
- key.inTangent === null || key.inTangent === undefined
- ? defaultTangent
- : this.getValueAsArray(type, key.inTangent)[coordinate];
- var outT =
- key.outTangent === null || key.inTangent === undefined
- ? defaultTangent
- : this.getValueAsArray(type, key.outTangent)[coordinate];
- //let y = this._heightScale - keyframe_valueAsArray * middle; // should be half of heightscale
- defaultWeight = 1 * this._pixelFrameUnit;
- if (inT !== null) {
- let valueInY = inT + keyframe_valueAsArray;
- let valueIn = this._heightScale - valueInY * middle;
- inTangent = new Vector2(key.frame * this._pixelFrameUnit - defaultWeight, valueIn);
- } else {
- inTangent = null;
- }
- if (outT !== null) {
- let valueOutY = outT + keyframe_valueAsArray;
- let valueOut = this._heightScale - valueOutY * middle;
- outTangent = new Vector2(key.frame * this._pixelFrameUnit + defaultWeight, valueOut);
- } else {
- outTangent = null;
- }
- if (i === 0) {
- svgKeyframe = {
- keyframePoint: new Vector2(
- key.frame * this._pixelFrameUnit,
- this._heightScale - keyframe_valueAsArray * middle
- ),
- rightControlPoint: outTangent,
- leftControlPoint: null,
- id: curveId,
- selected: false,
- isLeftActive: false,
- isRightActive: false,
- };
- if (outTangent !== null) {
- data += ` C${outTangent.x} ${outTangent.y} `;
- } else {
- data += ` C${svgKeyframe.keyframePoint.x} ${svgKeyframe.keyframePoint.y} `;
- }
- } else {
- svgKeyframe = {
- keyframePoint: new Vector2(
- key.frame * this._pixelFrameUnit,
- this._heightScale - keyframe_valueAsArray * middle
- ),
- rightControlPoint: outTangent,
- leftControlPoint: inTangent,
- id: curveId,
- selected: false,
- isLeftActive: false,
- isRightActive: false,
- };
- if (outTangent !== null && inTangent !== null) {
- data += ` ${inTangent.x} ${inTangent.y} ${svgKeyframe.keyframePoint.x} ${svgKeyframe.keyframePoint.y} C${outTangent.x} ${outTangent.y}`;
- }
- if (outTangent === null && inTangent !== null) {
- data += ` ${inTangent.x} ${inTangent.y} ${svgKeyframe.keyframePoint.x} ${svgKeyframe.keyframePoint.y} C${svgKeyframe.keyframePoint.x} ${svgKeyframe.keyframePoint.y}`;
- }
- if (inTangent === null && outTangent !== null) {
- const prev = this._svgKeyframes[i - 1];
- data += ` ${prev.keyframePoint.x} ${prev.keyframePoint.y} ${svgKeyframe.keyframePoint.x} ${svgKeyframe.keyframePoint.y} C${outTangent.x} ${outTangent.y}`;
- }
- if (inTangent === null && outTangent === null) {
- const prev = this._svgKeyframes[i - 1];
- data += ` ${prev.keyframePoint.x} ${prev.keyframePoint.y} ${svgKeyframe.keyframePoint.x} ${svgKeyframe.keyframePoint.y} C${svgKeyframe.keyframePoint.x} ${svgKeyframe.keyframePoint.y}`;
- }
- }
- if (this.state) {
- let prev = this.state.svgKeyframes?.find((kf) => kf.id === curveId);
- if (prev) {
- svgKeyframe.isLeftActive = prev?.isLeftActive;
- svgKeyframe.isRightActive = prev?.isRightActive;
- svgKeyframe.selected = prev?.selected;
- }
- }
- this._svgKeyframes.push(svgKeyframe);
- }, this);
- const lastCurveEnd = data.lastIndexOf("C");
- const cleanedData = data.substring(0, lastCurveEnd);
- return cleanedData;
- }
- curvePath(keyframes: IAnimationKey[], data: string, middle: number, easingFunction: EasingFunction) {
- // This will get 1/4 and 3/4 of points in eased curve
- const u = 0.25;
- const v = 0.75;
- keyframes.forEach((key, i) => {
- // identify type of value and split...
- // Gets previous initial point of curve segment
- var pointA = new Vector2(0, 0);
- if (i === 0) {
- pointA.x = key.frame;
- pointA.y = this._heightScale - key.value * middle;
- this.setKeyframePoint([pointA], i, keyframes.length);
- } else {
- pointA.x = keyframes[i - 1].frame;
- pointA.y = this._heightScale - keyframes[i - 1].value * middle;
- // Gets the end point of this curve segment
- var pointB = new Vector2(key.frame, this._heightScale - key.value * middle);
- // Get easing value of percentage to get the bezier control points below
- let du = easingFunction.easeInCore(u); // What to do here, when user edits the curve? Option 1: Modify the curve with the new control points as BezierEaseCurve(x,y,z,w)
- let dv = easingFunction.easeInCore(v); // Option 2: Create a easeInCore function and adapt it with the new control points values... needs more revision.
- // Direction of curve up/down
- let yInt25 = 0;
- if (pointB.y > pointA.y) {
- // if pointB.y > pointA.y = goes down
- yInt25 = (pointB.y - pointA.y) * du + pointA.y;
- } else if (pointB.y < pointA.y) {
- // if pointB.y < pointA.y = goes up
- yInt25 = pointA.y - (pointA.y - pointB.y) * du;
- }
- let yInt75 = 0;
- if (pointB.y > pointA.y) {
- yInt75 = (pointB.y - pointA.y) * dv + pointA.y;
- } else if (pointB.y < pointA.y) {
- yInt75 = pointA.y - (pointA.y - pointB.y) * dv;
- }
- // Intermediate points in curve
- let intermediatePoint25 = new Vector2((pointB.x - pointA.x) * u + pointA.x, yInt25);
- let intermediatePoint75 = new Vector2((pointB.x - pointA.x) * v + pointA.x, yInt75);
- // Gets the four control points of bezier curve
- let controlPoints = this.interpolateControlPoints(
- pointA,
- intermediatePoint25,
- u,
- intermediatePoint75,
- v,
- pointB
- );
- if (controlPoints !== undefined) {
- this.setKeyframePoint(controlPoints, i, keyframes.length);
- data += ` C${controlPoints[1].x} ${controlPoints[1].y} ${controlPoints[2].x} ${controlPoints[2].y} ${controlPoints[3].x} ${controlPoints[3].y}`;
- }
- }
- });
- return data;
- }
- setKeyframePoint(controlPoints: Vector2[], index: number, keyframesCount: number) {
- let svgKeyframe;
- if (index === 0) {
- svgKeyframe = {
- keyframePoint: controlPoints[0],
- rightControlPoint: null,
- leftControlPoint: null,
- id: index.toString(),
- selected: false,
- isLeftActive: false,
- isRightActive: false,
- };
- } else {
- this._svgKeyframes[index - 1].rightControlPoint = controlPoints[1];
- svgKeyframe = {
- keyframePoint: controlPoints[3],
- rightControlPoint: null,
- leftControlPoint: controlPoints[2],
- id: index.toString(),
- selected: false,
- isLeftActive: false,
- isRightActive: false,
- };
- }
- this._svgKeyframes.push(svgKeyframe);
- }
- interpolateControlPoints(
- p0: Vector2,
- p1: Vector2,
- u: number,
- p2: Vector2,
- v: number,
- p3: Vector2
- ): Vector2[] | undefined {
- let a = 0.0;
- let b = 0.0;
- let c = 0.0;
- let d = 0.0;
- let det = 0.0;
- let q1: Vector2 = new Vector2();
- let q2: Vector2 = new Vector2();
- let controlA: Vector2 = p0;
- let controlB: Vector2 = new Vector2();
- let controlC: Vector2 = new Vector2();
- let controlD: Vector2 = p3;
- if (u <= 0.0 || u >= 1.0 || v <= 0.0 || v >= 1.0 || u >= v) {
- return undefined;
- }
- a = 3 * (1 - u) * (1 - u) * u;
- b = 3 * (1 - u) * u * u;
- c = 3 * (1 - v) * (1 - v) * v;
- d = 3 * (1 - v) * v * v;
- det = a * d - b * c;
- if (det == 0.0) return undefined;
- q1.x = p1.x - ((1 - u) * (1 - u) * (1 - u) * p0.x + u * u * u * p3.x);
- q1.y = p1.y - ((1 - u) * (1 - u) * (1 - u) * p0.y + u * u * u * p3.y);
- q2.x = p2.x - ((1 - v) * (1 - v) * (1 - v) * p0.x + v * v * v * p3.x);
- q2.y = p2.y - ((1 - v) * (1 - v) * (1 - v) * p0.y + v * v * v * p3.y);
- controlB.x = (d * q1.x - b * q2.x) / det;
- controlB.y = (d * q1.y - b * q2.y) / det;
- controlC.x = (-c * q1.x + a * q2.x) / det;
- controlC.y = (-c * q1.y + a * q2.y) / det;
- return [controlA, controlB, controlC, controlD];
- }
- deselectAnimation = () => {
- const animations = (this.props.entity as IAnimatable).animations;
- if (animations && animations.length === 0) {
- setTimeout(() => this.cleanCanvas(), 0);
- }
- this.cleanCanvas();
- };
- cleanCanvas = () => {
- this.setState({
- selected: null,
- svgKeyframes: [],
- selectedPathData: undefined,
- selectedCoordinate: 0,
- });
- };
- /**
- * Core functions
- * This section handles main Curve Editor Functions.
- */
- selectAnimation = (animation: Animation, coordinate?: SelectedCoordinate) => {
- this._svgKeyframes = [];
- let updatedPath;
- let filteredSvgKeys;
- let selectedCurve = 0;
- this.stopAnimation();
- if (coordinate === undefined) {
- updatedPath = this.getPathData(animation);
- } else {
- let curves = this.getPathData(animation);
- updatedPath = [];
- filteredSvgKeys = this._svgKeyframes?.filter((curve) => {
- let id = parseInt(curve.id.split("_")[2]);
- if (id === coordinate) {
- return true;
- } else {
- return false;
- }
- });
- curves?.map((curve) => {
- let id = parseInt(curve.id.split("_")[2]);
- if (id === coordinate) {
- updatedPath.push(curve);
- }
- });
- selectedCurve = coordinate;
- }
- // check for empty svgKeyframes, lastframe, selected
- this.setState(
- {
- selected: animation,
- svgKeyframes: coordinate !== undefined ? filteredSvgKeys : this._svgKeyframes,
- selectedPathData: updatedPath,
- selectedCoordinate: selectedCurve,
- fps: animation.framePerSecond,
- },
- this.postSelectionEvents
- );
- };
- postSelectionEvents = () => {
- if (this.state.lastKeyframeCreated !== null) {
- this.deselectKeyframes();
- this.selectKeyframe(this.state.lastKeyframeCreated, false);
- this.setState({ lastKeyframeCreated: null });
- }
- this.setMainAnimatable();
- if (this.state.selected) {
- const lastKeyframe = this.state.selected.getHighestFrame();
- const currentLimit = this.state.animationLimit;
- if (currentLimit < lastKeyframe) {
- this.changeAnimationLimit(lastKeyframe);
- }
- }
- };
- setMainAnimatable() {
- if (this.state.selected !== null) {
- let target = this.props.entity;
- if (this.props.entity instanceof TargetedAnimation) {
- target = this.props.entity.target;
- }
- this.props.scene.stopAllAnimations();
- if (this._mainAnimatable?.target !== target) {
- const keys = this.state.selected.getKeys();
- if (keys.length !== 0) {
- const firstFrame = keys[0].frame;
- const LastFrame = this.state.selected.getHighestFrame();
- this._mainAnimatable = this.props.scene.beginAnimation(
- target,
- firstFrame,
- LastFrame,
- this.state.isLooping
- );
- this._mainAnimatable.stop();
- }
- }
- }
- }
- isAnimationPlaying() {
- let target = this.props.entity;
- if (this.props.entity instanceof TargetedAnimation) {
- target = this.props.entity.target;
- }
- return this.props.scene.getAllAnimatablesByTarget(target).length > 0;
- }
- stopAnimation() {
- let target = this.props.entity;
- if (this.props.entity instanceof TargetedAnimation) {
- target = this.props.entity.target;
- }
- this._isPlaying = this.props.scene.getAllAnimatablesByTarget(target).length > 0;
- if (this._isPlaying) {
- this.props.playOrPause && this.props.playOrPause();
- if (this.state !== undefined) {
- this.setState({ isPlaying: false });
- }
- this._isPlaying = false;
- }
- }
- setIsLooping = () => {
- this.setState({ isLooping: !this.state.isLooping, isPlaying: false }, () => this.stopAnimation());
- };
- setFramesPerSecond = (fps: number) => {
- this.setState({ fps: fps, isPlaying: false }, () => this.stopAnimation());
- };
- analizeAnimationForLerp(animation: Animation | null) {
- if (animation !== null) {
- const { easingMode, easingType, usesTangents } = this.getAnimationData(animation);
- if (easingType === undefined && easingMode === undefined && !usesTangents) {
- return true;
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- /**
- * Timeline
- * This section controls the timeline.
- */
- changeCurrentFrame = (frame: number) => {
- this.stopAnimation();
- const animation = this.state.selected;
- if (animation) {
- const hasKeyframe = animation.getKeys().find((x) => x.frame === frame);
- const currentValue = this.calculateCurrentPointInCurve(frame);
- const value = hasKeyframe
- ? this.getValueAsArray(animation.dataType, hasKeyframe.value)[this.state.selectedCoordinate]
- : currentValue ?? 0;
- const keyframe: IAnimationKey = { frame, value };
- this.setState(
- {
- currentFrame: frame,
- isPlaying: false,
- },
- () => {
- const index = animation.getKeys().findIndex((x) => x.frame === keyframe.frame);
- const animationName = `${animation.name}_${animation.targetProperty}_${this.state.selectedCoordinate}`;
- const id = this.encodeCurveId(animationName, index);
- this.selectKeyframeFromId(id, keyframe);
- this.setCanvasPosition(keyframe);
- }
- );
- }
- };
- calculateCurrentPointInCurve = (frame: number): number | undefined => {
- if (
- this.state.selectedPathData !== undefined &&
- this.state.selectedPathData[this.state.selectedCoordinate] !== undefined
- ) {
- const selectedCurve = this.state.selectedPathData[this.state.selectedCoordinate].domCurve.current;
- if (selectedCurve !== null) {
- const curveLength = selectedCurve.getTotalLength();
- const frameValue = (frame * curveLength) / 100;
- const currentPointInCurve = selectedCurve.getPointAtLength(frameValue);
- const middle = this._heightScale / 2;
- const offset =
- (currentPointInCurve?.y * this._heightScale - this._heightScale ** 2 / 2) /
- middle /
- this._heightScale;
- const unit = Math.sign(offset);
- const currentValue = unit === -1 ? Math.abs(offset + unit) : unit - offset;
- this.setState({
- currentValue: currentValue,
- currentPoint: currentPointInCurve,
- });
- return currentValue;
- }
- }
- return undefined;
- };
- setCanvasPosition = (keyframe: IAnimationKey) => {
- if (this.state.selected) {
- const positionX = (keyframe.frame - this._pixelFrameUnit) * this._pixelFrameUnit;
- let value = 0;
- if (keyframe.value === null) {
- value = this.state.panningY;
- } else {
- value = this.getValueAsArray(this.state.selected.dataType, keyframe.value)[
- this.state.selectedCoordinate
- ];
- }
- const valueScale = this._heightScale / this._scaleFactor;
- const middleCanvas = this._heightScale / 2;
- const positionY = value === 0 ? middleCanvas : middleCanvas - value * valueScale;
- // change initialframe, last frame
- const currentFramesInCanvas = this.state.framesInCanvasView.to - this.state.framesInCanvasView.from;
- const newStartFrameInCanvas = Math.round(positionX / this._pixelFrameUnit);
- this.setState({
- panningX: positionX,
- panningY: positionY,
- repositionCanvas: true,
- framesInCanvasView: { from: newStartFrameInCanvas, to: newStartFrameInCanvas + currentFramesInCanvas },
- });
- }
- };
- setCurrentFrame = (frame: number) => {
- this.setState({
- currentFrame: frame,
- });
- };
- changeAnimationLimit = (limit: number) => {
- this.stopAnimation();
- const doubleLimit = limit * 2;
- this.setState({
- animationLimit: limit,
- canvasLength: doubleLimit,
- frameAxisLength: this.setFrameAxis(doubleLimit),
- });
- };
- updateFrameInKeyFrame = (frame: number, index: number) => {
- if (this.state && this.state.selected) {
- let animation = this.state.selected;
- let keys = [...animation.getKeys()];
- keys[index].frame = frame;
- animation.setKeys(keys);
- this.selectAnimation(animation);
- }
- };
- playPause = (direction: number) => {
- this.registerObs();
- if (this.state.selected) {
- let target = this.props.entity;
- if (this.props.entity instanceof TargetedAnimation) {
- target = this.props.entity.target;
- }
- if (this.state.isPlaying && direction === 0) {
- this.props.scene.stopAnimation(target);
- this.setState({ isPlaying: false });
- this._isPlaying = false;
- this.forceUpdate();
- } else {
- if (this.state.isPlaying) {
- this.props.scene.stopAnimation(target);
- }
- this.props.scene.stopAllAnimations();
- let keys = this.state.selected.getKeys();
- if (keys.length !== 0) {
- let firstFrame = keys[0].frame;
- let LastFrame = this.state.selected.getHighestFrame();
- if (direction === 1) {
- this._mainAnimatable = this.props.scene.beginAnimation(
- target,
- firstFrame,
- LastFrame,
- this.state.isLooping
- );
- }
- if (direction === -1) {
- this._mainAnimatable = this.props.scene.beginAnimation(
- target,
- LastFrame,
- firstFrame,
- this.state.isLooping
- );
- }
- if (!this.state.isLooping && this._mainAnimatable) {
- this._mainAnimatable.onAnimationEnd = () => this.playPause(0);
- }
- }
- const zeroFrames = keys.filter((x) => x.frame === 0);
- if (zeroFrames.length > 1) {
- keys.shift();
- }
- keys.sort((a, b) => a.frame - b.frame);
- this._isPlaying = true;
- this.setState({ isPlaying: true });
- this.forceUpdate();
- }
- }
- };
- moveFrameTo(e: React.MouseEvent<SVGRectElement, MouseEvent>) {
- this.stopAnimation();
- var svg = e.currentTarget as SVGRectElement;
- var CTM = svg.getScreenCTM();
- let position;
- if (CTM) {
- position = new Vector2((e.clientX - CTM.e) / CTM.a, (e.clientY - CTM.f) / CTM.d);
- let selectedFrame = Math.round(position.x / this._pixelFrameUnit);
- this.setState({ currentFrame: selectedFrame, isPlaying: false }, () => {
- if (this.state.selected) {
- const index = this.state.selected.getKeys().findIndex((x) => x.frame === selectedFrame);
- const keyframe = this.state.selected.getKeys().find((x) => x.frame === selectedFrame);
- if (index !== undefined && keyframe !== undefined) {
- const animationName = `${this.state.selected.name}_${this.state.selected.targetProperty}_${this.state.selectedCoordinate}`;
- const id = this.encodeCurveId(animationName, index);
- this.selectKeyframeFromId(id, keyframe);
- this.setCanvasPosition(keyframe);
- }
- }
- });
- }
- }
- registerObs() {
- if (this._onBeforeRenderObserver) {
- this.props.scene.onBeforeRenderObservable.remove(this._onBeforeRenderObserver);
- this._onBeforeRenderObserver = null;
- }
- this._onBeforeRenderObserver = this.props.scene.onBeforeRenderObservable.add(() => {
- if (!this._isPlaying || !this._mainAnimatable) {
- return;
- }
- this.setState({
- currentFrame: Math.round(this._mainAnimatable.masterFrame),
- });
- });
- }
- componentWillUnmount() {
- this.playPause(0);
- if (this._onBeforeRenderObserver) {
- this.props.scene.onBeforeRenderObservable.remove(this._onBeforeRenderObserver);
- this._onBeforeRenderObserver = null;
- }
- }
- isCurrentFrame(frame: number) {
- return this.state.currentFrame === frame;
- }
- setPanningY = (panningY: number) => {
- this.setState({ panningY });
- };
- setPanningX = (panningX: number) => {
- const currentFramesInCanvas = this.state.framesInCanvasView.to - this.state.framesInCanvasView.from;
- const newStartFrameInCanvas = Math.round(panningX / this._pixelFrameUnit);
- this.setState({
- panningX,
- framesInCanvasView: { from: newStartFrameInCanvas, to: newStartFrameInCanvas + currentFramesInCanvas },
- });
- };
- canvasPositionEnded = () => {
- this.setState({ repositionCanvas: false });
- };
- setNotificationMessage = (message: string) => {
- this.setState({ notification: message });
- };
- frameSelectedKeyframes = () => {
- const animation = this.state.selected;
- const coordinate = this.state.selectedCoordinate;
- if (animation) {
- let highest, lowest, middleFrame, firstFrame, lastFrame;
- const keysCopy = [...animation.getKeys()];
- // calculate scale factor for Value Axis //
- const selectedKeyframes = this.state.svgKeyframes?.filter((x) => x.selected);
- if (selectedKeyframes?.length === 0) {
- firstFrame = keysCopy[0].frame;
- lastFrame = keysCopy[keysCopy.length - 1].frame;
- // If not selected get all keyframes
- keysCopy.sort(
- (a, b) =>
- this.getValueAsArray(animation.dataType, a.value)[coordinate] -
- this.getValueAsArray(animation.dataType, b.value)[coordinate]
- );
- lowest = keysCopy[0];
- highest = keysCopy[keysCopy.length - 1];
- keysCopy.sort((a, b) => a.frame - b.frame);
- middleFrame =
- Math.round((keysCopy[keysCopy.length - 1].frame - keysCopy[0].frame) / 2) + keysCopy[0].frame;
- } else {
- // If selected get keys
- const keysInRange = keysCopy.filter((kf, i) => {
- return selectedKeyframes?.find((a: IKeyframeSvgPoint) => {
- const { order } = this.decodeCurveId(a.id);
- return i === order ? kf : undefined;
- });
- });
- // Sort to get first and last frame
- keysInRange.sort((a, b) => a.frame - b.frame);
- // Get previous and next non selected keyframe in range
- const prevKey = keysCopy[keysCopy.indexOf(keysInRange[0]) - 1];
- const nextKey = keysCopy[keysCopy.indexOf(keysInRange[keysInRange.length - 1]) + 1];
- firstFrame = prevKey.frame;
- lastFrame = nextKey.frame;
- // Insert keys in range
- if (prevKey) {
- keysInRange.push(prevKey);
- }
- if (nextKey) {
- keysInRange.push(nextKey);
- }
- // Sort to get lowest and highest values for scale
- keysInRange.sort(
- (a, b) =>
- this.getValueAsArray(animation.dataType, a.value)[coordinate] -
- this.getValueAsArray(animation.dataType, b.value)[coordinate]
- );
- lowest = keysInRange[0];
- highest = keysInRange[keysInRange.length - 1];
- keysInRange.sort((a, b) => a.frame - b.frame);
- middleFrame =
- Math.round((keysInRange[keysInRange.length - 1].frame - keysInRange[0].frame) / 2) +
- keysInRange[0].frame;
- }
- // calculate scale...
- const scale =
- this.getValueAsArray(animation.dataType, highest?.value)[coordinate] -
- this.getValueAsArray(animation.dataType, lowest?.value)[coordinate];
- // Scale Frames to fit width of canvas
- // reposition canvas to middle value of scale
- const canvasMargin = 1.5;
- this._scaleFactor = isNaN(scale) || scale === 0 ? 2 : scale * canvasMargin;
- // Set a new scale factor but for Frames
- // ****
- // get client width of canvas
- // how many frames are in canvas ... get the pixelFrameUnit?
- // get frames needed (last frame - first frame)
- let currentSpace = 780;
- const frameUnit = 39;
- if (this._graphCanvas.current) {
- currentSpace = this._graphCanvas.current?.clientWidth;
- }
- const availableSpaceForFrames = currentSpace / frameUnit;
- // with client width divide the number of frames needed
- const frameDistance = lastFrame - firstFrame;
- this._pixelFrameUnit = availableSpaceForFrames / (frameDistance / 10); // Update scale here...
- const canvasValue = isNaN(scale) || scale === 0 ? 1 : scale / 2 + lowest?.value;
- const firstF = firstFrame;
- const centerFrame = middleFrame;
- this.setState(
- {
- framesInCanvasView: { from: firstFrame, to: lastFrame },
- },
- () => {
- // Need to center and reposition canvas
- this.setCanvasPosition({ frame: firstF, value: canvasValue });
- console.log(`Should center canvas at: ${centerFrame}, ${canvasValue}`);
- // Render new points
- this.selectAnimation(animation, coordinate);
- }
- );
- }
- };
- onWindowResizeWidth = () => {
- if (this._graphCanvas.current) {
- const defaultWidth = 781;
- const defaultSvgProportion = 1.8;
- const proportionResized = (defaultSvgProportion / 2) * 10;
- const svgWidth = 200;
- const width = (this._graphCanvas.current.clientWidth / svgWidth) * defaultSvgProportion;
- const percentResize = (this._graphCanvas.current.clientWidth * 100) / defaultWidth;
- const value = (percentResize - 100) * -1;
- this.setState({ valuesPositionResize: value - width + proportionResized });
- this.onTimelineResize();
- }
- };
- onTimelineResize = () => {
- if (this._editor.current) {
- const scrollHandle = this._editor.current.getElementsByClassName("scroll-handle")[0].clientWidth;
- this._resizedTimeline = scrollHandle;
- }
- };
- render() {
- return (
- <div ref={this._editor} id="animation-curve-editor">
- <Notification
- message={this.state.notification}
- open={this.state.notification !== "" ? true : false}
- close={this.clearNotification}
- />
- <GraphActionsBar
- setKeyframeValue={this.setKeyframeValueFromInput}
- enabled={this.state.selected === null || this.state.selected === undefined ? false : true}
- title={this._entityName}
- actionableKeyframe={this.state.actionableKeyframe}
- handleFrameChange={this.handleFrameChange}
- handleValueChange={this.handleValueChange}
- addKeyframe={this.addKeyframeClick}
- removeKeyframe={this.removeKeyframeClick}
- frameSelectedKeyframes={this.frameSelectedKeyframes}
- brokenMode={this.state.isBrokenMode}
- brokeTangents={this.setBrokenMode}
- lerpMode={this.state.lerpMode}
- setLerpToActiveControlPoint={this.setLerpToActiveControlPoint}
- flatTangent={this.setFlatTangent}
- frameRange={{ max: this.state.maxFrame, min: this.state.minFrame }}
- />
- <div className="content">
- <div className="row">
- <EditorControls
- deselectAnimation={this.deselectAnimation}
- selectAnimation={this.selectAnimation}
- isTargetedAnimation={this._isTargetedAnimation}
- entity={this.props.entity}
- selected={this.state.selected}
- lockObject={this.props.lockObject}
- setNotificationMessage={this.setNotificationMessage}
- globalState={this.props.globalState}
- snippetServer={this._snippetUrl}
- fps={this.state.fps}
- setFps={this.setFramesPerSecond}
- setIsLooping={this.setIsLooping}
- />
- <div ref={this._graphCanvas} className="graph-chart" onWheel={this.zoom}>
- {this.state.svgKeyframes && (
- <SvgDraggableArea
- ref={this._svgCanvas}
- viewBoxScale={this.state.frameAxisLength.length}
- scale={this.state.scale}
- removeSelectedKeyframes={this.removeKeyframes}
- deselectKeyframes={this.deselectKeyframes}
- updatePosition={this.renderPoints}
- panningY={this.setPanningY}
- panningX={this.setPanningX}
- setCurrentFrame={this.setCurrentFrame}
- positionCanvas={new Vector2(this.state.panningX, this.state.panningY)}
- repositionCanvas={this.state.repositionCanvas}
- canvasPositionEnded={this.canvasPositionEnded}
- keyframeSvgPoints={this.state.svgKeyframes}
- resetActionableKeyframe={this.resetActionableKeyframe}
- framesInCanvasView={this.state.framesInCanvasView}
- >
- {this.setValueLines().map((line, i) => {
- return (
- <text
- key={`value_inline_${i}`}
- x={this.state.panningX - 5}
- y={line.value}
- dx={this.state.valuesPositionResize}
- textAnchor="middle"
- dy="-1"
- style={{
- fontSize: `${0.18 * this.state.scale}em`,
- fontWeight: "bold",
- textAlign: "center",
- }}
- >
- {line.label}
- </text>
- );
- })}
- {this.setValueLines().map((line, i) => {
- return (
- <line
- key={i}
- x1={-((this.state.frameAxisLength.length * 10) / 2)}
- y1={line.value}
- x2={this.state.frameAxisLength.length * 10}
- y2={line.value}
- ></line>
- );
- })}
- {/* Multiple Curves */}
- {this.state.selectedPathData?.map((curve, i) => (
- <path
- key={i}
- ref={curve.domCurve}
- pathLength={curve.pathLength}
- id="curve"
- d={curve.pathData}
- style={{
- stroke: curve.color,
- fill: "none",
- strokeWidth: "0.5",
- }}
- ></path>
- ))}
- {this.state.svgKeyframes.map((keyframe, i) => (
- <KeyframeSvgPoint
- key={`${keyframe.id}_${i}`}
- id={keyframe.id}
- keyframePoint={keyframe.keyframePoint}
- leftControlPoint={keyframe.leftControlPoint}
- rightControlPoint={keyframe.rightControlPoint}
- isLeftActive={keyframe.isLeftActive}
- isRightActive={keyframe.isRightActive}
- selected={keyframe.selected}
- selectedControlPoint={this.selectedControlPoint}
- selectKeyframe={this.selectKeyframe}
- framesInCanvasView={this.state.framesInCanvasView}
- />
- ))}
- <rect
- onClick={(e) => this.moveFrameTo(e)}
- x={-((this.state.frameAxisLength.length * 10) / 2)}
- y={91 + this.state.panningY + "%"}
- width={this.state.frameAxisLength.length * 10}
- height="9%"
- fill="#222"
- style={{ cursor: "pointer" }}
- ></rect>
- {this.state.frameAxisLength.map((f, i) => (
- <svg key={i} x="0" y={96 + this.state.panningY + "%"} className="frame-contain">
- <text
- x={f.value}
- y="1px"
- dx="2px"
- style={{ fontSize: `${0.2 * this.state.scale}em` }}
- >
- {Math.round((f.label * 10) / this._pixelFrameUnit)}
- </text>
- <line x1={f.value} y1="0" x2={f.value} y2="5%"></line>
- {f.value % this.state.fps === 0 && f.value !== 0 ? (
- <line
- x1={f.value * this._pixelFrameUnit}
- y1="-100%"
- x2={f.value * this._pixelFrameUnit}
- y2="5%"
- ></line>
- ) : null}
- </svg>
- ))}
- {this.state.selected && this.state.currentFrame ? (
- <svg x="0" y={96 + this.state.panningY + "%"}>
- <line
- x1={this.state.currentFrame * this._pixelFrameUnit}
- y1="0"
- x2={this.state.currentFrame * this._pixelFrameUnit}
- y2="-100%"
- style={{
- stroke: "#a4a4a4",
- strokeWidth: 0.4,
- }}
- />
- <svg x={this.state.currentFrame * this._pixelFrameUnit} y="-1">
- <circle className="svg-playhead" cx="0" cy="0" r="2%" fill="white" />
- <text
- x="0"
- y="1%"
- textAnchor="middle"
- style={{
- fontSize: `${0.17 * this.state.scale}em`,
- pointerEvents: "none",
- fontWeight: 600,
- }}
- >
- {this.state.currentFrame}
- </text>
- </svg>
- </svg>
- ) : null}
- </SvgDraggableArea>
- )}
- <div
- style={{
- width: 30,
- height: "inherit",
- position: "absolute",
- zIndex: 1,
- pointerEvents: "none",
- backgroundColor: "rgb(188 188 188 / 11%)",
- top: 40,
- }}
- ></div>
- <ScaleLabel current={this.state.valueScaleType} />
- </div>
- </div>
- <div className="row-bottom">
- <Timeline
- currentFrame={this.state.currentFrame}
- playPause={this.playPause}
- isPlaying={this.state.isPlaying}
- dragKeyframe={this.updateFrameInKeyFrame}
- onCurrentFrameChange={this.changeCurrentFrame}
- onAnimationLimitChange={this.changeAnimationLimit}
- animationLimit={this.state.animationLimit}
- keyframes={this.state.selected && this.state.selected.getKeys()}
- selected={this.state.selected && this.state.selected.getKeys()[0]}
- fps={this.state.fps}
- repositionCanvas={this.setCanvasPosition}
- resizeWindowProportion={this._resizedTimeline}
- ></Timeline>
- </div>
- </div>
- </div>
- );
- }
- }
|