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; using CollabVMSharp; namespace CollabVMClient.ViewModels { internal partial class CollabVMScreenViewModel : ViewModelBase { private CollabVMSharp.CollabVMClient cvm; public WriteableBitmap Framebuffer { get; set; } [ObservableProperty] private string id; [ObservableProperty] private int width; [ObservableProperty] private int height; [ObservableProperty] private int windowHeight; [ObservableProperty] private string toggleTurnBtnText = "Take Turn"; public string Title => $"CollabVM - Screen ({id})"; public CollabVMScreenViewModel(CollabVMSharp.CollabVMClient cvm, string id) { this.cvm = cvm; cvm.TurnUpdate += (_, _) => { if (cvm.TurnStatus == TurnStatus.None) ToggleTurnBtnText = "Take Turn"; else ToggleTurnBtnText = "End Turn"; }; 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; this.WindowHeight = height + 34; 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(); } 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; } }