浏览代码

refine canvas.

/main
kg 6 年前
当前提交
ef55e07f
共有 21 个文件被更改,包括 796 次插入497 次删除
  1. 4
      Assets/UIWidgets/flow/layer.cs
  2. 2
      Assets/UIWidgets/flow/layer_builder.cs
  3. 241
      Assets/UIWidgets/flow/layer_canvas.cs
  4. 2
      Assets/UIWidgets/flow/picture_layer.cs
  5. 12
      Assets/UIWidgets/painting/box_border.cs
  6. 4
      Assets/UIWidgets/painting/box_decoration.cs
  7. 25
      Assets/UIWidgets/painting/matrix_utils.cs
  8. 2
      Assets/UIWidgets/rendering/object.cs
  9. 3
      Assets/UIWidgets/ui/painting.meta
  10. 98
      Assets/UIWidgets/ui/painting/canvas.cs
  11. 3
      Assets/UIWidgets/ui/painting/canvas.cs.meta
  12. 51
      Assets/UIWidgets/ui/painting/draw_cmd.cs
  13. 3
      Assets/UIWidgets/ui/painting/draw_cmd.cs.meta
  14. 281
      Assets/UIWidgets/ui/painting/editor_canvas.cs
  15. 3
      Assets/UIWidgets/ui/painting/editor_canvas.cs.meta
  16. 160
      Assets/UIWidgets/ui/painting/painting.cs
  17. 157
      Assets/UIWidgets/ui/painting/picture.cs
  18. 3
      Assets/UIWidgets/ui/painting/picture.cs.meta
  19. 239
      Assets/UIWidgets/ui/painting.cs
  20. 0
      /Assets/UIWidgets/ui/painting/painting.cs.meta

4
Assets/UIWidgets/flow/layer.cs


using UnityEngine;
using Rect = UIWidgets.ui.Rect;
using Canvas = UIWidgets.ui.Canvas;
namespace UIWidgets.flow {
public class PrerollContext {

public LayerCanvas canvas;
public Canvas canvas;
public abstract class Layer {
private ContainerLayer _parent;

2
Assets/UIWidgets/flow/layer_builder.cs


return;
}
Rect pictureRect = picture.cullRect();
Rect pictureRect = picture.paintBounds;
pictureRect = pictureRect.shift(offset);
if (!pictureRect.overlaps(this._cullRects.Peek())) {

241
Assets/UIWidgets/flow/layer_canvas.cs


using Rect = UIWidgets.ui.Rect;
namespace UIWidgets.flow {
public class LayerCanvas {
static LayerCanvas() {
var shader = Shader.Find("UIWidgets/2D Handles Lines");
if (shader == null) {
throw new Exception("UIWidgets/2D Handles Lines not found");
}
LayerCanvas.linesMat = new Material(shader);
LayerCanvas.linesMat.hideFlags = HideFlags.HideAndDontSave;
shader = Shader.Find("UIWidgets/GUIRoundedRect");
if (shader == null) {
throw new Exception("UIWidgets/GUIRoundedRect not found");
}
LayerCanvas.guiRoundedRectMat = new Material(shader);
LayerCanvas.guiRoundedRectMat.hideFlags = HideFlags.HideAndDontSave;
shader = Shader.Find("UIWidgets/GUITextureClip");
if (shader == null) {
throw new Exception("UIWidgets/GUITextureClip not found");
}
LayerCanvas.guiTextureClipMat = new Material(shader);
LayerCanvas.guiTextureClipMat.hideFlags = HideFlags.HideAndDontSave;
}
private static readonly Material linesMat;
private static readonly Material guiRoundedRectMat;
private static readonly Material guiTextureClipMat;
private Matrix4x4 _transform;
private ClipRec _clipRec;
private LayerRec _layerRec;
private Stack<CanvasRec> _stack;
public LayerCanvas() {
this._transform = Matrix4x4.identity;
}
private Stack<CanvasRec> stack {
get {
if (this._stack == null) {
this._stack = new Stack<CanvasRec>();
}
return this._stack;
}
}
public void save() {
var state = new CanvasRec {
transform = this._transform,
clipRect = this._clipRec,
layerRec = this._layerRec,
};
this.stack.Push(state);
}
public void restore() {
var layerRect = this._layerRec;
var state = this._stack.Pop();
this._transform = state.transform;
this._clipRec = state.clipRect;
this._layerRec = state.layerRec;
RenderTexture.active = this._layerRec != null ? this._layerRec.texture : null;
GL.PopMatrix();
if (layerRect != null) {
this.prepareGL(LayerCanvas.guiTextureClipMat);
Graphics.DrawTexture(layerRect.bounds.toRect(), layerRect.texture,
new UnityEngine.Rect(0.0f, 0.0f, 1f, 1f), 0, 0, 0, 0,
layerRect.paint.color.toColor(), LayerCanvas.guiTextureClipMat);
RenderTexture.ReleaseTemporary(layerRect.texture);
layerRect.texture = null;
}
}
public void concat(Matrix4x4 transform) {
this._transform = transform * this._transform;
}
public void saveLayer(Rect bounds, Paint paint) {
this.save();
var textureWidth = (int) Math.Round(bounds.width * EditorGUIUtility.pixelsPerPoint);
var textureHeight = (int) Math.Round(bounds.height * EditorGUIUtility.pixelsPerPoint);
var texture = RenderTexture.GetTemporary(
textureWidth, textureHeight, 32,
RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);
RenderTexture.active = texture;
GL.PushMatrix();
GL.LoadPixelMatrix((float) bounds.left, (float) bounds.right, (float) bounds.bottom, (float) bounds.top);
GL.Clear(true, true, new UnityEngine.Color(0, 0, 0, 0));
this._layerRec = new LayerRec {
bounds = bounds,
paint = paint,
texture = texture,
};
this._transform = Matrix4x4.identity;
this._clipRec = null;
}
public void drawPicture(Picture picture) {
var drawCmds = picture.drawCmds;
foreach (var drawCmd in drawCmds) {
if (drawCmd is DrawPloygon4) {
var drawPloygon4 = (DrawPloygon4) drawCmd;
if (drawPloygon4.color.alpha > 0) {
Vector3[] verts = new Vector3 [drawPloygon4.points.Length];
for (int i = 0; i < drawPloygon4.points.Length; i++) {
verts[i] = drawPloygon4.points[i].toVector();
}
this.prepareGL(LayerCanvas.linesMat);
LayerCanvas.linesMat.SetPass(0);
GL.Begin(GL.TRIANGLES);
GL.Color(drawPloygon4.color.toColor());
for (int index = 0; index < 2; ++index) {
GL.Vertex(verts[index * 2]);
GL.Vertex(verts[index * 2 + 1]);
GL.Vertex(verts[(index * 2 + 2) % 4]);
GL.Vertex(verts[index * 2]);
GL.Vertex(verts[(index * 2 + 2) % 4]);
GL.Vertex(verts[index * 2 + 1]);
}
GL.End();
}
}
}
}
private void prepareGL(Material mat) {
if (this._clipRec != null) {
mat.SetMatrix("UIWidgets_GUIClipMatrix", this._clipRec.transform.inverse);
if (this._clipRec.rect != null) {
var rect = this._clipRec.rect;
mat.SetVector("UIWidgets_GUIClipRect", new Vector4(
(float) rect.left,
(float) rect.top,
(float) rect.width,
(float) rect.height));
mat.SetVector("UIWidgets_GUIClipRectRadius", new Vector4(0, 0, 0, 0));
} else {
var rrect = this._clipRec.rrect;
var rect = rrect.outerRect;
mat.SetVector("UIWidgets_GUIClipRect", new Vector4(
(float) rect.left,
(float) rect.top,
(float) rect.width,
(float) rect.height));
mat.SetVector("UIWidgets_GUIClipRectRadius",
new Vector4(
(float) rrect.tlRadius,
(float) rrect.trRadius,
(float) rrect.brRadius,
(float) rrect.blRadius));
}
} else {
mat.SetMatrix("UIWidgets_GUIClipMatrix", Matrix4x4.identity);
var rect = Rect.largest;
mat.SetVector("UIWidgets_GUIClipRect", new Vector4(
(float) rect.left,
(float) rect.top,
(float) rect.width,
(float) rect.height));
mat.SetVector("UIWidgets_GUIClipRectRadius", new Vector4(0, 0, 0, 0));
}
GL.MultMatrix(this._transform);
}
public void clipRect(Rect rect) {
if (rect.isInfinite) {
return;
}
this.pushClipRect(rect, this._transform);
}
public void clipRRect(RRect rect) {
if (rect.isInfinite) {
return;
}
this.pushClipRRect(rect, this._transform);
}
private void pushClipRect(Rect clipRect, Matrix4x4 transform) {
if (this._clipRec != null) {
throw new Exception("already a clipRec, considering using saveLayer.");
}
this._clipRec = new ClipRec(transform, rect: clipRect);
}
private void pushClipRRect(RRect clipRRect, Matrix4x4 transform) {
if (this._clipRec != null) {
throw new Exception("already a clipRec, considering using saveLayer.");
}
this._clipRec = new ClipRec(transform, rrect: clipRRect);
}
private class ClipRec {
public ClipRec(Matrix4x4 transform, Rect rect = null, RRect rrect = null) {
this.transform = transform;
this.rect = rect;
this.rrect = rrect;
}
public readonly Matrix4x4 transform;
public readonly Rect rect;
public readonly RRect rrect;
}
private class LayerRec {
public Rect bounds;
public Paint paint;
public RenderTexture texture;
}
private class CanvasRec {
public Matrix4x4 transform;
public ClipRec clipRect;
public LayerRec layerRec;
}
}
}

2
Assets/UIWidgets/flow/picture_layer.cs


}
public override void preroll(PrerollContext context, Matrix4x4 matrix) {
var bounds = this._picture.cullRect().shift(this._offset);
var bounds = this._picture.paintBounds.shift(this._offset);
this.paintBounds = bounds;
}

12
Assets/UIWidgets/painting/box_border.cs


if (this.isSameColor) {
paint.color = this.top.color;
canvas.drawRect(paint, rect,
canvas.drawRect(rect,
borderRadius ?? BorderRadius.zero);
borderRadius ?? BorderRadius.zero, paint);
return;
}

new Offset(rect.right - this.right.width, rect.top + this.top.width),
new Offset(rect.left + this.right.width, rect.top + this.top.width),
};
canvas.drawPloygon4(paint, points);
canvas.drawPloygon4(points, paint);
}
if (this.right.width > 0) {

new Offset(rect.right - this.right.width, rect.bottom - this.bottom.width),
new Offset(rect.right - this.right.width, rect.top + this.top.width),
};
canvas.drawPloygon4(paint, points);
canvas.drawPloygon4(points, paint);
}
if (this.bottom.width > 0) {

new Offset(rect.left + this.left.width, rect.bottom - this.bottom.width),
new Offset(rect.right - this.right.width, rect.bottom - this.bottom.width),
};
canvas.drawPloygon4(paint, points);
canvas.drawPloygon4(points, paint);
}
if (this.left.width > 0) {

new Offset(rect.left + this.left.width, rect.top + this.top.width),
new Offset(rect.left + this.left.width, rect.bottom - this.bottom.width),
};
canvas.drawPloygon4(paint, points);
canvas.drawPloygon4(points, paint);
}
}
}

4
Assets/UIWidgets/painting/box_decoration.cs


}
public void _paintBox(Canvas canvas, Rect rect, Paint paint) {
canvas.drawRect(paint, rect, borderRadius: this._decoration.borderRadius);
canvas.drawRect(rect, null, this._decoration.borderRadius, paint);
}
public void _paintShadows(Canvas canvas, Rect rect) {

color = boxShadow.color,
blurSigma = boxShadow.blurRadius
};
canvas.drawRectShadow(paint, bounds);
canvas.drawRectShadow(bounds, paint);
}
}

25
Assets/UIWidgets/painting/matrix_utils.cs


return new Offset(transformed3.x, transformed3.y);
}
public static Rect transformRect(Matrix4x4 transform, Rect rect, out bool isRect) {
var topLeft = MatrixUtils.transformPoint(transform, rect.topLeft);
var topRight = MatrixUtils.transformPoint(transform, rect.topRight);
var bottomLeft = MatrixUtils.transformPoint(transform, rect.bottomLeft);
var bottomRight = MatrixUtils.transformPoint(transform, rect.bottomRight);
public static Rect transformRect(Matrix4x4 transform, Offset[] points, out bool isRect) {
if (points == null || points.Length != 4) {
throw new Exception("expected 4 points");
}
var topLeft = MatrixUtils.transformPoint(transform, points[0]);
var topRight = MatrixUtils.transformPoint(transform, points[1]);
var bottomLeft = MatrixUtils.transformPoint(transform, points[2]);
var bottomRight = MatrixUtils.transformPoint(transform, points[3]);
isRect = topLeft.dy == topRight.dy
&& topRight.dx == bottomRight.dx

var bottom = Math.Max(Math.Max(Math.Max(topLeft.dy, topRight.dy), bottomLeft.dy), bottomRight.dy);
return Rect.fromLTRB(left, top, right, bottom);
}
public static Rect transformRect(Matrix4x4 transform, Rect rect, out bool isRect) {
return MatrixUtils.transformRect(transform,
new[] {rect.topLeft, rect.topRight, rect.bottomLeft, rect.bottomRight},
out isRect);
}
public static Rect transformRect(Matrix4x4 transform, Offset[] points) {
bool isRect;
return MatrixUtils.transformRect(transform, points, out isRect);
}
public static Rect transformRect(Matrix4x4 transform, Rect rect) {

2
Assets/UIWidgets/rendering/object.cs


public void _startRecording() {
this._currentLayer = new PictureLayer();
this._recorder = new PictureRecorder();
this._canvas = new Canvas(this._recorder);
this._canvas = new RecorderCanvas(this._recorder);
this._containerLayer.append(this._currentLayer);
}

3
Assets/UIWidgets/ui/painting.meta


fileFormatVersion: 2
guid: 006314a4f502487eb7e3081964f0b0a0
timeCreated: 1534751521

98
Assets/UIWidgets/ui/painting/canvas.cs


using UIWidgets.painting;
using UnityEngine;
namespace UIWidgets.ui {
public interface Canvas {
void drawPloygon4(Offset[] points, Paint paint);
void drawRect(Rect rect, BorderWidth borderWidth, BorderRadius borderRadius, Paint paint);
void drawRectShadow(Rect rect, Paint paint);
void drawPicture(Picture picture);
void concat(Matrix4x4 transform);
void save();
void saveLayer(Rect rect, Paint paint);
void restore();
void clipRect(Rect rect);
void clipRRect(RRect rrect);
}
public class RecorderCanvas : Canvas {
public RecorderCanvas(PictureRecorder recorder) {
this._recorder = recorder;
}
private readonly PictureRecorder _recorder;
public void drawPloygon4(Offset[] points, Paint paint) {
this._recorder.addDrawCmd(new DrawPloygon4 {
points = points,
paint = paint,
});
}
public void drawRect(Rect rect, BorderWidth borderWidth, BorderRadius borderRadius, Paint paint) {
this._recorder.addDrawCmd(new DrawRect {
rect = rect,
borderWidth = borderWidth,
borderRadius = borderRadius,
paint = paint,
});
}
public void drawRectShadow(Rect rect, Paint paint) {
this._recorder.addDrawCmd(new DrawRectShadow {
rect = rect,
paint = paint,
});
}
public void drawPicture(Picture picture) {
this._recorder.addDrawCmd(new DrawPicture {
picture = picture,
});
}
public void concat(Matrix4x4 transform) {
this._recorder.addDrawCmd(new DrawConcat {
transform = transform,
});
}
public void save() {
this._recorder.addDrawCmd(new DrawSave {
});
}
public void saveLayer(Rect rect, Paint paint) {
this._recorder.addDrawCmd(new DrawSaveLayer {
rect = rect,
paint = paint,
});
}
public void restore() {
this._recorder.addDrawCmd(new DrawRestore {
});
}
public void clipRect(Rect rect) {
this._recorder.addDrawCmd(new DrawClipRect {
rect = rect,
});
}
public void clipRRect(RRect rrect) {
this._recorder.addDrawCmd(new DrawClipRRect {
rrect = rrect,
});
}
}
}

3
Assets/UIWidgets/ui/painting/canvas.cs.meta


fileFormatVersion: 2
guid: f25baa0ac29d4c9f861225ab69ba49c3
timeCreated: 1534751681

51
Assets/UIWidgets/ui/painting/draw_cmd.cs


using UIWidgets.painting;
using UnityEngine;
namespace UIWidgets.ui {
public interface DrawCmd {
}
public class DrawPloygon4 : DrawCmd {
public Offset[] points;
public Paint paint;
}
public class DrawRect : DrawCmd {
public Rect rect;
public BorderWidth borderWidth;
public BorderRadius borderRadius;
public Paint paint;
}
public class DrawRectShadow : DrawCmd {
public Rect rect;
public Paint paint;
}
public class DrawPicture : DrawCmd {
public Picture picture;
}
public class DrawConcat : DrawCmd {
public Matrix4x4 transform;
}
public class DrawSave : DrawCmd {
}
public class DrawSaveLayer : DrawCmd {
public Rect rect;
public Paint paint;
}
public class DrawRestore : DrawCmd {
}
public class DrawClipRect : DrawCmd {
public Rect rect;
}
public class DrawClipRRect : DrawCmd {
public RRect rrect;
}
}

3
Assets/UIWidgets/ui/painting/draw_cmd.cs.meta


fileFormatVersion: 2
guid: c740916fa5394cc99edf46dde375c0bb
timeCreated: 1534752179

281
Assets/UIWidgets/ui/painting/editor_canvas.cs


using System;
using System.Collections.Generic;
using UIWidgets.painting;
using UnityEditor;
using UnityEngine;
namespace UIWidgets.ui {
public class EditorCanvas : Canvas {
static EditorCanvas() {
var shader = Shader.Find("UIWidgets/2D Handles Lines");
if (shader == null) {
throw new Exception("UIWidgets/2D Handles Lines not found");
}
EditorCanvas.linesMat = new Material(shader);
EditorCanvas.linesMat.hideFlags = HideFlags.HideAndDontSave;
shader = Shader.Find("UIWidgets/GUIRoundedRect");
if (shader == null) {
throw new Exception("UIWidgets/GUIRoundedRect not found");
}
EditorCanvas.guiRoundedRectMat = new Material(shader);
EditorCanvas.guiRoundedRectMat.hideFlags = HideFlags.HideAndDontSave;
shader = Shader.Find("UIWidgets/GUITextureClip");
if (shader == null) {
throw new Exception("UIWidgets/GUITextureClip not found");
}
EditorCanvas.guiTextureClipMat = new Material(shader);
EditorCanvas.guiTextureClipMat.hideFlags = HideFlags.HideAndDontSave;
}
private static readonly Material linesMat;
private static readonly Material guiRoundedRectMat;
private static readonly Material guiTextureClipMat;
private Matrix4x4 _transform;
private ClipRec _clipRec;
private LayerRec _layerRec;
private Stack<CanvasRec> _stack;
private Stack<CanvasRec> stack {
get { return this._stack ?? (this._stack = new Stack<CanvasRec>()); }
}
public EditorCanvas() {
this._transform = Matrix4x4.identity;
}
public void drawPloygon4(Offset[] points, Paint paint) {
var color = paint.color;
if (color.alpha > 0) {
Vector3[] verts = new Vector3 [points.Length];
for (int i = 0; i < points.Length; i++) {
verts[i] = points[i].toVector();
}
this.prepareGL(EditorCanvas.linesMat);
EditorCanvas.linesMat.SetPass(0);
GL.Begin(GL.TRIANGLES);
GL.Color(color.toColor());
for (int index = 0; index < 2; ++index) {
GL.Vertex(verts[index * 2]);
GL.Vertex(verts[index * 2 + 1]);
GL.Vertex(verts[(index * 2 + 2) % 4]);
GL.Vertex(verts[index * 2]);
GL.Vertex(verts[(index * 2 + 2) % 4]);
GL.Vertex(verts[index * 2 + 1]);
}
GL.End();
}
}
public void drawRect(Rect rect, BorderWidth borderWidth, BorderRadius borderRadius, Paint paint) {
throw new NotImplementedException();
}
public void drawRectShadow(Rect rect, Paint paint) {
throw new NotImplementedException();
}
public void drawPicture(Picture picture) {
var drawCmds = picture.drawCmds;
foreach (var drawCmd in drawCmds) {
if (drawCmd is DrawPloygon4) {
var drawPloygon4 = (DrawPloygon4) drawCmd;
this.drawPloygon4(drawPloygon4.points, drawPloygon4.paint);
} else if (drawCmd is DrawRect) {
var drawRect = (DrawRect) drawCmd;
this.drawRect(drawRect.rect, drawRect.borderWidth, drawRect.borderRadius, drawRect.paint);
} else if (drawCmd is DrawRectShadow) {
var drawRectShadow = (DrawRectShadow) drawCmd;
this.drawRectShadow(drawRectShadow.rect, drawRectShadow.paint);
} else if (drawCmd is DrawPicture) {
var drawPicture = (DrawPicture) drawCmd;
this.drawPicture(drawPicture.picture);
} else if (drawCmd is DrawConcat) {
this.concat(((DrawConcat) drawCmd).transform);
} else if (drawCmd is DrawSave) {
this.save();
} else if (drawCmd is DrawSaveLayer) {
var drawSaveLayer = (DrawSaveLayer) drawCmd;
this.saveLayer(drawSaveLayer.rect, drawSaveLayer.paint);
} else if (drawCmd is DrawRestore) {
this.restore();
} else if (drawCmd is DrawClipRect) {
var drawClipRect = (DrawClipRect) drawCmd;
this.clipRect(drawClipRect.rect);
} else if (drawCmd is DrawClipRRect) {
var drawClipRRect = (DrawClipRRect) drawCmd;
this.clipRRect(drawClipRRect.rrect);
} else {
throw new Exception("unknown drawCmd: " + drawCmd);
}
}
}
public void concat(Matrix4x4 transform) {
this._transform = transform * this._transform;
}
public void save() {
var state = new CanvasRec {
transform = this._transform,
clipRect = this._clipRec,
layerRec = this._layerRec,
};
this.stack.Push(state);
}
public void saveLayer(Rect bounds, Paint paint) {
this.save();
var textureWidth = (int) Math.Round(bounds.width * EditorGUIUtility.pixelsPerPoint);
var textureHeight = (int) Math.Round(bounds.height * EditorGUIUtility.pixelsPerPoint);
var texture = RenderTexture.GetTemporary(
textureWidth, textureHeight, 32,
RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);
RenderTexture.active = texture;
GL.PushMatrix();
GL.LoadPixelMatrix((float) bounds.left, (float) bounds.right, (float) bounds.bottom, (float) bounds.top);
GL.Clear(true, true, new UnityEngine.Color(0, 0, 0, 0));
this._layerRec = new LayerRec {
bounds = bounds,
paint = paint,
texture = texture,
};
this._transform = Matrix4x4.identity;
this._clipRec = null;
}
public void restore() {
var layerRect = this._layerRec;
var state = this._stack.Pop();
this._transform = state.transform;
this._clipRec = state.clipRect;
this._layerRec = state.layerRec;
RenderTexture.active = this._layerRec != null ? this._layerRec.texture : null;
GL.PopMatrix();
if (layerRect != null) {
this.prepareGL(EditorCanvas.guiTextureClipMat);
Graphics.DrawTexture(layerRect.bounds.toRect(), layerRect.texture,
new UnityEngine.Rect(0.0f, 0.0f, 1f, 1f), 0, 0, 0, 0,
layerRect.paint.color.toColor(), EditorCanvas.guiTextureClipMat);
RenderTexture.ReleaseTemporary(layerRect.texture);
layerRect.texture = null;
}
}
public void clipRect(Rect rect) {
if (rect.isInfinite) {
return;
}
this.pushClipRect(rect, this._transform);
}
public void clipRRect(RRect rect) {
if (rect.isInfinite) {
return;
}
this.pushClipRRect(rect, this._transform);
}
private void pushClipRect(Rect clipRect, Matrix4x4 transform) {
if (this._clipRec != null) {
throw new Exception("already a clipRec, considering using saveLayer.");
}
this._clipRec = new ClipRec(transform, rect: clipRect);
}
private void pushClipRRect(RRect clipRRect, Matrix4x4 transform) {
if (this._clipRec != null) {
throw new Exception("already a clipRec, considering using saveLayer.");
}
this._clipRec = new ClipRec(transform, rrect: clipRRect);
}
private void prepareGL(Material mat) {
if (this._clipRec != null) {
mat.SetMatrix("UIWidgets_GUIClipMatrix", this._clipRec.transform.inverse);
if (this._clipRec.rect != null) {
var rect = this._clipRec.rect;
mat.SetVector("UIWidgets_GUIClipRect", new Vector4(
(float) rect.left,
(float) rect.top,
(float) rect.width,
(float) rect.height));
mat.SetVector("UIWidgets_GUIClipRectRadius", new Vector4(0, 0, 0, 0));
} else {
var rrect = this._clipRec.rrect;
var rect = rrect.outerRect;
mat.SetVector("UIWidgets_GUIClipRect", new Vector4(
(float) rect.left,
(float) rect.top,
(float) rect.width,
(float) rect.height));
mat.SetVector("UIWidgets_GUIClipRectRadius",
new Vector4(
(float) rrect.tlRadius,
(float) rrect.trRadius,
(float) rrect.brRadius,
(float) rrect.blRadius));
}
} else {
mat.SetMatrix("UIWidgets_GUIClipMatrix", Matrix4x4.identity);
var rect = Rect.largest;
mat.SetVector("UIWidgets_GUIClipRect", new Vector4(
(float) rect.left,
(float) rect.top,
(float) rect.width,
(float) rect.height));
mat.SetVector("UIWidgets_GUIClipRectRadius", new Vector4(0, 0, 0, 0));
}
GL.MultMatrix(this._transform);
}
private class ClipRec {
public ClipRec(Matrix4x4 transform, Rect rect = null, RRect rrect = null) {
this.transform = transform;
this.rect = rect;
this.rrect = rrect;
}
public readonly Matrix4x4 transform;
public readonly Rect rect;
public readonly RRect rrect;
}
private class LayerRec {
public Rect bounds;
public Paint paint;
public RenderTexture texture;
}
private class CanvasRec {
public Matrix4x4 transform;
public ClipRec clipRect;
public LayerRec layerRec;
}
}
}

3
Assets/UIWidgets/ui/painting/editor_canvas.cs.meta


fileFormatVersion: 2
guid: df28a226e3b24b8f8909bc1ab7a20262
timeCreated: 1534757880

160
Assets/UIWidgets/ui/painting/painting.cs


using System;
using UIWidgets.painting;
using UnityEngine;
namespace UIWidgets.ui {
public class Color : IEquatable<Color> {
public Color(long value) {
this.value = value & 0xFFFFFFFF;
}
public static Color fromARGB(int a, int r, int g, int b) {
return new Color(
(((a & 0xff) << 24) |
((r & 0xff) << 16) |
((g & 0xff) << 8) |
((b & 0xff) << 0)) & 0xFFFFFFFF);
}
public static Color fromRGBO(int r, int g, int b, double opacity) {
return new Color(
((((int) (opacity * 0xff) & 0xff) << 24) |
((r & 0xff) << 16) |
((g & 0xff) << 8) |
((b & 0xff) << 0)) & 0xFFFFFFFF);
}
public readonly long value;
public int alpha {
get { return (int) ((0xff000000 & this.value) >> 24); }
}
public double opacity {
get { return this.alpha / 255.0; }
}
public int red {
get { return (int) ((0x00ff0000 & this.value) >> 16); }
}
public int green {
get { return (int) ((0x0000ff00 & this.value) >> 8); }
}
public int blue {
get { return (int) ((0x000000ff & this.value) >> 0); }
}
public Color withAlpha(int a) {
return Color.fromARGB(a, this.red, this.green, this.blue);
}
public Color withOpacity(double opacity) {
return this.withAlpha((int) (opacity * 255));
}
public Color withRed(int r) {
return Color.fromARGB(this.alpha, r, this.green, this.blue);
}
public Color withGreen(int g) {
return Color.fromARGB(this.alpha, this.red, g, this.blue);
}
public Color withBlue(int b) {
return Color.fromARGB(this.alpha, this.red, this.green, b);
}
public bool Equals(Color other) {
if (object.ReferenceEquals(null, other)) return false;
if (object.ReferenceEquals(this, other)) return true;
return this.value == other.value;
}
public override bool Equals(object obj) {
if (object.ReferenceEquals(null, obj)) return false;
if (object.ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return this.Equals((Color) obj);
}
public override int GetHashCode() {
return this.value.GetHashCode();
}
public static bool operator ==(Color a, Color b) {
return object.ReferenceEquals(a, null) ? object.ReferenceEquals(b, null) : a.Equals(b);
}
public static bool operator !=(Color a, Color b) {
return !(a == b);
}
}
public class Paint {
public Color color;
public double blurSigma;
}
public static class Conversions {
public static UnityEngine.Color toColor(this Color color) {
return new UnityEngine.Color(
color.red / 255f, color.green / 255f, color.blue / 255f, color.alpha / 255f);
}
public static Vector2 toVector(this Offset offset) {
return new Vector2((float) offset.dx, (float) offset.dy);
}
public static UnityEngine.Rect toRect(this Rect rect) {
return new UnityEngine.Rect((float) rect.left, (float) rect.top, (float) rect.width, (float) rect.height);
}
public static Vector4 toVector(this BorderWidth borderWidth) {
return new Vector4((float) borderWidth.left, (float) borderWidth.top, (float) borderWidth.right,
(float) borderWidth.bottom);
}
public static Vector4 toVector(this BorderRadius borderRadius) {
return new Vector4((float) borderRadius.topLeft, (float) borderRadius.topRight,
(float) borderRadius.bottomRight, (float) borderRadius.bottomLeft);
}
}
// public class GUICanvas : Canvas {
// static GUICanvas() {
// GUICanvas.shadowMat = Resources.Load<Material>("UIWidgets_ShadowMat");
// if (GUICanvas.shadowMat == null) {
// throw new Exception("UIWidgets_ShadowShader not found");
// }
// }
//
// public static readonly Material shadowMat;
//
// public override void drawPloygon4(Paint paint, params Offset[] points) {
// Vector3[] vectors = new Vector3 [points.Length];
// for (int i = 0; i < points.Length; i++) {
// vectors[i] = points[i].toVector();
// }
//
// Handles.DrawSolidRectangleWithOutline(vectors, paint.color.toColor(),
// new UnityEngine.Color(0f, 0f, 0f, 0f));
// }
//
// public override void drawRect(Paint paint, Rect rect, BorderWidth borderWidth, BorderRadius borderRadius) {
// GUI.DrawTexture(rect.toRect(), EditorGUIUtility.whiteTexture, ScaleMode.StretchToFill, true, 0,
// paint.color.toColor(), borderWidth.toVector(), borderRadius.toVector());
// }
//
// public override void drawRectShadow(Paint paint, Rect rect) {
// GUICanvas.shadowMat.SetFloatArray("_Rect", new float[] {
// (float) rect.left, (float) rect.top, (float) rect.width, (float) rect.height,
// });
// GUICanvas.shadowMat.SetFloat("_sigma", (float) paint.blurSigma);
//
// Graphics.DrawTexture(rect.toRect(), EditorGUIUtility.whiteTexture,
// new UnityEngine.Rect(0.0f, 0.0f, 1f, 1f), 0, 0, 0, 0, paint.color.toColor(), GUICanvas.shadowMat);
// }
// }
}

157
Assets/UIWidgets/ui/painting/picture.cs


using System;
using System.Collections.Generic;
using UIWidgets.painting;
using UnityEngine;
namespace UIWidgets.ui {
public class Picture {
public Picture(List<DrawCmd> drawCmds, Rect paintBounds) {
this.drawCmds = drawCmds;
this.paintBounds = paintBounds;
}
public readonly List<DrawCmd> drawCmds;
public readonly Rect paintBounds;
}
public class PictureRecorder {
private List<DrawCmd> _drawCmds = new List<DrawCmd>();
private Matrix4x4 _transform;
private Rect _clipRect;
private bool _isInLayer;
private Rect _paintBounds;
private Stack<CanvasRec> _stack;
public PictureRecorder() {
this._transform = Matrix4x4.identity;
this._clipRect = null;
this._isInLayer = false;
this._paintBounds = Rect.zero;
}
private Stack<CanvasRec> stack {
get { return this._stack ?? (this._stack = new Stack<CanvasRec>()); }
}
public Picture endRecording() {
if (this._stack != null && this._stack.Count > 0) {
throw new Exception("unmatched save/restore commands");
}
return new Picture(this._drawCmds, this._paintBounds);
}
public void addDrawCmd(DrawCmd drawCmd) {
this._drawCmds.Add(drawCmd);
if (drawCmd is DrawPloygon4) {
var drawPloygon4 = (DrawPloygon4) drawCmd;
if (drawPloygon4.paint.color.alpha > 0) {
this.addPaintBounds(drawPloygon4.points);
}
} else if (drawCmd is DrawRect) {
var drawRect = (DrawRect) drawCmd;
if (drawRect.paint.color.alpha > 0) {
this.addPaintBounds(drawRect.rect);
}
} else if (drawCmd is DrawRectShadow) {
var drawRectShadow = (DrawRectShadow) drawCmd;
if (drawRectShadow.paint.color.alpha > 0) {
this.addPaintBounds(drawRectShadow.rect);
}
} else if (drawCmd is DrawPicture) {
var drawPicture = (DrawPicture) drawCmd;
this.addPaintBounds(drawPicture.picture.paintBounds);
} else if (drawCmd is DrawConcat) {
this._transform = ((DrawConcat) drawCmd).transform * this._transform;
} else if (drawCmd is DrawSave) {
this.stack.Push(new CanvasRec(
this._transform,
this._clipRect,
this._isInLayer,
null
));
} else if (drawCmd is DrawSaveLayer) {
this.stack.Push(new CanvasRec(
this._transform,
this._clipRect,
this._isInLayer,
this._paintBounds
));
var drawSaveLayer = (DrawSaveLayer) drawCmd;
this._transform = Matrix4x4.identity;
this._clipRect = drawSaveLayer.rect;
this._isInLayer = true;
this._paintBounds = Rect.zero;
} else if (drawCmd is DrawRestore) {
var isLayer = this._isInLayer;
var state = this._stack.Pop();
this._transform = state.transform;
this._clipRect = state.clipRect;
this._isInLayer = state.isInLayer;
if (isLayer) {
var paintBounds = this._paintBounds;
this._paintBounds = state.paintBounds;
this.addPaintBounds(paintBounds);
}
} else if (drawCmd is DrawClipRect) {
var drawClipRect = (DrawClipRect) drawCmd;
this.addClipRect(drawClipRect.rect);
} else if (drawCmd is DrawClipRRect) {
var drawClipRRect = (DrawClipRRect) drawCmd;
this.addClipRect(drawClipRRect.rrect.outerRect);
} else {
throw new Exception("unknown drawCmd: " + drawCmd);
}
}
private void addClipRect(Rect rect) {
if (rect.isInfinite) {
return;
}
if (this._clipRect != null) {
throw new Exception("already a clipRec, considering using saveLayer.");
}
this._clipRect = MatrixUtils.transformRect(this._transform, rect);
}
private void addPaintBounds(Rect paintBounds) {
paintBounds = MatrixUtils.transformRect(this._transform, paintBounds);
if (this._clipRect != null) {
paintBounds = paintBounds.intersect(this._clipRect);
}
this._paintBounds = this._paintBounds.expandToInclude(paintBounds);
}
private void addPaintBounds(Offset[] points) {
var paintBounds = MatrixUtils.transformRect(this._transform, points);
if (this._clipRect != null) {
paintBounds = paintBounds.intersect(this._clipRect);
}
this._paintBounds = this._paintBounds.expandToInclude(paintBounds);
}
private class CanvasRec {
public CanvasRec(Matrix4x4 transform, Rect clipRect, bool isInLayer, Rect paintBounds) {
this.transform = transform;
this.clipRect = clipRect;
this.isInLayer = isInLayer;
this.paintBounds = paintBounds;
}
public readonly Matrix4x4 transform;
public readonly Rect clipRect;
public readonly bool isInLayer;
public readonly Rect paintBounds;
}
}
}

3
Assets/UIWidgets/ui/painting/picture.cs.meta


fileFormatVersion: 2
guid: a8dc3b3def944466b0ba1703d56cdd4d
timeCreated: 1534751696

239
Assets/UIWidgets/ui/painting.cs


using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using UIWidgets.painting;
using UnityEditor;
using UnityEngine;
namespace UIWidgets.ui {
public class Color : IEquatable<Color> {
public Color(long value) {
this.value = value & 0xFFFFFFFF;
}
public static Color fromARGB(int a, int r, int g, int b) {
return new Color(
(((a & 0xff) << 24) |
((r & 0xff) << 16) |
((g & 0xff) << 8) |
((b & 0xff) << 0)) & 0xFFFFFFFF);
}
public static Color fromRGBO(int r, int g, int b, double opacity) {
return new Color(
((((int) (opacity * 0xff) & 0xff) << 24) |
((r & 0xff) << 16) |
((g & 0xff) << 8) |
((b & 0xff) << 0)) & 0xFFFFFFFF);
}
public readonly long value;
public int alpha {
get { return (int) ((0xff000000 & this.value) >> 24); }
}
public double opacity {
get { return this.alpha / 255.0; }
}
public int red {
get { return (int) ((0x00ff0000 & this.value) >> 16); }
}
public int green {
get { return (int) ((0x0000ff00 & this.value) >> 8); }
}
public int blue {
get { return (int) ((0x000000ff & this.value) >> 0); }
}
public Color withAlpha(int a) {
return Color.fromARGB(a, this.red, this.green, this.blue);
}
public Color withOpacity(double opacity) {
return this.withAlpha((int) (opacity * 255));
}
public Color withRed(int r) {
return Color.fromARGB(this.alpha, r, this.green, this.blue);
}
public Color withGreen(int g) {
return Color.fromARGB(this.alpha, this.red, g, this.blue);
}
public Color withBlue(int b) {
return Color.fromARGB(this.alpha, this.red, this.green, b);
}
public bool Equals(Color other) {
if (object.ReferenceEquals(null, other)) return false;
if (object.ReferenceEquals(this, other)) return true;
return this.value == other.value;
}
public override bool Equals(object obj) {
if (object.ReferenceEquals(null, obj)) return false;
if (object.ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return this.Equals((Color) obj);
}
public override int GetHashCode() {
return this.value.GetHashCode();
}
public static bool operator ==(Color a, Color b) {
return object.ReferenceEquals(a, null) ? object.ReferenceEquals(b, null) : a.Equals(b);
}
public static bool operator !=(Color a, Color b) {
return !(a == b);
}
}
public class Canvas {
public Canvas(PictureRecorder recorder) {
this.recorder = recorder;
}
public readonly PictureRecorder recorder;
public void drawPloygon4(Paint paint, params Offset[] points) {
this.recorder.addDrawCmd(new DrawPloygon4 {
color = paint.color,
points = points,
});
}
public void drawRect(Paint paint, Rect rect, BorderWidth borderWidth = null, BorderRadius borderRadius = null) {
this.recorder.addDrawCmd(new DrawRect {
color = paint.color,
rect = rect,
borderWidth = borderWidth,
borderRadius = borderRadius,
});
}
public void drawRectShadow(Paint paint, Rect rect) {
this.recorder.addDrawCmd(new DrawRectShadow {
color = paint.color,
blurSigma = paint.blurSigma,
rect = rect,
});
}
}
public class Picture {
public Picture(List<object> drawCmds) {
this.drawCmds = drawCmds;
}
public readonly List<object> drawCmds;
public Rect cullRect() {
return Rect.zero;
}
}
public class PictureRecorder {
public List<object> _drawCmds = new List<object>();
public Picture endRecording() {
var drawCmd = this._drawCmds;
this._drawCmds = new List<object>();
return new Picture(drawCmd);
}
public void addDrawCmd(object drawCmd) {
this._drawCmds.Add(drawCmd);
}
}
public class DrawPloygon4 {
public Color color;
public Offset[] points;
}
public class DrawRect {
public Color color;
public Rect rect;
public BorderWidth borderWidth;
public BorderRadius borderRadius;
}
public class DrawRectShadow {
public Color color;
public double blurSigma;
public Rect rect;
}
public class Paint {
public Color color;
public double blurSigma;
}
public static class Conversions {
public static UnityEngine.Color toColor(this Color color) {
return new UnityEngine.Color(
color.red / 255f, color.green / 255f, color.blue / 255f, color.alpha / 255f);
}
public static Vector2 toVector(this Offset offset) {
return new Vector2((float) offset.dx, (float) offset.dy);
}
public static UnityEngine.Rect toRect(this Rect rect) {
return new UnityEngine.Rect((float) rect.left, (float) rect.top, (float) rect.width, (float) rect.height);
}
public static Vector4 toVector(this BorderWidth borderWidth) {
return new Vector4((float) borderWidth.left, (float) borderWidth.top, (float) borderWidth.right,
(float) borderWidth.bottom);
}
public static Vector4 toVector(this BorderRadius borderRadius) {
return new Vector4((float) borderRadius.topLeft, (float) borderRadius.topRight,
(float) borderRadius.bottomRight, (float) borderRadius.bottomLeft);
}
}
// public class GUICanvas : Canvas {
// static GUICanvas() {
// GUICanvas.shadowMat = Resources.Load<Material>("UIWidgets_ShadowMat");
// if (GUICanvas.shadowMat == null) {
// throw new Exception("UIWidgets_ShadowShader not found");
// }
// }
//
// public static readonly Material shadowMat;
//
// public override void drawPloygon4(Paint paint, params Offset[] points) {
// Vector3[] vectors = new Vector3 [points.Length];
// for (int i = 0; i < points.Length; i++) {
// vectors[i] = points[i].toVector();
// }
//
// Handles.DrawSolidRectangleWithOutline(vectors, paint.color.toColor(),
// new UnityEngine.Color(0f, 0f, 0f, 0f));
// }
//
// public override void drawRect(Paint paint, Rect rect, BorderWidth borderWidth, BorderRadius borderRadius) {
// GUI.DrawTexture(rect.toRect(), EditorGUIUtility.whiteTexture, ScaleMode.StretchToFill, true, 0,
// paint.color.toColor(), borderWidth.toVector(), borderRadius.toVector());
// }
//
// public override void drawRectShadow(Paint paint, Rect rect) {
// GUICanvas.shadowMat.SetFloatArray("_Rect", new float[] {
// (float) rect.left, (float) rect.top, (float) rect.width, (float) rect.height,
// });
// GUICanvas.shadowMat.SetFloat("_sigma", (float) paint.blurSigma);
//
// Graphics.DrawTexture(rect.toRect(), EditorGUIUtility.whiteTexture,
// new UnityEngine.Rect(0.0f, 0.0f, 1f, 1f), 0, 0, 0, 0, paint.color.toColor(), GUICanvas.shadowMat);
// }
// }
}

/Assets/UIWidgets/ui/painting.cs.meta → /Assets/UIWidgets/ui/painting/painting.cs.meta

正在加载...
取消
保存