package com.oxylusflash.book { import com.oxylusflash.framework.keyboard.Key; import com.oxylusflash.utils.StringUtil; import flash.display.Sprite; import flash.events.Event; import flash.events.FocusEvent; import flash.events.KeyboardEvent; import flash.text.TextField; import flash.ui.Keyboard; import caurina.transitions.Tweener; import caurina.transitions.properties.TextShortcuts; public class InputField extends ObjectWithTooltip { // Events. public static const SUBMIT:String = "submit"; public static const CANCEL:String = "cancel"; public static const FOCUS:String = "focus"; public static const BLUR:String = "blur"; // Gfx. public var bgMc:Sprite; public var bgFocusMc:Sprite; public var inpTxt:TextField; public var inpFocusTxt:TextField; protected var txtNColor:uint; protected var txtFColor:uint; protected var _value:*; protected var _label:String = ""; protected var _hasFocus:Boolean = false; private var _nextFocus:InputField; public var prevFocus:InputField; protected var canSubmitOnEnter:Boolean = true; /** * Form input field. */ public function InputField() { TextShortcuts.init(); tipYOffset = -39; hideTipEvent = FocusEvent.FOCUS_IN; bgMc.cacheAsBitmap = true; txtNColor = inpTxt.textColor; txtFColor = inpFocusTxt.textColor; //inpTxt.styleSheet = Global.styleSheet; //inpTxt.condenseWhite = true; inpTxt.text = _label; inpTxt.multiline = false; inpTxt.wordWrap = false; bgFocusMc.filters = [Global.GLOW]; this.removeChild(inpFocusTxt); bgFocusMc.alpha = 0; this.width = bgMc.width; inpTxt.addEventListener(FocusEvent.FOCUS_IN, inpTxt_focusInHandler, false, 0, true); inpTxt.addEventListener(FocusEvent.FOCUS_OUT, inpTxt_focusOutHandler, false, 0, true); } /** * Event handlers. * @param e */ protected function inpTxt_focusInHandler(e:FocusEvent):void { Key.disabled = true; Key.clear(); _hasFocus = true; this.dispatchEvent(new Event(FOCUS)); Tweener.addTween(bgFocusMc, { alpha: 1, base: Global.baseTween } ); Tweener.addTween(inpTxt, { _text_color: txtFColor, base: Global.baseTween } ); this.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler, false, 0, true); } protected function inpTxt_focusOutHandler(e:FocusEvent):void { Key.disabled = false; Key.focusFix(); _hasFocus = false; this.dispatchEvent(new Event(BLUR)); Tweener.addTween(bgFocusMc, { alpha: 0, base: Global.baseTween, time: Global.baseTween.time * 0.5 } ); Tweener.addTween(inpTxt, { _text_color: txtNColor, base: Global.baseTween, time: Global.baseTween.time * 0.5 } ); inpTxt.scrollH = 1; this.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); } private function keyDownHandler(e:KeyboardEvent):void { switch(e.keyCode) { case Keyboard.TAB: if (e.shiftKey) { if (prevFocus) stage.focus = prevFocus.textField; } else { if (nextFocus) stage.focus = nextFocus.textField; } break; case Keyboard.ENTER: if (canSubmitOnEnter) { this.dispatchEvent(new Event(SUBMIT)); stage.focus = null; } break; case Keyboard.ESCAPE: this.dispatchEvent(new Event(CANCEL)); stage.focus = null; break; } } /** * Input field value. */ public function get value():* { return _value; } public function set value(val:*):void { _value = val; } /** * Input field value. */ public function get label():String { _label = inpTxt.text; return _label; } public function set label(value:String):void { _label = value; inpTxt.htmlText = _label; } /** * Access the textfield. */ public function get textField():TextField { return inpTxt; } /** * Input field is blank (has only white chars or is empty). */ public function get isBlank():Boolean { return StringUtil.isBlank(textField.text); } /** * Input field has focus. */ public function get hasFocus():Boolean { return _hasFocus; } /** * Next input field that will get focus from the current when pressing TAB. */ public function get nextFocus():InputField { return _nextFocus; } public function set nextFocus(value:InputField):void { _nextFocus = value; value.prevFocus = this; } /** * Force focus on current input. */ public function gainFocus():void { stage.focus = textField; } /** * Overrides. */ override public function get width():Number { return bgMc.width; } override public function set width(value:Number):void { bgMc.width = value; bgFocusMc.width = value - 2 * bgFocusMc.x; inpTxt.width = value - 2 * inpTxt.x; } override public function get height():Number { return bgMc.height; } override public function set height(value:Number):void { } } }