animationCurveEditorComponent.tsx 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. import * as React from "react";
  2. import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
  3. import { faTimes } from "@fortawesome/free-solid-svg-icons";
  4. import { Animation } from 'babylonjs/Animations/animation';
  5. import { Vector2, Vector3, Quaternion } from 'babylonjs/Maths/math.vector';
  6. import { Size } from 'babylonjs/Maths/math.size';
  7. import { Color3, Color4 } from 'babylonjs/Maths/math.color';
  8. import { EasingFunction } from 'babylonjs/Animations/easing';
  9. import { IAnimationKey } from 'babylonjs/Animations/animationKey';
  10. import { IKeyframeSvgPoint } from './keyframeSvgPoint';
  11. import { SvgDraggableArea } from './svgDraggableArea';
  12. import { Timeline } from './timeline';
  13. import { Playhead } from './playhead';
  14. import { Notification } from './notification';
  15. import { GraphActionsBar } from './graphActionsBar';
  16. import { Scene } from "babylonjs/scene";
  17. import { ButtonLineComponent } from '../../../lines/buttonLineComponent';
  18. import { IAnimatable } from 'babylonjs/Animations/animatable.interface';
  19. import { TargetedAnimation } from "babylonjs/Animations/animationGroup";
  20. import { Nullable } from 'babylonjs/types';
  21. require("./curveEditor.scss");
  22. interface IAnimationCurveEditorComponentProps {
  23. close: (event: any) => void;
  24. playOrPause?: () => void;
  25. title: string;
  26. scene: Scene;
  27. entity: IAnimatable | TargetedAnimation;
  28. }
  29. interface ICanvasAxis {
  30. value: number;
  31. label: number;
  32. }
  33. export class AnimationCurveEditorComponent extends React.Component<IAnimationCurveEditorComponentProps, {
  34. animationName: string,
  35. animationType: string,
  36. animationTargetProperty: string,
  37. isOpen: boolean,
  38. selected: Animation | null,
  39. currentPathData: string | undefined,
  40. svgKeyframes: IKeyframeSvgPoint[] | undefined,
  41. currentFrame: number,
  42. currentValue: number,
  43. frameAxisLength: ICanvasAxis[],
  44. valueAxisLength: ICanvasAxis[],
  45. isFlatTangentMode: boolean,
  46. isTangentMode: boolean,
  47. isBrokenMode: boolean,
  48. lerpMode: boolean,
  49. scale: number,
  50. playheadOffset: number,
  51. notification: string,
  52. currentPoint: SVGPoint | undefined,
  53. lastFrame: number,
  54. playheadPos: number
  55. }> {
  56. // Height scale *Review this functionaliy
  57. private _heightScale: number = 100;
  58. // Canvas Length *Review this functionality
  59. readonly _entityName: string;
  60. readonly _canvasLength: number = 20;
  61. private _svgKeyframes: IKeyframeSvgPoint[] = [];
  62. private _frames: Vector2[] = [];
  63. private _isPlaying: boolean = false;
  64. private _graphCanvas: React.RefObject<HTMLDivElement>;
  65. private _selectedCurve: React.RefObject<SVGPathElement>;
  66. private _svgCanvas: React.RefObject<SvgDraggableArea>;
  67. private _isTargetedAnimation: boolean;
  68. constructor(props: IAnimationCurveEditorComponentProps) {
  69. super(props);
  70. this._entityName = (this.props.entity as any).id;
  71. // Review is we really need this refs
  72. this._graphCanvas = React.createRef();
  73. this._selectedCurve = React.createRef();
  74. this._svgCanvas = React.createRef();
  75. console.log(this.props.entity instanceof TargetedAnimation)
  76. let initialSelection;
  77. let initialPathData;
  78. let initialLerpMode;
  79. if (this.props.entity instanceof TargetedAnimation) {
  80. this._isTargetedAnimation = true;
  81. initialSelection = this.props.entity.animation;
  82. initialLerpMode = this.analizeAnimation(this.props.entity.animation);
  83. initialPathData = this.getPathData(this.props.entity.animation);
  84. } else {
  85. this._isTargetedAnimation = false;
  86. initialLerpMode = this.analizeAnimation(this.props.entity.animations && this.props.entity.animations[0]);
  87. initialSelection = this.props.entity.animations !== null ? this.props.entity.animations[0] : null;
  88. initialPathData = this.props.entity.animations !== null ? this.getPathData(this.props.entity.animations[0]) : "";
  89. }
  90. // will update this until we have a top scroll/zoom feature
  91. let valueInd = [2, 1.8, 1.6, 1.4, 1.2, 1, 0.8, 0.6, 0.4, 0.2, 0];
  92. this.state = {
  93. selected: initialSelection,
  94. isOpen: true,
  95. currentPathData: initialPathData,
  96. svgKeyframes: this._svgKeyframes,
  97. animationTargetProperty: 'position.x',
  98. animationName: "",
  99. animationType: "Float",
  100. currentFrame: 0,
  101. currentValue: 1,
  102. isFlatTangentMode: false,
  103. isTangentMode: false,
  104. isBrokenMode: false,
  105. lerpMode: initialLerpMode,
  106. playheadOffset: this._graphCanvas.current ? (this._graphCanvas.current.children[1].clientWidth) / (this._canvasLength * 10) : 0,
  107. frameAxisLength: (new Array(this._canvasLength)).fill(0).map((s, i) => { return { value: i * 10, label: i * 10 } }),
  108. valueAxisLength: (new Array(10)).fill(0).map((s, i) => { return { value: i * 10, label: valueInd[i] } }),
  109. notification: "",
  110. lastFrame: 0,
  111. currentPoint: undefined,
  112. scale: 1,
  113. playheadPos: 0,
  114. }
  115. }
  116. componentDidMount() {
  117. setTimeout(() => this.resetPlayheadOffset(), 500);
  118. }
  119. /**
  120. * Notifications
  121. * To add notification we set the state and clear to make the notification bar hide.
  122. */
  123. clearNotification() {
  124. this.setState({ notification: "" });
  125. }
  126. /**
  127. * Zoom and Scroll
  128. * This section handles zoom and scroll
  129. * of the graph area.
  130. */
  131. zoom(e: React.WheelEvent<HTMLDivElement>) {
  132. e.nativeEvent.stopImmediatePropagation();
  133. console.log(e.deltaY);
  134. let scaleX = 1;
  135. if (Math.sign(e.deltaY) === -1) {
  136. scaleX = (this.state.scale - 0.01);
  137. } else {
  138. scaleX = (this.state.scale + 0.01);
  139. }
  140. this.setState({ scale: scaleX }, this.setAxesLength);
  141. }
  142. setAxesLength() {
  143. // Check why we get undefined, or NaN in length?
  144. let length = Math.round(this._canvasLength * this.state.scale);
  145. let highestFrame = 100;
  146. if (this.state.selected !== null) {
  147. highestFrame = this.state.selected.getHighestFrame();
  148. }
  149. if (length < (highestFrame * 2) / 10) {
  150. length = (highestFrame * 2) / 10
  151. }
  152. let valueLines = Math.round((this.state.scale * this._heightScale) / 10);
  153. let newFrameLength = (new Array(length)).fill(0).map((s, i) => { return { value: i * 10, label: i * 10 } });
  154. let newValueLength = (new Array(valueLines)).fill(0).map((s, i) => { return { value: i * 10, label: this.getValueLabel(i * 10) } });
  155. this.setState({ frameAxisLength: newFrameLength, valueAxisLength: newValueLength });
  156. this.resetPlayheadOffset();
  157. }
  158. getValueLabel(i: number) {
  159. // Need to update this when Y axis grows
  160. let label = 0;
  161. if (i === 0) {
  162. label = 2;
  163. }
  164. if (i === 50) {
  165. label = 1;
  166. } else {
  167. label = ((100 - (i * 2)) * 0.01) + 1;
  168. }
  169. return label;
  170. }
  171. resetPlayheadOffset() {
  172. if (this._graphCanvas && this._graphCanvas.current) {
  173. this.setState({ playheadOffset: (this._graphCanvas.current.children[1].clientWidth) / (this._canvasLength * 10 * this.state.scale) });
  174. }
  175. }
  176. /**
  177. * Add New Animation
  178. * This section handles events from AnimationCreation.
  179. */
  180. handleNameChange(event: React.ChangeEvent<HTMLInputElement>) {
  181. event.preventDefault();
  182. this.setState({ animationName: event.target.value.trim() });
  183. }
  184. handleTypeChange(event: React.ChangeEvent<HTMLSelectElement>) {
  185. event.preventDefault();
  186. this.setState({ animationType: event.target.value });
  187. }
  188. handlePropertyChange(event: React.ChangeEvent<HTMLInputElement>) {
  189. event.preventDefault();
  190. this.setState({ animationTargetProperty: event.target.value });
  191. }
  192. setListItem(animation: Animation, i: number) {
  193. let element;
  194. switch (animation.dataType) {
  195. case Animation.ANIMATIONTYPE_FLOAT:
  196. element = <li className={this.state.selected && this.state.selected.name === animation.name ? 'active' : ''} key={i} onClick={() => this.selectAnimation(animation)}>
  197. <p>{animation.name}&nbsp;
  198. <span>{animation.targetProperty}</span></p>
  199. {!(this.props.entity instanceof TargetedAnimation) ? this.state.selected && this.state.selected.name === animation.name ? <ButtonLineComponent label={"Remove"} onClick={() => this.deleteAnimation()} /> : null : null}
  200. </li>
  201. break;
  202. case Animation.ANIMATIONTYPE_VECTOR2:
  203. element = <li className="property" key={i}><p>{animation.targetProperty}</p>
  204. <ul>
  205. <li key={`${i}_x`}>Property <strong>X</strong></li>
  206. <li key={`${i}_y`}>Property <strong>Y</strong></li>
  207. </ul>
  208. </li>
  209. break;
  210. case Animation.ANIMATIONTYPE_VECTOR3:
  211. element = <li className="property" key={i}><p>{animation.targetProperty}</p>
  212. <ul>
  213. <li key={`${i}_x`}>Property <strong>X</strong></li>
  214. <li key={`${i}_y`}>Property <strong>Y</strong></li>
  215. <li key={`${i}_z`}>Property <strong>Z</strong></li>
  216. </ul>
  217. </li>
  218. break;
  219. case Animation.ANIMATIONTYPE_QUATERNION:
  220. element = <li className="property" key={i}><p>{animation.targetProperty}</p>
  221. <ul>
  222. <li key={`${i}_x`}>Property <strong>X</strong></li>
  223. <li key={`${i}_y`}>Property <strong>Y</strong></li>
  224. <li key={`${i}_z`}>Property <strong>Z</strong></li>
  225. <li key={`${i}_w`}>Property <strong>W</strong></li>
  226. </ul>
  227. </li>
  228. break;
  229. case Animation.ANIMATIONTYPE_COLOR3:
  230. element = <li className="property" key={i}><p>{animation.targetProperty}</p>
  231. <ul>
  232. <li key={`${i}_r`}>Property <strong>R</strong></li>
  233. <li key={`${i}_g`}>Property <strong>G</strong></li>
  234. <li key={`${i}_b`}>Property <strong>B</strong></li>
  235. </ul>
  236. </li>
  237. break;
  238. case Animation.ANIMATIONTYPE_COLOR4:
  239. element = <li className="property" key={i}><p>{animation.targetProperty}</p>
  240. <ul>
  241. <li key={`${i}_r`}>Property <strong>R</strong></li>
  242. <li key={`${i}_g`}>Property <strong>G</strong></li>
  243. <li key={`${i}_b`}>Property <strong>B</strong></li>
  244. <li key={`${i}_a`}>Property <strong>A</strong></li>
  245. </ul>
  246. </li>
  247. break;
  248. case Animation.ANIMATIONTYPE_SIZE:
  249. element = <li className="property" key={i}><p>{animation.targetProperty}</p>
  250. <ul>
  251. <li key={`${i}_width`}>Property <strong>Width</strong></li>
  252. <li key={`${i}_height`}>Property <strong>Height</strong></li>
  253. </ul>
  254. </li>
  255. break;
  256. default: console.log("not recognized");
  257. element = null;
  258. break;
  259. }
  260. return element;
  261. }
  262. getAnimationTypeofChange(selected: string) {
  263. let dataType = 0;
  264. switch (selected) {
  265. case "Float":
  266. dataType = Animation.ANIMATIONTYPE_FLOAT;
  267. break;
  268. case "Quaternion":
  269. dataType = Animation.ANIMATIONTYPE_QUATERNION;
  270. break;
  271. case "Vector3":
  272. dataType = Animation.ANIMATIONTYPE_VECTOR3;
  273. break;
  274. case "Vector2":
  275. dataType = Animation.ANIMATIONTYPE_VECTOR2;
  276. break;
  277. case "Size":
  278. dataType = Animation.ANIMATIONTYPE_SIZE;
  279. break;
  280. case "Color3":
  281. dataType = Animation.ANIMATIONTYPE_COLOR3;
  282. break;
  283. case "Color4":
  284. dataType = Animation.ANIMATIONTYPE_COLOR4;
  285. break;
  286. }
  287. return dataType;
  288. }
  289. deleteAnimation() {
  290. let currentSelected = this.state.selected;
  291. if (this.props.entity instanceof TargetedAnimation) {
  292. console.log("no animation remove allowed");
  293. } else {
  294. let animations = (this.props.entity as IAnimatable).animations;
  295. if (animations) {
  296. let updatedAnimations = animations.filter(anim => anim !== currentSelected);
  297. (this.props.entity as IAnimatable).animations = updatedAnimations as Nullable<Animation[]>;
  298. }
  299. }
  300. }
  301. addAnimation() {
  302. if (this.state.animationName != "" && this.state.animationTargetProperty != "") {
  303. let matchTypeTargetProperty = this.state.animationTargetProperty.split('.');
  304. let animationDataType = this.getAnimationTypeofChange(this.state.animationType);
  305. let matched = false;
  306. if (matchTypeTargetProperty.length === 1) {
  307. let match = (this.props.entity as any)[matchTypeTargetProperty[0]];
  308. if (match) {
  309. switch (match.constructor.name) {
  310. case "Vector2":
  311. animationDataType === Animation.ANIMATIONTYPE_VECTOR2 ? matched = true : matched = false;
  312. break;
  313. case "Vector3":
  314. animationDataType === Animation.ANIMATIONTYPE_VECTOR3 ? matched = true : matched = false;
  315. break;
  316. case "Quaternion":
  317. animationDataType === Animation.ANIMATIONTYPE_QUATERNION ? matched = true : matched = false;
  318. break;
  319. case "Color3":
  320. animationDataType === Animation.ANIMATIONTYPE_COLOR3 ? matched = true : matched = false;
  321. break;
  322. case "Color4":
  323. animationDataType === Animation.ANIMATIONTYPE_COLOR4 ? matched = true : matched = false;
  324. break;
  325. case "Size":
  326. animationDataType === Animation.ANIMATIONTYPE_SIZE ? matched = true : matched = false;
  327. break;
  328. default: console.log("not recognized");
  329. break;
  330. }
  331. } else {
  332. this.setState({ notification: `The selected entity doesn't have a ${matchTypeTargetProperty[0]} property` });
  333. }
  334. } else if (matchTypeTargetProperty.length > 1) {
  335. let match = (this.props.entity as any)[matchTypeTargetProperty[0]][matchTypeTargetProperty[1]];
  336. if (typeof match === "number") {
  337. animationDataType === Animation.ANIMATIONTYPE_FLOAT ? matched = true : matched = false;
  338. }
  339. }
  340. if (matched) {
  341. let startValue;
  342. let endValue;
  343. let outTangent;
  344. let inTangent;
  345. // Default start and end values for new animations
  346. switch (animationDataType) {
  347. case Animation.ANIMATIONTYPE_FLOAT:
  348. startValue = 1;
  349. endValue = 1;
  350. outTangent = 0;
  351. inTangent = 0;
  352. break;
  353. case Animation.ANIMATIONTYPE_VECTOR2:
  354. startValue = new Vector2(1, 1);
  355. endValue = new Vector2(1, 1);
  356. outTangent = Vector2.Zero();
  357. inTangent = Vector2.Zero();
  358. break;
  359. case Animation.ANIMATIONTYPE_VECTOR3:
  360. startValue = new Vector3(1, 1, 1);
  361. endValue = new Vector3(1, 1, 1);
  362. outTangent = Vector3.Zero();
  363. inTangent = Vector3.Zero();
  364. break;
  365. case Animation.ANIMATIONTYPE_QUATERNION:
  366. startValue = new Quaternion(1, 1, 1, 1);
  367. endValue = new Quaternion(1, 1, 1, 1);
  368. outTangent = Quaternion.Zero();
  369. inTangent = Quaternion.Zero();
  370. break;
  371. case Animation.ANIMATIONTYPE_COLOR3:
  372. startValue = new Color3(1, 1, 1);
  373. endValue = new Color3(1, 1, 1);
  374. outTangent = new Color3(0, 0, 0);
  375. inTangent = new Color3(0, 0, 0);
  376. break;
  377. case Animation.ANIMATIONTYPE_COLOR4:
  378. startValue = new Color4(1, 1, 1, 1);
  379. endValue = new Color4(1, 1, 1, 1);
  380. outTangent = new Color4(0, 0, 0, 0);
  381. inTangent = new Color4(0, 0, 0, 0);
  382. break;
  383. case Animation.ANIMATIONTYPE_SIZE:
  384. startValue = new Size(1, 1);
  385. endValue = new Size(1, 1);
  386. outTangent = Size.Zero();
  387. inTangent = Size.Zero();
  388. break;
  389. default: console.log("not recognized");
  390. break;
  391. }
  392. let alreadyAnimatedProperty = (this.props.entity as IAnimatable).animations?.find(anim =>
  393. anim.targetProperty === this.state.animationTargetProperty
  394. , this);
  395. let alreadyAnimationName = (this.props.entity as IAnimatable).animations?.find(anim =>
  396. anim.name === this.state.animationName
  397. , this);
  398. if (alreadyAnimatedProperty) {
  399. this.setState({ notification: `The property "${this.state.animationTargetProperty}" already has an animation` });
  400. } else if (alreadyAnimationName) {
  401. this.setState({ notification: `There is already an animation with the name: "${this.state.animationName}"` });
  402. } else {
  403. let animation = new Animation(this.state.animationName, this.state.animationTargetProperty, 30, animationDataType);
  404. // Start with two keyframes
  405. var keys = [];
  406. keys.push({
  407. frame: 0,
  408. value: startValue,
  409. outTangent: outTangent
  410. });
  411. keys.push({
  412. inTangent: inTangent,
  413. frame: 100,
  414. value: endValue
  415. });
  416. animation.setKeys(keys);
  417. (this.props.entity as IAnimatable).animations?.push(animation);
  418. }
  419. } else {
  420. this.setState({ notification: `The property "${this.state.animationTargetProperty}" is not a "${this.state.animationType}" type` });
  421. }
  422. } else {
  423. this.setState({ notification: "You need to provide a name and target property." });
  424. }
  425. }
  426. /**
  427. * Keyframe Manipulation
  428. * This section handles events from SvgDraggableArea.
  429. */
  430. selectKeyframe(id: string) {
  431. let updatedKeyframes = this.state.svgKeyframes?.map(kf => {
  432. if (kf.id === id) {
  433. kf.selected = !kf.selected
  434. }
  435. return kf;
  436. });
  437. this.setState({ svgKeyframes: updatedKeyframes });
  438. }
  439. selectedControlPoint(type: string, id: string) {
  440. let updatedKeyframes = this.state.svgKeyframes?.map(kf => {
  441. if (kf.id === id) {
  442. this.setState({ isFlatTangentMode: false });
  443. if (type === "left") {
  444. kf.isLeftActive = !kf.isLeftActive;
  445. kf.isRightActive = false;
  446. }
  447. if (type === "right") {
  448. kf.isRightActive = !kf.isRightActive;
  449. kf.isLeftActive = false;
  450. }
  451. }
  452. return kf;
  453. });
  454. this.setState({ svgKeyframes: updatedKeyframes });
  455. }
  456. renderPoints(updatedSvgKeyFrame: IKeyframeSvgPoint, index: number) {
  457. let animation = this.state.selected as Animation;
  458. // Bug: After play/stop we get an extra keyframe at 0
  459. let keys = [...animation.getKeys()];
  460. let newFrame = 0;
  461. if (updatedSvgKeyFrame.keyframePoint.x !== 0) {
  462. if (updatedSvgKeyFrame.keyframePoint.x > 0 && updatedSvgKeyFrame.keyframePoint.x < 1) {
  463. newFrame = 1;
  464. } else {
  465. newFrame = Math.round(updatedSvgKeyFrame.keyframePoint.x);
  466. }
  467. }
  468. keys[index].frame = newFrame; // This value comes as percentage/frame/time
  469. keys[index].value = ((this._heightScale - updatedSvgKeyFrame.keyframePoint.y) / this._heightScale) * 2; // this value comes inverted svg from 0 = 100 to 100 = 0
  470. if (updatedSvgKeyFrame.isLeftActive) {
  471. if (updatedSvgKeyFrame.leftControlPoint !== null) {
  472. // Rotate
  473. let updatedValue = ((this._heightScale - updatedSvgKeyFrame.leftControlPoint.y) / this._heightScale) * 2;
  474. let keyframeValue = ((this._heightScale - updatedSvgKeyFrame.keyframePoint.y) / this._heightScale) * 2;
  475. keys[index].inTangent = keyframeValue - updatedValue;
  476. if (!this.state.isBrokenMode) {
  477. // Right control point if exists
  478. if (updatedSvgKeyFrame.rightControlPoint !== null) {
  479. // Sets opposite value
  480. keys[index].outTangent = keys[index].inTangent * -1
  481. }
  482. }
  483. }
  484. }
  485. if (updatedSvgKeyFrame.isRightActive) {
  486. if (updatedSvgKeyFrame.rightControlPoint !== null) {
  487. // Rotate
  488. let updatedValue = ((this._heightScale - updatedSvgKeyFrame.rightControlPoint.y) / this._heightScale) * 2;
  489. let keyframeValue = ((this._heightScale - updatedSvgKeyFrame.keyframePoint.y) / this._heightScale) * 2;
  490. keys[index].outTangent = keyframeValue - updatedValue;
  491. if (!this.state.isBrokenMode) {
  492. if (updatedSvgKeyFrame.leftControlPoint !== null) { // Sets opposite value
  493. keys[index].inTangent = keys[index].outTangent * -1
  494. }
  495. }
  496. }
  497. }
  498. animation.setKeys(keys);
  499. this.selectAnimation(animation);
  500. }
  501. /**
  502. * Actions
  503. * This section handles events from GraphActionsBar.
  504. */
  505. handleFrameChange(event: React.ChangeEvent<HTMLInputElement>) {
  506. event.preventDefault();
  507. this.changeCurrentFrame(parseInt(event.target.value))
  508. }
  509. handleValueChange(event: React.ChangeEvent<HTMLInputElement>) {
  510. event.preventDefault();
  511. this.setState({ currentValue: parseFloat(event.target.value) }, () => {
  512. if (this.state.selected !== null) {
  513. let animation = this.state.selected;
  514. let keys = animation.getKeys();
  515. let isKeyframe = keys.find(k => k.frame === this.state.currentFrame);
  516. if (isKeyframe) {
  517. let updatedKeys = keys.map(k => {
  518. if (k.frame === this.state.currentFrame) {
  519. k.value = this.state.currentValue;
  520. }
  521. return k;
  522. });
  523. this.state.selected.setKeys(updatedKeys);
  524. this.selectAnimation(animation);
  525. }
  526. }
  527. });
  528. }
  529. setFlatTangent() {
  530. if (this.state.selected !== null) {
  531. let animation = this.state.selected;
  532. this.setState({ isFlatTangentMode: !this.state.isFlatTangentMode }, () => this.selectAnimation(animation));
  533. }
  534. }
  535. // Use this for Bezier curve mode
  536. setTangentMode() {
  537. if (this.state.selected !== null) {
  538. let animation = this.state.selected;
  539. this.setState({ isTangentMode: !this.state.isTangentMode }, () => this.selectAnimation(animation));
  540. }
  541. }
  542. setBrokenMode() {
  543. if (this.state.selected !== null) {
  544. let animation = this.state.selected;
  545. this.setState({ isBrokenMode: !this.state.isBrokenMode }, () => this.selectAnimation(animation));
  546. }
  547. }
  548. setLerpMode() {
  549. if (this.state.selected !== null) {
  550. let animation = this.state.selected;
  551. this.setState({ lerpMode: !this.state.lerpMode }, () => this.selectAnimation(animation));
  552. }
  553. }
  554. addKeyframeClick() {
  555. if (this.state.selected !== null) {
  556. let currentAnimation = this.state.selected;
  557. if (currentAnimation.dataType === Animation.ANIMATIONTYPE_FLOAT) {
  558. let keys = currentAnimation.getKeys();
  559. let x = this.state.currentFrame;
  560. let y = this.state.currentValue;
  561. keys.push({ frame: x, value: y, inTangent: 0, outTangent: 0 });
  562. keys.sort((a, b) => a.frame - b.frame);
  563. currentAnimation.setKeys(keys);
  564. this.selectAnimation(currentAnimation);
  565. }
  566. }
  567. }
  568. removeKeyframeClick() {
  569. if (this.state.selected !== null) {
  570. let currentAnimation = this.state.selected;
  571. if (currentAnimation.dataType === Animation.ANIMATIONTYPE_FLOAT) {
  572. let keys = currentAnimation.getKeys();
  573. let x = this.state.currentFrame;
  574. let filteredKeys = keys.filter(kf => kf.frame !== x);
  575. currentAnimation.setKeys(filteredKeys);
  576. this.selectAnimation(currentAnimation);
  577. }
  578. }
  579. }
  580. addKeyFrame(event: React.MouseEvent<SVGSVGElement>) {
  581. event.preventDefault();
  582. if (this.state.selected !== null) {
  583. var svg = event.target as SVGSVGElement;
  584. var pt = svg.createSVGPoint();
  585. pt.x = event.clientX;
  586. pt.y = event.clientY;
  587. var inverse = svg.getScreenCTM()?.inverse();
  588. var cursorpt = pt.matrixTransform(inverse);
  589. var currentAnimation = this.state.selected;
  590. var keys = currentAnimation.getKeys();
  591. var height = 100;
  592. var middle = (height / 2);
  593. var keyValue;
  594. if (cursorpt.y < middle) {
  595. keyValue = 1 + ((100 / cursorpt.y) * .1)
  596. }
  597. if (cursorpt.y > middle) {
  598. keyValue = 1 - ((100 / cursorpt.y) * .1)
  599. }
  600. keys.push({ frame: cursorpt.x, value: keyValue });
  601. currentAnimation.setKeys(keys);
  602. this.selectAnimation(currentAnimation);
  603. }
  604. }
  605. updateKeyframe(keyframe: Vector2, index: number) {
  606. let anim = this.state.selected as Animation;
  607. var keys: IAnimationKey[] = [];
  608. var svgKeyframes = this.state.svgKeyframes?.map((k, i) => {
  609. if (i === index) {
  610. k.keyframePoint.x = keyframe.x;
  611. k.keyframePoint.y = keyframe.y;
  612. }
  613. var height = 100;
  614. var middle = (height / 2);
  615. var keyValue;
  616. if (k.keyframePoint.y < middle) {
  617. keyValue = 1 + ((100 / k.keyframePoint.y) * .1)
  618. }
  619. if (k.keyframePoint.y > middle) {
  620. keyValue = 1 - ((100 / k.keyframePoint.y) * .1)
  621. }
  622. keys.push({ frame: k.keyframePoint.x, value: keyValue })
  623. return k;
  624. });
  625. anim.setKeys(keys);
  626. this.setState({ svgKeyframes: svgKeyframes });
  627. }
  628. /**
  629. * Curve Rendering Functions
  630. * This section handles how to render curves.
  631. */
  632. getAnimationProperties(animation: Animation) {
  633. let easingType, easingMode;
  634. let easingFunction: EasingFunction = animation.getEasingFunction() as EasingFunction;
  635. if (easingFunction === undefined) {
  636. easingType = undefined
  637. easingMode = undefined;
  638. } else {
  639. easingType = easingFunction.constructor.name;
  640. easingMode = easingFunction.getEasingMode();
  641. }
  642. return { easingType, easingMode }
  643. }
  644. linearInterpolation(keyframes: IAnimationKey[], data: string, middle: number): string {
  645. keyframes.forEach((key, i) => {
  646. var point = new Vector2(0, 0);
  647. point.x = key.frame;
  648. point.y = this._heightScale - (key.value * middle);
  649. this.setKeyframePointLinear(point, i);
  650. if (i !== 0) {
  651. data += ` L${point.x} ${point.y}`
  652. }
  653. });
  654. return data;
  655. }
  656. setKeyframePointLinear(point: Vector2, index: number) {
  657. let svgKeyframe = { keyframePoint: point, rightControlPoint: null, leftControlPoint: null, id: index.toString(), selected: false, isLeftActive: false, isRightActive: false }
  658. this._svgKeyframes.push(svgKeyframe);
  659. }
  660. getPathData(animation: Animation) {
  661. // Check if Tangent mode is active and broken mode is active. (Only one tangent moves)
  662. let keyframes = animation.getKeys();
  663. if (keyframes === undefined) {
  664. return "";
  665. }
  666. // Checks if Flat Tangent is active (tangents are set to zero)
  667. if (this.state && this.state.isFlatTangentMode) {
  668. keyframes = animation.getKeys().map(kf => {
  669. if (kf.inTangent !== undefined) {
  670. kf.inTangent = 0;
  671. }
  672. if (kf.outTangent !== undefined) {
  673. kf.outTangent = 0;
  674. }
  675. return kf;
  676. });
  677. } else {
  678. keyframes = animation.getKeys();
  679. }
  680. const startKey = keyframes[0];
  681. let middle = this._heightScale / 2;
  682. // START OF LINE/CURVE
  683. let data: string | undefined = `M${startKey.frame}, ${this._heightScale - (startKey.value * middle)}`;
  684. if (this.state && this.state.lerpMode) {
  685. data = this.linearInterpolation(keyframes, data, middle);
  686. } else {
  687. if (this.getAnimationData(animation).usesTangents) {
  688. data = this.curvePathWithTangents(keyframes, data, middle, animation.dataType);
  689. } else {
  690. const { easingMode, easingType } = this.getAnimationProperties(animation);
  691. if (easingType !== undefined && easingMode !== undefined) {
  692. let easingFunction = animation.getEasingFunction();
  693. data = this.curvePath(keyframes, data, middle, easingFunction as EasingFunction)
  694. } else {
  695. if (this.state !== undefined) {
  696. let emptyTangents = keyframes.map((kf, i) => {
  697. if (i === 0) {
  698. kf.outTangent = 0;
  699. } else if (i === keyframes.length - 1) {
  700. kf.inTangent = 0;
  701. } else {
  702. kf.inTangent = 0;
  703. kf.outTangent = 0;
  704. }
  705. return kf;
  706. });
  707. data = this.curvePathWithTangents(emptyTangents, data, middle, animation.dataType);
  708. } else {
  709. data = this.linearInterpolation(keyframes, data, middle);
  710. }
  711. }
  712. }
  713. }
  714. return data;
  715. }
  716. getAnimationData(animation: Animation) {
  717. // General Props
  718. let loopMode = animation.loopMode;
  719. let name = animation.name;
  720. let blendingSpeed = animation.blendingSpeed;
  721. let targetProperty = animation.targetProperty;
  722. let targetPropertyPath = animation.targetPropertyPath;
  723. let framesPerSecond = animation.framePerSecond;
  724. let highestFrame = animation.getHighestFrame();
  725. let serialized = animation.serialize();
  726. let usesTangents = animation.getKeys().find(kf => kf.hasOwnProperty('inTangent') || kf.hasOwnProperty('outTangent')) !== undefined ? true : false;
  727. return { loopMode, name, blendingSpeed, targetPropertyPath, targetProperty, framesPerSecond, highestFrame, serialized, usesTangents }
  728. }
  729. drawAllFrames(initialKey: IAnimationKey, endKey: IAnimationKey, easingFunction: EasingFunction) {
  730. let i = initialKey.frame;
  731. for (i; i < endKey.frame; i++) {
  732. (i * 100 / endKey.frame)
  733. let dy = easingFunction.easeInCore(i);
  734. let value = this._heightScale - (dy * (this._heightScale / 2));
  735. this._frames.push(new Vector2(i, value));
  736. }
  737. }
  738. curvePathFlat(keyframes: IAnimationKey[], data: string, middle: number, dataType: number) {
  739. keyframes.forEach((key, i) => {
  740. if (dataType === Animation.ANIMATIONTYPE_FLOAT) {
  741. var pointA = new Vector2(0, 0);
  742. if (i === 0) {
  743. pointA.set(key.frame, this._heightScale - (key.value * middle));
  744. this.setKeyframePoint([pointA], i, keyframes.length);
  745. } else {
  746. pointA.set(keyframes[i - 1].frame, this._heightScale - (keyframes[i - 1].value * middle));
  747. let defaultWeight = 10;
  748. let nextKeyframe = keyframes[i + 1];
  749. let prevKeyframe = keyframes[i - 1];
  750. if (nextKeyframe !== undefined) {
  751. let distance = keyframes[i + 1].frame - key.frame;
  752. defaultWeight = distance * .33;
  753. }
  754. if (prevKeyframe !== undefined) {
  755. let distance = key.frame - keyframes[i - 1].frame;
  756. defaultWeight = distance * .33;
  757. }
  758. let tangentA = new Vector2(pointA.x + defaultWeight, pointA.y);
  759. let pointB = new Vector2(key.frame, this._heightScale - (key.value * middle));
  760. let tangentB = new Vector2(pointB.x - defaultWeight, pointB.y);
  761. this.setKeyframePoint([pointA, tangentA, tangentB, pointB], i, keyframes.length);
  762. data += ` C${tangentA.x} ${tangentA.y} ${tangentB.x} ${tangentB.y} ${pointB.x} ${pointB.y} `
  763. }
  764. }
  765. });
  766. return data;
  767. }
  768. curvePathWithTangents(keyframes: IAnimationKey[], data: string, middle: number, type: number) {
  769. keyframes.forEach((key, i) => {
  770. let svgKeyframe;
  771. let outTangent;
  772. let inTangent;
  773. let defaultWeight = 5;
  774. var inT = key.inTangent === undefined ? null : key.inTangent;
  775. var outT = key.outTangent === undefined ? null : key.outTangent;
  776. let y = this._heightScale - (key.value * middle);
  777. let nextKeyframe = keyframes[i + 1];
  778. let prevKeyframe = keyframes[i - 1];
  779. if (nextKeyframe !== undefined) {
  780. let distance = keyframes[i + 1].frame - key.frame;
  781. defaultWeight = distance * .33;
  782. }
  783. if (prevKeyframe !== undefined) {
  784. let distance = key.frame - keyframes[i - 1].frame;
  785. defaultWeight = distance * .33;
  786. }
  787. if (inT !== null) {
  788. let valueIn = (y * inT) + y;
  789. inTangent = new Vector2(key.frame - defaultWeight, valueIn)
  790. } else {
  791. inTangent = null;
  792. }
  793. if (outT !== null) {
  794. let valueOut = (y * outT) + y;
  795. outTangent = new Vector2(key.frame + defaultWeight, valueOut);
  796. } else {
  797. outTangent = null;
  798. }
  799. if (i === 0) {
  800. svgKeyframe = { keyframePoint: new Vector2(key.frame, this._heightScale - (key.value * middle)), rightControlPoint: outTangent, leftControlPoint: null, id: i.toString(), selected: false, isLeftActive: false, isRightActive: false }
  801. if (outTangent !== null) {
  802. data += ` C${outTangent.x} ${outTangent.y} `;
  803. }
  804. } else {
  805. svgKeyframe = { keyframePoint: new Vector2(key.frame, this._heightScale - (key.value * middle)), rightControlPoint: outTangent, leftControlPoint: inTangent, id: i.toString(), selected: false, isLeftActive: false, isRightActive: false }
  806. if (outTangent !== null && inTangent !== null) {
  807. data += ` ${inTangent.x} ${inTangent.y} ${svgKeyframe.keyframePoint.x} ${svgKeyframe.keyframePoint.y} C${outTangent.x} ${outTangent.y} `
  808. } else if (inTangent !== null) {
  809. data += ` ${inTangent.x} ${inTangent.y} ${svgKeyframe.keyframePoint.x} ${svgKeyframe.keyframePoint.y} `
  810. }
  811. }
  812. if (this.state) {
  813. let prev = this.state.svgKeyframes?.find(kf => kf.id === i.toString());
  814. if (prev) {
  815. svgKeyframe.isLeftActive = prev?.isLeftActive;
  816. svgKeyframe.isRightActive = prev?.isRightActive;
  817. svgKeyframe.selected = prev?.selected
  818. }
  819. }
  820. this._svgKeyframes.push(svgKeyframe);
  821. }, this);
  822. return data;
  823. }
  824. curvePath(keyframes: IAnimationKey[], data: string, middle: number, easingFunction: EasingFunction) {
  825. // This will get 1/4 and 3/4 of points in eased curve
  826. const u = .25;
  827. const v = .75;
  828. keyframes.forEach((key, i) => {
  829. // Gets previous initial point of curve segment
  830. var pointA = new Vector2(0, 0);
  831. if (i === 0) {
  832. pointA.x = key.frame;
  833. pointA.y = this._heightScale - (key.value * middle);
  834. this.setKeyframePoint([pointA], i, keyframes.length);
  835. } else {
  836. pointA.x = keyframes[i - 1].frame;
  837. pointA.y = this._heightScale - (keyframes[i - 1].value * middle)
  838. // Gets the end point of this curve segment
  839. var pointB = new Vector2(key.frame, this._heightScale - (key.value * middle));
  840. // Get easing value of percentage to get the bezier control points below
  841. 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)
  842. let dv = easingFunction.easeInCore(v); // Option 2: Create a easeInCore function and adapt it with the new control points values... needs more revision.
  843. // Direction of curve up/down
  844. let yInt25 = 0;
  845. if (pointB.y > pointA.y) { // if pointB.y > pointA.y = goes down
  846. yInt25 = ((pointB.y - pointA.y) * du) + pointA.y
  847. } else if (pointB.y < pointA.y) { // if pointB.y < pointA.y = goes up
  848. yInt25 = pointA.y - ((pointA.y - pointB.y) * du);
  849. }
  850. let yInt75 = 0;
  851. if (pointB.y > pointA.y) {
  852. yInt75 = ((pointB.y - pointA.y) * dv) + pointA.y
  853. } else if (pointB.y < pointA.y) {
  854. yInt75 = pointA.y - ((pointA.y - pointB.y) * dv)
  855. }
  856. // Intermediate points in curve
  857. let intermediatePoint25 = new Vector2(((pointB.x - pointA.x) * u) + pointA.x, yInt25);
  858. let intermediatePoint75 = new Vector2(((pointB.x - pointA.x) * v) + pointA.x, yInt75);
  859. // Gets the four control points of bezier curve
  860. let controlPoints = this.interpolateControlPoints(pointA, intermediatePoint25, u, intermediatePoint75, v, pointB);
  861. if (controlPoints === undefined) {
  862. console.log("error getting bezier control points");
  863. } else {
  864. this.setKeyframePoint(controlPoints, i, keyframes.length);
  865. data += ` C${controlPoints[1].x} ${controlPoints[1].y} ${controlPoints[2].x} ${controlPoints[2].y} ${controlPoints[3].x} ${controlPoints[3].y}`
  866. }
  867. }
  868. });
  869. return data;
  870. }
  871. setKeyframePoint(controlPoints: Vector2[], index: number, keyframesCount: number) {
  872. let svgKeyframe;
  873. if (index === 0) {
  874. svgKeyframe = { keyframePoint: controlPoints[0], rightControlPoint: null, leftControlPoint: null, id: index.toString(), selected: false, isLeftActive: false, isRightActive: false }
  875. } else {
  876. this._svgKeyframes[index - 1].rightControlPoint = controlPoints[1];
  877. svgKeyframe = { keyframePoint: controlPoints[3], rightControlPoint: null, leftControlPoint: controlPoints[2], id: index.toString(), selected: false, isLeftActive: false, isRightActive: false }
  878. }
  879. this._svgKeyframes.push(svgKeyframe);
  880. }
  881. interpolateControlPoints(p0: Vector2, p1: Vector2, u: number, p2: Vector2, v: number, p3: Vector2): Vector2[] | undefined {
  882. let a = 0.0;
  883. let b = 0.0;
  884. let c = 0.0;
  885. let d = 0.0;
  886. let det = 0.0;
  887. let q1: Vector2 = new Vector2();
  888. let q2: Vector2 = new Vector2();
  889. let controlA: Vector2 = p0;
  890. let controlB: Vector2 = new Vector2();
  891. let controlC: Vector2 = new Vector2();
  892. let controlD: Vector2 = p3;
  893. if ((u <= 0.0) || (u >= 1.0) || (v <= 0.0) || (v >= 1.0) || (u >= v)) {
  894. return undefined;
  895. }
  896. a = 3 * (1 - u) * (1 - u) * u; b = 3 * (1 - u) * u * u;
  897. c = 3 * (1 - v) * (1 - v) * v; d = 3 * (1 - v) * v * v;
  898. det = a * d - b * c;
  899. if (det == 0.0) return undefined;
  900. q1.x = p1.x - ((1 - u) * (1 - u) * (1 - u) * p0.x + u * u * u * p3.x);
  901. q1.y = p1.y - ((1 - u) * (1 - u) * (1 - u) * p0.y + u * u * u * p3.y);
  902. q2.x = p2.x - ((1 - v) * (1 - v) * (1 - v) * p0.x + v * v * v * p3.x);
  903. q2.y = p2.y - ((1 - v) * (1 - v) * (1 - v) * p0.y + v * v * v * p3.y);
  904. controlB.x = (d * q1.x - b * q2.x) / det;
  905. controlB.y = (d * q1.y - b * q2.y) / det;
  906. controlC.x = ((-c) * q1.x + a * q2.x) / det;
  907. controlC.y = ((-c) * q1.y + a * q2.y) / det;
  908. return [controlA, controlB, controlC, controlD];
  909. }
  910. /**
  911. * Core functions
  912. * This section handles main Curve Editor Functions.
  913. */
  914. selectAnimation(animation: Animation) {
  915. this.isAnimationPlaying();
  916. this._svgKeyframes = [];
  917. const pathData = this.getPathData(animation);
  918. let lastFrame = animation.getHighestFrame();
  919. if (pathData === "") {
  920. console.log("no keyframes in this animation");
  921. }
  922. this.setState({ selected: animation, currentPathData: pathData, svgKeyframes: this._svgKeyframes, lastFrame: lastFrame });
  923. }
  924. isAnimationPlaying() {
  925. let target = this.props.entity;
  926. if (this.props.entity instanceof TargetedAnimation) {
  927. target = this.props.entity.target;
  928. }
  929. this._isPlaying = this.props.scene.getAllAnimatablesByTarget(target).length > 0;
  930. if (this._isPlaying) {
  931. this.props.playOrPause && this.props.playOrPause();
  932. } else {
  933. this._isPlaying = false;
  934. }
  935. }
  936. analizeAnimation(animation: Animation | null) {
  937. if (animation !== null) {
  938. const { easingMode, easingType } = this.getAnimationProperties(animation);
  939. let hasDefinedTangents = this.getAnimationData(animation).usesTangents;
  940. if (easingType === undefined && easingMode === undefined && !hasDefinedTangents) {
  941. return true;
  942. } else {
  943. return false;
  944. }
  945. } else {
  946. return false;
  947. }
  948. }
  949. /**
  950. * Timeline
  951. * This section controls the timeline.
  952. */
  953. changeCurrentFrame(frame: number) {
  954. let currentValue;
  955. let selectedCurve = this._selectedCurve.current;
  956. if (selectedCurve) {
  957. var curveLength = selectedCurve.getTotalLength();
  958. let frameValue = (frame * curveLength) / 100;
  959. let currentP = selectedCurve.getPointAtLength(frameValue);
  960. let middle = this._heightScale / 2;
  961. let offset = (((currentP?.y * this._heightScale) - (this._heightScale ** 2) / 2) / middle) / this._heightScale;
  962. let unit = Math.sign(offset);
  963. currentValue = unit === -1 ? Math.abs(offset + unit) : unit - offset;
  964. this.setState({ currentFrame: frame, currentValue: currentValue, currentPoint: currentP });
  965. }
  966. }
  967. updateFrameInKeyFrame(frame: number, index: number) {
  968. if (this.state && this.state.selected) {
  969. let animation = this.state.selected;
  970. let keys = [...animation.getKeys()];
  971. keys[index].frame = frame;
  972. animation.setKeys(keys);
  973. this.selectAnimation(animation);
  974. }
  975. }
  976. render() {
  977. return (
  978. <div id="animation-curve-editor">
  979. <Notification message={this.state.notification} open={this.state.notification !== "" ? true : false} close={() => this.clearNotification()} />
  980. <div className="header">
  981. <div className="title">{this.props.title}</div>
  982. <div className="close" onClick={(event: React.MouseEvent<HTMLDivElement, MouseEvent>) => this.props.close(event)}>
  983. <FontAwesomeIcon icon={faTimes} />
  984. </div>
  985. </div>
  986. <GraphActionsBar currentValue={this.state.currentValue}
  987. currentFrame={this.state.currentFrame}
  988. handleFrameChange={(e) => this.handleFrameChange(e)}
  989. handleValueChange={(e) => this.handleValueChange(e)}
  990. addKeyframe={() => this.addKeyframeClick()}
  991. removeKeyframe={() => this.removeKeyframeClick()}
  992. brokenMode={this.state.isBrokenMode}
  993. brokeTangents={() => this.setBrokenMode()}
  994. lerpMode={this.state.lerpMode}
  995. setLerpMode={() => this.setLerpMode()}
  996. flatTangent={() => this.setFlatTangent()} />
  997. <div className="content">
  998. <div className="row">
  999. <div className="animation-list">
  1000. <div style={{ display: this._isTargetedAnimation ? "none" : "block" }}>
  1001. <div className="label-input">
  1002. <label>Animation Name</label>
  1003. <input type="text" value={this.state.animationName} onChange={(e) => this.handleNameChange(e)}></input>
  1004. </div>
  1005. <div className="label-input">
  1006. <label>Type</label>
  1007. <select onChange={(e) => this.handleTypeChange(e)} value={this.state.animationType}>
  1008. <option value="Float">Float</option>
  1009. <option value="Vector3">Vector3</option>
  1010. <option value="Vector2">Vector2</option>
  1011. <option value="Quaternion">Quaternion</option>
  1012. <option value="Color3">Color3</option>
  1013. <option value="Color4">Color4</option>
  1014. <option value="Size">Size</option>
  1015. </select>
  1016. </div>
  1017. <div className="label-input">
  1018. <label>Target Property</label>
  1019. <input type="text" value={this.state.animationTargetProperty} onChange={(e) => this.handlePropertyChange(e)}></input>
  1020. </div>
  1021. <ButtonLineComponent label={"Add Animation"} onClick={() => this.addAnimation()} />
  1022. </div>
  1023. <div className="object-tree">
  1024. <h2>{this._entityName}</h2>
  1025. <ul>
  1026. {
  1027. this.props.entity instanceof TargetedAnimation ? this.setListItem(this.props.entity.animation, 0) :
  1028. this.props.entity.animations && this.props.entity.animations.map((animation, i) => {
  1029. return this.setListItem(animation, i);
  1030. })}
  1031. </ul>
  1032. </div>
  1033. </div>
  1034. <div ref={this._graphCanvas} className="graph-chart" onWheel={(e) => this.zoom(e)} >
  1035. <Playhead frame={this.state.currentFrame} offset={this.state.playheadOffset} />
  1036. {this.state.svgKeyframes && <SvgDraggableArea ref={this._svgCanvas}
  1037. selectKeyframe={(id: string) => this.selectKeyframe(id)}
  1038. viewBoxScale={this.state.frameAxisLength.length} scale={this.state.scale}
  1039. keyframeSvgPoints={this.state.svgKeyframes}
  1040. selectedControlPoint={(type: string, id: string) => this.selectedControlPoint(type, id)}
  1041. updatePosition={(updatedSvgKeyFrame: IKeyframeSvgPoint, index: number) => this.renderPoints(updatedSvgKeyFrame, index)}>
  1042. {/* Frame Labels */}
  1043. { /* Vertical Grid */}
  1044. {this.state.frameAxisLength.map((f, i) =>
  1045. <svg key={i}>
  1046. <text x={f.value} y="-2" dx="-1em" style={{ font: 'italic 0.2em sans-serif', fontSize: `${0.2 * this.state.scale}em` }}>{f.value}</text>
  1047. <line x1={f.value} y1="0" x2={f.value} y2="100%"></line>
  1048. </svg>
  1049. )}
  1050. {this.state.valueAxisLength.map((f, i) => {
  1051. return <svg key={i}>
  1052. <text x="-3" y={f.value} dx="-1em" style={{ font: 'italic 0.2em sans-serif', fontSize: `${0.2 * this.state.scale}em` }}>{f.label.toFixed(1)}</text>
  1053. <line x1="0" y1={f.value} x2="100%" y2={f.value}></line>
  1054. </svg>
  1055. })}
  1056. { /* Single Curve -Modify this for multiple selection and view */}
  1057. <path ref={this._selectedCurve} pathLength={this.state.lastFrame} id="curve" d={this.state.currentPathData} style={{ stroke: 'red', fill: 'none', strokeWidth: '0.5' }}></path>
  1058. {this._frames && this._frames.map(frame =>
  1059. <svg x={frame.x} y={frame.y} style={{ overflow: 'visible' }}>
  1060. <circle cx="0" cy="0" r="2" stroke="black" strokeWidth="1" fill="white" />
  1061. </svg>
  1062. )}
  1063. </SvgDraggableArea>
  1064. }
  1065. </div>
  1066. </div>
  1067. <div className="row">
  1068. <Timeline currentFrame={this.state.currentFrame} dragKeyframe={(frame: number, index: number) => this.updateFrameInKeyFrame(frame, index)} onCurrentFrameChange={(frame: number) => this.changeCurrentFrame(frame)} keyframes={this.state.selected && this.state.selected.getKeys()} selected={this.state.selected && this.state.selected.getKeys()[0]}></Timeline>
  1069. </div>
  1070. </div>
  1071. </div>
  1072. );
  1073. }
  1074. }