CrabUI
Loading...
Searching...
No Matches
CUIResizeHandle.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5
6using Barotrauma;
7using Microsoft.Xna.Framework;
8using Microsoft.Xna.Framework.Input;
9using Microsoft.Xna.Framework.Graphics;
10
11namespace CrabUI
12{
13 public class CUIResizeHandle : ICUIVitalizable
14 {
15 public void SetHost(CUIComponent host) => Host = host;
16 public CUIComponent Host;
17 public CUIRect Real;
18
19 public Vector2 Anchor;
20 public Vector2 StaticPointAnchor;
21 public Vector2 AnchorDif;
22
23 public CUINullRect Absolute;
24
25 public CUISprite Sprite;
26 public Vector2 MemoStaticPoint;
27
28 public bool Grabbed;
29 public bool Visible = false;
30
31 public CUIBool2 Direction { get; set; } = new CUIBool2(true, true);
32
33 public CUIMouseEvent Trigger = CUIMouseEvent.Down;
34
35 public bool ShouldStart(CUIInput input)
36 {
37 return Visible && Real.Contains(input.MousePosition) && (
38 (Trigger == CUIMouseEvent.Down && input.MouseDown) ||
39 (Trigger == CUIMouseEvent.DClick && input.DoubleClick)
40 );
41 }
42
43 public void BeginResize(Vector2 cursorPos)
44 {
45 Grabbed = true;
46 MemoStaticPoint = CUIAnchor.PosIn(Host.Real, StaticPointAnchor);
47 }
48
49 public void EndResize()
50 {
51 Grabbed = false;
52 Host.MainComponent?.OnResizeEnd(this);
53 }
54
55 public void Resize(Vector2 cursorPos)
56 {
57 float limitedX;
58 if (CUIAnchor.Direction(StaticPointAnchor).X >= 0)
59 {
60 limitedX = Math.Max(MemoStaticPoint.X + Real.Width, cursorPos.X);
61 }
62 else
63 {
64 limitedX = Math.Min(MemoStaticPoint.X - Real.Width, cursorPos.X);
65 }
66 float limitedY;
67 if (CUIAnchor.Direction(StaticPointAnchor).Y >= 0)
68 {
69 limitedY = Math.Max(MemoStaticPoint.Y + Real.Height, cursorPos.Y);
70 }
71 else
72 {
73 limitedY = Math.Min(MemoStaticPoint.Y - Real.Height, cursorPos.Y);
74 }
75
76 Vector2 LimitedCursorPos = new Vector2(limitedX, limitedY);
77
78
79 Vector2 RealDif = MemoStaticPoint - LimitedCursorPos;
80 Vector2 SizeFactor = RealDif / AnchorDif;
81 Vector2 TopLeft = MemoStaticPoint - SizeFactor * StaticPointAnchor;
82
83
84 Vector2 newSize = new Vector2(
85 Math.Max(Real.Width, SizeFactor.X),
86 Math.Max(Real.Height, SizeFactor.Y)
87 );
88
89 Vector2 newPos = TopLeft - CUIAnchor.PosIn(Host.Parent.Real, Host.ParentAnchor ?? Host.Anchor) + CUIAnchor.PosIn(new CUIRect(newSize), Host.Anchor);
90
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));
93 }
94 public void Update()
95 {
96 if (!Visible) return;
97
98 float x, y, w, h;
99 x = y = w = h = 0;
100
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;
105
106 Vector2 Pos = CUIAnchor.GetChildPos(Host.Real, Anchor, new Vector2(x, y), new Vector2(w, h));
107
108 Real = new CUIRect(Pos, new Vector2(w, h));
109 }
110 public void Draw(SpriteBatch spriteBatch)
111 {
112 if (!Visible) return;
113 CUI.DrawRectangle(spriteBatch, Real, Grabbed ? Host.ResizeHandleGrabbedColor : Host.ResizeHandleColor, Sprite);
114 }
115
116 public CUIResizeHandle(Vector2 anchor, CUIBool2 flipped)
117 {
118 if (anchor == CUIAnchor.Center)
119 {
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");
121 }
122
123 Anchor = anchor;
124 StaticPointAnchor = Vector2.One - Anchor;
125 AnchorDif = StaticPointAnchor - Anchor;
126
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;
132 }
133 }
134}