浏览代码

Merge remote-tracking branch 'upstream/master' into page_view

/main
fzhangtj 6 年前
当前提交
3669d263
共有 9 个文件被更改,包括 375 次插入49 次删除
  1. 3
      Runtime/gestures/multidrag.cs
  2. 3
      Runtime/widgets/app.cs
  3. 49
      Runtime/widgets/basic.cs
  4. 52
      Runtime/widgets/drag_target.cs
  5. 32
      Samples/UIWidgetSample/Navigation.unity
  6. 194
      Runtime/widgets/custom_paint.cs
  7. 11
      Runtime/widgets/custom_paint.cs.meta
  8. 69
      Samples/UIWidgetSample/CustomPaintCanvas.cs
  9. 11
      Samples/UIWidgetSample/CustomPaintCanvas.cs.meta

3
Runtime/gestures/multidrag.cs


this.initialPosition = initialPosition;
}
public readonly Offset initialPosition;
readonly VelocityTracker _velocityTracker = new VelocityTracker();

public override void addPointer(PointerDownEvent pEvent) {
D.assert(this._pointers != null);
D.assert(pEvent.pointer != null);
D.assert(pEvent.position != null);
D.assert(!this._pointers.ContainsKey(pEvent.pointer));

void _handleEvent(PointerEvent pEvent) {
D.assert(this._pointers != null);
D.assert(pEvent.pointer != null);
D.assert(pEvent.timeStamp != null);
D.assert(pEvent.position != null);
D.assert(this._pointers.ContainsKey(pEvent.pointer));

3
Runtime/widgets/app.cs


Widget home = null
) : base(key) {
D.assert(window != null);
routes = routes??new Dictionary<string, WidgetBuilder>();
this.window = window;
this.home = home;
this.navigatorKey = navigatorKey;

this.routes = routes ?? new Dictionary<string, WidgetBuilder>();
this.routes = routes;
this.navigatorObservers = navigatorObservers ?? new List<NavigatorObserver>();
this.initialRoute = initialRoute;
this.builder = builder;

49
Runtime/widgets/basic.cs


}
}
public class CustomPaint : SingleChildRenderObjectWidget {
public CustomPaint(
Key key = null,
CustomPainter painter = null,
CustomPainter foregroundPainter = null,
Size size = null,
bool isComplex = false,
bool willChange = false,
Widget child = null
) : base(key: key, child: child) {
size = size ?? Size.zero;
this.size = size;
this.painter = painter;
this.foregroundPainter = foregroundPainter;
this.isComplex = isComplex;
this.willChange = willChange;
}
public readonly CustomPainter painter;
public readonly CustomPainter foregroundPainter;
public readonly Size size;
public readonly bool isComplex;
public readonly bool willChange;
public override RenderObject createRenderObject(BuildContext context) {
return new RenderCustomPaint(
painter: this.painter,
foregroundPainter: this.foregroundPainter,
preferredSize: this.size,
isComplex: this.isComplex,
willChange: this.willChange
);
}
public override void updateRenderObject(BuildContext context, RenderObject renderObject) {
((RenderCustomPaint) renderObject).painter = this.painter;
((RenderCustomPaint) renderObject).foregroundPainter = this.foregroundPainter;
((RenderCustomPaint) renderObject).preferredSize = this.size;
((RenderCustomPaint) renderObject).isComplex = this.isComplex;
((RenderCustomPaint)renderObject).willChange = this.willChange;
}
public override void didUnmountRenderObject(RenderObject renderObject) {
((RenderCustomPaint)renderObject).painter = null;
((RenderCustomPaint)renderObject).foregroundPainter = null;
}
}
public class LimitedBox : SingleChildRenderObjectWidget {
public LimitedBox(
Key key = null,

52
Runtime/widgets/drag_target.cs


public class Draggable<T> : StatefulWidget {
public Draggable(
T data,
Widget child,
Widget feedback,
Widget child = null,
Widget feedback = null,
Axis? axis = null,
Widget childWhenDragging = null,
Offset feedbackOffset = null,

public class LongPressDraggable<T> : Draggable<T> {
readonly bool hapticFeedbackOnStart;
Widget child,
Widget feedback,
Key key = null,
Widget child = null,
Widget feedback = null,
Key key = null,
Widget childWhenDragging = null,
Offset feedbackOffset = null,
DragAnchor dragAnchor = DragAnchor.child,

DragEndCallback onDragEnd = null,
VoidCallback onDragCompleted = null,
bool hapticFeedbackOnStart = true
VoidCallback onDragCompleted = null
) : base(
key: key,
child: child,

onDragEnd: onDragEnd,
onDragCompleted: onDragCompleted
) {
this.hapticFeedbackOnStart = hapticFeedbackOnStart;
}
public override GestureRecognizer createRecognizer(GestureMultiDragStartCallback onStart) {

if (result != null && this.hapticFeedbackOnStart) {
//HapticFeedback.selectionClick();
}
return result;
}
};

public class DraggableDetails {
public DraggableDetails(
Velocity velocity,
Offset offset,
bool wasAccepted = false
bool wasAccepted = false,
Velocity velocity = null,
Offset offset = null
) {
D.assert(velocity != null);
D.assert(offset != null);

}
public bool wasAccepted;
public readonly bool wasAccepted;
public Velocity velocity;
public readonly Velocity velocity;
public Offset offset;
public readonly Offset offset;
DragTargetBuilder<T> builder,
DragTargetBuilder<T> builder = null,
D.assert(builder != null);
this.builder = builder;
this.onWillAccept = onWillAccept;
this.onAccept = onAccept;

public DragTargetBuilder<T> builder;
public readonly DragTargetBuilder<T> builder;
public DragTargetWillAccept<T> onWillAccept;
public readonly DragTargetWillAccept<T> onWillAccept;
public DragTargetAccept<T> onAccept;
public readonly DragTargetAccept<T> onAccept;
public DragTargetLeave<T> onLeave;
public readonly DragTargetLeave<T> onLeave;
public override State createState() {
return new _DragTargetState<T>();

readonly List<_DragTargetState<T>> _enteredTargets = new List<_DragTargetState<T>>();
OverlayEntry _entry;
Offset _lastOffset;
Offset _lastOffset;
OverlayEntry _entry;
public void update(DragUpdateDetails details) {
this._position += this._restrictAxis(details.delta);

32
Samples/UIWidgetSample/Navigation.unity


m_LocalScale: {x: 0, y: 0, z: 0}
m_Children:
- {fileID: 927824195}
- {fileID: 1818957069}
- {fileID: 1158582124}
m_Father: {fileID: 0}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
m_IsActive: 1
--- !u!224 &927824195
RectTransform:
m_ObjectHideFlags: 0

m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 927824194}
m_CullTransparentMesh: 0
--- !u!1 &1818957068
--- !u!1 &1158582123
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}

m_Component:
- component: {fileID: 1818957069}
- component: {fileID: 1818957071}
- component: {fileID: 1818957070}
- component: {fileID: 1158582124}
- component: {fileID: 1158582126}
- component: {fileID: 1158582125}
m_Name: PageView
m_Name: CustomPaint
m_IsActive: 1
--- !u!224 &1818957069
m_IsActive: 0
--- !u!224 &1158582124
m_GameObject: {fileID: 1818957068}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_GameObject: {fileID: 1158582123}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []

m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1818957070
--- !u!114 &1158582125
m_GameObject: {fileID: 1818957068}
m_GameObject: {fileID: 1158582123}
m_Script: {fileID: 11500000, guid: a8dea5be0500345ccb20b797d19e741c, type: 3}
m_Script: {fileID: 11500000, guid: 5debfd6de8ea942d487383a56aad8491, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}

y: 0
width: 1
height: 1
--- !u!222 &1818957071
--- !u!222 &1158582126
m_GameObject: {fileID: 1818957068}
m_GameObject: {fileID: 1158582123}
m_CullTransparentMesh: 0
--- !u!1 &1838283857
GameObject:

194
Runtime/widgets/custom_paint.cs


using Unity.UIWidgets.foundation;
using Unity.UIWidgets.gestures;
using Unity.UIWidgets.rendering;
using Unity.UIWidgets.ui;
namespace Unity.UIWidgets.widgets {
public abstract class CustomPainter : Listenable {
public CustomPainter(Listenable repaint) {
this._repaint = repaint;
}
readonly Listenable _repaint;
public void addListener(VoidCallback listener) {
this._repaint?.addListener(listener);
}
public void removeListener(VoidCallback listener) {
this._repaint?.removeListener(listener);
}
public abstract void paint(Canvas canvas, Size size);
public abstract bool shouldRepaint(CustomPainter oldDelegate);
public virtual bool hitTest(Offset position) {
return false;
}
public override string ToString() {
return $"{Diagnostics.describeIdentity(this)}({this._repaint?.ToString() ?? ""})";
}
}
public class RenderCustomPaint : RenderProxyBox {
public RenderCustomPaint(
CustomPainter painter = null,
CustomPainter foregroundPainter = null,
Size preferredSize = null,
bool isComplex = false,
bool willChange = false,
RenderBox child = null
): base(child) {
preferredSize = preferredSize ?? Size.zero;
this.preferredSize = preferredSize;
this._painter = painter;
this._foregroundPainter = foregroundPainter;
this.isComplex = isComplex;
this.willChange = willChange;
}
CustomPainter _painter;
public CustomPainter painter {
get => this._painter;
set {
if (this._painter == value)
return;
CustomPainter oldPainter = this._painter;
this._painter = value;
this._didUpdatePainter(this._painter, oldPainter);
}
}
CustomPainter _foregroundPainter;
public CustomPainter foregroundPainter {
get => this._foregroundPainter;
set {
if (this._foregroundPainter == value)
return;
CustomPainter oldPainter = this._foregroundPainter;
this._foregroundPainter = value;
this._didUpdatePainter(this._foregroundPainter, oldPainter);
}
}
void _didUpdatePainter(CustomPainter newPainter, CustomPainter oldPainter) {
if (newPainter == null) {
D.assert(oldPainter != null);
this.markNeedsPaint();
}
else if (oldPainter == null ||
newPainter.GetType() != oldPainter.GetType() ||
newPainter.shouldRepaint(oldPainter)) {
this.markNeedsPaint();
}
if (this.attached) {
oldPainter?.removeListener(this.markNeedsPaint);
newPainter?.addListener(this.markNeedsPaint);
}
}
Size _preferredSize;
public Size preferredSize {
get => this._preferredSize;
set {
D.assert(value != null);
if (this.preferredSize == value)
return;
this._preferredSize = value;
this.markNeedsLayout();
}
}
public bool isComplex;
public bool willChange;
public override void attach(object owner) {
base.attach(owner);
this._painter?.addListener(this.markNeedsPaint);
this._foregroundPainter?.addListener(this.markNeedsPaint);
}
public override void detach() {
this._painter?.removeListener(this.markNeedsPaint);
this._foregroundPainter?.removeListener(this.markNeedsPaint);
base.detach();
}
protected override bool hitTestChildren(HitTestResult result, Offset position) {
if (this._foregroundPainter != null && (this._foregroundPainter.hitTest(position)))
return true;
return base.hitTestChildren(result, position: position);
}
protected override bool hitTestSelf(Offset position) {
return this._painter != null && this._painter.hitTest(position);
}
protected override void performResize() {
this.size = this.constraints.constrain(this.preferredSize);
}
void _paintWithPainter(Canvas canvas, Offset offset, CustomPainter painter) {
int debugPreviousCanvasSaveCount = 0;
canvas.save();
D.assert(() => {
debugPreviousCanvasSaveCount = canvas.getSaveCount();
return true;
});
if (offset != Offset.zero)
canvas.translate(offset.dx, offset.dy);
painter.paint(canvas, this.size);
D.assert(() => {
int debugNewCanvasSaveCount = canvas.getSaveCount();
if (debugNewCanvasSaveCount > debugPreviousCanvasSaveCount)
throw new UIWidgetsError(
$"{debugNewCanvasSaveCount - debugPreviousCanvasSaveCount} more " +
$"time{((debugNewCanvasSaveCount - debugPreviousCanvasSaveCount == 1) ? "" : "s")} " +
"than it called canvas.restore().\n" +
"This leaves the canvas in an inconsistent state and will probably result in a broken display.\n" +
"You must pair each call to save()/saveLayer() with a later matching call to restore()."
);
if (debugNewCanvasSaveCount < debugPreviousCanvasSaveCount)
throw new UIWidgetsError(
$"The {painter} custom painter called canvas.restore() " +
$"{debugPreviousCanvasSaveCount - debugNewCanvasSaveCount} more " +
$"time{(debugPreviousCanvasSaveCount - debugNewCanvasSaveCount == 1 ? "" : "s")} " +
"than it called canvas.save() or canvas.saveLayer().\n" +
"This leaves the canvas in an inconsistent state and will result in a broken display.\n" +
"You should only call restore() if you first called save() or saveLayer()."
);
return debugNewCanvasSaveCount == debugPreviousCanvasSaveCount;
});
canvas.restore();
}
public override void paint(PaintingContext context, Offset offset) {
if (this._painter != null) {
this._paintWithPainter(context.canvas, offset, this._painter);
this._setRasterCacheHints(context);
}
base.paint(context, offset);
if (this._foregroundPainter != null) {
this._paintWithPainter(context.canvas, offset, this._foregroundPainter);
this._setRasterCacheHints(context);
}
}
void _setRasterCacheHints(PaintingContext context) {
if (this.isComplex)
context.setIsComplexHint();
if (this.willChange)
context.setWillChangeHint();
}
}
}

11
Runtime/widgets/custom_paint.cs.meta


fileFormatVersion: 2
guid: a4b3c34c9775f4b9995fb37535490751
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

69
Samples/UIWidgetSample/CustomPaintCanvas.cs


using Unity.UIWidgets.engine;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.ui;
using Unity.UIWidgets.widgets;
namespace UIWidgetsSample {
public class CustomPaintCanvas: WidgetCanvas {
protected override Widget getWidget() {
return new CustomPaint(
child: new Container(width: 300, height: 300, color: new Color(0XFFFFFFFF)),
foregroundPainter: new GridPainter(null)
);
}
}
public class GridPainter : CustomPainter {
public GridPainter(Listenable repaint) : base(repaint) {
}
public override void paint(Canvas canvas, Size size) {
int numGrid = 4;
var paint = new Paint();
paint.color = new Color(0xFFFF0000);
paint.strokeWidth = 2;
paint.style = PaintingStyle.stroke;
for (int i = 1; i < numGrid; i++) {
double offsetY = size.height * i / numGrid;
canvas.drawLine(new Offset(0, offsetY), new Offset(size.width, offsetY),
paint);
}
for (int i = 1; i < numGrid; i++) {
double offsetx = size.width * i / numGrid;
canvas.drawLine(new Offset(offsetx, 0), new Offset(offsetx, size.height),
paint);
}
// draw a arrow line
canvas.save();
canvas.rotate(0.4);
canvas.scale(2, 2);
canvas.translate(50, 50);
canvas.drawLine(new Offset(0, 0), new Offset(100, 0),
new Paint(){
color = new Color(0xFFFF0000),
strokeWidth = 2,
style = PaintingStyle.stroke
});
var path = new Path();
var arrowPaint = new Paint() {
color = new Color(0xFFFF0000),
style = PaintingStyle.fill
};
path.moveTo(100, 0);
path.lineTo(100, 5);
path.lineTo(120, 0);
path.lineTo(100, -5);
path.close();
canvas.drawPath(path, arrowPaint);
canvas.restore();
}
public override bool shouldRepaint(CustomPainter oldDelegate) {
return false;
}
}
}

11
Samples/UIWidgetSample/CustomPaintCanvas.cs.meta


fileFormatVersion: 2
guid: 5debfd6de8ea942d487383a56aad8491
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存