2using System.Collections.Generic;
4using System.Reflection;
7using Microsoft.Xna.Framework;
8using Microsoft.Xna.Framework.Input;
9using Microsoft.Xna.Framework.Graphics;
13 public class CUIDragHandle : ICUIVitalizable
15 public void SetHost(CUIComponent host) => Host = host;
16 public CUIComponent Host;
17 public Vector2 GrabOffset;
19 public bool Draggable;
20 public CUIMouseEvent Trigger = CUIMouseEvent.Down;
24 public bool DragRelative {
get;
set; } =
false;
25 public bool OutputRealPos {
get;
set; } =
false;
27 public bool ShouldStart(CUIInput input)
30 (Trigger == CUIMouseEvent.Down && input.MouseDown) ||
31 (Trigger == CUIMouseEvent.DClick && input.DoubleClick)
34 public void BeginDrag(Vector2 cursorPos)
37 GrabOffset = cursorPos - CUIAnchor.PosIn(Host);
43 Host.MainComponent?.OnDragEnd(
this);
47 public void DragTo(Vector2 to)
49 Vector2 pos = Host.Parent.ChildrenOffset.ToPlaneCoords(
50 to - GrabOffset - CUIAnchor.PosIn(Host.Parent.Real, Host.ParentAnchor ?? Host.Anchor)
55 Vector2 newRelPos =
new Vector2(
56 pos.X / Host.Parent.Real.Width,
57 pos.Y / Host.Parent.Real.Height
59 Host.CUIProps.Relative.SetValue(Host.Relative with { Position = newRelPos });
60 Host.InvokeOnDrag(newRelPos.X, newRelPos.Y);
64 Host.CUIProps.Absolute.SetValue(Host.Absolute with { Position = pos });
65 if (OutputRealPos) Host.InvokeOnDrag(to.X, to.Y);
66 else Host.InvokeOnDrag(pos.X, pos.Y);
70 public CUIDragHandle() { }
71 public CUIDragHandle(CUIComponent host) => Host = host;