/sitios/com/graphic/RotatableDynamicSprite.as

https://bitbucket.org/simkinjerkin/simkinfw · ActionScript · 220 lines · 188 code · 31 blank · 1 comment · 17 complexity · b62b41900199fb19c1fda128d0c56767 MD5 · raw file

  1. package com.graphic {
  2. import com.geom.ComplexPoint;
  3. import flash.display.InteractiveObject;
  4. import flash.display.Sprite;
  5. import flash.events.MouseEvent;
  6. import flash.geom.Point;
  7. import flash.geom.Rectangle;
  8. import flash.globalization.LastOperationStatus;
  9. public class RotatableDynamicSprite extends ResizableDynamicSprite {
  10. public static const ROTATING:String = "rotating";
  11. protected var _rotationSprite:Sprite;
  12. protected var _rotation:Number = 0;
  13. protected var _lastRotation:Number = 0;
  14. protected var _startingAngle:Number = 0;
  15. protected var _rotatePoint:ComplexPoint;
  16. protected var _updateRotateContainer:Boolean = true;
  17. protected var _lastContainerDelta:ComplexPoint = new ComplexPoint();
  18. protected var _lastRotationContainer:Number = 0;
  19. override public function get rotation():Number {
  20. return _rotation;
  21. }
  22. override public function set rotation($value:Number):void {
  23. _updateRotateContainer = true;
  24. _currentAction = ROTATING;
  25. _rotation = $value - _lastRotationContainer;
  26. bounds = _bounds;
  27. // _rotation = $value;
  28. }
  29. public function set rotateRegister($value:ComplexPoint):void {
  30. trace("rotateRegister :: " + $value);
  31. _rotatePoint = $value;
  32. }
  33. public function RotatableDynamicSprite() {
  34. super();
  35. }
  36. public function endManualRotation():void {
  37. _lastAction = ROTATING;
  38. _currentAction = INACTIVE;
  39. endRotation();
  40. }
  41. override protected function initialize():void {
  42. super.initialize();
  43. _rotationSprite = new Sprite();
  44. addElement(_rotationSprite, true, _controlsContainer);
  45. }
  46. override protected function getCurrentAction($io:InteractiveObject = null):String {
  47. switch($io) {
  48. case getButtonAt(0):
  49. case getButtonAt(1):
  50. case getButtonAt(2):
  51. case getButtonAt(3):
  52. var action:String = ($io as ResizableDynamicSpriteButton).currentAction;
  53. if(($io as ResizableDynamicSpriteButton).currentAction == RotatableDynamicSpriteButton.ROTATE_STATE) {
  54. return ROTATING;
  55. }
  56. break;
  57. }
  58. return super.getCurrentAction($io);
  59. }
  60. override protected function getButtonInstance($type:String):InteractiveObject {
  61. return new RotatableDynamicSpriteButton($type);
  62. }
  63. override protected function doCurrentAction():void {
  64. super.doCurrentAction();
  65. switch(_currentAction) {
  66. case ROTATING: doRotate(); break;
  67. }
  68. }
  69. override protected function setStartingPoint():void {
  70. super.setStartingPoint();
  71. _updateRotateContainer = true;
  72. if(_currentAction == ROTATING) {
  73. _updateRotateContainer = true;
  74. var currentButtonPosition:ComplexPoint = (_currentButton as BasicDynamicSpriteButton).position;
  75. _lastRotation = _rotation;
  76. _startingAngle = currentButtonPosition.getAngle(getRotationPoint());
  77. }
  78. }
  79. override protected function drawBorder():void {
  80. if(_cornerPositions && _cornerPositions.length > 1) {
  81. _borderContainer.graphics.clear();
  82. _borderContainer.graphics.lineStyle(3, 0x000000, .5);
  83. _borderContainer.graphics.moveTo(_cornerPositions[0].x, _cornerPositions[0].y);
  84. _borderContainer.graphics.lineTo(_cornerPositions[1].x, _cornerPositions[1].y);
  85. _borderContainer.graphics.lineTo(_cornerPositions[2].x, _cornerPositions[2].y);
  86. _borderContainer.graphics.lineTo(_cornerPositions[3].x, _cornerPositions[3].y);
  87. _borderContainer.graphics.lineTo(_cornerPositions[0].x, _cornerPositions[0].y);
  88. } else {
  89. super.drawBorder();
  90. }
  91. }
  92. override protected function updateCornerButtons():void {
  93. super.updateCornerButtons();
  94. for(var i:uint = 0; i < 4; i++) {
  95. if(_currentAction == ROTATING) {
  96. _cornerPositions[i] = (_cornerPositions[i] as ComplexPoint).rotateAxisPoint(-_rotation, getRotationPoint());
  97. } else {
  98. _cornerPositions[i] = (_cornerPositions[i] as ComplexPoint).rotateAxisPoint(-_rotation, _registerPoint);
  99. }
  100. }
  101. }
  102. override protected function updateButtonPosition():void {
  103. super.updateButtonPosition();
  104. for(var i:uint = 0; i < 4; i++) {
  105. getButtonAt(i).rotation = _rotation;
  106. }
  107. }
  108. override protected function doInactive():void {
  109. super.doInactive();
  110. _startingAngle = 0;
  111. _lastRotation = 0;
  112. }
  113. override protected function updateContainerBounds():void {
  114. super.updateContainerBounds();
  115. _container.rotation = _rotation + _lastRotationContainer;
  116. if(_cornerPositions && _cornerPositions.length > 1) {
  117. var register:ComplexPoint = new ComplexPoint(_cornerPositions[0].x + ((_cornerPositions[2].x - _cornerPositions[0].x) / 2),
  118. _cornerPositions[0].y + ((_cornerPositions[2].y - _cornerPositions[0].y) / 2));
  119. var position:ComplexPoint = (_cornerPositions[0] as ComplexPoint).rotateAxisPoint(-_lastRotationContainer, register);
  120. _lastContainerDelta = new ComplexPoint(position.x - _container.x, position.y - _container.y);
  121. _container.x = position.x;
  122. _container.y = position.y;
  123. }
  124. }
  125. override protected function getCurrentPosition($type:String, $width:Number, $height:Number):ComplexPoint {
  126. switch($type) {
  127. case ResizableDynamicSpriteButton.TOP_LEFT: return new ComplexPoint(this.mouseX, this.mouseY).rotateAxisPoint(_rotation, _registerPoint);
  128. case ResizableDynamicSpriteButton.TOP_RIGHT: return (new ComplexPoint(this.mouseX, this.mouseY)).rotateAxisPoint(_rotation, _registerPoint).moveToPolar($width, -180);
  129. case ResizableDynamicSpriteButton.BOTTOM_LEFT: return (new ComplexPoint(this.mouseX, this.mouseY)).rotateAxisPoint(_rotation, _registerPoint).moveToPolar($height, -90);
  130. case ResizableDynamicSpriteButton.BOTTOM_RIGHT: return new ComplexPoint(_bounds.x, _bounds.y);
  131. }
  132. return new ComplexPoint();
  133. }
  134. override protected function doResize():void {
  135. var referencePoint:ComplexPoint = getReferencePoint((_currentButton as BasicDynamicSpriteButton).type);
  136. var dragPoint:ComplexPoint = new ComplexPoint(this.mouseX, this.mouseY);
  137. var referencePointR:ComplexPoint = referencePoint.rotateAxisPoint(_rotation, _registerPoint);
  138. var dragPointR:ComplexPoint = dragPoint.rotateAxisPoint(_rotation, _registerPoint);
  139. var currentPosition:ComplexPoint = getCurrentPosition((_currentButton as BasicDynamicSpriteButton).type, Math.abs(dragPointR.x - referencePointR.x), Math.abs(dragPointR.y - referencePointR.y));
  140. var newBounds:Rectangle = new Rectangle(currentPosition.x, currentPosition.y, Math.abs(dragPointR.x - referencePointR.x), Math.abs(dragPointR.y - referencePointR.y));
  141. var newReference:ComplexPoint = (new ComplexPoint(newBounds.x + (newBounds.width / 2), newBounds.y + (newBounds.height / 2)));
  142. var newPosition:ComplexPoint = getBoundsCorner(newBounds, (_currentButton as BasicDynamicSpriteButton).type).rotateAxisPoint(-_rotation, newReference);
  143. var delta:ComplexPoint = newPosition.clonePoint().minus(dragPoint);
  144. bounds = new Rectangle(newBounds.x - delta.x, newBounds.y - delta.y, newBounds.width, newBounds.height);
  145. }
  146. override protected function onButtonMouseUp($event:MouseEvent):void {
  147. super.onButtonMouseUp($event);
  148. endRotation();
  149. }
  150. protected function endRotation():void {
  151. if(_rotatePoint) {
  152. if(_lastAction == ROTATING) {
  153. correctBounds();
  154. }
  155. }
  156. }
  157. protected function correctBounds():void {
  158. var register:ComplexPoint = new ComplexPoint(_cornerPositions[0].x + ((_cornerPositions[2].x - _cornerPositions[0].x) / 2),
  159. _cornerPositions[0].y + ((_cornerPositions[2].y - _cornerPositions[0].y) / 2));
  160. var correctedPos:ComplexPoint = (_cornerPositions[0] as ComplexPoint).rotateAxisPoint(_rotation, register);
  161. bounds = new Rectangle(correctedPos.x, correctedPos.y, _bounds.width, _bounds.height);
  162. _lastRotationContainer = _container.rotation;
  163. _rotation = 0;
  164. _updateRotateContainer = false;
  165. }
  166. protected function drawPoint($point:ComplexPoint, $color:uint):void {
  167. _controlsContainer.graphics.lineStyle(3, $color);
  168. _controlsContainer.graphics.drawCircle($point.x, $point.y, 5);
  169. _controlsContainer.graphics.endFill();
  170. }
  171. protected function getBoundsCorner($bounds:Rectangle, $type:String):ComplexPoint {
  172. switch($type) {
  173. case ResizableDynamicSpriteButton.TOP_LEFT: return new ComplexPoint($bounds.left, $bounds.top);
  174. case ResizableDynamicSpriteButton.TOP_RIGHT: return new ComplexPoint($bounds.right, $bounds.top);
  175. case ResizableDynamicSpriteButton.BOTTOM_LEFT: return new ComplexPoint($bounds.left, $bounds.bottom);
  176. case ResizableDynamicSpriteButton.BOTTOM_RIGHT: return new ComplexPoint($bounds.right, $bounds.bottom);
  177. }
  178. return new ComplexPoint();
  179. }
  180. protected function doRotate():void {
  181. var mousePoint:ComplexPoint = new ComplexPoint(this.mouseX, this.mouseY);
  182. _rotation = _lastRotation - (_startingAngle - mousePoint.getAngle(getRotationPoint()));
  183. bounds = _bounds;
  184. }
  185. protected function getRotationPoint():ComplexPoint {
  186. return _rotatePoint ? _rotatePoint : _registerPoint;
  187. }
  188. }
  189. }