Guest User

Untitled

a guest
Jun 20th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. class Dock extends MovieClip {
  2.  
  3. var icon_min : Number;
  4. var icon_max : Number;
  5. var icon_size : Number;
  6. var icon_spacing : Number;
  7. var width : Number;
  8. var span : Number;
  9. var amplitude : Number;
  10. var ratio : Number;
  11. var scale : Number = Number.NEGATIVE_INFINITY;
  12. var trend : Number = 0;
  13. var xmouse : Number;
  14. var ymouse : Number;
  15. var layout : String;
  16. var callback : Function;
  17. var items : Array;
  18.  
  19. function Dock() {
  20. setParameters();
  21. setLayout();
  22. createIcons();
  23. createTray();
  24. onEnterFrame = monitorDock;
  25. }
  26.  
  27. private function setParameters():Void {
  28. this.layout = this.layout ? this.layout : 'bottom';
  29. this.icon_min = this.icon_min ? this.icon_min : 32;
  30. this.icon_max = this.icon_max ? this.icon_max : 96;
  31. this.icon_spacing = this.icon_spacing ? this.icon_spacing : 2;
  32. this.span = this.span ? this.span : getSpan();
  33. this.amplitude = this.amplitude ? this.amplitude : getAmplitude();
  34. this.ratio = Math.PI / 2 / this.span;
  35. }
  36.  
  37. private function getSpan():Number {
  38. return (this.icon_min - 16) * (240 - 60) / (96 - 16) + 60;
  39. }
  40.  
  41. private function getAmplitude():Number {
  42. return 2 * (this.icon_max - this.icon_min + this.icon_spacing);
  43. }
  44.  
  45. private function createIcons():Void {
  46. var i:Number;
  47. var id:String;
  48. this.scale = 0;
  49. this.width = (this.items.length - 1) * this.icon_spacing + this.items.length * this.icon_min;
  50. var left:Number = (this.icon_min - this.width) / 2;
  51. for(i = 0; i < this.items.length; i++) {
  52. this.createEmptyMovieClip(String(i), i + 10).attachMovie(this.items[i].id, '_mc', 1);
  53. this[i]._mc._y = -this.icon_size / 2;
  54. this[i]._mc._rotation = -this._rotation;
  55. this[i]._x = this[i].x = left + i * (this.icon_min + this.icon_spacing) + this.icon_spacing / 2;
  56. this[i]._y = -this.icon_spacing;
  57. this[i].onRelease = launchIcon;
  58. this[i].useHandCursor = false;
  59. }
  60. }
  61.  
  62. private function launchIcon():Void {
  63. this._parent.callback(this._parent.items[this._name].label);
  64. }
  65.  
  66. private function createTray():Void {
  67. var height:Number = this.icon_min + 2 * this.icon_spacing;
  68. var width:Number = this.width + 2 * this.icon_spacing;
  69. var mc:MovieClip = this.createEmptyMovieClip('tray_mc', 1);
  70. mc.lineStyle(0, 0xcccccc, 80);
  71. mc.beginFill(0xe8e8e8, 50);
  72. mc.lineTo(0, -height);
  73. mc.lineTo(width, -height);
  74. mc.lineTo(width, 0);
  75. mc.lineTo(0, 0);
  76. mc.endFill();
  77. }
  78.  
  79. private function setLayout():Void {
  80. switch(this.layout) {
  81. case 'left':
  82. this._rotation = 90;
  83. break;
  84. case 'top':
  85. this._rotation = 180;
  86. break;
  87. case 'right':
  88. this._rotation = 270;
  89. break;
  90. default:
  91. this._rotation = Number(this.layout);
  92. }
  93. }
  94.  
  95. private function checkBoundary():Boolean {
  96. var buffer:Number = 4 * this.scale;
  97. return (this.ymouse < 0)
  98. && (this.ymouse > -2 * this.icon_spacing - this.icon_min + (this.icon_min - this.icon_max) * this.scale)
  99. && (this.xmouse > this[0]._x - this[0]._width / 2 - this.icon_spacing - buffer)
  100. && (this.xmouse < this[this.items.length - 1]._x + this[this.items.length - 1]._width / 2 + this.icon_spacing + buffer);
  101. }
  102.  
  103. private function updateTray():Void {
  104. var x:Number;
  105. var w:Number;
  106. x = this[0]._x - this[0]._width / 2 - this.icon_spacing;
  107. w = this[this.items.length - 1]._x + this[this.items.length - 1]._width / 2 + this.icon_spacing;
  108. this['tray_mc']._x = x;
  109. this['tray_mc']._width = w - x;
  110. }
  111.  
  112. private function monitorDock():Boolean {
  113. var i:Number;
  114. var x:Number;
  115. var dx:Number;
  116. var dim:Number;
  117.  
  118. // Mouse did not move and Dock is not between states. Skip rest of the block.
  119. if((this.xmouse == this._xmouse) && (this.ymouse == this._ymouse) && ((this.scale <= 0.01) || (this.scale >= 0.99))) { return false; }
  120.  
  121. // Mouse moved or Dock is between states. Update Dock.
  122. this.xmouse = this._xmouse;
  123. this.ymouse = this._ymouse;
  124.  
  125. // Ensure that inflation does not change direction.
  126. this.trend = (this.trend == 0 ) ? (checkBoundary() ? 0.25 : -0.25) : (this.trend);
  127. this.scale += this.trend;
  128. if( (this.scale < 0.02) || (this.scale > 0.98) ) { this.trend = 0; }
  129.  
  130. // Actual scale is in the range of 0..1
  131. this.scale = Math.min(1, Math.max(0, this.scale));
  132.  
  133. // Hard stuff. Calculating position and scale of individual icons.
  134. for(i = 0; i < this.items.length; i++) {
  135. dx = this[i].x - this.xmouse;
  136. dx = Math.min(Math.max(dx, -this.span), this.span);
  137. dim = this.icon_min + (this.icon_max - this.icon_min) * Math.cos(dx * this.ratio) * (Math.abs(dx) > this.span ? 0 : 1) * this.scale;
  138. this[i]._x = this[i].x + this.scale * this.amplitude * Math.sin(dx * this.ratio);
  139. this[i]._xscale = this[i]._yscale = 100 * dim / this.icon_size;
  140. }
  141.  
  142. // Resize tray to contain icons.
  143. updateTray();
  144. return true;
  145. }
  146.  
  147. }
Add Comment
Please, Sign In to add comment