PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Transformer.as

https://github.com/miragine/AS3Utils
ActionScript | 572 lines | 478 code | 70 blank | 24 comment | 26 complexity | a00fbb8bd88e2ce33579401ce1faf309 MD5 | raw file
  1. //****************************************************************************
  2. // Author Scile
  3. // Site www.scile.cn
  4. // Version 1.0
  5. // Wed May 06 14:52:52 2009
  6. //****************************************************************************
  7. // Language Version: ActionScript 3.0
  8. // Runtime Versions: Flash Player 9
  9. //****************************************************************************
  10. package{
  11. import flash.display.MovieClip;
  12. import flash.display.Sprite;
  13. import flash.display.DisplayObject;
  14. import flash.display.DisplayObjectContainer;
  15. import flash.geom.Point;
  16. import flash.geom.Matrix;
  17. import flash.geom.Transform;
  18. import flash.geom.Rectangle;
  19. import flash.events.MouseEvent;
  20. import flash.events.Event;
  21. import flash.ui.Mouse;
  22. import flash.ui.MouseCursor;
  23. public class Transformer extends Sprite{
  24. public var TL:Sprite;
  25. public var TM:Sprite;
  26. public var TR:Sprite;
  27. public var ML:Sprite;
  28. public var MR:Sprite;
  29. public var BL:Sprite;
  30. public var BM:Sprite;
  31. public var BR:Sprite;
  32. public var simpleMode:Boolean = true;
  33. private var point_TL:Point;
  34. private var point_TM:Point;
  35. private var point_TR:Point;
  36. private var point_ML:Point;
  37. private var point_MM:Point;
  38. private var point_MR:Point;
  39. private var point_BL:Point;
  40. private var point_BM:Point;
  41. private var point_BR:Point;
  42. private var body:Sprite;
  43. private var rotor_TL:Sprite;
  44. private var rotor_TR:Sprite;
  45. private var rotor_BL:Sprite;
  46. private var rotor_BR:Sprite;
  47. private var targetRect:Rectangle;
  48. private var targetBevelAngle:Number;
  49. private var targetBevelEdge:Number;
  50. private var targetCenterPoint:Point;
  51. private var targetRotation:Number;
  52. private var targetMatrix:Matrix;
  53. private var targetScaleX:Number;
  54. private var targetScaleY:Number;
  55. private var repairRadian:Number;
  56. private var currentTarget:Sprite;
  57. //set & get
  58. private var _transformTarget:DisplayObject = null;
  59. private var _constrain:Boolean = false;
  60. public function Transformer(){
  61. visible = false;
  62. rotor_TL = addChild(new Sprite()) as Sprite;
  63. rotor_TR = addChild(new Sprite()) as Sprite;
  64. rotor_BL = addChild(new Sprite()) as Sprite;
  65. rotor_BR = addChild(new Sprite()) as Sprite;
  66. body = addChild(new Sprite()) as Sprite;
  67. TL = addChild(new Sprite()) as Sprite;
  68. TM = addChild(new Sprite()) as Sprite;
  69. TR = addChild(new Sprite()) as Sprite;
  70. ML = addChild(new Sprite()) as Sprite;
  71. MR = addChild(new Sprite()) as Sprite;
  72. BL = addChild(new Sprite()) as Sprite;
  73. BM = addChild(new Sprite()) as Sprite;
  74. BR = addChild(new Sprite()) as Sprite;
  75. setSkin(new DefaultSkin());
  76. }
  77. //set & get method
  78. public function set transformTarget(target:DisplayObject):void{
  79. _transformTarget = target;
  80. if(target == null){
  81. visible = false;
  82. }else{
  83. visible = true;
  84. initBody();
  85. }
  86. }
  87. public function get transformTarget():DisplayObject{
  88. return _transformTarget;
  89. }
  90. public function set constrain(bool:Boolean):void{
  91. _constrain = bool;
  92. TM.visible = !bool;
  93. ML.visible = !bool;
  94. MR.visible = !bool;
  95. BM.visible = !bool;
  96. }
  97. public function get constrain():Boolean{
  98. return _constrain;
  99. }
  100. //public method
  101. public function setSkin(skin:DisplayObjectContainer):void {
  102. if (skin.getChildByName("TL") != null) {
  103. removeChild(TL);
  104. TL = addChild(skin.getChildByName("TL")) as Sprite;
  105. }
  106. if (skin.getChildByName("TM") != null) {
  107. removeChild(TM);
  108. TM = addChild(skin.getChildByName("TM")) as Sprite;
  109. }
  110. if (skin.getChildByName("TR") != null) {
  111. removeChild(TR);
  112. TR = addChild(skin.getChildByName("TR")) as Sprite;
  113. }
  114. if (skin.getChildByName("ML") != null) {
  115. removeChild(ML);
  116. ML = addChild(skin.getChildByName("ML")) as Sprite;
  117. }
  118. if (skin.getChildByName("MR") != null) {
  119. removeChild(MR);
  120. MR = addChild(skin.getChildByName("MR")) as Sprite;
  121. }
  122. if (skin.getChildByName("BL") != null) {
  123. removeChild(BL);
  124. BL = addChild(skin.getChildByName("BL")) as Sprite;
  125. }
  126. if (skin.getChildByName("BM") != null) {
  127. removeChild(BM);
  128. BM = addChild(skin.getChildByName("BM")) as Sprite;
  129. }
  130. if (skin.getChildByName("BR") != null) {
  131. removeChild(BR);
  132. BR = addChild(skin.getChildByName("BR")) as Sprite;
  133. }
  134. initEventListener();
  135. }
  136. private function initBody():void{
  137. targetRect = transformTarget.getRect(transformTarget);
  138. point_TL = new Point(targetRect.x , targetRect.y);
  139. point_TM = new Point(targetRect.x + targetRect.width/2, targetRect.y);
  140. point_TR = new Point(targetRect.x + targetRect.width, targetRect.y);
  141. point_ML = new Point(targetRect.x , targetRect.y + targetRect.height/2);
  142. point_MM = new Point(targetRect.x + targetRect.width/2, targetRect.y + targetRect.height/2);
  143. point_MR = new Point(targetRect.x + targetRect.width, targetRect.y + targetRect.height/2);
  144. point_BL = new Point(targetRect.x , targetRect.y + targetRect.height);
  145. point_BM = new Point(targetRect.x + targetRect.width/2, targetRect.y + targetRect.height);
  146. point_BR = new Point(targetRect.x + targetRect.width , targetRect.y + targetRect.height);
  147. body.graphics.clear();
  148. body.graphics.lineStyle(0,0x000000)
  149. body.graphics.beginFill(0xff0000, 0);
  150. body.graphics.drawRect(targetRect.x,targetRect.y,targetRect.width,targetRect.height);
  151. body.graphics.endFill();
  152. rotor_TL.graphics.clear();
  153. rotor_TL.graphics.beginFill(0xff0000, 0);
  154. rotor_TL.graphics.drawCircle(0, 0, 16);
  155. rotor_TL.graphics.endFill();
  156. rotor_TR.graphics.clear();
  157. rotor_TR.graphics.beginFill(0xff0000, 0);
  158. rotor_TR.graphics.drawCircle(0, 0, 16);
  159. rotor_TR.graphics.endFill();
  160. rotor_BL.graphics.clear();
  161. rotor_BL.graphics.beginFill(0xff0000, 0);
  162. rotor_BL.graphics.drawCircle(0, 0, 16);
  163. rotor_BL.graphics.endFill();
  164. rotor_BR.graphics.clear();
  165. rotor_BR.graphics.beginFill(0xff0000, 0);
  166. rotor_BR.graphics.drawCircle(0, 0, 16);
  167. rotor_BR.graphics.endFill();
  168. mapping(transformTarget.transform.matrix)
  169. }
  170. private function initEventListener():void{
  171. TL.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
  172. TR.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
  173. BL.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
  174. BR.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
  175. ML.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
  176. MR.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
  177. TM.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
  178. BM.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
  179. body.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
  180. rotor_TL.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
  181. rotor_TR.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
  182. rotor_BL.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
  183. rotor_BR.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
  184. TL.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
  185. TR.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
  186. BL.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
  187. BR.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
  188. ML.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
  189. MR.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
  190. TM.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
  191. BM.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
  192. body.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
  193. rotor_TL.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
  194. rotor_TR.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
  195. rotor_BL.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
  196. rotor_BR.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
  197. TL.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
  198. TR.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
  199. BL.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
  200. BR.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
  201. ML.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
  202. MR.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
  203. TM.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
  204. BM.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
  205. body.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
  206. rotor_TL.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
  207. rotor_TR.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
  208. rotor_BL.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
  209. rotor_BR.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
  210. TL.buttonMode = true;
  211. TR.buttonMode = true;
  212. BL.buttonMode = true;
  213. BR.buttonMode = true;
  214. ML.buttonMode = true;
  215. MR.buttonMode = true;
  216. TM.buttonMode = true;
  217. BM.buttonMode = true;
  218. rotor_TL.buttonMode = true;
  219. rotor_TR.buttonMode = true;
  220. rotor_BL.buttonMode = true;
  221. rotor_BR.buttonMode = true;
  222. }
  223. private function mouseDownHandler(e:MouseEvent):void {
  224. currentTarget = e.currentTarget as Sprite;
  225. setTargetStatus();
  226. switch(e.currentTarget) {
  227. case body:
  228. body.startDrag();
  229. break;
  230. case TL:
  231. repairRadian = Math.PI - targetBevelAngle;
  232. break;
  233. case TR:
  234. repairRadian = targetBevelAngle;
  235. break;
  236. case BL:
  237. repairRadian = targetBevelAngle - Math.PI;
  238. break;
  239. case BR:
  240. repairRadian = -targetBevelAngle;
  241. break;
  242. case rotor_TL:
  243. repairRadian = Math.PI - targetBevelAngle;
  244. break;
  245. case rotor_TR:
  246. repairRadian = targetBevelAngle;
  247. break;
  248. case rotor_BL:
  249. repairRadian = targetBevelAngle - Math.PI;
  250. break;
  251. case rotor_BR:
  252. repairRadian = -targetBevelAngle;
  253. break;
  254. }
  255. this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  256. stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
  257. }
  258. private function rollOverHandler(e:Event):void {
  259. /*switch(e.currentTarget) {
  260. case body:
  261. Mouse.cursor = MouseCursor.HAND
  262. break;
  263. }*/
  264. }
  265. private function rollOutHandler(e:Event):void {
  266. /*switch(e.currentTarget) {
  267. case body:
  268. Mouse.cursor = MouseCursor.ARROW
  269. break;
  270. }*/
  271. }
  272. private function enterFrameHandler(e:Event):void {
  273. switch(currentTarget) {
  274. case body:
  275. mapping(body.transform.matrix);
  276. break;
  277. case TL:
  278. if(simpleMode){
  279. calculateRotationScale();
  280. }else {
  281. calculateScale();
  282. }
  283. break;
  284. case TR:
  285. if(simpleMode){
  286. calculateRotationScale();
  287. }else {
  288. calculateScale();
  289. }
  290. break;
  291. case BL:
  292. if(simpleMode){
  293. calculateRotationScale();
  294. }else {
  295. calculateScale();
  296. }
  297. break;
  298. case BR:
  299. if(simpleMode){
  300. calculateRotationScale();
  301. }else {
  302. calculateScale();
  303. }
  304. break;
  305. case ML:
  306. calculateWidth();
  307. break;
  308. case MR:
  309. calculateWidth();
  310. break;
  311. case TM:
  312. calculateHeight();
  313. break;
  314. case BM:
  315. calculateHeight();
  316. break;
  317. case rotor_TL:
  318. if(simpleMode){
  319. calculateRotationScale();
  320. }else {
  321. calculateRotation();
  322. }
  323. break;
  324. case rotor_TR:
  325. if(simpleMode){
  326. calculateRotationScale();
  327. }else {
  328. calculateRotation();
  329. }
  330. break;
  331. case rotor_BL:
  332. if(simpleMode){
  333. calculateRotationScale();
  334. }else {
  335. calculateRotation();
  336. }
  337. break;
  338. case rotor_BR:
  339. if(simpleMode){
  340. calculateRotationScale();
  341. }else {
  342. calculateRotation();
  343. }
  344. break;
  345. }
  346. }
  347. private function mouseUpHandler(e:MouseEvent):void {
  348. this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
  349. stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
  350. switch(currentTarget) {
  351. case body:
  352. body.stopDrag();
  353. mapping(body.transform.matrix);
  354. break;
  355. }
  356. }
  357. private function mapping(_matrix:Matrix):void{
  358. body.transform.matrix = _matrix;
  359. transformTarget.transform.matrix = _matrix;
  360. var PTL:Point = body.localToGlobal(point_TL);
  361. PTL = this.globalToLocal(PTL)
  362. TL.x = PTL.x;
  363. TL.y = PTL.y;
  364. rotor_TL.x = PTL.x;
  365. rotor_TL.y = PTL.y;
  366. var PTM:Point = body.localToGlobal(point_TM);
  367. PTM = this.globalToLocal(PTM)
  368. TM.x = PTM.x;
  369. TM.y = PTM.y;
  370. var PTR:Point = body.localToGlobal(point_TR);
  371. PTR = this.globalToLocal(PTR)
  372. TR.x = PTR.x;
  373. TR.y = PTR.y;
  374. rotor_TR.x = PTR.x;
  375. rotor_TR.y = PTR.y;
  376. var PML:Point = body.localToGlobal(point_ML);
  377. PML = this.globalToLocal(PML)
  378. ML.x = PML.x;
  379. ML.y = PML.y;
  380. var PMR:Point = body.localToGlobal(point_MR);
  381. PMR = this.globalToLocal(PMR)
  382. MR.x = PMR.x;
  383. MR.y = PMR.y;
  384. var PBL:Point = body.localToGlobal(point_BL);
  385. PBL = this.globalToLocal(PBL)
  386. BL.x = PBL.x;
  387. BL.y = PBL.y;
  388. rotor_BL.x = PBL.x;
  389. rotor_BL.y = PBL.y;
  390. var PBM:Point = body.localToGlobal(point_BM);
  391. PBM = this.globalToLocal(PBM)
  392. BM.x = PBM.x;
  393. BM.y = PBM.y;
  394. var PBR:Point = body.localToGlobal(point_BR);
  395. PBR = this.globalToLocal(PBR)
  396. BR.x = PBR.x;
  397. BR.y = PBR.y;
  398. rotor_BR.x = PBR.x;
  399. rotor_BR.y = PBR.y;
  400. }
  401. private function setTargetStatus():void{
  402. targetMatrix = transformTarget.transform.matrix.clone();
  403. targetRotation = transformTarget.rotation/180*Math.PI;
  404. targetCenterPoint = body.localToGlobal(point_MM);
  405. targetCenterPoint = this.globalToLocal(targetCenterPoint);
  406. targetScaleX = transformTarget.scaleX;
  407. targetScaleY = transformTarget.scaleY;
  408. targetBevelEdge = Math.sqrt(Math.pow(targetRect.width*targetScaleX,2) + Math.pow(targetRect.height*targetScaleY,2))/2;
  409. targetBevelAngle = Math.atan2(targetRect.height * targetScaleY / 2, targetRect.width * targetScaleX / 2);
  410. }
  411. private function calculateRotationScale():void{
  412. var dis:Number = Math.sqrt(Math.pow(mouseY - targetCenterPoint.y,2) + Math.pow(mouseX - targetCenterPoint.x,2));
  413. var rad:Number = Math.atan2(mouseY - targetCenterPoint.y, mouseX - targetCenterPoint.x)
  414. var mat:Matrix = targetMatrix.clone()
  415. mat.translate(-targetCenterPoint.x, -targetCenterPoint.y);
  416. mat.scale((dis/targetBevelEdge), (dis/targetBevelEdge));
  417. mat.rotate(rad + repairRadian - targetRotation);
  418. mat.translate(targetCenterPoint.x, targetCenterPoint.y);
  419. mapping(mat);
  420. }
  421. private function calculateRotation():void{
  422. var rad:Number = Math.atan2(mouseY - targetCenterPoint.y, mouseX - targetCenterPoint.x)
  423. var mat:Matrix = targetMatrix.clone()
  424. mat.translate(-targetCenterPoint.x, -targetCenterPoint.y);
  425. mat.rotate(rad + repairRadian - targetRotation);
  426. mat.translate(targetCenterPoint.x, targetCenterPoint.y);
  427. mapping(mat);
  428. }
  429. private function calculateScale():void{
  430. var dis:Number = Math.sqrt(Math.pow(mouseY - targetCenterPoint.y,2) + Math.pow(mouseX - targetCenterPoint.x,2));
  431. var mat:Matrix = targetMatrix.clone()
  432. mat.translate(-targetCenterPoint.x, -targetCenterPoint.y);
  433. mat.scale((dis/targetBevelEdge), (dis/targetBevelEdge));
  434. mat.translate(targetCenterPoint.x, targetCenterPoint.y);
  435. mapping(mat);
  436. }
  437. private function calculateWidth():void{
  438. var dis:Number = Math.sqrt(Math.pow(mouseY - targetCenterPoint.y,2) + Math.pow(mouseX - targetCenterPoint.x,2));
  439. var mat:Matrix = targetMatrix.clone()
  440. mat.translate(-targetCenterPoint.x, -targetCenterPoint.y);
  441. mat.rotate(-targetRotation);
  442. mat.scale(dis/(targetRect.width/2*targetScaleX), 1);
  443. mat.rotate(targetRotation);
  444. mat.translate(targetCenterPoint.x, targetCenterPoint.y);
  445. mapping(mat);
  446. }
  447. private function calculateHeight():void{
  448. var dis:Number = Math.sqrt(Math.pow(mouseY - targetCenterPoint.y,2) + Math.pow(mouseX - targetCenterPoint.x,2));
  449. var mat:Matrix = targetMatrix.clone()
  450. mat.translate(-targetCenterPoint.x, -targetCenterPoint.y);
  451. mat.rotate(-targetRotation);
  452. mat.scale(1, dis/(targetRect.height/2*targetScaleY));
  453. mat.rotate(targetRotation);
  454. mat.translate(targetCenterPoint.x, targetCenterPoint.y);
  455. mapping(mat);
  456. }
  457. public override function toString():String {
  458. var str:String = "(TL=" + (TL is MovieClip).toString() +
  459. ", TM="+ (TM is MovieClip).toString() +
  460. ", TR="+ (TR is MovieClip).toString() +
  461. ", ML="+ (ML is MovieClip).toString() +
  462. ", MR="+ (MR is MovieClip).toString() +
  463. ", BL="+ (BL is MovieClip).toString() +
  464. ", BM="+ (BM is MovieClip).toString() +
  465. ", BR="+ (BR is MovieClip).toString() + ")"
  466. return str;
  467. }
  468. }
  469. }
  470. import flash.display.Sprite;
  471. class DefaultSkin extends Sprite {
  472. private var defaultColor1:int = 0xffffff;
  473. private var defaultWidth1:int = 8;
  474. private var defaultHeight1:int = 8;
  475. private var defaultColor2:int = 0x000000;
  476. private var defaultWidth2:int = 4;
  477. private var defaultHeight2:int = 4;
  478. private var defaultAlpha:Number = 1;
  479. public function DefaultSkin() {
  480. addChild(buildBody("TL"))
  481. addChild(buildBody("TM"))
  482. addChild(buildBody("TR"))
  483. addChild(buildBody("ML"))
  484. addChild(buildBody("MR"))
  485. addChild(buildBody("BL"))
  486. addChild(buildBody("BM"))
  487. addChild(buildBody("BR"))
  488. }
  489. private function buildBody(n:String):Sprite{
  490. var tmp:Sprite = new Sprite();
  491. tmp.name = n;
  492. tmp.graphics.beginFill(defaultColor1, defaultAlpha);
  493. tmp.graphics.drawRect(-defaultWidth1/2,-defaultHeight1/2,defaultWidth1,defaultHeight1);
  494. tmp.graphics.endFill();
  495. tmp.graphics.beginFill(defaultColor2, defaultAlpha);
  496. tmp.graphics.drawRect(-defaultWidth2/2,-defaultHeight2/2,defaultWidth2,defaultHeight2);
  497. tmp.graphics.endFill();
  498. return tmp;
  499. }
  500. }