void AdjustCaret(TextField.CharPosition cp) { _caretPosition = cp.caretIndex; Vector2 pos = GetCharLocation(cp); TextField.LineInfo line = textField.lines[cp.lineIndex]; pos.y = line.y + textField.y; Vector2 newPos = pos; if (newPos.x < textField.textFormat.size) { newPos.x += Math.Min(50, (int)(_contentRect.width / 2)); } else if (newPos.x > _contentRect.width - GUTTER_X - textField.textFormat.size) { newPos.x -= Math.Min(50, (int)(_contentRect.width / 2)); } if (newPos.x < GUTTER_X) { newPos.x = GUTTER_X; } else if (newPos.x > _contentRect.width - GUTTER_X) { newPos.x = Math.Max(GUTTER_X, _contentRect.width - GUTTER_X); } if (newPos.y < GUTTER_Y) { newPos.y = GUTTER_Y; } else if (newPos.y + line.height >= _contentRect.height - GUTTER_Y) { newPos.y = Math.Max(GUTTER_Y, _contentRect.height - line.height - GUTTER_Y); } pos += MoveContent(newPos - pos); if (_editing) { if (line.height > 0) //将光标居中 { pos.y += (int)(line.height - textField.textFormat.size) / 2; } _caret.SetPosition(pos.x, pos.y, 0); Vector2 cursorPos = _caret.LocalToGlobal(new Vector2(0, _caret.height)); Input.compositionCursorPos = cursorPos; _nextBlink = Time.time + 0.5f; _caret.graphics.enabled = true; if (_selectionStart != null) { UpdateSelection(cp); } } }
/// <summary> /// 获得字符的坐标。这个坐标是容器的坐标,不是文本里的坐标。 /// </summary> /// <param name="cp"></param> /// <returns></returns> Vector2 GetCharLocation(TextField.CharPosition cp) { TextField.LineInfo line = textField.lines[cp.lineIndex]; Vector2 pos; if (line.charCount == 0 || textField.charPositions.Count == 0) { if (textField.align == AlignType.Center) { pos.x = (int)(_contentRect.width / 2); } else { pos.x = GUTTER_X; } } else { TextField.CharPosition v = textField.charPositions[Math.Min(cp.charIndex, textField.charPositions.Count - 1)]; pos.x = v.offsetX - 1; } pos.x += textField.x; pos.y = textField.y + line.y; return(pos); }
void Scroll(int hScroll, int vScroll) { vScroll = Mathf.Clamp(vScroll, 0, textField.lines.Count - 1); TextField.LineInfo line = textField.lines[vScroll]; hScroll = Mathf.Clamp(hScroll, 0, line.charCount - 1); TextField.CharPosition cp = GetCharPosition(line.charIndex + hScroll); Vector2 pt = GetCharLocation(cp); MoveContent(new Vector2(GUTTER_X - pt.x, GUTTER_Y - pt.y), false); }
void UpdateCaret(bool forceUpdate = false) { TextField.CharPosition cp; if (_editing) { cp = GetCharPosition(_caretPosition + Input.compositionString.Length); } else { cp = GetCharPosition(_caretPosition); } Vector2 pos = GetCharLocation(cp); TextField.LineInfo line = textField.lines[cp.lineIndex]; Vector2 offset = pos + textField.xy; if (offset.x < textField.textFormat.size) { offset.x += Mathf.Min(50, _contentRect.width * 0.5f); } else if (offset.x > _contentRect.width - GUTTER_X - textField.textFormat.size) { offset.x -= Mathf.Min(50, _contentRect.width * 0.5f); } if (offset.x < GUTTER_X) { offset.x = GUTTER_X; } else if (offset.x > _contentRect.width - GUTTER_X) { offset.x = Mathf.Max(GUTTER_X, _contentRect.width - GUTTER_X); } if (offset.y < GUTTER_Y) { offset.y = GUTTER_Y; } else if (offset.y + line.height >= _contentRect.height - GUTTER_Y) { offset.y = Mathf.Max(GUTTER_Y, _contentRect.height - line.height - GUTTER_Y); } MoveContent(offset - pos, forceUpdate); if (_editing) { _caret.position = textField.xy + pos; _caret.height = line.height > 0 ? line.height : textField.textFormat.size; if (_editable) { Vector2 cursorPos = _caret.LocalToWorld(new Vector2(0, _caret.height)); cursorPos = StageCamera.main.WorldToScreenPoint(cursorPos); #if !UNITY_2019_OR_NEWER if (Stage.devicePixelRatio == 1) { #endif cursorPos.y = Screen.height - cursorPos.y; cursorPos = cursorPos / Stage.devicePixelRatio; Input.compositionCursorPos = cursorPos + new Vector2(0, 20); #if !UNITY_2019_OR_NEWER } else { Input.compositionCursorPos = cursorPos - new Vector2(0, 20); } #endif } _nextBlink = Time.time + 0.5f; _caret.graphics.enabled = true; UpdateSelection(cp); } }
bool HandleKey(InputEvent evt) { bool keyCodeHandled = true; switch (evt.keyCode) { case KeyCode.Backspace: { if (evt.command) { //for mac:CMD+Backspace=Delete if (_selectionStart == _caretPosition && _caretPosition < textField.charPositions.Count - 1) { _selectionStart = _caretPosition + 1; } } else { if (_selectionStart == _caretPosition && _caretPosition > 0) { _selectionStart = _caretPosition - 1; } } if (_editable) { ReplaceSelection(null); } break; } case KeyCode.Delete: { if (_selectionStart == _caretPosition && _caretPosition < textField.charPositions.Count - 1) { _selectionStart = _caretPosition + 1; } if (_editable) { ReplaceSelection(null); } break; } case KeyCode.LeftArrow: { if (!evt.shift) { ClearSelection(); } if (_caretPosition > 0) { if (evt.command) //mac keyboard { TextField.CharPosition cp = GetCharPosition(_caretPosition); TextField.LineInfo line = textField.lines[cp.lineIndex]; cp = GetCharPosition(new Vector2(int.MinValue, line.y + textField.y)); AdjustCaret(cp, !evt.shift); } else { TextField.CharPosition cp = GetCharPosition(_caretPosition - 1); AdjustCaret(cp, !evt.shift); } } break; } case KeyCode.RightArrow: { if (!evt.shift) { ClearSelection(); } if (_caretPosition < textField.charPositions.Count - 1) { if (evt.command) { TextField.CharPosition cp = GetCharPosition(_caretPosition); TextField.LineInfo line = textField.lines[cp.lineIndex]; cp = GetCharPosition(new Vector2(int.MaxValue, line.y + textField.y)); AdjustCaret(cp, !evt.shift); } else { TextField.CharPosition cp = GetCharPosition(_caretPosition + 1); AdjustCaret(cp, !evt.shift); } } break; } case KeyCode.UpArrow: { if (!evt.shift) { ClearSelection(); } TextField.CharPosition cp = GetCharPosition(_caretPosition); if (cp.lineIndex > 0) { TextField.LineInfo line = textField.lines[cp.lineIndex - 1]; cp = GetCharPosition(new Vector2(_caret.x, line.y + textField.y)); AdjustCaret(cp, !evt.shift); } break; } case KeyCode.DownArrow: { if (!evt.shift) { ClearSelection(); } TextField.CharPosition cp = GetCharPosition(_caretPosition); if (cp.lineIndex == textField.lines.Count - 1) { cp.charIndex = textField.charPositions.Count - 1; } else { TextField.LineInfo line = textField.lines[cp.lineIndex + 1]; cp = GetCharPosition(new Vector2(_caret.x, line.y + textField.y)); } AdjustCaret(cp, !evt.shift); break; } case KeyCode.PageUp: { ClearSelection(); break; } case KeyCode.PageDown: { ClearSelection(); break; } case KeyCode.Home: { if (!evt.shift) { ClearSelection(); } TextField.CharPosition cp = GetCharPosition(_caretPosition); TextField.LineInfo line = textField.lines[cp.lineIndex]; cp = GetCharPosition(new Vector2(int.MinValue, line.y + textField.y)); AdjustCaret(cp, !evt.shift); break; } case KeyCode.End: { if (!evt.shift) { ClearSelection(); } TextField.CharPosition cp = GetCharPosition(_caretPosition); TextField.LineInfo line = textField.lines[cp.lineIndex]; cp = GetCharPosition(new Vector2(int.MaxValue, line.y + textField.y)); AdjustCaret(cp, !evt.shift); break; } //Select All case KeyCode.A: { if (evt.ctrlOrCmd) { _selectionStart = 0; AdjustCaret(GetCharPosition(int.MaxValue)); } break; } //Copy case KeyCode.C: { if (evt.ctrlOrCmd && !_displayAsPassword) { string s = GetSelection(); if (!string.IsNullOrEmpty(s)) { DoCopy(s); } } break; } //Paste case KeyCode.V: { if (evt.ctrlOrCmd && _editable) { DoPaste(); } break; } //Cut case KeyCode.X: { if (evt.ctrlOrCmd && !_displayAsPassword) { string s = GetSelection(); if (!string.IsNullOrEmpty(s)) { DoCopy(s); if (_editable) { ReplaceSelection(null); } } } break; } case KeyCode.Z: { if (evt.ctrlOrCmd && _editable) { if (evt.shift) { TextInputHistory.inst.Redo(this); } else { TextInputHistory.inst.Undo(this); } } break; } case KeyCode.Y: { if (evt.ctrlOrCmd && _editable) { TextInputHistory.inst.Redo(this); } break; } case KeyCode.Return: case KeyCode.KeypadEnter: { if (textField.singleLine) { Stage.inst.focus = parent; DispatchEvent("onSubmit", null); DispatchEvent("onKeyDown", null); //for backward compatibility } break; } case KeyCode.Tab: { if (textField.singleLine) { Stage.inst.DoKeyNavigate(evt.shift); keyCodeHandled = false; } break; } case KeyCode.Escape: { this.text = _textBeforeEdit; Stage.inst.focus = parent; break; } default: keyCodeHandled = (int)evt.keyCode <= 272 && !evt.ctrlOrCmd; break; } char c = evt.character; if (c != 0) { if (evt.ctrlOrCmd) { return(true); } if (c == '\r' || c == 3) { c = '\n'; } if (c == 25)/*shift+tab*/ { c = '\t'; } if (c == 27 /*escape*/ || textField.singleLine && (c == '\n' || c == '\t')) { return(true); } if (char.IsHighSurrogate(c)) { _highSurrogateChar = c; return(true); } if (_editable) { if (char.IsLowSurrogate(c)) { ReplaceSelection(char.ConvertFromUtf32(((int)c & 0x03FF) + ((((int)_highSurrogateChar & 0x03FF) + 0x40) << 10))); } else { ReplaceSelection(c.ToString()); } } return(true); } else { if (Input.compositionString.Length > 0 && _editable) { UpdateText(); } return(keyCodeHandled); } }
void __keydown(EventContext context) { if (!_editing || context.isDefaultPrevented) { return; } InputEvent evt = context.inputEvent; switch (evt.keyCode) { case KeyCode.Backspace: { context.PreventDefault(); if (_selectionStart == _caretPosition && _caretPosition > 0) { _selectionStart = _caretPosition - 1; } ReplaceSelection(null); break; } case KeyCode.Delete: { context.PreventDefault(); if (_selectionStart == _caretPosition && _caretPosition < textField.charPositions.Count - 1) { _selectionStart = _caretPosition + 1; } ReplaceSelection(null); break; } case KeyCode.LeftArrow: { context.PreventDefault(); if (!evt.shift) { ClearSelection(); } if (_caretPosition > 0) { TextField.CharPosition cp = GetCharPosition(_caretPosition - 1); AdjustCaret(cp, !evt.shift); } break; } case KeyCode.RightArrow: { context.PreventDefault(); if (!evt.shift) { ClearSelection(); } if (_caretPosition < textField.charPositions.Count - 1) { TextField.CharPosition cp = GetCharPosition(_caretPosition + 1); AdjustCaret(cp, !evt.shift); } break; } case KeyCode.UpArrow: { context.PreventDefault(); if (!evt.shift) { ClearSelection(); } TextField.CharPosition cp = GetCharPosition(_caretPosition); if (cp.lineIndex == 0) { return; } TextField.LineInfo line = textField.lines[cp.lineIndex - 1]; cp = GetCharPosition(new Vector2(_caret.x, line.y + textField.y)); AdjustCaret(cp, !evt.shift); break; } case KeyCode.DownArrow: { context.PreventDefault(); if (!evt.shift) { ClearSelection(); } TextField.CharPosition cp = GetCharPosition(_caretPosition); if (cp.lineIndex == textField.lines.Count - 1) { cp.charIndex = textField.charPositions.Count - 1; } else { TextField.LineInfo line = textField.lines[cp.lineIndex + 1]; cp = GetCharPosition(new Vector2(_caret.x, line.y + textField.y)); } AdjustCaret(cp, !evt.shift); break; } case KeyCode.PageUp: { context.PreventDefault(); ClearSelection(); break; } case KeyCode.PageDown: { context.PreventDefault(); ClearSelection(); break; } case KeyCode.Home: { context.PreventDefault(); if (!evt.shift) { ClearSelection(); } TextField.CharPosition cp = GetCharPosition(_caretPosition); TextField.LineInfo line = textField.lines[cp.lineIndex]; cp = GetCharPosition(new Vector2(int.MinValue, line.y + textField.y)); AdjustCaret(cp, !evt.shift); break; } case KeyCode.End: { context.PreventDefault(); if (!evt.shift) { ClearSelection(); } TextField.CharPosition cp = GetCharPosition(_caretPosition); TextField.LineInfo line = textField.lines[cp.lineIndex]; cp = GetCharPosition(new Vector2(int.MaxValue, line.y + textField.y)); AdjustCaret(cp, !evt.shift); break; } //Select All case KeyCode.A: { if (evt.ctrl) { context.PreventDefault(); _selectionStart = 0; AdjustCaret(GetCharPosition(int.MaxValue)); } break; } //Copy case KeyCode.C: { if (evt.ctrl && !_displayAsPassword) { context.PreventDefault(); string s = GetSelection(); if (!string.IsNullOrEmpty(s)) { DoCopy(s); } } break; } //Paste case KeyCode.V: { if (evt.ctrl) { context.PreventDefault(); DoPaste(); } break; } //Cut case KeyCode.X: { if (evt.ctrl && !_displayAsPassword) { context.PreventDefault(); string s = GetSelection(); if (!string.IsNullOrEmpty(s)) { DoCopy(s); ReplaceSelection(null); } } break; } case KeyCode.Return: case KeyCode.KeypadEnter: { if (textField.singleLine) { onSubmit.Call(); return; } break; } } char c = evt.character; if (c != 0) { if (evt.ctrl) { return; } if (c == '\r' || (int)c == 3) { c = '\n'; } if (c == 127 || textField.singleLine && c == '\n') { return; } if (char.IsHighSurrogate(c)) { _highSurrogateChar = c; return; } if (char.IsLowSurrogate(c)) { ReplaceSelection(char.ConvertFromUtf32(((int)c & 0x03FF) + ((((int)_highSurrogateChar & 0x03FF) + 0x40) << 10))); } else { ReplaceSelection(c.ToString()); } } else { if (Input.compositionString.Length > 0) { UpdateText(); } } }
void __keydown(EventContext context) { if (!_editing || context.isDefaultPrevented) { return; } InputEvent evt = context.inputEvent; switch (evt.keyCode) { case Keys.Back: { if (textField.text.Length > 0) { IMEAdapter.compositionString = textField.text.Remove(textField.text.Length - 1, 1); } context.PreventDefault(); if (_selectionStart == _caretPosition && _caretPosition > 0) { _selectionStart = _caretPosition - 1; } ReplaceSelection(null); break; } case Keys.Delete: { context.PreventDefault(); if (_selectionStart == _caretPosition && _caretPosition < textField.charPositions.Count - 1) { _selectionStart = _caretPosition + 1; } ReplaceSelection(null); break; } case Keys.Left: { context.PreventDefault(); if (!evt.shift) { ClearSelection(); } if (_caretPosition > 0) { TextField.CharPosition cp = GetCharPosition(_caretPosition - 1); AdjustCaret(cp, !evt.shift); } break; } case Keys.Right: { context.PreventDefault(); if (!evt.shift) { ClearSelection(); } if (_caretPosition < textField.charPositions.Count - 1) { TextField.CharPosition cp = GetCharPosition(_caretPosition + 1); AdjustCaret(cp, !evt.shift); } break; } case Keys.Up: { context.PreventDefault(); if (!evt.shift) { ClearSelection(); } TextField.CharPosition cp = GetCharPosition(_caretPosition); if (cp.lineIndex == 0) { return; } TextField.LineInfo line = textField.lines[cp.lineIndex - 1]; cp = GetCharPosition(new Vector2(_caret.x, line.y + textField.y)); AdjustCaret(cp, !evt.shift); break; } case Keys.Down: { context.PreventDefault(); if (!evt.shift) { ClearSelection(); } TextField.CharPosition cp = GetCharPosition(_caretPosition); if (cp.lineIndex == textField.lines.Count - 1) { cp.charIndex = textField.charPositions.Count - 1; } else { TextField.LineInfo line = textField.lines[cp.lineIndex + 1]; cp = GetCharPosition(new Vector2(_caret.x, line.y + textField.y)); } AdjustCaret(cp, !evt.shift); break; } case Keys.PageUp: { context.PreventDefault(); ClearSelection(); break; } case Keys.PageDown: { context.PreventDefault(); ClearSelection(); break; } case Keys.Home: { context.PreventDefault(); if (!evt.shift) { ClearSelection(); } TextField.CharPosition cp = GetCharPosition(_caretPosition); TextField.LineInfo line = textField.lines[cp.lineIndex]; cp = GetCharPosition(new Vector2(int.MinValue, line.y + textField.y)); AdjustCaret(cp, !evt.shift); break; } case Keys.End: { context.PreventDefault(); if (!evt.shift) { ClearSelection(); } TextField.CharPosition cp = GetCharPosition(_caretPosition); TextField.LineInfo line = textField.lines[cp.lineIndex]; cp = GetCharPosition(new Vector2(int.MaxValue, line.y + textField.y)); AdjustCaret(cp, !evt.shift); break; } //Select All case Keys.A: { if (evt.ctrl) { context.PreventDefault(); _selectionStart = 0; AdjustCaret(GetCharPosition(int.MaxValue)); } break; } //Copy case Keys.C: { if (evt.ctrl && !_displayAsPassword) { context.PreventDefault(); string s = GetSelection(); if (!string.IsNullOrEmpty(s)) { DoCopy(s); } } break; } //Paste case Keys.V: { if (evt.ctrl) { context.PreventDefault(); DoPaste(); } break; } //Cut case Keys.X: { if (evt.ctrl && !_displayAsPassword) { context.PreventDefault(); string s = GetSelection(); if (!string.IsNullOrEmpty(s)) { DoCopy(s); ReplaceSelection(null); } } break; } case Keys.Enter: { if (textField.singleLine) { DispatchEvent("onSubmit", null); return; } break; } } if (evt.ctrl) { return; } string str = evt.KeyName; if (!string.IsNullOrEmpty(str) && IMEAdapter.compositionMode == IMEAdapter.CompositionMode.Off) { if (textField.singleLine && str == "\n") { return; } ReplaceSelection(str); } else { if (IMEAdapter.compositionString.Length > 0) { UpdateText(); } } }
void UpdateCaret(bool forceUpdate = false) { TextField.CharPosition cp; if (_editing) { cp = GetCharPosition(_caretPosition + IMEAdapter.compositionString.Length); } else { cp = GetCharPosition(_caretPosition); } Vector2 pos = GetCharLocation(cp); TextField.LineInfo line = textField.lines[cp.lineIndex]; pos.Y = line.y + textField.y; Vector2 newPos = pos; if (newPos.X < textField.textFormat.size) { newPos.X += Math.Min(50, (int)(_contentRect.Width / 2)); } else if (newPos.X > _contentRect.Width - GUTTER_X - textField.textFormat.size) { newPos.X -= Math.Min(50, (int)(_contentRect.Width / 2)); } if (newPos.X < GUTTER_X) { newPos.X = GUTTER_X; } else if (newPos.X > _contentRect.Width - GUTTER_X) { newPos.X = Math.Max(GUTTER_X, _contentRect.Width - GUTTER_X); } if (newPos.Y < GUTTER_Y) { newPos.Y = GUTTER_Y; } else if (newPos.Y + line.height >= _contentRect.Height - GUTTER_Y) { newPos.Y = Math.Max(GUTTER_Y, _contentRect.Height - line.height - GUTTER_Y); } pos += MoveContent(newPos - pos, forceUpdate); if (_editing) { if (line.height > 0) //将光标居中 { pos.Y += (int)(line.height - textField.textFormat.size) / 2; } _caret.SetPosition(pos.X, pos.Y, 0); Vector2 cursorPos = _caret.LocalToGlobal(new Vector2(0, _caret.height)); IMEAdapter.compositionCursorPos = cursorPos; _nextBlink = Timers.time + 0.5f; _caret.graphics.enabled = true; UpdateSelection(cp); } }
void __keydown(EventContext context) { if (!_editing || context.isDefaultPrevented) { return; } InputEvent evt = context.inputEvent; switch (evt.keyCode) { case KeyCode.Backspace: { context.PreventDefault(); if (_selectionStart != null) { ReplaceSelection(null); } else if (_caretPosition > 0) { int tmp = _caretPosition; _caretPosition--; ReplaceText(textField.text.Substring(0, tmp - 1) + textField.text.Substring(tmp)); } break; } case KeyCode.Delete: { context.PreventDefault(); if (_selectionStart != null) { ReplaceSelection(null); } else if (_caretPosition < textField.text.Length) { ReplaceText(textField.text.Substring(0, _caretPosition) + textField.text.Substring(_caretPosition + 1)); } break; } case KeyCode.LeftArrow: { context.PreventDefault(); if (evt.shift) { if (_selectionStart == null) { _selectionStart = GetCharPosition(_caretPosition); } } else { ClearSelection(); } if (_caretPosition > 0) { CharPosition cp = GetCharPosition(_caretPosition - 1); AdjustCaret(cp); } break; } case KeyCode.RightArrow: { context.PreventDefault(); if (evt.shift) { if (_selectionStart == null) { _selectionStart = GetCharPosition(_caretPosition); } } else { ClearSelection(); } if (_caretPosition < textField.text.Length) { CharPosition cp = GetCharPosition(_caretPosition + 1); AdjustCaret(cp); } break; } case KeyCode.UpArrow: { context.PreventDefault(); if (evt.shift) { if (_selectionStart == null) { _selectionStart = GetCharPosition(_caretPosition); } } else { ClearSelection(); } CharPosition cp = GetCharPosition(_caretPosition); if (cp.lineIndex == 0) { return; } TextField.LineInfo line = textField.lines[cp.lineIndex - 1]; cp = GetCharPosition(new Vector2(_caret.x, line.y + textField.y)); AdjustCaret(cp); break; } case KeyCode.DownArrow: { context.PreventDefault(); if (evt.shift) { if (_selectionStart == null) { _selectionStart = GetCharPosition(_caretPosition); } } else { ClearSelection(); } CharPosition cp = GetCharPosition(_caretPosition); if (cp.lineIndex == textField.lines.Count - 1) { cp.charIndex = textField.charPositions.Count - 1; } else { TextField.LineInfo line = textField.lines[cp.lineIndex + 1]; cp = GetCharPosition(new Vector2(_caret.x, line.y + textField.y)); } AdjustCaret(cp); break; } case KeyCode.PageUp: { context.PreventDefault(); ClearSelection(); break; } case KeyCode.PageDown: { context.PreventDefault(); ClearSelection(); break; } case KeyCode.Home: { context.PreventDefault(); ClearSelection(); CharPosition cp = GetCharPosition(_caretPosition); TextField.LineInfo line = textField.lines[cp.lineIndex]; cp = GetCharPosition(new Vector2(int.MinValue, line.y + textField.y)); AdjustCaret(cp); break; } case KeyCode.End: { context.PreventDefault(); ClearSelection(); CharPosition cp = GetCharPosition(_caretPosition); TextField.LineInfo line = textField.lines[cp.lineIndex]; cp = GetCharPosition(new Vector2(int.MaxValue, line.y + textField.y)); AdjustCaret(cp); break; } //Select All case KeyCode.A: { if (evt.ctrl) { context.PreventDefault(); _selectionStart = GetCharPosition(0); AdjustCaret(GetCharPosition(textField.text.Length)); } break; } //Copy case KeyCode.C: { if (evt.ctrl && !_displayAsPassword) { context.PreventDefault(); string s = GetSelection(); if (!string.IsNullOrEmpty(s)) { DoCopy(s); } } break; } //Paste case KeyCode.V: { if (evt.ctrl) { context.PreventDefault(); DoPaste(); } break; } //Cut case KeyCode.X: { if (evt.ctrl && !_displayAsPassword) { context.PreventDefault(); string s = GetSelection(); if (!string.IsNullOrEmpty(s)) { DoCopy(s); ReplaceSelection(null); } } break; } case KeyCode.Return: case KeyCode.KeypadEnter: { if (!evt.ctrl && !evt.shift) { context.PreventDefault(); if (!textField.singleLine) { ReplaceSelection("\n"); } } break; } } }
void UpdateCaret(bool forceUpdate = false) { TextField.CharPosition cp; if (_editing) { cp = GetCharPosition(_caretPosition + Input.compositionString.Length); } else { cp = GetCharPosition(_caretPosition); } Vector2 pos = GetCharLocation(cp); TextField.LineInfo line = textField.lines[cp.lineIndex]; pos.y = line.y + textField.y; Vector2 newPos = pos; if (newPos.x < textField.textFormat.size) { newPos.x += Math.Min(50, (int)(_contentRect.width / 2)); } else if (newPos.x > _contentRect.width - GUTTER_X - textField.textFormat.size) { newPos.x -= Math.Min(50, (int)(_contentRect.width / 2)); } if (newPos.x < GUTTER_X) { newPos.x = GUTTER_X; } else if (newPos.x > _contentRect.width - GUTTER_X) { newPos.x = Math.Max(GUTTER_X, _contentRect.width - GUTTER_X); } if (newPos.y < GUTTER_Y) { newPos.y = GUTTER_Y; } else if (newPos.y + line.height >= _contentRect.height - GUTTER_Y) { newPos.y = Math.Max(GUTTER_Y, _contentRect.height - line.height - GUTTER_Y); } pos += MoveContent(newPos - pos, forceUpdate); if (_editing) { if (line.height > 0) //将光标居中 { pos.y += (int)(line.height - textField.textFormat.size) / 2; } _caret.SetPosition(pos.x, pos.y, 0); Vector2 cursorPos = _caret.LocalToGlobal(new Vector2(0, _caret.height)); Input.compositionCursorPos = UIScreen.UIScreenPosToScreenPos(cursorPos); _nextBlink = Time.time + 0.5f; _caret.graphics.enabled = true; UpdateSelection(cp); } }