Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
GuiTabToggle.cs
Go to the documentation of this file.
1 using Loot.UI.Common;
2 using Microsoft.Xna.Framework;
3 using Microsoft.Xna.Framework.Graphics;
4 using Terraria.GameContent.UI.Elements;
5 using Terraria.UI;
6 
7 namespace Loot.UI
8 {
9  internal class GuiTabToggle : UIElement
10  {
11  public readonly GuiTabState TargetState;
12 
13  public delegate void WhenClickedEvent(UIMouseEvent evt, UIElement listeningElement, GuiTabToggle toggle);
14  public event WhenClickedEvent WhenClicked;
15 
16  private UIImageButton _button;
17  private Texture2D _texture;
18 
19  public new float VisibilityActive { get; private set; } = 1f;
20  public new float VisibilityInactive { get; private set; } = 0.4f;
21  public float VisiblityMultiplier => IsActive ? VisibilityActive : VisibilityInactive;
22  public bool IsActive { get; private set; }
23 
24  public GuiTabToggle(GuiTabState state)
25  {
26  TargetState = state;
27  _texture = GetTexture();
28  OnClick += (evt, element) =>
29  {
30  WhenClicked?.Invoke(evt, element, this);
31  };
32  }
33 
34  public override void OnInitialize()
35  {
36  Height.Set(_texture.Height, 0);
37  Width.Set(_texture.Width, 0);
38  _button = new UIImageButton(_texture);
39  Append(_button);
40  }
41 
42  protected override void DrawSelf(SpriteBatch spriteBatch)
43  {
44  CalculatedStyle dimensions = GetDimensions();
45  spriteBatch.Draw(_texture, dimensions.Position(), Color.White * VisiblityMultiplier);
46  }
47 
48  private Texture2D GetTexture()
49  {
50  switch (TargetState)
51  {
52  default:
53  case GuiTabState.CUBING:
54  return Assets.Textures.GUI.Tabs.CubingTabTexture;
55  case GuiTabState.ESSENCE:
56  return Assets.Textures.GUI.Tabs.EssenceCraftTexture;
57  case GuiTabState.SOULFORGE:
58  return Assets.Textures.GUI.Tabs.SoulforgeTexture;
59  }
60  }
61 
62  public void SetVisibility(float whenActive, float whenInactive)
63  {
64  VisibilityActive = MathHelper.Clamp(whenActive, 0f, 1f);
65  VisibilityInactive = MathHelper.Clamp(whenInactive, 0f, 1f);
66  _button.SetVisibility(whenActive, whenInactive);
67  }
68 
69  public void SetActive(bool state)
70  {
71  IsActive = state;
72  }
73  }
74 }