浏览代码

more keyboard edit function

/main
fzhangtj 6 年前
当前提交
392445f7
共有 6 个文件被更改,包括 248 次插入11 次删除
  1. 72
      Assets/UIWidgets/painting/text_painter.cs
  2. 30
      Assets/UIWidgets/rendering/editable.cs
  3. 23
      Assets/UIWidgets/service/text_input.cs
  4. 55
      Assets/UIWidgets/ui/txt/paragraph.cs
  5. 4
      Assets/UIWidgets/ui/txt/word_separate.cs
  6. 75
      Assets/UIWidgets/widgets/editable_text.cs

72
Assets/UIWidgets/painting/text_painter.cs


return null;
}
public List<TextBox> getBoxesForSelection(TextSelection selection)
{
D.assert(!_needsLayout);

D.assert(!_needsLayout);
var range = _paragraph.getWordBoundary(position.offset);
return new TextRange(range.start, range.end);
}
public TextPosition getPositionVerticalMove(TextPosition position, int move)
{
D.assert(!_needsLayout);
var offset = getOffsetForCaret(position, Rect.zero);
var line = _paragraph.getLine(position);
var targetLineStart = _paragraph.getLineStart(line + move);
var newLineOffset = getOffsetForCaret(targetLineStart, Rect.zero);
return getPositionForOffset(new Offset(offset.dx, newLineOffset.dy));
}
public int getLineIndex(TextPosition position)
{
D.assert(!_needsLayout);
return _paragraph.getLine(position);
public TextPosition getLineStartPosition(int line)
{
D.assert(!_needsLayout);
return _paragraph.getLineStart(line);
}
public TextPosition getLineEndPosition(int line)
{
D.assert(!_needsLayout);
return _paragraph.getLineEnd(line);
}
public TextPosition getWordRight(TextPosition position)
{
D.assert(!_needsLayout);
var offset = position.offset;
while(true)
{
var range = _paragraph.getWordBoundary(offset);
if (range.end == range.start)
{
break;
}
if (!char.IsWhiteSpace((char)(text.codeUnitAt(range.start)??0)))
{
return new TextPosition(range.end);
}
offset = range.end;
}
return new TextPosition(offset);
}
public TextPosition getWordLeft(TextPosition position)
{
D.assert(!_needsLayout);
var offset = Math.Max(position.offset - 1, 0);
while(true)
{
var range = _paragraph.getWordBoundary(offset);
if (!char.IsWhiteSpace((char)(text.codeUnitAt(range.start)??0)))
{
return new TextPosition(range.start);
}
offset = Math.Max(range.start - 1, 0);
if (offset == 0)
{
break;
}
}
return new TextPosition(offset);
}
private ParagraphStyle _createParagraphStyle(TextDirection defaultTextDirection = TextDirection.ltr)
{
if (_text.style == null)

30
Assets/UIWidgets/rendering/editable.cs


var caretOffset = _textPainter.getOffsetForCaret(caretPosition, _caretPrototype);
return Rect.fromLTWH(0.0, 0.0, _kCaretWidth, preferredLineHeight).shift(caretOffset + _paintOffset);
}
public TextPosition getPositionDown(TextPosition position)
{
return _textPainter.getPositionVerticalMove(position, 1);
}
public TextPosition getPositionUp(TextPosition position)
{
return _textPainter.getPositionVerticalMove(position, -1);
}
public TextPosition getLineStartPosition(TextPosition position)
{
return _textPainter.getLineStartPosition(_textPainter.getLineIndex(position));
}
public TextPosition getLineEndPosition(TextPosition position)
{
return _textPainter.getLineEndPosition(_textPainter.getLineIndex(position));
}
public TextPosition getWordRight(TextPosition position)
{
return _textPainter.getWordRight(position);
}
public TextPosition getWordLeft(TextPosition position)
{
return _textPainter.getWordLeft(position);
}
protected override double computeMinIntrinsicWidth(double height) {
_layoutText(double.PositiveInfinity);
return _textPainter.minIntrinsicWidth;

23
Assets/UIWidgets/service/text_input.cs


);
}
public TextEditingValue deleteSelection()
public TextEditingValue deleteSelection(bool backDelete = true)
if (selection.start == 0)
if (backDelete)
{
if (selection.start == 0)
{
return this;
}
return this.copyWith(text: text.Substring(0, selection.start - 1) + selection.textAfter(this.text),
selection: TextSelection.collapsed(selection.start - 1));
}
if (selection.start >= text.Length)
return this.copyWith(text: text.Substring(0, selection.start - 1) + selection.textAfter(this.text),
selection: TextSelection.collapsed(selection.start - 1));
return this.copyWith(text: text.Substring(0, selection.start) + text.Substring(selection.start + 1));
}
else
{

public interface TextInputClient
{
void updateEditingValue(TextEditingValue value);
TextEditingValue getValueForOperation(TextEditOp operation);
// void performAction(TextInputAction action);
}

if (s_Keyactions.ContainsKey(e))
{
TextEditOp op = s_Keyactions[e];
var newValue = performOperation(op);
var newValue =_currentConnection._client.getValueForOperation(op);
if (_value != newValue)
{
_value = newValue;

}
}
internal enum TextEditOp
public enum TextEditOp
{
MoveLeft,
MoveRight,

55
Assets/UIWidgets/ui/txt/paragraph.cs


using System;
using System.Collections.Generic;
using UIWidgets.foundation;
using UIWidgets.ui.txt;
using UnityEngine;

_characterPositions, line.start, line.endExcludingWhitespace);
}
}
}
computeWidthMetrics(maxWordWidth);

double top = baseLine - run.fontMetrics.ascent;
double bottom = baseLine + run.fontMetrics.descent;
// double left, right;
var from = Math.Max(start, run.codeUnits.start);
var to = Math.Min(end, run.codeUnits.end);
if (from < to)

return new PositionWithAffinity(index, TextAffinity.downstream);
} else
{
return new PositionWithAffinity(index, TextAffinity.upstream);
return new PositionWithAffinity(index + 1, TextAffinity.upstream);
}
}
public int getLine(TextPosition position)
{
D.assert(!_needsLayout);
if (position.offset < 0)
{
return 0;
}
var offset = position.offset;
if (position.affinity == TextAffinity.upstream && offset > 0)
{
offset = _isUtf16Surrogate(_text[offset - 1]) ? offset - 2 : offset - 1;
}
var lineCount = getLineCount();
for (int lineIndex = 0; lineIndex < getLineCount(); ++lineIndex)
{
var line = _lineRanges[lineIndex];
if (offset >= line.start && offset <= line.end)
{
return lineIndex;
}
return Math.Max(lineCount - 1, 0);
public TextPosition getLineStart(int lineIndex)
{
lineIndex = Math.Min(getLineCount() - 1, Math.Max(lineIndex, 0));
return new TextPosition(_lineRanges[lineIndex].start);
}
public TextPosition getLineEnd(int lineIndex)
{
lineIndex = Math.Min(getLineCount() - 1, Math.Max(lineIndex, 0));
return new TextPosition(_lineRanges[lineIndex].end);
}
public IndexRange getWordBoundary(int offset)
{
WordSeparate s = new WordSeparate(_text);

}
}
}
public int getLineCount()
{
return _lineHeights.Count;
}
private void computeWidthMetrics(double maxWordWidth)
{

yOffset = yOffsetOriginal;
}
}
}
private static bool _isUtf16Surrogate(int value)
{
return (value & 0xF800) == 0xD800;
}
}

4
Assets/UIWidgets/ui/txt/word_separate.cs


public IndexRange findWordRange(int index)
{
if (index >= _text.Length)
{
return new IndexRange(0, 0);
}
var t = classifyChar(index);
int start = index;
for (int i = index; i >= 0; --i)

75
Assets/UIWidgets/widgets/editable_text.cs


_formatAndSetValue(value);
}
public TextEditingValue getValueForOperation(TextEditOp operation)
{
TextPosition newPosition = null;
switch (operation)
{
case TextEditOp.MoveLeft:
return _value.moveLeft();
case TextEditOp.MoveRight:
return _value.moveRight();
case TextEditOp.MoveUp:
newPosition = this.renderEditable.getPositionUp(new TextPosition(_value.selection.start, _value.selection.affinity));
return _value.copyWith(selection: TextSelection.fromPosition(newPosition));
case TextEditOp.MoveDown:
newPosition = this.renderEditable.getPositionDown(new TextPosition(_value.selection.start, _value.selection.affinity));
return _value.copyWith(selection: TextSelection.fromPosition(newPosition));
case TextEditOp.MoveLineStart:
newPosition = this.renderEditable.getLineStartPosition(new TextPosition(_value.selection.start, _value.selection.affinity));
return _value.copyWith(selection: TextSelection.fromPosition(newPosition));
case TextEditOp.MoveLineEnd:
newPosition = this.renderEditable.getLineEndPosition(new TextPosition(_value.selection.start, _value.selection.affinity));
return _value.copyWith(selection: TextSelection.fromPosition(newPosition));
case TextEditOp.MoveWordRight:
newPosition = this.renderEditable.getWordRight(new TextPosition(_value.selection.start));
return _value.copyWith(selection: TextSelection.fromPosition(newPosition));
case TextEditOp.MoveWordLeft:
newPosition = this.renderEditable.getWordLeft(new TextPosition(_value.selection.start));
return _value.copyWith(selection: TextSelection.fromPosition(newPosition));
// case TextEditOp.MoveToStartOfNextWord: MoveToStartOfNextWord(); break;
// case TextEditOp.MoveToEndOfPreviousWord: MoveToEndOfPreviousWord(); break;
case TextEditOp.MoveTextStart:
return _value.copyWith(selection: TextSelection.collapsed(0));
case TextEditOp.MoveTextEnd:
return _value.copyWith(selection: TextSelection.collapsed(_value.text.Length));
// case TextEditOp.MoveParagraphForward: MoveParagraphForward(); break;
// case TextEditOp.MoveParagraphBackward: MoveParagraphBackward(); break;
// case TextEditOp.MoveGraphicalLineStart: MoveGraphicalLineStart(); break;
// case TextEditOp.MoveGraphicalLineEnd: MoveGraphicalLineEnd(); break;
case TextEditOp.SelectLeft:
return _value.extendLeft();
case TextEditOp.SelectRight:
return _value.extendRight();
case TextEditOp.SelectUp:
newPosition = this.renderEditable.getPositionUp(_value.selection.extendPos);
return _value.copyWith(selection: _value.selection.copyWith(extentOffset: newPosition.offset));
case TextEditOp.SelectDown:
newPosition = this.renderEditable.getPositionDown(_value.selection.extendPos);
return _value.copyWith(selection: _value.selection.copyWith(extentOffset: newPosition.offset));
case TextEditOp.SelectWordRight:
newPosition = this.renderEditable.getWordRight(_value.selection.extendPos);
return _value.copyWith(selection: _value.selection.copyWith(extentOffset: newPosition.offset));
case TextEditOp.SelectWordLeft:
newPosition = this.renderEditable.getWordLeft(_value.selection.extendPos);
return _value.copyWith(selection: _value.selection.copyWith(extentOffset: newPosition.offset));
// case TextEditOp.SelectToEndOfPreviousWord: SelectToEndOfPreviousWord(); break;
// case TextEditOp.SelectToStartOfNextWord: SelectToStartOfNextWord(); break;
//
case TextEditOp.SelectTextStart:
return _value.copyWith(selection: _value.selection.copyWith(extentOffset: 0));
case TextEditOp.SelectTextEnd:
return _value.copyWith(selection: _value.selection.copyWith(extentOffset: _value.text.Length));
// case TextEditOp.ExpandSelectGraphicalLineStart: ExpandSelectGraphicalLineStart(); break;
// case TextEditOp.ExpandSelectGraphicalLineEnd: ExpandSelectGraphicalLineEnd(); break;
// case TextEditOp.SelectParagraphForward: SelectParagraphForward(); break;
// case TextEditOp.SelectParagraphBackward: SelectParagraphBackward(); break;
// case TextEditOp.SelectGraphicalLineStart: SelectGraphicalLineStart(); break;
// case TextEditOp.SelectGraphicalLineEnd: SelectGraphicalLineEnd(); break;
case TextEditOp.Delete: return _value.deleteSelection(false);
case TextEditOp.Backspace:
return _value.deleteSelection();
}
return _value;
}
void _updateRemoteEditingValueIfNeeded()
{
if (!_hasInputConnection)

正在加载...
取消
保存