PageRenderTime 46ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/cn/itamt/utils/inspector/ui/InspectView.as

http://tcodes.googlecode.com/
ActionScript | 315 lines | 178 code | 44 blank | 93 comment | 14 complexity | c0984a1daa9bb0c4ccf111cf29fd3e65 MD5 | raw file
  1. package cn.itamt.utils.inspector.ui {
  2. import cn.itamt.utils.Debug;
  3. import flash.display.DisplayObject;
  4. import flash.display.Sprite;
  5. import flash.events.Event;
  6. import flash.events.MouseEvent;
  7. import flash.geom.Point;
  8. import flash.geom.Rectangle;
  9. import flash.text.TextField;
  10. import flash.text.TextFieldAutoSize;
  11. import flash.text.TextFormat;
  12. import flash.utils.getQualifiedClassName;
  13. /**
  14. * @author tamt
  15. */
  16. public class InspectView extends Sprite {
  17. public static const MOVE : String = 'move';
  18. public static const START_MOVE : String = 'start_move';
  19. public static const END_MOVE : String = 'end_move';
  20. public static const RESET_MOVE : String = 'reset_move';
  21. public static const CLOSE : String = 'close';
  22. public static const INSPECT_PARENT : String = 'inspect_parent';
  23. public static const INSPECT_CHILD : String = 'inspect_child';
  24. public static const INSPECT_BROTHER : String = 'inspect_brother';
  25. public static const INSPECT_INFO : String = 'inspect_info';
  26. public static const INSPECT : String = 'inpsect';
  27. public static const LIVE_INSPECT : String = 'live_inpect';
  28. public var target : DisplayObject;
  29. private var _des : TextField;
  30. //??
  31. private var _mBtn : Sprite;
  32. private var _bar : OperationBar;
  33. //private var _layoutView : LayoutView;
  34. //private var _structView : StructureView;
  35. public function InspectView() : void {
  36. super();
  37. // this.mouseChildren = this.mouseEnabled = false;
  38. _des = new TextField();
  39. _des.autoSize = TextFieldAutoSize.LEFT;
  40. _des.textColor = 0xffffff;
  41. _des.background = true;
  42. _des.backgroundColor = 0x636C02;
  43. _des.border = true;
  44. _des.borderColor = 0x636C02;
  45. _des.cacheAsBitmap = true;
  46. var tfmt : TextFormat = new TextFormat(null, null, 0xffffff);
  47. _des.defaultTextFormat = tfmt;
  48. addChild(_des);
  49. _mBtn = new Sprite();
  50. _mBtn.buttonMode = true;
  51. addChild(_mBtn);
  52. //------???------
  53. _bar = new OperationBar();
  54. _bar.init();
  55. //------------------
  56. if(stage) {
  57. initEventListeners();
  58. } else {
  59. addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
  60. }
  61. }
  62. private var inited : Boolean;
  63. private function initEventListeners() : void {
  64. if(inited)return;
  65. inited = true;
  66. // stage.doubleClickEnabled = true;
  67. // stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
  68. // stage.addEventListener(MouseEvent.DOUBLE_CLICK, onDoubleClick);
  69. _mBtn.addEventListener(MouseEvent.CLICK, onMouseDown);
  70. _bar.addEventListener(OperationBar.DOWN_MOVE, onDownMove);
  71. _bar.addEventListener(OperationBar.UP_MOVE, onMouseUp); _bar.addEventListener(OperationBar.PRESS_CLOSE, onCloseBar);
  72. _bar.addEventListener(OperationBar.PRESS_PARENT, onPressParent); _bar.addEventListener(OperationBar.PRESS_CHILD, onPressChild); _bar.addEventListener(OperationBar.PRESS_BROTHER, onPressBrother); // _bar.addEventListener(OperationBar.PRESS_INFO, onPressInfo);
  73. }
  74. private function onAddToStage(evt : Event = null) : void {
  75. initEventListeners();
  76. }
  77. private var rect : Rectangle;
  78. //??????
  79. private var reg : Point;
  80. //?????????
  81. private var upReg:Point;
  82. public function update(isLiveMode : Boolean = false) : void {
  83. if(!contains(_des))addChild(_des);
  84. if(!contains(_mBtn))addChild(_mBtn);
  85. rect = target.getBounds(stage);
  86. reg = target.localToGlobal(new Point(0, 0));
  87. if(target.parent){
  88. upReg = target.parent.localToGlobal(new Point(0,0));
  89. }else{
  90. upReg = null;
  91. }
  92. _des.text = '[' + getClassName(target) + ']' + '[x: ' + target.x + ', y: ' + target.y + '][w: ' + int(target.width) + ', h: ' + int(target.height) + ']';
  93. _des.x = rect.x - .5;
  94. _des.y = rect.y - _des.height + .5;
  95. if(isLiveMode) {
  96. this.drawMbtn();
  97. } else {
  98. this.drawMbtn(0, 0x636C02);
  99. }
  100. // _mBtn.x = rect.x;
  101. // _mBtn.y = rect.y;
  102. _bar.x = rect.x - .5;
  103. _bar.y = _des.y - _bar.barHeight;
  104. }
  105. private function getClassName(value : *) : String {
  106. var str : String = getQualifiedClassName(value);
  107. return str.slice((str.lastIndexOf('::') >= 0) ? str.lastIndexOf('::') + 2 : 0);
  108. }
  109. private function onDoubleClick(evt : MouseEvent = null) : void {
  110. if(_mBtn.hitTestPoint(mouseX, mouseY)) {
  111. dispatchEvent(new Event(InspectView.RESET_MOVE, false, true));
  112. }
  113. }
  114. private function onMouseDown(evt : MouseEvent) : void {
  115. // if(this._mBtn.hitTestPoint(this.mouseX, this.mouseY)) {
  116. dispatchEvent(new Event(InspectView.INSPECT, false, true));
  117. this._mBtn.buttonMode = this._mBtn.mouseEnabled = false;
  118. // }
  119. }
  120. private function onDownMove(evt : Event) : void {
  121. stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
  122. dispatchEvent(new Event(InspectView.START_MOVE, false, true));
  123. }
  124. private function onMouseUp(evt : Event = null) : void {
  125. stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
  126. dispatchEvent(new Event(InspectView.END_MOVE, false, true));
  127. }
  128. private function onMouseMove(evt : MouseEvent = null) : void {
  129. dispatchEvent(new Event(InspectView.MOVE, false, true));
  130. }
  131. private function onCloseBar(evt : Event = null) : void {
  132. dispatchEvent(new Event(InspectView.CLOSE, false, true));
  133. }
  134. private function onPressParent(evt : Event) : void {
  135. dispatchEvent(new Event(InspectView.INSPECT_PARENT, false, true));
  136. }
  137. private function onPressChild(evt : Event) : void {
  138. dispatchEvent(new Event(InspectView.INSPECT_CHILD, false, true));
  139. }
  140. private function onPressBrother(evt : Event) : void {
  141. dispatchEvent(new Event(InspectView.INSPECT_BROTHER, false, true));
  142. }
  143. private function drawMbtn(alpha : Number = .3, bColor : uint = 0xff0000) : void {
  144. _mBtn.graphics.clear();
  145. _mBtn.graphics.lineStyle(2, bColor, 1, false, 'normal', 'square', 'miter');
  146. _mBtn.graphics.beginFill(0xff0000, alpha);
  147. _mBtn.graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
  148. //????????.
  149. _mBtn.graphics.lineStyle(1, bColor, 1, false, 'normal', 'square', 'miter');
  150. _mBtn.graphics.drawCircle(reg.x, reg.y, 5);
  151. _mBtn.graphics.lineStyle(2, bColor, 1, false, 'normal', 'square', 'miter');
  152. _mBtn.graphics.moveTo(reg.x - 3, reg.y);
  153. _mBtn.graphics.lineTo(reg.x + 3, reg.y);
  154. _mBtn.graphics.moveTo(reg.x, reg.y - 3);
  155. _mBtn.graphics.lineTo(reg.x, reg.y + 3);
  156. _mBtn.graphics.endFill();
  157. //?????????
  158. if(upReg){
  159. _mBtn.graphics.lineStyle(2, 0x0000ff, 1, false, 'normal', 'square', 'miter');
  160. _mBtn.graphics.moveTo(upReg.x - 4, upReg.y);
  161. _mBtn.graphics.lineTo(upReg.x + 4, upReg.y);
  162. _mBtn.graphics.moveTo(upReg.x, upReg.y - 4);
  163. _mBtn.graphics.lineTo(upReg.x, upReg.y + 4);
  164. }
  165. }
  166. /*
  167. private function onPressInfo(evt : Event) : void {
  168. if(_layoutView == null) {
  169. _layoutView = new LayoutView();
  170. _layoutView.addEventListener(LayoutView.INSPECT, onInpectFromLayout); _layoutView.addEventListener(LayoutView.LIVE_INSPECT, onLiveInpectFromLayout);
  171. _layoutView.x = stage.stageWidth - _layoutView.width;
  172. _layoutView.y = (stage.stageHeight - _layoutView.height) / 2;
  173. stage.addChild(_layoutView);
  174. _layoutView.inspect(target);
  175. }else {
  176. if(_layoutView.target == target) {
  177. stage.removeChild(_layoutView);
  178. _layoutView.dispose();
  179. _layoutView = null;
  180. _layoutView.removeEventListener(LayoutView.INSPECT, onInpectFromLayout); _layoutView.removeEventListener(LayoutView.LIVE_INSPECT, onLiveInpectFromLayout);
  181. }else {
  182. _layoutView.inspect(target);
  183. }
  184. }
  185. if(_structView == null) {
  186. _structView = new StructureView();
  187. stage.addChild(_structView);
  188. _structView.inspect(target);
  189. _structView.x = (stage.stageWidth - _structView.width) / 2;
  190. _structView.y = stage.stageHeight - _structView.height;
  191. _structView.addEventListener(InspectEvent.LIVE_INSPECT, onStructViewLiveInspect);
  192. }else {
  193. if(_structView.target == target) {
  194. stage.removeChild(_structView);
  195. _structView.dispose();
  196. _structView = null;
  197. }else {
  198. _structView.inspect(target);
  199. }
  200. }
  201. }
  202. private function onStructViewLiveInspect(evt:InspectEvent):void{
  203. trace("[InspectView][onStructViewLiveInspect]");
  204. this.target = evt.inspectTarget;
  205. dispatchEvent(new Event(InspectView.LIVE_INSPECT));
  206. }
  207. private function onInpectFromLayout(evt : Event) : void {
  208. this.target = _layoutView.target;
  209. dispatchEvent(new Event(InspectView.INSPECT));
  210. }
  211. private function onLiveInpectFromLayout(evt : Event) : void {
  212. this.target = _layoutView.target;
  213. dispatchEvent(new Event(InspectView.LIVE_INSPECT));
  214. }
  215. *
  216. */
  217. public function dispose() : void {
  218. Debug.printf('inspectview dispose');
  219. graphics.clear();
  220. if(_des)if(_des.parent)_des.parent.removeChild(_des);
  221. target = null;
  222. removeEventListener(Event.ADDED_TO_STAGE, onAddToStage);
  223. stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
  224. stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
  225. stage.removeEventListener(MouseEvent.DOUBLE_CLICK, onDoubleClick);
  226. _mBtn.removeEventListener(MouseEvent.CLICK, onMouseDown);
  227. _bar.removeEventListener(OperationBar.DOWN_MOVE, onDownMove);
  228. _bar.removeEventListener(OperationBar.UP_MOVE, onMouseUp);
  229. _bar.removeEventListener(OperationBar.PRESS_CLOSE, onCloseBar);
  230. _bar.removeEventListener(OperationBar.PRESS_PARENT, onPressParent);
  231. _bar.removeEventListener(OperationBar.PRESS_CHILD, onPressChild);
  232. _bar.removeEventListener(OperationBar.PRESS_BROTHER, onPressBrother);
  233. //
  234. // if(_layoutView) {
  235. // stage.removeChild(_layoutView);
  236. // _layoutView.dispose();
  237. // _layoutView = null;
  238. // }
  239. // if(_structView) {
  240. // stage.removeChild(_structView);
  241. // _structView.dispose();
  242. // _structView = null;
  243. // }
  244. }
  245. /**
  246. * ????
  247. */
  248. public function inspect(ele : DisplayObject) : void {
  249. Debug.printf('[InspectView]inspect');
  250. target = ele;
  251. update();
  252. if(_bar.stage == null)addChild(_bar);
  253. _bar.validate(target);
  254. }
  255. /**
  256. * ????
  257. */
  258. public function liveInspect(ele : DisplayObject) : void {
  259. Debug.printf('[InspectView]liveInspect');
  260. target = ele;
  261. update(true);
  262. if(_bar.stage)removeChild(_bar);
  263. _bar.validate(target);
  264. }
  265. }
  266. }