Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
GuiItemButton.cs
Go to the documentation of this file.
1 using Microsoft.Xna.Framework;
2 using Microsoft.Xna.Framework.Graphics;
3 using System;
4 using Terraria;
5 using Terraria.UI;
6 
7 namespace Loot.UI.Common.Controls.Button
8 {
9  internal class GuiItemButton : GuiButton
10  {
11  public string HintText { get; protected internal set; }
12  public Texture2D HintTexture { get; protected internal set; }
13  public string HintOnHover { get; protected internal set; }
14  public bool DrawBackground { get; protected internal set; } = true;
15  public bool DrawStack { get; protected internal set; } = true;
16  public bool ShowOnlyHintOnHover { get; protected internal set; } = false;
17  public float DrawScale { get; protected internal set; } = 1f;
18  public int ShowStackFrom { get; protected internal set; } = 2;
19 
20  public bool DynamicScaling
21  {
22  get;
23  protected internal set;
24  } = true;
25  public Color? DrawColor
26  {
27  get;
28  protected internal set;
29  } = null;
30 
31  public Item Item
32  {
33  get;
34  protected internal set;
35  }
36 
37  internal GuiItemButton(ButtonType buttonType,
38  int netId = 0, int stack = 0, Texture2D hintTexture = null,
39  string hintText = null, string hintOnHover = null) : base(buttonType)
40  {
41  Item = new Item();
42  Item.netDefaults(netId);
43  Item.stack = stack;
44  HintTexture = hintTexture;
45  HintText = hintText;
46  HintOnHover = hintOnHover;
47  }
48 
49  protected override void DrawSelf(SpriteBatch spriteBatch)
50  {
51  if (DrawBackground)
52  {
53  base.DrawSelf(spriteBatch);
54  }
55 
56  Texture2D texture2D;
57  CalculatedStyle innerDimensions = GetInnerDimensions();
58  Color drawColor;
59 
60  if (HintTexture != null && Item.IsAir)
61  {
62  texture2D = HintTexture;
63  drawColor = Color.LightGray * 0.5f;
64  if (IsMouseHovering)
65  {
66  Main.hoverItemName = HintText ?? string.Empty;
67  }
68  }
69  else if (Item.IsAir)
70  {
71  return;
72  }
73  else
74  {
75  texture2D = Main.itemTexture[Item.type];
76  drawColor = DrawColor ?? Item.GetAlpha(Color.White);
77  if (IsMouseHovering)
78  {
79  if (ShowOnlyHintOnHover)
80  {
81  Main.hoverItemName = HintOnHover;
82  }
83  else
84  {
85  Main.hoverItemName = Item.Name;
86  Main.HoverItem = Item.Clone();
87  Main.HoverItem.SetNameOverride(
88  $"{Main.HoverItem.Name}{Main.HoverItem.modItem?.mod.Name.Insert((int)Main.HoverItem.modItem?.mod.Name.Length, "]").Insert(0, " [")}{HintOnHover ?? ""}");
89  }
90  }
91  }
92 
93  var frame =
94  !Item.IsAir && Main.itemAnimations[Item.type] != null
95  ? Main.itemAnimations[Item.type].GetFrame(texture2D)
96  : texture2D.Frame();
97 
98  float drawScale = DrawScale;
99  if (DynamicScaling)
100  {
101  if (frame.Width > innerDimensions.Width
102  || frame.Height > innerDimensions.Width)
103  {
104  if (frame.Width > frame.Height)
105  {
106  drawScale = innerDimensions.Width / frame.Width;
107  }
108  else
109  {
110  drawScale = innerDimensions.Width / frame.Height;
111  }
112  }
113  }
114 
115  var unreflectedScale = drawScale;
116 
117  // TODO why was this here?
118  //var tmpcolor = Color.White;
120  //ItemSlot.GetItemLight(ref tmpcolor, ref drawScale, item.type);
121 
122  Vector2 drawPosition = new Vector2(innerDimensions.X, innerDimensions.Y);
123 
124  drawPosition.X += innerDimensions.Width * 1f / 2f - frame.Width * drawScale / 2f;
125  drawPosition.Y += innerDimensions.Height * 1f / 2f - frame.Height * drawScale / 2f;
126 
127  // TODO globalitem Pre and Post draws?
128  if (Item.modItem == null
129  || Item.modItem.PreDrawInInventory(spriteBatch, drawPosition, frame, drawColor, drawColor, Vector2.Zero, drawScale))
130  {
131  spriteBatch.Draw(texture2D, drawPosition, frame, drawColor, 0f,
132  Vector2.Zero, drawScale, SpriteEffects.None, 0f);
133 
134  if (Item?.color != default(Color))
135  {
136  spriteBatch.Draw(texture2D, drawPosition, frame, drawColor, 0f,
137  Vector2.Zero, drawScale, SpriteEffects.None, 0f);
138  }
139  }
140 
141  Item.modItem?.PostDrawInInventory(spriteBatch, drawPosition, frame, drawColor, drawColor, Vector2.Zero, drawScale);
142 
143  if (!DrawStack || !(Item?.stack >= ShowStackFrom))
144  {
145  return;
146  }
147 
148  string stack = Math.Min(9999, Item.stack).ToString();
149  Vector2 stackMeasuring = Main.fontItemStack.MeasureString(stack);
150  var corner = innerDimensions.Center() + new Vector2(WIDTH / 2f, HEIGHT / 2f);
151 
152  Utils.DrawBorderStringFourWay(
153  spriteBatch,
154  Main.fontItemStack,
155  stack,
156  corner.X - stackMeasuring.X,
157  corner.Y - stackMeasuring.Y,
158  Color.White,
159  Color.Black,
160  Vector2.Zero,
161  unreflectedScale * 0.8f);
162  }
163  }
164 }