Skip to content

Commit 637b59f

Browse files
committed
Changed: Ignoring \n characters to make it easier to add them to the characters field
1 parent 0295208 commit 637b59f

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

Editor/BitmapFontCreator.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace dev.klebersilva.tools.bitmapfontcreator
77
{
88
internal static class BitmapFontCreator
99
{
10+
private const char IgnoreCharacter = ' ';
11+
1012
public static void TryCreateFont(ExecutionData data, bool warnBeforeOverwrite)
1113
{
1214
var error = CheckForErrors(data);
@@ -45,8 +47,8 @@ private static string CheckForErrors(ExecutionData data)
4547
if (data.Texture == null) return "Texture cannot be null";
4648
if (!data.Texture.isReadable) return "Texture must be readable. Set Read/Write Enabled to true inside Texture Properties";
4749

48-
if (data.Characters.Length != data.Cols * data.Rows)
49-
return $"Characters length ({data.Characters.Length}) must be equal to Cols ({data.Cols}) * Rows ({data.Rows})";
50+
if (data.ValidCharactersCount != data.Cols * data.Rows)
51+
return $"Characters length ({data.ValidCharactersCount}) must be equal to Cols ({data.Cols}) * Rows ({data.Rows})";
5052

5153
return null;
5254
}
@@ -90,8 +92,8 @@ private static CharacterInfo[] CreateCharacters(ExecutionData data, Dictionary<c
9092
var i = data.Orientation == Orientation.Horizontal
9193
? (row * data.Cols) + col
9294
: (col * data.Rows) + row;
93-
var ch = data.Characters[i];
94-
if (ch == ' ' || ch == '\r' || ch == '\n') continue;
95+
var ch = data.ValidCharacters[i];
96+
if (ch == IgnoreCharacter) continue;
9597

9698
GetCharacterBounds(
9799
tex: data.Texture,

Editor/Model/BitmapFontCreatorModel.cs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ internal class CharacterProps
1717
public int Spacing = 0;
1818
}
1919

20-
internal class BitmapFontCreatorData
20+
[Serializable]
21+
internal class BitmapFontCreatorData : ISerializationCallbackReceiver
2122
{
22-
public string Characters;
23+
private const string IgnoreCharacter = "\n";
24+
25+
[SerializeField] private string _characters = string.Empty;
2326
public Orientation Orientation;
2427
public int Cols;
2528
public int Rows;
@@ -28,11 +31,25 @@ internal class BitmapFontCreatorData
2831
public int DefaultCharacterSpacing;
2932
public List<CharacterProps> CustomCharacterProps;
3033

34+
public string Characters
35+
{
36+
get { return _characters; }
37+
set
38+
{
39+
if (_characters == value) return;
40+
_characters = value;
41+
UpdateChacters();
42+
}
43+
}
44+
45+
public string ValidCharacters { get; protected set; } = string.Empty;
46+
public int ValidCharactersCount { get; protected set; } = 0;
47+
3148
public virtual void CopyTo(BitmapFontCreatorData dest)
3249
{
3350
if (dest == null) return;
3451

35-
dest.Characters = Characters;
52+
dest.Characters = _characters;
3653
dest.Orientation = Orientation;
3754
dest.Cols = Cols;
3855
dest.Rows = Rows;
@@ -45,6 +62,22 @@ public virtual void CopyTo(BitmapFontCreatorData dest)
4562

4663
foreach (var e in CustomCharacterProps)
4764
dest.CustomCharacterProps.Add(new CharacterProps() { Character = e.Character, Spacing = e.Spacing });
65+
66+
UpdateChacters();
67+
}
68+
69+
// TODO this should be called automatically when the field Characters changes
70+
private void UpdateChacters()
71+
{
72+
ValidCharacters = _characters.Replace(IgnoreCharacter, string.Empty);
73+
ValidCharactersCount = ValidCharacters.Length;
74+
}
75+
76+
public void OnBeforeSerialize() { }
77+
public void OnAfterDeserialize()
78+
{
79+
Debug.Log("Deserializing");
80+
UpdateChacters();
4881
}
4982
}
5083

@@ -55,14 +88,16 @@ internal class ExecutionData : BitmapFontCreatorData
5588
public static ExecutionData Default => new()
5689
{
5790
Texture = null,
58-
Characters = "",
91+
Characters = string.Empty,
5992
Orientation = Orientation.Horizontal,
6093
Cols = 1,
6194
Rows = 1,
6295
AlphaThreshold = 0f,
6396
DefaultCharacterSpacing = 10,
6497
Monospaced = false,
6598
CustomCharacterProps = new List<CharacterProps>(),
99+
ValidCharacters = string.Empty,
100+
ValidCharactersCount = 0
66101
};
67102
}
68103
}

Editor/UI/BitmapFontCreatorEditor.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class BitmapFontCreatorEditor : EditorWindow
1717
private Vector2 _mainScrollPos = Vector2.zero;
1818
private int _selectedCharacterSetIndex = 0;
1919

20-
private static Vector2Int _guessCache;
20+
private Vector2Int _guessCache;
2121

2222
[MenuItem(MenuItemPath)]
2323
public static void ShowWindow()
@@ -109,11 +109,10 @@ private void DrawCharacterSetDropDown()
109109

110110
private void DrawCharactersField()
111111
{
112-
var count = _data.Characters.Length;
113112
GUILayout.BeginHorizontal();
114113
GUILayout.Label(UI.Characters, Styles.HeaderLabel);
115114
GUILayout.FlexibleSpace();
116-
GUILayout.Label(count.ToString(), count == _data.Cols * _data.Rows ? Styles.CounterLabelRight : Styles.CounterLabelWrong);
115+
GUILayout.Label(_data.ValidCharactersCount.ToString(), _data.ValidCharactersCount == _data.Cols * _data.Rows ? Styles.CounterLabelRight : Styles.CounterLabelWrong);
117116
GUILayout.EndHorizontal();
118117

119118
_charactersScrollPos = GUILayout.BeginScrollView(_charactersScrollPos, false, false, GUIStyle.none, GUI.skin.verticalScrollbar, GUILayout.Height(100));
@@ -124,7 +123,6 @@ private void DrawCharactersField()
124123
_selectedCharacterSetIndex = 0;
125124

126125
GUILayout.EndScrollView();
127-
128126
}
129127

130128
private void DrawCreateFontButton()

0 commit comments

Comments
 (0)