/src/com/bit101/components/TextArea.as
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
29package com.bit101.components
30{
31 import com.dgrigg.minimalcomps.skins.TextAreaSkin;
32 import com.dgrigg.utils.Logger;
33
34 import flash.display.DisplayObjectContainer;
35 import flash.events.Event;
36
37 public class TextArea extends Text
38 {
39 protected var _scrollbar:VScrollBar;
40 private var _dirtyScroller:Boolean = false;
41
42 /**
43 * Constructor
44 * @param parent The parent DisplayObjectContainer on which to add this Label.
45 * @param xpos The x position to place this component.
46 * @param ypos The y position to place this component.
47 * @param text The initial text to display in this component.
48 */
49 public function TextArea(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, text:String="")
50 {
51 skinClass = com.dgrigg.minimalcomps.skins.TextAreaSkin;
52 super(parent, xpos, ypos, text);
53
54 }
55
56 /**
57 * Creates and adds the child display objects of this component.
58 */
59 /*
60 override protected function addChildren():void
61 {
62 super.addChildren();
63
64 }
65 */
66
67 /**
68 * Changes the thumb percent of the scrollbar based on how much text is shown in the text area.
69 */
70 protected function updateScrollbar():void
71 {
72 if (_tf)
73 {
74 var visibleLines:int = _tf.numLines - _tf.maxScrollV + 1;
75 var percent:Number = visibleLines / _tf.numLines;
76
77 if (_scrollbar)
78 {
79 _scrollbar.setSliderParams(1, _tf.maxScrollV, _tf.scrollV);
80 _scrollbar.setThumbPercent(percent);
81 _scrollbar.pageSize = visibleLines;
82
83 _dirtyScroller = true;
84 invalidate();
85
86 }
87 }
88
89
90
91 }
92
93
94 override protected function skinPartAdded(part:String, instance:Object):void
95 {
96 super.skinPartAdded(part, instance);
97
98 switch (part)
99 {
100 case "textField":
101 _tf.addEventListener(Event.SCROLL, onTextScroll);
102 _tf.addEventListener(Event.CHANGE, onChange);
103
104 break;
105 case "scrollBar":
106 _scrollbar = instance as VScrollBar;
107 _scrollbar.addEventListener(Event.CHANGE, onScrollbarScroll);
108 break;
109 }
110 }
111
112
113
114 ///////////////////////////////////
115 // public methods
116 ///////////////////////////////////
117
118 /**
119 * Draws the visual ui of the component.
120 */
121 override public function draw():void
122 {
123 super.draw();
124
125 _skin.validate();
126
127 addEventListener(Event.ENTER_FRAME, onTextScrollDelay);
128
129 }
130
131
132
133
134 ///////////////////////////////////
135 // event handlers
136 ///////////////////////////////////
137
138 /**
139 * Waits one more frame before updating scroll bar.
140 * It seems that numLines and maxScrollV are not valid immediately after changing a TextField's size.
141 */
142 protected function onTextScrollDelay(event:Event):void
143 {
144 removeEventListener(Event.ENTER_FRAME, onTextScrollDelay);
145 if (_dirtyScroller)
146 {
147 updateScrollbar();
148 }
149 _dirtyScroller = false;
150 }
151
152 /**
153 * Called when the text in the text field is manually changed.
154 */
155 protected override function onChange(event:Event):void
156 {
157 super.onChange(event);
158 updateScrollbar();
159 }
160
161 /**
162 * Called when the scroll bar is moved. Scrolls text accordingly.
163 */
164 protected function onScrollbarScroll(event:Event):void
165 {
166 if (_tf && _scrollbar)
167 {
168 _tf.scrollV = Math.round(_scrollbar.value);
169 }
170 }
171
172 /**
173 * Called when the text is scrolled manually. Updates the position of the scroll bar.
174 */
175 protected function onTextScroll(event:Event):void
176 {
177 if (_tf && _scrollbar)
178 {
179 _scrollbar.value = _tf.scrollV;
180 updateScrollbar();
181 }
182 }
183 }
184}