2using System.Collections.Generic;
4using System.Reflection;
7using Microsoft.Xna.Framework;
8using Microsoft.Xna.Framework.Input;
9using Microsoft.Xna.Framework.Graphics;
13 public class CUIResizeHandle : ICUIVitalizable
15 public void SetHost(CUIComponent host) => Host = host;
16 public CUIComponent Host;
19 public Vector2 Anchor;
20 public Vector2 StaticPointAnchor;
21 public Vector2 AnchorDif;
23 public CUINullRect Absolute;
25 public CUISprite Sprite;
26 public Vector2 MemoStaticPoint;
29 public bool Visible =
false;
31 public CUIBool2 Direction {
get;
set; } =
new CUIBool2(
true,
true);
33 public CUIMouseEvent Trigger = CUIMouseEvent.Down;
35 public bool ShouldStart(CUIInput input)
37 return Visible && Real.Contains(input.MousePosition) && (
38 (Trigger == CUIMouseEvent.Down && input.MouseDown) ||
39 (Trigger == CUIMouseEvent.DClick && input.DoubleClick)
43 public void BeginResize(Vector2 cursorPos)
46 MemoStaticPoint = CUIAnchor.PosIn(Host.Real, StaticPointAnchor);
49 public void EndResize()
52 Host.MainComponent?.OnResizeEnd(
this);
55 public void Resize(Vector2 cursorPos)
58 if (CUIAnchor.Direction(StaticPointAnchor).X >= 0)
60 limitedX = Math.Max(MemoStaticPoint.X + Real.Width, cursorPos.X);
64 limitedX = Math.Min(MemoStaticPoint.X - Real.Width, cursorPos.X);
67 if (CUIAnchor.Direction(StaticPointAnchor).Y >= 0)
69 limitedY = Math.Max(MemoStaticPoint.Y + Real.Height, cursorPos.Y);
73 limitedY = Math.Min(MemoStaticPoint.Y - Real.Height, cursorPos.Y);
76 Vector2 LimitedCursorPos =
new Vector2(limitedX, limitedY);
79 Vector2 RealDif = MemoStaticPoint - LimitedCursorPos;
80 Vector2 SizeFactor = RealDif / AnchorDif;
81 Vector2 TopLeft = MemoStaticPoint - SizeFactor * StaticPointAnchor;
84 Vector2 newSize =
new Vector2(
85 Math.Max(Real.Width, SizeFactor.X),
86 Math.Max(Real.Height, SizeFactor.Y)
89 Vector2 newPos = TopLeft - CUIAnchor.PosIn(Host.Parent.Real, Host.ParentAnchor ?? Host.Anchor) + CUIAnchor.PosIn(
new CUIRect(newSize), Host.Anchor);
91 if (Direction.X) Host.CUIProps.Absolute.SetValue(
new CUINullRect(newPos.X, Host.Absolute.Top, newSize.X, Host.Absolute.Height));
92 if (Direction.Y) Host.CUIProps.Absolute.SetValue(
new CUINullRect(Host.Absolute.Left, newPos.Y, Host.Absolute.Width, newSize.Y));
101 if (Absolute.Left.HasValue) x = Absolute.Left.Value;
102 if (Absolute.Top.HasValue) y = Absolute.Top.Value;
103 if (Absolute.Width.HasValue) w = Absolute.Width.Value;
104 if (Absolute.Height.HasValue) h = Absolute.Height.Value;
106 Vector2 Pos = CUIAnchor.GetChildPos(Host.Real, Anchor,
new Vector2(x, y),
new Vector2(w, h));
108 Real =
new CUIRect(Pos,
new Vector2(w, h));
110 public void Draw(SpriteBatch spriteBatch)
112 if (!Visible)
return;
113 CUI.DrawRectangle(spriteBatch, Real, Grabbed ? Host.ResizeHandleGrabbedColor : Host.ResizeHandleColor, Sprite);
116 public CUIResizeHandle(Vector2 anchor, CUIBool2 flipped)
118 if (anchor == CUIAnchor.Center)
120 CUI.Log($
"Pls don't use CUIAnchor.Center for CUIResizeHandle, it makes no sense:\nThe StaticPointAnchor is symetric to Anchor and in this edge case == Anchor");
124 StaticPointAnchor = Vector2.One - Anchor;
125 AnchorDif = StaticPointAnchor - Anchor;
127 Absolute =
new CUINullRect(0, 0, 15, 15);
128 Sprite = CUI.TextureManager.GetSprite(CUI.CUITexturePath);
129 Sprite.SourceRect =
new Rectangle(0, 32, 32, 32);
130 if (flipped.X) Sprite.Effects |= SpriteEffects.FlipHorizontally;
131 if (flipped.Y) Sprite.Effects |= SpriteEffects.FlipVertically;