浏览代码

material phase 1: step 1

/main
xingwei.zhu 6 年前
当前提交
eb8fd1b7
共有 5 个文件被更改,包括 588 次插入0 次删除
  1. 29
      Runtime/material/utils.cs
  2. 394
      Runtime/material/arc.cs
  3. 51
      Runtime/material/card.cs
  4. 5
      Runtime/material/expansion_tile.cs
  5. 109
      Runtime/material/shadows.cs

29
Runtime/material/utils.cs


using Unity.UIWidgets.rendering;
using Unity.UIWidgets.ui;
namespace Unity.UIWidgets.material {
public static class ThemeDataUtils {
public static readonly Color _kLightThemeHighlightColor = new Color(0x66BCBCBC);

double d3 = (position - bounds.bottomLeft(Offset.zero)).distance;
double d4 = (position - bounds.bottomRight(Offset.zero)).distance;
return Math.Max(Math.Max(d1, d2), Math.Max(d3, d4)).ceil();
}
}
public static class ArcUtils {
public const double _kOnAxisDelta = 2.0;
public static List<_Diagonal> _allDiagonals = new List<_Diagonal> {
new _Diagonal(_CornerId.topLeft, _CornerId.bottomRight),
new _Diagonal(_CornerId.bottomRight, _CornerId.topLeft),
new _Diagonal(_CornerId.topRight, _CornerId.bottomLeft),
new _Diagonal(_CornerId.bottomLeft, _CornerId.topRight)
};
public delegate double _KeyFunc<T>(T input);
public static T _maxBy<T>(List<T> input, _KeyFunc<T> keyFunc) {
T maxValue = default(T);
double? maxKey = null;
foreach (T value in input) {
double key = keyFunc(value);
if (maxKey == null || key > maxKey) {
maxValue = value;
maxKey = key;
}
}
return maxValue;
}
}
}

394
Runtime/material/arc.cs


using System;
using Unity.UIWidgets.animation;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.ui;
namespace Unity.UIWidgets.material {
public class MaterialPointArcTween : Tween<Offset> {
public MaterialPointArcTween(
Offset begin = null,
Offset end = null) : base(begin: begin, end: end) {
}
bool _dirty = true;
void _initialze() {
D.assert(this.begin != null);
D.assert(this.end != null);
Offset delta = this.end - this.begin;
double deltaX = delta.dx.abs();
double deltaY = delta.dy.abs();
double distanceFromAtoB = delta.distance;
Offset c = new Offset(this.end.dx, this.begin.dy);
double sweepAngle() {
return 2.0 * Math.Asin(distanceFromAtoB / (2.0 * this._radius));
}
if (deltaX > ArcUtils._kOnAxisDelta && deltaY > ArcUtils._kOnAxisDelta) {
if (deltaX < deltaY) {
this._radius = distanceFromAtoB * distanceFromAtoB / (c - this.begin).distance / 2.0;
this._center = new Offset(this.end.dx + this._radius * (this.begin.dx - this.end.dx).sign(),
this.end.dy);
if (this.begin.dx < this.end.dx) {
this._beginAngle = sweepAngle();
this._endAngle = 0.0;
}
}
else {
this._radius = distanceFromAtoB * distanceFromAtoB / (c - this.end).distance / 2.0;
this._center = new Offset(this.begin.dx,
this.begin.dy + (this.end.dy - this.begin.dy).sign() * this._radius);
if (this.begin.dy < this.end.dy) {
this._beginAngle = -Math.PI / 2.0;
this._endAngle = this._beginAngle + sweepAngle() * (this.end.dx - this.begin.dx).sign();
}
else {
this._beginAngle = Math.PI / 2.0;
this._endAngle = this._beginAngle + sweepAngle() * (this.begin.dx - this.end.dx).sign();
}
}
D.assert(this._beginAngle != null);
D.assert(this._endAngle != null);
}
else {
this._beginAngle = null;
this._endAngle = null;
}
this._dirty = false;
}
public Offset center {
get {
if (this.begin == null || this.end == null) {
return null;
}
if (this._dirty) {
this._initialze();
;
}
return this._center;
}
}
Offset _center;
public double? radius {
get {
if (this.begin == null || this.end == null) {
return null;
}
if (this._dirty) {
this._initialze();
}
return this._radius;
}
}
double _radius;
public double? beginAngle {
get {
if (this.begin == null || this.end == null) {
return null;
}
if (this._dirty) {
this._initialze();
}
return this._beginAngle;
}
}
double? _beginAngle;
public double? endAngle {
get {
if (this.begin == null || this.end == null) {
return null;
}
if (this._dirty) {
this._initialze();
}
return this._endAngle;
}
}
double? _endAngle;
new public Offset begin {
get { return base.begin; }
set {
if (value != base.begin) {
base.begin = value;
this._dirty = true;
}
}
}
new public Offset end {
get { return base.end; }
set {
if (value != base.end) {
base.end = value;
this._dirty = true;
}
}
}
public override Offset lerp(double t) {
if (this._dirty) {
this._initialze();
}
if (t == 0.0) {
return this.begin;
}
if (t == 1.0) {
return this.end;
}
if (this._beginAngle == null || this._endAngle == null) {
return Offset.lerp(this.begin, this.end, t);
}
double angle = MathUtils.lerpNullableDouble(this._beginAngle, this._endAngle, t) ?? 0.0;
double x = Math.Cos(angle) * this._radius;
double y = Math.Sin(angle) * this._radius;
return this._center + new Offset(x, y);
}
public string toString() {
return this.GetType() + "(" + this.begin + "->" + this.end + "); center=" + this.center +
", radius=" + this.radius + ", beginAngle=" + this.beginAngle + ", endAngle=" + this.endAngle;
}
}
public enum _CornerId {
topLeft,
topRight,
bottomLeft,
bottomRight
}
public class _Diagonal {
public _Diagonal(
_CornerId beginId,
_CornerId endId) {
this.beginId = beginId;
this.endId = endId;
}
public readonly _CornerId beginId;
public readonly _CornerId endId;
}
public class MaterialRectArcTween : RectTween {
public MaterialRectArcTween(
Rect begin = null,
Rect end = null) : base(begin: begin, end: end) {
}
bool _dirty = true;
void _initialize() {
D.assert(this.begin != null);
D.assert(this.end != null);
Offset centersVector = this.end.center - this.begin.center;
_Diagonal diagonal = ArcUtils._maxBy(ArcUtils._allDiagonals,
(_Diagonal d) => this._diagonalSupport(centersVector, d));
this._beginArc = new MaterialPointArcTween(
begin: this._cornerFor(this.begin, diagonal.beginId),
end: this._cornerFor(this.end, diagonal.beginId));
this._endArc = new MaterialPointArcTween(
begin: this._cornerFor(this.begin, diagonal.endId),
end: this._cornerFor(this.end, diagonal.endId));
this._dirty = false;
}
double _diagonalSupport(Offset centersVector, _Diagonal diagonal) {
Offset delta = this._cornerFor(this.begin, diagonal.endId) - this._cornerFor(this.begin, diagonal.beginId);
double length = delta.distance;
return centersVector.dx * delta.dx / length + centersVector.dy * delta.dy / length;
}
Offset _cornerFor(Rect rect, _CornerId id) {
switch (id) {
case _CornerId.topLeft: return rect.topLeft;
case _CornerId.topRight: return rect.topRight;
case _CornerId.bottomLeft: return rect.bottomLeft;
case _CornerId.bottomRight: return rect.bottomRight;
}
return Offset.zero;
}
MaterialPointArcTween beginArc {
get {
if (this.begin == null) {
return null;
}
if (this._dirty) {
this._initialize();
;
}
return this._beginArc;
}
}
MaterialPointArcTween _beginArc;
MaterialPointArcTween endArc {
get {
if (this.end == null) {
return null;
}
if (this._dirty) {
this._initialize();
}
return this._endArc;
}
}
MaterialPointArcTween _endArc;
new public Rect begin {
get { return base.begin; }
set {
if (value != base.begin) {
base.begin = value;
this._dirty = true;
}
}
}
new public Rect end {
get { return base.end; }
set {
if (value != base.end) {
base.end = value;
this._dirty = true;
}
}
}
public override Rect lerp(double t) {
if (this._dirty) {
this._initialize();
}
if (t == 0.0) {
return this.begin;
}
if (t == 1.0) {
return this.end;
}
return Rect.fromPoints(this._beginArc.lerp(t), this._endArc.lerp(t));
}
public string toString() {
return this.GetType() + "(" + this.begin + "->" + this.end + ")";
}
}
public class MaterialRectCenterArcTween : RectTween {
public MaterialRectCenterArcTween(
Rect begin = null,
Rect end = null) : base(begin: begin, end: end) {
}
bool _dirty = true;
void _initialize() {
D.assert(this.begin != null);
D.assert(this.end != null);
this._centerArc = new MaterialPointArcTween(
begin: this.begin.center,
end: this.end.center);
this._dirty = false;
}
public MaterialPointArcTween centerArc {
get {
if (this.begin == null || this.end == null) {
return null;
}
if (this._dirty) {
this._initialize();
}
return this._centerArc;
}
}
MaterialPointArcTween _centerArc;
new public Rect begin {
get { return base.begin; }
set {
if (value != base.begin) {
base.begin = value;
this._dirty = true;
}
}
}
new public Rect end {
get { return base.end; }
set {
if (value != base.end) {
base.end = value;
this._dirty = true;
}
}
}
public override Rect lerp(double t) {
if (this._dirty) {
this._initialize();
;
}
if (t == 0.0) {
return this.begin;
}
if (t == 1.0) {
return this.end;
}
Offset center = this._centerArc.lerp(t);
double width = MathUtils.lerpDouble(this.begin.width, this.end.width, t);
double height = MathUtils.lerpDouble(this.begin.height, this.end.height, t);
return Rect.fromLTWH(
center.dx - width / 2.0,
center.dy - height / 2.0,
width,
height);
}
public string toString() {
return this.GetType() + "(" + this.begin + "->" + this.end + "); centerArc=" + this.centerArc;
}
}
}

51
Runtime/material/card.cs


using Unity.UIWidgets.foundation;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.ui;
using Unity.UIWidgets.widgets;
namespace Unity.UIWidgets.material {
class Card : StatelessWidget {
public Card(
Key key = null,
Color color = null,
double? elevation = null,
ShapeBorder shape = null,
EdgeInsets margin = null,
Clip clipBehavior = Clip.none,
Widget child = null) : base(key: key) {
this.color = color;
this.elevation = elevation;
this.shape = shape;
this.margin = margin ?? EdgeInsets.all(4.0);
this.clipBehavior = clipBehavior;
this.child = child;
}
public readonly Color color;
public readonly double? elevation;
public readonly ShapeBorder shape;
public readonly Clip clipBehavior;
public readonly EdgeInsets margin;
public readonly Widget child;
public override Widget build(BuildContext context) {
return new Container(
margin: this.margin ?? EdgeInsets.all(4.0),
child: new Material(
type: MaterialType.card,
color: this.color ?? Theme.of(context).cardColor,
elevation: this.elevation ?? 1.0,
shape: this.shape ?? new RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(4.0))
),
clipBehavior: this.clipBehavior,
child: this.child)
);
}
}
}

5
Runtime/material/expansion_tile.cs


namespace Unity.UIWidgets.material {
public class expansion_tile {
}
}

109
Runtime/material/shadows.cs


using System.Collections.Generic;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.ui;
namespace Unity.UIWidgets.material {
public class ShadowConstants {
public static Dictionary<int, List<BoxShadow>>
kElevationToShadow = _elevationToShadow; // to hide the literal from the docs
public static Color _kKeyUmbraOpacity = new Color(0x33000000); // alpha = 0.2
public static Color _kKeyPenumbraOpacity = new Color(0x24000000); // alpha = 0.14
public static Color _kAmbientShadowOpacity = new Color(0x1F000000); // alpha = 0.12
static Dictionary<int, List<BoxShadow>> _elevationToShadow = new Dictionary<int, List<BoxShadow>> {
{
1, new List<BoxShadow> {
new BoxShadow(offset: new Offset(0.0, 2.0), blurRadius: 1.0, spreadRadius: -1.0,
color: _kKeyUmbraOpacity),
new BoxShadow(offset: new Offset(0.0, 1.0), blurRadius: 1.0, spreadRadius: 0.0,
color: _kKeyPenumbraOpacity),
new BoxShadow(offset: new Offset(0.0, 1.0), blurRadius: 3.0, spreadRadius: 0.0,
color: _kAmbientShadowOpacity)
}
}, {
2, new List<BoxShadow> {
new BoxShadow(offset: new Offset(0.0, 3.0), blurRadius: 1.0, spreadRadius: -2.0,
color: _kKeyUmbraOpacity),
new BoxShadow(offset: new Offset(0.0, 2.0), blurRadius: 2.0, spreadRadius: 0.0,
color: _kKeyPenumbraOpacity),
new BoxShadow(offset: new Offset(0.0, 1.0), blurRadius: 5.0, spreadRadius: 0.0,
color: _kAmbientShadowOpacity)
}
}, {
3, new List<BoxShadow> {
new BoxShadow(offset: new Offset(0.0, 3.0), blurRadius: 3.0, spreadRadius: -2.0,
color: _kKeyUmbraOpacity),
new BoxShadow(offset: new Offset(0.0, 3.0), blurRadius: 4.0, spreadRadius: 0.0,
color: _kKeyPenumbraOpacity),
new BoxShadow(offset: new Offset(0.0, 1.0), blurRadius: 8.0, spreadRadius: 0.0,
color: _kAmbientShadowOpacity)
}
}, {
4, new List<BoxShadow> {
new BoxShadow(offset: new Offset(0.0, 2.0), blurRadius: 4.0, spreadRadius: -1.0,
color: _kKeyUmbraOpacity),
new BoxShadow(offset: new Offset(0.0, 4.0), blurRadius: 5.0, spreadRadius: 0.0,
color: _kKeyPenumbraOpacity),
new BoxShadow(offset: new Offset(0.0, 1.0), blurRadius: 10.0, spreadRadius: 0.0,
color: _kAmbientShadowOpacity)
}
}, {
6, new List<BoxShadow> {
new BoxShadow(offset: new Offset(0.0, 3.0), blurRadius: 5.0, spreadRadius: -1.0,
color: _kKeyUmbraOpacity),
new BoxShadow(offset: new Offset(0.0, 6.0), blurRadius: 10.0, spreadRadius: 0.0,
color: _kKeyPenumbraOpacity),
new BoxShadow(offset: new Offset(0.0, 1.0), blurRadius: 18.0, spreadRadius: 0.0,
color: _kAmbientShadowOpacity)
}
}, {
8, new List<BoxShadow> {
new BoxShadow(offset: new Offset(0.0, 5.0), blurRadius: 5.0, spreadRadius: -3.0,
color: _kKeyUmbraOpacity),
new BoxShadow(offset: new Offset(0.0, 8.0), blurRadius: 10.0, spreadRadius: 1.0,
color: _kKeyPenumbraOpacity),
new BoxShadow(offset: new Offset(0.0, 3.0), blurRadius: 14.0, spreadRadius: 2.0,
color: _kAmbientShadowOpacity)
}
}, {
9, new List<BoxShadow> {
new BoxShadow(offset: new Offset(0.0, 5.0), blurRadius: 6.0, spreadRadius: -3.0,
color: _kKeyUmbraOpacity),
new BoxShadow(offset: new Offset(0.0, 9.0), blurRadius: 12.0, spreadRadius: 1.0,
color: _kKeyPenumbraOpacity),
new BoxShadow(offset: new Offset(0.0, 3.0), blurRadius: 16.0, spreadRadius: 2.0,
color: _kAmbientShadowOpacity)
}
}, {
12, new List<BoxShadow> {
new BoxShadow(offset: new Offset(0.0, 7.0), blurRadius: 8.0, spreadRadius: -4.0,
color: _kKeyUmbraOpacity),
new BoxShadow(offset: new Offset(0.0, 12.0), blurRadius: 17.0, spreadRadius: 2.0,
color: _kKeyPenumbraOpacity),
new BoxShadow(offset: new Offset(0.0, 5.0), blurRadius: 22.0, spreadRadius: 4.0,
color: _kAmbientShadowOpacity)
}
}, {
16, new List<BoxShadow> {
new BoxShadow(offset: new Offset(0.0, 8.0), blurRadius: 10.0, spreadRadius: -5.0,
color: _kKeyUmbraOpacity),
new BoxShadow(offset: new Offset(0.0, 16.0), blurRadius: 24.0, spreadRadius: 2.0,
color: _kKeyPenumbraOpacity),
new BoxShadow(offset: new Offset(0.0, 6.0), blurRadius: 30.0, spreadRadius: 5.0,
color: _kAmbientShadowOpacity)
}
}, {
24, new List<BoxShadow> {
new BoxShadow(offset: new Offset(0.0, 11.0), blurRadius: 15.0, spreadRadius: -7.0,
color: _kKeyUmbraOpacity),
new BoxShadow(offset: new Offset(0.0, 24.0), blurRadius: 38.0, spreadRadius: 3.0,
color: _kKeyPenumbraOpacity),
new BoxShadow(offset: new Offset(0.0, 9.0), blurRadius: 46.0, spreadRadius: 8.0,
color: _kAmbientShadowOpacity)
}
}
};
}
}
正在加载...
取消
保存