Yuncong Zhang
6 年前
当前提交
9800ccea
共有 12 个文件被更改,包括 355 次插入 和 2 次删除
-
2Runtime/widgets/framework.cs
-
8Tests/Editor/Widgets.cs
-
8Tests/Editor/demo_charts.meta
-
149Tests/Editor/demo_charts/bar.cs
-
11Tests/Editor/demo_charts/bar.cs.meta
-
38Tests/Editor/demo_charts/color_palette.cs
-
11Tests/Editor/demo_charts/color_palette.cs.meta
-
67Tests/Editor/demo_charts/main.cs
-
11Tests/Editor/demo_charts/main.cs.meta
-
41Tests/Editor/demo_charts/tween.cs
-
11Tests/Editor/demo_charts/tween.cs.meta
|
|||
fileFormatVersion: 2 |
|||
guid: 16325b3157a5144c789848b8ecc53311 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using Unity.UIWidgets.animation; |
|||
using Unity.UIWidgets.foundation; |
|||
using Unity.UIWidgets.ui; |
|||
using Unity.UIWidgets.widgets; |
|||
using UnityEngine; |
|||
using Canvas = Unity.UIWidgets.ui.Canvas; |
|||
using Color = Unity.UIWidgets.ui.Color; |
|||
using Rect = Unity.UIWidgets.ui.Rect; |
|||
|
|||
namespace UIWidgets.Tests.demo_charts { |
|||
public class BarChart { |
|||
public BarChart(List<Bar> bars) { |
|||
this.bars = bars; |
|||
} |
|||
|
|||
public static BarChart empty() { |
|||
return new BarChart(new List<Bar>()); |
|||
} |
|||
|
|||
public static BarChart random(Size size) { |
|||
var barWidthFraction = 0.75f; |
|||
var ranks = selectRanks(ColorPalette.primary.length); |
|||
var barCount = ranks.Count; |
|||
var barDistance = size.width / (1 + barCount); |
|||
var barWidth = barDistance * barWidthFraction; |
|||
var startX = barDistance - barWidth / 2; |
|||
var bars = Enumerable.Range(0, barCount).Select(i => new Bar( |
|||
ranks[i], |
|||
startX + i * barDistance, |
|||
barWidth, |
|||
Random.value * size.height, |
|||
ColorPalette.primary[ranks[i]] |
|||
)).ToList(); |
|||
return new BarChart(bars); |
|||
} |
|||
|
|||
static List<int> selectRanks(int cap) { |
|||
var ranks = new List<int>(); |
|||
var rank = 0; |
|||
while (true) { |
|||
if (Random.value < 0.2f) { |
|||
rank++; |
|||
} |
|||
if (cap <= rank) { |
|||
break; |
|||
} |
|||
ranks.Add(rank); |
|||
rank++; |
|||
} |
|||
return ranks; |
|||
} |
|||
|
|||
public readonly List<Bar> bars; |
|||
} |
|||
|
|||
public class BarChartTween : Tween<BarChart> { |
|||
readonly MergeTween<Bar> _barsTween; |
|||
|
|||
public BarChartTween(BarChart begin, BarChart end) : base(begin: begin, end: end) { |
|||
this._barsTween = new MergeTween<Bar>(begin.bars, end.bars); |
|||
} |
|||
|
|||
public override BarChart lerp(float t) { |
|||
return new BarChart(this._barsTween.lerp(t)); |
|||
} |
|||
} |
|||
|
|||
public class Bar : MergeTweenable<Bar> { |
|||
public Bar(int rank, float x, float width, float height, Color color) { |
|||
this.rank = rank; |
|||
this.x = x; |
|||
this.width = width; |
|||
this.height = height; |
|||
this.color = color; |
|||
} |
|||
|
|||
public readonly int rank; |
|||
public readonly float x; |
|||
public readonly float width; |
|||
public readonly float height; |
|||
public readonly Color color; |
|||
|
|||
public Bar empty { |
|||
get { return new Bar(this.rank, this.x, 0.0f, 0.0f, this.color); } |
|||
} |
|||
|
|||
public bool less(Bar other) { |
|||
return this.rank < other.rank; |
|||
} |
|||
|
|||
public Tween<Bar> tweenTo(Bar other) { |
|||
return new BarTween(this, other); |
|||
} |
|||
|
|||
public static Bar lerp(Bar begin, Bar end, float t) { |
|||
D.assert(begin.rank == end.rank); |
|||
return new Bar( |
|||
begin.rank, |
|||
MathUtils.lerpFloat(begin.x, end.x, t), |
|||
MathUtils.lerpFloat(begin.width, end.width, t), |
|||
MathUtils.lerpFloat(begin.height, end.height, t), |
|||
Color.lerp(begin.color, end.color, t) |
|||
); |
|||
} |
|||
} |
|||
|
|||
public class BarTween : Tween<Bar> { |
|||
public BarTween(Bar begin, Bar end) : base(begin: begin, end: end) { |
|||
D.assert(begin.rank == end.rank); |
|||
} |
|||
|
|||
public override Bar lerp(float t) { |
|||
return Bar.lerp(this.begin, this.end, t); |
|||
} |
|||
} |
|||
|
|||
public class BarChartPainter : AbstractCustomPainter { |
|||
public BarChartPainter(Animation<BarChart> animation) |
|||
: base(repaint: animation) { |
|||
this.animation = animation; |
|||
} |
|||
|
|||
public readonly Animation<BarChart> animation; |
|||
|
|||
public override void paint(Canvas canvas, Size size) { |
|||
var paint = new Paint(); |
|||
paint.style = PaintingStyle.fill; |
|||
var chart = this.animation.value; |
|||
foreach (var bar in chart.bars) { |
|||
paint.color = bar.color; |
|||
canvas.drawRect( |
|||
Rect.fromLTWH( |
|||
bar.x, |
|||
size.height - bar.height, |
|||
bar.width, |
|||
bar.height |
|||
), |
|||
paint |
|||
); |
|||
} |
|||
} |
|||
|
|||
public override bool shouldRepaint(CustomPainter old) { |
|||
return false; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 20c43f616dd7640279364f93d965f4c0 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System.Collections.Generic; |
|||
using Unity.UIWidgets.foundation; |
|||
using Unity.UIWidgets.material; |
|||
using UnityEngine; |
|||
using Color = Unity.UIWidgets.ui.Color; |
|||
|
|||
namespace UIWidgets.Tests.demo_charts { |
|||
public class ColorPalette { |
|||
public static readonly ColorPalette primary = new ColorPalette(new List<Color> { |
|||
Colors.blue[400], |
|||
Colors.red[400], |
|||
Colors.green[400], |
|||
Colors.yellow[400], |
|||
Colors.purple[400], |
|||
Colors.orange[400], |
|||
Colors.teal[400] |
|||
}); |
|||
|
|||
public ColorPalette(List<Color> colors) { |
|||
D.assert(colors.isNotEmpty); |
|||
this._colors = colors; |
|||
} |
|||
|
|||
readonly List<Color> _colors; |
|||
|
|||
public Color this[int index] { |
|||
get { return this._colors[index % this.length]; } |
|||
} |
|||
|
|||
public int length { |
|||
get { return this._colors.Count; } |
|||
} |
|||
|
|||
public Color random() { |
|||
return this[Random.Range(0, this.length - 1)]; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: f9e9e9f9c5ecd474b8ef6a194f2d9fc7 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using Unity.UIWidgets.animation; |
|||
using Unity.UIWidgets.material; |
|||
using Unity.UIWidgets.ui; |
|||
using Unity.UIWidgets.widgets; |
|||
|
|||
namespace UIWidgets.Tests.demo_charts { |
|||
/*** |
|||
* from https://github.com/mravn/charts
|
|||
*/ |
|||
public class ChartPage : StatefulWidget { |
|||
public override State createState() { |
|||
return new ChartPageState(); |
|||
} |
|||
} |
|||
|
|||
public class ChartPageState : TickerProviderStateMixin<ChartPage> { |
|||
public static readonly Size size = new Size(200.0f, 100.0f); |
|||
|
|||
AnimationController _animation; |
|||
BarChartTween _tween; |
|||
|
|||
public override |
|||
void initState() { |
|||
base.initState(); |
|||
this._animation = new AnimationController( |
|||
duration: new TimeSpan(0, 0, 0, 0, 300), |
|||
vsync: this |
|||
); |
|||
this._tween = new BarChartTween( |
|||
BarChart.empty(), |
|||
BarChart.random(size) |
|||
); |
|||
this._animation.forward(); |
|||
} |
|||
|
|||
public override void dispose() { |
|||
this._animation.dispose(); |
|||
base.dispose(); |
|||
} |
|||
|
|||
void changeData() { |
|||
this.setState(() => { |
|||
this._tween = new BarChartTween( |
|||
this._tween.evaluate(this._animation), |
|||
BarChart.random(size) |
|||
); |
|||
this._animation.forward(from: 0.0f); |
|||
}); |
|||
} |
|||
|
|||
public override Widget build(BuildContext context) { |
|||
return new Scaffold( |
|||
body: new Center( |
|||
child: new CustomPaint( |
|||
size: size, |
|||
painter: new BarChartPainter(this._tween.animate(this._animation)) |
|||
) |
|||
), |
|||
floatingActionButton: new FloatingActionButton( |
|||
child: new Icon(Unity.UIWidgets.material.Icons.refresh), |
|||
onPressed: this.changeData |
|||
) |
|||
); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 905d6da1ea7b947be937556edda2b664 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using Unity.UIWidgets.animation; |
|||
|
|||
namespace UIWidgets.Tests.demo_charts { |
|||
public interface MergeTweenable<T> { |
|||
T empty { get; } |
|||
|
|||
Tween<T> tweenTo(T other); |
|||
|
|||
bool less(T other); |
|||
} |
|||
|
|||
public class MergeTween<T> : Tween<List<T>> where T : MergeTweenable<T> { |
|||
public MergeTween(List<T> begin, List<T> end) : base(begin: begin, end: end) { |
|||
int bMax = begin.Count; |
|||
int eMax = end.Count; |
|||
var b = 0; |
|||
var e = 0; |
|||
while (b + e < bMax + eMax) { |
|||
if (b < bMax && (e == eMax || begin[b].less(end[e]))) { |
|||
this._tweens.Add(begin[b].tweenTo(begin[b].empty)); |
|||
b++; |
|||
} else if (e < eMax && (b == bMax || end[e].less(begin[b]))) { |
|||
this._tweens.Add(end[e].empty.tweenTo(end[e])); |
|||
e++; |
|||
} else { |
|||
this._tweens.Add(begin[b].tweenTo(end[e])); |
|||
b++; |
|||
e++; |
|||
} |
|||
} |
|||
} |
|||
|
|||
readonly List<Tween<T>> _tweens = new List<Tween<T>>(); |
|||
|
|||
public override List<T> lerp(float t) { |
|||
return Enumerable.Range(0, this._tweens.Count).Select(i => this._tweens[i].lerp(t)).ToList(); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: ae95c92eb6d8346f3bbc89a91d9e191d |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue