//////////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2008 Josh Tynjala // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to // deal in the Software without restriction, including without limitation the // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or // sell copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS // IN THE SOFTWARE. // //////////////////////////////////////////////////////////////////////////////// package com.joshtynjala.text { import flash.events.Event; import flash.events.TimerEvent; import flash.text.TextField; import flash.utils.Timer; //-------------------------------------- // Events //-------------------------------------- /** * Event that is fired when the TypingTextField has finished typing all of * its characters. * * @eventType flash.events.Event.COMPLETE */ [Event(name="complete",type="flash.events.Event")] /** * A TextField that types its text character by character. * Similar to typing into a command terminal. :) * * @author Josh Tynjala */ public class TypingTextField extends TextField { //-------------------------------------- // Constructor //-------------------------------------- /** * Constructor. */ public function TypingTextField() { super(); this._typingTimer = new Timer(this.delay); this._typingTimer.addEventListener(TimerEvent.TIMER, timerUpdateHandler); } //-------------------------------------- // Properties //-------------------------------------- /** * @private */ private var _typingTimer:Timer; /** * @private */ private var _position:int; /** * @private */ private var _cursorOn:Boolean = true; /** * @private */ private var _delay:Number = 25; /** * The number of milliseconds before a new character is added. */ public function get delay():Number { return this._delay; } /** * @private */ public function set delay(value:Number):void { this._delay = value; } /** * @private * Storage for the showBlinkingCursor property. */ private var _showBlinkingCursor:Boolean = true; /** * If true, a blinking cursor will be displayed while * the typing action is active. */ public function get showBlinkingCursor():Boolean { return this._showBlinkingCursor; } /** * @private */ public function set showBlinkingCursor(value:Boolean):void { this._showBlinkingCursor = value; } /** * @private * Storage for the cursorText property. */ private var _cursorText:String = "_"; /** * The text representing a cursor. */ public function get cursorText():String { return this._cursorText; } /** * @private */ public function set cursorText(value:String):void { this._cursorText = value; } /** * @private * Storage for the text property. */ private var _text:String = ""; /** * @copy flash.text.TextField#text */ override public function get text():String { return this._text; } /** * @private */ override public function set text(value:String):void { if(this._showBlinkingCursor && value.length > 0) { super.text = this.cursorText; } else super.text = ""; this._position = 0; this._text = value; if(value.length > 0) { this._typingTimer.delay = this.delay; this._typingTimer.start(); } } //-------------------------------------- // Private Methods //-------------------------------------- /** * @private */ private function timerUpdateHandler(event:TimerEvent):void { //it might change during the typing this._typingTimer.delay = this.delay; this._position++; if(this._position > this._text.length) { //if we've typed the full text, we can stop super.text = this._text; //make sure the cursor is gone! this._typingTimer.stop(); this.dispatchEvent(new Event(Event.COMPLETE)); return; } this._cursorOn = !this._cursorOn; super.text = this._text.substr(0, this._position) + (this._showBlinkingCursor && this._cursorOn ? this._cursorText : ""); } } }