using System.Collections.Generic; using System.Globalization; 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 PointerPad : View { private readonly Dictionary contacts = new(10); private bool hovering; private int displayedCount; private string countLabel = "Active contacts: 0"; public override void OnPointerEvent(ref PointerEventRef e, EventPhase phase) { if (e.State.PointerType == PointerType.Mouse) { if (e.Type == PointerEventType.Enter) { hovering = true; InvalidateRender(); } if (e.Type == PointerEventType.Leave) { hovering = false; InvalidateRender(); } } if (phase != EventPhase.Bubble) return; if (e.Type == PointerEventType.Down) { contacts[e.PointerId] = e.State.Position; CapturePointer(e.PointerId, PointerGestures.Drag); } else if (e.Type == PointerEventType.Move && contacts.ContainsKey(e.PointerId)) contacts[e.PointerId] = e.State.Position; else if (e.Type is PointerEventType.Up or PointerEventType.Cancel or PointerEventType.LostCapture) { contacts.Remove(e.PointerId); ReleasePointer(e.PointerId); } UpdateCountLabel(); InvalidateRender(); } protected override void OnDeactivate() { // Release one at a time: LostCapture may synchronously remove the entry. while (contacts.Count > 0) { using var iterator = contacts.Keys.GetEnumerator(); iterator.MoveNext(); var id = iterator.Current; contacts.Remove(id); ReleasePointer(id); } hovering = false; UpdateCountLabel(); base.OnDeactivate(); } private void UpdateCountLabel() { if (contacts.Count == displayedCount) return; displayedCount = contacts.Count; countLabel = "Active contacts: " + displayedCount.ToString(CultureInfo.InvariantCulture); } protected override void RenderCore(IContext context) { context.Save(); try { context.SetFill(new Color(hovering ? 0xEEE2FAFFu : 0xFAF7FFFFu)); context.FillRect(Frame); context.SetFill(new Color(0x8538CEFF)); foreach (var contact in contacts.Values) { context.BeginPath(); context.Arc(contact, 32, 0, NFloat.Pi * 2); context.Fill(); } context.SetFont(new Font(18, "Inter")); context.TextAlign = TextAlign.Center; context.TextBaseline = TextBaseline.Middle; context.FillText(countLabel, (Frame.X + Frame.Width / 2, Frame.Y + 64)); context.SetFont(new Font(14, "Inter")); context.FillText("Press, drag, then release", (Frame.X + Frame.Width / 2, Frame.Bottom - 64)); } finally { context.Restore(); } } }