PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/br/dcoder/console/Console.as

http://as3console.googlecode.com/
ActionScript | 817 lines | 552 code | 123 blank | 142 comment | 85 complexity | 391a345097002efa8ae6466594ad1b13 MD5 | raw file
Possible License(s): LGPL-3.0
  1. // AS3Console Copyright 2011 Lucas Teixeira (aka Disturbed Coder)
  2. // Project page: http://code.google.com/p/as3console/
  3. //
  4. // This software is distribuited under the terms of the GNU Lesser Public License.
  5. // See license.txt for more information.
  6. package br.dcoder.console
  7. {
  8. import flash.display.DisplayObjectContainer;
  9. import flash.display.Sprite;
  10. import flash.display.Stage;
  11. import flash.events.Event;
  12. import flash.events.EventDispatcher;
  13. import flash.events.KeyboardEvent;
  14. import flash.events.MouseEvent;
  15. import flash.external.ExternalInterface;
  16. import flash.geom.Point;
  17. import flash.geom.Rectangle;
  18. import flash.system.System;
  19. import flash.text.TextField;
  20. import flash.text.TextFieldType;
  21. import flash.text.TextFormat;
  22. import flash.text.TextFormatAlign;
  23. import flash.utils.getTimer;
  24. public class Console extends EventDispatcher
  25. {
  26. /**
  27. * Console version.
  28. */
  29. public static const VERSION:String = "0.2.2";
  30. /**
  31. * Max characters stored in console text area. Can be modified at runtime.
  32. */
  33. public static var MAX_CHARACTERS:uint = 100000;
  34. /**
  35. * Max line length before automatic breaking. Can be modified at runtime.
  36. * Note that modifying this attribute at runtime won't change text previously written.
  37. */
  38. public static var MAX_LINE_LENGTH:uint = 1000;
  39. /**
  40. * Max input navigation history. Can be modified at runtime.
  41. * You can access input history using up/down arrow keys when input text field is selected.
  42. */
  43. public static var MAX_INPUT_HISTORY:uint = 15;
  44. /**
  45. * Print elapsed milliseconds since the flash virtual machine started. Can be modified at runtime.
  46. */
  47. public static var PRINT_TIMER:Boolean = false;
  48. /**
  49. * Print output in flash trace window. Can be modified at runtime.
  50. */
  51. public static var TRACE_ECHO:Boolean = false;
  52. /**
  53. * Print output in firebug window. Can be modified at runtime.
  54. */
  55. public static var FIREBUG_ECHO:Boolean = false;
  56. //
  57. // internal data
  58. //
  59. private static const FONT_SIZE:uint = 12;
  60. private static const CAPTION_TEXT:String = "AS3Console";
  61. private static const CAPTION_BAR_HEIGHT:uint = 20;
  62. private static const MARKER_FIELD_WIDTH:uint = 12;
  63. private static const DEFAULT_ALPHA:Number = 0.75;
  64. private static var release:Boolean;
  65. private static var instance:Console = null;
  66. private var stage:Stage;
  67. private var assetFactory:AssetFactory;
  68. private var rect:Rectangle;
  69. private var container:Sprite;
  70. private var captionBar:Sprite;
  71. private var captionTextField:TextField;
  72. private var minimizeButton:Button;
  73. private var content:Sprite;
  74. private var textArea:TextField;
  75. private var markerField:TextField;
  76. private var inputField:TextField;
  77. private var hScrollBar:ScrollBar;
  78. private var vScrollBar:ScrollBar;
  79. private var resizeArea:Sprite;
  80. private var resizeOffset:Point;
  81. private var resizing:Boolean;
  82. private var shortcutKeys:Array, shortcutStates:Array;
  83. private var shortcutUseAlt:Boolean, shortcutUseCtrl:Boolean, shortcutUseShift:Boolean;
  84. private var inputHistory:Array;
  85. private var historyIndex:int;
  86. //
  87. // static methods
  88. //
  89. /**
  90. * Create console singleton instance. Should be called once.
  91. * If assetFactory parameter is null, default AssetFactory is created.
  92. * If release parameter is true, an empty console is created to avoid overhead.
  93. * @param stage Reference to application stage object.
  94. * @param assetFactory AssetFactory instance used to set component appearance.
  95. * @param release Use to create release versions and remove console overhead.
  96. * @return Console singleton instance.
  97. */
  98. public static function create(stage:Stage, assetFactory:AssetFactory = null, release:Boolean = false):Console
  99. {
  100. Console.release = release;
  101. if (release)
  102. instance = new DummyConsole(stage);
  103. else
  104. instance = new Console(stage, !assetFactory ? new AssetFactory() : assetFactory);
  105. return instance;
  106. }
  107. /**
  108. * Check if console is running release version.
  109. * @return Return true if is release version.
  110. */
  111. public static function isRelease():Boolean
  112. {
  113. return release;
  114. }
  115. /**
  116. * Return console singleton instance.
  117. * @return Console singleton instance.
  118. */
  119. public static function getInstance():Console
  120. {
  121. return instance;
  122. }
  123. private static function getString(info:Object):String {
  124. if (info == null)
  125. return "(null)";
  126. else if (info is String)
  127. return info as String;
  128. return info.toString();
  129. }
  130. //
  131. // constructor
  132. //
  133. /**
  134. * @private
  135. */
  136. public function Console(stage:Stage, assetFactory:AssetFactory)
  137. {
  138. super();
  139. if (release)
  140. return;
  141. this.stage = stage;
  142. this.assetFactory = assetFactory;
  143. var tFormat:TextFormat;
  144. rect = new Rectangle(50, 50, 250, 250);
  145. container = new Sprite();
  146. stage.addChild(container);
  147. // caption bar
  148. captionBar = new Sprite();
  149. captionBar.addEventListener(MouseEvent.MOUSE_UP, onCaptionBarMouseUp);
  150. captionBar.addEventListener(MouseEvent.MOUSE_DOWN, onCaptionBarMouseDown);
  151. captionBar.buttonMode = true;
  152. container.addChild(captionBar);
  153. captionTextField = new TextField();
  154. captionTextField.text = CAPTION_TEXT + " " + VERSION;
  155. captionTextField.height = CAPTION_BAR_HEIGHT;
  156. captionTextField.mouseEnabled = false;
  157. captionTextField.selectable = false;
  158. captionTextField.y = 2;
  159. captionBar.addChild(captionTextField);
  160. tFormat = new TextFormat();
  161. tFormat.align = TextFormatAlign.CENTER;
  162. tFormat.bold = true;
  163. tFormat.color = assetFactory.getButtonForegroundColor();
  164. tFormat.font = "_typewriter";
  165. tFormat.size = 14;
  166. captionTextField.setTextFormat(tFormat);
  167. captionTextField.defaultTextFormat = tFormat;
  168. minimizeButton = new Button(Button.ARROW_ICON, assetFactory);
  169. minimizeButton.draw(CAPTION_BAR_HEIGHT - CAPTION_BAR_HEIGHT * 0.3, CAPTION_BAR_HEIGHT - CAPTION_BAR_HEIGHT * 0.3);
  170. minimizeButton.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void
  171. {
  172. if (isMaximized())
  173. {
  174. minimize();
  175. }
  176. else
  177. {
  178. maximize();
  179. focusInputField();
  180. }
  181. });
  182. captionBar.addChild(minimizeButton);
  183. // content
  184. content = new Sprite();
  185. content.y = CAPTION_BAR_HEIGHT;
  186. container.addChild(content);
  187. // text area
  188. textArea = new TextField();
  189. textArea.multiline = true;
  190. textArea.addEventListener(MouseEvent.MOUSE_WHEEL, onTextAreaMouseWheel);
  191. content.addChild(textArea);
  192. tFormat = new TextFormat();
  193. tFormat.color = assetFactory.getButtonForegroundColor();
  194. tFormat.font = "_typewriter";
  195. tFormat.size = FONT_SIZE;
  196. textArea.defaultTextFormat = tFormat;
  197. // marker field
  198. markerField = new TextField();
  199. markerField.background = true;
  200. markerField.backgroundColor = assetFactory.getButtonBackgroundColor();
  201. markerField.selectable = false;
  202. markerField.text = ">";
  203. content.addChild(markerField);
  204. tFormat = new TextFormat();
  205. tFormat.color = assetFactory.getButtonForegroundColor();
  206. tFormat.font = "_typewriter";
  207. tFormat.size = FONT_SIZE;
  208. markerField.setTextFormat(tFormat);
  209. // input field
  210. inputField = new TextField();
  211. inputField.background = true;
  212. inputField.backgroundColor = assetFactory.getButtonBackgroundColor();
  213. inputField.type = TextFieldType.INPUT;
  214. inputField.addEventListener(KeyboardEvent.KEY_DOWN, onInputFieldKeyDown);
  215. content.addChild(inputField);
  216. tFormat = new TextFormat();
  217. tFormat.color = assetFactory.getButtonForegroundColor();
  218. tFormat.font = "_typewriter";
  219. tFormat.size = FONT_SIZE;
  220. inputField.defaultTextFormat = tFormat;
  221. // scroll bars
  222. hScrollBar = new ScrollBar(assetFactory, ScrollBar.HORIZONTAL, 250);
  223. hScrollBar.addEventListener(Event.CHANGE, scrollBarChange);
  224. hScrollBar.setMaxValue(0);
  225. content.addChild(hScrollBar);
  226. vScrollBar = new ScrollBar(assetFactory, ScrollBar.VERTICAL, 250);
  227. vScrollBar.addEventListener(Event.CHANGE, scrollBarChange);
  228. vScrollBar.setMaxValue(0);
  229. content.addChild(vScrollBar);
  230. // resize area
  231. resizeArea = new Sprite();
  232. resizeArea.buttonMode = true;
  233. resizeArea.addEventListener(MouseEvent.MOUSE_DOWN, onResizeAreaMouseDown);
  234. content.addChild(resizeArea);
  235. resizeOffset = new Point();
  236. resizing = false;
  237. // shortcut
  238. shortcutKeys = [ "m" ];
  239. shortcutStates = [ false ];
  240. shortcutUseAlt = false;
  241. shortcutUseCtrl = true;
  242. shortcutUseShift = false;
  243. // input history
  244. inputHistory = new Array();
  245. historyIndex = -1;
  246. // stage events
  247. stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
  248. stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
  249. stage.addEventListener(MouseEvent.MOUSE_MOVE, stageMouseMove);
  250. stage.addEventListener(MouseEvent.MOUSE_UP, stageMouseUp);
  251. // update component
  252. alpha = DEFAULT_ALPHA;
  253. update();
  254. }
  255. //
  256. // public interface
  257. //
  258. /**
  259. * Console position and dimension represented by a Rectangle object.
  260. */
  261. public function get area():Rectangle
  262. {
  263. return rect;
  264. }
  265. public function set area(rect:Rectangle):void
  266. {
  267. this.rect = rect;
  268. update();
  269. }
  270. /**
  271. * Console transparecy.
  272. */
  273. public function get alpha():Number
  274. {
  275. return container.alpha;
  276. }
  277. public function set alpha(value:Number):void
  278. {
  279. container.alpha = value;
  280. }
  281. /**
  282. * Console caption text.
  283. */
  284. public function get caption():String
  285. {
  286. return captionTextField.text;
  287. }
  288. public function set caption(text:String):void
  289. {
  290. captionTextField.text = getString(text);
  291. }
  292. /**
  293. * Update console gaphical interface.
  294. * It's automatically called when console area has changed.
  295. */
  296. public function update():void
  297. {
  298. container.x = rect.left;
  299. container.y = rect.top;
  300. inputField.width = rect.width - assetFactory.getButtonContainerSize() - MARKER_FIELD_WIDTH;
  301. inputField.height = 20;
  302. inputField.x = MARKER_FIELD_WIDTH + 1;
  303. inputField.y = rect.height - CAPTION_BAR_HEIGHT - assetFactory.getButtonContainerSize() - inputField.height;
  304. markerField.x = 1;
  305. markerField.y = inputField.y;
  306. markerField.width = MARKER_FIELD_WIDTH;
  307. markerField.height = 20;
  308. captionTextField.width = rect.width;
  309. minimizeButton.x = rect.width - CAPTION_BAR_HEIGHT + CAPTION_BAR_HEIGHT * 0.15;
  310. minimizeButton.y = CAPTION_BAR_HEIGHT * 0.15;
  311. textArea.x = textArea.y = 1;
  312. textArea.width = rect.width - assetFactory.getButtonContainerSize() - 1;
  313. textArea.height = rect.height - CAPTION_BAR_HEIGHT - assetFactory.getButtonContainerSize() - inputField.height - 1;
  314. hScrollBar.x = 0;
  315. hScrollBar.y = rect.height - CAPTION_BAR_HEIGHT - assetFactory.getButtonContainerSize();
  316. hScrollBar.setLength(rect.width - assetFactory.getButtonContainerSize() + 1);
  317. hScrollBar.setMaxValue(textArea.maxScrollH == 0 ? 0 : textArea.maxScrollH - 1);
  318. hScrollBar.draw();
  319. if (hScrollBar.getValue() > hScrollBar.getMaxValue())
  320. hScrollBar.toMaxValue();
  321. vScrollBar.x = rect.width - assetFactory.getButtonContainerSize();
  322. vScrollBar.y = 0;
  323. vScrollBar.setLength(rect.height - CAPTION_BAR_HEIGHT - assetFactory.getButtonContainerSize() + 1);
  324. vScrollBar.setMaxValue(textArea.maxScrollV == 0 ? 0 : textArea.maxScrollV - 1);
  325. vScrollBar.draw();
  326. if (vScrollBar.getValue() > vScrollBar.getMaxValue())
  327. vScrollBar.toMaxValue();
  328. resizeArea.x = hScrollBar.x + hScrollBar.getLength();
  329. resizeArea.y = vScrollBar.y + vScrollBar.getLength();
  330. drawResizeArea();
  331. drawBackground();
  332. textArea.scrollV = textArea.maxScrollV;
  333. }
  334. /**
  335. * Show console component.
  336. */
  337. public function show():void
  338. {
  339. dispatchEvent(new ConsoleEvent(ConsoleEvent.SHOW));
  340. toFront();
  341. container.visible = true;
  342. focusInputField();
  343. }
  344. /**
  345. * Hide console component.
  346. */
  347. public function hide():void
  348. {
  349. dispatchEvent(new ConsoleEvent(ConsoleEvent.HIDE));
  350. container.visible = false;
  351. }
  352. /**
  353. * Check if console component is visible.
  354. * @return Return true if console is visible.
  355. */
  356. public function isVisible():Boolean
  357. {
  358. return container.visible;
  359. }
  360. /**
  361. * Completely show console component (caption bar and text area content).
  362. */
  363. public function maximize():void
  364. {
  365. content.visible = true;
  366. }
  367. /**
  368. * Minimize console component, only caption bar remains visible.
  369. */
  370. public function minimize():void
  371. {
  372. content.visible = false;
  373. }
  374. /**
  375. * Check if console component is maximized.
  376. * @return Return true if console is maximized.
  377. */
  378. public function isMaximized():Boolean
  379. {
  380. return content.visible;
  381. }
  382. /**
  383. * Set show/hide shortcut. If this shortcut is already used by container application (Flash IDE, Web Browser, etc) console shortcut will never be triggered.
  384. * @param shortcutKeys An array with all keys that should be pressed to trigger shortcut. Each key is a one-character string. Example: [ "m" ].
  385. * @param shortcutUseAlt If true ALT key should be pressed to trigger shortcut.
  386. * @param shortcutUseCtrl If true CTRL key should be pressed to trigger shortcut.
  387. * @param shortcutUseShift If true SHIFT key should be pressed to trigger shortcut.
  388. */
  389. public function shortcut(shortcutKeys:Array, shortcutUseAlt:Boolean = false, shortcutUseCtrl:Boolean = true, shortcutUseShift:Boolean = false):void
  390. {
  391. this.shortcutKeys = shortcutKeys;
  392. this.shortcutUseAlt = shortcutUseAlt;
  393. this.shortcutUseCtrl = shortcutUseCtrl;
  394. this.shortcutUseShift = shortcutUseShift;
  395. shortcutStates = new Array();
  396. for (var i:uint = 0; i < this.shortcutKeys.length; i++)
  397. {
  398. this.shortcutKeys[i] = this.shortcutKeys[i].toLowerCase();
  399. shortcutStates.push(false);
  400. }
  401. }
  402. /**
  403. * Bring console to front of other display objects. This call don't change the order of the objects added to stage, making console component invisible to host application.
  404. */
  405. public function toFront():void
  406. {
  407. while (container.parent.getChildIndex(container) < container.parent.numChildren - 1)
  408. container.parent.swapChildren(container, container.parent.getChildAt(container.parent.getChildIndex(container) + 1));
  409. }
  410. /**
  411. * Clear console text.
  412. */
  413. public function clear():void
  414. {
  415. textArea.text = "";
  416. hScrollBar.setMaxValue(0);
  417. hScrollBar.toMaxValue();
  418. vScrollBar.setMaxValue(0);
  419. vScrollBar.toMaxValue();
  420. }
  421. /**
  422. * Print information to console text area plus "\n".
  423. * @param info Any information to be printed. If is null, "(null)" string is used.
  424. */
  425. public function println(info:Object):void
  426. {
  427. // build string
  428. var str:String = getString(info);
  429. if (PRINT_TIMER)
  430. str = "[" + getTimer() + "] " + str;
  431. // throw events
  432. if (TRACE_ECHO)
  433. trace(str);
  434. if (FIREBUG_ECHO)
  435. {
  436. try
  437. {
  438. ExternalInterface.call("console.log", "[AS3Console] " + str);
  439. }
  440. catch (e:Error)
  441. {
  442. str = "[Error writing on firebug: " + e + "]\n" + str;
  443. }
  444. }
  445. dispatchEvent(new ConsoleEvent(ConsoleEvent.OUTPUT, str));
  446. // write text
  447. while (str.length > MAX_LINE_LENGTH)
  448. {
  449. var tmp:String = str.substr(0, MAX_LINE_LENGTH);
  450. textArea.appendText(tmp + "\n");
  451. str = str.substr(MAX_LINE_LENGTH);
  452. }
  453. textArea.appendText(str);
  454. textArea.appendText("\n");
  455. if (textArea.length > MAX_CHARACTERS)
  456. textArea.replaceText(0, textArea.length - MAX_CHARACTERS, "");
  457. // scroll bar
  458. hScrollBar.setMaxValue(textArea.maxScrollH == 0 ? 0 : textArea.maxScrollH - 1);
  459. vScrollBar.setMaxValue(textArea.maxScrollV == 0 ? 0 : textArea.maxScrollV - 1);
  460. vScrollBar.toMaxValue();
  461. // bring to front if visible
  462. if (isVisible())
  463. toFront();
  464. }
  465. //
  466. // private methods
  467. //
  468. private function drawResizeArea():void
  469. {
  470. resizeArea.graphics.clear();
  471. resizeArea.graphics.beginFill(assetFactory.getBackgroundColor());
  472. resizeArea.graphics.drawRect(0, 0, assetFactory.getButtonContainerSize() - 2, assetFactory.getButtonContainerSize() - 2);
  473. resizeArea.graphics.endFill();
  474. resizeArea.graphics.lineStyle(1, assetFactory.getButtonForegroundColor());
  475. resizeArea.graphics.beginFill(assetFactory.getButtonBackgroundColor());
  476. resizeArea.graphics.moveTo(assetFactory.getButtonContainerSize() - 4, 3);
  477. resizeArea.graphics.lineTo(assetFactory.getButtonContainerSize() - 4, assetFactory.getButtonContainerSize() - 4);
  478. resizeArea.graphics.lineTo(3, assetFactory.getButtonContainerSize() - 4);
  479. resizeArea.graphics.endFill();
  480. }
  481. private function drawBackground():void
  482. {
  483. captionBar.graphics.clear();
  484. captionBar.graphics.lineStyle(1, assetFactory.getButtonForegroundColor());
  485. captionBar.graphics.beginFill(assetFactory.getButtonBackgroundColor());
  486. captionBar.graphics.drawRect(0, 0, rect.width - 1, CAPTION_BAR_HEIGHT);
  487. captionBar.graphics.endFill();
  488. content.graphics.clear();
  489. content.graphics.lineStyle(1, assetFactory.getButtonForegroundColor());
  490. content.graphics.beginFill(assetFactory.getBackgroundColor());
  491. content.graphics.drawRect(0, 0, rect.width - 1, rect.height - CAPTION_BAR_HEIGHT - 1);
  492. content.graphics.endFill();
  493. }
  494. private function focusInputField():void {
  495. stage.focus = inputField;
  496. inputField.setSelection(inputField.text.length, inputField.text.length);
  497. }
  498. private function handleEmbedCommands(params:Array):Boolean {
  499. if (params[0] == "alpha")
  500. {
  501. if (params.length == 1)
  502. {
  503. println("Console component alpha: " + alpha);
  504. println("");
  505. }
  506. else
  507. {
  508. var value:Number = parseFloat(params[1]);
  509. if (isNaN(value) || value < 0 || value > 1)
  510. {
  511. println("Invalid alpha value: " + params[1]);
  512. println("Valid range is [0..1]");
  513. println("");
  514. }
  515. else
  516. {
  517. alpha = value;
  518. }
  519. }
  520. return true;
  521. }
  522. else if (params[0] == "clear")
  523. {
  524. clear();
  525. return true;
  526. }
  527. else if (params[0] == "hide")
  528. {
  529. hide();
  530. return true;
  531. }
  532. else if (params[0] == "mem")
  533. {
  534. var memUsed:String = System.totalMemory.toString();
  535. var result:String = "";
  536. var lastIndex:int = memUsed.length;
  537. for (var i:int = memUsed.length - 1; i >= 0; i--)
  538. {
  539. result = memUsed.charAt(i) + result;
  540. if (lastIndex - i == 3 && i > 0)
  541. {
  542. lastIndex = i;
  543. result = "." + result;
  544. }
  545. }
  546. println("Memory used: " + result + " bytes");
  547. println("");
  548. return true;
  549. }
  550. else if (params[0] == "version")
  551. {
  552. println("AS3Console version " + VERSION);
  553. println("Created by loteixeira at gmail dot com");
  554. println("Project page: http://code.google.com/p/as3console/");
  555. println("");
  556. return true;
  557. }
  558. return false;
  559. }
  560. //
  561. // keyboard events
  562. //
  563. private function onKeyDown(event:KeyboardEvent):void
  564. {
  565. var i:uint;
  566. for (i = 0; i < shortcutKeys.length; i++)
  567. {
  568. if (event.charCode == shortcutKeys[i].charCodeAt(0) || event.charCode == shortcutKeys[i].toUpperCase().charCodeAt(0)) {
  569. shortcutStates[i] = true;
  570. break;
  571. }
  572. }
  573. var triggerShortcut:Boolean = true;
  574. for (i = 0; i < shortcutKeys.length; i++)
  575. {
  576. if (!shortcutStates[i])
  577. {
  578. triggerShortcut = false;
  579. break;
  580. }
  581. }
  582. if (shortcutUseAlt && !event.altKey)
  583. triggerShortcut = false;
  584. if (shortcutUseCtrl && !event.ctrlKey)
  585. triggerShortcut = false;
  586. if (shortcutUseShift && !event.shiftKey)
  587. triggerShortcut = false;
  588. if (triggerShortcut) {
  589. if (isVisible())
  590. hide();
  591. else
  592. show();
  593. }
  594. }
  595. private function onKeyUp(event:KeyboardEvent):void
  596. {
  597. for (var i:uint = 0; i < shortcutKeys.length; i++)
  598. {
  599. if (event.charCode == shortcutKeys[i].charCodeAt(0) || event.charCode == shortcutKeys[i].toUpperCase().charCodeAt(0))
  600. {
  601. shortcutStates[i] = false;
  602. break;
  603. }
  604. }
  605. }
  606. private function onInputFieldKeyDown(event:KeyboardEvent):void
  607. {
  608. // enter key pressed
  609. if (event.charCode == 13)
  610. {
  611. println("> " + inputField.text);
  612. if (!handleEmbedCommands(inputField.text.split(" ")))
  613. dispatchEvent(new ConsoleEvent(ConsoleEvent.INPUT, inputField.text));
  614. // input history
  615. inputHistory.splice(0, 0, inputField.text);
  616. while (inputHistory.length > MAX_INPUT_HISTORY)
  617. inputHistory.pop();
  618. inputField.text = "";
  619. historyIndex = -1;
  620. // up key pressed
  621. } else if (event.keyCode == 38) {
  622. if (historyIndex < inputHistory.length - 1)
  623. historyIndex++;
  624. inputField.text = inputHistory[historyIndex];
  625. // down key pressed
  626. } else if (event.keyCode == 40) {
  627. if (historyIndex >= 0)
  628. historyIndex--;
  629. if (historyIndex == -1)
  630. inputField.text = "";
  631. else
  632. inputField.text = inputHistory[historyIndex];
  633. }
  634. }
  635. //
  636. // mouse events
  637. //
  638. private function onCaptionBarMouseUp(event:MouseEvent):void
  639. {
  640. container.stopDrag();
  641. rect.x = container.x;
  642. rect.y = container.y;
  643. focusInputField();
  644. }
  645. private function onCaptionBarMouseDown(event:MouseEvent):void
  646. {
  647. toFront();
  648. container.startDrag();
  649. }
  650. private function onTextAreaMouseWheel(event:MouseEvent):void
  651. {
  652. vScrollBar.setValue(textArea.scrollV - 1);
  653. }
  654. private function stageMouseMove(event:MouseEvent):void
  655. {
  656. hScrollBar.onStageMouseMove(event);
  657. vScrollBar.onStageMouseMove(event);
  658. if (resizing)
  659. {
  660. rect.width += event.stageX - resizeOffset.x;
  661. rect.height += event.stageY - resizeOffset.y;
  662. resizeOffset.x = event.stageX;
  663. resizeOffset.y = event.stageY;
  664. update();
  665. }
  666. }
  667. private function stageMouseUp(event:MouseEvent):void
  668. {
  669. hScrollBar.onStageMouseUp(event);
  670. vScrollBar.onStageMouseUp(event);
  671. if (resizing)
  672. {
  673. resizing = false;
  674. focusInputField();
  675. }
  676. }
  677. private function onResizeAreaMouseDown(event:MouseEvent):void
  678. {
  679. resizeOffset.x = event.stageX;
  680. resizeOffset.y = event.stageY;
  681. resizing = true;
  682. }
  683. //
  684. // scroll bar events
  685. //
  686. private function scrollBarChange(event:Event):void
  687. {
  688. if (event.target == hScrollBar)
  689. textArea.scrollH = hScrollBar.getValue() + 1;
  690. else if (event.target == vScrollBar)
  691. textArea.scrollV = vScrollBar.getValue() + 1;
  692. }
  693. }
  694. }