Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
GuiInteractableItemButton.cs
Go to the documentation of this file.
1 using System;
2 using Microsoft.Xna.Framework;
3 using Microsoft.Xna.Framework.Graphics;
4 using Terraria;
5 using Terraria.ID;
6 using Terraria.UI;
7 
8 namespace Loot.UI.Common.Controls.Button
9 {
10  internal class GuiInteractableItemButton : GuiItemButton
11  {
12  protected bool RightClickFunctionalityEnabled = true;
13  protected bool TakeUserItemOnClick = true;
14  public Action<Item> OnItemChange;
15 
16  public bool PerformRegularClickInteraction { get; protected internal set; } = true;
17 
18  internal GuiInteractableItemButton(ButtonType buttonType, int netId = 0, int stack = 0, Texture2D hintTexture = null, string hintText = null, string hintOnHover = null) : base(buttonType, netId, stack, hintTexture, hintText, hintOnHover)
19  {
20  OnClick += HandleClickAction;
21  }
22 
23  public override void Update(GameTime gameTime)
24  {
25  base.Update(gameTime);
26  if (!RightClickFunctionalityEnabled || !IsMouseHovering || !Main.mouseRight)
27  {
28  return;
29  }
30 
31  // Slot has an item
32  if (!Item.IsAir)
33  {
34  // Open inventory
35  Main.playerInventory = true;
36 
37  // Mouseitem has to be the same as slot
38  if (Main.stackSplit <= 1 &&
39  (Main.mouseItem.type == Item.type
40  || Main.mouseItem.IsAir))
41  {
42  int num2 = Main.superFastStack + 1;
43  for (int j = 0; j < num2; j++)
44  {
45  // Mouseitem is air, or stack is smaller than maxstack, and slot has stack
46  if (!Main.mouseItem.IsAir && ((Main.mouseItem.stack >= Main.mouseItem.maxStack) || Item.stack <= 0))
47  {
48  continue;
49  }
50 
51  // Play sound
52  if (j == 0)
53  {
54  Main.PlaySound(18, -1, -1, 1);
55  }
56 
57  // Mouseitem is air, clone item
58  if (Main.mouseItem.IsAir)
59  {
60  Main.mouseItem = Item.Clone();
61  // If it has prefix, copy it
62  if (Item.prefix != 0)
63  {
64  Main.mouseItem.Prefix((int)Item.prefix);
65  }
66 
67  Main.mouseItem.stack = 0;
68  }
69 
70  // Add to mouseitem stack
71  Main.mouseItem.stack++;
72  // Take from slot stack
73  Item.stack--;
74  Main.stackSplit = Main.stackSplit == 0 ? 15 : Main.stackDelay;
75 
76  // Reset item
77  if (Item.stack <= 0)
78  {
79  ChangeItem(0);
80  }
81  }
82  }
83  }
84 
85  PostOnRightClick();
86  }
87 
88  public virtual void ChangeItem(int type, Item item = null)
89  {
90  if (item != null)
91  {
92  Item = item;
93  }
94  else if (type == 0)
95  {
96  Item.TurnToAir();
97  }
98  else
99  {
100  Item.SetDefaults(type);
101  }
102 
103  OnItemChange?.Invoke(Item);
104  }
105 
106  public virtual bool CanTakeItem(Item givenItem) => !givenItem.IsAir;
107 
108  public virtual void PostOnRightClick()
109  {
110  }
111 
112  public virtual void PreOnClick(UIMouseEvent evt, UIElement e)
113  {
114  }
115 
116  public virtual void PostOnClick(UIMouseEvent evt, UIElement e)
117  {
118  }
119 
120  private void HandleClickAction(UIMouseEvent evt, UIElement e)
121  {
122  PreOnClick(evt, e);
123 
124  if (PerformRegularClickInteraction)
125  {
126  // Slot has an item
127  if (!Item.IsAir)
128  {
129  // Only slot has an item
130  if (Main.mouseItem.IsAir)
131  {
132  Main.PlaySound(SoundID.Grab);
133  Main.playerInventory = true;
134  if (TakeUserItemOnClick)
135  {
136  Main.mouseItem = Item.Clone();
137  }
138 
139  ChangeItem(0);
140  }
141  // Mouse has an item
142  // Can take mouse item
143  else if (CanTakeItem(Main.mouseItem))
144  {
145  Main.PlaySound(SoundID.Grab);
146  Main.playerInventory = true;
147  // Items are the same type
148  if (Item.type == Main.mouseItem.type)
149  {
150  // Attempt increment stack
151  var newStack = Item.stack + Main.mouseItem.stack;
152  // Mouse item stack fits, increment
153  if (Item.maxStack >= newStack)
154  {
155  Item.stack = newStack;
156  if (TakeUserItemOnClick)
157  {
158  Main.mouseItem.TurnToAir();
159  }
160  }
161  // Doesn't fit, set item to maxstack, set mouse item stack to difference
162  else
163  {
164  var stackDiff = newStack - Item.maxStack;
165  Item.stack = Item.maxStack;
166  if (TakeUserItemOnClick)
167  {
168  Main.mouseItem.stack = stackDiff;
169  }
170  }
171  }
172  // Items are not the same type
173  else
174  {
175  // Swap mouse item and slot item
176  var tmp = Item.Clone();
177  var tmp2 = Main.mouseItem.Clone();
178  if (TakeUserItemOnClick)
179  {
180  Main.mouseItem = tmp;
181  }
182 
183  ChangeItem(0, tmp2);
184  }
185  }
186  }
187  // Slot has no item
188  // Slot can take mouse item
189  else if (CanTakeItem(Main.mouseItem))
190  {
191  Main.PlaySound(SoundID.Grab);
192  Main.playerInventory = true;
193  ChangeItem(0, Main.mouseItem.Clone());
194  if (TakeUserItemOnClick)
195  {
196  Main.mouseItem.TurnToAir();
197  }
198  }
199  }
200 
201  PostOnClick(evt, e);
202  }
203  }
204 }