-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathElectronicsManualUI.cs
129 lines (102 loc) · 3.79 KB
/
ElectronicsManualUI.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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.DataStructures;
using Terraria.GameContent.UI.Elements;
using Terraria.ID;
using Terraria.UI;
using Terraria.UI.Chat;
using WireMod.UI.Elements;
namespace WireMod.UI
{
public class ElectronicsManualUI : UIState
{
public static bool Visible { get; set; }
public DragableUIPanel Panel;
public float OffsetX = 300f;
public float OffsetY = 0f;
public float PanelWidth = 270f;
public float PanelHeight = 320f;
public float ButtonSize = 32f;
public float Padding = 20f;
public float InnerPadding = 10f;
public float ScaleX;
public float ScaleY;
public int PerRow = 6;
public override void OnInitialize()
{
this.Panel = new DragableUIPanel();
this.Panel.Left.Set(((Main.screenWidth - this.PanelWidth) / 2) + this.OffsetX, 0f);
this.Panel.Width.Set(this.PanelWidth, 0f);
this.Panel.BorderColor = new Color(0, 0, 0, 0);
var textHeight = ChatManager.GetStringSize(Main.fontMouseText, Constants.ToolCategories[0], new Vector2(0, 0)).Y;
var currentY = 0f;
var currentX = this.Padding;
var uiTitle = new UIText("Electronics Manual", 1.5f);
uiTitle.Height.Set(textHeight, 0f);
uiTitle.Top.Set(currentY, 0f);
this.Panel.Append(uiTitle);
currentY += 40;
for (var cat = 0; cat < Constants.ToolCategories.Count; cat++)
{
var uiCatTitle = new UIText(Constants.ToolCategories[cat]);
uiCatTitle.Height.Set(textHeight, 0f);
uiCatTitle.Top.Set(currentY, 0f);
this.Panel.Append(uiCatTitle);
var i = 0;
foreach (var tool in Constants.Tools[cat])
{
if (i % this.PerRow == 0)
{
currentY += this.ButtonSize + this.InnerPadding;
currentX = 0;
}
else
{
currentX += this.ButtonSize + this.InnerPadding;
}
// TODO: Issue #6: Remove need for separate device icons
var button = new ElectronicsManualButton(tool, WireMod.Instance.GetTexture($"Images/Icons/{tool}Icon"))
{
ToolCat = cat,
Tool = i
};
button.Height.Set(this.ButtonSize, 0f);
button.Width.Set(this.ButtonSize, 0f);
button.Left.Set(currentX, 0f);
button.Top.Set(currentY - (this.ButtonSize / 2), 0f);
button.SetVisibility(0f, 0.25f);
this.Panel.Append(button);
i++;
}
currentY += this.Padding + this.InnerPadding;
currentX = this.Padding;
}
this.Panel.Height.Set(currentY + this.Padding, 0f);
this.Panel.Top.Set(((Main.screenHeight - this.PanelHeight) / 2) + this.OffsetY, 0f);
this.Append(this.Panel);
Recalculate();
}
protected override void DrawSelf(SpriteBatch spriteBatch)
{
base.DrawSelf(spriteBatch);
var modPlayer = Main.LocalPlayer.GetModPlayer<WireModPlayer>();
if (modPlayer != null && modPlayer.ShowPreview) this.DrawPreview(spriteBatch);
}
protected void DrawPreview(SpriteBatch spriteBatch)
{
if (Main.netMode == NetmodeID.Server) return;
var modPlayer = Main.LocalPlayer.GetModPlayer<WireModPlayer>(WireMod.Instance);
var dev = modPlayer?.PlacingDevice;
if (dev == null) return;
var screenRect = new Rectangle((int)Main.screenPosition.X, (int)Main.screenPosition.Y, Main.screenWidth, Main.screenHeight);
var (x, y) = WireMod.Instance.GetMouseTilePosition();
var offsetMouseTile = new Point16(x - dev.Origin.X, y - dev.Origin.Y);
var offsetMouseWorld = new Point16(offsetMouseTile.X * 16, offsetMouseTile.Y * 16);
var offsetMouseScreen = new Point16(offsetMouseWorld.X - screenRect.X, offsetMouseWorld.Y - screenRect.Y);
var texture = WireMod.Instance.GetTexture($"Images/{dev.GetType().Name}");
var deviceScreenRect = new Rectangle(offsetMouseScreen.X, offsetMouseScreen.Y, dev.Width * 16, dev.Height * 16);
spriteBatch.Draw(texture, deviceScreenRect, dev.GetSourceRect(0), Color.White * 0.5f);
}
}
}