2021-10-17 16:04:08 +13:00
|
|
|
|
using RBG_Server;
|
|
|
|
|
using System;
|
2021-10-17 13:16:40 +13:00
|
|
|
|
using System.Collections.Generic;
|
2021-10-17 16:04:08 +13:00
|
|
|
|
using System.IO;
|
2021-10-17 13:16:40 +13:00
|
|
|
|
using System.Linq;
|
2021-10-17 16:04:08 +13:00
|
|
|
|
using System.Net.Sockets;
|
2021-10-17 13:16:40 +13:00
|
|
|
|
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.Navigation;
|
|
|
|
|
using System.Windows.Shapes;
|
|
|
|
|
|
|
|
|
|
namespace RBG_Server_WPF
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Interaction logic for ConnectionPage.xaml
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class ConnectionPage : Page
|
|
|
|
|
{
|
|
|
|
|
public ConnectionPage()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HostButton_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
// Draw host window
|
|
|
|
|
ServerGrid.Visibility = Visibility.Visible;
|
|
|
|
|
ServerSelectPanel.Visibility = Visibility.Collapsed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ConnectButton_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
2021-10-17 16:04:08 +13:00
|
|
|
|
// Draw client window
|
|
|
|
|
ClientGrid.Visibility = Visibility.Visible;
|
|
|
|
|
ServerSelectPanel.Visibility = Visibility.Collapsed;
|
2021-10-17 13:16:40 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void BackButton_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
// Hide host view, show connect view
|
|
|
|
|
ServerGrid.Visibility = Visibility.Collapsed;
|
|
|
|
|
ServerSelectPanel.Visibility = Visibility.Visible;
|
|
|
|
|
}
|
2021-10-17 16:04:08 +13:00
|
|
|
|
|
|
|
|
|
private void ClientBackButton_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
ClientGrid.Visibility = Visibility.Collapsed;
|
|
|
|
|
ServerSelectPanel.Visibility = Visibility.Visible;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ClientServerConnectButton_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
// Run an async connection task
|
|
|
|
|
// This task establishes a connection, then
|
|
|
|
|
Task clientConnect = new Task( async () =>
|
|
|
|
|
{
|
|
|
|
|
TcpClient client = new TcpClient();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await client.ConnectAsync(ServerAddressConnectBox.Text, int.Parse(ServerPortConnectBox.Text));
|
|
|
|
|
if (client.Connected)
|
|
|
|
|
{
|
|
|
|
|
// Invoke the display to update
|
|
|
|
|
Dispatcher.Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
StatusTextBlock.Text = "Connected!";
|
|
|
|
|
StatusTextBlock.Foreground = new SolidColorBrush(Color.FromRgb(255, 255, 255));
|
|
|
|
|
});
|
|
|
|
|
// Run image downloading for the sprite
|
|
|
|
|
// This task is contained in the CommunicationHandler class
|
|
|
|
|
Progress<CommunicationHandler.ProgressData> progress = new Progress<CommunicationHandler.ProgressData>();
|
|
|
|
|
Task t = App.Context.GameCommunicationHandler.InitDataLoader(client.GetStream(), progress);
|
|
|
|
|
progress.ProgressChanged += Progress_ProgressChanged;
|
|
|
|
|
t.Start(); // t is asynchronous; we don't need to monitor/await progress as it fires its progress through the assigned handler
|
|
|
|
|
// We should, however, ensure that we don't make requests on client until progress is completed
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Dispatcher.Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
StatusTextBlock.Text = "Connection Failed";
|
|
|
|
|
StatusTextBlock.Foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Dispatcher.Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
StatusTextBlock.Text = ex.Message[..(128 < ex.Message.Length ? 128 : ex.Message.Length)];
|
|
|
|
|
StatusTextBlock.Foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0));
|
|
|
|
|
});
|
|
|
|
|
client?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Callback that updates the UI with the new images, once downloaded
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
|
private void Progress_ProgressChanged(object sender, CommunicationHandler.ProgressData e)
|
|
|
|
|
{
|
|
|
|
|
// Load sprites as they arrive
|
|
|
|
|
if (e.CurrentActivity == CommunicationHandler.ProgressData.Activity.ImageDownloaded)
|
|
|
|
|
{
|
2021-10-17 16:28:58 +13:00
|
|
|
|
|
2021-10-17 16:04:08 +13:00
|
|
|
|
Dispatcher.Invoke(() => StatusTextBlock.Text = $"Downloaded {e.Progress} of {App.Context.GameCommunicationHandler.ImageList.Count} images.");
|
|
|
|
|
App.Context.GameCommunicationHandler.ImageCollection.TryGetValue(e.Message, out CachedByteArray cachedByteArray);
|
|
|
|
|
BitmapImage image = new();
|
|
|
|
|
image.BeginInit();
|
|
|
|
|
image.StreamSource = new MemoryStream(cachedByteArray, false);
|
|
|
|
|
image.EndInit();
|
|
|
|
|
Image spriteImage = new()
|
|
|
|
|
{
|
|
|
|
|
Source = image
|
|
|
|
|
};
|
2021-10-17 16:28:58 +13:00
|
|
|
|
if (e.Message == App.Context.GameCommunicationHandler.BoardName)
|
2021-10-17 16:04:08 +13:00
|
|
|
|
{
|
2021-10-17 16:28:58 +13:00
|
|
|
|
// Image is the board image
|
|
|
|
|
Dispatcher.Invoke(() =>BoardPreviewImage.Source = image);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Image is a sprite, load it
|
|
|
|
|
LoadedSpriteGrid loadedSprite = null;
|
|
|
|
|
foreach (LoadedSpriteGrid item in ClientLoadedSprites.Children)
|
2021-10-17 16:04:08 +13:00
|
|
|
|
{
|
2021-10-17 16:28:58 +13:00
|
|
|
|
if (item.Equals(e.Message))
|
|
|
|
|
{
|
|
|
|
|
loadedSprite = item;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-10-17 16:04:08 +13:00
|
|
|
|
}
|
2021-10-17 16:40:26 +13:00
|
|
|
|
if (loadedSprite == null)
|
|
|
|
|
{
|
|
|
|
|
//spriteImage.MouseDown += SpriteImage_Tapped;
|
|
|
|
|
//spriteImage.TouchDown += SpriteImage_Tapped;
|
|
|
|
|
//spriteImage.StylusDown += SpriteImage_Tapped;
|
|
|
|
|
loadedSprite = new LoadedSpriteGrid(spriteImage, e.Message);
|
|
|
|
|
|
|
|
|
|
loadedSprite.MouseDown += SpriteImage_Tapped;
|
|
|
|
|
loadedSprite.TouchDown += SpriteImage_Tapped;
|
|
|
|
|
loadedSprite.StylusDown += SpriteImage_Tapped;
|
|
|
|
|
}
|
2021-10-17 16:04:08 +13:00
|
|
|
|
|
2021-10-17 16:28:58 +13:00
|
|
|
|
// Add the loaded image into the view
|
|
|
|
|
Dispatcher.Invoke(() => ClientLoadedSprites.Children.Add(loadedSprite));
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-17 16:04:08 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-17 16:40:26 +13:00
|
|
|
|
LoadedSpriteGrid currentlySelected = null;
|
|
|
|
|
|
|
|
|
|
private void SpriteImage_Tapped(object sender, InputEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
currentlySelected.frame.BorderThickness = new Thickness(0, 0, 0, 0);
|
|
|
|
|
currentlySelected = sender as LoadedSpriteGrid;
|
|
|
|
|
currentlySelected.frame.BorderThickness = new Thickness(3, 3, 3, 3);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-17 16:04:08 +13:00
|
|
|
|
internal class LoadedSpriteGrid : Grid
|
|
|
|
|
{
|
|
|
|
|
string imageName;
|
2021-10-17 16:40:26 +13:00
|
|
|
|
public Border frame;
|
2021-10-17 16:04:08 +13:00
|
|
|
|
public LoadedSpriteGrid(Image spriteImage, string imageName)
|
|
|
|
|
{
|
2021-10-17 16:40:26 +13:00
|
|
|
|
// Listen to events
|
|
|
|
|
frame = new Border();
|
|
|
|
|
frame.Child = spriteImage;
|
|
|
|
|
frame.BorderThickness = new Thickness(0, 0, 0, 0);
|
|
|
|
|
frame.BorderBrush = new SolidColorBrush(Color.FromArgb(255, 0, 100, 200));
|
|
|
|
|
Children.Add(frame);
|
2021-10-17 16:04:08 +13:00
|
|
|
|
this.imageName = imageName;
|
|
|
|
|
}
|
2021-10-17 16:40:26 +13:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-10-17 16:04:08 +13:00
|
|
|
|
public new bool Equals(object obj)
|
|
|
|
|
{
|
|
|
|
|
if (obj == null || GetType() != obj.GetType())
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return imageName.Equals(((LoadedSpriteGrid)obj).imageName);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-17 13:16:40 +13:00
|
|
|
|
}
|
|
|
|
|
}
|