Interaction-without-Interac.../FrontEnd/MainWindow.xaml.cs

221 lines
8.1 KiB
C#
Raw Normal View History

2019-06-06 21:30:30 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace FrontEnd
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
2019-07-18 18:37:56 +00:00
// Load and set canvas background image.
2019-06-06 21:30:30 +00:00
var img = new BitmapImage(new Uri(@"pack://application:,,,/Images/map.png"));
imgMap.Source = img;
}
2019-07-18 18:37:56 +00:00
/// <summary>
/// Creates a camera marker on the map.
/// </summary>
2019-06-07 10:58:11 +00:00
private void CreateCameraMarker(Point pos, Cam cam)
2019-06-06 21:30:30 +00:00
{
2019-07-18 18:37:56 +00:00
// Represent the client with a custom images.
// Load the green camera image from the resources when it's active and the red one when not.
2019-06-07 10:58:11 +00:00
var img = new Image
2019-06-06 21:30:30 +00:00
{
2019-06-07 10:58:11 +00:00
Source = new BitmapImage(new Uri($@"pack://application:,,,/Images/cam_{(cam.Status ? "green" : "red")}.png")),
2019-06-06 21:30:30 +00:00
RenderTransformOrigin = new Point(0.5, 0.5),
2019-06-07 10:58:11 +00:00
RenderTransform = new RotateTransform(cam.Angle),
2019-06-06 21:30:30 +00:00
Width = 32,
2019-06-07 10:58:11 +00:00
Height = 32,
2019-06-07 18:03:39 +00:00
Tag = cam,
2019-06-07 10:58:11 +00:00
ToolTip = cam.Label
2019-06-06 21:30:30 +00:00
};
2019-07-18 18:37:56 +00:00
// Set mouse event handlers.
2019-06-07 10:58:11 +00:00
img.MouseDown += Cam_MouseDown;
2019-06-27 18:45:10 +00:00
img.MouseEnter += Img_MouseEnter;
2019-07-18 18:37:56 +00:00
// Place the camera image on the canvas.
2019-06-07 10:58:11 +00:00
cnvMap.Children.Add(img);
Canvas.SetLeft(img, pos.X - img.Width / 2);
Canvas.SetTop(img, pos.Y - img.Height / 2);
2019-06-06 21:30:30 +00:00
}
2019-07-18 18:37:56 +00:00
/// <summary>
/// Select the corresponding list item of the hovered camera marker.
/// </summary>
2019-06-27 18:45:10 +00:00
private void Img_MouseEnter(object sender, MouseEventArgs e)
{
var cam = (Cam)((Image)sender).Tag;
var newSelection = from ListBoxItem item in lstDevices.Items
where cam.Label.Equals(item.Content)
select item;
if (newSelection.Any())
lstDevices.SelectedItem = newSelection.First();
}
2019-07-18 18:37:56 +00:00
/// <summary>
/// Show the stream of the hovered camera marker. (LMB = original stream, LMB = processed stream)
/// </summary>
2019-06-06 21:30:30 +00:00
private void Cam_MouseDown(object sender, MouseButtonEventArgs e)
{
2019-06-07 18:03:39 +00:00
var cam = (Cam)((Image)sender).Tag;
if (e.LeftButton == MouseButtonState.Pressed)
new StreamWindow(cam, false).ShowDialog();
else if (e.RightButton == MouseButtonState.Pressed)
new StreamWindow(cam, true).ShowDialog();
2019-06-06 21:30:30 +00:00
}
2019-07-18 18:37:56 +00:00
/// <summary>
/// Creates a client marker on the map.
/// </summary>
2019-06-07 10:58:11 +00:00
private void CreateClientMarker(Point pos, Client client)
2019-06-06 21:30:30 +00:00
{
2019-07-18 18:37:56 +00:00
// Represent the client with a colored ellipse.
// The color is green when active and red when not.
2019-06-06 21:30:30 +00:00
var ellipse = new Ellipse
{
Width = 20,
Height = 20,
2019-06-07 10:58:11 +00:00
Fill = client.Status ? Brushes.Green : Brushes.Red,
Stroke = Brushes.Black,
StrokeThickness = 1,
2019-06-27 18:45:10 +00:00
ToolTip = client.Label,
Tag = client
2019-06-06 21:30:30 +00:00
};
2019-07-18 18:37:56 +00:00
// Set mouse event handlers.
2019-06-27 18:45:10 +00:00
ellipse.MouseEnter += Ellipse_MouseEnter;
2019-07-18 18:37:56 +00:00
// Place the ellipse on the canvas.
2019-06-06 21:30:30 +00:00
cnvMap.Children.Add(ellipse);
Canvas.SetLeft(ellipse, pos.X - ellipse.Width / 2);
Canvas.SetTop(ellipse, pos.Y - ellipse.Height / 2);
}
2019-06-27 18:45:10 +00:00
2019-07-18 18:37:56 +00:00
/// <summary>
/// Select the corresponding list item of the hovered client marker.
/// </summary>
2019-06-27 18:45:10 +00:00
private void Ellipse_MouseEnter(object sender, MouseEventArgs e)
{
var client = (Client)((Ellipse)sender).Tag;
var newSelection = from ListBoxItem item in lstDevices.Items
where client.Label.Equals(item.Content)
select item;
if (newSelection.Any())
lstDevices.SelectedItem = newSelection.First();
}
2019-06-06 21:30:30 +00:00
private void Window_Loaded(object sender, RoutedEventArgs e)
{
2019-06-07 10:58:11 +00:00
LoadInformation();
2019-07-18 18:37:56 +00:00
// Set a timer to periodically reload shown information.
2019-06-07 17:26:52 +00:00
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 5);
dispatcherTimer.Start();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
2019-07-18 18:37:56 +00:00
// Get the current selected list item to restore the selection after refresh.
2019-06-07 17:26:52 +00:00
var selectedItem = (ListBoxItem)lstDevices.SelectedItem;
2019-06-27 18:45:10 +00:00
LoadInformation();
2019-06-07 17:26:52 +00:00
if (selectedItem != null)
{
var newSelection = from ListBoxItem item in lstDevices.Items
where selectedItem.Content.Equals(item.Content)
select item;
if (newSelection.Any())
lstDevices.SelectedItem = newSelection.First();
}
2019-06-07 10:58:11 +00:00
}
2019-07-18 18:37:56 +00:00
/// <summary>
/// Populates the canvas map and device list.
/// </summary>
2019-06-07 10:58:11 +00:00
private void LoadInformation()
{
2019-07-18 18:37:56 +00:00
// Retrieve cam and client objects.
2019-06-07 10:58:11 +00:00
List<Cam> cams = Task.Run(async () => { return await Communicator.GetCamsAsync(); }).Result;
List<Client> clients = Task.Run(async () => { return await Communicator.GetClientsAsync(); }).Result;
2019-07-18 18:37:56 +00:00
// Clear canvas map and properly set it's size.
2019-06-07 10:58:11 +00:00
cnvMap.Children.Clear();
2019-06-06 21:30:30 +00:00
cnvMap.Width = imgMap.ActualWidth;
cnvMap.Height = imgMap.ActualHeight;
2019-07-18 18:37:56 +00:00
// Also clear the list and description textbox.
2019-06-07 10:58:11 +00:00
lstDevices.Items.Clear();
txtInfo.Clear();
2019-06-06 21:30:30 +00:00
2019-07-18 18:37:56 +00:00
// Create client markers on canvas map.
2019-06-07 10:58:11 +00:00
foreach (var client in clients)
2019-06-06 21:30:30 +00:00
{
2019-06-07 10:58:11 +00:00
CreateClientMarker(new Point(cnvMap.Width * client.X, cnvMap.Height * client.Y), client);
2019-06-06 21:30:30 +00:00
}
2019-06-07 10:58:11 +00:00
foreach (var cam in cams)
2019-06-06 21:30:30 +00:00
{
2019-07-18 18:37:56 +00:00
// Create a cam marker on the canvas map...
2019-06-07 10:58:11 +00:00
CreateCameraMarker(new Point(cnvMap.Width * cam.X, cnvMap.Height * cam.Y), cam);
2019-07-18 18:37:56 +00:00
// ...and create a correspondig list item...
2019-06-07 10:58:11 +00:00
ListBoxItem item = new ListBoxItem
{
Tag = cam,
Content = cam.Label,
FontWeight = FontWeights.Bold
};
2019-06-06 21:30:30 +00:00
lstDevices.Items.Add(item);
2019-06-07 10:58:11 +00:00
2019-07-18 18:37:56 +00:00
// ...with associated clients as subitems.
2019-06-07 10:58:11 +00:00
foreach (var client in clients.Where(c => c.Id == cam.Client_Id))
2019-06-06 21:30:30 +00:00
{
2019-06-07 10:58:11 +00:00
ListBoxItem subitem = new ListBoxItem
{
Tag = client,
Content = client.Label
};
2019-06-06 21:30:30 +00:00
lstDevices.Items.Add(subitem);
2019-06-07 10:58:11 +00:00
}
2019-06-06 21:30:30 +00:00
}
}
private void LstDevices_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
2019-07-18 18:37:56 +00:00
// Show device properties in textbox.
2019-06-06 21:30:30 +00:00
var selectedItem = lstDevices.SelectedItem as ListBoxItem;
if (selectedItem != null)
{
if (selectedItem.Tag is Client client)
{
2019-06-07 10:58:11 +00:00
txtInfo.Text = client.ToString();
2019-06-06 21:30:30 +00:00
}
else if (selectedItem.Tag is Cam cam)
{
2019-06-07 10:58:11 +00:00
txtInfo.Text = cam.ToString();
2019-06-06 21:30:30 +00:00
}
}
}
2019-06-07 10:58:11 +00:00
private void BtnRefresh_Click(object sender, RoutedEventArgs e)
{
2019-07-18 18:37:56 +00:00
// Reload information on button click.
2019-06-07 10:58:11 +00:00
LoadInformation();
}
2019-06-06 21:30:30 +00:00
}
}