2023-08-29 08:52:35 -04:00
|
|
|
|
using Avalonia.Media.Imaging;
|
|
|
|
|
using Avalonia.Platform;
|
|
|
|
|
using SixLabors.ImageSharp.PixelFormats;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using SixLabors.ImageSharp;
|
|
|
|
|
using SixLabors.ImageSharp.Processing;
|
|
|
|
|
using Avalonia.Media;
|
|
|
|
|
using Avalonia.Controls;
|
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
2023-08-29 13:39:51 -04:00
|
|
|
|
using CollabVMSharp;
|
2023-08-29 08:52:35 -04:00
|
|
|
|
|
|
|
|
|
namespace CollabVMClient.ViewModels
|
|
|
|
|
{
|
|
|
|
|
internal partial class CollabVMScreenViewModel : ViewModelBase
|
|
|
|
|
{
|
2023-08-29 13:39:51 -04:00
|
|
|
|
private CollabVMSharp.CollabVMClient cvm;
|
2023-08-29 08:52:35 -04:00
|
|
|
|
public WriteableBitmap Framebuffer { get; set; }
|
|
|
|
|
[ObservableProperty] private string id;
|
|
|
|
|
[ObservableProperty] private int width;
|
|
|
|
|
[ObservableProperty] private int height;
|
2023-08-29 13:39:51 -04:00
|
|
|
|
[ObservableProperty] private int windowHeight;
|
|
|
|
|
[ObservableProperty] private string toggleTurnBtnText = "Take Turn";
|
|
|
|
|
|
2023-08-29 08:52:35 -04:00
|
|
|
|
public string Title => $"CollabVM - Screen ({id})";
|
2023-08-29 13:39:51 -04:00
|
|
|
|
public CollabVMScreenViewModel(CollabVMSharp.CollabVMClient cvm, string id)
|
2023-08-29 08:52:35 -04:00
|
|
|
|
{
|
2023-08-29 13:39:51 -04:00
|
|
|
|
this.cvm = cvm;
|
|
|
|
|
cvm.TurnUpdate += (_, _) =>
|
|
|
|
|
{
|
|
|
|
|
if (cvm.TurnStatus == TurnStatus.None) ToggleTurnBtnText = "Take Turn";
|
|
|
|
|
else ToggleTurnBtnText = "End Turn";
|
|
|
|
|
};
|
2023-08-29 08:52:35 -04:00
|
|
|
|
this.Id = id;
|
|
|
|
|
Framebuffer = new WriteableBitmap(new Avalonia.PixelSize(1, 1), new Avalonia.Vector(1, 1));
|
|
|
|
|
}
|
|
|
|
|
public void SetSize(int width, int height)
|
|
|
|
|
{
|
|
|
|
|
this.Width = width;
|
|
|
|
|
this.Height = height;
|
2023-08-29 13:39:51 -04:00
|
|
|
|
this.WindowHeight = height + 34;
|
2023-08-29 08:52:35 -04:00
|
|
|
|
Framebuffer = new WriteableBitmap(new Avalonia.PixelSize(width, height), new Avalonia.Vector(96.0f, 96.0f), PixelFormat.Rgba8888);
|
|
|
|
|
}
|
|
|
|
|
public void LoadRect(byte[] rect, int width, int height, int x, int y)
|
|
|
|
|
{
|
|
|
|
|
// writing this method made me suicidal
|
|
|
|
|
var l = Framebuffer.Lock();
|
|
|
|
|
var byteswritten = 0;
|
|
|
|
|
for (var i = 0; i < height; i++)
|
|
|
|
|
{
|
|
|
|
|
Marshal.Copy(rect, byteswritten, IntPtr.Add(l.Address, (int)(4 * ((y + i) * Framebuffer.Size.Width + x))), width * 4);
|
|
|
|
|
byteswritten += width * 4;
|
|
|
|
|
}
|
|
|
|
|
l.Dispose();
|
|
|
|
|
|
|
|
|
|
}
|
2023-08-29 13:39:51 -04:00
|
|
|
|
|
|
|
|
|
public void ToggleTurn()
|
|
|
|
|
{
|
|
|
|
|
cvm.Turn(cvm.TurnStatus == TurnStatus.None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void MoveMouse(int x, int y) => cvm.MoveMouse(x, y);
|
|
|
|
|
|
|
|
|
|
public void MouseBtn(MouseButton btn, bool down) => cvm.MouseBtn(btn, down);
|
|
|
|
|
|
|
|
|
|
public bool CanToggleTurn() => true;
|
2023-08-29 08:52:35 -04:00
|
|
|
|
}
|
|
|
|
|
}
|