CurveModifiers.as 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package caurina.transitions.properties {
  2. /**
  3. * properties.CurveModifiers
  4. * List of default special properties modifiers for the Tweener class
  5. * The function names are strange/inverted because it makes for easier debugging (alphabetic order). They're only for internal use (on this class) anyways.
  6. *
  7. * @author Zeh Fernando, Nate Chatellier, Arthur Debert
  8. * @version 1.0.0
  9. */
  10. import caurina.transitions.Tweener;
  11. public class CurveModifiers {
  12. /**
  13. * There's no constructor.
  14. */
  15. public function CurveModifiers () {
  16. trace ("This is an static class and should not be instantiated.")
  17. }
  18. /**
  19. * Registers all the special properties to the Tweener class, so the Tweener knows what to do with them.
  20. */
  21. public static function init(): void {
  22. // Bezier modifiers
  23. Tweener.registerSpecialPropertyModifier("_bezier", _bezier_modifier, _bezier_get);
  24. }
  25. // ==================================================================================================================================
  26. // SPECIAL PROPERTY MODIFIER functions ----------------------------------------------------------------------------------------------
  27. // ----------------------------------------------------------------------------------------------------------------------------------
  28. // _bezier
  29. /**
  30. * Given the parameter object passed to this special property, return an array listing the properties that should be modified, and their parameters
  31. *
  32. * @param p_obj Object Parameter passed to this property
  33. * @return Array Array listing name and parameter of each property
  34. */
  35. public static function _bezier_modifier (p_obj:*):Array {
  36. var mList:Array = []; // List of properties to be modified
  37. var pList:Array; // List of parameters passed, normalized as an array
  38. if (p_obj is Array) {
  39. // Complex
  40. pList = p_obj;
  41. } else {
  42. pList = [p_obj];
  43. }
  44. var i:uint;
  45. var istr:String;
  46. var mListObj:Object = {}; // Object describing each property name and parameter
  47. for (i = 0; i < pList.length; i++) {
  48. for (istr in pList[i]) {
  49. if (mListObj[istr] == undefined) mListObj[istr] = [];
  50. mListObj[istr].push(pList[i][istr]);
  51. }
  52. }
  53. for (istr in mListObj) {
  54. mList.push({name:istr, parameters:mListObj[istr]});
  55. }
  56. return mList;
  57. }
  58. /**
  59. * Given tweening specifications (beging, end, t), applies the property parameter to it, returning new t
  60. *
  61. * @param b Number Beginning value of the property
  62. * @param e Number Ending (desired) value of the property
  63. * @param t Number Current t of this tweening (0-1), after applying the easing equation
  64. * @param p Array Array of parameters passed to this specific property
  65. * @return Number New t, with the p parameters applied to it
  66. */
  67. public static function _bezier_get (b:Number, e:Number, t:Number, p:Array):Number {
  68. // This is based on Robert Penner's code
  69. if (p.length == 1) {
  70. // Simple curve with just one bezier control point
  71. return b + t*(2*(1-t)*(p[0]-b) + t*(e - b));
  72. } else {
  73. // Array of bezier control points, must find the point between each pair of bezier points
  74. var ip:uint = Math.floor(t * p.length); // Position on the bezier list
  75. var it:Number = (t - (ip * (1 / p.length))) * p.length; // t inside this ip
  76. var p1:Number, p2:Number;
  77. if (ip == 0) {
  78. // First part: belongs to the first control point, find second midpoint
  79. p1 = b;
  80. p2 = (p[0]+p[1])/2;
  81. } else if (ip == p.length - 1) {
  82. // Last part: belongs to the last control point, find first midpoint
  83. p1 = (p[ip-1]+p[ip])/2;
  84. p2 = e;
  85. } else {
  86. // Any middle part: find both midpoints
  87. p1 = (p[ip-1]+p[ip])/2;
  88. p2 = (p[ip]+p[ip+1])/2;
  89. }
  90. return p1+it*(2*(1-it)*(p[ip]-p1) + it*(p2 - p1));
  91. }
  92. }
  93. }
  94. }