浏览代码

Merge branch 'material' into 'master'

support mouse+touch input

See merge request upm-packages/ui-widgets/com.unity.uiwidgets!85
/main
Shenhua Gu 6 年前
当前提交
9b04c88a
共有 2 个文件被更改,包括 53 次插入57 次删除
  1. 68
      Runtime/engine/WidgetCanvas.cs
  2. 42
      Runtime/engine/input_utils.cs

68
Runtime/engine/WidgetCanvas.cs


WindowAdapter _windowAdapter;
Texture _texture;
Vector2 _lastMouseMove;
bool _mouseEntered;
HashSet<int> _enteredPointers;
bool _mouseEntered {
get { return !this._enteredPointers.isEmpty(); }
}
readonly ScrollInput _scrollInput = new ScrollInput();
DisplayMetrics _displayMetrics;

protected override void OnEnable() {
base.OnEnable();
//Disable touch -> mouse event on mobile devices
//Disable the default touch -> mouse event conversion on mobile devices
Input.simulateMouseWithTouches = false;
this._displayMetrics = DisplayMetricsProvider.provider();

this._windowAdapter.attachRootWidget(root);
this._lastMouseMove = Input.mousePosition;
this._enteredPointers = new HashSet<int>();
}
public float devicePixelRatio {

this.unfocusIfNeeded();
}
#if UNITY_IOS || UNITY_ANDROID
if (this._mouseEntered && Input.touchCount != 0) {
this.handleTouchMove();
}
#else
if (this._mouseEntered && (this._lastMouseMove.x != Input.mousePosition.x ||
this._lastMouseMove.y != Input.mousePosition.y)) {
this.handleMouseMove();
if (this._mouseEntered) {
if (this._lastMouseMove.x != Input.mousePosition.x || this._lastMouseMove.y != Input.mousePosition.y) {
this.handleMouseMovement();
}
#endif
if (this._mouseEntered) {
this.handleMouseScroll();

}
}
void handleTouchMove() {
for (var touchIndex = 0; touchIndex < Input.touchCount; touchIndex++) {
var touchEvent = Input.GetTouch(touchIndex);
if (touchEvent.phase != TouchPhase.Moved) {
continue;
}
var pos = this.getPointPosition(touchEvent.position);
this._windowAdapter.postPointerEvent(new PointerData(
timeStamp: Timer.timespanSinceStartup,
change: PointerChange.hover,
kind: PointerDeviceKind.touch,
device: InputUtils.getTouchFingerKey(touchEvent.fingerId),
physicalX: pos.x,
physicalY: pos.y
));
}
}
void handleMouseMove() {
void handleMouseMovement() {
var pos = this.getPointPosition(Input.mousePosition);
this._windowAdapter.postPointerEvent(new PointerData(
timeStamp: Timer.timespanSinceStartup,

this._windowAdapter.postPointerEvent(new PointerData(
timeStamp: Timer.timespanSinceStartup,
change: PointerChange.down,
kind: InputUtils.getPointerDeviceKind(),
kind: InputUtils.getPointerDeviceKind(eventData),
device: InputUtils.getPointerDeviceKey(eventData),
physicalX: position.x,
physicalY: position.y

this._windowAdapter.postPointerEvent(new PointerData(
timeStamp: Timer.timespanSinceStartup,
change: PointerChange.up,
kind: InputUtils.getPointerDeviceKind(),
kind: InputUtils.getPointerDeviceKind(eventData),
device: InputUtils.getPointerDeviceKey(eventData),
physicalX: position.x,
physicalY: position.y

this._windowAdapter.postPointerEvent(new PointerData(
timeStamp: Timer.timespanSinceStartup,
change: PointerChange.move,
kind: InputUtils.getPointerDeviceKind(),
kind: InputUtils.getPointerDeviceKind(eventData),
device: InputUtils.getPointerDeviceKey(eventData),
physicalX: position.x,
physicalY: position.y

public void OnPointerEnter(PointerEventData eventData) {
this._mouseEntered = true;
var pointerKey = InputUtils.getPointerDeviceKey(eventData);
this._enteredPointers.Add(pointerKey);
kind: InputUtils.getPointerDeviceKind(),
device: InputUtils.getPointerDeviceKey(eventData),
kind: InputUtils.getPointerDeviceKind(eventData),
device: pointerKey,
physicalX: position.x,
physicalY: position.y
));

this._mouseEntered = false;
var pointerKey = InputUtils.getPointerDeviceKey(eventData);
this._enteredPointers.Remove(pointerKey);
kind: InputUtils.getPointerDeviceKind(),
device: InputUtils.getPointerDeviceKey(eventData),
kind: InputUtils.getPointerDeviceKind(eventData),
device: pointerKey,
physicalX: position.x,
physicalY: position.y
));

42
Runtime/engine/input_utils.cs


using Unity.UIWidgets.foundation;
public static class InputUtils {
static class InputUtils {
const int preservedPointerKeyNum = 10;
const int preservedKeyNum = 10;
const int preservedMouseKeyNum = 100;
const int fingerKeyStart = preservedKeyNum + preservedMouseKeyNum;
public static PointerDeviceKind getPointerDeviceKind() {
#if UNITY_IOS || UNITY_ANDROID
return PointerDeviceKind.touch;
#else
return PointerDeviceKind.mouse;
#endif
public static PointerDeviceKind getPointerDeviceKind(PointerEventData eventData) {
return isTouchEvent(eventData) ? PointerDeviceKind.touch : PointerDeviceKind.mouse;
#if UNITY_IOS || UNITY_ANDROID
return getTouchFingerKey(eventData.pointerId);
#else
return getMouseButtonKey((int) eventData.button);
#endif
return isTouchEvent(eventData)
? getTouchFingerKey(eventData.pointerId)
: getMouseButtonKey((int) eventData.button);
}
public static int getScrollButtonKey() {

public static int getMouseButtonKey(int buttonId) {
return buttonId + preservedPointerKeyNum;
D.assert(buttonId < preservedMouseKeyNum);
return buttonId + preservedKeyNum;
}
static int getTouchFingerKey(int fingerId) {
return fingerId + fingerKeyStart;
}
static bool isTouchEvent(PointerEventData eventData) {
//pointerId >= 0 : touches
//ref: https://docs.unity3d.com/ScriptReference/EventSystems.PointerEventData-pointerId.html
return eventData.pointerId >= 0;
public static int getTouchFingerKey(int fingerId) {
return fingerId + preservedPointerKeyNum;
static bool isMouseEvent(PointerEventData eventData) {
//pointerId = -1, -2, -3 : mouse buttons
//ref: https://docs.unity3d.com/ScriptReference/EventSystems.PointerEventData-pointerId.html
return eventData.pointerId < 0;
}
}
}
正在加载...
取消
保存