Multi-content support for CoverFlow

This commit is contained in:
NaBian committed 2019-05-11 21:21:32 +08:00
1 parent ddb5a58cd0
commit 3c2b045807
4 files changed
+39 -23

No files matched your search

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
namespace HandyControlDemo.UserControl
{
@@ -9,7 +8,7 @@ namespace HandyControlDemo.UserControl
{
InitializeComponent();
CoverFlowMain.AddRange(new List<Uri>
CoverFlowMain.AddRange(new []
{
new Uri(@"pack://application:,,,/Resources/Img/Album/1.jpg"),
new Uri(@"pack://application:,,,/Resources/Img/Album/2.jpg"),
@@ -45,9 +45,9 @@ namespace HandyControl.Controls
{
return 0;
}
if (v >= ctl._uriList.Count)
if (v >= ctl._contentDic.Count)
{
return ctl._uriList.Count - 1;
return ctl._contentDic.Count - 1;
}
return v;
}
@@ -65,9 +65,9 @@ namespace HandyControl.Controls
"Loop", typeof(bool), typeof(CoverFlow), new PropertyMetadata(ValueBoxes.FalseBox));
/// <summary>
/// 存储所有的资源标识符
/// 存储所有的内容
/// </summary>
private readonly Dictionary<int, Uri> _uriList = new Dictionary<int, Uri>();
private readonly Dictionary<int, object> _contentDic = new Dictionary<int, object>();
/// <summary>
/// 当前在显示范围内的项
@@ -154,12 +154,12 @@ namespace HandyControl.Controls
/// <summary>
/// 批量添加资源
/// </summary>
/// <param name="uriList"></param>
public void AddRange(List<Uri> uriList)
/// <param name="contentList"></param>
public void AddRange(IEnumerable<object> contentList)
{
foreach (var uri in uriList)
foreach (var content in contentList)
{
_uriList.Add(_uriList.Count, uri);
_contentDic.Add(_contentDic.Count, content);
}
}
@@ -167,13 +167,13 @@ namespace HandyControl.Controls
/// 添加一项资源
/// </summary>
/// <param name="uriString"></param>
public void Add(string uriString) => _uriList.Add(_uriList.Count, new Uri(uriString));
public void Add(string uriString) => _contentDic.Add(_contentDic.Count, new Uri(uriString));
/// <summary>
/// 添加一项资源
/// </summary>
/// <param name="uri"></param>
public void Add(Uri uri) => _uriList.Add(_uriList.Count, uri);
public void Add(Uri uri) => _contentDic.Add(_contentDic.Count, uri);
/// <summary>
/// 跳转
@@ -187,12 +187,12 @@ namespace HandyControl.Controls
if (e.Delta < 0)
{
var index = PageIndex + 1;
PageIndex = index >= _uriList.Count ? (Loop ? 0 : _uriList.Count - 1) : index;
PageIndex = index >= _contentDic.Count ? Loop ? 0 : _contentDic.Count - 1 : index;
}
else
{
var index = PageIndex - 1;
PageIndex = index < 0 ? (Loop ? _uriList.Count - 1 : 0) : index;
PageIndex = index < 0 ? Loop ? _contentDic.Count - 1 : 0 : index;
}
e.Handled = true;
@@ -273,7 +273,7 @@ namespace HandyControl.Controls
private void UpdateShowRange()
{
var newFirstShowIndex = Math.Max(PageIndex - MaxShowCountHalf, 0);
var newLastShowIndex = Math.Min(PageIndex + MaxShowCountHalf, _uriList.Count - 1);
var newLastShowIndex = Math.Min(PageIndex + MaxShowCountHalf, _contentDic.Count - 1);
if (_firstShowIndex < newFirstShowIndex)
{
@@ -294,7 +294,7 @@ namespace HandyControl.Controls
{
if (!_itemShowList.ContainsKey(i))
{
var cover = new CoverFlowItem(i, PageIndex, BitmapFrame.Create(_uriList[i]));
var cover = CreateCoverFlowItem(i, _contentDic[i]);
_itemShowList[i] = cover;
_visualParent.Children.Add(cover);
}
@@ -303,5 +303,21 @@ namespace HandyControl.Controls
_firstShowIndex = newFirstShowIndex;
_lastShowIndex = newLastShowIndex;
}
private CoverFlowItem CreateCoverFlowItem(int index, object content)
{
if (content is Uri uri)
{
return new CoverFlowItem(index, PageIndex, new Image
{
Source = BitmapFrame.Create(uri)
});
}
return new CoverFlowItem(index, PageIndex, new ContentControl
{
Content = content
});
}
}
}
@@ -18,14 +18,15 @@ namespace HandyControl.Controls
private readonly Model3DGroup _model3DGroup;
private readonly ImageSource _source;
private readonly UIElement _uiElement;
internal int Index { get; set; }
public CoverFlowItem(int itemIndex, int currentIndex, ImageSource source)
public CoverFlowItem(int itemIndex, int currentIndex, UIElement element)
{
Index = itemIndex;
_source = source;
_uiElement = element;
_uiElement.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
_rotation3D = new AxisAngleRotation3D(new Vector3D(0, 1, 0), GetAngleByPos(currentIndex));
_transform3D = new TranslateTransform3D(GetXByPos(currentIndex), 0, GetZByPos(currentIndex));
@@ -35,7 +36,7 @@ namespace HandyControl.Controls
transformGroup.Children.Add(_transform3D);
_model3DGroup = new Model3DGroup();
_model3DGroup.Children.Add(new GeometryModel3D(CreateItemGeometry(), new DiffuseMaterial(new ImageBrush(_source))));
_model3DGroup.Children.Add(new GeometryModel3D(CreateItemGeometry(), new DiffuseMaterial(new VisualBrush(element))));
_model3DGroup.Transform = transformGroup;
Content = _model3DGroup;
@@ -103,8 +104,8 @@ namespace HandyControl.Controls
/// <returns></returns>
private Geometry3D CreateItemGeometry()
{
var sx = _source.Width > _source.Height ? 0 : 1 - _source.Width / _source.Height;
var sy = _source.Width < _source.Height ? 0 : 1 - _source.Height / _source.Width;
var sx = _uiElement.DesiredSize.Width > _uiElement.DesiredSize.Height ? 0 : 1 - _uiElement.DesiredSize.Width / _uiElement.DesiredSize.Height;
var sy = _uiElement.DesiredSize.Width < _uiElement.DesiredSize.Height ? 0 : 1 - _uiElement.DesiredSize.Height / _uiElement.DesiredSize.Width;
var p0 = new Point3D(-1 + sx, -1 + sy, 0);
var p1 = new Point3D(1 - sx, -1 + sy, 0);
@@ -9,7 +9,7 @@
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
<Viewport3D x:Name="PART_Viewport3D">
<Viewport3D.Camera>
<PerspectiveCamera x:Name="PART_Camera" Position="0,0,3" UpDirection="0,1,0" LookDirection="0,0,-1" FieldOfView="100" NearPlaneDistance="0.125"/>
<PerspectiveCamera x:Name="PART_Camera" Position="0,0,3" UpDirection="0,1,0" LookDirection="0,0,-1" FieldOfView="90"/>
</Viewport3D.Camera>
<Viewport3D.Children>
<ModelVisual3D>