/component/components/nfCDraggable.as

https://github.com/dareksob/nextFramework
ActionScript | 161 lines | 114 code | 21 blank | 26 comment | 14 complexity | ff4588cde0c3538c299895b107e30df0 MD5 | raw file
  1. package nextFramework.component.components
  2. {
  3. import flash.display.DisplayObject;
  4. import flash.display.Stage;
  5. import flash.display.Sprite;
  6. import flash.events.MouseEvent;
  7. import nextFramework.component.nfComponentObject;
  8. import nextFramework.component.nfComponentObjectContainer;
  9. import nextFramework.nF;
  10. import nextFramework.nfProperties;
  11. /**
  12. * @author Darius Sobczak
  13. * @website dsobczak.de
  14. * @mail mail@dsobczak.de
  15. *
  16. * @website nextframework.de
  17. * @version 1.04 beta
  18. */
  19. public class nfCDraggable extends nfComponentObject
  20. {
  21. public function nfCDraggable(container:nfComponentObjectContainer, object:Object, name:String = "")
  22. {
  23. super(container, object, name);
  24. }
  25. override public function added():void
  26. {
  27. if (this.object is DisplayObject) {
  28. if(this.object.stage){
  29. this.addedToStage(null);
  30. }else {
  31. this.object.addEventListener('addedToStage', this.addedToStage);
  32. }
  33. }
  34. }
  35. override public function removed():void
  36. {
  37. if (this.object is DisplayObject) {
  38. this.removeHandler(this.object as DisplayObject);
  39. }
  40. }
  41. private function addedToStage(event:*) {
  42. this.addHandler(this.object as DisplayObject);
  43. }
  44. private function addHandler(object:DisplayObject):void {
  45. if (object.stage is Stage) {
  46. object.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.eventMouseMove, false, 0, true);
  47. object.stage.addEventListener(MouseEvent.MOUSE_UP, this.eventMouseUp, false, 0, true);
  48. object.addEventListener(MouseEvent.MOUSE_DOWN, this.eventMouseDown);
  49. }
  50. }
  51. private function removeHandler(object:DisplayObject):void {
  52. if(object.stage is Stage){
  53. object.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.eventMouseMove, false);
  54. object.stage.removeEventListener(MouseEvent.MOUSE_UP, this.eventMouseUp);
  55. object.removeEventListener(MouseEvent.MOUSE_DOWN, this.eventMouseDown);
  56. }
  57. }
  58. /*
  59. * events
  60. */
  61. private function eventMouseDown(event:MouseEvent):void {
  62. if (this.object is Sprite) {
  63. (this.object as Sprite).startDrag();
  64. }
  65. this.setAutoIndex();
  66. this.mouseDown = true;
  67. }
  68. private function eventMouseUp(event:MouseEvent):void {
  69. if (this.object is Sprite) {
  70. (this.object as Sprite).stopDrag();
  71. }
  72. this.mouseDown = false;
  73. }
  74. private function eventMouseMove(event:MouseEvent):void {
  75. if (this.mouseDown) {
  76. if (this.object is DisplayObject) {
  77. nF.create(this.object).prop( { x: event.stageX, y: event.stageY } );
  78. }
  79. if (this.onDrag is Function) {
  80. this.onDrag(this);
  81. }
  82. }
  83. }
  84. /*
  85. * properties
  86. */
  87. // property mouseDown
  88. private var _mouseDown:Boolean = false;
  89. protected function get mouseDown():Boolean {
  90. return this._mouseDown;
  91. }
  92. protected function set mouseDown(value:Boolean):void {
  93. this._mouseDown = value;
  94. }
  95. // property childControl
  96. private var _childControl:* = null;
  97. public function get childControl():* {
  98. return this._childControl;
  99. }
  100. public function set childControl(value:*):void {
  101. this._childControl = value;
  102. }
  103. public function getChildControl():Sprite {
  104. if (this.childControl is Sprite) {
  105. return this.childControl as Sprite;
  106. }else if (this.childControl != null) {
  107. return nF.create(this.object).getChilds(this.childControl).getSelectionAt('first') as Sprite;
  108. }
  109. return null;
  110. }
  111. // property autoIndex
  112. private var _autoIndex:Boolean = false;
  113. public function get autoIndex():Boolean {
  114. return this._autoIndex;
  115. }
  116. public function set autoIndex(value:Boolean):void {
  117. this._autoIndex = value;
  118. }
  119. private function setAutoIndex():void {
  120. if (!this.autoIndex) return;
  121. nfProperties.setObjectProperties(this.object, { index: 'top' } );
  122. /*if (this.object is DisplayObject) {
  123. var displayobject:DisplayObject = this.object as DisplayObject;
  124. if (displayobject.parent is DisplayObjectContainer) {
  125. displayobject.parent.setChildIndex(displayobject, displayobject.parent.numChildren -1);
  126. }
  127. }*/
  128. }
  129. /*
  130. * property
  131. */
  132. private var _onDrag:Function = null;
  133. public function get onDrag():Function {
  134. return this._onDrag;
  135. }
  136. public function set onDrag(value:Function):void {
  137. this._onDrag = value;
  138. }
  139. }
  140. }