Skip to content

Commit 9f5df47

Browse files
committed
Merge branch 'feature/profiles' into develop
2 parents 0992b05 + 5e0ddda commit 9f5df47

22 files changed

+393
-14
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,7 @@ crashlytics-build.properties
6969

7070
# Temporary auto-generated Android Assets
7171
/[Aa]ssets/[Ss]treamingAssets/aa.meta
72-
/[Aa]ssets/[Ss]treamingAssets/aa/*
72+
/[Aa]ssets/[Ss]treamingAssets/aa/*
73+
74+
# Project specifics
75+
/[Aa]ssets/BitmapFontCreator/Resources/*

Assets/BitmapFontCreator/Editor/BitmapFontCreator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace kleberswf.tools.bitmapfontcreator
77
{
88
internal static class BitmapFontCreator
99
{
10-
public static void CreateFont(BitmapFontCreatorData data)
10+
public static void CreateFont(ExecutionData data)
1111
{
1212
var error = CheckForErrors(data);
1313
if (!string.IsNullOrEmpty(error))
@@ -32,7 +32,7 @@ public static void CreateFont(BitmapFontCreatorData data)
3232
}
3333

3434
// TODO all checks
35-
private static string CheckForErrors(BitmapFontCreatorData data)
35+
private static string CheckForErrors(ExecutionData data)
3636
{
3737
if (data.Texture == null) return "Texture cannot be null";
3838
if (!data.Texture.isReadable) return "Texture must be readable. Set Read/Write Enabled to true inside Texture Properties";
@@ -52,7 +52,7 @@ private static Material CreateMaterial(string baseName, Texture2D texture)
5252
};
5353
}
5454

55-
private static Font CreateFontAsset(string baseName, Material material, BitmapFontCreatorData data)
55+
private static Font CreateFontAsset(string baseName, Material material, ExecutionData data)
5656
{
5757
var map = new Dictionary<char, CharacterProps>();
5858
foreach (var e in data.CustomCharacterProps)
@@ -65,7 +65,7 @@ private static Font CreateFontAsset(string baseName, Material material, BitmapFo
6565
};
6666
}
6767

68-
private static CharacterInfo[] CreateCharacters(BitmapFontCreatorData data, Dictionary<char, CharacterProps> map)
68+
private static CharacterInfo[] CreateCharacters(ExecutionData data, Dictionary<char, CharacterProps> map)
6969
{
7070
var texSize = new Vector2(data.Texture.width, data.Texture.height);
7171
var cellSize = new Vector2(texSize.x / data.Cols, texSize.y / data.Rows);

Assets/BitmapFontCreator/Editor/Model/BitmapFontCreatorModel.cs

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

20-
internal struct BitmapFontCreatorData
20+
internal class BitmapFontCreatorData
2121
{
22-
public Texture2D Texture;
2322
public string Characters;
2423
public Orientation Orientation;
2524
public int Cols;
@@ -30,7 +29,32 @@ internal struct BitmapFontCreatorData
3029
public int DefaultCharacterSpacing;
3130
public List<CharacterProps> CustomCharacterProps;
3231

33-
public static BitmapFontCreatorData Default => new()
32+
public virtual void CopyTo(BitmapFontCreatorData dest)
33+
{
34+
if (dest == null) return;
35+
36+
dest.Characters = Characters;
37+
dest.Orientation = Orientation;
38+
dest.Cols = Cols;
39+
dest.Rows = Rows;
40+
dest.AlphaThreshold = AlphaThreshold;
41+
dest.Monospaced = Monospaced;
42+
dest.DefaultCharacterSpacing = DefaultCharacterSpacing;
43+
44+
if (dest.CustomCharacterProps != null) dest.CustomCharacterProps.Clear();
45+
else dest.CustomCharacterProps = new List<CharacterProps>();
46+
47+
foreach (var e in CustomCharacterProps)
48+
dest.CustomCharacterProps.Add(new CharacterProps() { Character = e.Character, Spacing = e.Spacing });
49+
}
50+
}
51+
52+
internal class ExecutionData : BitmapFontCreatorData
53+
{
54+
public Texture2D Texture;
55+
56+
// TODO move this to ui
57+
public static ExecutionData Default => new()
3458
{
3559
Texture = null,
3660
Characters = "$ 0123456789 . ",

Assets/BitmapFontCreator/Editor/Model/BitmapFontCreatorUIModel.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ internal static class UI
3030
public static readonly GUIContent AlphaThreshold = new("Alpha Threshold", "Alpha threshold to identify characters bounds");
3131
public static readonly GUIContent Monospaced = new("Monospaced", "Whether the result font should be monospaced");
3232
public static readonly GUIContent CharacterSet = new("Character Set", "Predefined character set to use");
33-
public static readonly GUIContent Characters = new("Characters", "Characters used in the font in order they appear in the texture");
33+
public static readonly GUIContent Characters = new("Characters", "Characters used in the font in order they appear in the texture. Use the space character to represent blank spaces in the texture");
3434
public static readonly GUIContent DefaultCharacterSpacing = new("Character Spacing", "Default spacing between characters");
3535
public static readonly GUIContent CustomCharacterProperties = new("Custom Character Properties", "Custom properties for each characters, if any");
3636
public static readonly GUIContent CreateFont = new("Create", "Create the font");
37+
public static readonly GUIContent Profile = new("Profile", "Manage profiles");
3738
}
3839

3940
internal static class Styles
@@ -43,12 +44,16 @@ internal static class Styles
4344
margin = new(EditorStyles.label.margin.left, EditorStyles.label.margin.right, 5, 5),
4445
};
4546

46-
public static readonly GUIStyle CreateButton = new(EditorStyles.miniButton)
47+
public static readonly GUIStyle CreateButton = new(GUI.skin.button)
4748
{
48-
fontSize = 12,
49-
padding = new(20, 20, 8, 8),
49+
padding = new(20, 20, 7, 8),
5050
margin = new(10, 10, 20, 20),
5151
fixedHeight = 32,
5252
};
53+
54+
public static readonly GUIStyle BottomMenu = new("flow background")
55+
{
56+
padding = new(3, 3, 5, 0),
57+
};
5358
}
5459
}

Assets/BitmapFontCreator/Editor/Model/Prefs.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace kleberswf.tools.bitmapfontcreator
5+
{
6+
internal class BitmapFontCreatorPrefs : ScriptableObject, ISerializationCallbackReceiver
7+
{
8+
private const string PrefsFilename = "BitmapFontCreatorPrefs";
9+
private const string PrefsFilepath = "Assets/BitmapFontCreator/Resources/" + PrefsFilename + ".asset";
10+
11+
public ProfileList Profiles;
12+
13+
public static BitmapFontCreatorPrefs Load()
14+
{
15+
var prefs = Resources.Load(PrefsFilename, typeof(BitmapFontCreatorPrefs)) as BitmapFontCreatorPrefs;
16+
if (prefs == null)
17+
{
18+
prefs = CreateInstance<BitmapFontCreatorPrefs>();
19+
AssetDatabase.CreateAsset(prefs, PrefsFilepath);
20+
EditorUtility.SetDirty(prefs);
21+
}
22+
return prefs;
23+
}
24+
25+
public void OnAfterDeserialize()
26+
{
27+
Profiles.UpdateCache();
28+
}
29+
30+
public void OnBeforeSerialize() { }
31+
}
32+
}

Assets/BitmapFontCreator/Editor/Model/Prefs/BitmapFontCreatorPrefs.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.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace kleberswf.tools.bitmapfontcreator
4+
{
5+
[Serializable]
6+
internal class Profile : BitmapFontCreatorData
7+
{
8+
public string Name;
9+
public Profile() { }
10+
11+
public Profile(string name, BitmapFontCreatorData src)
12+
{
13+
Name = name;
14+
src?.CopyTo(this);
15+
}
16+
}
17+
}

Assets/BitmapFontCreator/Editor/Model/Prefs/Profile.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.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using UnityEngine;
5+
6+
namespace kleberswf.tools.bitmapfontcreator
7+
{
8+
[Serializable]
9+
internal class ProfileList
10+
{
11+
[SerializeField] private int _selectedIndex = -1;
12+
[SerializeField] private List<Profile> _profiles = new();
13+
14+
private string[] _names = new string[0];
15+
16+
public int SelectedIndex
17+
{
18+
get => _selectedIndex;
19+
set => _selectedIndex = value;
20+
}
21+
22+
public Profile Selected
23+
{
24+
get
25+
{
26+
return _selectedIndex < 0 || _selectedIndex >= _profiles.Count
27+
? null : _profiles[_selectedIndex];
28+
}
29+
}
30+
31+
public string[] Names => _names;
32+
33+
public void UpdateCache()
34+
{
35+
var lastSelected = _selectedIndex < 0 ? null : _profiles[_selectedIndex].Name;
36+
_profiles.Sort((a, b) => a.Name.CompareTo(b.Name));
37+
_names = _profiles.Select(e => e.Name).ToArray();
38+
if (!string.IsNullOrEmpty(lastSelected)) _selectedIndex = Array.IndexOf(_names, lastSelected);
39+
}
40+
41+
public void Add(Profile data)
42+
{
43+
_profiles.Add(data);
44+
_selectedIndex = _profiles.Count - 1;
45+
UpdateCache();
46+
}
47+
48+
public bool RemoveAt(int index)
49+
{
50+
if (index < 0 || index >= _profiles.Count) return false;
51+
_profiles.RemoveAt(index);
52+
_selectedIndex = -1;
53+
UpdateCache();
54+
return true;
55+
}
56+
57+
public void Update(int index, Profile data)
58+
{
59+
if (data == null) return;
60+
if (index < 0 || index >= _profiles.Count)
61+
Add(data);
62+
else
63+
data.CopyTo(_profiles[index]);
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)