-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDamageClassDefinition.cs
142 lines (141 loc) · 5.18 KB
/
DamageClassDefinition.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Terraria.Localization;
using Terraria.ModLoader.Config;
using Terraria.ModLoader.IO;
using Terraria.ModLoader;
using Terraria.ModLoader.Config.UI;
using Terraria.UI;
using Terraria;
using Terraria.GameContent.UI.Elements;
using Terraria.GameContent;
using Microsoft.Xna.Framework.Graphics;
using Newtonsoft.Json.Linq;
using Terraria.UI.Chat;
using Terraria.ModLoader.UI;
using Origins.Reflection;
using Newtonsoft.Json;
using System.Reflection;
using tModPorter;
using FullSerializer;
using System.Globalization;
using System.ComponentModel;
using System.IO;
namespace Origins {
[CustomModConfigItem(typeof(DamageClassDefinitionConfigElement))]
[TypeConverter(typeof(ToFromStringConverter<DamageClassDefinition>))]
public class DamageClassDefinition : EntityDefinition, IEquatable<DamageClassDefinition> {
public static readonly Func<TagCompound, DamageClassDefinition> DESERIALIZER = Load;
public override bool IsUnloaded => !(Mod == "Terraria" && Name == "None" || Mod == "" && Name == "");
public string FullName => $"{Mod}/{Name}";
public DamageClass DamageClass => ModContent.TryFind(FullName, out DamageClass @class) ? @class : null;
public override int Type => DamageClass?.Type ?? -1;
public DamageClassDefinition() : base() { }
/// <summary><b>Note: </b>As ModConfig loads before other content, make sure to only use <see cref="DamageClassDefinition(string, string)"/> for modded content in ModConfig classes. </summary>
public DamageClassDefinition(int type) : base(DamageClassLoader.GetDamageClass(type).FullName) { }
public DamageClassDefinition(string key) : base(key) { }
public DamageClassDefinition(string mod, string name) : base(mod, name) { }
public static DamageClassDefinition FromString(string s) => new(s);
public static DamageClassDefinition Load(TagCompound tag) => new(tag.GetString("mod"), tag.GetString("name"));
public bool Equals(DamageClassDefinition other) => other?.FullName == FullName;
public override string DisplayName => IsUnloaded || Type == -1 ? Language.GetTextValue("Mods.ModLoader.Unloaded") : Name;
public override bool Equals(object obj) => Equals(obj as DamageClassDefinition);
public override int GetHashCode() => FullName.GetHashCode();
}
public class DamageClassDefinitionConfigElement : ConfigElement<DamageClassDefinition> {
protected bool pendingChanges = false;
public override void OnBind() {
base.OnBind();
base.TextDisplayFunction = TextDisplayOverride ?? base.TextDisplayFunction;
pendingChanges = true;
SetupList();
normalTooltip = TooltipFunction?.Invoke() ?? string.Empty;
TooltipFunction = () => tooltip;
}
public Func<string> TextDisplayOverride { get; set; }
float height = 0;
bool opened = false;
string normalTooltip;
string tooltip = string.Empty;
protected void SetupList() {
RemoveAllChildren();
height = 30;
foreach (DamageClass @class in DamageClasses.All) {
if (DamageClasses.HideInConfig.Contains(@class)) continue;
string text = @class.DisplayName.Value.Trim();
Vector2 size = FontAssets.MouseText.Value.MeasureString(text) * 0.8f;
UIPanel panel = new() {
Left = new(0, 0),
Top = new(height + 4, 0),
Width = new(-8, 1),
Height = new(size.Y + 4, 0),
HAlign = 0.5f,
PaddingTop = 0
};
UIText element = new(text, 0.8f) {
Width = new(0, 1),
Top = new(0, 0.5f),
VAlign = 0.5f
};
panel.OnUpdate += element => {
if (element is not UIPanel panel) return;
if (panel.IsMouseHovering) {
panel.BackgroundColor = UICommon.DefaultUIBlue;
tooltip = @class.FullName;
} else {
panel.BackgroundColor = UICommon.MainPanelBackground;
}
};
panel.OnLeftClick += (_, _) => {
if (Value.FullName != @class.FullName) Value = new(@class.FullName);
opened = false;
SetupList();
};
element.TextColor = Value.FullName == @class.FullName ? Color.Goldenrod : Color.White;
panel.Append(element);
Append(panel);
height += size.Y + 8;
}
Recalculate();
}
public override void LeftClick(UIMouseEvent evt) {
opened = true;
}
public override void Update(GameTime gameTime) {
SetHeight();
tooltip = normalTooltip;
if (opened) base.Update(gameTime);
}
void SetHeight() {
float targetHeight = opened ? height : 32;
if (Height.Pixels != targetHeight) {
Height.Pixels = targetHeight;
Parent.Height.Pixels = targetHeight;
this.Recalculate();
Parent.Recalculate();
}
}
protected override void DrawChildren(SpriteBatch spriteBatch) {
if (opened) {
base.DrawChildren(spriteBatch);
} else {
string text = $"{Value.DamageClass?.DisplayName.Value.Trim() ?? ""} ({Value.FullName})";
Vector2 size = FontAssets.MouseText.Value.MeasureString(text) * 0.8f;
CalculatedStyle innerDimensions = GetInnerDimensions();
ChatManager.DrawColorCodedStringWithShadow(
spriteBatch,
FontAssets.MouseText.Value,
text,
innerDimensions.Position() + new Vector2(innerDimensions.Width - size.X, (innerDimensions.Height - size.Y) * 0.5f + 4),
Color.White,
0f,
Vector2.Zero,
Vector2.One * 0.8f
);
}
}
}
}