/src/com/bit101/components/TextArea.as

https://bitbucket.org/HopeSky/mars_nd2d · ActionScript · 184 lines · 83 code · 32 blank · 69 comment · 8 complexity · 3ec2b3985304806198cd527419e326ef MD5 · raw file

  1. /**
  2. * TextArea.as
  3. * Keith Peters
  4. * version 0.9.2
  5. *
  6. * A Text component for displaying multiple lines of text with a scrollbar.
  7. *
  8. * Copyright (c) 2010 Keith Peters
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. */
  28. package com.bit101.components
  29. {
  30. import com.dgrigg.minimalcomps.skins.TextAreaSkin;
  31. import com.dgrigg.utils.Logger;
  32. import flash.display.DisplayObjectContainer;
  33. import flash.events.Event;
  34. public class TextArea extends Text
  35. {
  36. protected var _scrollbar:VScrollBar;
  37. private var _dirtyScroller:Boolean = false;
  38. /**
  39. * Constructor
  40. * @param parent The parent DisplayObjectContainer on which to add this Label.
  41. * @param xpos The x position to place this component.
  42. * @param ypos The y position to place this component.
  43. * @param text The initial text to display in this component.
  44. */
  45. public function TextArea(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, text:String="")
  46. {
  47. skinClass = com.dgrigg.minimalcomps.skins.TextAreaSkin;
  48. super(parent, xpos, ypos, text);
  49. }
  50. /**
  51. * Creates and adds the child display objects of this component.
  52. */
  53. /*
  54. override protected function addChildren():void
  55. {
  56. super.addChildren();
  57. }
  58. */
  59. /**
  60. * Changes the thumb percent of the scrollbar based on how much text is shown in the text area.
  61. */
  62. protected function updateScrollbar():void
  63. {
  64. if (_tf)
  65. {
  66. var visibleLines:int = _tf.numLines - _tf.maxScrollV + 1;
  67. var percent:Number = visibleLines / _tf.numLines;
  68. if (_scrollbar)
  69. {
  70. _scrollbar.setSliderParams(1, _tf.maxScrollV, _tf.scrollV);
  71. _scrollbar.setThumbPercent(percent);
  72. _scrollbar.pageSize = visibleLines;
  73. _dirtyScroller = true;
  74. invalidate();
  75. }
  76. }
  77. }
  78. override protected function skinPartAdded(part:String, instance:Object):void
  79. {
  80. super.skinPartAdded(part, instance);
  81. switch (part)
  82. {
  83. case "textField":
  84. _tf.addEventListener(Event.SCROLL, onTextScroll);
  85. _tf.addEventListener(Event.CHANGE, onChange);
  86. break;
  87. case "scrollBar":
  88. _scrollbar = instance as VScrollBar;
  89. _scrollbar.addEventListener(Event.CHANGE, onScrollbarScroll);
  90. break;
  91. }
  92. }
  93. ///////////////////////////////////
  94. // public methods
  95. ///////////////////////////////////
  96. /**
  97. * Draws the visual ui of the component.
  98. */
  99. override public function draw():void
  100. {
  101. super.draw();
  102. _skin.validate();
  103. addEventListener(Event.ENTER_FRAME, onTextScrollDelay);
  104. }
  105. ///////////////////////////////////
  106. // event handlers
  107. ///////////////////////////////////
  108. /**
  109. * Waits one more frame before updating scroll bar.
  110. * It seems that numLines and maxScrollV are not valid immediately after changing a TextField's size.
  111. */
  112. protected function onTextScrollDelay(event:Event):void
  113. {
  114. removeEventListener(Event.ENTER_FRAME, onTextScrollDelay);
  115. if (_dirtyScroller)
  116. {
  117. updateScrollbar();
  118. }
  119. _dirtyScroller = false;
  120. }
  121. /**
  122. * Called when the text in the text field is manually changed.
  123. */
  124. protected override function onChange(event:Event):void
  125. {
  126. super.onChange(event);
  127. updateScrollbar();
  128. }
  129. /**
  130. * Called when the scroll bar is moved. Scrolls text accordingly.
  131. */
  132. protected function onScrollbarScroll(event:Event):void
  133. {
  134. if (_tf && _scrollbar)
  135. {
  136. _tf.scrollV = Math.round(_scrollbar.value);
  137. }
  138. }
  139. /**
  140. * Called when the text is scrolled manually. Updates the position of the scroll bar.
  141. */
  142. protected function onTextScroll(event:Event):void
  143. {
  144. if (_tf && _scrollbar)
  145. {
  146. _scrollbar.value = _tf.scrollV;
  147. updateScrollbar();
  148. }
  149. }
  150. }
  151. }