浏览代码

handle mouse event

/main
fzhangtj 6 年前
当前提交
ab45a021
共有 13 个文件被更改,包括 1267 次插入71 次删除
  1. 7
      Assets/UIWidgets/editor/editor_window.cs
  2. 70
      Assets/UIWidgets/editor/window_adapter.cs
  3. 149
      Assets/UIWidgets/engine/WidgetCanvas.cs
  4. 25
      Assets/UIWidgets/ui/window.cs
  5. 35
      Assets/UIWidgets/engine/canvas_window.cs
  6. 3
      Assets/UIWidgetsSample.meta
  7. 13
      Assets/UIWidgetsSample/SampleCanvas.cs
  8. 3
      Assets/UIWidgetsSample/SampleCanvas.cs.meta
  9. 1001
      Assets/UIWidgetsSample/UIWidgetSample.unity
  10. 7
      Assets/UIWidgetsSample/UIWidgetSample.unity.meta
  11. 25
      Assets/UIWidgets/editor/canvas_window.cs
  12. 0
      /Assets/UIWidgets/engine/canvas_window.cs.meta

7
Assets/UIWidgets/editor/editor_window.cs


#if UNITY_EDITOR
using UIWidgets.ui;
using UnityEditor;
using UnityEngine;
using Rect = UnityEngine.Rect;

{
devicePixelRatio = EditorGUIUtility.pixelsPerPoint;
position = this.editorWindow.position;
}
protected override Vector2d convertPointerPosition(Vector2 postion)
{
return new Vector2d(postion.x * this._devicePixelRatio,
postion.y * this._devicePixelRatio);
}
}

70
Assets/UIWidgets/editor/window_adapter.cs


using UIWidgets.ui;
using UIWidgets.widgets;
using UnityEngine;
using Debug = UnityEngine.Debug;
using Rect = UnityEngine.Rect;
namespace UIWidgets.editor

{
get { return null; }
}
readonly WidgetsBinding _binding;
readonly RasterCache _rasterCache;

}
}
public void Paint()
public void PostPointerEvent(List<PointerData> data)
instance = this;
WidgetsBinding.instance = this._binding;
try {
if (this.onBeginFrame != null) {
this.onBeginFrame(new DateTime(Stopwatch.GetTimestamp()) - this._epoch);
}
this.flushMicrotasks();
if (this.onDrawFrame != null) {
this.onDrawFrame();
}
}
finally {
instance = null;
WidgetsBinding.instance = null;
}
WithBinding(() =>
{
this.onPointerEvent(new PointerDataPacket(data));
});
}
public void PostPointerEvent(PointerData data)
{
PostPointerEvent(new List<PointerData>(){data});
public void WithBinding(Action fn)
{
instance = this;

if (this.onDrawFrame != null) {
this.onDrawFrame();
}
pointerData = new PointerData(
timeStamp: DateTime.Now,
change: PointerChange.down,
kind: PointerDeviceKind.mouse,
device: evt.button,
physicalX: evt.mousePosition.x * this._devicePixelRatio,
physicalY: evt.mousePosition.y * this._devicePixelRatio
);
var pysicalPos = convertPointerPosition(evt.mousePosition);
Debug.Log("clicked");
pointerData = new PointerData(
timeStamp: DateTime.Now,
change: PointerChange.down,
kind: PointerDeviceKind.mouse,
device: evt.button,
physicalX: pysicalPos.x,
physicalY: pysicalPos.y
);
var pysicalPos = convertPointerPosition(evt.mousePosition);
physicalX: evt.mousePosition.x * this._devicePixelRatio,
physicalY: evt.mousePosition.y * this._devicePixelRatio
physicalX: pysicalPos.x,
physicalY: pysicalPos.y
var pysicalPos = convertPointerPosition(evt.mousePosition);
physicalX: evt.mousePosition.x * this._devicePixelRatio,
physicalY: evt.mousePosition.y * this._devicePixelRatio
physicalX: pysicalPos.x,
physicalY: pysicalPos.y
var pysicalPos = convertPointerPosition(evt.mousePosition);
physicalX: evt.mousePosition.x * this._devicePixelRatio,
physicalY: evt.mousePosition.y * this._devicePixelRatio
physicalX: pysicalPos.x,
physicalY: pysicalPos.y
);
}

}
protected abstract void getWindowMetrics(out double devicePixelRatio , out Rect position);
protected abstract Vector2d convertPointerPosition(Vector2 postion);
private void doUpdate() {
this.flushMicrotasks();

149
Assets/UIWidgets/engine/WidgetCanvas.cs


using UIWidgets.editor;
using System;
using UIWidgets.editor;
using UIWidgets.ui;
using Color = UIWidgets.ui.Color;
using Rect = UnityEngine.Rect;
namespace UIWidgets.engine
{

{
this._paintingBinding = new PaintingBinding(null);
_paintingBinding.initInstances();
_windowAdapter = new CanvasWindowAdapter(new Rect(0, 0, _canvasWidth, _canvasHeight), EditorGUIUtility.pixelsPerPoint);
_windowAdapter.attachRootWidget(new AsScreen());
_windowAdapter = new CanvasWindowAdapter(new Rect(0, 0, _canvasWidth, _canvasHeight),
EditorGUIUtility.pixelsPerPoint, this.transform);
_windowAdapter.attachRootWidget(getWidget());
var boxColider = gameObject.AddComponent(typeof(BoxCollider)) as BoxCollider;
boxColider.size = new Vector3(1, 1, 0.00001f); // very thin box
private void setupMeshRenderer()
protected virtual Widget getWidget()
var meshRenderer = GetComponent<MeshRenderer>();
return new AsScreen();
}
meshRenderer = gameObject.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
private void setupMeshRenderer()
{
var meshRenderer = gameObject.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
var shader = Shader.Find("UI/Default"); // todo
var material = new Material(shader);
meshRenderer.material = material;

var mesh = new Mesh();
meshFilter.mesh = mesh;
var vertices = new Vector3[4];
float minX = -0.5f;

uv[1] = new Vector2(1, 0);
uv[2] = new Vector2(0, 1);
uv[3] = new Vector2(1, 1);
mesh.uv = uv;
}

}
Destroy(GetComponent<MeshRenderer>());
Destroy(GetComponent<MeshFilter>());
Destroy(GetComponent<BoxCollider>());
}
private void Update() {

}
_renderTexture = new RenderTexture(_canvasWidth, _canvasHeight, 24);
}
private void OnGUI()
{

return;
}
if (this._windowAdapter != null) {
this._windowAdapter.OnGUI();
if (Event.current.type == EventType.KeyDown || Event.current.type == EventType.KeyUp)
{
if (this._windowAdapter != null) {
this._windowAdapter.OnGUI();
}
}
}
void OnMouseEnter()
{
var pos = convertPosition(Input.mousePosition);
this._windowAdapter.PostPointerEvent(new PointerData(
timeStamp: DateTime.Now,
change: PointerChange.hover,
kind: PointerDeviceKind.mouse,
device: getMouseButtonDown(),
physicalX: pos.x,
physicalY: pos.y
));
}
void OnMouseExit()
{
var pos = convertPosition(Input.mousePosition);
this._windowAdapter.PostPointerEvent(new PointerData(
timeStamp: DateTime.Now,
change: PointerChange.hover,
kind: PointerDeviceKind.mouse,
device: getMouseButtonDown(),
physicalX: pos.x,
physicalY: pos.y
));
}
void OnMouseOver()
{
var pos = convertPosition(Input.mousePosition);
this._windowAdapter.PostPointerEvent(new PointerData(
timeStamp: DateTime.Now,
change: PointerChange.hover,
kind: PointerDeviceKind.mouse,
device: getMouseButtonDown(),
physicalX: pos.x,
physicalY: pos.y
));
}
void OnMouseDown()
{
var pos = convertPosition(Input.mousePosition);
this._windowAdapter.PostPointerEvent(new PointerData(
timeStamp: DateTime.Now,
change: PointerChange.down,
kind: PointerDeviceKind.mouse,
device: getMouseButtonDown(),
physicalX: pos.x,
physicalY: pos.y
));
}
void OnMouseUp()
{
var pos = convertPosition(Input.mousePosition);
this._windowAdapter.PostPointerEvent(new PointerData(
timeStamp: DateTime.Now,
change: PointerChange.up,
kind: PointerDeviceKind.mouse,
device: getMouseButtonDown(),
physicalX: pos.x,
physicalY: pos.y
));
}
void OnMouseDrag()
{
var pos = convertPosition(Input.mousePosition);
this._windowAdapter.PostPointerEvent(new PointerData(
timeStamp: DateTime.Now,
change: PointerChange.move,
kind: PointerDeviceKind.mouse,
device: getMouseButtonDown(),
physicalX: pos.x,
physicalY: pos.y
));
}
Vector2d convertPosition(Vector2 mousePos)
{
var cam = Camera.main;
var plan = new Plane(transform.TransformDirection(0, 0, 1), transform.TransformPoint(0, 0, 0));
var ray = cam.ScreenPointToRay(new Vector3(mousePos.x, mousePos.y, 0));
float enter;
Vector3 hitPoint;
if (plan.Raycast(ray, out enter))
{
hitPoint = ray.GetPoint(enter);
}
else
{
ray.direction = -ray.direction;
plan.Raycast(ray, out enter);
hitPoint = ray.GetPoint(enter);
var localPoint = transform.InverseTransformPoint(hitPoint);
return new Vector2d(
(localPoint.x + 0.5) * _canvasWidth * EditorGUIUtility.pixelsPerPoint,
(-localPoint.y + 0.5) * _canvasHeight * EditorGUIUtility.pixelsPerPoint
);
}
private int getMouseButtonDown()
{
for (int key = 0; key < 3; key++)
{
if (Input.GetMouseButton(key))
{
return key;
}
}
return 0;
}
}
}

25
Assets/UIWidgets/ui/window.cs


}
static Window _instance;
private bool _focus = false;
public bool focus
{
set
{
var preFocuse = this._focus;
this._focus = value;
if (preFocuse != _focus)
{
var lastInstance = instance;
try
{
instance = this;
}
finally
{
lastInstance = instance;
}
// todo notify focus change
}
}
get { return this._focus; }
}
public double devicePixelRatio {
get { return this._devicePixelRatio; }

35
Assets/UIWidgets/engine/canvas_window.cs


using System;
using UIWidgets.editor;
using UIWidgets.ui;
using UnityEngine;
using Rect = UnityEngine.Rect;
namespace UIWidgets.engine
{
public class CanvasWindowAdapter : WindowAdapter
{
private Rect _position;
private double __devicePixelRatio;
public CanvasWindowAdapter(Rect position, double devicePixelRatio, Transform tranform): base(position, devicePixelRatio)
{
this._position = position;
this.__devicePixelRatio = devicePixelRatio;
}
public override void scheduleFrame()
{
}
protected override void getWindowMetrics(out double devicePixelRatio, out Rect position)
{
devicePixelRatio = this.__devicePixelRatio;
position = this._position;
}
protected override Vector2d convertPointerPosition(Vector2 postion)
{
throw new NotImplementedException("pointer event should not be handled by this class");
}
}
}

3
Assets/UIWidgetsSample.meta


fileFormatVersion: 2
guid: ade2732b79fb40cfacd288e4083d3a8e
timeCreated: 1545377495

13
Assets/UIWidgetsSample/SampleCanvas.cs


using UIWidgets.engine;
using UIWidgets.widgets;
namespace UIWidgetsSample
{
public class SampleCanvas:WidgetCanvas
{
protected override Widget getWidget()
{
return new AsScreen();
}
}
}

3
Assets/UIWidgetsSample/SampleCanvas.cs.meta


fileFormatVersion: 2
guid: 45ef9c98c7594497921b0c714f2b4c33
timeCreated: 1545377537

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

7
Assets/UIWidgetsSample/UIWidgetSample.unity.meta


fileFormatVersion: 2
guid: 4e3b67b8f19ff4635b924fb6e01a071d
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

25
Assets/UIWidgets/editor/canvas_window.cs


using UnityEngine;
namespace UIWidgets.editor
{
public class CanvasWindowAdapter : WindowAdapter
{
private Rect _position;
private double __devicePixelRatio;
public CanvasWindowAdapter(Rect position, double devicePixelRatio): base(position, devicePixelRatio)
{
this._position = position;
this.__devicePixelRatio = devicePixelRatio;
}
public override void scheduleFrame()
{
}
protected override void getWindowMetrics(out double devicePixelRatio, out Rect position)
{
devicePixelRatio = this.__devicePixelRatio;
position = this._position;
}
}
}

/Assets/UIWidgets/editor/canvas_window.cs.meta → /Assets/UIWidgets/engine/canvas_window.cs.meta

正在加载...
取消
保存