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 DragPad : View { private int? pointer; private NFloat fraction = 0.5f; private int displayedPercent = 50; private string valueLabel = "50%"; 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) { pointer = e.PointerId; CapturePointer(e.PointerId, PointerGestures.Drag); } if (pointer != e.PointerId) return; if (e.Type is PointerEventType.Down or PointerEventType.Move or PointerEventType.Up) { // Routing already maps this point through ancestor transforms. fraction = NFloat.Clamp((e.State.Position.X - Frame.X - 32) / NFloat.Max(1, Frame.Width - 64), 0, 1); var percent = (int)(fraction * 100); if (percent != displayedPercent) { displayedPercent = percent; valueLabel = percent.ToString(CultureInfo.InvariantCulture) + "%"; } InvalidateRender(); } if (e.Type is PointerEventType.Up or PointerEventType.Cancel or PointerEventType.LostCapture) { pointer = null; ReleasePointer(e.PointerId); InvalidateRender(); } } protected override void OnDeactivate() { var old = pointer; pointer = null; if (old is { } id) ReleasePointer(id); base.OnDeactivate(); } protected override void RenderCore(IContext context) { context.Save(); try { context.SetFill(new Color(0xFAF7FFFF)); context.FillRect(Frame); var y = Frame.Y + Frame.Height / 2; var x = Frame.X + 32 + fraction * (Frame.Width - 64); context.SetFill(new Color(0xD9B6F4FF)); context.FillRect(new Rect(Frame.X + 32, y - 4, Frame.Width - 64, 8)); context.SetFill(new Color(pointer is null ? 0x8538CEFFu : 0xD957A8FFu)); context.BeginPath(); context.Arc((x, y), 24, 0, NFloat.Pi * 2); context.Fill(); context.SetFont(new Font(18, "Inter")); context.TextAlign = TextAlign.Center; context.TextBaseline = TextBaseline.Middle; context.FillText(valueLabel, (Frame.X + Frame.Width / 2, y - 64)); context.SetFont(new Font(14, "Inter")); context.FillText("Drag across the track", (Frame.X + Frame.Width / 2, y + 72)); } finally { context.Restore(); } } }