@@ -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		} 
0 commit comments