using Xui.Core.Abstract.Events; using Xui.Core.Canvas; using Xui.Core.Math2D; using Xui.Core.UI; using Xui.Core.UI.Input; using NFloat = System.Runtime.InteropServices.NFloat; public sealed class DrawingSurface : View { private Rect card = new(70, 170, 170, 120); private Vector pan; private NFloat zoom = 1; private int? pointer; private Point previous; private bool movingCard; private Point ToWorld(Point point) => new((point.X - Frame.X - pan.X) / zoom, (point.Y - Frame.Y - pan.Y) / zoom); public override void OnPointerEvent(ref PointerEventRef e, EventPhase phase) { if (phase != EventPhase.Bubble) return; if (e.Type == PointerEventType.Down && pointer is null && e.State.Button == PointerButton.Left) { previous = e.State.Position; movingCard = card.Contains(ToWorld(previous)); pointer = e.PointerId; CapturePointer(e.PointerId, PointerGestures.Drag); InvalidateRender(); } else if (e.PointerId == pointer && e.Type == PointerEventType.Move) { var delta = e.State.Position - previous; previous = e.State.Position; if (movingCard) card = new Rect(card.X + delta.X / zoom, card.Y + delta.Y / zoom, card.Width, card.Height); else pan += delta; InvalidateRender(); } else if (e.PointerId == pointer && e.Type is PointerEventType.Up or PointerEventType.Cancel or PointerEventType.LostCapture) StopDrag(); } public override void OnZoom(ref ZoomEventRef e) { if (!e.HasPosition || !NFloat.IsFinite(e.ScaleFactor) || e.ScaleFactor <= 0) return; // ZoomEventRef.Position stays in window coordinates; pointer positions are routed. var anchor = GlobalToLocal(e.Position); var world = ToWorld(anchor); zoom = NFloat.Clamp(zoom * e.ScaleFactor, (NFloat)0.5, 3); pan = new Vector(anchor.X - Frame.X - world.X * zoom, anchor.Y - Frame.Y - world.Y * zoom); e.Handled = true; InvalidateRender(); } private void StopDrag() { int? captured = pointer; pointer = null; if (captured is int id) ReleasePointer(id); InvalidateRender(); } protected override void OnDeactivate() => StopDrag(); protected override void RenderCore(IContext context) { context.SetFill(new Color(0xFAF7FFFF)); context.FillRect(Frame); context.Save(); try { context.BeginPath(); context.Rect(Frame); context.Clip(); context.Translate((Frame.X + pan.X, Frame.Y + pan.Y)); context.Scale((zoom, zoom)); context.SetFill(new Color(0xEADCF5FF)); for (int x = 0; x <= 600; x += 40) for (int y = 0; y <= 800; y += 40) context.FillRect(new Rect(x, y, 3, 3)); context.SetFill(new Color(pointer is not null && movingCard ? 0xD957A8FFu : 0x8538CEFFu)); context.BeginPath(); context.RoundRect(card, 16); context.Fill(); context.SetFill(Colors.White); context.SetFont(new Font(20, "Inter")); context.TextAlign = TextAlign.Center; context.TextBaseline = TextBaseline.Middle; context.FillText("Drag me", card.Center); } finally { context.Restore(); } context.SetFill(new Color(0x492571FF)); context.SetFont(new Font(16, "Inter")); context.TextAlign = TextAlign.Center; context.FillText("Drag the card or pan the background", (Frame.Center.X, Frame.Y + 40)); } }