PageRenderTime 60ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/net/flashpunk/debug/Console.as

https://bitbucket.org/jagt/basketball-ftl
ActionScript | 897 lines | 663 code | 91 blank | 143 comment | 108 complexity | 7aec5d3391511e9de47fdef7a18e52cb MD5 | raw file
  1. package net.flashpunk.debug
  2. {
  3. import flash.display.Bitmap;
  4. import flash.display.BitmapData;
  5. import flash.display.BlendMode;
  6. import flash.display.Graphics;
  7. import flash.display.Sprite;
  8. import flash.geom.ColorTransform;
  9. import flash.geom.Rectangle;
  10. import flash.system.System;
  11. import flash.text.TextField;
  12. import flash.text.TextFormat;
  13. import net.flashpunk.Entity;
  14. import net.flashpunk.FP;
  15. import net.flashpunk.graphics.Text;
  16. import net.flashpunk.utils.Input;
  17. import net.flashpunk.utils.Key;
  18. /**
  19. * FlashPunk debug console; can use to log information or pause the game and view/move Entities and step the frame.
  20. */
  21. public class Console
  22. {
  23. /**
  24. * The key used to toggle the Console on/off. Tilde (~) by default.
  25. */
  26. public var toggleKey:uint = 192;
  27. /**
  28. * Constructor.
  29. */
  30. public function Console()
  31. {
  32. Input.define("_ARROWS", Key.RIGHT, Key.LEFT, Key.DOWN, Key.UP);
  33. }
  34. /**
  35. * Logs data to the console.
  36. * @param data The data parameters to log, can be variables, objects, etc. Parameters will be separated by a space (" ").
  37. */
  38. public function log(...data):void
  39. {
  40. var s:String;
  41. if (data.length > 1)
  42. {
  43. s = "";
  44. var i:int = 0;
  45. while (i < data.length)
  46. {
  47. if (i > 0) s += " ";
  48. s += data[i ++].toString();
  49. }
  50. }
  51. else s = data[0].toString();
  52. if (s.indexOf("\n") >= 0)
  53. {
  54. var a:Array = s.split("\n");
  55. for each (s in a) LOG.push(s);
  56. }
  57. else LOG.push(s);
  58. if (_enabled && _sprite.visible) updateLog();
  59. }
  60. /**
  61. * Adds properties to watch in the console's debug panel.
  62. * @param properties The properties (strings) to watch.
  63. */
  64. public function watch(...properties):void
  65. {
  66. var i:String;
  67. if (properties.length > 1)
  68. {
  69. for each (i in properties) WATCH_LIST.push(i);
  70. }
  71. else if (properties[0] is Array || properties[0] is Vector.<*>)
  72. {
  73. for each (i in properties[0]) WATCH_LIST.push(i);
  74. }
  75. else WATCH_LIST.push(properties[0]);
  76. }
  77. /**
  78. * Enables the console.
  79. */
  80. public function enable():void
  81. {
  82. // Quit if the console is already enabled.
  83. if (_enabled) return;
  84. // Enable it and add the Sprite to the stage.
  85. _enabled = true;
  86. FP.engine.addChild(_sprite);
  87. // Used to determine some text sizing.
  88. var big:Boolean = width >= 480;
  89. // The transparent FlashPunk logo overlay bitmap.
  90. _sprite.addChild(_back);
  91. _back.bitmapData = new BitmapData(width, height, true, 0xFFFFFFFF);
  92. var b:BitmapData = (new CONSOLE_LOGO).bitmapData;
  93. FP.matrix.identity();
  94. FP.matrix.tx = Math.max((_back.bitmapData.width - b.width) / 2, 0);
  95. FP.matrix.ty = Math.max((_back.bitmapData.height - b.height) / 2, 0);
  96. FP.matrix.scale(Math.min(width / _back.bitmapData.width, 1), Math.min(height / _back.bitmapData.height, 1));
  97. _back.bitmapData.draw(b, FP.matrix, null, BlendMode.MULTIPLY);
  98. _back.bitmapData.draw(_back.bitmapData, null, null, BlendMode.INVERT);
  99. _back.bitmapData.colorTransform(_back.bitmapData.rect, new ColorTransform(1, 1, 1, 0.5));
  100. // The entity and selection sprites.
  101. _sprite.addChild(_entScreen);
  102. _entScreen.addChild(_entSelect);
  103. // The entity count text.
  104. _sprite.addChild(_entRead);
  105. _entRead.addChild(_entReadText);
  106. _entReadText.defaultTextFormat = format(16, 0xFFFFFF, "right");
  107. _entReadText.embedFonts = true;
  108. _entReadText.width = 100;
  109. _entReadText.height = 20;
  110. _entRead.x = width - _entReadText.width;
  111. // The entity count panel.
  112. _entRead.graphics.clear();
  113. _entRead.graphics.beginFill(0, .5);
  114. _entRead.graphics.drawRoundRectComplex(0, 0, _entReadText.width, 20, 0, 0, 20, 0);
  115. // The FPS text.
  116. _sprite.addChild(_fpsRead);
  117. _fpsRead.addChild(_fpsReadText);
  118. _fpsReadText.defaultTextFormat = format(16);
  119. _fpsReadText.embedFonts = true;
  120. _fpsReadText.width = 70;
  121. _fpsReadText.height = 20;
  122. _fpsReadText.x = 2;
  123. _fpsReadText.y = 1;
  124. // The FPS and frame timing panel.
  125. _fpsRead.graphics.clear();
  126. _fpsRead.graphics.beginFill(0, .75);
  127. _fpsRead.graphics.drawRoundRectComplex(0, 0, big ? 320 : 160, 20, 0, 0, 0, 20);
  128. // The frame timing text.
  129. if (big) _sprite.addChild(_fpsInfo);
  130. _fpsInfo.addChild(_fpsInfoText0);
  131. _fpsInfo.addChild(_fpsInfoText1);
  132. _fpsInfoText0.defaultTextFormat = format(8, 0xAAAAAA);
  133. _fpsInfoText1.defaultTextFormat = format(8, 0xAAAAAA);
  134. _fpsInfoText0.embedFonts = true;
  135. _fpsInfoText1.embedFonts = true;
  136. _fpsInfoText0.width = _fpsInfoText1.width = 60;
  137. _fpsInfoText0.height = _fpsInfoText1.height = 20;
  138. _fpsInfo.x = 75;
  139. _fpsInfoText1.x = 60;
  140. // The memory usage
  141. _fpsRead.addChild(_memReadText);
  142. _memReadText.defaultTextFormat = format(16);
  143. _memReadText.embedFonts = true;
  144. _memReadText.width = 110;
  145. _memReadText.height = 20;
  146. _memReadText.x = _fpsInfo.x + _fpsInfo.width + 5;
  147. _memReadText.y = 1;
  148. // The output log text.
  149. _sprite.addChild(_logRead);
  150. _logRead.addChild(_logReadText0);
  151. _logRead.addChild(_logReadText1);
  152. _logReadText0.defaultTextFormat = format(16, 0xFFFFFF);
  153. _logReadText1.defaultTextFormat = format(big ? 16 : 8, 0xFFFFFF);
  154. _logReadText0.embedFonts = true;
  155. _logReadText1.embedFonts = true;
  156. _logReadText0.selectable = false;
  157. _logReadText0.width = 80;
  158. _logReadText0.height = 20;
  159. _logReadText1.width = width;
  160. _logReadText0.x = 2;
  161. _logReadText0.y = 3;
  162. _logReadText0.text = "OUTPUT:";
  163. _logHeight = height - 60;
  164. _logBar = new Rectangle(8, 24, 16, _logHeight - 8);
  165. _logBarGlobal = _logBar.clone();
  166. _logBarGlobal.y += 40;
  167. _logLines = _logHeight / (big ? 16.5 : 8.5);
  168. // The debug text.
  169. _sprite.addChild(_debRead);
  170. _debRead.addChild(_debReadText0);
  171. _debRead.addChild(_debReadText1);
  172. _debReadText0.defaultTextFormat = format(16, 0xFFFFFF);
  173. _debReadText1.defaultTextFormat = format(8, 0xFFFFFF);
  174. _debReadText0.embedFonts = true;
  175. _debReadText1.embedFonts = true;
  176. _debReadText0.selectable = false;
  177. _debReadText0.width = 80;
  178. _debReadText0.height = 20;
  179. _debReadText1.width = 160;
  180. _debReadText1.height = int(height / 4);
  181. _debReadText0.x = 2;
  182. _debReadText0.y = 3;
  183. _debReadText1.x = 2;
  184. _debReadText1.y = 24;
  185. _debReadText0.text = "DEBUG:";
  186. _debRead.y = height - (_debReadText1.y + _debReadText1.height);
  187. // The button panel buttons.
  188. _sprite.addChild(_butRead);
  189. _butRead.addChild(_butDebug = new CONSOLE_DEBUG);
  190. _butRead.addChild(_butOutput = new CONSOLE_OUTPUT);
  191. _butRead.addChild(_butPlay = new CONSOLE_PLAY).x = 20;
  192. _butRead.addChild(_butPause = new CONSOLE_PAUSE).x = 20;
  193. _butRead.addChild(_butStep = new CONSOLE_STEP).x = 40;
  194. updateButtons();
  195. // The button panel.
  196. _butRead.graphics.clear();
  197. _butRead.graphics.beginFill(0, .75);
  198. _butRead.graphics.drawRoundRectComplex(-20, 0, 100, 20, 0, 0, 20, 20);
  199. // Default the display to debug view
  200. debug = true;
  201. // Set the state to unpaused.
  202. paused = false;
  203. }
  204. /**
  205. * If the console should be visible.
  206. */
  207. public function get visible():Boolean { return _sprite.visible; }
  208. public function set visible(value:Boolean):void
  209. {
  210. _sprite.visible = value;
  211. if (_enabled && value) updateLog();
  212. }
  213. /**
  214. * Console update, called by game loop.
  215. */
  216. public function update():void
  217. {
  218. // Quit if the console isn't enabled.
  219. if (!_enabled) return;
  220. // If the console is paused.
  221. if (_paused)
  222. {
  223. // Update buttons.
  224. updateButtons();
  225. // While in debug mode.
  226. if (_debug)
  227. {
  228. // While the game is paused.
  229. if (FP.engine.paused)
  230. {
  231. // When the mouse is pressed.
  232. if (Input.mousePressed)
  233. {
  234. // Mouse is within clickable area.
  235. if (Input.mouseFlashY > 20 && (Input.mouseFlashX > _debReadText1.width || Input.mouseFlashY < _debRead.y))
  236. {
  237. if (Input.check(Key.SHIFT))
  238. {
  239. if (SELECT_LIST.length) startDragging();
  240. else startPanning();
  241. }
  242. else startSelection();
  243. }
  244. }
  245. else
  246. {
  247. // Update mouse movement functions.
  248. if (_selecting) updateSelection();
  249. if (_dragging) updateDragging();
  250. if (_panning) updatePanning();
  251. }
  252. // Select all Entities
  253. if (Input.pressed(Key.A)) selectAll();
  254. // If the shift key is held.
  255. if (Input.check(Key.SHIFT))
  256. {
  257. // If Entities are selected.
  258. if (SELECT_LIST.length)
  259. {
  260. // Move Entities with the arrow keys.
  261. if (Input.pressed("_ARROWS")) updateKeyMoving();
  262. }
  263. else
  264. {
  265. // Pan the camera with the arrow keys.
  266. if (Input.check("_ARROWS")) updateKeyPanning();
  267. }
  268. }
  269. }
  270. else
  271. {
  272. // Update info while the game runs.
  273. updateEntityLists(FP.world.count != ENTITY_LIST.length);
  274. renderEntities();
  275. updateFPSRead();
  276. updateEntityCount();
  277. }
  278. // Update debug panel.
  279. updateDebugRead();
  280. }
  281. else
  282. {
  283. // log scrollbar
  284. if (_scrolling) updateScrolling();
  285. else if (Input.mousePressed) startScrolling();
  286. }
  287. }
  288. else
  289. {
  290. // Update info while the game runs.
  291. updateFPSRead();
  292. updateEntityCount();
  293. }
  294. // Console toggle.
  295. if (Input.pressed(toggleKey)) paused = !_paused;
  296. }
  297. /**
  298. * If the Console is currently in paused mode.
  299. */
  300. public function get paused():Boolean { return _paused; }
  301. public function set paused(value:Boolean):void
  302. {
  303. // Quit if the console isn't enabled.
  304. if (!_enabled) return;
  305. // Set the console to paused.
  306. _paused = value;
  307. FP.engine.paused = value;
  308. // Panel visibility.
  309. _back.visible = value;
  310. _entScreen.visible = value;
  311. _butRead.visible = value;
  312. // If the console is paused.
  313. if (value)
  314. {
  315. // Set the console to paused mode.
  316. if (_debug) debug = true;
  317. else updateLog();
  318. }
  319. else
  320. {
  321. // Set the console to running mode.
  322. _debRead.visible = false;
  323. _logRead.visible = true;
  324. updateLog();
  325. ENTITY_LIST.length = 0;
  326. SCREEN_LIST.length = 0;
  327. SELECT_LIST.length = 0;
  328. }
  329. }
  330. /**
  331. * If the Console is currently in debug mode.
  332. */
  333. public function get debug():Boolean { return _debug; }
  334. public function set debug(value:Boolean):void
  335. {
  336. // Quit if the console isn't enabled.
  337. if (!_enabled) return;
  338. // Set the console to debug mode.
  339. _debug = value;
  340. _debRead.visible = value;
  341. _logRead.visible = !value;
  342. // Update console state.
  343. if (value) updateEntityLists();
  344. else updateLog();
  345. renderEntities();
  346. }
  347. /** @private Steps the frame ahead. */
  348. private function stepFrame():void
  349. {
  350. FP.engine.update();
  351. FP.engine.render();
  352. updateEntityCount();
  353. updateEntityLists();
  354. renderEntities();
  355. }
  356. /** @private Starts Entity dragging. */
  357. private function startDragging():void
  358. {
  359. _dragging = true;
  360. _entRect.x = Input.mouseX;
  361. _entRect.y = Input.mouseY;
  362. }
  363. /** @private Updates Entity dragging. */
  364. private function updateDragging():void
  365. {
  366. moveSelected(Input.mouseX - _entRect.x, Input.mouseY - _entRect.y);
  367. _entRect.x = Input.mouseX;
  368. _entRect.y = Input.mouseY;
  369. if (Input.mouseReleased) _dragging = false;
  370. }
  371. /** @private Move the selected Entities by the amount. */
  372. private function moveSelected(xDelta:int, yDelta:int):void
  373. {
  374. for each (var e:Entity in SELECT_LIST)
  375. {
  376. e.x += xDelta;
  377. e.y += yDelta;
  378. }
  379. FP.engine.render();
  380. renderEntities();
  381. updateEntityLists(true);
  382. }
  383. /** @private Starts camera panning. */
  384. private function startPanning():void
  385. {
  386. _panning = true;
  387. _entRect.x = Input.mouseX;
  388. _entRect.y = Input.mouseY;
  389. }
  390. /** @private Updates camera panning. */
  391. private function updatePanning():void
  392. {
  393. if (Input.mouseReleased) _panning = false;
  394. panCamera(_entRect.x - Input.mouseX, _entRect.y - Input.mouseY);
  395. _entRect.x = Input.mouseX;
  396. _entRect.y = Input.mouseY;
  397. }
  398. /** @private Pans the camera. */
  399. private function panCamera(xDelta:int, yDelta:int):void
  400. {
  401. FP.camera.x += xDelta;
  402. FP.camera.y += yDelta;
  403. FP.engine.render();
  404. updateEntityLists(true);
  405. renderEntities();
  406. }
  407. /** @private Sets the camera position. */
  408. private function setCamera(x:int, y:int):void
  409. {
  410. FP.camera.x = x;
  411. FP.camera.y = y;
  412. FP.engine.render();
  413. updateEntityLists(true);
  414. renderEntities();
  415. }
  416. /** @private Starts Entity selection. */
  417. private function startSelection():void
  418. {
  419. _selecting = true;
  420. _entRect.x = Input.mouseFlashX;
  421. _entRect.y = Input.mouseFlashY;
  422. _entRect.width = 0;
  423. _entRect.height = 0;
  424. }
  425. /** @private Updates Entity selection. */
  426. private function updateSelection():void
  427. {
  428. _entRect.width = Input.mouseFlashX - _entRect.x;
  429. _entRect.height = Input.mouseFlashY - _entRect.y;
  430. if (Input.mouseReleased)
  431. {
  432. selectEntities(_entRect);
  433. renderEntities();
  434. _selecting = false;
  435. _entSelect.graphics.clear();
  436. }
  437. else
  438. {
  439. _entSelect.graphics.clear();
  440. _entSelect.graphics.lineStyle(1, 0xFFFFFF);
  441. _entSelect.graphics.drawRect(_entRect.x, _entRect.y, _entRect.width, _entRect.height);
  442. }
  443. }
  444. /** @private Selects the Entities in the rectangle. */
  445. private function selectEntities(rect:Rectangle):void
  446. {
  447. if (rect.width < 0) rect.x -= (rect.width = -rect.width);
  448. else if (!rect.width) rect.width = 1;
  449. if (rect.height < 0) rect.y -= (rect.height = -rect.height);
  450. else if (!rect.height) rect.height = 1;
  451. FP.rect.width = FP.rect.height = 6;
  452. var sx:Number = FP.screen.scaleX * FP.screen.scale,
  453. sy:Number = FP.screen.scaleY * FP.screen.scale,
  454. e:Entity;
  455. if (Input.check(Key.CONTROL))
  456. {
  457. // Append selected Entities with new selections.
  458. for each (e in SCREEN_LIST)
  459. {
  460. if (SELECT_LIST.indexOf(e) < 0)
  461. {
  462. FP.rect.x = (e.x - FP.camera.x) * sx - 3;
  463. FP.rect.y = (e.y - FP.camera.y) * sy - 3;
  464. if (rect.intersects(FP.rect)) SELECT_LIST.push(e);
  465. }
  466. }
  467. }
  468. else
  469. {
  470. // Replace selections with new selections.
  471. SELECT_LIST.length = 0;
  472. for each (e in SCREEN_LIST)
  473. {
  474. FP.rect.x = (e.x - FP.camera.x) * sx - 3;
  475. FP.rect.y = (e.y - FP.camera.y) * sy - 3;
  476. if (rect.intersects(FP.rect)) SELECT_LIST.push(e);
  477. }
  478. }
  479. }
  480. /** @private Selects all entities on screen. */
  481. private function selectAll():void
  482. {
  483. SELECT_LIST.length = 0;
  484. for each (var e:Entity in SCREEN_LIST) SELECT_LIST.push(e);
  485. renderEntities();
  486. }
  487. /** @private Starts log text scrolling. */
  488. private function startScrolling():void
  489. {
  490. if (LOG.length > _logLines) _scrolling = _logBarGlobal.contains(Input.mouseFlashX, Input.mouseFlashY);
  491. }
  492. /** @private Updates log text scrolling. */
  493. private function updateScrolling():void
  494. {
  495. _scrolling = Input.mouseDown;
  496. _logScroll = FP.scaleClamp(Input.mouseFlashY, _logBarGlobal.y, _logBarGlobal.bottom, 0, 1);
  497. updateLog();
  498. }
  499. /** @private Moves Entities with the arrow keys. */
  500. private function updateKeyMoving():void
  501. {
  502. FP.point.x = (Input.pressed(Key.RIGHT) ? 1 : 0) - (Input.pressed(Key.LEFT) ? 1 : 0);
  503. FP.point.y = (Input.pressed(Key.DOWN) ? 1 : 0) - (Input.pressed(Key.UP) ? 1 : 0);
  504. if (FP.point.x != 0 || FP.point.y != 0) moveSelected(FP.point.x, FP.point.y);
  505. }
  506. /** @private Pans the camera with the arrow keys. */
  507. private function updateKeyPanning():void
  508. {
  509. FP.point.x = (Input.check(Key.RIGHT) ? 1 : 0) - (Input.check(Key.LEFT) ? 1 : 0);
  510. FP.point.y = (Input.check(Key.DOWN) ? 1 : 0) - (Input.check(Key.UP) ? 1 : 0);
  511. if (FP.point.x != 0 || FP.point.y != 0) panCamera(FP.point.x, FP.point.y);
  512. }
  513. /** @private Update the Entity list information. */
  514. private function updateEntityLists(fetchList:Boolean = true):void
  515. {
  516. // If the list should be re-populated.
  517. if (fetchList)
  518. {
  519. ENTITY_LIST.length = 0;
  520. FP.world.getAll(ENTITY_LIST);
  521. }
  522. // Update the list of Entities on screen.
  523. SCREEN_LIST.length = 0;
  524. for each (var e:Entity in ENTITY_LIST)
  525. {
  526. if (e.collideRect(e.x, e.y, FP.camera.x, FP.camera.y, FP.width, FP.height))
  527. SCREEN_LIST.push(e);
  528. }
  529. }
  530. /** @private Renders the Entities positions and hitboxes. */
  531. private function renderEntities():void
  532. {
  533. // If debug mode is on.
  534. _entScreen.visible = _debug;
  535. if (_debug)
  536. {
  537. var g:Graphics = _entScreen.graphics,
  538. sx:Number = FP.screen.scaleX * FP.screen.scale,
  539. sy:Number = FP.screen.scaleY * FP.screen.scale;
  540. g.clear();
  541. for each (var e:Entity in SCREEN_LIST)
  542. {
  543. // If the Entity is not selected.
  544. if (SELECT_LIST.indexOf(e) < 0)
  545. {
  546. // Draw the normal hitbox and position.
  547. if (e.width && e.height)
  548. {
  549. g.lineStyle(1, 0xFF0000);
  550. g.drawRect((e.x - e.originX - FP.camera.x) * sx, (e.y - e.originY - FP.camera.y) * sy, e.width * sx, e.height * sy);
  551. if (e.mask) e.mask.renderDebug(g);
  552. }
  553. g.lineStyle(1, 0x00FF00);
  554. g.drawRect((e.x - FP.camera.x) * sx - 3, (e.y - FP.camera.y) * sy - 3, 6, 6);
  555. }
  556. else
  557. {
  558. // Draw the selected hitbox and position.
  559. if (e.width && e.height)
  560. {
  561. g.lineStyle(1, 0xFFFFFF);
  562. g.drawRect((e.x - e.originX - FP.camera.x) * sx, (e.y - e.originY - FP.camera.y) * sy, e.width * sx, e.height * sy);
  563. if (e.mask) e.mask.renderDebug(g);
  564. }
  565. g.lineStyle(1, 0xFFFFFF);
  566. g.drawRect((e.x - FP.camera.x) * sx - 3, (e.y - FP.camera.y) * sy - 3, 6, 6);
  567. }
  568. }
  569. }
  570. }
  571. /** @private Updates the log window. */
  572. private function updateLog():void
  573. {
  574. // If the console is paused.
  575. if (_paused)
  576. {
  577. // Draw the log panel.
  578. _logRead.y = 40;
  579. _logRead.graphics.clear();
  580. _logRead.graphics.beginFill(0, .75);
  581. _logRead.graphics.drawRoundRectComplex(0, 0, _logReadText0.width, 20, 0, 20, 0, 0);
  582. _logRead.graphics.drawRect(0, 20, width, _logHeight);
  583. // Draw the log scrollbar.
  584. _logRead.graphics.beginFill(0x202020, 1);
  585. _logRead.graphics.drawRoundRectComplex(_logBar.x, _logBar.y, _logBar.width, _logBar.height, 8, 8, 8, 8);
  586. // If the log has more lines than the display limit.
  587. if (LOG.length > _logLines)
  588. {
  589. // Draw the log scrollbar handle.
  590. _logRead.graphics.beginFill(0xFFFFFF, 1);
  591. var y:uint = _logBar.y + 2 + (_logBar.height - 16) * _logScroll;
  592. _logRead.graphics.drawRoundRectComplex(_logBar.x + 2, y, 12, 12, 6, 6, 6, 6);
  593. }
  594. // Display the log text lines.
  595. if (LOG.length)
  596. {
  597. var i:int = LOG.length > _logLines ? Math.round((LOG.length - _logLines) * _logScroll) : 0,
  598. n:int = i + Math.min(_logLines, LOG.length),
  599. s:String = "";
  600. while (i < n) s += LOG[i ++] + "\n";
  601. _logReadText1.text = s;
  602. }
  603. else _logReadText1.text = "";
  604. // Indent the text for the scrollbar and size it to the log panel.
  605. _logReadText1.height = _logHeight;
  606. _logReadText1.x = 32;
  607. _logReadText1.y = 24;
  608. // Make text selectable in paused mode.
  609. _fpsReadText.selectable = true;
  610. _fpsInfoText0.selectable = true;
  611. _fpsInfoText1.selectable = true;
  612. _memReadText.selectable = true;
  613. _entReadText.selectable = true;
  614. _debReadText1.selectable = true;
  615. }
  616. else
  617. {
  618. // Draw the single-line log panel.
  619. _logRead.y = height - 40;
  620. _logReadText1.height = 20;
  621. _logRead.graphics.clear();
  622. _logRead.graphics.beginFill(0, .75);
  623. _logRead.graphics.drawRoundRectComplex(0, 0, _logReadText0.width, 20, 0, 20, 0, 0);
  624. _logRead.graphics.drawRect(0, 20, width, 20);
  625. // Draw the single-line log text with the latests logged text.
  626. _logReadText1.text = LOG.length ? LOG[LOG.length - 1] : "";
  627. _logReadText1.x = 2;
  628. _logReadText1.y = 21;
  629. // Make text non-selectable while running.
  630. _logReadText1.selectable = false;
  631. _fpsReadText.selectable = false;
  632. _fpsInfoText0.selectable = false;
  633. _fpsInfoText1.selectable = false;
  634. _memReadText.selectable = false;
  635. _entReadText.selectable = false;
  636. _debReadText0.selectable = false;
  637. _debReadText1.selectable = false;
  638. }
  639. }
  640. /** @private Update the FPS/frame timing panel text. */
  641. private function updateFPSRead():void
  642. {
  643. _fpsReadText.text = "FPS: " + FP.frameRate.toFixed();
  644. _fpsInfoText0.text =
  645. "Update: " + String(FP._updateTime) + "ms\n" +
  646. "Render: " + String(FP._renderTime) + "ms";
  647. _fpsInfoText1.text =
  648. "Game: " + String(FP._gameTime) + "ms\n" +
  649. "Flash: " + String(FP._flashTime) + "ms";
  650. _memReadText.text = "MEM: " + Number(System.totalMemory/1024/1024).toFixed(2) + "MB";
  651. }
  652. /** @private Update the debug panel text. */
  653. private function updateDebugRead():void
  654. {
  655. // Find out the screen size and set the text.
  656. var big:Boolean = width >= 480;
  657. // Update the Debug read text.
  658. var s:String =
  659. "Mouse: " + String(FP.world.mouseX) + ", " + String(FP.world.mouseY) +
  660. "\nCamera: " + String(FP.camera.x) + ", " + String(FP.camera.y);
  661. if (SELECT_LIST.length)
  662. {
  663. if (SELECT_LIST.length > 1)
  664. {
  665. s += "\n\nSelected: " + String(SELECT_LIST.length);
  666. }
  667. else
  668. {
  669. var e:Entity = SELECT_LIST[0];
  670. s += "\n\n- " + String(e) + " -\n";
  671. for each (var i:String in WATCH_LIST)
  672. {
  673. if (e.hasOwnProperty(i)) s += "\n" + i + ": " + e[i];
  674. }
  675. }
  676. }
  677. // Set the text and format.
  678. _debReadText1.text = s;
  679. _debReadText1.setTextFormat(format(big ? 16 : 8));
  680. _debReadText1.width = Math.max(_debReadText1.textWidth + 4, _debReadText0.width);
  681. _debReadText1.height = _debReadText1.y + _debReadText1.textHeight + 4;
  682. // The debug panel.
  683. _debRead.y = int(height - _debReadText1.height);
  684. _debRead.graphics.clear();
  685. _debRead.graphics.beginFill(0, .75);
  686. _debRead.graphics.drawRoundRectComplex(0, 0, _debReadText0.width, 20, 0, 20, 0, 0);
  687. _debRead.graphics.drawRoundRectComplex(0, 20, _debReadText1.width + 20, height - _debRead.y - 20, 0, 20, 0, 0);
  688. }
  689. /** @private Updates the Entity count text. */
  690. private function updateEntityCount():void
  691. {
  692. _entReadText.text = String(FP.world.count) + " Entities";
  693. }
  694. /** @private Updates the Button panel. */
  695. private function updateButtons():void
  696. {
  697. // Button visibility.
  698. _butRead.x = _fpsInfo.x + _fpsInfo.width + int((_entRead.x - (_fpsInfo.x + _fpsInfo.width)) / 2) - 30;
  699. _butDebug.visible = !_debug;
  700. _butOutput.visible = _debug;
  701. _butPlay.visible = FP.engine.paused;
  702. _butPause.visible = !FP.engine.paused;
  703. // Debug/Output button.
  704. if (_butDebug.bitmapData.rect.contains(_butDebug.mouseX, _butDebug.mouseY))
  705. {
  706. _butDebug.alpha = _butOutput.alpha = 1;
  707. if (Input.mousePressed) debug = !_debug;
  708. }
  709. else _butDebug.alpha = _butOutput.alpha = .5;
  710. // Play/Pause button.
  711. if (_butPlay.bitmapData.rect.contains(_butPlay.mouseX, _butPlay.mouseY))
  712. {
  713. _butPlay.alpha = _butPause.alpha = 1;
  714. if (Input.mousePressed)
  715. {
  716. FP.engine.paused = !FP.engine.paused;
  717. renderEntities();
  718. }
  719. }
  720. else _butPlay.alpha = _butPause.alpha = .5;
  721. // Frame step button.
  722. if (_butStep.bitmapData.rect.contains(_butStep.mouseX, _butStep.mouseY))
  723. {
  724. _butStep.alpha = 1;
  725. if (Input.mousePressed) stepFrame();
  726. }
  727. else _butStep.alpha = .5;
  728. }
  729. /** @private Gets a TextFormat object with the formatting. */
  730. private function format(size:uint = 16, color:uint = 0xFFFFFF, align:String = "left"):TextFormat
  731. {
  732. _format.size = size;
  733. _format.color = color;
  734. _format.align = align;
  735. return _format;
  736. }
  737. /**
  738. * Get the unscaled screen size for the Console.
  739. */
  740. private function get width():uint { return FP.width * FP.screen.scaleX * FP.screen.scale; }
  741. private function get height():uint { return FP.height * FP.screen.scaleY * FP.screen.scale; }
  742. // Console state information.
  743. /** @private */ private var _enabled:Boolean;
  744. /** @private */ private var _paused:Boolean;
  745. /** @private */ private var _debug:Boolean;
  746. /** @private */ private var _scrolling:Boolean;
  747. /** @private */ private var _selecting:Boolean;
  748. /** @private */ private var _dragging:Boolean;
  749. /** @private */ private var _panning:Boolean;
  750. // Console display objects.
  751. /** @private */ private var _sprite:Sprite = new Sprite;
  752. /** @private */ private var _format:TextFormat = new TextFormat("default");
  753. /** @private */ private var _back:Bitmap = new Bitmap;
  754. // FPS panel information.
  755. /** @private */ private var _fpsRead:Sprite = new Sprite;
  756. /** @private */ private var _fpsReadText:TextField = new TextField;
  757. /** @private */ private var _fpsInfo:Sprite = new Sprite;
  758. /** @private */ private var _fpsInfoText0:TextField = new TextField;
  759. /** @private */ private var _fpsInfoText1:TextField = new TextField;
  760. /** @private */ private var _memReadText:TextField = new TextField;
  761. // Output panel information.
  762. /** @private */ private var _logRead:Sprite = new Sprite;
  763. /** @private */ private var _logReadText0:TextField = new TextField;
  764. /** @private */ private var _logReadText1:TextField = new TextField;
  765. /** @private */ private var _logHeight:uint;
  766. /** @private */ private var _logBar:Rectangle;
  767. /** @private */ private var _logBarGlobal:Rectangle;
  768. /** @private */ private var _logScroll:Number = 0;
  769. // Entity count panel information.
  770. /** @private */ private var _entRead:Sprite = new Sprite;
  771. /** @private */ private var _entReadText:TextField = new TextField;
  772. // Debug panel information.
  773. /** @private */ private var _debRead:Sprite = new Sprite;
  774. /** @private */ private var _debReadText0:TextField = new TextField;
  775. /** @private */ private var _debReadText1:TextField = new TextField;
  776. // Button panel information
  777. /** @private */ private var _butRead:Sprite = new Sprite;
  778. /** @private */ private var _butDebug:Bitmap;
  779. /** @private */ private var _butOutput:Bitmap;
  780. /** @private */ private var _butPlay:Bitmap;
  781. /** @private */ private var _butPause:Bitmap;
  782. /** @private */ private var _butStep:Bitmap;
  783. // Entity selection information.
  784. /** @private */ private var _entScreen:Sprite = new Sprite;
  785. /** @private */ private var _entSelect:Sprite = new Sprite;
  786. /** @private */ private var _entRect:Rectangle = new Rectangle;
  787. // Log information.
  788. /** @private */ private var _logLines:uint = 33;
  789. /** @private */ private const LOG:Vector.<String> = new Vector.<String>;
  790. // Entity lists.
  791. /** @private */ private const ENTITY_LIST:Vector.<Entity> = new Vector.<Entity>;
  792. /** @private */ private const SCREEN_LIST:Vector.<Entity> = new Vector.<Entity>;
  793. /** @private */ private const SELECT_LIST:Vector.<Entity> = new Vector.<Entity>;
  794. // Watch information.
  795. /** @private */ private const WATCH_LIST:Vector.<String> = Vector.<String>(["x", "y"]);
  796. // Embedded assets.
  797. [Embed(source = 'console_logo.png')] private const CONSOLE_LOGO:Class;
  798. [Embed(source = 'console_debug.png')] private const CONSOLE_DEBUG:Class;
  799. [Embed(source = 'console_output.png')] private const CONSOLE_OUTPUT:Class;
  800. [Embed(source = 'console_play.png')] private const CONSOLE_PLAY:Class;
  801. [Embed(source = 'console_pause.png')] private const CONSOLE_PAUSE:Class;
  802. [Embed(source = 'console_step.png')] private const CONSOLE_STEP:Class;
  803. // Reference the Text class so we can access its embedded font
  804. private static var textRef:Text;
  805. }
  806. }