2023-08-29 08:52:35 -04:00
using Avalonia.Controls ;
using CollabVMClient.ViewModels ;
using CollabVMClient.Views ;
using CollabVMSharp ;
using MsBox.Avalonia ;
using SixLabors.ImageSharp ;
using SixLabors.ImageSharp.Advanced ;
using SixLabors.ImageSharp.PixelFormats ;
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
namespace CollabVMClient
{
public class CollabVMViewer
{
private CollabVMSharp . CollabVMClient cvm ;
private CollabVMScreenViewModel screen_vm ;
private CollabVMScreenView screen ;
private CollabVMChatViewModel chat_vm ;
private CollabVMChatView chat ;
private UserListViewModel users_vm ;
private UserList users ;
private string url ;
private string node ;
public CollabVMViewer ( string url , string node )
{
this . url = url ;
this . node = node ;
cvm = new CollabVMSharp . CollabVMClient ( url , null , node ) ;
cvm . ScreenSize + = Cvm_ScreenSize ;
cvm . Rect + = Cvm_Rect ;
cvm . Chat + = Cvm_Chat ;
cvm . ChatHistory + = Cvm_ChatHistory ;
cvm . Renamed + = Cvm_Renamed ;
cvm . UserJoined + = Cvm_UserJoined ;
cvm . UserLeft + = Cvm_UserLeft ;
cvm . UserRenamed + = Cvm_UserRenamed ;
cvm . TurnUpdate + = Cvm_TurnUpdate ;
2023-08-29 13:39:51 -04:00
screen_vm = new CollabVMScreenViewModel ( cvm , node ) ;
2023-08-29 08:52:35 -04:00
screen = new CollabVMScreenView ( ) ;
screen . DataContext = screen_vm ;
chat = new CollabVMChatView ( ) ;
chat_vm = new CollabVMChatViewModel ( node ) ;
chat . DataContext = chat_vm ;
chat . ChatSend + = ( s , e ) = > cvm . SendChat ( e ) ;
users_vm = new UserListViewModel ( node ) ;
users = new UserList ( ) ;
users . DataContext = users_vm ;
// Exit if any window is closed
screen . Closed + = WindowClosed ;
chat . Closed + = WindowClosed ;
users . Closed + = WindowClosed ;
}
private void Cvm_TurnUpdate ( object? sender , TurnUpdateEventArgs e )
{
users_vm . ParseTurnUpdate ( e ) ;
//users.ReloadUsers();
}
private void Cvm_UserRenamed ( object? sender , UserRenamedEventArgs e )
{
users_vm . RenameUser ( e . OldName , e . NewName ) ;
}
private void Cvm_UserLeft ( object? sender , User e )
{
users_vm . RemoveUser ( e . Username ) ;
}
private void Cvm_UserJoined ( object? sender , User e )
{
users_vm . AddUser ( e . Username , e . Rank ) ;
}
public async Task < bool > Open ( )
{
try {
await cvm . Connect ( ) ;
} catch ( Exception e )
{
await MessageBoxManager . GetMessageBoxStandard ( "CollabVM Connection Error" , $"Failed to connect to the CollabVM Server at {url}:\n{e.Message}" , MsBox . Avalonia . Enums . ButtonEnum . Ok , MsBox . Avalonia . Enums . Icon . Error ) . ShowAsync ( ) ;
return false ;
}
cvm . ConnectedToNode + = Cvm_ConnectedToNode ;
cvm . NodeConnectFailed + = Cvm_NodeConnectFailed ;
return true ;
}
private void Cvm_NodeConnectFailed ( object? sender , EventArgs e )
{
MessageBoxManager . GetMessageBoxStandard ( "CollabVM Connection Error" , $"Failed to connect to the CollabVM Server at {url}:\n{node} is not a valid node on this server." , MsBox . Avalonia . Enums . ButtonEnum . Ok , MsBox . Avalonia . Enums . Icon . Error ) . ShowAsync ( ) ;
}
private void Cvm_ConnectedToNode ( object? sender , EventArgs e )
{
screen . Show ( ) ;
chat . Show ( ) ;
users . Show ( ) ;
}
private void WindowClosed ( object? sender , EventArgs e )
{
cvm . Disconnect ( ) ;
screen . Close ( ) ;
chat . Close ( ) ;
users . Close ( ) ;
}
private void Cvm_Renamed ( object? sender , string e )
{
chat_vm . SetUsername ( e ) ;
}
private void Cvm_ChatHistory ( object? sender , ChatMessage [ ] e )
{
foreach ( var chat in e )
{
chat_vm . AddChatMessage ( chat . Username , chat . Message ) ;
}
}
private void Cvm_Chat ( object? sender , ChatMessage e )
{
chat_vm . AddChatMessage ( e . Username , e . Message ) ;
}
private void Cvm_ScreenSize ( object? sender , ScreenSizeEventArgs e )
{
screen_vm . SetSize ( e . Width , e . Height ) ;
}
private void Cvm_Rect ( object? sender , RectEventArgs e )
{
byte [ ] rect = new byte [ e . Data . Width * e . Data . Height * 4 ] ;
e . Data . CloneAs < Rgba32 > ( ) . CopyPixelDataTo ( rect ) ;
screen_vm . LoadRect ( rect , e . Data . Width , e . Data . Height , e . X , e . Y ) ;
screen . RefreshScreen ( ) ;
}
}
}