-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathUtilities.cs
216 lines (186 loc) · 7.51 KB
/
Utilities.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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ReLogic.Content;
using Terraria;
using Terraria.GameContent;
using Terraria.ID;
using Terraria.Map;
using Terraria.ObjectData;
using Terraria.ModLoader;
namespace ItemChecklist
{
static class Utilities
{
//public static Asset<Texture2D> ToAsset(this Texture2D texture)
//{
// using MemoryStream stream = new();
// texture.SaveAsPng(stream, texture.Width, texture.Height);
// stream.Position = 0;
// return RecipeBrowser.instance.Assets.CreateUntracked<Texture2D>(stream, "any.png");
//}
internal static Texture2D StackResizeImage(Asset<Texture2D>[] texture2D, int desiredWidth, int desiredHeight)
{
foreach (Asset<Texture2D> asset in texture2D)
asset.Wait?.Invoke();
return StackResizeImage(texture2D.Select(asset => asset.Value).ToArray(), desiredWidth, desiredHeight);
}
internal static Texture2D StackResizeImage(Texture2D[] texture2D, int desiredWidth, int desiredHeight)
{
float overlap = .5f;
float totalScale = 1 / (1f + ((1 - overlap) * (texture2D.Length - 1)));
int newWidth = (int)(desiredWidth * totalScale);
int newHeight = (int)(desiredHeight * totalScale);
//var texture2Ds = texture2D.Select(x => ResizeImage(x, newWidth, newHeight));
RenderTarget2D renderTarget = new RenderTarget2D(Main.graphics.GraphicsDevice, desiredWidth, desiredHeight);
Main.instance.GraphicsDevice.SetRenderTarget(renderTarget);
Main.instance.GraphicsDevice.Clear(Color.Transparent);
Main.spriteBatch.Begin();
int index = 0;
foreach (var texture in texture2D)
{
float scale = 1;
if (texture.Width > newWidth || texture.Height > newHeight)
{
if (texture.Height > texture.Width)
scale = (float)newHeight / texture.Height;
else
scale = (float)newWidth / texture.Width;
}
Vector2 position = new Vector2(newWidth / 2, newHeight / 2);
position += new Vector2(index * (1 - overlap) * newWidth, index * (1 - overlap) * newHeight);
Main.spriteBatch.Draw(texture, position, null, Color.White, 0f, new Vector2(texture.Width / 2, texture.Height / 2), scale, SpriteEffects.None, 0f);
index++;
}
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 static Texture2D ResizeImage(Asset<Texture2D> asset, int desiredWidth, int desiredHeight)
{
asset.Wait?.Invoke();
return ResizeImage(asset.Value, desiredWidth, desiredHeight);
}
internal static 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 static Dictionary<int, Texture2D> tileTextures;
internal static void GenerateTileTexture(int tile)
{
Texture2D texture;
Main.instance.LoadTiles(tile);
var tileObjectData = TileObjectData.GetTileData(tile, 0, 0);
if (tileObjectData == null)
{
tileTextures[tile] = TextureAssets.MagicPixel.Value;
return;
}
int width = tileObjectData.Width;
int height = tileObjectData.Height;
int padding = tileObjectData.CoordinatePadding;
//Main.spriteBatch.End();
RenderTarget2D renderTarget = new RenderTarget2D(Main.graphics.GraphicsDevice, width * 16, height * 16);
Main.instance.GraphicsDevice.SetRenderTarget(renderTarget);
Main.instance.GraphicsDevice.Clear(Color.Transparent);
Main.spriteBatch.Begin();
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
Main.spriteBatch.Draw(TextureAssets.Tile[tile].Value, new Vector2(i * 16, j * 16), new Rectangle(i * 16 + i * padding, j * 16 + j * padding, 16, 16), Color.White, 0f, Vector2.Zero, 1, SpriteEffects.None, 0f);
}
}
Main.spriteBatch.End();
Main.instance.GraphicsDevice.SetRenderTarget(null);
texture = new Texture2D(Main.instance.GraphicsDevice, width * 16, height * 16);
Color[] content = new Color[width * 16 * height * 16];
renderTarget.GetData<Color>(content);
texture.SetData<Color>(content);
tileTextures[tile] = texture;
}
internal static string GetTileName(int tile)
{
int requiredTileStyle = Recipe.GetRequiredTileStyle(tile);
string tileName = Lang.GetMapObjectName(MapHelper.TileToLookup(tile, requiredTileStyle));
if (tileName == "")
{
if (tile < TileID.Count)
tileName = TileID.Search.GetName(tile);// $"Tile {tile}";
else
tileName = Terraria.ModLoader.TileLoader.GetTile(tile).Name + " (err no entry)";
}
return tileName;
}
internal static List<int> PopulateAdjTilesForTile(int Tile) {
List<int> adjTiles = new List<int>();
adjTiles.Add(Tile);
ModTile modTile = TileLoader.GetTile(Tile);
if (modTile != null) {
adjTiles.AddRange(modTile.AdjTiles);
}
if (Tile == 302)
adjTiles.Add(17);
if (Tile == 77)
adjTiles.Add(17);
if (Tile == 133) {
adjTiles.Add(17);
adjTiles.Add(77);
}
if (Tile == 134)
adjTiles.Add(16);
if (Tile == 354)
adjTiles.Add(14);
if (Tile == 469)
adjTiles.Add(14);
if (Tile == 487)
adjTiles.Add(14);
if (Tile == 355) {
adjTiles.Add(13);
adjTiles.Add(14);
}
// TODO: GlobalTile.AdjTiles support (no player object, reflection needed since private)
return adjTiles;
}
internal static Color textColor = Color.White; // new Color(Main.mouseTextColor, Main.mouseTextColor, Main.mouseTextColor);
internal static Color noColor = Color.LightSalmon; // OrangeRed Red
internal static Color yesColor = Color.LightGreen; // Green
internal static Color maybeColor = Color.Yellow; // LightYellow LightGoldenrodYellow Yellow Goldenrod
internal static void LoadItem(int type) {
// Use this instead of Main.instance.LoadItem because we don't need ImmediateLoad
if (TextureAssets.Item[type].State == AssetState.NotLoaded)
Main.Assets.Request<Texture2D>(TextureAssets.Item[type].Name, AssetRequestMode.AsyncLoad);
}
internal static void LoadNPC(int type) {
// Use this instead of Main.instance.LoadNPC because we don't need ImmediateLoad
if (TextureAssets.Npc[type].State == AssetState.NotLoaded)
Main.Assets.Request<Texture2D>(TextureAssets.Npc[type].Name, AssetRequestMode.AsyncLoad);
}
}
}