PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/net/flashpunk/debug/Console.as

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