1 using Microsoft.Xna.Framework;
2 using Microsoft.Xna.Framework.Graphics;
7 namespace Loot.UI.Common.Controls.Button
9 internal class GuiItemButton : GuiButton
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;
20 public bool DynamicScaling
23 protected internal set;
25 public Color? DrawColor
28 protected internal set;
34 protected internal set;
37 internal GuiItemButton(ButtonType buttonType,
38 int netId = 0,
int stack = 0, Texture2D hintTexture = null,
39 string hintText = null,
string hintOnHover = null) : base(buttonType)
42 Item.netDefaults(netId);
44 HintTexture = hintTexture;
46 HintOnHover = hintOnHover;
49 protected override void DrawSelf(SpriteBatch spriteBatch)
53 base.DrawSelf(spriteBatch);
57 CalculatedStyle innerDimensions = GetInnerDimensions();
60 if (HintTexture != null && Item.IsAir)
62 texture2D = HintTexture;
63 drawColor = Color.LightGray * 0.5f;
66 Main.hoverItemName = HintText ??
string.Empty;
75 texture2D = Main.itemTexture[Item.type];
76 drawColor = DrawColor ?? Item.GetAlpha(Color.White);
79 if (ShowOnlyHintOnHover)
81 Main.hoverItemName = HintOnHover;
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 ?? ""}");
94 !Item.IsAir && Main.itemAnimations[Item.type] != null
95 ? Main.itemAnimations[Item.type].GetFrame(texture2D)
98 float drawScale = DrawScale;
101 if (frame.Width > innerDimensions.Width
102 || frame.Height > innerDimensions.Width)
104 if (frame.Width > frame.Height)
106 drawScale = innerDimensions.Width / frame.Width;
110 drawScale = innerDimensions.Width / frame.Height;
115 var unreflectedScale = drawScale;
122 Vector2 drawPosition =
new Vector2(innerDimensions.X, innerDimensions.Y);
124 drawPosition.X += innerDimensions.Width * 1f / 2f - frame.Width * drawScale / 2f;
125 drawPosition.Y += innerDimensions.Height * 1f / 2f - frame.Height * drawScale / 2f;
128 if (Item.modItem == null
129 || Item.modItem.PreDrawInInventory(spriteBatch, drawPosition, frame, drawColor, drawColor, Vector2.Zero, drawScale))
131 spriteBatch.Draw(texture2D, drawPosition, frame, drawColor, 0f,
132 Vector2.Zero, drawScale, SpriteEffects.None, 0f);
134 if (Item?.color !=
default(Color))
136 spriteBatch.Draw(texture2D, drawPosition, frame, drawColor, 0f,
137 Vector2.Zero, drawScale, SpriteEffects.None, 0f);
141 Item.modItem?.PostDrawInInventory(spriteBatch, drawPosition, frame, drawColor, drawColor, Vector2.Zero, drawScale);
143 if (!DrawStack || !(Item?.stack >= ShowStackFrom))
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);
152 Utils.DrawBorderStringFourWay(
156 corner.X - stackMeasuring.X,
157 corner.Y - stackMeasuring.Y,
161 unreflectedScale * 0.8f);