From 7b3544fd2b1e3aea13d66913eadc908c9bdfe735 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Mon, 1 Dec 2008 21:12:09 +0000 Subject: [PATCH] - Added setFocuable(). - createField calls it. git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@725 ed5b42cb-e716-0410-a449-f6a68f950b19 --- src/as/com/threerings/flash/TextFieldUtil.as | 35 ++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/as/com/threerings/flash/TextFieldUtil.as b/src/as/com/threerings/flash/TextFieldUtil.as index 68452df9..f1c5e797 100644 --- a/src/as/com/threerings/flash/TextFieldUtil.as +++ b/src/as/com/threerings/flash/TextFieldUtil.as @@ -91,6 +91,9 @@ public class TextFieldUtil /** * Create a TextField. + * The field will have setFocusable() called on it. + * Note that if the autoSize property is not none, then the field will be resized + * to the size of the text, overwriting any width/height properties specified. * * @param initProps contains properties with which to initialize the TextField. * Additionally it may contain the following properties: @@ -112,6 +115,7 @@ public class TextFieldUtil tf.defaultTextFormat = createFormat(formatProps); } updateText(tf, text); + setFocusable(tf); return tf; } @@ -152,6 +156,15 @@ public class TextFieldUtil } } + /** + * Add a special MouseEvent.CLICK listener so that the specified field is focusable + * even inside a security boundary. + */ + public static function setFocusable (field :TextField) :void + { + field.addEventListener(MouseEvent.CLICK, handleFieldFocus); + } + /** * Include the specified TextField in a set of TextFields in which only * one may have a selection at a time. @@ -208,6 +221,28 @@ public class TextFieldUtil _lastSelected = null; } + /** + * Handle focusing the text field. + */ + protected static function handleFieldFocus (event :MouseEvent) :void + { + var tf :TextField = TextField(event.currentTarget); + if (tf.stage.focus == tf) { + return; // already has focus, bail. + } + + // else, assign focus + tf.stage.focus = tf; + + // try to be smart and move the cursor near the click + if (tf.selectable) { + var idx :int = tf.getCharIndexAtPoint(event.localX, event.localY); + if (idx != -1) { + tf.setSelection(idx, idx); + } + } + } + /** The last tracked TextField to be selected. */ protected static var _lastSelected :TextField;