PageRenderTime 2181ms CodeModel.GetById 21ms RepoModel.GetById 7ms app.codeStats 0ms

/fp10/src/com/furusystems/dconsole2/core/gui/maindisplay/input/InputField.as

http://doomsdayconsole.googlecode.com/
ActionScript | 182 lines | 153 code | 19 blank | 10 comment | 6 complexity | 55099e08c7f10091c224b4b552526210 MD5 | raw file
  1. package com.furusystems.dconsole2.core.gui.maindisplay.input
  2. {
  3. import com.furusystems.dconsole2.core.gui.layout.IContainable;
  4. import com.furusystems.dconsole2.core.interfaces.IThemeable;
  5. import com.furusystems.dconsole2.core.Notifications;
  6. import com.furusystems.dconsole2.core.style.Colors;
  7. import com.furusystems.dconsole2.core.style.GUIUnits;
  8. import com.furusystems.dconsole2.core.style.TextFormats;
  9. import com.furusystems.dconsole2.core.text.TextUtils;
  10. import com.furusystems.dconsole2.IConsole;
  11. import com.furusystems.messaging.pimp.MessageData;
  12. import com.furusystems.messaging.pimp.PimpCentral;
  13. import flash.display.Sprite;
  14. import flash.events.Event;
  15. import flash.geom.Rectangle;
  16. import flash.text.TextField;
  17. import flash.text.TextFieldType;
  18. /**
  19. * The main input field
  20. * @author Andreas Roenning
  21. */
  22. public class InputField extends Sprite implements IContainable,IThemeable
  23. {
  24. private var _inputTextField:TextField;
  25. private var _console:IConsole;
  26. public function InputField(console:IConsole)
  27. {
  28. _console = console;
  29. tabEnabled = tabChildren = false;
  30. _inputTextField = new TextField();
  31. _inputTextField.border = true;
  32. _inputTextField.embedFonts = TextFormats.INPUT_FONT.charAt(0) != "_";
  33. _inputTextField.defaultTextFormat = TextFormats.inputTformat;
  34. _inputTextField.multiline = false;
  35. _inputTextField.type = TextFieldType.INPUT;
  36. _inputTextField.background = true;
  37. _inputTextField.borderColor = Colors.INPUT_BORDER;
  38. _inputTextField.backgroundColor = Colors.INPUT_BG;
  39. _inputTextField.textColor = Colors.INPUT_FG;
  40. _inputTextField.tabEnabled = false;
  41. _inputTextField.text = "Input";
  42. _inputTextField.addEventListener(Event.CHANGE, onInputChange);
  43. addChild(_inputTextField);
  44. _console.messaging.addCallback(Notifications.CONSOLE_INPUT_LINE_CHANGE_REQUEST, onInputlineChangeRequest);
  45. _console.messaging.addCallback(Notifications.THEME_CHANGED, onThemeChange);
  46. }
  47. private function onInputlineChangeRequest(md:MessageData):void
  48. {
  49. text = String(md.data);
  50. dispatchEvent(new Event(Event.CHANGE));
  51. }
  52. private function onInputChange(e:Event):void
  53. {
  54. dispatchEvent(e.clone());
  55. }
  56. public function get text():String {
  57. return _inputTextField.text;
  58. }
  59. public function set text(s:String):void {
  60. _inputTextField.text = s;
  61. focus();
  62. }
  63. /* INTERFACE com.furusystems.dconsole2.core.gui.layout.IContainable */
  64. public function onParentUpdate(allotedRect:Rectangle):void
  65. {
  66. y = allotedRect.y;
  67. x = allotedRect.x;
  68. _inputTextField.height = GUIUnits.SQUARE_UNIT;
  69. _inputTextField.width = allotedRect.width-1;
  70. }
  71. public function get rect():Rectangle
  72. {
  73. return getRect(parent);
  74. }
  75. public function get minHeight():Number
  76. {
  77. return 0;
  78. }
  79. public function get minWidth():Number
  80. {
  81. return 0;
  82. }
  83. public function moveCaretToIndex(index:int = -1):void
  84. {
  85. if (index == -1) {
  86. index = inputTextField.length;
  87. }
  88. inputTextField.setSelection(index, index);
  89. }
  90. public function get selectionBeginIndex():int {
  91. return _inputTextField.selectionBeginIndex;
  92. }
  93. public function get selectionEndIndex():int {
  94. return _inputTextField.selectionEndIndex;
  95. }
  96. public function getWordAtIndex(index:int):String {
  97. return TextUtils.getWordAtIndex(_inputTextField, index);
  98. }
  99. public function get firstIndexOfWordAtCaret():int {
  100. return TextUtils.getFirstIndexOfWordAtCaretIndex(_inputTextField);
  101. }
  102. public function get firstWord():String {
  103. return getWordAtIndex(0);
  104. }
  105. public function get wordAtCaret():String {
  106. return TextUtils.getWordAtCaretIndex(inputTextField);
  107. }
  108. public function selectWordAtCaret():void {
  109. TextUtils.selectWordAtCaretIndex(_inputTextField);
  110. }
  111. public function moveCaretToEnd():void {
  112. moveCaretToIndex( -1);
  113. }
  114. public function moveCaretToStart():void {
  115. moveCaretToIndex(0);
  116. }
  117. public function focus():void
  118. {
  119. if (stage.focus == _inputTextField) return;
  120. stage.focus = _inputTextField;
  121. moveCaretToEnd();
  122. }
  123. /* INTERFACE com.furusystems.dconsole2.core.interfaces.IThemable */
  124. public function onThemeChange(md:MessageData):void
  125. {
  126. _inputTextField.borderColor = Colors.INPUT_BORDER;
  127. _inputTextField.backgroundColor = Colors.INPUT_BG;
  128. _inputTextField.textColor = Colors.INPUT_FG;
  129. }
  130. public function clear():void
  131. {
  132. text = "";
  133. }
  134. /**
  135. * Insert a string at the current caret index, as though the user typed a word
  136. * @param string
  137. */
  138. public function insertAtCaret(string:String):void
  139. {
  140. deleteSelection();
  141. var a:String = text.substr(0, caretIndex);
  142. var b:String = text.substr(caretIndex);
  143. text = a + string + b;
  144. moveCaretToIndex(a.length + string.length);
  145. }
  146. public function deleteSelection():void
  147. {
  148. var a:String = text.substr(0, _inputTextField.selectionBeginIndex);
  149. var b:String = text.substr(_inputTextField.selectionEndIndex);
  150. _inputTextField.text = a + b;
  151. }
  152. public function get selectionActive():Boolean
  153. {
  154. return _inputTextField.selectedText != "";
  155. }
  156. public function get length():int
  157. {
  158. return _inputTextField.length;
  159. }
  160. public function get caretIndex():int { return _inputTextField.caretIndex; }
  161. public function get inputTextField():TextField { return _inputTextField; }
  162. }
  163. }