浏览代码

widgets init

/main
gewentao 6 年前
当前提交
5cbbdf04
共有 10 个文件被更改,包括 478 次插入3 次删除
  1. 6
      Assets/UIWidgets/painting/image_provider.cs
  2. 25
      Assets/UIWidgets/painting/image_stream.cs
  3. 14
      Assets/UIWidgets/ui/window.cs
  4. 140
      Assets/UIWidgets/widgets/framework.cs
  5. 57
      Assets/UIWidgets/widgets/basic.cs
  6. 3
      Assets/UIWidgets/widgets/basic.cs.meta
  7. 52
      Assets/UIWidgets/widgets/binding.cs
  8. 3
      Assets/UIWidgets/widgets/binding.cs.meta
  9. 178
      Assets/UIWidgets/widgets/image.cs
  10. 3
      Assets/UIWidgets/widgets/image.cs.meta

6
Assets/UIWidgets/painting/image_provider.cs


using UnityEngine;
namespace UIWidgets.painting {
public abstract class ImageProvider<T> {
public interface IImageProvider<out T> {
ImageStream resolve(ImageConfiguration configuration);
}
public abstract class ImageProvider<T> : IImageProvider<T> {
public ImageStream resolve(ImageConfiguration configuration) {
ImageStream stream = new ImageStream();
T obtainedKey;

25
Assets/UIWidgets/painting/image_stream.cs


private ImageStreamCompleter _completer;
private List<_ImageListenerPair> _listeners;
public System.Object key {
get {
return _completer != null ? (object) _completer : this;
}
}
public ImageStreamCompleter completer {
get { return _completer; }
}

listenerPair.errorListener
);
}
}
}
public void addListener(ImageListener listener, ImageErrorListerner onError = null) {
if (_completer != null) {
_completer.addListener(listener, onError);
} else {
if (_listeners == null) {
_listeners = new List<_ImageListenerPair>();
}
_listeners.Add(new _ImageListenerPair(listener, onError));
}
}
public void removeListener(ImageListener listener) {
if (_completer != null) {
_completer.removeListener(listener);
} else {
_listeners.RemoveAll(listenerPair => listenerPair.listener == listener);
}
}
}

14
Assets/UIWidgets/ui/window.cs


VoidCallback _onMetricsChanged;
public VoidCallback onLocaleChanged {
get { return this._onLocaleChanged; }
set { this._onLocaleChanged = value; }
}
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; }

140
Assets/UIWidgets/widgets/framework.cs


public abstract Widget build(BuildContext context);
}
public abstract class StatefulWidget : Widget {
protected StatefulWidget(string key) : base(key) {
}
public override Element createElement() {
return new StatefulElement(this);
}
public abstract State createState();
}
enum _StateLifecycle {
created,
initialized,
ready,
defunct,
}
public abstract class State : Diagnosticable {
public StatefulWidget widget {
get { return _widget; }
set { _widget = value; }
}
private StatefulWidget _widget;
_StateLifecycle _debugLifecycleState = _StateLifecycle.created;
public BuildContext context {
get { return _element; }
}
public StatefulElement element {
get { return _element; }
set { _element = value; }
}
private StatefulElement _element;
public bool mounted {
get { return _element != null; }
}
public virtual void initState() {
D.assert(_debugLifecycleState == _StateLifecycle.created);
}
public abstract void didChangeDependencies();
public abstract void didUpdateWidget(StatefulWidget oldWidget);
public abstract void reassemble();
public void setState(VoidCallback fn) {
fn();
_element.markNeedsBuild();
}
public abstract Widget build(BuildContext context);
protected internal override void debugFillProperties(DiagnosticPropertiesBuilder properties) {
base.debugFillProperties(properties);
}
}
public abstract class RenderObjectWidget : Widget {
protected RenderObjectWidget(string key) : base(key) {
}

}
}
public abstract class LeafRenderObjectWidget : RenderObjectWidget {
protected LeafRenderObjectWidget(string key) : base(key) {
}
public override Element createElement() {
return new LeafRenderObjectElement(this);
}
}
public abstract class SingleChildRenderObjectWidget : RenderObjectWidget {
protected SingleChildRenderObjectWidget(string key) : base(key) {
}
public Widget child;
public override Element createElement() {
return new SingleChildRenderObjectElement(this);
}
}
public abstract class Element : BuildContext {
protected Element(Widget widget) {
Assert.IsNotNull(widget);

public bool _active = false;
public virtual void _reassemble() {
markNeedsBuild();
ElementVisitor visit = null;
visit = child => { child._reassemble(); };
visitChildren(visit);
}
public RenderObject renderObject {
get {
RenderObject result = null;

Assert.IsNull(result);
if (element is RenderObjectElement) {
result = element.renderObject;
} else {
}
else {
element.visitChildren(visit);
}
};

this._firstBuild();
}
public void _firstBuild() {
public virtual void _firstBuild() {
this.rebuild();
}

}
}
public class StatefulElement : ComponentElement {
public StatefulElement(StatefulWidget widget) : base(widget) {
_state = widget.createState();
_state.element = this;
_state.widget = widget;
}
public State state {
get { return _state; }
}
private State _state;
public override Widget build() {
return state.build(this);
}
public override void _reassemble() {
state.reassemble();
base._reassemble();
}
public override void _firstBuild() {
base._firstBuild();
}
}
public abstract class RenderObjectElement : Element {
protected RenderObjectElement(RenderObjectWidget widget) : base(widget) {
}

public void finalizeTree() {
this._inactiveElements._unmountAll();
}
}
public class LeafRenderObjectElement : RenderObjectElement {
public LeafRenderObjectElement(LeafRenderObjectWidget widget) : base(widget) {
}
public override void forgetChild(Element child) {
}
}
public class SingleChildRenderObjectElement : RenderObjectElement {
public SingleChildRenderObjectElement(SingleChildRenderObjectWidget widget) : base(widget) {
}
public override void forgetChild(Element child) {
}
}

57
Assets/UIWidgets/widgets/basic.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UIWidgets.foundation;
using UIWidgets.painting;
using UIWidgets.rendering;
using UIWidgets.ui;
using UnityEngine.Assertions;
namespace UIWidgets.widgets {
public class RawImage : LeafRenderObjectWidget {
public RawImage(string key, ui.Image image, double width, double height, double scale, Color color,
BlendMode colorBlendMode, BoxFit fit, Rect centerSlice, Alignment alignment = null,
ImageRepeat repeat = ImageRepeat.noRepeat) : base(key) {
this.image = image;
this.width = width;
this.height = height;
this.scale = scale;
this.color = color;
this.blendMode = colorBlendMode;
this.centerSlice = centerSlice;
this.fit = fit;
this.alignment = alignment == null ? Alignment.center : alignment;
this.repeat = repeat;
}
public override Element createElement() {
throw new NotImplementedException();
}
public override RenderObject createRenderObject(BuildContext context) {
return new RenderImage(
this.image,
this.width,
this.height,
this.color,
this.blendMode,
this.fit,
this.repeat,
this.centerSlice,
this.alignment
);
}
public ui.Image image;
public double width;
public double height;
public double scale;
public Color color;
public BlendMode blendMode;
public BoxFit fit;
public Alignment alignment;
public ImageRepeat repeat;
public Rect centerSlice;
}
}

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


fileFormatVersion: 2
guid: 69241aaa538e4e2baa651ffa39d6f355
timeCreated: 1536564821

52
Assets/UIWidgets/widgets/binding.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 {
abstract class WidgetsBinding : RendererBinding {
protected WidgetsBinding(Window window) : base(window) {
this.buildOwner.onBuildScheduled = this._handleBuildScheduled;
window.onLocaleChanged += this.handleLocaleChanged;
window.onAccessibilityFeaturesChanged += handleAccessibilityFeaturesChanged;
}
public BuildOwner buildOwner {
get { return this._buildOwner; }
}
readonly BuildOwner _buildOwner;
public Element renderViewElement {
get { return this._renderViewElement; }
}
Element _renderViewElement;
void _handleBuildScheduled() {
ensureVisualUpdate();
}
void handleLocaleChanged() {
// todo
// dispatchLocaleChanged(window.locale);
}
void handleAccessibilityFeaturesChanged() {
// for (WidgetsBindingObserver observer in _observers) {
// observer.didChangeAccessibilityFeatures();
// }
}
public void drawFrame() {
if (renderViewElement != null) {
buildOwner.buildScope(renderViewElement);
}
base.drawFrame();
buildOwner.finalizeTree();
}
}
}

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


fileFormatVersion: 2
guid: 39d3379616cb4c64afdedd889e3ac2a2
timeCreated: 1536213633

178
Assets/UIWidgets/widgets/image.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UIWidgets.painting;
using UIWidgets.foundation;
using UIWidgets.rendering;
using UIWidgets.ui;
using UnityEngine;
using UnityEngine.Assertions;
using Color = UIWidgets.ui.Color;
using Rect = UnityEngine.Rect;
namespace UIWidgets.widgets {
internal class Util {
public static ImageConfiguration createLocalImageConfiguration(BuildContext context, Size size) {
return new ImageConfiguration(
size: size
);
}
}
public class Image<T> : StatefulWidget {
public IImageProvider<System.Object> image;
public double width;
public double height;
public Color color;
public BoxFit fit;
public Alignment alignment;
public BlendMode colorBlendMode;
public ImageRepeat repeat;
public ui.Rect centerSlice;
public bool gaplessPlayback;
public Image(
string key,
ImageProvider<System.Object> image,
double width,
double height,
Color color,
BlendMode colorBlendMode,
BoxFit fit,
ui.Rect centerSlice,
Alignment alignment,
ImageRepeat repeat = ImageRepeat.noRepeat,
bool gaplessPlayback = false
) : base(key) {
this.image = image;
this.width = width;
this.height = height;
this.color = color;
this.colorBlendMode = colorBlendMode;
this.fit = fit;
this.alignment = alignment == null ? Alignment.center : alignment;
this.repeat = repeat;
this.centerSlice = centerSlice;
this.gaplessPlayback = gaplessPlayback;
}
// Network Image
public Image(
string src,
string key,
double width,
double height,
Color color,
BlendMode colorBlendMode,
BoxFit fit,
Alignment alignment,
ui.Rect centerSlice,
Dictionary<String, String> headers,
ImageRepeat repeat = ImageRepeat.noRepeat,
bool gaplessPlayback = false,
double scale = 1.0
) : base(key) {
this.image = new NetworkImage(src, headers, scale);
this.width = width;
this.height = height;
this.color = color;
this.colorBlendMode = colorBlendMode;
this.fit = fit;
this.alignment = alignment;
this.centerSlice = centerSlice;
this.repeat = repeat;
this.gaplessPlayback = gaplessPlayback;
}
public override State createState() {
return new _ImageState<T>();
}
}
public class _ImageState<T> : State {
ImageStream _imageStream;
ImageInfo _imageInfo;
bool _isListeningToStream = false;
public override void didChangeDependencies() {
_resolveImage();
// if (TickerMode.of(context))
// _listenToStream();
// else
// _stopListeningToStream();
}
public override void didUpdateWidget(StatefulWidget oldWidget) {
if (((Image<T>) widget).image != ((Image<T>) oldWidget).image)
_resolveImage();
}
public override void reassemble() {
_resolveImage(); // in case the image cache was flushed
}
void _resolveImage() {
var imageWidget = (Image<T>) widget;
ImageStream newStream =
imageWidget.image.resolve(Util.createLocalImageConfiguration(
context,
size: new Size(imageWidget.width, imageWidget.height)
));
_updateSourceStream(newStream);
}
void _handleImageChanged(ImageInfo imageInfo, bool synchronousCall) {
setState(() => { _imageInfo = imageInfo; });
}
void _updateSourceStream(ImageStream newStream) {
if ((_imageStream == null ? null : _imageStream.key) == (newStream == null ? null : newStream.key))
return;
if (_isListeningToStream)
_imageStream.removeListener(_handleImageChanged);
if (!((Image<T>) widget).gaplessPlayback) {
setState(() => { _imageInfo = null; });
_imageStream = newStream;
if (_isListeningToStream)
_imageStream.addListener(_handleImageChanged);
}
}
void _listenToStream() {
if (_isListeningToStream)
return;
_imageStream.addListener(_handleImageChanged);
_isListeningToStream = true;
}
void _stopListeningToStream() {
if (!_isListeningToStream)
return;
_imageStream.removeListener(_handleImageChanged);
_isListeningToStream = false;
}
public override Widget build(BuildContext context) {
var imageWidget = (Image<T>) widget;
RawImage image = new RawImage(
"", // todo
_imageInfo == null ? null : _imageInfo.image,
imageWidget.width,
imageWidget.height,
_imageInfo == null ? 1.0 : _imageInfo.scale,
imageWidget.color,
imageWidget.colorBlendMode,
imageWidget.fit,
imageWidget.centerSlice,
imageWidget.alignment,
imageWidget.repeat
);
return image;
}
}
}

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


fileFormatVersion: 2
guid: d1d313d74f6044198caad151f52721b8
timeCreated: 1536219326
正在加载...
取消
保存