浏览代码

Merge pull request #10 from UnityTech/image

update widgets
/main
GitHub 6 年前
当前提交
7a2e9138
共有 12 个文件被更改,包括 700 次插入29 次删除
  1. 2
      Assets/UIWidgets/rendering/object.cs
  2. 129
      Assets/UIWidgets/rendering/proxy_box.cs
  3. 7
      Assets/UIWidgets/ui/window.cs
  4. 197
      Assets/UIWidgets/widgets/basic.cs
  5. 162
      Assets/UIWidgets/widgets/binding.cs
  6. 14
      Assets/UIWidgets/widgets/image.cs
  7. 43
      Assets/UIWidgets/rendering/error.cs
  8. 3
      Assets/UIWidgets/rendering/error.cs.meta
  9. 136
      Assets/UIWidgets/widgets/container.cs
  10. 3
      Assets/UIWidgets/widgets/container.cs.meta
  11. 30
      Assets/UIWidgets/widgets/ticker_provider.cs
  12. 3
      Assets/UIWidgets/widgets/ticker_provider.cs.meta

2
Assets/UIWidgets/rendering/object.cs


}
}
void pushOpacity(Offset offset, int alpha, PaintingContextCallback painter) {
public void pushOpacity(Offset offset, int alpha, PaintingContextCallback painter) {
this.pushLayer(new OpacityLayer(alpha), painter, offset);
}
}

129
Assets/UIWidgets/rendering/proxy_box.cs


properties.add(new EnumerableProperty<string>("listeners", listeners));
}
}
public class RenderTransform : RenderProxyBox {
public RenderTransform(
Matrix4x4 transform,
Offset origin,
Alignment alignment,
TextDirection textDirection,
RenderBox child = null,
bool transformHitTests = true
) {
this.transform = transform;
this.origin = origin;
this.alignment = alignment;
this.textDirection = textDirection;
this.child = child;
this.transformHitTests = transformHitTests;
}
public Offset origin {
get { return _origin; }
set {
if (_origin.Equals(value)) {
return;
}
_origin = value;
markNeedsPaint();
}
}
private Offset _origin;
public Alignment alignment {
get { return _alignment; }
set {
if (_alignment.Equals(value)) {
return;
}
_alignment = value;
markNeedsPaint();
}
}
private Alignment _alignment;
public TextDirection textDirection {
get { return _textDirection; }
set {
if (_textDirection.Equals(value)) {
return;
}
_textDirection = value;
markNeedsPaint();
}
}
private TextDirection _textDirection;
public bool transformHitTests;
public Matrix4x4 transform {
set {
if (_transform.Equals(value)) {
return;
}
_transform = value;
}
}
private Matrix4x4 _transform;
}
public class RenderOpacity : RenderProxyBox {
public RenderOpacity(RenderBox child = null, double opacity = 1.0) : base(child) {
D.assert(opacity >= 0.0 && opacity <= 1.0);
this._opacity = opacity;
this._alpha = _getAlphaFromOpacity(opacity);
}
public override bool alwaysNeedsCompositing {
get { return base.alwaysNeedsCompositing; }
}
private int _alpha;
private static int _getAlphaFromOpacity(double opacity) {
return (opacity * 255).round();
}
public double opacity {
get { return _opacity; }
set {
D.assert(value >= 0.0 && value <= 1.0);
if (_opacity == value) {
return;
}
bool didNeedCompositing = alwaysNeedsCompositing;
bool wasVisible = _alpha != 0;
_opacity = value;
_alpha = _getAlphaFromOpacity(_opacity);
if (didNeedCompositing != alwaysNeedsCompositing) {
markNeedsCompositingBitsUpdate();
}
markNeedsPaint();
}
}
private double _opacity;
public override void paint(PaintingContext context, Offset offset) {
if (child != null) {
if (_alpha == 0) {
return;
}
}
if (_alpha == 255) {
context.paintChild(child, offset);
return;
}
D.assert(needsCompositing);
context.pushOpacity(offset, _alpha, base.paint);
}
}
}

7
Assets/UIWidgets/ui/window.cs


private VoidCallback _onLocaleChanged;
public VoidCallback onAccessibilityFeaturesChanged {
get { return this._onAccessibilityFeaturesChanged; }
set { this._onAccessibilityFeaturesChanged = value; }
}
private VoidCallback _onAccessibilityFeaturesChanged;
public FrameCallback onBeginFrame {
get { return this._onBeginFrame; }
set { this._onBeginFrame = value; }

197
Assets/UIWidgets/widgets/basic.cs


using UIWidgets.painting;
using UIWidgets.rendering;
using UIWidgets.ui;
using UnityEngine;
using Color = UIWidgets.ui.Color;
using Rect = UIWidgets.ui.Rect;
public class Directionality : InheritedWidget {
public Directionality(
Widget child,
TextDirection textDirection,
Key key = null
) : base(key, child) {
this.textDirection = textDirection;
}
public TextDirection textDirection;
public static TextDirection of(BuildContext context) {
Directionality widget = context.inheritFromWidgetOfExactType(typeof(Directionality)) as Directionality;
return widget == null ? TextDirection.ltr : widget.textDirection;
}
public override Element createElement() {
throw new NotImplementedException();
}
public override bool updateShouldNotify(InheritedWidget oldWidget) {
return textDirection != ((Directionality) oldWidget).textDirection;
}
}
public class Opacity : SingleChildRenderObjectWidget {
public Opacity(double opacity, Key key = null, Widget child = null) : base(key, child) {
this.opacity = opacity;
}
public double opacity;
public override RenderObject createRenderObject(BuildContext context) {
return new RenderOpacity(opacity: opacity);
}
public override void updateRenderObject(BuildContext context, RenderObject renderObject) {
((RenderOpacity) renderObject).opacity = opacity;
}
}
public class LimitedBox : SingleChildRenderObjectWidget {
public LimitedBox(
Key key = null,
Widget child = null,
double maxWidth = double.MaxValue,
double maxHeight = double.MaxValue
) : base(key, child) {
this.maxHeight = maxHeight;
this.maxWidth = maxWidth;
}
public double maxWidth;
public double maxHeight;
public override RenderObject createRenderObject(BuildContext context) {
return new RenderLimitedBox(
maxWidth: maxWidth,
maxHeight: maxHeight
);
}
public override void updateRenderObject(BuildContext context, RenderObject renderObject) {
((RenderLimitedBox) renderObject).maxWidth = maxWidth;
((RenderLimitedBox) renderObject).maxHeight = maxHeight;
}
}
public class ConstrainedBox : SingleChildRenderObjectWidget {
public ConstrainedBox(
Key key = null,
BoxConstraints constraints = null,
Widget child = null
) : base(key, child) {
this.constraints = constraints;
}
public BoxConstraints constraints;
public override RenderObject createRenderObject(BuildContext context) {
return new RenderConstrainedBox(additionalConstraints: constraints);
}
public override void updateRenderObject(BuildContext context, RenderObject renderObject) {
((RenderConstrainedBox) renderObject)._additionalConstraints = constraints;
}
}
public class Padding : SingleChildRenderObjectWidget {
public Padding(
EdgeInsets padding,
Key key = null,
Widget child = null
) : base(key, child) {
this.padding = padding;
}
public EdgeInsets padding;
public override RenderObject createRenderObject(BuildContext context) {
return new RenderPadding(
padding: padding
);
}
public override void updateRenderObject(BuildContext context, RenderObject renderObject) {
((RenderPadding) renderObject).padding = padding;
}
}
public class Transform : SingleChildRenderObjectWidget {
public Transform(
Matrix4x4 transform,
Offset origin = null,
Alignment alignment = null,
bool transformHitTests = false,
Key key = null,
Widget child = null
) : base(key, child) {
this.alignment = alignment ?? Alignment.center;
this.origin = origin;
this.transformHitTests = transformHitTests;
this.transform = transform;
}
// scale
public Transform(
double scale,
Offset origin,
Alignment alignment,
bool transformHitTests = false,
Key key = null,
Widget child = null
) : base(key, child) {
this.alignment = alignment ?? Alignment.center;
this.origin = origin;
this.transformHitTests = transformHitTests;
this.transform = Matrix4x4.Scale(new Vector3((float) scale, (float) scale, (float) 1.0));
}
public Matrix4x4 transform;
public Offset origin;
public Alignment alignment;
public bool transformHitTests;
public override RenderObject createRenderObject(BuildContext context) {
return new RenderTransform(
transform: transform,
origin: origin,
alignment: alignment,
textDirection: Directionality.of(context),
transformHitTests: transformHitTests
);
}
public override void updateRenderObject(BuildContext context, RenderObject renderObject) {
((RenderTransform) renderObject).transform = transform;
((RenderTransform) renderObject).origin = origin;
((RenderTransform) renderObject).alignment = alignment;
((RenderTransform) renderObject).textDirection = Directionality.of(context);
((RenderTransform) renderObject).transformHitTests = transformHitTests;
}
}
public class Align : SingleChildRenderObjectWidget {
public Align(
double widthFactor = 0.0,
double heightFactor = 0.0,
Key key = null,
Widget child = null,
Alignment alignment = null
) : base(key, child) {
this.alignment = alignment ?? Alignment.center;
this.widthFactor = widthFactor;
this.heightFactor = heightFactor;
}
public Alignment alignment;
public double widthFactor;
public double heightFactor;
public override RenderObject createRenderObject(BuildContext context) {
return new RenderPositionedBox(
alignment: alignment,
widthFactor: widthFactor,
heightFactor: heightFactor
);
}
}
public class RawImage : LeafRenderObjectWidget {
public RawImage(Key key, ui.Image image, double width, double height, double scale, Color color,
BlendMode colorBlendMode, BoxFit fit, Rect centerSlice, Alignment alignment = null,

public ImageRepeat repeat;
public Rect centerSlice;
}
}

162
Assets/UIWidgets/widgets/binding.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Assertions;
abstract class WidgetsBinding : RendererBinding {
interface WidgetsBindingObserver {
void didChangeMetrics();
}
abstract class WidgetsBinding : RendererBinding {
window.onAccessibilityFeaturesChanged += handleAccessibilityFeaturesChanged;
public BuildOwner buildOwner {
get { return this._buildOwner; }
}

public Element renderViewElement {
get { return this._renderViewElement; }
}
public List<WidgetsBindingObserver> _observers = new List<WidgetsBindingObserver>();
void addObserver(WidgetsBindingObserver observer) {
_observers.Add(observer);
}
protected override void handleMetricsChanged() {
base.handleMetricsChanged();
foreach (WidgetsBindingObserver observer in _observers) {
observer.didChangeMetrics();
}
}
bool removeObserver(WidgetsBindingObserver observer) {
return _observers.Remove(observer);
}
void handleAccessibilityFeaturesChanged() {
// for (WidgetsBindingObserver observer in _observers) {
// observer.didChangeAccessibilityFeatures();
// }
}
}
void attachRootWidget(Widget rootWidget) {
_renderViewElement = new RenderObjectToWidgetAdapter<RenderBox>(
container: renderView,
child: rootWidget
).attachToRenderTree(buildOwner, _renderViewElement as RenderObjectToWidgetElement<RenderBox>);
}
}
public class RenderObjectToWidgetAdapter<T> : RenderObjectWidget where T : RenderObject {
public RenderObjectToWidgetAdapter(Widget child, RenderObjectWithChildMixinRenderObject<T> container) : base(
new GlobalObjectKey<State<StatefulWidget>>(container)) {
this.child = child;
this.container = container;
}
public Widget child;
public RenderObjectWithChildMixinRenderObject<T> container;
public string debugShortDescription;
public override Element createElement() {
return new RenderObjectToWidgetElement<T>(this);
}
public override RenderObject createRenderObject(BuildContext context) {
return container;
}
public void updateRenderObject(BuildContext context, RenderObject renderObject) {
}
public RenderObjectToWidgetElement<T> attachToRenderTree(BuildOwner owner, RenderObjectToWidgetElement<T> element) {
if (element == null) {
element = (RenderObjectToWidgetElement<T>) createElement();
element.assignOwner(owner);
owner.buildScope(element, () => { element.mount(null, null); });
}
else {
element._newWidget = this;
element.markNeedsBuild();
}
return element;
}
}
public class RenderObjectToWidgetElement<T> : RootRenderObjectElement where T : RenderObject {
public RenderObjectToWidgetElement(RenderObjectWidget widget) : base(widget) {
}
public RenderObjectToWidgetAdapter<T> widget {
get { return (RenderObjectToWidgetAdapter<T>) base.widget; }
}
Element _child;
static readonly object _rootChildSlot = new object();
public Widget _newWidget;
public new RenderObjectWithChildMixin renderObject {
get { return base.renderObject as RenderObjectWithChildMixin; }
}
public override void visitChildren(ElementVisitor visitor) {
if (_child != null)
visitor(_child);
}
protected override void forgetChild(Element child) {
D.assert(child == _child);
_child = null;
}
public override void mount(Element parent, object newSlot) {
D.assert(parent == null);
base.mount(parent, newSlot);
_rebuild();
}
public override void update(Widget newWidget) {
base.update(newWidget);
D.assert(widget == newWidget);
_rebuild();
}
protected override void performRebuild() {
if (_newWidget != null) {
Widget newWidget = _newWidget;
_newWidget = null;
update(newWidget);
}
base.performRebuild();
D.assert(_newWidget == null);
}
protected override void insertChildRenderObject(RenderObject child, object slot) {
D.assert(slot == _rootChildSlot);
renderObject.child = child;
}
protected override void moveChildRenderObject(RenderObject child, object slot) {
D.assert(false);
}
protected override void removeChildRenderObject(RenderObject child) {
D.assert(renderObject.child == child);
renderObject.child = null;
}
void _rebuild() {
try {
_child = updateChild(_child, widget.child, _rootChildSlot);
D.assert(_child != null);
}
catch (Exception e) {
Widget error = ErrorWidget.builder(new UIWidgetsErrorDetails(e));
_child = updateChild(null, error, _rootChildSlot);
}
}
}
}

14
Assets/UIWidgets/widgets/image.cs


using Rect = UnityEngine.Rect;
namespace UIWidgets.widgets {
internal class Util {
public static ImageConfiguration createLocalImageConfiguration(BuildContext context, Size size) {
internal class ImageUtil {
public static ImageConfiguration createLocalImageConfiguration(BuildContext context, Size size = null) {
return new ImageConfiguration(
size: size
);

public override void didChangeDependencies() {
_resolveImage();
// if (TickerMode.of(context))
// _listenToStream();
// else
// _stopListeningToStream();
if (TickerMode.of(context))
_listenToStream();
else
_stopListeningToStream();
}
public override void didUpdateWidget(StatefulWidget oldWidget) {

void _resolveImage() {
var imageWidget = (Image<T>) widget;
ImageStream newStream =
imageWidget.image.resolve(Util.createLocalImageConfiguration(
imageWidget.image.resolve(ImageUtil.createLocalImageConfiguration(
context,
size: new Size(imageWidget.width, imageWidget.height)
));

43
Assets/UIWidgets/rendering/error.cs


using System;
using System.Collections.Generic;
using UIWidgets.foundation;
using UIWidgets.gestures;
using UIWidgets.painting;
using UIWidgets.ui;
using UnityEngine;
using Rect = UIWidgets.ui.Rect;
namespace UIWidgets.rendering {
public class RenderErrorBox : RenderBox {
const string _kLine = "\n\n────────────────────\n\n";
public RenderErrorBox(string message = "") {
this.message = message;
if (message == "") return;
ui.ParagraphBuilder builder = new ui.ParagraphBuilder(paragraphStyle);
builder.pushStyle(textStyle);
builder.addText(
string.Format(
"{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}{17}{18}{19}{20}{21}{22}{23}",
message, _kLine, message, _kLine, message, _kLine, message, _kLine, message, _kLine, message,
_kLine, message, _kLine, message, _kLine, message, _kLine, message, _kLine, message, _kLine,
message, _kLine)
);
_paragraph = builder.build();
}
private string message;
ui.Paragraph _paragraph;
static ui.TextStyle textStyle = new ui.TextStyle(
color: new ui.Color(0xFFFFFF66),
fontFamily: "monospace",
fontSize: 14.0,
fontWeight: FontWeight.w700
);
static ui.ParagraphStyle paragraphStyle = new ui.ParagraphStyle(
lineHeight: 1.0
);
}
}

3
Assets/UIWidgets/rendering/error.cs.meta


fileFormatVersion: 2
guid: 64dfb38ea47d49c7b9f3d0debeee4409
timeCreated: 1536819405

136
Assets/UIWidgets/widgets/container.cs


using System;
using System.Collections.Generic;
using UIWidgets.foundation;
using UIWidgets.rendering;
using UIWidgets.painting;
using UnityEngine;
using Color = UIWidgets.ui.Color;
namespace UIWidgets.widgets {
public class DecoratedBox : SingleChildRenderObjectWidget {
public DecoratedBox(
Decoration decoration,
Widget child,
Key key = null,
DecorationPosition position = DecorationPosition.background
) : base(key, child) {
this.position = position;
this.decoration = decoration;
}
public Decoration decoration;
public DecorationPosition position;
public override RenderObject createRenderObject(BuildContext context) {
return new RenderDecoratedBox(
decoration: decoration,
position: position,
configuration: ImageUtil.createLocalImageConfiguration(context)
);
}
public override void updateRenderObject(BuildContext context, RenderObject renderObject) {
((RenderDecoratedBox) renderObject).decoration = decoration;
((RenderDecoratedBox) renderObject).configuration = ImageUtil.createLocalImageConfiguration(context);
((RenderDecoratedBox) renderObject).position = position;
}
}
public class Container : StatelessWidget {
public Container(
Key key,
Alignment alignment,
EdgeInsets padding,
Color color,
Decoration decoration,
Decoration forgroundDecoration,
double width,
double height,
BoxConstraints constraints,
EdgeInsets margin,
Matrix4x4 transfrom,
Widget child
) : base(key) {
this.alignment = alignment;
this.foregroundDecoration = forgroundDecoration;
this.transform = transfrom;
this.margin = margin;
this.child = child;
this.padding = padding;
this.decoration = decoration ?? (color != null ? new BoxDecoration(color) : null);
this.constraints = (width != 0.0 || height != 0.0)
? ((constraints == null ? null : constraints.tighten(width, height))
?? BoxConstraints.tightFor(width, height))
: constraints;
}
public Widget child;
public Alignment alignment;
public EdgeInsets padding;
public Decoration decoration;
public Decoration foregroundDecoration;
public BoxConstraints constraints;
public EdgeInsets margin;
public Matrix4x4 transform;
EdgeInsets _paddingIncludingDecoration {
get {
if (decoration == null || decoration.padding == null)
return padding;
EdgeInsets decorationPadding = decoration.padding;
if (padding == null)
return decorationPadding;
return padding.add(decorationPadding);
}
}
public override Widget build(BuildContext context) {
Widget current = child;
if (child == null && (constraints == null || !constraints.isTight)) {
current = new LimitedBox(
maxWidth: 0.0,
maxHeight: 0.0,
child: new ConstrainedBox(constraints: BoxConstraints.expand())
);
}
if (alignment != null) {
current = new Align(alignment: alignment, child: current);
}
EdgeInsets effetivePadding = _paddingIncludingDecoration;
if (effetivePadding != null) {
current = new Padding(padding: effetivePadding, child: current);
}
if (decoration != null) {
current = new DecoratedBox(decoration: decoration, child: current);
}
if (foregroundDecoration != null) {
current = new DecoratedBox(
decoration: decoration,
position: DecorationPosition.foreground,
child: current
);
}
if (constraints != null) {
current = new ConstrainedBox(constraints: constraints, child: current);
}
if (margin != null) {
current = new Padding(padding: margin, child: current);
}
if (transform != null) {
current = new Transform(transform: transform, child: current);
}
return current;
}
}
}

3
Assets/UIWidgets/widgets/container.cs.meta


fileFormatVersion: 2
guid: 7c96e4ace3a14122bc2107afe55771da
timeCreated: 1536831109

30
Assets/UIWidgets/widgets/ticker_provider.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UIWidgets.foundation;
using UIWidgets.rendering;
using UIWidgets.ui;
using UnityEngine.Assertions;
namespace UIWidgets.widgets {
public class TickerMode : InheritedWidget {
public TickerMode(Key key, bool enabled, Widget child) : base(key, child) {
this.enabled = enabled;
}
public readonly bool enabled;
public static bool of(BuildContext context) {
var widget = context.inheritFromWidgetOfExactType(typeof(TickerMode));
return widget is TickerMode ? (widget as TickerMode).enabled : true;
}
public override bool updateShouldNotify(InheritedWidget oldWidget) {
return this.enabled != ((TickerMode)oldWidget).enabled;
}
public override Element createElement() {
throw new NotImplementedException();
}
}
}

3
Assets/UIWidgets/widgets/ticker_provider.cs.meta


fileFormatVersion: 2
guid: aa6922a60d7945ffb3b92dede5941836
timeCreated: 1536807803
正在加载...
取消
保存