浏览代码

update

/main
fzhangtj 6 年前
当前提交
d83557f3
共有 14 个文件被更改,包括 270 次插入1192 次删除
  1. 28
      Assets/UIWidgets/editor/editor_window.cs
  2. 20
      Assets/UIWidgets/editor/surface.cs
  3. 370
      Assets/UIWidgets/engine/WidgetCanvas.cs
  4. 4
      Assets/UIWidgets/engine/WidgetCanvas.cs.meta
  5. 7
      Assets/UIWidgets/foundation/basic_types.cs
  6. 2
      Assets/UIWidgets/widgets/sliver.cs
  7. 13
      Assets/UIWidgetsSample/ToDoAppCanvas.cs
  8. 906
      Assets/UIWidgetsSample/UIWidgetSample.unity
  9. 7
      ProjectSettings/EditorBuildSettings.asset
  10. 57
      Assets/UIWidgetsSample/AsScreenCanvas.cs
  11. 3
      Assets/UIWidgets/engine/AsScreen.cs.meta
  12. 3
      Assets/UIWidgets/engine/canvas_window.cs.meta
  13. 42
      Assets/UIWidgets/engine/canvas_window.cs
  14. 0
      /Assets/UIWidgetsSample/AsScreenCanvas.cs

28
Assets/UIWidgets/editor/editor_window.cs


}
protected abstract Widget rootWidget();
public Vector2 size
{
get
{
return position.size;
}
}
public double devicePixelRatio
{
get { return EditorGUIUtility.pixelsPerPoint; }
}
public void scheduleFrame()
{
Repaint();
}
}
public class EditorWindowAdapter : WindowAdapter

this._lastWindowHeight * this._devicePixelRatio);
D.assert(this._surface == null);
this._surface = new EditorWindowSurface();
this._surface = createSurface();
_windowAdapters.Add(this);
this._alive = true;
}

using (this.getScope()) {
bool dirty = false;
if (this._devicePixelRatio != EditorGUIUtility.pixelsPerPoint) {
if (this._devicePixelRatio != queryDevicePixelRatio()) {
dirty = true;
}

protected abstract double queryDevicePixelRatio();
protected abstract Vector2 queryWindowSize();
protected virtual Surface createSurface()
{
return new EditorWindowSurface();
}
void _beginFrame() {

20
Assets/UIWidgets/editor/surface.cs


public class EditorWindowSurface : Surface {
static Material _guiTextureMat;
public delegate void DrawToTargetFunc(Rect screenRect, Texture texture, Material mat);
internal static Material _getGUITextureMat() {
if (_guiTextureMat) {
return _guiTextureMat;

}
GrSurface _surface;
public EditorWindowSurface() {
private DrawToTargetFunc _drawToTargetFunc;
public EditorWindowSurface(DrawToTargetFunc drawToTargetFunc = null)
{
this._drawToTargetFunc = drawToTargetFunc;
}
public SurfaceFrame acquireFrame(Size size, double devicePixelRatio) {

}
}
bool _presentSurface(Canvas canvas) {
protected bool _presentSurface(Canvas canvas) {
if (canvas == null) {
return false;
}

(float) (this._surface.size.width / this._surface.devicePixelRatio),
(float) (this._surface.size.height / this._surface.devicePixelRatio));
Graphics.DrawTexture(screenRect, this._surface.getRenderTexture(), _getGUITextureMat());
if (_drawToTargetFunc == null)
{
Graphics.DrawTexture(screenRect, this._surface.getRenderTexture(), _getGUITextureMat());
}
else
{
_drawToTargetFunc(screenRect, this._surface.getRenderTexture(), _getGUITextureMat());
}
return true;
}

370
Assets/UIWidgets/engine/WidgetCanvas.cs


using UIWidgets.painting;
using UIWidgets.ui;
using UIWidgets.widgets;
using UnityEditor;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using Image = UnityEngine.UI.Image;
public class WidgetCanvas: MonoBehaviour
public class UIWidgetWindowAdapter : WindowAdapter
{
private WidgetCanvas _widgetCanvas;
public UIWidgetWindowAdapter(WidgetCanvas widgetCanvas)
{
this._widgetCanvas = widgetCanvas;
}
protected override Surface createSurface()
{
return new EditorWindowSurface(_widgetCanvas.applyRenderTexture);
}
public override GUIContent titleContent
{
get { return new GUIContent(_widgetCanvas.gameObject.name); }
}
protected override double queryDevicePixelRatio()
{
return _widgetCanvas.canvas.scaleFactor;
}
protected override Vector2 queryWindowSize()
{
return _widgetCanvas.rectTransform.rect.size;
}
}
[RequireComponent(typeof(RectTransform))]
public abstract class WidgetCanvas : MaskableGraphic, IPointerDownHandler, IPointerUpHandler, IDragHandler,
IPointerEnterHandler, IPointerExitHandler
[SerializeField]
private int _canvasWidth = 1000;
[SerializeField]
private int _canvasHeight = 800;
private RenderTexture _renderTexture;
void OnEnable()
private Texture _texture;
private Vector2 _lastMouseMove;
private bool _mouseEntered;
protected override void OnEnable()
base.OnEnable();
_windowAdapter = new CanvasWindowAdapter(this);
var root = new WidgetsApp(null, getWidget());
_windowAdapter.attachRootWidget(root);
_windowAdapter = new UIWidgetWindowAdapter(this);
_windowAdapter.OnEnable();
var root = new WidgetsApp(null, getWidget());
_windowAdapter.attachRootWidget(root);
_lastMouseMove = Input.mousePosition;
}
setupMeshRenderer();
var boxColider = gameObject.AddComponent(typeof(BoxCollider)) as BoxCollider;
boxColider.size = new Vector3(1, 1, 0.00001f); // very thin box
private void OnDisable()
{
this._windowAdapter.OnDisable();
base.OnDisable();
protected virtual Widget getWidget()
protected abstract Widget getWidget();
public override Texture mainTexture
return new AsScreen();
get { return _texture; }
private void setupMeshRenderer()
internal void applyRenderTexture(Rect screenRect, Texture texture, Material mat)
var meshRenderer = gameObject.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
var shader = Shader.Find("UI/Default"); // todo
var material = new Material(shader);
meshRenderer.material = material;
var meshFilter = gameObject.AddComponent(typeof(MeshFilter)) as MeshFilter;
var mesh = new Mesh();
meshFilter.mesh = mesh;
var vertices = new Vector3[4];
float minX = -0.5f;
float minY = -0.5f;
float maxX = 0.5f;
float maxY = 0.5f;
vertices[0] = new Vector3(minX, minY, 0);
vertices[1] = new Vector3(maxX, minY, 0);
vertices[2] = new Vector3(minX, maxY, 0);
vertices[3] = new Vector3(maxX, maxY, 0);
mesh.vertices = vertices;
var tri = new int[6];
tri[0] = 0;
tri[1] = 2;
tri[2] = 1;
tri[3] = 2;
tri[4] = 3;
tri[5] = 1;
mesh.triangles = tri;
var uv = new Vector2[4];
uv[0] = new Vector2(0, 0);
uv[1] = new Vector2(1, 0);
uv[2] = new Vector2(0, 1);
uv[3] = new Vector2(1, 1);
mesh.uv = uv;
_texture = texture;
SetMaterialDirty();
if (_renderTexture != null)
{
_renderTexture.Release();
_renderTexture = null;
}
Destroy(GetComponent<MeshRenderer>());
Destroy(GetComponent<MeshFilter>());
Destroy(GetComponent<BoxCollider>());
}
private void Update() {
if (this._windowAdapter != null) {
this._windowAdapter.Update();
}
base.OnDestroy();
private void ensureRenderTexture()
private void Update()
if (_renderTexture != null && _renderTexture.IsCreated() &&
_renderTexture.width == getCanvasWidth() && _renderTexture.height == getCanvasHeight())
if (_mouseEntered && (_lastMouseMove.x != Input.mousePosition.x || _lastMouseMove.y != Input.mousePosition.y))
return;
this.OnMouseOver();
if (_renderTexture != null)
_lastMouseMove = Input.mousePosition;
if (this._windowAdapter != null)
_renderTexture.Release();
this._windowAdapter.Update();
_renderTexture = new RenderTexture(getCanvasWidth(), getCanvasHeight(), 24);
var effectiveWidth = getCanvasWidth();
var effectiveHeight = getCanvasHeight();
GL.PushMatrix();
GL.LoadIdentity();
Matrix4x4 m =Matrix4x4.Scale(new Vector3(2.0f / effectiveWidth, -2.0f / effectiveHeight, 1.0f))
* Matrix4x4.Translate(new Vector3(-effectiveWidth / 2.0f, -effectiveHeight / 2.0f, 0));
GL.LoadProjectionMatrix(m);
this._windowAdapter.OnGUI();
}
ensureRenderTexture();
var renderer = GetComponent<MeshRenderer>();
renderer.material.mainTexture = _renderTexture;
RenderTexture currentActiveRT = RenderTexture.active;
try
{
RenderTexture.active = _renderTexture;
GL.Clear(true, true, new UnityEngine.Color(0,0,0,0));
this._windowAdapter.OnGUI();
}
finally
{
RenderTexture.active = currentActiveRT;
}
GL.PopMatrix();
return;
}
if (this._windowAdapter != null) {
if (this._windowAdapter != null)
{
void OnMouseEnter()
private void OnMouseOver()
var pos = convertPosition(Input.mousePosition);
var pos = getPointPosition(Input.mousePosition);
// Debug.Log(string.Format("mouse move {0} {1}", pos.x, pos.y));
this._windowAdapter.PostPointerEvent(new PointerData(
timeStamp: DateTime.Now,
change: PointerChange.hover,

));
}
void OnMouseExit()
private int getMouseButtonDown()
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
));
for (int key = 0; key < 3; key++)
{
if (Input.GetMouseButton(key))
{
return key;
}
}
return 0;
void OnMouseOver()
public void OnPointerDown(PointerEventData eventData)
var pos = convertPosition(Input.mousePosition);
var position = getPointPosition(eventData);
change: PointerChange.hover,
change: PointerChange.down,
device: getMouseButtonDown(),
physicalX: pos.x,
physicalY: pos.y
device: (int) eventData.button,
physicalX: position.x,
physicalY: position.y
void OnMouseDown()
public void OnPointerUp(PointerEventData eventData)
int x = Screen.width;
int y = Screen.height;
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);
var position = getPointPosition(eventData);
device: getMouseButtonDown(),
physicalX: pos.x,
physicalY: pos.y
device: (int) eventData.button,
physicalX: position.x,
physicalY: position.y
void OnMouseDrag()
public Vector2 getPointPosition(PointerEventData eventData)
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
));
Vector2 localPoint;
RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, eventData.position,
eventData.enterEventCamera, out localPoint);
localPoint.x = localPoint.x - rectTransform.rect.min.x;
localPoint.y = rectTransform.rect.max.y - localPoint.y;
return localPoint;
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) * getCanvasWidth() * _windowAdapter.devicePixelRatio,
(-localPoint.y + 0.5) * getCanvasHeight() * _windowAdapter.devicePixelRatio
);
}
public Vector2 getPointPosition(Vector2 position)
{
// Debug.Log("mouse posse " + position.x + " " + position.y);
Vector2 localPoint;
Camera eventCamera = null;
private int getMouseButtonDown()
{
for (int key = 0; key < 3; key++)
if (canvas.renderMode != RenderMode.ScreenSpaceCamera)
if (Input.GetMouseButton(key))
{
return key;
}
eventCamera = canvas.GetComponent<GraphicRaycaster>().eventCamera;
return 0;
}
private int getCanvasWidth()
{
return _canvasWidth;
}
private int getCanvasHeight()
{
return _canvasHeight;
RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, position,
eventCamera, out localPoint);
localPoint.x = localPoint.x - rectTransform.rect.min.x;
localPoint.y = rectTransform.rect.max.y - localPoint.y;
return localPoint;
public Vector2 size
public void OnDrag(PointerEventData eventData)
get
{
return new Vector2(_canvasWidth, _canvasHeight);
}
var position = getPointPosition(eventData);
this._windowAdapter.PostPointerEvent(new PointerData(
timeStamp: DateTime.Now,
change: PointerChange.move,
kind: PointerDeviceKind.mouse,
device: (int) eventData.button,
physicalX: position.x,
physicalY: position.y
));
public double devicePixelRatio
public void OnPointerEnter(PointerEventData eventData)
get { return 1; }
}
public void scheduleFrame()
{
_mouseEntered = true;
_lastMouseMove = eventData.position;
var position = getPointPosition(eventData);
this._windowAdapter.PostPointerEvent(new PointerData(
timeStamp: DateTime.Now,
change: PointerChange.hover,
kind: PointerDeviceKind.mouse,
device: (int) eventData.button,
physicalX: position.x,
physicalY: position.y
));
public GUIContent titleContent
public void OnPointerExit(PointerEventData eventData)
get
{
return new GUIContent(gameObject.name);
}
_mouseEntered = false;
var position = getPointPosition(eventData);
this._windowAdapter.PostPointerEvent(new PointerData(
timeStamp: DateTime.Now,
change: PointerChange.hover,
kind: PointerDeviceKind.mouse,
device: (int) eventData.button,
physicalX: position.x,
physicalY: position.y
));
}
}
}

4
Assets/UIWidgets/engine/WidgetCanvas.cs.meta


fileFormatVersion: 2
guid: 8ddcbbf7b3ab463692e623f84581e29a
timeCreated: 1545112466
guid: 5f8434687aa245d9ae7f766b6109166b
timeCreated: 1545642183

7
Assets/UIWidgets/foundation/basic_types.cs


public static bool isNotEmpty(this string it) {
return !string.IsNullOrEmpty(it);
}
public static TValue getOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> it, TKey key)
{
TValue v;
it.TryGetValue(key, out v);
return v;
}
}
}

2
Assets/UIWidgets/widgets/sliver.cs


for (int index = firstIndex; index <= lastIndex; ++index) {
this._currentlyUpdatingChildIndex = index;
Element newChild = this.updateChild(this._childElements[index], this._build(index), index);
Element newChild = this.updateChild(this._childElements.getOrDefault(index), this._build(index), index);
if (newChild != null) {
this._childElements[index] = newChild;
this._currentBeforeChild = (RenderBox) newChild.renderObject;

13
Assets/UIWidgetsSample/ToDoAppCanvas.cs


{
this.onPressed = onPressed;
this.padding = padding ?? EdgeInsets.all(8.0);
this.backgroundColor = backgroundColor ?? CLColors.transparent;
this.backgroundColor = backgroundColor ?? AsScreenCanvas.CLColors.transparent;
this.child = child;
}

height: 1.5
)
); });
/*return new Flexible( todo use scroll list
return new Flexible(
);*/
return new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: children.ToList());
);
}
public override Widget build(BuildContext context)

decoration: new BoxDecoration(border:Border.all(color: Color.fromARGB(255, 255, 0, 0), width: 5),
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,

{
this.onPressed = onPressed;
this.padding = padding ?? EdgeInsets.all(8.0);
this.backgroundColor = backgroundColor ?? CLColors.transparent;
this.backgroundColor = backgroundColor ?? AsScreenCanvas.CLColors.transparent;
this.child = child;
}

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

7
ProjectSettings/EditorBuildSettings.asset


m_ObjectHideFlags: 0
serializedVersion: 2
m_Scenes:
- enabled: 0
path:
guid: 00000000000000000000000000000000
path: Assets/Scenes/SampleScene.unity
guid: 99c9720ab356a0642a771bea13969a05
path: Assets/UIWidgetsSample/UIWidgetSample.unity
guid: 4e3b67b8f19ff4635b924fb6e01a071d
m_configObjects: {}

57
Assets/UIWidgetsSample/AsScreenCanvas.cs


using System.Collections.Generic;
using UIWidgets.engine;
using UIWidgets.gestures;
using Image = UIWidgets.widgets.Image;
namespace UIWidgets.engine
namespace UIWidgetsSample
public class AsScreen : StatefulWidget
public class AsScreenCanvas : WidgetCanvas
{
protected override Widget getWidget()
{
return new AsScreen();
}
public class AsScreen : StatefulWidget
{
public AsScreen(Key key = null) : base(key)
{

color: CLColors.icon2
)
),
new widgets.Container(
new Container(
decoration: new BoxDecoration(
color: CLColors.white,
borderRadius: BorderRadius.all(3)

return new Container(
height: 450,
color: CLColors.white,
child: widgets.Image.network(
child: Image.network(
"https://d2ujflorbtfzji.cloudfront.net/banner/5c57178c-4be6-4903-953b-85125bfb7154.jpg",
fit: BoxFit.cover
)

),
width: 200,
height: 124,
child: widgets.Image.network(
child: Image.network(
this.imageSrc,
fit: BoxFit.fill
)

const double headerHeight = 80.0;
double _offsetY = 0.0;
int _index = -1;
Widget _buildHeader(BuildContext context)
{

setState(() => { _offsetY = 0.0; });
}
}
return true;
}

}
}
public class CustomButton : StatelessWidget
{
public CustomButton(
Key key = null,
GestureTapCallback onPressed = null,
EdgeInsets padding = null,
Color backgroundColor = null,
Widget child = null
) : base(key: key)
{
this.onPressed = onPressed;
this.padding = padding ?? EdgeInsets.all(8.0);
this.backgroundColor = backgroundColor ?? CLColors.transparent;
this.child = child;
}
public readonly GestureTapCallback onPressed;
public readonly EdgeInsets padding;
public readonly Widget child;
public readonly Color backgroundColor;
public override Widget build(BuildContext context)
{
return new GestureDetector(
onTap: this.onPressed,
child: new Container(
padding: this.padding,
color: this.backgroundColor,
child: this.child
)
);
}
}
public static class Icons
{
public static readonly IconData notifications = new IconData(0xe7f4, fontFamily: "Material Icons");

public static readonly Color header = new Color(0xFF060B0C);
}
}
}

3
Assets/UIWidgets/engine/AsScreen.cs.meta


fileFormatVersion: 2
guid: 2c3cc71d76704a998af01c99da3be173
timeCreated: 1545115345

3
Assets/UIWidgets/engine/canvas_window.cs.meta


fileFormatVersion: 2
guid: a0bffa61dae34193b6c01986d6f7f22b
timeCreated: 1545211142

42
Assets/UIWidgets/engine/canvas_window.cs


using System;
using UIWidgets.editor;
using UIWidgets.ui;
using UnityEngine;
using UIWidgets.ui;
using UnityEditor;
using UnityEngine;
using Rect = UnityEngine.Rect;
namespace UIWidgets.engine
{
public class CanvasWindowAdapter : WindowAdapter
{
private Rect _position;
private double __devicePixelRatio;
private WidgetCanvas canvas;
public CanvasWindowAdapter(WidgetCanvas canvas)
{
this.canvas = canvas;
}
public override GUIContent titleContent
{
get
{
return new GUIContent(canvas.gameObject.name);
}
}
protected override double queryDevicePixelRatio()
{
return canvas.devicePixelRatio;
}
protected override Vector2 queryWindowSize()
{
return canvas.size;
}
}
}

/Assets/UIWidgets/engine/AsScreen.cs → /Assets/UIWidgetsSample/AsScreenCanvas.cs

正在加载...
取消
保存