Plane.FormationCreator/Plane.FormationCreator/MainWindow.xaml.cs
2021-07-11 12:57:45 +08:00

578 lines
24 KiB
C#

using Plane.Copters;
using Plane.FormationCreator.Formation;
using Plane.FormationCreator.ViewModels;
using Plane.FormationCreator.Views;
using Plane.Windows.Messages;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using Microsoft.Practices.ServiceLocation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Plane.CommunicationManagement;
using System.Reflection;
namespace Plane.FormationCreator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : MetroWindow
{
public MainWindow()
{
InitializeComponent();
this.DataContext = ServiceLocator.Current.GetInstance<MainViewModel>();
// this.OnClosing += Window_Closing;
//改到设置窗口显示版本信息
// var version = Assembly.GetExecutingAssembly().GetName().Version;
// Title = "飞行魔方无人机编队控制系统 V" + version.ToString() + "编译时间[" + System.IO.File.GetLastWriteTime(this.GetType().Assembly.Location).ToString() + "]";
//ShowConnectDialog();
//if (_copterManager.CoptersForControlling.Count <= 0)
//{
// //App.Current.Shutdown();
//}
}
private CommModuleManager _commModuleManager = CommModuleManager.Instance;
private CopterManager _copterManager = ServiceLocator.Current.GetInstance<CopterManager>();
private FlightTaskManager _flightTaskManager= ServiceLocator.Current.GetInstance<FlightTaskManager>();
private void ShowConnectDialog(ConnectWindow loginWindow = null)
{
//var result = await this.ShowLoginAsync("登录", "请输入您的帐户 ID 和密码");
if (loginWindow == null)
{
loginWindow = new ConnectWindow();
}
loginWindow.ShowDialog ();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
bool waitnet = _copterManager.Logined;
_copterManager.NetLogout();
//等待网络退出
if (waitnet)
System.Threading.Thread.Sleep(1000);
/*
MessageBoxResult result = MessageBox.Show("Do you really want to exit?", "", MessageBoxButton.YesNo);
if (result == MessageBoxResult.No)
{
e.Cancel = true;
}
*/
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ServiceLocator.Current.GetInstance<MainViewModel>().LoginCommand.Execute(this);
}
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
var loginWindow = new ConnectWindow
{
WindowTransitionsEnabled = false,
WindowStartupLocation = WindowStartupLocation.Manual
};
var point = PointToScreen(Mouse.GetPosition(this));
point.X = Math.Min(point.X, SystemParameters.WorkArea.Width - loginWindow.Width - 30);
point.Y = Math.Min(point.Y, SystemParameters.WorkArea.Height - loginWindow.Height - 30);
loginWindow.Left = point.X;
loginWindow.Top = point.Y;
ShowConnectDialog(loginWindow);
}
private void btnConnect_Click(object sender, RoutedEventArgs e)
{
var connectWindow = new ConnectWindow
{
WindowTransitionsEnabled = false,
WindowStartupLocation = WindowStartupLocation.Manual
};
var point = PointToScreen(Mouse.GetPosition(this));
point.X = Math.Min(point.X, SystemParameters.WorkArea.Width - connectWindow.Width - 30);
point.Y = Math.Min(point.Y+10, SystemParameters.WorkArea.Height - connectWindow.Height - 30);
connectWindow.Left = point.X;
connectWindow.Top = point.Y;
ShowConnectDialog(connectWindow);
}
private void btnRefreshMap_Click(object sender, RoutedEventArgs e)
{
map.Refresh();
}
private void btnGoHome_Click(object sender, RoutedEventArgs e)
{
map.GoHome();
}
bool Shiftkeydown = false;
private async void MetroWindow_PreviewKeyDown(object sender, KeyEventArgs e)
{
// 如果正在输入,忽略。
if (e.OriginalSource is TextBox || e.OriginalSource is PasswordBox) return;
var isInFastMode = AppEx.Current.IsInFastMode;
bool isledon;
switch (e.Key)
{
case Key.LeftCtrl:
case Key.RightCtrl:
case Key.LeftAlt:
{
var copters = _copterManager.AcceptingControlCopters;
Shiftkeydown = true;
_copterManager.shiftkeydown = true;
break;
}
//开关SHOWLED
case Key.C:
{
var copters = _copterManager.AcceptingControlCopters;
await Task.WhenAll(copters.Select(async copter =>
{
isledon = copter.GetShowLEDAsync();
await copter.SetShowLEDAsync(!isledon);
})).ConfigureAwait(false);
break;
}
//俯仰控制
case Key.W:
case Key.S:
{
if (Shiftkeydown &&(_flightTaskManager.CurrentRunningTask == null) && (_flightTaskManager.SelectedTask != null))
{
var selectedCopter = _copterManager.SelectedCopters.FirstOrDefault();
bool copterisselect;
for (int i = 0; i < _flightTaskManager.SelectedTask.SingleCopterInfos.Count; i++)
{
copterisselect = false;
selectedCopter = _flightTaskManager.SelectedTask.SingleCopterInfos[i].Copter;
foreach (var capter in _copterManager.SelectedCopters)
{
if (capter == selectedCopter)
copterisselect = true;
}
if (copterisselect)
{
// double tlat = _flightTaskManager.SelectedTask.SingleCopterInfos[i].TargetLat;
// int tmplat = (int)(tlat * 100000);
// tlat = (float)(tmplat * 1.0) / 100000;
// _flightTaskManager.SelectedTask.SingleCopterInfos[i].TargetLat = tlat;
_flightTaskManager.SelectedTask.SingleCopterInfos[i].TargetLat+= e.Key == Key.W ? 0.00001 : -0.00001;
}
}
break;
}
var copters = _copterManager.AcceptingControlCopters;
if (isInFastMode)
{
await _commModuleManager.SetChannelsAsync(copters, ch2: e.Key == Key.W ? (ushort)1100 : (ushort)1900);
}
else
{
await _commModuleManager.SetChannelsAsync(copters, ch2: e.Key == Key.W ? (ushort)1350 : (ushort)1650);
}
// await Task.WhenAll(copters.Select(async copter =>
// {
// if (isInFastMode)
// {
// await copter.SetChannelsAsync(ch2: e.Key == Key.W ? (ushort)1100 : (ushort)1900);
// }
// else
// {
// await copter.SetChannelsAsync(ch2: e.Key == Key.W ? (ushort)1350 : (ushort)1650);
// }
// })).ConfigureAwait(false);
break;
}
//横滚控制
case Key.A:
case Key.D:
{
if (Shiftkeydown && (_flightTaskManager.CurrentRunningTask == null)&&(_flightTaskManager.SelectedTask!=null))
{
var selectedCopter = _copterManager.SelectedCopters.FirstOrDefault();
bool copterisselect;
for (int i = 0; i < _flightTaskManager.SelectedTask.SingleCopterInfos.Count; i++)
{
copterisselect = false;
selectedCopter = _flightTaskManager.SelectedTask.SingleCopterInfos[i].Copter;
foreach (var capter in _copterManager.SelectedCopters)
{
if (capter == selectedCopter)
copterisselect = true;
}
if (copterisselect)
{
// double tlng = _flightTaskManager.SelectedTask.SingleCopterInfos[i].TargetLng;
// int tmplng = (int)(tlng * 100000);
// tlng = (float)(tmplng * 1.0) / 100000;
// _flightTaskManager.SelectedTask.SingleCopterInfos[i].TargetLng = tlng;
_flightTaskManager.SelectedTask.SingleCopterInfos[i].TargetLng += e.Key == Key.D ? 0.00001 : -0.00001;
}
}
break;
}
var copters = _copterManager.AcceptingControlCopters;
if (isInFastMode)
{
await _commModuleManager.SetChannelsAsync(copters, ch1: e.Key == Key.A ? (ushort)1100 : (ushort)1900);
}
else
{
await _commModuleManager.SetChannelsAsync(copters, ch1: e.Key == Key.A ? (ushort)1350 : (ushort)1650);
}
// await Task.WhenAll(copters.Select(async copter =>
// {
// if (isInFastMode)
// {
// await copter.SetChannelsAsync(ch1: e.Key == Key.A ? (ushort)1100 : (ushort)1900);
// }
// else
// {
// await copter.SetChannelsAsync(ch1: e.Key == Key.A ? (ushort)1350 : (ushort)1650);
// }
// })).ConfigureAwait(false);
break;
}
//高度控制
case Key.I:
case Key.K:
{
var copters = _copterManager.AcceptingControlCopters;
if (isInFastMode)
{
await _commModuleManager.SetChannelsAsync(copters, ch3: e.Key == Key.K ? (ushort)1100 : (ushort)1900);
}
else
{
await _commModuleManager.SetChannelsAsync(copters, ch3: e.Key == Key.K ? (ushort)1350 : (ushort)1650);
}
/*
await Task.WhenAll(copters.Select(async copter =>
{
if (isInFastMode)
{
await copter.SetChannelsAsync(ch3: e.Key == Key.K ? (ushort)1100 : (ushort)1900);
}
else
{
await copter.SetChannelsAsync(ch3: e.Key == Key.K ? (ushort)1350 : (ushort)1650);
}
})).ConfigureAwait(false);
*/
break;
}
//航向控制
case Key.J:
case Key.L:
{
var copters = _copterManager.AcceptingControlCopters;
await _commModuleManager.SetChannelsAsync(copters, ch4: e.Key == Key.J ? (ushort)1200 : (ushort)1800);
/*
await Task.WhenAll(copters.Select(async copter =>
{
//await copter.SetMobileControlAsync(yaw: (copter.Yaw + (e.Key == Key.J ? -20 : 20)) % 360);
await copter.SetChannelsAsync(ch4: e.Key == Key.J ? (ushort)1200 : (ushort)1800);
})).ConfigureAwait(false);
*/
break;
}
case Key.M: // 低速下降。
{
var copters = _copterManager.AcceptingControlCopters;
await Task.WhenAll(copters.Select(async copter =>
{
await copter.SetChannelsAsync(ch3: 1350);
})).ConfigureAwait(false);
break;
}
case Key.N: // 飞行器着陆后,按此键将油门打到 1100.
{
var copters = _copterManager.AcceptingControlCopters;
await Task.WhenAll(copters.Select(async copter =>
{
await copter.SetChannelsAsync(ch3: 1100);
})).ConfigureAwait(false);
break;
}
case Key.Back: // 锁定。
{
if (Alert.Show("您确定要上锁吗?飞行器将立即停止运转!", "警告", MessageBoxButton.OKCancel, MessageBoxImage.Warning)
== MessageBoxResult.OK)
{
var copters = _copterManager.AcceptingControlCopters;
await Task.WhenAll(copters.Select(async copter =>
{
await copter.LockAsync();
})).ConfigureAwait(false);
}
break;
}
//设置6通道
case Key.P:
{
var copters = _copterManager.AcceptingControlCopters;
await Task.WhenAll(copters.Select(async copter =>
{
if (isInFastMode)
{
if (e.Key == Key.P)
{
await copter.SetChannelsAsync(ch6: (ushort)1900);
}
}
else
{
if (e.Key == Key.P)
{
await copter.SetChannelsAsync(ch6: (ushort)1900);
}
}
})).ConfigureAwait(false);
break;
}
//设置7通道
case Key.O:
{
var copters = _copterManager.AcceptingControlCopters;
await Task.WhenAll(copters.Select(async copter =>
{
if (isInFastMode)
{
if (e.Key == Key.O)
{
await copter.SetChannelsAsync(ch7: (ushort)1900);
}
}
else
{
if (e.Key == Key.O)
{
await copter.SetChannelsAsync(ch7: (ushort)1900);
}
}
})).ConfigureAwait(false);
break;
}
}
}
private async void MetroWindow_PreviewKeyUp(object sender, KeyEventArgs e)
{
// 如果正在输入,忽略。
if (e.OriginalSource is TextBox || e.OriginalSource is PasswordBox) return;
switch (e.Key)
{
case Key.LeftCtrl:
case Key.RightCtrl:
case Key.LeftAlt:
{
Shiftkeydown = false;
_copterManager.shiftkeydown = false;
break;
}
case Key.W:
case Key.S:
{
var copters = _copterManager.AcceptingControlCopters;
for (int i = 0; i < 3; i++)
{
await _commModuleManager.SetChannelsAsync(copters, ch2: 1500);
await Task.Delay(100);
}
// await Task.WhenAll(copters.Select(async copter =>
// {
// await copter.SetChannelsAsync(ch2: 1500);
// })).ConfigureAwait(false);
break;
}
case Key.A:
case Key.D:
{
var copters = _copterManager.AcceptingControlCopters;
for (int i = 0; i < 3; i++)
{
await _commModuleManager.SetChannelsAsync(copters, ch1: 1500);
await Task.Delay(100);
}
// await Task.WhenAll(copters.Select(async copter =>
// {
// await copter.SetChannelsAsync(ch1: 1500);
// })).ConfigureAwait(false);
break;
}
case Key.I:
case Key.K:
{
var copters = _copterManager.AcceptingControlCopters;
for (int i = 0; i < 3; i++)
{
await _commModuleManager.SetChannelsAsync(copters, ch3: 1500);
await Task.Delay(100);
}
// await Task.WhenAll(copters.Select(async copter =>
// {
// await copter.SetChannelsAsync(ch3: 1500);
// })).ConfigureAwait(false);
break;
}
case Key.J:
case Key.L:
{
var copters = _copterManager.AcceptingControlCopters;
for (int i = 0; i < 3; i++)
{
await _commModuleManager.SetChannelsAsync(copters, ch4: 1500);
await Task.Delay(100);
}
// await Task.WhenAll(copters.Select(async copter =>
// {
// // await copter.SetMobileControlAsync(yaw: (copter.Yaw + (e.Key == Key.J ? -10 : 10)) % 360);
// await copter.SetChannelsAsync(ch4: 1500);
// })).ConfigureAwait(false);
break;
}
case Key.M: // 停止低速下降。
{
var copters = _copterManager.AcceptingControlCopters;
await Task.WhenAll(copters.Select(async copter =>
{
await copter.SetChannelsAsync(ch3: 1500);
})).ConfigureAwait(false);
break;
}
}
}
private void btnShowLog_Click(object sender, RoutedEventArgs e)
{
MainViewModel vm = DataContext as MainViewModel;
LogWindow logWindows = new LogWindow(vm.Messages);
logWindows.ShowDialog();
}
private void LogShowHide(object sender, MouseButtonEventArgs e)
{
if (logGrid.Visibility == Visibility.Visible)
logGrid.Visibility = Visibility.Hidden;
else
{
logGrid.Height = Math.Max(map.ActualHeight, map3D.ActualHeight) - ((TaskBarView)TaskbarControl).TasksControl.ActualHeight;
logTextBox.Height = logGrid.Height;
logGrid.Visibility = Visibility.Visible;
}
}
private void LogTextChange(object sender, TextChangedEventArgs e)
{
logTextBox.ScrollToEnd();
}
private void Map_Opacity_Click(object sender, RoutedEventArgs e)
{
if (map.Opacity == 1)
{
map.Opacity = 0.5;
}
else
{
map.Opacity = 1;
}
}
private void btnAttribute_Click(object sender, RoutedEventArgs e)
{
CheckBox checkbox = sender as CheckBox;
if (checkbox.IsChecked == false)
attributeView.Visibility = Visibility.Collapsed;
else
attributeView.Visibility = Visibility.Visible;
}
private void btnGroup_Click(object sender, RoutedEventArgs e)
{
CheckBox checkbox = sender as CheckBox;
if (checkbox.IsChecked == false)
groupsView.Visibility = Visibility.Collapsed;
else
groupsView.Visibility = Visibility.Visible;
}
private void btnVirtualId_Click(object sender, RoutedEventArgs e)
{
CheckBox checkbox = sender as CheckBox;
if (checkbox.IsChecked == false)
configVirtualView.Visibility = Visibility.Collapsed;
else
configVirtualView.Visibility = Visibility.Visible;
}
}
}