forked from JavidPack/RecipeBrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilities.cs
More file actions
142 lines (122 loc) · 5.18 KB
/
Utilities.cs
File metadata and controls
142 lines (122 loc) · 5.18 KB
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 Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using Terraria;
using Terraria.ID;
using Terraria.Map;
using Terraria.ObjectData;
namespace RecipeBrowser
{
static class Utilities
{
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(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] = Main.magicPixel;
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(Main.tileTexture[tile], 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)
{
string tileName = Lang.GetMapObjectName(MapHelper.TileToLookup(tile, 0));
if (tileName == "")
{
if (tile < TileID.Count)
tileName = $"Tile {tile}";
else
tileName = Terraria.ModLoader.TileLoader.GetTile(tile).Name + " (err no entry)";
}
return tileName;
}
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
}
}