Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
GuiArrowButton.cs
Go to the documentation of this file.
1 using Microsoft.Xna.Framework;
2 using Microsoft.Xna.Framework.Graphics;
3 using Terraria;
4 using Terraria.UI;
5 
6 namespace Loot.UI.Common.Controls.Button
7 {
8  internal class GuiArrowButton : UIElement
9  {
10  public enum ArrowDirection
11  {
12  LEFT,
13  RIGHT,
14  DEFAULT = RIGHT
15  }
16 
17  private static Texture2D _btnTexture => Assets.Textures.GUI.ArrowButtonTexture;
18  public bool CanBeClicked;
19  public string HoverText;
20  private readonly ArrowDirection _arrowDirection;
21 
22  public delegate void WhenClickedEvent(UIMouseEvent evt, UIElement listeningElement, GuiArrowButton btn);
23  public event WhenClickedEvent WhenClicked;
24 
25  private SpriteEffects GetSpriteEffects()
26  {
27  switch (_arrowDirection)
28  {
29  default:
30  case ArrowDirection.RIGHT:
31  return SpriteEffects.None;
32  case ArrowDirection.LEFT:
33  return SpriteEffects.FlipHorizontally;
34  }
35  }
36 
37  public GuiArrowButton(ArrowDirection direction)
38  {
39  _arrowDirection = direction;
40  Width.Set(_btnTexture.Width, 0);
41  Height.Set(_btnTexture.Height, 0);
42  }
43 
44  public override void OnInitialize()
45  {
46  OnClick += (evt, element) =>
47  {
48  if (CanBeClicked)
49  {
50  WhenClicked?.Invoke(evt, element, this);
51  }
52  };
53  }
54 
55  protected override void DrawSelf(SpriteBatch spriteBatch)
56  {
57  Color drawColor = Color.Gray * 0.75f;
58 
59  var parentBounds = GetOuterDimensions().ToRectangle();
60 
61  if (parentBounds.Contains(Main.MouseScreen.ToPoint()))
62  {
63  if (CanBeClicked)
64  {
65  drawColor = Color.White;
66  Main.hoverItemName = HoverText;
67  // This is needed because the current arrows are outside
68  // the UI frame causing vanilla mouse behavior to not register
69  if (Main.mouseLeft && Main.mouseLeftRelease)
70  {
71  Click(new UIMouseEvent(this, Main.MouseScreen));
72  }
73  }
74  }
75 
76  spriteBatch.Draw(
77  _btnTexture,
78  parentBounds,
79  null, drawColor, 0f, Vector2.Zero,
80  GetSpriteEffects(), 0f
81  );
82  }
83  }
84 }