// Chessboard.cs using System; using Godot; public partial class ChessBoard : Node2D { public event EventHandler OnMouseClicked; public event EventHandler OnPosClicked; public override void _Ready() { } public void LoadBoard(VirtualBoard board) { board.OnRemove += (sender, piece) => { if (piece.data != null) { RemoveChild(piece.data as Node); } }; board.OnInsert += (sender, piece) => { if (piece.data != null && piece.data is Node) { AddChild(piece.data as Node); } else { ChessPiece chessPiece = new(piece.name, piece); if (piece.Pos().Y >= 5) { chessPiece.LabelColor = new Color("red"); } else { chessPiece.LabelColor = new Color("black"); } AddChild(chessPiece); } }; } public override void _Input(InputEvent @event) { if (@event is InputEventMouseButton mouseEvent && mouseEvent.Pressed && mouseEvent.ButtonIndex == MouseButton.Left) { OnMouseClicked?.Invoke(this, GetLocalMousePosition()); OnPosClicked?.Invoke(this, (PosTrans.transArrToPix.AffineInverse() * GetLocalMousePosition()).Round()); } } }