Skip to content

Commit f7201e9

Browse files
committed
Added: Guess rows and cols based on the texture
1 parent 9f7eea2 commit f7201e9

File tree

3 files changed

+99
-4
lines changed

3 files changed

+99
-4
lines changed

Editor/BitmapFontCreator.cs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ private static CharacterInfo[] CreateCharacters(ExecutionData data, Dictionary<c
8383
int xMin, xMax, advance;
8484
int largestAdvance = 0;
8585

86-
// horizontal
8786
for (var row = 0; row < data.Rows; row++)
8887
{
8988
for (var col = 0; col < data.Cols; col++)
@@ -174,5 +173,67 @@ private static void CreateOrReplaceAsset<T>(T asset, string path) where T : Obje
174173
AssetDatabase.SaveAssets();
175174
}
176175
}
176+
177+
public static Vector2Int GuessRowsAndCols(Texture2D tex)
178+
{
179+
var rows = 0;
180+
var cols = 0;
181+
182+
uint state = 0; // 0 = looking for not transparent, 1 = looking for transparent
183+
bool foundNonTransparentPixel;
184+
185+
for (var x = 0; x < tex.width; x++)
186+
{
187+
foundNonTransparentPixel = false;
188+
for (var y = 0; y < tex.height; y++)
189+
{
190+
if (tex.GetPixel(x, y).a == 0) continue;
191+
foundNonTransparentPixel = true;
192+
break;
193+
}
194+
195+
if (state == 0)
196+
{
197+
if (foundNonTransparentPixel)
198+
{
199+
state = 1;
200+
cols++;
201+
}
202+
}
203+
else
204+
{
205+
if (!foundNonTransparentPixel)
206+
state = 0;
207+
}
208+
}
209+
210+
state = 0;
211+
for (var y = 0; y < tex.height; y++)
212+
{
213+
foundNonTransparentPixel = false;
214+
for (var x = 0; x < tex.width; x++)
215+
{
216+
if (tex.GetPixel(x, y).a == 0) continue;
217+
foundNonTransparentPixel = true;
218+
break;
219+
}
220+
221+
if (state == 0)
222+
{
223+
if (foundNonTransparentPixel)
224+
{
225+
state = 1;
226+
rows++;
227+
}
228+
}
229+
else
230+
{
231+
if (!foundNonTransparentPixel)
232+
state = 0;
233+
}
234+
}
235+
236+
return new(rows, cols);
237+
}
177238
}
178239
}

Editor/UI/BitmapFontCreatorEditor.cs

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

20+
private static Vector2Int _guessCache;
21+
2022
[MenuItem(MenuItemPath)]
2123
public static void ShowWindow()
2224
{
23-
var size = new Vector2(300, 550);
25+
var size = new Vector2(300, 580);
2426
var window = GetWindowWithRect<BitmapFontCreatorEditor>(
2527
new Rect((Screen.width - size.x) * 0.5f, (Screen.height - size.y) * 0.5f, size.x, size.y),
2628
false,
@@ -52,11 +54,18 @@ private void OnGUI()
5254
_mainScrollPos = GUILayout.BeginScrollView(_mainScrollPos, false, false, GUIStyle.none, GUI.skin.verticalScrollbar, GUILayout.ExpandHeight(true));
5355
GUILayout.BeginVertical();
5456

55-
_data.Texture = EditorGUILayout.ObjectField(UI.Texture, _data.Texture, typeof(Texture2D), false) as Texture2D;
56-
_data.Orientation = (Orientation)EditorGUILayout.EnumPopup(UI.Orientation, _data.Orientation);
57+
DrawTextureField();
5758

5859
_data.Cols = EditorGUILayout.IntField(UI.Cols, _data.Cols);
5960
_data.Rows = EditorGUILayout.IntField(UI.Rows, _data.Rows);
61+
62+
GUILayout.BeginHorizontal();
63+
GUILayout.Space(EditorGUIUtility.labelWidth);
64+
if (GUILayout.Button(UI.GuessButton)) GuessRowsAndCols();
65+
GUILayout.EndHorizontal();
66+
67+
EditorGUILayout.Space();
68+
_data.Orientation = (Orientation)EditorGUILayout.EnumPopup(UI.Orientation, _data.Orientation);
6069
_data.AlphaThreshold = EditorGUILayout.Slider(UI.AlphaThreshold, _data.AlphaThreshold, 0f, 1f);
6170
_data.Monospaced = EditorGUILayout.Toggle(UI.Monospaced, _data.Monospaced);
6271

@@ -82,6 +91,14 @@ private void OnGUI()
8291
DrawBottomMenu();
8392
}
8493

94+
private void DrawTextureField()
95+
{
96+
EditorGUI.BeginChangeCheck();
97+
_data.Texture = EditorGUILayout.ObjectField(UI.Texture, _data.Texture, typeof(Texture2D), false) as Texture2D;
98+
if (EditorGUI.EndChangeCheck())
99+
_guessCache = Vector2Int.zero;
100+
}
101+
85102
private void DrawCharacterSetDropDown()
86103
{
87104
_selectedCharacterSetIndex = EditorGUILayout.Popup(UI.CharacterSet, _selectedCharacterSetIndex, CharacterSets.Names);
@@ -140,5 +157,21 @@ private void RollbackSettings()
140157
var profile = (BitmapFontCreatorData)_settings.Profiles.Selected ?? ExecutionData.Default;
141158
profile.CopyTo(_data);
142159
}
160+
161+
private void GuessRowsAndCols()
162+
{
163+
if (_data.Texture == null)
164+
{
165+
Debug.LogWarning("Texture cannot be null");
166+
return;
167+
}
168+
169+
_guessCache = _guessCache.x == 0 || _guessCache.y == 0
170+
? BitmapFontCreator.GuessRowsAndCols(_data.Texture)
171+
: _guessCache;
172+
173+
_data.Cols = _guessCache.y;
174+
_data.Rows = _guessCache.x;
175+
}
143176
}
144177
}

Editor/UI/UIModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ internal static class UI
2727
public static readonly GUIContent Orientation = new("Orientation", "Order to look up for characters in the texture");
2828
public static readonly GUIContent Cols = new("Cols", "Number of columns in the texture");
2929
public static readonly GUIContent Rows = new("Rows", "Number of rows in the texture");
30+
public static readonly GUIContent GuessButton = new("Guess", "Guess Rows and Cols");
3031
public static readonly GUIContent AlphaThreshold = new("Alpha Threshold", "Alpha threshold to identify characters bounds");
3132
public static readonly GUIContent Monospaced = new("Monospaced", "Whether the result font should be monospaced");
3233
public static readonly GUIContent CharacterSet = new("Character Set", "Predefined character set to use");

0 commit comments

Comments
 (0)