156 lines
4.9 KiB
C#
156 lines
4.9 KiB
C#
using Plane.Copters;
|
||
using GalaSoft.MvvmLight;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Collections.ObjectModel;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using Plane.FormationCreator.Util;
|
||
|
||
namespace Plane.FormationCreator.Formation
|
||
{
|
||
|
||
public class CopterCollection : ObservableCollection<ICopter>
|
||
{
|
||
/// <summary>
|
||
/// 实现排序插入
|
||
/// </summary>
|
||
/// <param name="baseSemObjects"></param>
|
||
public int AddCopter(ICopter entityObject)
|
||
{
|
||
|
||
////给第三方时候限制数量和时间用
|
||
DateTime dateTime2019 = DateTime.Parse("2019-06-20");
|
||
|
||
if (DateTime.UtcNow > dateTime2019)
|
||
{
|
||
throw new NotImplementedException();
|
||
}
|
||
|
||
|
||
if (this.Count >= VersionControl.CopterUpperLimit)
|
||
{
|
||
return 0;
|
||
}
|
||
|
||
int _index = 0;
|
||
if (this.Count == 0)
|
||
{
|
||
Add(entityObject);
|
||
_index = 0;
|
||
}
|
||
else
|
||
{
|
||
bool isInsret = false;
|
||
for (int i = 0; i < this.Count; i++)
|
||
{
|
||
if ( int.Parse(this[i].Id) > int.Parse(entityObject.Id))
|
||
//if (String.Compare(this[i].Name, entityObject.Name, false) >= 0)
|
||
{
|
||
InsertItem(i, entityObject);
|
||
isInsret = true;
|
||
_index= i;
|
||
break;
|
||
|
||
}
|
||
}
|
||
if (!isInsret)
|
||
{
|
||
Add(entityObject);
|
||
_index = this.Count()-1 ;
|
||
}
|
||
}
|
||
return _index;
|
||
}
|
||
}
|
||
|
||
|
||
public class CopterManager : ObservableObject
|
||
{
|
||
public CopterManager()
|
||
{
|
||
AppEx.Current.AppModeChanged += (sender, e) =>
|
||
{
|
||
RaisePropertyChanged(nameof(Copters));
|
||
};
|
||
App.Current.Exit += (sender, e) =>
|
||
{
|
||
Task.WhenAll(Copters.Select(c => c.DisconnectAsync()));
|
||
};
|
||
|
||
}
|
||
|
||
public List<ICopter> ShowCopter = new List<ICopter>();
|
||
|
||
public CopterCollection Copters { get;} = new CopterCollection();
|
||
|
||
public ArrayList CopterStatus = new ArrayList();
|
||
//public ObservableCollection<ICopter> Copters
|
||
//{
|
||
// get
|
||
// {
|
||
// switch (AppEx.Instance.CurrentMode)
|
||
// {
|
||
// case AppMode.ControllingCopters:
|
||
// default:
|
||
// return CoptersForControlling;
|
||
// case AppMode.PreparedForRunningTasks:
|
||
// case AppMode.ModifyingTask:
|
||
// return CoptersForModifyingTask;
|
||
// case AppMode.RunningTasks:
|
||
// throw new NotImplementedException();
|
||
// }
|
||
// }
|
||
//}
|
||
|
||
public bool shiftkeydown;
|
||
public IEnumerable<ICopter> SelectedCopters { get { return _selectedCoptersGetter().Cast<ICopter>(); } }
|
||
|
||
/// <summary>
|
||
/// 注意:为避免多线程操作出问题,每次使用此属性时都会新建一个 List!
|
||
/// </summary>
|
||
public IEnumerable<ICopter> AcceptingControlCopters { get { return SelectedCopters.ToList(); } }
|
||
private int _SeletedCopterCount;
|
||
public int SeletedCopterCount
|
||
{
|
||
get { return _SeletedCopterCount; }
|
||
set { Set(nameof(SeletedCopterCount), ref _SeletedCopterCount, value); }
|
||
}
|
||
|
||
private Func<IList> _selectedCoptersGetter;
|
||
|
||
private Action<ICopter> _selectCopterAction;
|
||
|
||
public void SetSelectionDelegates(Func<IList> selectedCoptersGetter, Action<ICopter> selectCopterAction)
|
||
{
|
||
_selectedCoptersGetter = selectedCoptersGetter;
|
||
_selectCopterAction = selectCopterAction;
|
||
}
|
||
|
||
public event EventHandler<SelectedCoptersChangedEventArgs> SelectedCoptersChanged;
|
||
|
||
public void RaiseSelectedCoptersChanged(IEnumerable<ICopter> addedCopters, IEnumerable<ICopter> removedCopters)
|
||
{
|
||
SelectedCoptersChanged?.Invoke(this, new SelectedCoptersChangedEventArgs { AddedCopters = addedCopters, RemovedCopters = removedCopters });
|
||
SeletedCopterCount = SelectedCopters.Count();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 选择飞机
|
||
/// </summary>
|
||
/// <param name="copter">Null表示清除所有选择</param>
|
||
public void Select(ICopter copter)
|
||
{
|
||
_selectCopterAction(copter);
|
||
}
|
||
}
|
||
|
||
public class SelectedCoptersChangedEventArgs : EventArgs
|
||
{
|
||
public IEnumerable<ICopter> AddedCopters { get; set; }
|
||
public IEnumerable<ICopter> RemovedCopters { get; set; }
|
||
}
|
||
}
|