/** * */ protected void textInput_changeHandler(Event e) { _userTypedIntoText = true; TextFieldEvent tfe = (TextFieldEvent)e; /*var operation:FlowOperation = tfe.operation; * * // Close the dropDown if we press delete or cut the selected text * if (operation is DeleteTextOperation || operation is CutOperation) * { * super.changeHighlightedSelection(CUSTOM_SELECTED_ITEM); * } * else * { * if (openOnInput) * { * if (!isDropDownOpen) * { * // Open the dropDown if it isn't already open * openDropDown(); * addEventListener(DropDownEvent.OPEN, editingOpenHandler); * return; * } * } * * processInputField(); * }*/ }
protected override void Render() { _oldCursorColor = GUI.skin.settings.cursorColor; _oldSelectionColor = GUI.skin.settings.selectionColor; GUI.skin.settings.cursorColor = _cursorColor; GUI.skin.settings.selectionColor = _selectionColor; var hasFocus = FocusManager.Instance.FocusedComponent == this; // I am not focusable, but the parent is /*if (hasFocus) * Debug.Log("Focused: " + Uid);*/ var isMouseOvered = Contains(MouseEventDispatcher.MouseTarget, true); _shouldOptimize = Optimized && !isMouseOvered && !hasFocus && !PasswordMode; /* 20150402 Uncommented "&& !PasswordMode" - BUGFIX for http://forum.edrivengui.com/index.php?threads/textfield-passwordmode-property-bug.295/ */ //if (_shouldOptimize) // Debug.LogWarning("_shouldOptimize is true!?! " + _shouldOptimize); /** * NOTE (20120616) * While investigating for the reason why text field/area looses selection while optimized * I found out that it's not the metter of this text field, but of the other optimized text field which is mouse-overed * Until this other field hasn't been mouse-overed, it is being drawn as a bow * However, when mouse-overed, it is being drawn as a text field and then it steals unity focus immediatelly! * This demands the further investigation * */ // draw 2 instances as text fields, others as boxes // also, if there is a password mode on, draw TextBox if (_shouldOptimize) { if (UnityEngine.Event.current.type == EventType.Repaint) { GUI.Box(RenderingRect, _text, ActiveStyle); } } else { var shouldRender = hasFocus || UnityEngine.Event.current.type == EventType.Repaint; if (shouldRender) { GUI.SetNextControlName(Uid); /** * TextField * */ if (_passwordMode) { _currentText = MaxChars > -1 ? GUI.PasswordField(RenderingRect, _text, PasswordCharMask, MaxChars, ActiveStyle) : GUI.PasswordField(RenderingRect, _text, PasswordCharMask, ActiveStyle); } else { _currentText = GUI.TextField(RenderingRect, _text, ActiveStyle); if (MaxChars > 0) { _currentText = _currentText.Substring(0, MaxChars); } } } } //Debug.Log(string.Format("hasFocus: {0}, Editable: {1}, Enabled: {2}", hasFocus, Editable, Enabled)); if ((hasFocus || isMouseOvered) && Editable && Enabled) { if (_currentText != _text) { // if a list has a length greater than 0: if (_alowedCharsList.Count > 0) { char[] oldArr = _currentText.ToCharArray(); StringBuilder sb = new StringBuilder(); foreach (char c in oldArr) { if (_alowedCharsList.Contains(c)) { sb.Append(c); } } _currentText = sb.ToString(); } // one more time if (_currentText != _text) { //Debug.Log("Dispatching change"); if (HasEventListener(TextFieldEvent.TEXT_CHANGING)) { _tfe = new TextFieldEvent(TextFieldEvent.TEXT_CHANGING, true) { OldText = _text, NewText = _currentText }; // bubbles DispatchEvent(_tfe); } if (null != _tfe && _tfe.Canceled) // _tfe is null if nobody listens { return; } /** * Checking if event is canceled from the outside * */ _text = _currentText; _textChanged = true; InvalidateProperties(); if (HasEventListener(TextFieldEvent.TEXT_CHANGE)) { _tfe = new TextFieldEvent(TextFieldEvent.TEXT_CHANGE, true) { OldText = _text, NewText = _currentText }; // bubbles DispatchEvent(_tfe); } } } } GUI.skin.settings.cursorColor = _oldCursorColor; GUI.skin.settings.selectionColor = _oldSelectionColor; Rendered = true; }
protected override void Render() { _oldCursorColor = GUI.skin.settings.cursorColor; _oldSelectionColor = GUI.skin.settings.selectionColor; GUI.skin.settings.cursorColor = _cursorColor; GUI.skin.settings.selectionColor = _selectionColor; //var owner = Owner ?? this; var hasFocus = FocusManager.Instance.FocusedComponent == this; // I am not focusable, but the parent is //var ownerCmp = owner as Component; var isMouseOvered = Contains(MouseEventDispatcher.MouseTarget, true); //Debug.Log("MouseEventDispatcher.MouseTarget: " + MouseEventDispatcher.MouseTarget); _shouldOptimize = Optimized && !isMouseOvered && !hasFocus; if (_shouldOptimize) { //if (Uid == "TextField2") // Debug.Log("Drawing box"); if (UnityEngine.Event.current.type == EventType.Repaint) { GUI.Box(RenderingRect, _text, ActiveStyle); } _activeMode = false; } else { if (hasFocus) { //TextFieldFocusHelper.BlurUnityFocus(); //Debug.Log(Uid); GUI.SetNextControlName(Uid); } _currentText = MaxChars > -1 ? GUI.TextArea(RenderingRect, _text, MaxChars, ActiveStyle) : GUI.TextArea(RenderingRect, _text, ActiveStyle); //ActiveStyle.DrawCursor(RenderingRect, new GUIContent(_currentText), 1, 40); TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl); //Debug.Log("editor.scrollOffset: " + editor.scrollOffset); //Debug.Log("editor.graphicalCursorPos: " + editor.graphicalCursorPos); if (null != _newCursorPos) { //Debug.Log("Handling text"); Vector2 v = _newCursorPos.ToVector2(); //Debug.Log(" LocalRenderingRect: " + LocalRenderingRect); //Debug.Log("index: " + index); //Debug.Log(" v: " + v); //int index = ActiveStyle.GetCursorStringIndex(editor.position, new GUIContent(_text), v); //editor.MoveCursorToPosition(v); //editor.SelectNone(); //editor.selectPos = -1; //editor.selectPos = index; //editor.pos = index; //editor.DrawCursor("*****************"); //editor.selectPos = 10; if (UnityEngine.Event.current.type == EventType.repaint) { //ActiveStyle.DrawCursor(RenderingRect, new GUIContent(_text), 111, index); //ActiveStyle.DrawWithTextSelection(RenderingRect, new GUIContent(_text), 111, 0, 5); } _newCursorPos = null; } if (!_activeMode) { // just switched //Debug.Log("Loading: " + _selectPos + ", " + _pos); editor.pos = _pos; editor.selectPos = _selectPos; _activeMode = true; } else { //Debug.Log("Saving: " + _selectPos + ", " + _pos); _pos = editor.pos; _selectPos = editor.selectPos; } if (hasFocus) { //Debug.Log("Has focus: " + Uid); GUI.FocusControl(Uid); } } //Debug.Log(string.Format("hasFocus: {0}, Editable: {1}, Enabled: {2}", hasFocus, Editable, Enabled)); if ((hasFocus || isMouseOvered) && Editable && Enabled) { //Debug.Log("miki " + _text + " -> " + _currentText); if (_currentText != _text) { // if a list has a length greater than 0: if (_alowedCharsList.Count > 0) { char[] oldArr = _currentText.ToCharArray(); StringBuilder sb = new StringBuilder(); foreach (char c in oldArr) { if (_alowedCharsList.Contains(c)) { sb.Append(c); } } _currentText = sb.ToString(); } // one more time if (_currentText != _text) { //Debug.Log("Dispatching change"); if (HasEventListener(TextFieldEvent.TEXT_CHANGING)) { _tfe = new TextFieldEvent(TextFieldEvent.TEXT_CHANGING, true) { OldText = _text, NewText = _currentText }; // bubbles DispatchEvent(_tfe); } if (null != _tfe && _tfe.Canceled) // _tfe is null if nobody listens { return; } /** * Checking if event is canceled from the outside * */ //if (null == _tfe || !_tfe.Canceled) { _text = _currentText; _textChanged = true; InvalidateProperties(); if (HasEventListener(TextFieldEvent.TEXT_CHANGE)) { _tfe = new TextFieldEvent(TextFieldEvent.TEXT_CHANGE, true) { OldText = _text, NewText = _currentText }; // bubbles DispatchEvent(_tfe); } } } } GUI.skin.settings.cursorColor = _oldCursorColor; GUI.skin.settings.selectionColor = _oldSelectionColor; }