-
Notifications
You must be signed in to change notification settings - Fork 23
/
BossChecklistUI.cs
286 lines (246 loc) · 11.3 KB
/
BossChecklistUI.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using ReLogic.Content;
using Terraria;
using Terraria.Audio;
using Terraria.GameContent;
using Terraria.GameContent.UI.Elements;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.UI;
using Terraria.UI.Chat;
using Terraria.Localization;
namespace BossChecklist.UIElements
{
class BossChecklistUI : UIState {
public UIHoverImageButton toggleCompletedButton;
public UIHoverImageButton toggleMiniBossButton;
public UIHoverImageButton toggleEventButton;
public UIHoverImageButton toggleHiddenButton;
public UIPanel checklistPanel;
public UIList checklistList;
float spacing = 8f;
public static bool Visible {
get { return BossUISystem.bossChecklistInterface.CurrentState == BossUISystem.Instance.bossChecklistUI; }
set { BossUISystem.bossChecklistInterface.SetState(value ? BossUISystem.Instance.bossChecklistUI : null); }
}
public static bool showCompleted = true;
public static bool showMiniBoss = true;
public static bool showEvent = true;
public static bool showHidden = false;
bool imagesResized;
public override void Update(GameTime gameTime) {
base.Update(gameTime);
if (IsMouseHovering)
Terraria.GameInput.PlayerInput.LockVanillaMouseScroll("BossChecklist/BossChecklistUI");
if (!imagesResized) {
Main.instance.LoadItem(ItemID.SuspiciousLookingEye);
//Main.instance.LoadItem(ItemID.CandyCorn);
//Main.instance.LoadItem(ItemID.SnowGlobe);
//Main.instance.LoadItem(ItemID.InvisibilityPotion);
Texture2D completedToggle = ResizeImage(TextureAssets.Item[ItemID.SuspiciousLookingEye].Value, 26, 26);
//Texture2D miniBossToggle = ResizeImage(TextureAssets.Item[ItemID.CandyCorn].Value, 26, 26);
//Texture2D eventToggle = ResizeImage(TextureAssets.Item[ItemID.SnowGlobe].Value, 26, 26);
//Texture2D showHiddenToggle = ResizeImage(TextureAssets.Item[ItemID.InvisibilityPotion].Value, 26, 26);
toggleCompletedButton.SetImage(TextureAsset(completedToggle));
//toggleMiniBossButton.SetImage(TextureAsset(miniBossToggle));
//toggleEventButton.SetImage(TextureAsset(eventToggle));
//toggleHiddenButton.SetImage(TextureAsset(showHiddenToggle));
imagesResized = true;
}
BossUISystem.Instance.bossChecklistUI.checklistPanel.Left.Pixels = Main.playerInventory ? -200 : 0;
}
public static Asset<Texture2D> TextureAsset(Texture2D texture) {
using MemoryStream stream = new();
texture.SaveAsPng(stream, texture.Width, texture.Height);
stream.Position = 0;
return BossChecklist.instance.Assets.CreateUntracked<Texture2D>(stream, ".png");
}
public override void OnInitialize() {
checklistPanel = new UIPanel();
checklistPanel.SetPadding(10);
checklistPanel.Left.Pixels = 0;
checklistPanel.HAlign = 1f;
checklistPanel.Top.Set(50f, 0f);
checklistPanel.Width.Set(250f, 0f);
checklistPanel.Height.Set(-100, 1f);
checklistPanel.BackgroundColor = new Color(73, 94, 171);
var buttonPanel = new UIPanel();
buttonPanel.BackgroundColor = Color.AliceBlue;
buttonPanel.SetPadding(6);
buttonPanel.Width.Set(0, 1f);
buttonPanel.Height.Set(36, 0f);
checklistPanel.Append(buttonPanel);
toggleCompletedButton = new UIHoverImageButton(TextureAssets.MagicPixel, Language.GetTextValue("Mods.BossChecklist.Checklist.ToggleCompletedTooltip"));
toggleCompletedButton.OnLeftClick += ToggleCompletedButtonClicked;
toggleCompletedButton.Left.Pixels = spacing;
toggleCompletedButton.Top.Pixels = 0;
toggleCompletedButton.SetVisibility(1f, 0.7f);
buttonPanel.Append(toggleCompletedButton);
toggleMiniBossButton = new UIHoverImageButton(ModContent.Request<Texture2D>("BossChecklist/Resources/Nav_Miniboss", AssetRequestMode.ImmediateLoad), Language.GetTextValue("Mods.BossChecklist.Checklist.ToggleMiniBossesTooltip"));
toggleMiniBossButton.OnLeftClick += ToggleMiniBossButtonClicked;
toggleMiniBossButton.Left.Pixels = spacing + 32;
toggleMiniBossButton.Top.Pixels = 0;
toggleMiniBossButton.SetVisibility(1f, 0.7f);
buttonPanel.Append(toggleMiniBossButton);
toggleEventButton = new UIHoverImageButton(ModContent.Request<Texture2D>("BossChecklist/Resources/Nav_Event", AssetRequestMode.ImmediateLoad), Language.GetTextValue("Mods.BossChecklist.Checklist.ToggleEventsTooltip"));
toggleEventButton.OnLeftClick += ToggleEventButtonClicked;
toggleEventButton.Left.Pixels = spacing + 64;
toggleEventButton.Top.Pixels = 0;
toggleEventButton.SetVisibility(1f, 0.7f);
buttonPanel.Append(toggleEventButton);
toggleHiddenButton = new UIHoverImageButton(ModContent.Request<Texture2D>("BossChecklist/Resources/Nav_Hidden", AssetRequestMode.ImmediateLoad), Language.GetTextValue("Mods.BossChecklist.Checklist.ToggleShowHiddenBossesTooltip"));
toggleHiddenButton.OnLeftClick += ToggleHiddenButtonClicked;
toggleHiddenButton.Left.Pixels = spacing + 96;
toggleHiddenButton.Top.Pixels = 0;
toggleHiddenButton.SetVisibility(1f, 0.7f);
buttonPanel.Append(toggleHiddenButton);
checklistList = new UIList();
checklistList.Top.Pixels = 42f + spacing;
checklistList.Width.Set(-25f, 1f);
checklistList.Height.Set(-42f, 1f);
checklistList.ListPadding = 12f;
checklistPanel.Append(checklistList);
FixedUIScrollbar checklistListScrollbar = new FixedUIScrollbar();
checklistListScrollbar.SetView(100f, 1000f);
//checklistListScrollbar.Height.Set(0f, 1f);
checklistListScrollbar.Top.Pixels = 42f + spacing;
checklistListScrollbar.Height.Set(-42f - spacing, 1f);
checklistListScrollbar.HAlign = 1f;
checklistPanel.Append(checklistListScrollbar);
checklistList.SetScrollbar(checklistListScrollbar);
// Checklistlist populated when the panel is shown: UpdateCheckboxes()
Append(checklistPanel);
}
private void ToggleCompletedButtonClicked(UIMouseEvent evt, UIElement listeningElement) {
showCompleted = !showCompleted;
SoundEngine.PlaySound(showCompleted ? SoundID.MenuOpen : SoundID.MenuClose);
UpdateCheckboxes();
}
private void ToggleMiniBossButtonClicked(UIMouseEvent evt, UIElement listeningElement) {
showMiniBoss = !showMiniBoss;
SoundEngine.PlaySound(showMiniBoss ? SoundID.MenuOpen : SoundID.MenuClose);
UpdateCheckboxes();
}
private void ToggleEventButtonClicked(UIMouseEvent evt, UIElement listeningElement) {
showEvent = !showEvent;
SoundEngine.PlaySound(showEvent ? SoundID.MenuOpen : SoundID.MenuClose);
UpdateCheckboxes();
}
private void ToggleHiddenButtonClicked(UIMouseEvent evt, UIElement listeningElement) {
if (Main.keyState.IsKeyDown(Keys.LeftAlt) || Main.keyState.IsKeyDown(Keys.RightAlt)) {
WorldAssist.HiddenEntries.Clear();
showHidden = false;
UpdateCheckboxes();
if (Main.netMode == NetmodeID.MultiplayerClient) {
ModPacket packet = BossChecklist.instance.GetPacket();
packet.Write((byte)PacketMessageType.RequestClearHidden);
packet.Send();
}
SoundEngine.PlaySound(showHidden ? SoundID.MenuOpen : SoundID.MenuClose);
return;
}
showHidden = !showHidden;
SoundEngine.PlaySound(showHidden ? SoundID.MenuOpen : SoundID.MenuClose);
UpdateCheckboxes();
}
/*public bool ThoriumModDownedScout
{
get { return ThoriumMod.ThoriumWorld.downedScout; }
}
public bool CalamityDS => CalamityMod.CalamityWorld.downedDesertScourge;*/
internal void UpdateCheckboxes() {
var expandedBoss = (checklistList._items.FirstOrDefault(x => x is UIBossCheckbox checkbox && checkbox.expanded) as UIBossCheckbox)?.boss;
checklistList.Clear();
foreach (EntryInfo boss in BossChecklist.bossTracker.SortedEntries) {
boss.hidden = WorldAssist.HiddenEntries.Contains(boss.Key);
if (boss.available() && (!boss.hidden || showHidden)) {
if (showCompleted || !boss.downed()) {
if (boss.type == EntryType.Event && !showEvent)
continue;
if (boss.type == EntryType.MiniBoss && !showMiniBoss)
continue;
UIBossCheckbox box = new UIBossCheckbox(boss);
checklistList.Add(box);
if (expandedBoss == boss) {
box.expanded = true;
box.PostExpand();
}
}
}
}
//if (BossChecklist.instance.thoriumLoaded)
//{
// if (ThoriumModDownedScout)
// {
// // Add items here
// }
//}
}
protected override void DrawSelf(SpriteBatch spriteBatch) {
Vector2 MousePosition = new Vector2((float)Main.mouseX, (float)Main.mouseY);
if (checklistPanel.ContainsPoint(MousePosition)) {
Main.player[Main.myPlayer].mouseInterface = true;
// Doesn't fully fix problem. Clicks still happen in back to front manner.
//Main.HoverItem = new Item();
//Main.hoverItemName = "";
}
}
public TextSnippet hoveredTextSnippet;
public override void Draw(SpriteBatch spriteBatch) {
base.Draw(spriteBatch);
// now we can draw after all other drawing.
if (hoveredTextSnippet != null) {
hoveredTextSnippet.OnHover();
if (Main.mouseLeft && Main.mouseLeftRelease) {
hoveredTextSnippet.OnClick();
}
hoveredTextSnippet = null;
}
}
private Texture2D ResizeImage(Texture2D texture2D, int desiredWidth, int desiredHeight) {
RenderTarget2D renderTarget = new RenderTarget2D(Main.graphics.GraphicsDevice, desiredWidth, desiredHeight);
Main.instance.GraphicsDevice.SetRenderTarget(renderTarget);
Main.instance.GraphicsDevice.Clear(Color.Transparent);
Main.spriteBatch.Begin();
float scale = 1;
if (texture2D.Width > desiredWidth || texture2D.Height > desiredHeight) {
if (texture2D.Height > texture2D.Width)
scale = (float)desiredWidth / texture2D.Height;
else
scale = (float)desiredWidth / texture2D.Width;
}
//new Vector2(texture2D.Width / 2 * scale, texture2D.Height / 2 * scale) desiredWidth/2, desiredHeight/2
Main.spriteBatch.Draw(texture2D, new Vector2(desiredWidth / 2, desiredHeight / 2), null, Color.White, 0f, new Vector2(texture2D.Width / 2, texture2D.Height / 2), scale, SpriteEffects.None, 0f);
Main.spriteBatch.End();
Main.instance.GraphicsDevice.SetRenderTarget(null);
Texture2D mergedTexture = new Texture2D(Main.instance.GraphicsDevice, desiredWidth, desiredHeight);
Color[] content = new Color[desiredWidth * desiredHeight];
renderTarget.GetData<Color>(content);
mergedTexture.SetData<Color>(content);
return mergedTexture;
}
}
internal class HoveredTextSnippetTooltipHack : GlobalItem
{
const int paddingForBox = 10;
public override bool PreDrawTooltip(Item item, ReadOnlyCollection<TooltipLine> lines, ref int x, ref int y) {
if (BossUISystem.Instance.bossChecklistUI.hoveredTextSnippet != null || BossUISystem.Instance.BossLog.hoveredTextSnippet != null) {
var texts = lines.Select(z => z.Text);
string longestText = texts.ToList().OrderByDescending(z => z.Length).First();
int widthForBox = (int)FontAssets.MouseText.Value.MeasureString(longestText).X;
int heightForBox = (int)texts.ToList().Sum(z => FontAssets.MouseText.Value.MeasureString(z).Y);
Vector2 drawPosForBox = new Vector2(x - paddingForBox, y - paddingForBox);
Rectangle drawRectForBox = new Rectangle(x, y, widthForBox, heightForBox);
drawRectForBox.Inflate(paddingForBox, paddingForBox);
// Draw the magic box
Main.spriteBatch.Draw(TextureAssets.MagicPixel.Value, drawRectForBox, Color.NavajoWhite);
}
return base.PreDrawTooltip(item, lines, ref x, ref y);
}
}
}