浏览代码

stack & aspect ratio

/main
gewentao 6 年前
当前提交
6b8f30d9
共有 5 个文件被更改,包括 568 次插入3 次删除
  1. 30
      Assets/UIWidgets/Tests/Widgets.cs
  2. 64
      Assets/UIWidgets/painting/alignment.cs
  3. 103
      Assets/UIWidgets/rendering/proxy_box.cs
  4. 298
      Assets/UIWidgets/rendering/stack.cs
  5. 76
      Assets/UIWidgets/widgets/basic.cs

30
Assets/UIWidgets/Tests/Widgets.cs


this.flexColumn,
this.containerSimple,
this.eventsPage,
this.stack
};
this._optionStrings = this._options.Select(x => x.Method.Name).ToArray();
this._selected = 0;

// if local image test
if (selected == 0) {
localImagePath = EditorGUILayout.TextField(localImagePath);
if (this._selected != selected) {
this._selected = selected;
this.windowAdapter.attachRootWidget(null);

var rootWidget = this._options[this._selected]();
this.windowAdapter.attachRootWidget(rootWidget);
}
} else if (selected != this._selected || !this.hasInvoked) {
}
else if (selected != this._selected || !this.hasInvoked) {
this._selected = selected;
this.hasInvoked = true;

void OnDestroy() {
this.windowAdapter = null;
}
Widget stack() {
var image = new widgets.Container(
width: 150,
height: 150,
child: widgets.Image.network(
"https://tse3.mm.bing.net/th?id=OIP.XOAIpvR1kh-CzISe_Nj9GgHaHs&pid=Api",
width: 100,
height: 100
)
);
var text = new widgets.Container(
width: 150,
height: 150,
child: new Text("TTTTTTTTTTTTTTTTEST")
);
List<Widget> rowImages = new List<Widget>();
rowImages.Add(image);
rowImages.Add(text);
return new Stack(
children: rowImages,
alignment: AlignmentDirectional.center
);
}
Widget localImage() {

64
Assets/UIWidgets/painting/alignment.cs


return !(a == b);
}
}
public class AlignmentDirectional : IEquatable<AlignmentDirectional> {
public AlignmentDirectional(double start, double y) {
this.start = start;
this.y = y;
}
public double start;
public double y;
public static readonly AlignmentDirectional topStart = new AlignmentDirectional(-1.0, -1.0);
public static readonly AlignmentDirectional topCenter = new AlignmentDirectional(0.0, -1.0);
public static readonly AlignmentDirectional topEnd = new AlignmentDirectional(1.0, -1.0);
public static readonly AlignmentDirectional centerStart = new AlignmentDirectional(-1.0, 0.0);
public static readonly AlignmentDirectional center = new AlignmentDirectional(0.0, 0.0);
public static readonly AlignmentDirectional centerEnd = new AlignmentDirectional(1.0, 0.0);
public static readonly AlignmentDirectional bottomStart = new AlignmentDirectional(-1.0, 1.0);
public static readonly AlignmentDirectional bottomCenter = new AlignmentDirectional(0.0, 1.0);
public static readonly AlignmentDirectional bottomEnd = new AlignmentDirectional(1.0, 1.0);
public AlignmentDirectional add(AlignmentDirectional other) {
return this + other;
}
public static AlignmentDirectional operator -(AlignmentDirectional a, AlignmentDirectional b) {
return new AlignmentDirectional(a.start - b.start, a.y - b.y);
}
public static AlignmentDirectional operator +(AlignmentDirectional a, AlignmentDirectional b) {
return new AlignmentDirectional(a.start + b.start, a.y + b.y);
}
public static AlignmentDirectional operator -(AlignmentDirectional a) {
return new AlignmentDirectional(-a.start, -a.y);
}
public static AlignmentDirectional operator *(AlignmentDirectional a, double b) {
return new AlignmentDirectional(a.start * b, a.y * b);
}
public static AlignmentDirectional operator /(AlignmentDirectional a, double b) {
return new AlignmentDirectional(a.start / b, a.y / b);
}
public static AlignmentDirectional operator %(AlignmentDirectional a, double b) {
return new AlignmentDirectional(a.start % b, a.y % b);
}
public bool Equals(AlignmentDirectional other) {
if (object.ReferenceEquals(null, other)) return false;
if (object.ReferenceEquals(this, other)) return true;
return this.start.Equals(other.start) && this.y.Equals(other.y);
}
public Alignment resolve(TextDirection direction) {
switch (direction) {
case TextDirection.rtl:
return new Alignment(-start, y);
case TextDirection.ltr:
return new Alignment(start, y);
}
return null;
}
}
}

103
Assets/UIWidgets/rendering/proxy_box.cs


}
}
public class RenderAspectRatio : RenderProxyBox {
public RenderAspectRatio(double aspectRatio, RenderBox child = null) : base(child) {
this._aspectRatio = aspectRatio;
}
public double aspectRatio {
get { return _aspectRatio; }
set {
if (_aspectRatio == value) {
return;
}
_aspectRatio = value;
markNeedsLayout();
}
}
private double _aspectRatio;
protected override double computeMinIntrinsicWidth(double height) {
if (!double.IsInfinity(height))
return height * _aspectRatio;
if (child != null)
return child.getMinIntrinsicWidth(height);
return 0.0;
}
protected override double computeMaxIntrinsicWidth(double height) {
if (!double.IsInfinity(height))
return height * _aspectRatio;
if (child != null) {
return child.getMaxIntrinsicWidth(height);
}
return 0.0;
}
protected override double computeMinIntrinsicHeight(double width) {
if (!double.IsInfinity(width))
return width / _aspectRatio;
if (child != null) {
return child.getMinIntrinsicHeight(width);
}
return 0.0;
}
protected override double computeMaxIntrinsicHeight(double width) {
if (!double.IsInfinity(width))
return width / _aspectRatio;
if (child != null) {
return child.getMaxIntrinsicHeight(width);
}
return 0.0;
}
Size _applyAspectRatio(BoxConstraints constraints) {
D.assert(constraints.debugAssertIsValid());
if (constraints.isTight) {
return constraints.smallest;
}
double width = constraints.maxWidth;
double height = width / _aspectRatio;
if (width > constraints.maxWidth) {
width = constraints.maxWidth;
height = width / _aspectRatio;
}
if (height > constraints.maxHeight) {
height = constraints.maxHeight;
width = height * _aspectRatio;
}
if (width < constraints.minWidth) {
width = constraints.minWidth;
height = width / _aspectRatio;
}
if (height < constraints.minHeight) {
height = constraints.minHeight;
width = height * _aspectRatio;
}
return constraints.constrain(new Size(width, height));
}
protected override void performLayout() {
size = _applyAspectRatio(constraints);
if (child != null) {
child.layout(BoxConstraints.tight(size));
}
}
public override void debugFillProperties(DiagnosticPropertiesBuilder properties) {
base.debugFillProperties(properties);
properties.add(new DoubleProperty("aspectRatio", aspectRatio));
}
}
public class RenderOpacity : RenderProxyBox {
public RenderOpacity(double opacity = 1.0, RenderBox child = null) : base(child) {
D.assert(opacity >= 0.0 && opacity <= 1.0);

298
Assets/UIWidgets/rendering/stack.cs


using System;
using System.Collections.Generic;
using UIWidgets.painting;
using UIWidgets.foundation;
using UnityEngine;
using Rect = UIWidgets.ui.Rect;
namespace UIWidgets.rendering {
public class RelativeRect : IEquatable<RelativeRect> {

public static bool operator !=(RelativeRect a, RelativeRect b) {
return !(a == b);
}
}
public class StackParentData : ContainerParentDataMixinBoxParentData<RenderBox> {
public double? top;
public double? right;
public double? bottom;
public double? left;
public double? width;
public double? height;
public bool isPositioned {
get { return top != null || right != null || bottom != null || left != null || width != null || height != null; }
}
private RelativeRect rect {
get { return RelativeRect.fromLTRB(left ?? 0.0, top ?? 0.0, right ?? 0.0, bottom ?? 0.0); }
set {
top = value.top;
right = value.right;
bottom = value.bottom;
left = value.left;
}
}
}
public enum StackFit {
loose,
expand,
passthrough,
}
public enum Overflow {
visible,
clip,
}
public class RenderStack : RenderBoxContainerDefaultsMixinContainerRenderObjectMixinRenderBox<RenderBox,
StackParentData> {
public RenderStack(
TextDirection? textDirection,
StackFit? fit,
Overflow? overflow,
List<RenderBox> children = null,
AlignmentDirectional alignment = null) {
this._alignment = alignment ?? AlignmentDirectional.topStart;
this._textDirection = textDirection ?? TextDirection.ltr;
this._fit = fit ?? StackFit.loose;
this._overflow = overflow ?? Overflow.clip;
addAll(children);
}
bool _hasVisualOverflow = false;
public override void setupParentData(RenderObject child) {
Debug.Log(child);
if (!(child.parentData is StackParentData)) {
child.parentData = new StackParentData();
}
}
Alignment _resolvedAlignment;
void _resolve() {
if (_resolvedAlignment != null)
return;
_resolvedAlignment = alignment.resolve(textDirection);
}
void _markNeedResolution() {
_resolvedAlignment = null;
markNeedsLayout();
}
private AlignmentDirectional _alignment;
public AlignmentDirectional alignment {
get { return _alignment; }
set {
if (_alignment == value)
return;
_alignment = value;
_markNeedResolution();
}
}
private TextDirection _textDirection;
public TextDirection textDirection {
get { return _textDirection; }
set {
if (_textDirection == value)
return;
_textDirection = value;
_markNeedResolution();
}
}
private StackFit _fit;
public StackFit fit {
get { return _fit; }
set {
if (_fit == value)
return;
_fit = value;
markNeedsLayout();
}
}
private Overflow _overflow;
public Overflow overflow {
get { return _overflow; }
set {
if (_overflow == value)
return;
_overflow = value;
markNeedsPaint();
}
}
public delegate double mainChildSizeGetter(RenderBox child);
double _getIntrinsicDimension(mainChildSizeGetter getter) {
double extent = 0.0;
RenderBox child = firstChild;
while (child != null) {
StackParentData childParentData = (StackParentData) child.parentData;
if (!childParentData.isPositioned)
extent = Math.Max(extent, getter(child));
D.assert(child.parentData == childParentData);
if (childParentData != null) child = childParentData.nextSibling;
}
return extent;
}
protected override double computeMinIntrinsicWidth(double height) {
return _getIntrinsicDimension((RenderBox child) => child.getMinIntrinsicWidth(height));
}
protected override double computeMaxIntrinsicWidth(double height) {
return _getIntrinsicDimension((RenderBox child) => child.getMaxIntrinsicWidth(height));
}
protected override double computeMinIntrinsicHeight(double width) {
return _getIntrinsicDimension((RenderBox child) => child.getMinIntrinsicHeight(width));
}
protected override double computeMaxIntrinsicHeight(double width) {
return _getIntrinsicDimension((RenderBox child) => child.getMaxIntrinsicHeight(width));
}
protected override double? computeDistanceToActualBaseline(TextBaseline baseline) {
return defaultComputeDistanceToHighestActualBaseline(baseline);
}
protected override void performLayout() {
_resolve();
D.assert(_resolvedAlignment != null);
_hasVisualOverflow = false;
bool hasNonPositionedChildren = false;
if (childCount == 0) {
size = constraints.biggest;
return;
}
double width = constraints.minWidth;
double height = constraints.minHeight;
BoxConstraints nonPositionedConstraints = null;
switch (fit) {
case StackFit.loose:
nonPositionedConstraints = constraints.loosen();
break;
case StackFit.expand:
nonPositionedConstraints = BoxConstraints.tight(constraints.biggest);
break;
case StackFit.passthrough:
nonPositionedConstraints = constraints;
break;
}
RenderBox child = firstChild;
while (child != null) {
StackParentData childParentData = (StackParentData) child.parentData;
if (!childParentData.isPositioned) {
hasNonPositionedChildren = true;
child.layout(nonPositionedConstraints, parentUsesSize: true);
Size childSize = child.size;
width = Math.Max(width, childSize.width);
height = Math.Max(height, childSize.height);
}
child = childParentData.nextSibling;
}
if (hasNonPositionedChildren) {
size = new Size(width, height);
D.assert(size.width == constraints.constrainWidth(width));
D.assert(size.height == constraints.constrainHeight(height));
}
else {
size = constraints.biggest;
}
child = firstChild;
while (child != null) {
StackParentData childParentData = (StackParentData) child.parentData;
if (!childParentData.isPositioned) {
childParentData.offset = _resolvedAlignment.alongOffset(size - child.size);
}
else {
BoxConstraints childConstraints = new BoxConstraints();
if (childParentData.left != null && childParentData.right != null)
childConstraints =
childConstraints.tighten(width: size.width - childParentData.right - childParentData.left);
else if (childParentData.width != null)
childConstraints = childConstraints.tighten(width: childParentData.width);
if (childParentData.top != null && childParentData.bottom != null)
childConstraints =
childConstraints.tighten(
height: size.height - childParentData.bottom - childParentData.top);
else if (childParentData.height != null)
childConstraints = childConstraints.tighten(height: childParentData.height);
child.layout(childConstraints, parentUsesSize: true);
double x;
if (childParentData.left != null) {
x = childParentData.left.Value;
}
else if (childParentData.right != null) {
x = size.width - childParentData.right.Value - child.size.width;
}
else {
x = _resolvedAlignment.alongOffset(size - child.size).dx;
}
if (x < 0.0 || x + child.size.width > size.width)
_hasVisualOverflow = true;
double y;
if (childParentData.top != null) {
y = childParentData.top.Value;
}
else if (childParentData.bottom != null) {
y = size.height - childParentData.bottom.Value - child.size.height;
}
else {
y = _resolvedAlignment.alongOffset(size - child.size).dy;
}
if (y < 0.0 || y + child.size.height > size.height)
_hasVisualOverflow = true;
childParentData.offset = new Offset(x, y);
}
D.assert(child.parentData == childParentData);
child = childParentData.nextSibling;
}
}
public void paintStack(PaintingContext context, Offset offset) {
defaultPaint(context, offset);
}
public override void paint(PaintingContext context, Offset offset) {
if (_overflow == Overflow.clip && _hasVisualOverflow) {
context.pushClipRect(needsCompositing, offset, Offset.zero & size, paintStack);
} else {
paintStack(context, offset);
}
}
public override Rect describeApproximatePaintClip(RenderObject childRaw) {
return _hasVisualOverflow ? Offset.zero & size : null;
}
public override void debugFillProperties(DiagnosticPropertiesBuilder properties) {
base.debugFillProperties(properties);
properties.add(new DiagnosticsProperty<AlignmentDirectional>("alignment", alignment));
properties.add(new EnumProperty<StackFit>("fit", fit));
properties.add(new EnumProperty<Overflow>("overflow", overflow));
}
}
}

76
Assets/UIWidgets/widgets/basic.cs


using UIWidgets.rendering;
using UIWidgets.ui;
using UnityEngine;
using UnityEngine.Experimental.UIElements.StyleEnums;
using Overflow = UIWidgets.rendering.Overflow;
using Rect = UIWidgets.ui.Rect;
namespace UIWidgets.widgets {

if ((this.width == double.PositiveInfinity && this.height == double.PositiveInfinity) ||
(this.width == 0.0 && this.height == 0.0)) {
level = DiagnosticLevel.hidden;
} else {
}
else {
level = DiagnosticLevel.info;
}

((RenderFlex) renderObject).textDirection = this.textDirection ?? TextDirection.ltr;
((RenderFlex) renderObject).verticalDirection = this.verticalDirection;
((RenderFlex) renderObject).textBaseline = this.textBaseline ?? TextBaseline.alphabetic;
}
}
public class AspectRatio : SingleChildRenderObjectWidget {
public AspectRatio(
Key key = null,
double aspectRatio = 1.0,
Widget child = null
) : base(key: key, child: child) {
this.aspectRatio = aspectRatio;
}
public readonly double aspectRatio;
public override RenderObject createRenderObject(BuildContext context) {
return new RenderAspectRatio(aspectRatio: aspectRatio);
}
public override void updateRenderObject(BuildContext context, RenderObject renderObject) {
((RenderAspectRatio)renderObject).aspectRatio = aspectRatio;
}
public override void debugFillProperties(DiagnosticPropertiesBuilder properties) {
base.debugFillProperties(properties);
properties.add(new DoubleProperty("aspectRatio", aspectRatio));
}
}
public class Stack : MultiChildRenderObjectWidget {
public Stack(
Key key = null,
AlignmentDirectional alignment = null,
TextDirection? textDirection = null,
StackFit fit = StackFit.loose,
Overflow overflow = Overflow.clip,
List<Widget> children = null
) : base(key: key, children: children) {
this.alignment = alignment ?? AlignmentDirectional.bottomStart;
this.textDirection = textDirection;
this.fit = fit;
this.overflow = overflow;
}
public AlignmentDirectional alignment;
public TextDirection? textDirection;
public StackFit fit;
public rendering.Overflow overflow;
public override RenderObject createRenderObject(BuildContext context) {
return new RenderStack(
textDirection: textDirection ?? Directionality.of(context),
alignment: alignment,
fit: fit,
overflow: overflow
);
}
public override void updateRenderObject(BuildContext context, RenderObject renderObjectRaw) {
var renderObject = (RenderStack) renderObjectRaw;
renderObject.alignment = this.alignment;
renderObject.textDirection = this.textDirection ?? TextDirection.ltr;
renderObject.fit = this.fit;
renderObject.overflow = this.overflow;
}
public override void debugFillProperties(DiagnosticPropertiesBuilder properties) {
base.debugFillProperties(properties);
properties.add(new DiagnosticsProperty<AlignmentDirectional>("alignment", alignment));
properties.add(new EnumProperty<StackFit>("fit", fit));
properties.add(new EnumProperty<Overflow>("overflow", overflow));
}
}

正在加载...
取消
保存