PageRenderTime 65ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/Flixel Versions/Flixel v2.5 Patch 1.0/org/flixel/system/debug/VCR.as

http://flash-game-dev-tips.googlecode.com/
ActionScript | 451 lines | 381 code | 66 blank | 4 comment | 93 complexity | d907a5d37cecc749b789641dcc3b07ff MD5 | raw file
  1. package org.flixel.system.debug
  2. {
  3. import flash.display.Bitmap;
  4. import flash.display.BitmapData;
  5. import flash.display.Sprite;
  6. import flash.events.Event;
  7. import flash.events.IOErrorEvent;
  8. import flash.events.MouseEvent;
  9. import flash.net.FileFilter;
  10. import flash.net.FileReference;
  11. import flash.text.TextField;
  12. import flash.text.TextFormat;
  13. import flash.utils.ByteArray;
  14. import org.flixel.FlxG;
  15. import org.flixel.FlxU;
  16. import org.flixel.system.FlxReplay;
  17. import org.flixel.system.replay.FrameRecord;
  18. import org.flixel.system.replay.MouseRecord;
  19. public class VCR extends Sprite
  20. {
  21. [Embed(source="../../data/vcr/open.png")] protected var ImgOpen:Class;
  22. [Embed(source="../../data/vcr/record_off.png")] protected var ImgRecordOff:Class;
  23. [Embed(source="../../data/vcr/record_on.png")] protected var ImgRecordOn:Class;
  24. [Embed(source="../../data/vcr/stop.png")] protected var ImgStop:Class;
  25. [Embed(source="../../data/vcr/flixel.png")] protected var ImgFlixel:Class;
  26. [Embed(source="../../data/vcr/restart.png")] protected var ImgRestart:Class;
  27. [Embed(source="../../data/vcr/pause.png")] protected var ImgPause:Class;
  28. [Embed(source="../../data/vcr/play.png")] protected var ImgPlay:Class;
  29. [Embed(source="../../data/vcr/step.png")] protected var ImgStep:Class;
  30. static protected const FILE_TYPES:Array = [new FileFilter("Flixel Game Recording", "*.fgr")];
  31. static protected const DEFAULT_FILE_NAME:String = "replay.fgr";
  32. public var paused:Boolean;
  33. public var stepRequested:Boolean;
  34. protected var _open:Bitmap;
  35. protected var _recordOff:Bitmap;
  36. protected var _recordOn:Bitmap;
  37. protected var _stop:Bitmap;
  38. protected var _flixel:Bitmap;
  39. protected var _restart:Bitmap;
  40. protected var _pause:Bitmap;
  41. protected var _play:Bitmap;
  42. protected var _step:Bitmap;
  43. protected var _overOpen:Boolean;
  44. protected var _overRecord:Boolean;
  45. protected var _overRestart:Boolean;
  46. protected var _overPause:Boolean;
  47. protected var _overStep:Boolean;
  48. protected var _pressingOpen:Boolean;
  49. protected var _pressingRecord:Boolean;
  50. protected var _pressingRestart:Boolean;
  51. protected var _pressingPause:Boolean;
  52. protected var _pressingStep:Boolean;
  53. protected var _file:FileReference;
  54. protected var _runtimeDisplay:TextField;
  55. protected var _runtime:uint;
  56. public function VCR()
  57. {
  58. super();
  59. var spacing:uint = 7;
  60. _open = new ImgOpen();
  61. addChild(_open);
  62. _recordOff = new ImgRecordOff();
  63. _recordOff.x = _open.x + _open.width + spacing;
  64. addChild(_recordOff);
  65. _recordOn = new ImgRecordOn();
  66. _recordOn.x = _recordOff.x;
  67. _recordOn.visible = false;
  68. addChild(_recordOn);
  69. _stop = new ImgStop();
  70. _stop.x = _recordOff.x;
  71. _stop.visible = false;
  72. addChild(_stop);
  73. _flixel = new ImgFlixel();
  74. _flixel.x = _recordOff.x + _recordOff.width + spacing;
  75. addChild(_flixel);
  76. _restart = new ImgRestart();
  77. _restart.x = _flixel.x + _flixel.width + spacing;
  78. addChild(_restart);
  79. _pause = new ImgPause();
  80. _pause.x = _restart.x + _restart.width + spacing;
  81. addChild(_pause);
  82. _play = new ImgPlay();
  83. _play.x = _pause.x;
  84. _play.visible = false;
  85. addChild(_play);
  86. _step = new ImgStep();
  87. _step.x = _pause.x + _pause.width + spacing;
  88. addChild(_step);
  89. _runtimeDisplay = new TextField();
  90. _runtimeDisplay.width = width;
  91. _runtimeDisplay.x = width;
  92. _runtimeDisplay.y = -2;
  93. _runtimeDisplay.multiline = false;
  94. _runtimeDisplay.wordWrap = false;
  95. _runtimeDisplay.selectable = false;
  96. _runtimeDisplay.defaultTextFormat = new TextFormat("Courier",12,0xffffff,null,null,null,null,null,"center");
  97. _runtimeDisplay.visible = false;
  98. addChild(_runtimeDisplay);
  99. _runtime = 0;
  100. stepRequested = false;
  101. _file = null;
  102. unpress();
  103. checkOver();
  104. updateGUI();
  105. addEventListener(Event.ENTER_FRAME,init);
  106. }
  107. public function destroy():void
  108. {
  109. _file = null;
  110. removeChild(_open);
  111. _open = null;
  112. removeChild(_recordOff);
  113. _recordOff = null;
  114. removeChild(_recordOn);
  115. _recordOn = null;
  116. removeChild(_stop);
  117. _stop = null;
  118. removeChild(_flixel);
  119. _flixel = null;
  120. removeChild(_restart);
  121. _restart = null;
  122. removeChild(_pause);
  123. _pause = null;
  124. removeChild(_play);
  125. _play = null;
  126. removeChild(_step);
  127. _step = null;
  128. stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
  129. stage.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
  130. stage.removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);
  131. }
  132. public function recording():void
  133. {
  134. _stop.visible = false;
  135. _recordOff.visible = false;
  136. _recordOn.visible = true;
  137. }
  138. public function stopped():void
  139. {
  140. _stop.visible = false;
  141. _recordOn.visible = false;
  142. _recordOff.visible = true;
  143. }
  144. public function playing():void
  145. {
  146. _recordOff.visible = false;
  147. _recordOn.visible = false;
  148. _stop.visible = true;
  149. }
  150. public function updateRuntime(Time:uint):void
  151. {
  152. _runtime += Time;
  153. _runtimeDisplay.text = FlxU.formatTime(_runtime/1000,true);
  154. if(!_runtimeDisplay.visible)
  155. _runtimeDisplay.visible = true;
  156. }
  157. //***ACTUAL BUTTON BEHAVIORS***//
  158. public function onOpen():void
  159. {
  160. _file = new FileReference();
  161. _file.addEventListener(Event.SELECT, onOpenSelect);
  162. _file.addEventListener(Event.CANCEL, onOpenCancel);
  163. _file.browse(FILE_TYPES);
  164. }
  165. protected function onOpenSelect(E:Event=null):void
  166. {
  167. _file.removeEventListener(Event.SELECT, onOpenSelect);
  168. _file.removeEventListener(Event.CANCEL, onOpenCancel);
  169. _file.addEventListener(Event.COMPLETE, onOpenComplete);
  170. _file.addEventListener(IOErrorEvent.IO_ERROR, onOpenError);
  171. _file.load();
  172. }
  173. protected function onOpenComplete(E:Event=null):void
  174. {
  175. _file.removeEventListener(Event.COMPLETE, onOpenComplete);
  176. _file.removeEventListener(IOErrorEvent.IO_ERROR, onOpenError);
  177. //Turn the file into a giant string
  178. var fileContents:String = null;
  179. var data:ByteArray = _file.data;
  180. if(data != null)
  181. fileContents = data.readUTFBytes(data.bytesAvailable);
  182. _file = null;
  183. if((fileContents == null) || (fileContents.length <= 0))
  184. return FlxG.log("ERROR: Empty flixel gameplay record.");
  185. FlxG.loadReplay(fileContents);
  186. }
  187. protected function onOpenCancel(E:Event=null):void
  188. {
  189. _file.removeEventListener(Event.SELECT, onOpenSelect);
  190. _file.removeEventListener(Event.CANCEL, onOpenCancel);
  191. _file = null;
  192. }
  193. protected function onOpenError(E:Event=null):void
  194. {
  195. _file.removeEventListener(Event.COMPLETE, onOpenComplete);
  196. _file.removeEventListener(IOErrorEvent.IO_ERROR, onOpenError);
  197. _file = null;
  198. FlxG.log("ERROR: Unable to open flixel gameplay record.");
  199. }
  200. public function onRecord(StandardMode:Boolean=false):void
  201. {
  202. if(_play.visible)
  203. onPlay();
  204. FlxG.recordReplay(StandardMode);
  205. }
  206. public function stopRecording():void
  207. {
  208. var data:String = FlxG.stopRecording();
  209. if((data != null) && (data.length > 0))
  210. {
  211. _file = new FileReference();
  212. _file.addEventListener(Event.COMPLETE, onSaveComplete);
  213. _file.addEventListener(Event.CANCEL,onSaveCancel);
  214. _file.addEventListener(IOErrorEvent.IO_ERROR, onSaveError);
  215. _file.save(data, DEFAULT_FILE_NAME);
  216. }
  217. }
  218. protected function onSaveComplete(E:Event=null):void
  219. {
  220. _file.removeEventListener(Event.COMPLETE, onSaveComplete);
  221. _file.removeEventListener(Event.CANCEL,onSaveCancel);
  222. _file.removeEventListener(IOErrorEvent.IO_ERROR, onSaveError);
  223. _file = null;
  224. FlxG.log("FLIXEL: successfully saved flixel gameplay record.");
  225. }
  226. protected function onSaveCancel(E:Event=null):void
  227. {
  228. _file.removeEventListener(Event.COMPLETE, onSaveComplete);
  229. _file.removeEventListener(Event.CANCEL,onSaveCancel);
  230. _file.removeEventListener(IOErrorEvent.IO_ERROR, onSaveError);
  231. _file = null;
  232. }
  233. protected function onSaveError(E:Event=null):void
  234. {
  235. _file.removeEventListener(Event.COMPLETE, onSaveComplete);
  236. _file.removeEventListener(Event.CANCEL,onSaveCancel);
  237. _file.removeEventListener(IOErrorEvent.IO_ERROR, onSaveError);
  238. _file = null;
  239. FlxG.log("ERROR: problem saving flixel gameplay record.");
  240. }
  241. public function onStop():void
  242. {
  243. FlxG.stopReplay();
  244. }
  245. public function onRestart(StandardMode:Boolean=false):void
  246. {
  247. if(FlxG.reloadReplay(StandardMode))
  248. {
  249. _recordOff.visible = false;
  250. _recordOn.visible = false;
  251. _stop.visible = true;
  252. }
  253. }
  254. public function onPause():void
  255. {
  256. paused = true;
  257. _pause.visible = false;
  258. _play.visible = true;
  259. }
  260. public function onPlay():void
  261. {
  262. paused = false;
  263. _play.visible = false;
  264. _pause.visible = true;
  265. }
  266. public function onStep():void
  267. {
  268. if(!paused)
  269. onPause();
  270. stepRequested = true;
  271. }
  272. //***EVENT HANDLERS***//
  273. protected function init(E:Event=null):void
  274. {
  275. if(root == null)
  276. return;
  277. removeEventListener(Event.ENTER_FRAME,init);
  278. stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
  279. stage.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
  280. stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
  281. }
  282. protected function onMouseMove(E:MouseEvent=null):void
  283. {
  284. if(!checkOver())
  285. unpress();
  286. updateGUI();
  287. }
  288. protected function onMouseDown(E:MouseEvent=null):void
  289. {
  290. unpress();
  291. if(_overOpen)
  292. _pressingOpen = true;
  293. if(_overRecord)
  294. _pressingRecord = true;
  295. if(_overRestart)
  296. _pressingRestart = true;
  297. if(_overPause)
  298. _pressingPause = true;
  299. if(_overStep)
  300. _pressingStep = true;
  301. }
  302. protected function onMouseUp(E:MouseEvent=null):void
  303. {
  304. if(_overOpen && _pressingOpen)
  305. onOpen();
  306. else if(_overRecord && _pressingRecord)
  307. {
  308. if(_stop.visible)
  309. onStop();
  310. else if(_recordOn.visible)
  311. stopRecording();
  312. else
  313. onRecord(!E.altKey);
  314. }
  315. else if(_overRestart && _pressingRestart)
  316. onRestart(!E.altKey);
  317. else if(_overPause && _pressingPause)
  318. {
  319. if(_play.visible)
  320. onPlay();
  321. else
  322. onPause();
  323. }
  324. else if(_overStep && _pressingStep)
  325. onStep();
  326. unpress();
  327. checkOver();
  328. updateGUI();
  329. }
  330. //***MISC GUI MGMT STUFF***//
  331. protected function checkOver():Boolean
  332. {
  333. _overOpen = _overRecord = _overRestart = _overPause = _overStep = false;
  334. if((mouseX < 0) || (mouseX > width) || (mouseY < 0) || (mouseY > 15))
  335. return false;
  336. if((mouseX >= _recordOff.x) && (mouseX <= _recordOff.x + _recordOff.width))
  337. _overRecord = true;
  338. if(!_recordOn.visible && !_overRecord)
  339. {
  340. if((mouseX >= _open.x) && (mouseX <= _open.x + _open.width))
  341. _overOpen = true;
  342. else if((mouseX >= _restart.x) && (mouseX <= _restart.x + _restart.width))
  343. _overRestart = true;
  344. else if((mouseX >= _pause.x) && (mouseX <= _pause.x + _pause.width))
  345. _overPause = true;
  346. else if((mouseX >= _step.x) && (mouseX <= _step.x + _step.width))
  347. _overStep = true;
  348. }
  349. return true;
  350. }
  351. protected function unpress():void
  352. {
  353. _pressingOpen = _pressingRecord = _pressingRestart = _pressingPause = _pressingStep = false;
  354. }
  355. protected function updateGUI():void
  356. {
  357. if(_recordOn.visible)
  358. {
  359. _open.alpha = _restart.alpha = _pause.alpha = _step.alpha = 0.35;
  360. _recordOn.alpha = 1.0;
  361. return;
  362. }
  363. if(_overOpen && (_open.alpha != 1.0))
  364. _open.alpha = 1.0;
  365. else if(!_overOpen && (_open.alpha != 0.8))
  366. _open.alpha = 0.8;
  367. if(_overRecord && (_recordOff.alpha != 1.0))
  368. _recordOff.alpha = _recordOn.alpha = _stop.alpha = 1.0;
  369. else if(!_overRecord && (_recordOff.alpha != 0.8))
  370. _recordOff.alpha = _recordOn.alpha = _stop.alpha = 0.8;
  371. if(_overRestart && (_restart.alpha != 1.0))
  372. _restart.alpha = 1.0;
  373. else if(!_overRestart && (_restart.alpha != 0.8))
  374. _restart.alpha = 0.8;
  375. if(_overPause && (_pause.alpha != 1.0))
  376. _pause.alpha = _play.alpha = 1.0;
  377. else if(!_overPause && (_pause.alpha != 0.8))
  378. _pause.alpha = _play.alpha = 0.8;
  379. if(_overStep && (_step.alpha != 1.0))
  380. _step.alpha = 1.0;
  381. else if(!_overStep && (_step.alpha != 0.8))
  382. _step.alpha = 0.8;
  383. }
  384. }
  385. }