PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/net/flashpunk/debug/Console.as

https://github.com/Draknek/Terrible-Tiny-Traps
ActionScript | 895 lines | 664 code | 89 blank | 142 comment | 106 complexity | 9cb7a975804a73ee4af4c74065d1398f 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.display.Stage;
  9. import flash.geom.ColorTransform;
  10. import flash.geom.Rectangle;
  11. import flash.text.TextField;
  12. import flash.text.TextFormat;
  13. import flash.system.System;
  14. import net.flashpunk.Entity;
  15. import net.flashpunk.FP;
  16. import net.flashpunk.utils.Draw;
  17. import net.flashpunk.utils.Input;
  18. import net.flashpunk.utils.Key;
  19. /**
  20. * FlashPunk debug console; can use to log information or pause the game and view/move Entities and step the frame.
  21. */
  22. public class Console
  23. {
  24. /**
  25. * The key used to toggle the Console on/off. Tilde (~) by default.
  26. */
  27. public var toggleKey:uint = 192;
  28. /**
  29. * Constructor.
  30. */
  31. public function Console()
  32. {
  33. Input.define("_ARROWS", Key.RIGHT, Key.LEFT, Key.DOWN, Key.UP);
  34. }
  35. /**
  36. * Logs data to the console.
  37. * @param ...data The data parameters to log, can be variables, objects, etc. Parameters will be separated by a space (" ").
  38. */
  39. public function log(...data):void
  40. {
  41. var s:String;
  42. if (data.length > 1)
  43. {
  44. s = "";
  45. var i:int = 0;
  46. while (i < data.length)
  47. {
  48. if (i > 0) s += " ";
  49. s += data[i ++].toString();
  50. }
  51. }
  52. else s = data[0].toString();
  53. if (s.indexOf("\n") >= 0)
  54. {
  55. var a:Array = s.split("\n");
  56. for each (s in a) LOG.push(s);
  57. }
  58. else LOG.push(s);
  59. if (_enabled && _sprite.visible) updateLog();
  60. }
  61. /**
  62. * Adds properties to watch in the console's debug panel.
  63. * @param ...properties The properties (strings) to watch.
  64. */
  65. public function watch(...properties):void
  66. {
  67. var i:String;
  68. if (properties.length > 1)
  69. {
  70. for each (i in properties) WATCH_LIST.push(i);
  71. }
  72. else if (properties[0] is Array || properties[0] is Vector.<*>)
  73. {
  74. for each (i in properties[0]) WATCH_LIST.push(i);
  75. }
  76. else WATCH_LIST.push(properties[0]);
  77. }
  78. /**
  79. * Enables the console.
  80. */
  81. public function enable():void
  82. {
  83. // Quit if the console is already enabled.
  84. if (_enabled) return;
  85. // Enable it and add the Sprite to the stage.
  86. _enabled = true;
  87. FP.engine.addChild(_sprite);
  88. // Used to determine some text sizing.
  89. var big:Boolean = width >= 480;
  90. // The transparent FlashPunk logo overlay bitmap.
  91. _sprite.addChild(_back);
  92. _back.bitmapData = new BitmapData(width, height, true, 0xFFFFFFFF);
  93. var b:BitmapData = (new CONSOLE_LOGO).bitmapData;
  94. FP.matrix.identity();
  95. FP.matrix.tx = Math.max((_back.bitmapData.width - b.width) / 2, 0);
  96. FP.matrix.ty = Math.max((_back.bitmapData.height - b.height) / 2, 0);
  97. FP.matrix.scale(Math.min(width / _back.bitmapData.width, 1), Math.min(height / _back.bitmapData.height, 1));
  98. _back.bitmapData.draw(b, FP.matrix, null, BlendMode.MULTIPLY);
  99. _back.bitmapData.draw(_back.bitmapData, null, null, BlendMode.INVERT);
  100. _back.bitmapData.colorTransform(_back.bitmapData.rect, new ColorTransform(1, 1, 1, 0.5));
  101. // The entity and selection sprites.
  102. _sprite.addChild(_entScreen);
  103. _entScreen.addChild(_entSelect);
  104. // The entity count text.
  105. _sprite.addChild(_entRead);
  106. _entRead.addChild(_entReadText);
  107. _entReadText.defaultTextFormat = format(16, 0xFFFFFF, "right");
  108. _entReadText.embedFonts = true;
  109. _entReadText.width = 100;
  110. _entReadText.height = 20;
  111. _entRead.x = width - _entReadText.width;
  112. // The entity count panel.
  113. _entRead.graphics.clear();
  114. _entRead.graphics.beginFill(0, .5);
  115. _entRead.graphics.drawRoundRectComplex(0, 0, _entReadText.width, 20, 0, 0, 20, 0);
  116. // The FPS text.
  117. _sprite.addChild(_fpsRead);
  118. _fpsRead.addChild(_fpsReadText);
  119. _fpsReadText.defaultTextFormat = format(16);
  120. _fpsReadText.embedFonts = true;
  121. _fpsReadText.width = 70;
  122. _fpsReadText.height = 20;
  123. _fpsReadText.x = 2;
  124. _fpsReadText.y = 1;
  125. // The FPS and frame timing panel.
  126. _fpsRead.graphics.clear();
  127. _fpsRead.graphics.beginFill(0, .75);
  128. _fpsRead.graphics.drawRoundRectComplex(0, 0, big ? 320 : 160, 20, 0, 0, 0, 20);
  129. // The frame timing text.
  130. if (big) _sprite.addChild(_fpsInfo);
  131. _fpsInfo.addChild(_fpsInfoText0);
  132. _fpsInfo.addChild(_fpsInfoText1);
  133. _fpsInfoText0.defaultTextFormat = format(8, 0xAAAAAA);
  134. _fpsInfoText1.defaultTextFormat = format(8, 0xAAAAAA);
  135. _fpsInfoText0.embedFonts = true;
  136. _fpsInfoText1.embedFonts = true;
  137. _fpsInfoText0.width = _fpsInfoText1.width = 60;
  138. _fpsInfoText0.height = _fpsInfoText1.height = 20;
  139. _fpsInfo.x = 75;
  140. _fpsInfoText1.x = 60;
  141. // The memory usage
  142. _fpsRead.addChild(_memReadText);
  143. _memReadText.defaultTextFormat = format(16);
  144. _memReadText.embedFonts = true;
  145. _memReadText.width = 110;
  146. _memReadText.height = 20;
  147. _memReadText.x = _fpsInfo.x + _fpsInfo.width + 5;
  148. _memReadText.y = 1;
  149. // The output log text.
  150. _sprite.addChild(_logRead);
  151. _logRead.addChild(_logReadText0);
  152. _logRead.addChild(_logReadText1);
  153. _logReadText0.defaultTextFormat = format(16, 0xFFFFFF);
  154. _logReadText1.defaultTextFormat = format(big ? 16 : 8, 0xFFFFFF);
  155. _logReadText0.embedFonts = true;
  156. _logReadText1.embedFonts = true;
  157. _logReadText0.selectable = false;
  158. _logReadText0.width = 80;
  159. _logReadText0.height = 20;
  160. _logReadText1.width = width;
  161. _logReadText0.x = 2;
  162. _logReadText0.y = 3;
  163. _logReadText0.text = "OUTPUT:";
  164. _logHeight = height - 60;
  165. _logBar = new Rectangle(8, 24, 16, _logHeight - 8);
  166. _logBarGlobal = _logBar.clone();
  167. _logBarGlobal.y += 40;
  168. _logLines = _logHeight / (big ? 16.5 : 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 Entitites 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 Entitites 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 Entitites 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. }
  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. }
  564. g.lineStyle(1, 0xFFFFFF);
  565. g.drawRect((e.x - FP.camera.x) * sx - 3, (e.y - FP.camera.y) * sy - 3, 6, 6);
  566. }
  567. }
  568. }
  569. }
  570. /** @private Updates the log window. */
  571. private function updateLog():void
  572. {
  573. // If the console is paused.
  574. if (_paused)
  575. {
  576. // Draw the log panel.
  577. _logRead.y = 40;
  578. _logRead.graphics.clear();
  579. _logRead.graphics.beginFill(0, .75);
  580. _logRead.graphics.drawRoundRectComplex(0, 0, _logReadText0.width, 20, 0, 20, 0, 0);
  581. _logRead.graphics.drawRect(0, 20, width, _logHeight);
  582. // Draw the log scrollbar.
  583. _logRead.graphics.beginFill(0x202020, 1);
  584. _logRead.graphics.drawRoundRectComplex(_logBar.x, _logBar.y, _logBar.width, _logBar.height, 8, 8, 8, 8);
  585. // If the log has more lines than the display limit.
  586. if (LOG.length > _logLines)
  587. {
  588. // Draw the log scrollbar handle.
  589. _logRead.graphics.beginFill(0xFFFFFF, 1);
  590. var h:uint = FP.clamp(_logBar.height * (_logLines / LOG.length), 12, _logBar.height - 4),
  591. 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("console");
  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. /** @private */ private var _debWidth:uint;
  777. // Button panel information
  778. /** @private */ private var _butRead:Sprite = new Sprite;
  779. /** @private */ private var _butDebug:Bitmap;
  780. /** @private */ private var _butOutput:Bitmap;
  781. /** @private */ private var _butPlay:Bitmap;
  782. /** @private */ private var _butPause:Bitmap;
  783. /** @private */ private var _butStep:Bitmap;
  784. // Entity selection information.
  785. /** @private */ private var _entScreen:Sprite = new Sprite;
  786. /** @private */ private var _entSelect:Sprite = new Sprite;
  787. /** @private */ private var _entRect:Rectangle = new Rectangle;
  788. // Log information.
  789. /** @private */ private var _logLines:uint = 33;
  790. /** @private */ private const LOG:Vector.<String> = new Vector.<String>;
  791. // Entity lists.
  792. /** @private */ private const ENTITY_LIST:Vector.<Entity> = new Vector.<Entity>;
  793. /** @private */ private const SCREEN_LIST:Vector.<Entity> = new Vector.<Entity>;
  794. /** @private */ private const SELECT_LIST:Vector.<Entity> = new Vector.<Entity>;
  795. // Watch information.
  796. /** @private */ private const WATCH_LIST:Vector.<String> = Vector.<String>(["x", "y"]);
  797. // Embedded assets.
  798. [Embed(source = '../graphics/04B_03__.TTF', fontFamily = 'console')] private const FONT_SMALL:Class;
  799. [Embed(source = 'console_logo.png')] private const CONSOLE_LOGO:Class;
  800. [Embed(source = 'console_debug.png')] private const CONSOLE_DEBUG:Class;
  801. [Embed(source = 'console_output.png')] private const CONSOLE_OUTPUT:Class;
  802. [Embed(source = 'console_play.png')] private const CONSOLE_PLAY:Class;
  803. [Embed(source = 'console_pause.png')] private const CONSOLE_PAUSE:Class;
  804. [Embed(source = 'console_step.png')] private const CONSOLE_STEP:Class;
  805. }
  806. }