Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
DraggableUIState.cs
Go to the documentation of this file.
1 using Microsoft.Xna.Framework;
2 using Microsoft.Xna.Framework.Graphics;
3 using System.Collections.Generic;
4 using Terraria;
5 using Terraria.UI;
6 
7 namespace Loot.UI.Common.Core
8 {
12  internal abstract class DraggableUIState : VisibilityUI
13  {
14  private int _dragging;
15 
16  private readonly List<(UIElement, UIElement)> _draggingList = new List<(UIElement, UIElement)>();
17  private readonly Dictionary<UIElement, Vector2> _offsets = new Dictionary<UIElement, Vector2>();
18 
19  protected void AddDragging(UIElement dragger, UIElement draggable = null)
20  {
21  draggable = draggable ?? dragger;
22  _draggingList.Add((dragger, draggable));
23  if (!_offsets.ContainsKey(dragger))
24  {
25  _offsets.Add(dragger, Vector2.Zero);
26  }
27 
28  dragger.OnMouseDown += (evt, element) =>
29  {
30  //start
31  _offsets[dragger] = new Vector2(evt.MousePosition.X - draggable.Left.Pixels, evt.MousePosition.Y - draggable.Top.Pixels);
32  _dragging++;
33  };
34 
35  dragger.OnMouseUp += (evt, element) =>
36  {
37  //end
38  Vector2 end = evt.MousePosition;
39  _dragging--;
40 
41  draggable.Left.Set(end.X - _offsets[dragger].X, 0f);
42  draggable.Top.Set(end.Y - _offsets[dragger].Y, 0f);
43 
44  dragger.Recalculate();
45  draggable.Recalculate();
46  };
47  }
48 
49  protected override void DrawSelf(SpriteBatch spriteBatch)
50  {
51  Main.LocalPlayer.mouseInterface = IsMouseHovering;
52 
53  if (_dragging <= 0)
54  {
55  return;
56  }
57 
58  foreach (var (dragger, draggable) in _draggingList)
59  {
60  draggable.Left.Set(Main.MouseScreen.X - _offsets[dragger].X, 0f);
61  draggable.Top.Set(Main.MouseScreen.Y - _offsets[dragger].Y, 0f);
62 
63  dragger.Recalculate();
64  draggable.Recalculate();
65  }
66  }
67  }
68 }