Skip to content

Commit 897360a

Browse files
committed
Merge branch 'release/1.1.0'
2 parents 3547e0e + a516de9 commit 897360a

35 files changed

+376
-1056
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

Documentation/screenshot-01.png

-643 Bytes
Loading

Editor/BitmapFontCreator.cs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace dev.klebersilva.tools.bitmapfontcreator
77
{
88
internal static class BitmapFontCreator
99
{
10-
public static void CreateFont(ExecutionData data)
10+
public static void TryCreateFont(ExecutionData data, bool warnBeforeOverwrite)
1111
{
1212
var error = CheckForErrors(data);
1313
if (!string.IsNullOrEmpty(error))
@@ -16,24 +16,32 @@ public static void CreateFont(ExecutionData data)
1616
return;
1717
}
1818

19-
// TODO check if the asset already exists to warn the user
20-
2119
var path = AssetDatabase.GetAssetPath(data.Texture);
2220
var baseName = Path.GetFileNameWithoutExtension(path);
21+
path = path[..path.LastIndexOf(".")];
22+
var materialPath = path + ".mat";
23+
var fontPath = path + ".fontsettings";
24+
25+
if (warnBeforeOverwrite && !(AssetDatabase.GUIDFromAssetPath(materialPath) == null && AssetDatabase.GUIDFromAssetPath(fontPath) == null))
26+
{
27+
if (!EditorUtility.DisplayDialog("Warning", "Asset already exists. Overwrite? (It will keep the references)", "Yes", "No"))
28+
return;
29+
}
2330

2431
var material = CreateMaterial(baseName, data.Texture);
2532
var font = CreateFontAsset(baseName, material, data);
2633

27-
path = path[..path.LastIndexOf(".")];
28-
AssetDatabase.CreateAsset(material, path + ".mat");
29-
CreateOrReplaceAsset(font, path + ".fontsettings");
34+
AssetDatabase.CreateAsset(material, materialPath);
35+
CreateOrReplaceAsset(font, fontPath);
3036

3137
AssetDatabase.Refresh();
3238
}
3339

34-
// TODO all checks
3540
private static string CheckForErrors(ExecutionData data)
3641
{
42+
if (data.Cols < 1) return "Cols must be greater than 0";
43+
if (data.Rows < 1) return "Rows must be greater than 0";
44+
3745
if (data.Texture == null) return "Texture cannot be null";
3846
if (!data.Texture.isReadable) return "Texture must be readable. Set Read/Write Enabled to true inside Texture Properties";
3947

@@ -72,7 +80,7 @@ private static CharacterInfo[] CreateCharacters(ExecutionData data, Dictionary<c
7280
var cellUVSize = new Vector2(1f / data.Cols, 1f / data.Rows);
7381

7482
var characters = new List<CharacterInfo>();
75-
int xMin, yMin, xMax, yMax, advance;
83+
int xMin, xMax, advance;
7684
int largestAdvance = 0;
7785

7886
// horizontal
@@ -87,13 +95,14 @@ private static CharacterInfo[] CreateCharacters(ExecutionData data, Dictionary<c
8795
if (ch == ' ' || ch == '\r' || ch == '\n') continue;
8896

8997
GetCharacterBounds(
90-
data.Texture,
91-
data.AlphaThreshold,
92-
col * (int)cellSize.x,
93-
(data.Rows - row) * (int)cellSize.y,
94-
(int)cellSize.x,
95-
(int)cellSize.y,
96-
out xMin, out yMin, out xMax, out yMax
98+
tex: data.Texture,
99+
alphaThreshold: data.AlphaThreshold,
100+
x0: col * (int)cellSize.x,
101+
y0: (data.Rows - row) * (int)cellSize.y,
102+
width: (int)cellSize.x,
103+
height: (int)cellSize.y,
104+
xMin: out xMin,
105+
xMax: out xMax
97106
);
98107

99108
advance = xMax - xMin + data.DefaultCharacterSpacing;
@@ -130,14 +139,13 @@ private static CharacterInfo[] CreateCharacters(ExecutionData data, Dictionary<c
130139
return characters.ToArray();
131140
}
132141

133-
// TODO maybe we can remove yMin and yMax
134-
private static void GetCharacterBounds(Texture2D tex, float alphaThreshold, int x0, int y0, int width, int height,
135-
out int xMin, out int yMin, out int xMax, out int yMax)
142+
private static void GetCharacterBounds(Texture2D tex, float alphaThreshold, int x0, int y0,
143+
int width, int height, out int xMin, out int xMax)
136144
{
137145
xMin = width;
138-
yMin = height;
139146
xMax = 0;
140-
yMax = 0;
147+
// yMin = height;
148+
// yMax = 0;
141149

142150
int xx, yy;
143151
for (var y = 0; y < height; y++)
@@ -149,8 +157,8 @@ private static void GetCharacterBounds(Texture2D tex, float alphaThreshold, int
149157
if (tex.GetPixel(xx, yy).a <= alphaThreshold) continue;
150158
if (x < xMin) xMin = x;
151159
if (x > xMax) xMax = x;
152-
if (y < yMin) yMin = y;
153-
if (y > yMax) yMax = y;
160+
// if (y < yMin) yMin = y;
161+
// if (y > yMax) yMax = y;
154162
}
155163
}
156164
}

Editor/Model/BitmapFontCreatorModel.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ internal class BitmapFontCreatorData
2424
public int Cols;
2525
public int Rows;
2626
public float AlphaThreshold;
27-
// public int LineSpacing;
2827
public bool Monospaced;
2928
public int DefaultCharacterSpacing;
3029
public List<CharacterProps> CustomCharacterProps;
@@ -53,18 +52,17 @@ internal class ExecutionData : BitmapFontCreatorData
5352
{
5453
public Texture2D Texture;
5554

56-
// TODO move this to ui
5755
public static ExecutionData Default => new()
5856
{
5957
Texture = null,
60-
Characters = "$ 0123456789 . ",
58+
Characters = "",
6159
Orientation = Orientation.Horizontal,
62-
Cols = 3,
63-
Rows = 5,
60+
Cols = 1,
61+
Rows = 1,
6462
AlphaThreshold = 0f,
6563
DefaultCharacterSpacing = 10,
6664
Monospaced = false,
67-
CustomCharacterProps = new List<CharacterProps>() { new() { Character = ".", Spacing = 0 } },
65+
CustomCharacterProps = new List<CharacterProps>(),
6866
};
6967
}
7068
}

Editor/Model/Prefs/BitmapFontCreatorPrefs.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

Editor/Model/PrefsModel.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace dev.klebersilva.tools.bitmapfontcreator
5+
{
6+
internal class PrefsModel
7+
{
8+
private const string EditorPrefsKey = "dev.klebersilva.tools.bitmapfontcreator.preferences";
9+
10+
public bool WarnOnReplaceFont = true;
11+
public bool WarnOnReplaceSettings = true;
12+
public bool WarnOnReplaceProfile = true;
13+
14+
public static PrefsModel Load()
15+
{
16+
var content = EditorPrefs.GetString(EditorPrefsKey, null);
17+
return string.IsNullOrEmpty(content)
18+
? new PrefsModel()
19+
: JsonUtility.FromJson<PrefsModel>(content);
20+
}
21+
22+
public static void Save(PrefsModel model)
23+
{
24+
var content = JsonUtility.ToJson(model);
25+
EditorPrefs.SetString(EditorPrefsKey, content);
26+
}
27+
}
28+
}

Editor/Model/PrefsModel.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)