SanAndreasUnity/Assets/Scripts/Importing/FontsImporter.cs

358 lines
11 KiB
C#
Raw Normal View History

using SanAndreasUnity.Importing.Conversion;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
2021-01-31 20:03:51 +00:00
#pragma warning disable 618
2021-01-31 19:58:44 +00:00
namespace SanAndreasUnity.Importing
{
2021-01-31 19:58:44 +00:00
public static class FontsImporter
{
2021-01-31 19:58:44 +00:00
private static List<Font> _loadedFonts = new List<Font>();
2021-01-31 19:58:44 +00:00
public static Font GetFont(int font)
{
2021-01-31 19:58:44 +00:00
return _loadedFonts.ElementAtOrDefault(font);
}
2021-01-31 19:58:44 +00:00
public static void LoadFonts()
{
for (int i = 1; i < 3; i++)
{
2021-01-31 19:58:44 +00:00
Texture2D fontTexture = TextureDictionary.Load("fonts").GetDiffuse($"font{i}", new TextureLoadParams() {makeNoLongerReadable = false}).Texture;
Texture2D flippedTexture = FlipTexture(fontTexture);
2021-01-31 19:58:44 +00:00
Font font = Resources.Load<Font>($"font{i}");
2021-01-31 19:58:44 +00:00
font.material = new Material(Shader.Find("Standard"))
{
mainTexture = flippedTexture
};
2021-01-31 19:58:44 +00:00
CharacterInfo[] characterInfo = new CharacterInfo[94];
2021-01-31 19:58:44 +00:00
float xSpacing = 32;
float ySpacing = 40;
2021-01-31 19:58:44 +00:00
float x = 1;
float y = 480;
2021-01-31 19:58:44 +00:00
float letterSize = 30f;
2021-01-31 19:58:44 +00:00
Rect vertRect = new Rect(0, 10, 10, -10);
2021-01-31 19:58:44 +00:00
//whitespace character
characterInfo[0].index = 32;
characterInfo[0].advance = 7;
characterInfo[0].vert = vertRect;
characterInfo[0].uv = new Rect(0, 0, letterSize / flippedTexture.width, letterSize / flippedTexture.height);
2021-01-31 19:58:44 +00:00
for (int j = 1; j < characterInfo.Length; j++)
{
2021-01-31 19:58:44 +00:00
characterInfo[j].index = 32 + j;
characterInfo[j].advance = GetAdvanceForIndex(i, characterInfo[j].index);
characterInfo[j].vert = vertRect;
if (i == 1)
{
characterInfo[j].uv = new Rect((x * xSpacing) / flippedTexture.width, (y - 4) / flippedTexture.height, letterSize / flippedTexture.width, letterSize / flippedTexture.height);
}
else
{
characterInfo[j].uv = new Rect((x * xSpacing) / flippedTexture.width, (y - 4) / flippedTexture.height, letterSize / flippedTexture.width, (letterSize + 5) / flippedTexture.height);
}
x++;
if (x >= 16)
{
x = 0;
y -= ySpacing;
}
}
2021-01-31 19:58:44 +00:00
font.characterInfo = characterInfo;
_loadedFonts.Add(font);
2021-01-31 19:58:44 +00:00
Font secondaryFont = Resources.Load<Font>($"font{i + 2}");
secondaryFont.material = font.material;
x = 0;
y = 120;
int index = 0;
bool skip = false;
for (int j = 1; j < 38; j++)
{
2021-01-31 19:58:44 +00:00
if (skip)
{
characterInfo[j].index = 96 + index;
}
else
{
characterInfo[j].index = 48 + index;
}
characterInfo[j].advance = GetAdvanceForIndexSecondary(i, characterInfo[j].index);
characterInfo[j].vert = vertRect;
characterInfo[j].uv = new Rect((x * xSpacing) / flippedTexture.width, (y - 7) / flippedTexture.height, letterSize / flippedTexture.width, (letterSize + 7) / flippedTexture.height);
x++;
if (x >= 16)
{
x = 0;
y -= ySpacing;
}
index++;
if (index == 10 && !skip)
{
skip = true;
index = 0;
}
}
2021-01-31 19:58:44 +00:00
index = 0;
2021-01-31 19:58:44 +00:00
x = 10;
y = 120;
//assign capital letters
for (int j = 38; j < 65; j++)
{
2021-01-31 19:58:44 +00:00
characterInfo[j].index = 64 + index;
characterInfo[j].advance = GetAdvanceForIndexSecondary(i, characterInfo[j].index);
characterInfo[j].vert = vertRect;
characterInfo[j].uv = new Rect((x * xSpacing) / flippedTexture.width, (y - 7) / flippedTexture.height, letterSize / flippedTexture.width, (letterSize + 7) / flippedTexture.height);
x++;
if (x >= 16)
{
x = 0;
y -= ySpacing;
}
index++;
}
2021-01-31 19:58:44 +00:00
secondaryFont.characterInfo = characterInfo;
_loadedFonts.Add(secondaryFont);
}
}
2021-01-31 19:58:44 +00:00
private static int GetAdvanceForIndexSecondary(int font, int index)
{
2021-01-31 19:58:44 +00:00
if (font == 1)
{
switch (index)
{
// 73 I
case 73:
2021-01-31 19:58:44 +00:00
// 105 i
case 105: return 3;
2021-01-31 19:58:44 +00:00
// 77 M
case 77:
2021-01-31 19:58:44 +00:00
// 109 m
case 109:
//87 W
case 87:
//119 w
case 119: return 10;
//76 L
case 76:
2021-01-31 19:58:44 +00:00
//108 l
case 108: return 5;
default: return 7;
}
}
else
{
switch (index)
{
2021-01-31 19:58:44 +00:00
// 73 I
case 73:
// 105 i
case 105: return 3;
// 77 M
case 77:
// 109 m
case 109:
2021-01-31 19:58:44 +00:00
//87 W
case 87:
//119 w
case 119: return 10;
2021-01-31 19:58:44 +00:00
//76 L
case 76:
//108 l
2021-01-31 19:58:44 +00:00
case 108: return 7;
2021-01-31 19:58:44 +00:00
default: return 8;
}
}
}
2021-01-31 19:58:44 +00:00
private static int GetAdvanceForIndex(int font, int index)
{
2021-01-31 19:58:44 +00:00
if (font == 1)
{
2021-01-31 19:58:44 +00:00
if (char.IsUpper((char) index))
{
2021-01-31 19:58:44 +00:00
switch (index)
{
//78 N
case 78:
//65 A
case 65: return 8;
// 73 I
case 73:
return 4;
//79 O
case 79:
//81 Q
case 81: return 9;
//77 M
case 77:
// 87 W
case 87: return 10;
//80 P
case 80:
//76 L
case 76:
//70 F
case 70:
// 69 E
case 69:
return 6;
//74 J
//case 106: return 2;
default: return 7;
}
}
else
{
switch (index)
{
// 109 m
case 109:
//119 w
case 119: return 10;
// 105 i
case 105:
//106 j
case 106: return 3;
//102 f
case 102:
//114 r
case 114:
//115 s
case 115:
// 99 c
case 99:
//116 t
case 116:
return 5;
//108 l
case 108: return 4;
default: return 7;
}
}
}
else
{
2021-01-31 19:58:44 +00:00
if (char.IsUpper((char) index))
{
2021-01-31 19:58:44 +00:00
switch (index)
{
//79 O
case 79:
//81 Q
case 81: return 11;
//77 M
case 77:
// 87 W
case 87: return 12;
default: return 9;
}
}
else
{
switch (index)
{
// 109 m
case 109:
//119 w
case 119: return 7;
//120 x
case 115: return 6;
// 105 i
case 105:
//106 j
case 106:
//102 f
case 102:
//114 r
case 114:
// 99 c
case 99:
//116 t
case 116:
return 4;
//108 l
case 108: return 3;
default: return 5;
}
}
}
}
2021-01-31 19:58:44 +00:00
private static Texture2D FlipTexture(Texture2D original, bool upSideDown = true)
{
Texture2D flipped = new Texture2D(original.width, original.height);
2021-01-31 19:58:44 +00:00
int xN = original.width;
int yN = original.height;
2021-01-31 19:58:44 +00:00
for (int i = 0; i < xN; i++)
{
2021-01-31 19:58:44 +00:00
for (int j = 0; j < yN; j++)
{
2021-01-31 19:58:44 +00:00
if (upSideDown)
{
flipped.SetPixel(j, xN - i - 1, original.GetPixel(j, i));
}
else
{
flipped.SetPixel(xN - i - 1, j, original.GetPixel(i, j));
}
}
}
2021-01-31 19:58:44 +00:00
flipped.Apply(true, true);
return flipped;
}
}
2021-01-31 19:58:44 +00:00
}
2021-01-31 20:03:51 +00:00
#pragma warning restore 618