Client LuaCsForBarotrauma
GUIDragHandle.cs
1 using Microsoft.Xna.Framework;
2 using System;
3 
4 namespace Barotrauma
5 {
6  public class GUIDragHandle : GUIComponent
7  {
8  private readonly RectTransform elementToMove;
9 
10  private Point originalOffset;
11 
12  private Vector2 dragStart;
13  private bool dragStarted;
14 
16 
17  public Func<RectTransform, bool> ValidatePosition;
18 
19  public bool Dragging => dragStarted;
20 
21  public GUIDragHandle(RectTransform rectT, RectTransform elementToMove, string style = "GUIDragIndicator")
22  : base(style, rectT)
23  {
24  enabled = true;
25  this.elementToMove = elementToMove;
27  }
28 
29  protected override void Update(float deltaTime)
30  {
31  if (!Visible) { return; }
32  base.Update(deltaTime);
33 
34  if (enabled)
35  {
36  if (dragStarted)
37  {
38  Point moveAmount = (PlayerInput.MousePosition - dragStart).ToPoint() - elementToMove.ScreenSpaceOffset;
39  Rectangle rect = elementToMove.Rect;
40  rect.Location += moveAmount;
41 
42  moveAmount.X += Math.Max(DragArea.X - rect.X, 0);
43  moveAmount.X -= Math.Max(rect.Right - DragArea.Right, 0);
44  moveAmount.Y += Math.Max(DragArea.Y - rect.Y, 0);
45  moveAmount.Y -= Math.Max(rect.Bottom - DragArea.Bottom, 0);
46 
47  if (moveAmount != Point.Zero)
48  {
49  elementToMove.ScreenSpaceOffset += moveAmount;
50  }
51 
52  bool isPositionValid = ValidatePosition == null || ValidatePosition.Invoke(elementToMove);
53 
55  {
56  if (!isPositionValid)
57  {
58  elementToMove.ScreenSpaceOffset = originalOffset;
59  elementToMove.GUIComponent?.Flash();
60  SoundPlayer.PlayUISound(GUISoundType.PickItemFail);
61  }
62  dragStarted = false;
63  }
64  }
65  else if (Rect.Contains(PlayerInput.MousePosition) && CanBeFocused && Enabled && GUI.IsMouseOn(this) && !(GUI.MouseOn is GUIButton))
66  {
67  State = Selected ? ComponentState.HoverSelected : ComponentState.Hover;
69  {
70  originalOffset = elementToMove.ScreenSpaceOffset;
71  dragStart = PlayerInput.MousePosition - elementToMove.ScreenSpaceOffset.ToVector2();
72  dragStarted = true;
73  }
74  }
75  else
76  {
77  if (!ExternalHighlight)
78  {
79  State = Selected ? ComponentState.Selected : ComponentState.None;
80  }
81  else
82  {
83  State = ComponentState.Hover;
84  }
85  }
86  }
87 
88  foreach (GUIComponent child in Children)
89  {
90  //allow buttons to handle their states themselves
91  if (child is GUIButton) { continue; }
92  child.State = State;
93  child.Enabled = enabled;
94  }
95  }
96  }
97 }
virtual ComponentState State
virtual Rectangle Rect
IEnumerable< GUIComponent > Children
Definition: GUIComponent.cs:29
override void Update(float deltaTime)
GUIDragHandle(RectTransform rectT, RectTransform elementToMove, string style="GUIDragIndicator")
Func< RectTransform, bool > ValidatePosition
static int GraphicsWidth
Definition: GameMain.cs:162
static int GraphicsHeight
Definition: GameMain.cs:168
GUISoundType
Definition: GUI.cs:21