您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
222 行
7.1 KiB
222 行
7.1 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using UIWidgets.async;
|
|
using UIWidgets.flow;
|
|
using UIWidgets.service;
|
|
using UIWidgets.rendering;
|
|
using UIWidgets.ui;
|
|
using UIWidgets.widgets;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using Rect = UnityEngine.Rect;
|
|
|
|
namespace UIWidgets.editor {
|
|
public class WindowAdapter : Window {
|
|
public WindowAdapter(EditorWindow editorWindow) {
|
|
this.editorWindow = editorWindow;
|
|
this.editorWindow.wantsMouseMove = false;
|
|
this.editorWindow.wantsMouseEnterLeaveWindow = false;
|
|
|
|
this._devicePixelRatio = EditorGUIUtility.pixelsPerPoint;
|
|
|
|
this._lastPosition = editorWindow.position;
|
|
this._physicalSize = new Size(
|
|
this._lastPosition.width * EditorGUIUtility.pixelsPerPoint,
|
|
this._lastPosition.height * EditorGUIUtility.pixelsPerPoint);
|
|
|
|
|
|
Window.instance = this;
|
|
try {
|
|
this._binding = new WidgetsBinding();
|
|
}
|
|
finally {
|
|
Window.instance = null;
|
|
}
|
|
}
|
|
|
|
public readonly EditorWindow editorWindow;
|
|
|
|
readonly WidgetsBinding _binding;
|
|
|
|
Rect _lastPosition;
|
|
readonly DateTime _epoch = new DateTime(Stopwatch.GetTimestamp());
|
|
readonly MicrotaskQueue _microtaskQueue = new MicrotaskQueue();
|
|
readonly TimerProvider _timerProvider = new TimerProvider();
|
|
readonly TextInput _textInput = new TextInput();
|
|
|
|
public void OnGUI() {
|
|
Window.instance = this;
|
|
WidgetsBinding.instance = this._binding;
|
|
|
|
try {
|
|
this.doOnGUI();
|
|
}
|
|
finally {
|
|
Window.instance = null;
|
|
WidgetsBinding.instance = null;
|
|
}
|
|
}
|
|
|
|
private void doOnGUI() {
|
|
var evt = Event.current;
|
|
|
|
if (evt.type == EventType.Repaint) {
|
|
if (this.onBeginFrame != null) {
|
|
this.onBeginFrame(new DateTime(Stopwatch.GetTimestamp()) - this._epoch);
|
|
}
|
|
|
|
this.flushMicrotasks();
|
|
|
|
if (this.onDrawFrame != null) {
|
|
this.onDrawFrame();
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
if (this.onPointerEvent != null) {
|
|
PointerData pointerData = null;
|
|
|
|
if (evt.type == EventType.MouseDown) {
|
|
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
|
|
);
|
|
} else if (evt.type == EventType.MouseUp || evt.rawType == EventType.MouseUp) {
|
|
pointerData = new PointerData(
|
|
timeStamp: DateTime.Now,
|
|
change: PointerChange.up,
|
|
kind: PointerDeviceKind.mouse,
|
|
device: evt.button,
|
|
physicalX: evt.mousePosition.x * this._devicePixelRatio,
|
|
physicalY: evt.mousePosition.y * this._devicePixelRatio
|
|
);
|
|
} else if (evt.type == EventType.MouseDrag) {
|
|
pointerData = new PointerData(
|
|
timeStamp: DateTime.Now,
|
|
change: PointerChange.move,
|
|
kind: PointerDeviceKind.mouse,
|
|
device: evt.button,
|
|
physicalX: evt.mousePosition.x * this._devicePixelRatio,
|
|
physicalY: evt.mousePosition.y * this._devicePixelRatio
|
|
);
|
|
}
|
|
|
|
if (pointerData != null) {
|
|
this.onPointerEvent(new PointerDataPacket(new List<PointerData> {
|
|
pointerData
|
|
}));
|
|
}
|
|
}
|
|
|
|
if (_textInput != null)
|
|
{
|
|
_textInput.OnGUI();
|
|
}
|
|
}
|
|
|
|
public void Update() {
|
|
Window.instance = this;
|
|
WidgetsBinding.instance = this._binding;
|
|
|
|
try {
|
|
this.doUpdate();
|
|
}
|
|
finally {
|
|
Window.instance = null;
|
|
WidgetsBinding.instance = null;
|
|
}
|
|
}
|
|
|
|
private void doUpdate() {
|
|
this.flushMicrotasks();
|
|
|
|
this._timerProvider.update();
|
|
|
|
bool dirty = false;
|
|
if (this._devicePixelRatio != EditorGUIUtility.pixelsPerPoint) {
|
|
dirty = true;
|
|
}
|
|
|
|
if (this._lastPosition != this.editorWindow.position) {
|
|
dirty = true;
|
|
}
|
|
|
|
if (dirty) {
|
|
this._devicePixelRatio = EditorGUIUtility.pixelsPerPoint;
|
|
this._lastPosition = this.editorWindow.position;
|
|
this._physicalSize = new Size(
|
|
this._lastPosition.width * EditorGUIUtility.pixelsPerPoint,
|
|
this._lastPosition.height * EditorGUIUtility.pixelsPerPoint);
|
|
|
|
if (this.onMetricsChanged != null) {
|
|
this.onMetricsChanged();
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void scheduleFrame() {
|
|
if (this.editorWindow != null) {
|
|
this.editorWindow.Repaint();
|
|
}
|
|
}
|
|
|
|
public override void render(Scene scene) {
|
|
var layer = scene.takeLayer();
|
|
|
|
var prerollContext = new PrerollContext();
|
|
layer.preroll(prerollContext, Matrix4x4.identity);
|
|
|
|
var paintContext = new PaintContext {canvas = new CanvasImpl()};
|
|
layer.paint(paintContext);
|
|
}
|
|
|
|
public override void scheduleMicrotask(Action callback) {
|
|
this._microtaskQueue.scheduleMicrotask(callback);
|
|
}
|
|
|
|
public override void flushMicrotasks() {
|
|
this._microtaskQueue.flushMicrotasks();
|
|
}
|
|
|
|
public override Timer run(TimeSpan duration, Action callback) {
|
|
return this._timerProvider.run(duration, callback);
|
|
}
|
|
|
|
public void attachRootRenderBox(RenderBox root) {
|
|
Window.instance = this;
|
|
WidgetsBinding.instance = this._binding;
|
|
|
|
try {
|
|
this._binding.renderView.child = root;
|
|
}
|
|
finally {
|
|
Window.instance = null;
|
|
WidgetsBinding.instance = null;
|
|
}
|
|
}
|
|
|
|
public void attachRootWidget(Widget root) {
|
|
Window.instance = this;
|
|
WidgetsBinding.instance = this._binding;
|
|
|
|
try {
|
|
this._binding.attachRootWidget(root);
|
|
}
|
|
finally {
|
|
Window.instance = null;
|
|
WidgetsBinding.instance = null;
|
|
}
|
|
}
|
|
|
|
public override TextInput textInput
|
|
{
|
|
get { return _textInput; }
|
|
}
|
|
}
|
|
}
|