using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace Plugins.Vm.Views
{
///
/// VM 主页多画面显示控件(镜像 UCDispVpp 的角色):按 RowColNumber 铺设画面框网格。
/// 单例,供 VisionMasterEngine 与 VM 图像显示插件访问。
///
public partial class UCDispVm : UserControl
{
private static readonly Lazy _instance = new Lazy(() => new UCDispVm());
public static UCDispVm Instance => _instance.Value;
/// 当前画面框集合(下标 = 画面序号-1)。
public List vmWindow { get; } = new List();
public UCDispVm()
{
InitializeComponent();
}
private int[] _rowCol = new[] { 0, 1 };
/// {画面总数, 排列方式}:arrange 1=水平最优行列,2=垂直最优行列;总数0=清空。
public int[] RowColNumber
{
get => _rowCol;
set
{
_rowCol = value ?? new[] { 0, 1 };
RebuildFrames();
}
}
private void RebuildFrames()
{
if (!Dispatcher.CheckAccess()) { Dispatcher.Invoke(new Action(RebuildFrames)); return; }
RootGrid.Children.Clear();
RootGrid.RowDefinitions.Clear();
RootGrid.ColumnDefinitions.Clear();
vmWindow.Clear();
int count = _rowCol.Length > 0 ? _rowCol[0] : 0;
int arrange = _rowCol.Length > 1 ? _rowCol[1] : 1;
if (count <= 0) return;
int cols = (int)Math.Ceiling(Math.Sqrt(count));
if (cols < 1) cols = 1;
if (arrange == 2)
{
int rows = (int)Math.Ceiling(Math.Sqrt(count));
if (rows < 1) rows = 1;
cols = (int)Math.Ceiling((double)count / rows);
if (cols < 1) cols = 1;
}
int rowCount = (count + cols - 1) / cols;
for (int c = 0; c < cols; c++) RootGrid.ColumnDefinitions.Add(new ColumnDefinition());
for (int r = 0; r < rowCount; r++) RootGrid.RowDefinitions.Add(new RowDefinition());
for (int i = 0; i < count; i++)
{
var w = new VmWindow();
Grid.SetRow(w, i / cols);
Grid.SetColumn(w, i % cols);
RootGrid.Children.Add(w);
vmWindow.Add(w);
}
}
}
}