Skip to content

Commit

Permalink
Merge pull request #89 from InfernoDragon0/main
Browse files Browse the repository at this point in the history
Custom Tarot rotation fix, custom tarot back sprite
  • Loading branch information
xhayper authored Aug 9, 2024
2 parents 185e790 + 87a8017 commit 27db314
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions COTL_API/CustomSkins/CustomSkinManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,12 @@ public static void AddPlayerSkin(CustomPlayerSkin playerSkin)
new[] { "Default" }.Concat(CustomPlayerSkins.Keys).ToArray();
}

public static void AddTarotBackSkin(Sprite skin)
{
var name = "CustomCardBack";
if (!TarotSprites.ContainsKey(name)) TarotSprites.Add(name, skin);
}

private static List<Tuple<int, string>> RegionOverrideFunction(AtlasRegion region)
{
var simpleName = region.name;
Expand Down Expand Up @@ -592,7 +598,58 @@ private static Skin CreateTarotSkin(Skin template, string skinName)
var atlasRegion = atlas.GetAtlas().FindRegion("GENERIC_ATTACHMENT").Clone();
var back = template.Attachments.ToList()[0];
back = new Skin.SkinEntry(back.SlotIndex, back.Name, back.Attachment.Copy());
skin.SetAttachment(back.SlotIndex, back.Name, back.Attachment);

if (back.Attachment is MeshAttachment customAttachmentBack && TarotSprites.ContainsKey("CustomCardBack"))
{
var backSprite = TarotSprites["CustomCardBack"];
var backAtlas = CreateSingleTextureAtlas(backSprite);
var backAtlasRegion = backAtlas.GetAtlas().FindRegion("GENERIC_ATTACHMENT").Clone();

float minX = int.MaxValue;
float maxX = int.MinValue;
float minY = int.MaxValue;
float maxY = int.MinValue;

for (var j = 0; j < customAttachmentBack.Vertices.Length; j++)
switch (j % 3)
{
case 0:
minY = Math.Min(minY, customAttachmentBack.Vertices[j]);
maxY = Math.Max(maxY, customAttachmentBack.Vertices[j]);
break;
case 1:
minX = Math.Min(minX, customAttachmentBack.Vertices[j]);
maxX = Math.Max(maxX, customAttachmentBack.Vertices[j]);
break;
}

customAttachmentBack.Name = "CustomTarotSkin_" + skinName;
customAttachmentBack.SetRegion(backAtlasRegion, false);
backAtlasRegion.name = "CustomTarotSkin_" + atlasRegion.name;
customAttachmentBack.HullLength = 4;
customAttachmentBack.Triangles = [1, 2, 3, 1, 3, 0];

float pw = backAtlasRegion.page.width;
float ph = backAtlasRegion.page.height;
float x = backAtlasRegion.x;
float y = backAtlasRegion.y;
float w = backAtlasRegion.width;
float h = backAtlasRegion.height;
customAttachmentBack.UVs =
[
(x + w) / pw, y / ph, (x + w) / pw, (y + h) / ph, x / pw, (y + h) / ph, x / pw, y / ph
];
customAttachmentBack.Vertices = [minY, minX, 1, maxY, minX, 1, maxY, maxX, 1, minY, maxX, 1];
customAttachmentBack.WorldVerticesLength = 8;
customAttachmentBack.UpdateUVs();

skin.SetAttachment(back.SlotIndex, back.Name, customAttachmentBack);
}
else
{
skin.SetAttachment(back.SlotIndex, back.Name, back.Attachment);
}

var front = template.Attachments.ToList()[1];
front = new Skin.SkinEntry(front.SlotIndex, front.Name, front.Attachment.Copy());
if (front.Attachment is MeshAttachment customAttachment)
Expand All @@ -616,10 +673,11 @@ private static Skin CreateTarotSkin(Skin template, string skinName)
}

customAttachment.Name = "CustomTarotSkin_" + skinName;
customAttachment.SetRegion(atlasRegion);
customAttachment.SetRegion(atlasRegion, false);
atlasRegion.name = "CustomTarotSkin_" + atlasRegion.name;
customAttachment.HullLength = 4;
customAttachment.Triangles = [1, 2, 3, 1, 3, 0];

float pw = atlasRegion.page.width;
float ph = atlasRegion.page.height;
float x = atlasRegion.x;
Expand All @@ -632,6 +690,7 @@ private static Skin CreateTarotSkin(Skin template, string skinName)
];
customAttachment.Vertices = [minY, minX, 1, maxY, minX, 1, maxY, maxX, 1, minY, maxX, 1];
customAttachment.WorldVerticesLength = 8;
customAttachment.UpdateUVs();

skin.SetAttachment(front.SlotIndex, front.Name, customAttachment);
}
Expand Down

0 comments on commit 27db314

Please sign in to comment.