浏览代码

test image widget with margin container

/main
gewentao 6 年前
当前提交
25b32e04
共有 14 个文件被更改,包括 176 次插入82 次删除
  1. 5
      Assets/UIWidgets/Tests/Menu.cs
  2. 7
      Assets/UIWidgets/painting/decoration_image.cs
  3. 14
      Assets/UIWidgets/painting/image_provider.cs
  4. 16
      Assets/UIWidgets/rendering/image.cs
  5. 1
      Assets/UIWidgets/ui/painting/canvas.cs
  6. 6
      Assets/UIWidgets/ui/painting/canvas_impl.cs
  7. 1
      Assets/UIWidgets/ui/painting/draw_cmd.cs
  8. 6
      Assets/UIWidgets/ui/painting/picture.cs
  9. 23
      Assets/UIWidgets/widgets/basic.cs
  10. 37
      Assets/UIWidgets/widgets/binding.cs
  11. 35
      Assets/UIWidgets/widgets/container.cs
  12. 43
      Assets/UIWidgets/widgets/image.cs
  13. 61
      Assets/UIWidgets/Tests/Widgets.cs
  14. 3
      Assets/UIWidgets/Tests/Widgets.cs.meta

5
Assets/UIWidgets/Tests/Menu.cs


public static void renderEditable() {
EditorWindow.GetWindow(typeof(RenderEditable));
}
[MenuItem("UIWidgetsTests/Widgets")]
public static void renderWidgets() {
EditorWindow.GetWindow(typeof(Widgets));
}
}
}

7
Assets/UIWidgets/painting/decoration_image.cs


}
public static class DecorationImageUtil {
public static void paintImage(Canvas canvas, Rect rect, ui.Image image) {
canvas.drawImageRect(Rect.fromLTWH(0, 0, 100, 100), rect, new Paint(), image);
}
// todo refine logic below, no just draw in Rect(0, 0, 100, 100) for testing widgets
/*
public static void paintImage(Canvas canvas, Rect rect, ui.Image image, BoxFit fit, Rect centerSlice,
Alignment alignment = null,
ImageRepeat repeat = ImageRepeat.noRepeat) {

if (needSave)
canvas.restore();
}
}*/
public static List<Rect> _generateImageTileRects(Rect outputRect, Rect fundamentalRect,
ImageRepeat repeat) {

14
Assets/UIWidgets/painting/image_provider.cs


using RSG;
using System.Net;
using System;
using System.IO;
using UnityEngine;
public interface IImageProvider<out T> {
ImageStream resolve(ImageConfiguration configuration);
public abstract class ImageProvider {
public abstract ImageStream resolve(ImageConfiguration configuration);
public abstract class ImageProvider<T> : IImageProvider<T> {
public ImageStream resolve(ImageConfiguration configuration) {
public abstract class ImageProvider<T> : ImageProvider {
public override ImageStream resolve(ImageConfiguration configuration) {
ImageStream stream = new ImageStream();
T obtainedKey;
obtainedKey = obtainKey(configuration);

}
public override ImageStreamCompleter load(NetworkImage key) {
// return new OneFrameImageStreamCompleter(_loadAsync(key));
return new OneFrameImageStreamCompleter(_loadAsyncWithCache(key));
return new OneFrameImageStreamCompleter(_loadAsync(key));
// return new OneFrameImageStreamCompleter(_loadAsyncWithCache(key));
}
public static IPromise<ImageInfo> _loadAsync(NetworkImage key) {

16
Assets/UIWidgets/rendering/image.cs


using UIWidgets.ui;
using UIWidgets.painting;
using UnityEngine.Rendering;
using BlendMode = UIWidgets.ui.BlendMode;
namespace UIWidgets.rendering {
class RenderImage : RenderBox {

_image.height / _scale
));
}
public override void performLayout() {
this.size = _sizeForConstraints(constraints);
}
public override void paint(PaintingContext context, Offset offset) {
if (_image == null)

context.canvas,
offset & size,
_image,
_fit,
_centerSlice,
_resolvedAlignment,
_repeat
_image
// _fit,
// _centerSlice,
// _resolvedAlignment,
// _repeat
);
}
}

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


public void drawImageRect(Rect src, Rect dst, Paint paint, Image image) {
this._recorder.addDrawCmd(new DrawImageRect
{
paint = paint,
image = image,
src = src,
dst = dst,

6
Assets/UIWidgets/ui/painting/canvas_impl.cs


} else if (drawCmd is DrawTextBlob) {
var drawTextBlob = (DrawTextBlob) drawCmd;
this.drawTextBlob(drawTextBlob.textBlob, drawTextBlob.x, drawTextBlob.y);
} else {
} else if (drawCmd is DrawImageRect) {
var drawImageProperties = (DrawImageRect) drawCmd;
this.drawImageRect(drawImageProperties.src, drawImageProperties.dst, drawImageProperties.paint, drawImageProperties.image);
}
else {
throw new Exception("unknown drawCmd: " + drawCmd);
}
}

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


}
public class DrawImageRect : DrawCmd {
public Paint paint;
public Image image;
public Rect src;
public Rect dst;

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


var drawTextBlob = (DrawTextBlob) drawCmd;
var bounds = drawTextBlob.textBlob.boundsInText.shift(new Offset(drawTextBlob.x, drawTextBlob.y));
this.addPaintBounds(bounds);
} else
{
} else if (drawCmd is DrawImageRect) {
var drawImageRect = (DrawImageRect) drawCmd;
this.addPaintBounds(drawImageRect.src);
} else {
throw new Exception("unknown drawCmd: " + drawCmd);
}
}

23
Assets/UIWidgets/widgets/basic.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Assertions;
using Color = UIWidgets.ui.Color;
using Rect = UIWidgets.ui.Rect;

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) {

this.repeat = repeat;
}
public override Element createElement() {
throw new NotImplementedException();
}
public override RenderObject createRenderObject(BuildContext context) {
return new RenderImage(
this.image,

this.alignment
);
}
public override void updateRenderObject(BuildContext context, RenderObject renderObject) {
((RenderImage) renderObject).image = this.image;
((RenderImage) renderObject).width = this.width;
((RenderImage) renderObject).height = this.height;
((RenderImage) renderObject).color = this.color;
((RenderImage) renderObject).fit = this.fit;
((RenderImage) renderObject).repeat = this.repeat;
((RenderImage) renderObject).centerSlice = this.centerSlice;
((RenderImage) renderObject).alignment = this.alignment;
}
public ui.Image image;
public double width;

37
Assets/UIWidgets/widgets/binding.cs


using UIWidgets.ui;
namespace UIWidgets.widgets {
interface WidgetsBindingObserver {
public interface WidgetsBindingObserver {
abstract class WidgetsBinding : RendererBinding {
protected WidgetsBinding(Window window) : base(window) {
public class WidgetsBinding : RendererBinding {
public WidgetsBinding(Window window) : base(window) {
this.buildOwner.onBuildScheduled = this._handleBuildScheduled;
window.onLocaleChanged += this.handleLocaleChanged;
}

}
readonly BuildOwner _buildOwner;
readonly BuildOwner _buildOwner = new BuildOwner();
public Element renderViewElement {
get { return this._renderViewElement; }

public List<WidgetsBindingObserver> _observers = new List<WidgetsBindingObserver>();
void addObserver(WidgetsBindingObserver observer) {

base.drawFrame();
buildOwner.finalizeTree();
}
void attachRootWidget(Widget rootWidget) {
_renderViewElement = new RenderObjectToWidgetAdapter<RenderBox>(
public void attachRootWidget(Widget rootWidget) {
var _adapter = new RenderObjectToWidgetAdapter<RenderBox>(
).attachToRenderTree(buildOwner, _renderViewElement as RenderObjectToWidgetElement<RenderBox>);
);
_renderViewElement = _adapter.attachToRenderTree(buildOwner,
_renderViewElement as RenderObjectToWidgetElement<RenderBox>);
}
}

public void updateRenderObject(BuildContext context, RenderObject renderObject) {
}
public RenderObjectToWidgetElement<T> attachToRenderTree(BuildOwner owner, RenderObjectToWidgetElement<T> element) {
public RenderObjectToWidgetElement<T> attachToRenderTree(BuildOwner owner,
RenderObjectToWidgetElement<T> element) {
if (element == null) {
element = (RenderObjectToWidgetElement<T>) createElement();
element.assignOwner(owner);

Widget error = ErrorWidget.builder(new UIWidgetsErrorDetails(e));
_child = updateChild(null, error, _rootChildSlot);
}
}
}
public class WidgetsBindings {
public WidgetsBindings(Window window) {
this.window = window;
this.widgetsBinding = new WidgetsBinding(window);
}
public readonly Window window;
public readonly WidgetsBinding widgetsBinding;
public void attachRootWidget(Widget root) {
this.widgetsBinding.attachRootWidget(root);
}
}
}

35
Assets/UIWidgets/widgets/container.cs


}
public class Container : StatelessWidget {
// todo transform
Key key,
Alignment alignment,
EdgeInsets padding,
Color color,
Decoration decoration,
Decoration forgroundDecoration,
double width,
double height,
BoxConstraints constraints,
EdgeInsets margin,
Matrix4x4 transfrom,
Widget child
Key key = null,
Alignment alignment = null,
EdgeInsets padding = null,
Color color = null,
Decoration decoration = null,
Decoration forgroundDecoration = null,
double width = 0.0,
double height = 0.0,
BoxConstraints constraints = null,
EdgeInsets margin = null,
// Matrix4x4 transfrom = default(Matrix4x4),
Widget child = null
this.transform = transfrom;
// this.transform = transfrom;
this.margin = margin;
this.child = child;
this.padding = padding;

public Decoration foregroundDecoration;
public BoxConstraints constraints;
public EdgeInsets margin;
public Matrix4x4 transform;
// public Matrix4x4 transform;
EdgeInsets _paddingIncludingDecoration {
get {

current = new Padding(padding: margin, child: current);
}
if (transform != null) {
current = new Transform(transform: transform, child: current);
}
// if (transform != null) {
// current = new Transform(transform: transform, child: current);
// }
return current;
}

43
Assets/UIWidgets/widgets/image.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UIWidgets.rendering;
using UnityEngine;
using UnityEngine.Assertions;
using Rect = UnityEngine.Rect;
namespace UIWidgets.widgets {
internal class ImageUtil {

}
}
public class Image<T> : StatefulWidget {
public IImageProvider<System.Object> image;
public class Image : StatefulWidget {
public ImageProvider image;
public double width;
public double height;
public Color color;

public Image(
Key key,
ImageProvider<System.Object> image,
ImageProvider<object> image,
double width,
double height,
Color color,

// Network Image
public Image(
string src,
Key key,
double width,
double height,
Color color,
BlendMode colorBlendMode,
BoxFit fit,
Alignment alignment,
ui.Rect centerSlice,
Dictionary<String, String> headers,
Key key = null,
double width = 0.0,
double height = 0.0,
Color color = null,
BlendMode colorBlendMode = BlendMode.None,
BoxFit fit = BoxFit.none,
Alignment alignment = null,
ui.Rect centerSlice = null,
Dictionary<String, String> headers = null,
ImageRepeat repeat = ImageRepeat.noRepeat,
bool gaplessPlayback = false,
double scale = 1.0

}
public override State createState() {
return new _ImageState<T>();
return new _ImageState();
public class _ImageState<T> : State {
public class _ImageState : State {
ImageStream _imageStream;
ImageInfo _imageInfo;
bool _isListeningToStream = false;

}
public override void didUpdateWidget(StatefulWidget oldWidget) {
if (((Image<T>) widget).image != ((Image<T>) oldWidget).image)
if (((Image) widget).image != ((Image) oldWidget).image)
_resolveImage();
}

void _resolveImage() {
var imageWidget = (Image<T>) widget;
var imageWidget = (Image) widget;
ImageStream newStream =
imageWidget.image.resolve(ImageUtil.createLocalImageConfiguration(
context,

if (_isListeningToStream && _imageStream != null)
_imageStream.removeListener(_handleImageChanged);
if (!((Image<T>) widget).gaplessPlayback) {
if (!((Image) widget).gaplessPlayback) {
setState(() => { _imageInfo = null; });
_imageStream = newStream;

}
public override Widget build(BuildContext context) {
var imageWidget = (Image<T>) widget;
var imageWidget = (Image) widget;
null, // todo
null,
_imageInfo == null ? null : _imageInfo.image,
imageWidget.width,
imageWidget.height,

61
Assets/UIWidgets/Tests/Widgets.cs


using UIWidgets.painting;
using UIWidgets.editor;
using UIWidgets.widgets;
using UnityEditor;
using UnityEngine;
namespace UIWidgets.Tests {
public class Widgets : EditorWindow {
private WindowAdapter windowAdapter;
private WidgetsBindings widgetsBindings;
private PaintingBinding paintingBinding;
private Widget root;
private Widget image;
Widgets() {
this.titleContent = new GUIContent("Widgets Test");
this.image = new widgets.Image(
"https://tse3.mm.bing.net/th?id=OIP.XOAIpvR1kh-CzISe_Nj9GgHaHs&pid=Api",
width: 100,
height: 100
);
this.root = new widgets.Container(
width: 200,
height: 200,
margin: EdgeInsets.all(30.0),
child: image
);
}
void OnGUI() {
if (this.windowAdapter != null) {
this.windowAdapter.OnGUI();
}
}
private void Update() {
if (this.windowAdapter != null) {
this.windowAdapter.Update();
}
}
private void OnEnable() {
this.paintingBinding = new PaintingBinding(null);
paintingBinding.initInstances();
this.windowAdapter = new WindowAdapter(this);
this.widgetsBindings = new WidgetsBindings(windowAdapter);
if (widgetsBindings != null) {
widgetsBindings.attachRootWidget(root);
}
}
void OnDestroy() {
this.windowAdapter = null;
this.widgetsBindings = null;
}
}
}

3
Assets/UIWidgets/Tests/Widgets.cs.meta


fileFormatVersion: 2
guid: 1d2e1850f336481aa984d96b28c513b2
timeCreated: 1536916429
正在加载...
取消
保存