LayerInterface.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import {
  2. getDescriptor,
  3. } from '../functionExtensions';
  4. import Matrix from '../../3rd_party/transformation-matrix';
  5. import MaskManagerInterface from './MaskInterface';
  6. import TransformExpressionInterface from './TransformInterface';
  7. const LayerExpressionInterface = (function () {
  8. function getMatrix(time) {
  9. var toWorldMat = new Matrix();
  10. if (time !== undefined) {
  11. var propMatrix = this._elem.finalTransform.mProp.getValueAtTime(time);
  12. propMatrix.clone(toWorldMat);
  13. } else {
  14. var transformMat = this._elem.finalTransform.mProp;
  15. transformMat.applyToMatrix(toWorldMat);
  16. }
  17. return toWorldMat;
  18. }
  19. function toWorldVec(arr, time) {
  20. var toWorldMat = this.getMatrix(time);
  21. toWorldMat.props[12] = 0;
  22. toWorldMat.props[13] = 0;
  23. toWorldMat.props[14] = 0;
  24. return this.applyPoint(toWorldMat, arr);
  25. }
  26. function toWorld(arr, time) {
  27. var toWorldMat = this.getMatrix(time);
  28. return this.applyPoint(toWorldMat, arr);
  29. }
  30. function fromWorldVec(arr, time) {
  31. var toWorldMat = this.getMatrix(time);
  32. toWorldMat.props[12] = 0;
  33. toWorldMat.props[13] = 0;
  34. toWorldMat.props[14] = 0;
  35. return this.invertPoint(toWorldMat, arr);
  36. }
  37. function fromWorld(arr, time) {
  38. var toWorldMat = this.getMatrix(time);
  39. return this.invertPoint(toWorldMat, arr);
  40. }
  41. function applyPoint(matrix, arr) {
  42. if (this._elem.hierarchy && this._elem.hierarchy.length) {
  43. var i;
  44. var len = this._elem.hierarchy.length;
  45. for (i = 0; i < len; i += 1) {
  46. this._elem.hierarchy[i].finalTransform.mProp.applyToMatrix(matrix);
  47. }
  48. }
  49. return matrix.applyToPointArray(arr[0], arr[1], arr[2] || 0);
  50. }
  51. function invertPoint(matrix, arr) {
  52. if (this._elem.hierarchy && this._elem.hierarchy.length) {
  53. var i;
  54. var len = this._elem.hierarchy.length;
  55. for (i = 0; i < len; i += 1) {
  56. this._elem.hierarchy[i].finalTransform.mProp.applyToMatrix(matrix);
  57. }
  58. }
  59. return matrix.inversePoint(arr);
  60. }
  61. function fromComp(arr) {
  62. var toWorldMat = new Matrix();
  63. toWorldMat.reset();
  64. this._elem.finalTransform.mProp.applyToMatrix(toWorldMat);
  65. if (this._elem.hierarchy && this._elem.hierarchy.length) {
  66. var i;
  67. var len = this._elem.hierarchy.length;
  68. for (i = 0; i < len; i += 1) {
  69. this._elem.hierarchy[i].finalTransform.mProp.applyToMatrix(toWorldMat);
  70. }
  71. return toWorldMat.inversePoint(arr);
  72. }
  73. return toWorldMat.inversePoint(arr);
  74. }
  75. function sampleImage() {
  76. return [1, 1, 1, 1];
  77. }
  78. return function (elem) {
  79. var transformInterface;
  80. function _registerMaskInterface(maskManager) {
  81. _thisLayerFunction.mask = new MaskManagerInterface(maskManager, elem);
  82. }
  83. function _registerEffectsInterface(effects) {
  84. _thisLayerFunction.effect = effects;
  85. }
  86. function _thisLayerFunction(name) {
  87. switch (name) {
  88. case 'ADBE Root Vectors Group':
  89. case 'Contents':
  90. case 2:
  91. return _thisLayerFunction.shapeInterface;
  92. case 1:
  93. case 6:
  94. case 'Transform':
  95. case 'transform':
  96. case 'ADBE Transform Group':
  97. return transformInterface;
  98. case 4:
  99. case 'ADBE Effect Parade':
  100. case 'effects':
  101. case 'Effects':
  102. return _thisLayerFunction.effect;
  103. case 'ADBE Text Properties':
  104. return _thisLayerFunction.textInterface;
  105. default:
  106. return null;
  107. }
  108. }
  109. _thisLayerFunction.getMatrix = getMatrix;
  110. _thisLayerFunction.invertPoint = invertPoint;
  111. _thisLayerFunction.applyPoint = applyPoint;
  112. _thisLayerFunction.toWorld = toWorld;
  113. _thisLayerFunction.toWorldVec = toWorldVec;
  114. _thisLayerFunction.fromWorld = fromWorld;
  115. _thisLayerFunction.fromWorldVec = fromWorldVec;
  116. _thisLayerFunction.toComp = toWorld;
  117. _thisLayerFunction.fromComp = fromComp;
  118. _thisLayerFunction.sampleImage = sampleImage;
  119. _thisLayerFunction.sourceRectAtTime = elem.sourceRectAtTime.bind(elem);
  120. _thisLayerFunction._elem = elem;
  121. transformInterface = TransformExpressionInterface(elem.finalTransform.mProp);
  122. var anchorPointDescriptor = getDescriptor(transformInterface, 'anchorPoint');
  123. Object.defineProperties(_thisLayerFunction, {
  124. hasParent: {
  125. get: function () {
  126. return elem.hierarchy.length;
  127. },
  128. },
  129. parent: {
  130. get: function () {
  131. return elem.hierarchy[0].layerInterface;
  132. },
  133. },
  134. rotation: getDescriptor(transformInterface, 'rotation'),
  135. scale: getDescriptor(transformInterface, 'scale'),
  136. position: getDescriptor(transformInterface, 'position'),
  137. opacity: getDescriptor(transformInterface, 'opacity'),
  138. anchorPoint: anchorPointDescriptor,
  139. anchor_point: anchorPointDescriptor,
  140. transform: {
  141. get: function () {
  142. return transformInterface;
  143. },
  144. },
  145. active: {
  146. get: function () {
  147. return elem.isInRange;
  148. },
  149. },
  150. });
  151. _thisLayerFunction.startTime = elem.data.st;
  152. _thisLayerFunction.index = elem.data.ind;
  153. _thisLayerFunction.source = elem.data.refId;
  154. _thisLayerFunction.height = elem.data.ty === 0 ? elem.data.h : 100;
  155. _thisLayerFunction.width = elem.data.ty === 0 ? elem.data.w : 100;
  156. _thisLayerFunction.inPoint = elem.data.ip / elem.comp.globalData.frameRate;
  157. _thisLayerFunction.outPoint = elem.data.op / elem.comp.globalData.frameRate;
  158. _thisLayerFunction._name = elem.data.nm;
  159. _thisLayerFunction.registerMaskInterface = _registerMaskInterface;
  160. _thisLayerFunction.registerEffectsInterface = _registerEffectsInterface;
  161. return _thisLayerFunction;
  162. };
  163. }());
  164. export default LayerExpressionInterface;