PageRenderTime 57ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/net/flashpunk/debug/Console.as

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