PageRenderTime 115ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/StageEditor/src/StageManager.as

https://github.com/lvshiling/qizhon
ActionScript | 262 lines | 226 code | 24 blank | 12 comment | 9 complexity | 0fa60debb93f51646cb045ffa1077a37 MD5 | raw file
  1. package
  2. {
  3. import com.demonsters.debugger.MonsterDebugger;
  4. import com.greensock.*;
  5. import com.greensock.events.LoaderEvent;
  6. import com.greensock.loading.*;
  7. import com.greensock.loading.display.ContentDisplay;
  8. import flash.display.Bitmap;
  9. import flash.display.BitmapData;
  10. import flash.display.DisplayObject;
  11. import flash.display.Sprite;
  12. import flash.events.MouseEvent;
  13. import flash.external.ExternalInterface;
  14. import flash.geom.Point;
  15. import flash.geom.Rectangle;
  16. import flash.net.FileReference;
  17. import flash.net.URLVariables;
  18. import flash.utils.ByteArray;
  19. import mx.controls.Tree;
  20. import mx.events.*;
  21. import org.flintparticles.common.displayObjects.Rect;
  22. /**
  23. * ...
  24. * @author Stefan
  25. */
  26. public class StageManager
  27. {
  28. private var editorZone:StageWin;
  29. private var loader:DataLoader;
  30. private var stageEditor:StageEditor;
  31. private static var _instance:StageManager = null;
  32. private var stageWidth:uint = 0;
  33. private var stageHeight:uint = 0;
  34. public static const RAND_POINT:String = 'RAND_POINT';
  35. public static const FIXED_POINT:String = 'FIXED_POINT';
  36. public static const ASSET_PATH:String = '/images/gift/';
  37. public static const shapeUrl:String = "/images/gift/shape.xml"
  38. private var file:FileReference = new FileReference();
  39. public function StageManager(win:StageEditor)
  40. {
  41. stageEditor = win;
  42. stageWidth = stageEditor.width;
  43. stageHeight = stageEditor.height;
  44. editorZone = new StageWin();
  45. stageEditor.editor.addChild(editorZone);
  46. stageEditor.shapes.addEventListener(ListEvent.ITEM_CLICK, onShapeClick);
  47. stageEditor.newObjBtn.addEventListener(MouseEvent.CLICK, onNewObjClick);
  48. stageEditor.saveBtn.addEventListener(MouseEvent.CLICK, onSave);
  49. stageEditor.setBtn.addEventListener(MouseEvent.CLICK, onSetBtn);
  50. stageEditor.resetEditor.addEventListener(MouseEvent.CLICK, onResetEditor);
  51. }
  52. public static function getInstance(win:StageEditor):StageManager
  53. {
  54. if (!_instance)
  55. {
  56. _instance = new StageManager(win);
  57. }
  58. return _instance;
  59. }
  60. private function onResetEditor(evt:MouseEvent):void
  61. {
  62. while(editorZone.numChildren)
  63. {
  64. editorZone.removeChildAt(0);
  65. }
  66. }
  67. private function onSetBtn(evt:MouseEvent):void
  68. {
  69. var left:uint = stageEditor.leftNS.value;
  70. var top:uint = stageEditor.topNS.value;
  71. var w:uint = stageEditor.widthNS.value;
  72. var h:uint = stageEditor.heightNS.value;
  73. stageEditor.editor.x = left;
  74. stageEditor.editor.y = top;
  75. editorZone.redraw(stageWidth - 2 * left, stageHeight - 2 * top);
  76. }
  77. private function onSave(evt:MouseEvent):void
  78. {
  79. var xml:XML = new XML('<gift_shape></gift_shape>');
  80. xml.appendChild(<points leftWidth={stageEditor.editor.x} topHeight={stageEditor.editor.y}></points>);
  81. for(var i:uint=0; i<editorZone.numChildren; i++)
  82. {
  83. var obj:DisplayObject = editorZone.getChildAt(i);
  84. if(obj is Block || obj is Bitmap )
  85. {
  86. var block:Block = obj as Block;
  87. xml.points.appendChild(<point x={obj.x} y={obj.y} /> );
  88. }
  89. }
  90. trace(xml);
  91. file.save(xml,'tmp.xml');
  92. }
  93. private function onNewObjClick(evt:MouseEvent):void
  94. {
  95. var block:Block = new Block(70, 70, 0xFF0000);
  96. editorZone.addChild(block);
  97. }
  98. private function onShapeClick(evt:ListEvent):void
  99. {
  100. var selected:XML;
  101. var item:Object = (evt.currentTarget as Tree).selectedItem;
  102. trace(item.@label);
  103. while(editorZone.numChildren)
  104. {
  105. editorZone.removeChildAt(0);
  106. }
  107. if(item.@label != 'main')
  108. loadShapeShow('rose', item.@label);
  109. }
  110. public function loadConfig():void
  111. {
  112. var queue:LoaderMax = new LoaderMax({name:"confQueue", onProgress:progressHandler, onComplete:completeHandler, onError:errorHandler});
  113. loader = new DataLoader(shapeUrl, { name:"shapetxt", requireWithRoot:this.stageEditor.root, estimatedBytes:900 } );
  114. queue.append(loader);
  115. queue.load();
  116. function progressHandler(event:LoaderEvent):void
  117. {
  118. trace("shape show progress: " + event.target.progress);
  119. }
  120. function errorHandler(event:LoaderEvent):void
  121. {
  122. trace("error occured with " + event.target + ": " + event.text);
  123. }
  124. function completeHandler(event:LoaderEvent):void
  125. {
  126. var giftBitmapData:BitmapData;
  127. var giftBitmap:Bitmap;
  128. var text:String = LoaderMax.getContent("shapetxt");
  129. var gift_shape:XML = new XML(text);
  130. stageEditor.shapes.dataProvider = gift_shape;
  131. }
  132. }
  133. public function loadShapeShow(giftName:String, shapeName:String):void
  134. {
  135. var shapeUrl:String = generateShapeUrl(shapeName);
  136. var giftUrl:String = generateGiftUrl(giftName);
  137. var varUrl:String = generateVarUrl(shapeName);
  138. var queue:LoaderMax = new LoaderMax({name:"mainQueue", onProgress:progressHandler, onComplete:completeHandler, onError:errorHandler});
  139. loader = new DataLoader(shapeUrl, { name:"myText", requireWithRoot:this.stageEditor.root, estimatedBytes:900 } );
  140. queue.append(loader);
  141. queue.append( new DataLoader(varUrl, {name:"myVariables", format:"variables"}) );
  142. queue.append( new ImageLoader(giftUrl, {name:'gift'}));
  143. queue.load();
  144. function progressHandler(event:LoaderEvent):void
  145. {
  146. trace("shape show progress: " + event.target.progress);
  147. }
  148. function completeHandler(event:LoaderEvent):void
  149. {
  150. var giftBitmapData:BitmapData;
  151. var giftBitmap:Bitmap;
  152. var text:String = LoaderMax.getContent("myText");
  153. var variables:URLVariables = LoaderMax.getContent("myVariables");
  154. var gift:ContentDisplay = LoaderMax.getContent("gift");
  155. var gift_shape:XML = new XML(text);
  156. var bornPoints:Array = [];
  157. var shapePoints:Array = [];
  158. MonsterDebugger.log('OnBeginShowGift');
  159. giftBitmapData = new BitmapData(gift.width, gift.height, true, 0xFFFFFF);
  160. giftBitmapData.draw(gift,null,null,null,null,true);
  161. var left:uint = gift_shape.points.@leftWidth;
  162. var top:uint = gift_shape.points.@topHeight;
  163. var plist:XMLList = gift_shape..point;
  164. stageEditor.editor.x = left;
  165. stageEditor.editor.y = top;
  166. trace('left: '+ left + '|' + 'top: ' + top);
  167. editorZone.redraw(stageWidth - 2 * left, stageHeight - 2 * top);
  168. trace(editorZone.x+'||'+editorZone.y);
  169. stageEditor.leftNS.value = left;
  170. stageEditor.topNS.value = top;
  171. stageEditor.widthNS.value = stageWidth - 2 * left;
  172. stageEditor.heightNS.value = stageHeight - 2 * top;
  173. for each(var point:XML in plist)
  174. {
  175. shapePoints.push(new Point(point.@x, point.@y));
  176. }
  177. bornPoints = generatePointArray(shapePoints.length);
  178. for each (var bornPoint:Point in bornPoints)
  179. {
  180. giftBitmap = new Bitmap(giftBitmapData);
  181. giftBitmap.x = bornPoint.x;
  182. giftBitmap.y = bornPoint.y;
  183. editorZone.addChild(giftBitmap);
  184. var p:Point = shapePoints.shift();
  185. TweenLite.to(giftBitmap, 4, { alpha:1, x:(p.x), y:(p.y),onComplete:onTweenComplete,onCompleteParams:[giftBitmap] });
  186. }
  187. var tweenGiftNum:int = 0;
  188. function onTweenComplete(gift:Bitmap):void
  189. {
  190. tweenGiftNum = tweenGiftNum + 1;
  191. /*editorZone.removeChild(gift);
  192. if (tweenGiftNum == bornPoints.length)
  193. {
  194. MonsterDebugger.log('shape show end');
  195. editorZone.redraw(486, 424);
  196. stageEditor.editor.x = 202;
  197. stageEditor.editor.y = 0;
  198. }*/
  199. }
  200. }
  201. function errorHandler(event:LoaderEvent):void
  202. {
  203. trace("error occured with " + event.target + ": " + event.text);
  204. }
  205. }
  206. private function generateGiftUrl(giftName:String):String
  207. {
  208. var ret:String = '';
  209. ret = ASSET_PATH + giftName +'.png';
  210. return ret;
  211. }
  212. private function generateShapeUrl(shape:String):String
  213. {
  214. var ret:String = '';
  215. ret = ASSET_PATH + shape + '.xml';
  216. return ret;
  217. }
  218. private function generateVarUrl(shapeName:String):String
  219. {
  220. var ret:String = '';
  221. ret = ASSET_PATH + shapeName + '_var.txt';
  222. return ret;
  223. }
  224. private function generatePointArray(num:uint):Array
  225. {
  226. var ret:Array = [];
  227. var p:Point;
  228. for (var i:uint = 0; i < num; i++ )
  229. {
  230. p = new Point();
  231. p.x = Math.random() * stageWidth;
  232. p.y = Math.random() * stageHeight;
  233. ret.push(p);
  234. }
  235. return ret;
  236. }
  237. }
  238. }