浏览代码

handle select event in widget canvas

/main
fzhangtj 6 年前
当前提交
c9f82a6b
共有 11 个文件被更改,包括 2192 次插入9 次删除
  1. 19
      Runtime/engine/WidgetCanvas.cs
  2. 2
      Runtime/widgets/editable_text.cs
  3. 11
      Runtime/widgets/focus_manager.cs
  4. 17
      Samples/UIWidgetSample/ToDoAppCanvas.cs
  5. 968
      Samples/UIWidgetSample/UIWidgetSample.unity
  6. 13
      Samples/UIWidgetSample/SelectOnStart.cs
  7. 11
      Samples/UIWidgetSample/SelectOnStart.cs.meta
  8. 1001
      Samples/UIWidgetSample/TextInput.unity
  9. 7
      Samples/UIWidgetSample/TextInput.unity.meta
  10. 141
      Samples/UIWidgetSample/TextInputCanvas.cs
  11. 11
      Samples/UIWidgetSample/TextInputCanvas.cs.meta

19
Runtime/engine/WidgetCanvas.cs


using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using Image = UnityEngine.UI.Image;
using Rect = UnityEngine.Rect;
namespace Unity.UIWidgets.engine

private void Update()
{
if (EventSystem.current != null && EventSystem.current.currentSelectedGameObject != gameObject)
{
unfocusIfNeeded();
}
if (_mouseEntered && (_lastMouseMove.x != Input.mousePosition.x || _lastMouseMove.y != Input.mousePosition.y))
{
this.OnMouseOver();

}
}
private void OnGUI()
{
if (Event.current.type == EventType.Repaint)

public void OnPointerDown(PointerEventData eventData)
{
EventSystem.current.SetSelectedGameObject(gameObject, eventData);
var position = getPointPosition(eventData);
this._windowAdapter.PostPointerEvent(new PointerData(
timeStamp: DateTime.Now,

physicalX: position.x,
physicalY: position.y
));
}
private void unfocusIfNeeded()
{
using (_windowAdapter.getScope())
{
var focusNode = WidgetsBinding.instance.focusManager.currentFocus;
if (focusNode != null)
{
focusNode.unfocus();
}
}
}
}
}

2
Runtime/widgets/editable_text.cs


const int _kObscureShowLatestCharCursorTicks = 3;
private static TimeSpan _kCursorBlinkHalfPeriod = TimeSpan.FromMilliseconds(500);
private Timer _cursorTimer;
private ValueNotifier<bool> _showCursor = new ValueNotifier<bool>(true);
private ValueNotifier<bool> _showCursor = new ValueNotifier<bool>(false);
private GlobalKey _editableKey = GlobalKey.key();
private bool _didAutoFocus = false;
public ScrollController _scrollController = new ScrollController();

11
Runtime/widgets/focus_manager.cs


D.assert(rootScope._firstChild == null);
D.assert(rootScope._lastChild == null);
}
public readonly FocusScopeNode rootScope = new FocusScopeNode();
public readonly FocusScopeNode rootScope = new FocusScopeNode();
public FocusNode currentFocus
{
get { return _currentFocus; }
}
internal void _willDisposeFocusNode(FocusNode node) {
D.assert(node != null);

bool _haveScheduledUpdate = false;
internal void _markNeedsUpdate() {

Window.instance.scheduleMicrotask(_update);
}
internal FocusNode _findNextFocus() {
FocusScopeNode scope = rootScope;
while (scope._firstChild != null)

17
Samples/UIWidgetSample/ToDoAppCanvas.cs


using Unity.UIWidgets.rendering;
using Unity.UIWidgets.ui;
using Unity.UIWidgets.widgets;
using Color = Unity.UIWidgets.ui.Color;
using TextStyle = Unity.UIWidgets.painting.TextStyle;
namespace UIWidgetsSample

public class ToDoListApp : StatefulWidget
{
public ToDoListApp(Key key = null) : base(key)

private List<ToDoItem> items = new List<ToDoItem>();
private int nextId = 0;
private TextEditingController controller = new TextEditingController("");
private FocusNode _focusNode;
public override void initState() {
base.initState();
_focusNode = new FocusNode();
}
public override void dispose()
{
_focusNode.dispose();
base.dispose();
}
private Widget title()
{
return new Text("ToDo App", textAlign: TextAlign.center,

padding: EdgeInsets.fromLTRB(8, 0, 8, 0),
child: new EditableText(maxLines: 1,
controller: controller,
autofocus: true,
focusNode: new FocusNode(),
style: new TextStyle(
fontSize: 18,

{
title(),
textInput(),
// textInput(),
contents(),
}
)

968
Samples/UIWidgetSample/UIWidgetSample.unity
文件差异内容过多而无法显示
查看文件

13
Samples/UIWidgetSample/SelectOnStart.cs


using UnityEngine;
using UnityEngine.EventSystems;
namespace UIWidgetsSample
{
public class SelectOnStart: MonoBehaviour
{
private void Start()
{
EventSystem.current.SetSelectedGameObject(gameObject);
}
}
}

11
Samples/UIWidgetSample/SelectOnStart.cs.meta


fileFormatVersion: 2
guid: c42d7614d405d465aab861f4a0cb146e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

1001
Samples/UIWidgetSample/TextInput.unity
文件差异内容过多而无法显示
查看文件

7
Samples/UIWidgetSample/TextInput.unity.meta


fileFormatVersion: 2
guid: a677aa8bdebec4af786ef0846602c037
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

141
Samples/UIWidgetSample/TextInputCanvas.cs


using System.Collections.Generic;
using Unity.UIWidgets.engine;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.rendering;
using Unity.UIWidgets.ui;
using Unity.UIWidgets.widgets;
using TextStyle = Unity.UIWidgets.painting.TextStyle;
namespace UIWidgetsSample
{
public class TextInputCanvas: WidgetCanvas
{
public class TextInputSample : StatefulWidget
{
public readonly string title;
public TextInputSample(Key key = null, string title = null) : base(key)
{
this.title = title;
}
public override State createState()
{
return new _TextInputSampleState();
}
}
protected override Widget getWidget()
{
return new TextInputSample(key:null, title:gameObject.name);
}
class _TextInputSampleState : State<TextInputSample>
{
public class ToDoItem
{
public int id;
public string content;
}
private int nextId = 0;
private TextEditingController titleController = new TextEditingController("");
private TextEditingController descController = new TextEditingController("");
private FocusNode _titleFocusNode;
private FocusNode _descFocusNode;
public override void initState() {
base.initState();
_titleFocusNode = new FocusNode();
_descFocusNode = new FocusNode();
}
public override void dispose()
{
_titleFocusNode.dispose();
_descFocusNode.dispose();
base.dispose();
}
private Widget title()
{
return new Text(widget.title??"", textAlign: TextAlign.center,
style: new TextStyle(fontSize:24, fontWeight: FontWeight.w700));
}
private Widget titleInput()
{
return new Row(
children: new List<Widget>(
)
{
new SizedBox(width:100, child: new Text("Title")),
new Flexible(child: new Container(
decoration: new BoxDecoration(border: Border.all(new Color(0xFF000000), 1)),
padding: EdgeInsets.fromLTRB(8, 0, 8, 0),
child: new EditableText(maxLines: 1,
controller: titleController,
autofocus: true,
focusNode: new FocusNode(),
style: new TextStyle(
fontSize: 18,
height: 1.5f,
color: new Color(0xFF1389FD)
),
selectionColor: Color.fromARGB(255, 255, 0, 0),
cursorColor: Color.fromARGB(255, 0, 0, 0))
)),
}
);
}
private Widget descInput()
{
return new Container(
margin: EdgeInsets.fromLTRB(0, 10, 0, 10),
child: new Row(
children: new List<Widget>(
)
{
new SizedBox(width:100, child: new Text("Description")),
new Flexible(child: new Container(
decoration: new BoxDecoration(border: Border.all(new Color(0xFF000000), 1)),
padding: EdgeInsets.fromLTRB(8, 0, 8, 0),
child: new EditableText(maxLines: 1,
controller: descController,
focusNode: new FocusNode(),
style: new TextStyle(
fontSize: 18,
height: 1.5f,
color: new Color(0xFF1389FD)
),
selectionColor: Color.fromARGB(255, 255, 0, 0),
cursorColor: Color.fromARGB(255, 0, 0, 0))
)),
}
));
}
public override Widget build(BuildContext context)
{
var container = new Container(
padding: EdgeInsets.all(10),
decoration: new BoxDecoration(color:new Color(0x7F000000), border:Border.all(color: Color.fromARGB(255, 255, 0, 0), width: 5),
borderRadius: BorderRadius.all(2)),
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: new List<Widget>
{
title(),
titleInput(),
descInput(),
}
)
);
return container;
}
}
}
}

11
Samples/UIWidgetSample/TextInputCanvas.cs.meta


fileFormatVersion: 2
guid: e9bc91696c1584e11b23dca1a9e3cde3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存