Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
GuiTabWindow.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using Loot.UI.Common.Core;
5 using Loot.UI.Tabs.Cubing;
7 using Loot.UI.Tabs.Soulforging;
8 using Microsoft.Xna.Framework;
9 using Microsoft.Xna.Framework.Graphics;
10 using Terraria;
11 using Terraria.UI;
12 
13 namespace Loot.UI
14 {
19  internal class GuiTabWindow : DraggableUIState
20  {
21  private GuiHeader _header;
22  private GuiCloseButton _closeButton;
23  private GuiTab _currentTab;
24  internal GuiTabState _currentTabState = GuiTabState.CUBING;
25 
26  internal readonly Dictionary<GuiTabState, GuiTab> _tabs = new Dictionary<GuiTabState, GuiTab>
27  {
28  {GuiTabState.CUBING, new GuiCubingTab()},
29  {GuiTabState.ESSENCE, new GuiEssenceTab()},
30  {GuiTabState.SOULFORGE, new GuiSoulforgeTab()},
31  };
32 
33  internal readonly Dictionary<GuiTabState, GuiTabToggle> _toggles = new Dictionary<GuiTabState, GuiTabToggle>
34  {
35  {GuiTabState.CUBING, new GuiTabToggle(GuiTabState.CUBING)},
36  {GuiTabState.ESSENCE, new GuiTabToggle(GuiTabState.ESSENCE)},
37  {GuiTabState.SOULFORGE, new GuiTabToggle(GuiTabState.SOULFORGE)}
38  };
39 
40  public GuiTab GetCurrentTab()
41  {
42  _tabs.TryGetValue(_currentTabState, out _currentTab);
43  _currentTab?._GetPageHeight();
44  return _currentTab;
45  }
46 
47  public T GetTab<T>() where T : GuiTab
48  {
49  return (T)_tabs.Values.FirstOrDefault(t => t.GetType() == typeof(T));
50  }
51 
52  private void ToggleTab(UIMouseEvent evt, UIElement element, GuiTabToggle toggle)
53  {
54  if (_currentTabState == toggle.TargetState)
55  {
56  return;
57  }
58 
59  //SoundHelper.PlayCustomSound(SoundHelper.SoundType.OpenUI);
60  UpdateTabTo(toggle.TargetState);
61  }
62 
63  public void UpdateTabTo(GuiTabState newState)
64  {
65  _toggles[newState].SetActive(true);
66  _toggles[_currentTabState].SetActive(false);
67  _currentTabState = _toggles[newState].TargetState;
68  UpdateTab();
69  }
70 
71  public void UpdateTab()
72  {
73  if (HasChild(_currentTab))
74  {
75  RemoveChild(_currentTab);
76  }
77 
78  _currentTab?.OnHide();
79  GetCurrentTab();
80  _header.SetHeader(_currentTab.Header);
81  Append(_currentTab);
82  _currentTab.Activate();
83  _currentTab.OnShow();
84  UpdateWindow();
85  }
86 
87  public void UpdateWindow()
88  {
89  Height.Set(_header.Height.Pixels + _currentTab.TotalHeight, 0);
90  Recalculate();
91  }
92 
93  public void CenterWindow()
94  {
95  float useW = Main.screenWidth > Main.minScreenW ? Main.screenWidth : Main.minScreenW;
96  float useH = Main.screenHeight > Main.minScreenH ? Main.screenHeight : Main.minScreenH;
97  Left.Set(useW * 0.5f - Width.Pixels * 0.5f, 0);
98  Top.Set(useH * 0.5f - Height.Pixels * 0.5f, 0);
99  }
100 
101  public override void OnInitialize()
102  {
103  base.OnInitialize();
104 
105  Width.Set(422, 0);
106 
107  _header = new GuiHeader();
108 
109  Append(_header);
110  AddDragging(_header, this);
111 
112  _closeButton = new GuiCloseButton();
113  Append(_closeButton);
114 
115  foreach (var kvp in _tabs)
116  {
117  kvp.Value.Top.Set(_header.GetOffset(), 0);
118  }
119 
120  var togglePosition = new Vector2(422, _header.GetOffset());
121  var toggleOffset = Vector2.UnitY * 10;
122 
123  foreach (var toggle in _toggles.Select(x => x.Value))
124  {
125  toggle.Activate();
126  toggle.WhenClicked += ToggleTab;
127  var usePosition = togglePosition + toggleOffset;
128  toggle.Left.Set(usePosition.X, 0);
129  toggle.Top.Set(usePosition.Y, 0);
130  toggleOffset.Y += toggle.Height.Pixels;
131  Append(toggle);
132  }
133 
134  _toggles[_currentTabState].SetActive(true);
135  CenterWindow();
136  }
137 
138  public override void ToggleUI(UserInterface theInterface, UIState uiStateInstance = null)
139  {
140  base.ToggleUI(theInterface, uiStateInstance);
141 
142  if (Visible)
143  {
144  Main.playerInventory = true;
145  UpdateTab();
146  }
147  else
148  {
149  GiveBackItems();
150  }
151  _currentTab?.ToggleUI(Visible);
152  }
153 
154  public override void Update(GameTime gameTime)
155  {
156  base.Update(gameTime);
157 
158  if (Visible && !Main.playerInventory)
159  {
160  ToggleUI(Loot.Instance.GuiInterface);
161  }
162  }
163 
164  public override void Draw(SpriteBatch spriteBatch)
165  {
166  base.Draw(spriteBatch);
167 
168  if (IsMouseHovering || MouseOverUI())
169  {
170  Main.LocalPlayer.mouseInterface = true;
171  }
172  }
173 
174  private bool MouseOverUI()
175  {
176  var rect = new Rectangle((int)Left.Pixels, (int)Top.Pixels, (int)Width.Pixels, (int)Height.Pixels);
177  return rect.Contains(Main.MouseScreen.ToPoint());
178  }
179 
180  public void GiveBackItems()
181  {
182  foreach (var tab in _tabs.Values)
183  {
184  tab.GiveBackItems();
185  }
186  }
187  }
188 }