-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathFontImporter.cs
192 lines (158 loc) · 8.47 KB
/
FontImporter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// This file is part of the Prowl Game Engine
// Licensed under the MIT License. See the LICENSE file in the project root for details.
using Prowl.Editor.Preferences;
using Prowl.Runtime;
using Prowl.Runtime.GUI;
using Prowl.Runtime.GUI.Layout;
using Prowl.Runtime.Utils;
namespace Prowl.Editor.Assets;
[Importer("FileIcon.png", typeof(Font), ".ttf")]
public class FontImporter : ScriptedImporter
{
public List<Font.CharacterRange> characterRanges = [Font.CharacterRange.BasicLatin];
public float fontSize = 20;
public int width = 1024;
public int height = 1024;
public override void Import(SerializedAsset ctx, FileInfo assetPath)
{
var ttf = File.ReadAllBytes(assetPath.FullName);
ctx.SetMainObject(Font.CreateFromTTFMemory(ttf, fontSize, width, height, characterRanges.ToArray()));
}
}
[CustomEditor(typeof(FontImporter))]
public class FontEditor : ScriptedEditor
{
// static int start, end;
private readonly int numberOfProperties = 0;
public void InputFloat(string name, ref float val)
{
double ItemSize = EditorStylePrefs.Instance.ItemSize;
using (gui.Node("f" + name, numberOfProperties).ExpandWidth().Height(ItemSize).Layout(LayoutType.Row).ScaleChildren().Enter())
{
// Label
using (gui.Node("#_Label").ExpandHeight().Clip().Enter())
{
var pos = gui.CurrentNode.LayoutData.Rect.Min;
pos.x += 28;
pos.y += 5;
gui.Draw2D.DrawText(name, pos, Color.white);
}
// Value
using (gui.Node("#_Value").ExpandHeight().Enter())
EditorGUI.DrawProperty(0, name, ref val);
}
}
public void InputInt<T>(string name, ref T val) where T : struct
{
double ItemSize = EditorStylePrefs.Instance.ItemSize;
using (gui.Node("f" + name, numberOfProperties).ExpandWidth().Height(ItemSize).Layout(LayoutType.Row).ScaleChildren().Enter())
{
// Label
using (gui.Node("#_Label").ExpandHeight().Clip().Enter())
{
var pos = gui.CurrentNode.LayoutData.Rect.Min;
pos.x += 28;
pos.y += 5;
gui.Draw2D.DrawText(name, pos, Color.white);
}
// Value
using (gui.Node("#_Value").ExpandHeight().Enter())
EditorGUI.DrawProperty(0, name, ref val);
}
}
public bool QuickButton(string label, LayoutNode? popupHolder)
{
double ItemSize = EditorStylePrefs.Instance.ItemSize;
using (gui.Node(label).ExpandWidth().Height(ItemSize).Enter())
{
gui.Draw2D.DrawRectFilled(gui.CurrentNode.LayoutData.Rect, EditorStylePrefs.Instance.LesserText * 0.8f, (float)EditorStylePrefs.Instance.ButtonRoundness);
gui.Draw2D.DrawText(label, gui.CurrentNode.LayoutData.Rect, Color.white);
if (gui.IsNodePressed())
{
gui.Draw2D.DrawRectFilled(gui.CurrentNode.LayoutData.Rect, EditorStylePrefs.Instance.Highlighted, (float)EditorStylePrefs.Instance.ButtonRoundness);
if (popupHolder != null)
gui.ClosePopup(popupHolder);
return true;
}
if (gui.IsNodeHovered())
gui.Draw2D.DrawRectFilled(gui.CurrentNode.LayoutData.Rect, EditorStylePrefs.Instance.Hovering, (float)EditorStylePrefs.Instance.ButtonRoundness);
return false;
}
}
public override void OnInspectorGUI()
{
double ItemSize = EditorStylePrefs.Instance.ItemSize;
var importer = (FontImporter)(target as MetaFile).Importer;
gui.CurrentNode.Layout(LayoutType.Column);
InputFloat("Font Size", ref importer.fontSize);
InputInt("Width", ref importer.width);
InputInt("Height", ref importer.height);
using (gui.Node("Ranges").ExpandWidth().FitContentHeight().Layout(LayoutType.Column).Enter())
{
gui.Draw2D.DrawRectFilled(gui.CurrentNode.LayoutData.Rect, EditorStylePrefs.Instance.WindowBGOne * 0.8f, (float)EditorStylePrefs.Instance.WindowRoundness);
int rangeIndex = 0;
foreach (var range in importer.characterRanges)
{
using (gui.Node("DelRange" + rangeIndex++).Scale(ItemSize).Enter())
{
gui.Draw2D.DrawRectFilled(gui.CurrentNode.LayoutData.Rect, EditorStylePrefs.Instance.LesserText * 0.8f, (float)EditorStylePrefs.Instance.ButtonRoundness);
gui.Draw2D.DrawText("X", gui.CurrentNode.LayoutData.GlobalContentPosition + new Vector2(8, 8), Color.white);
if (gui.IsNodeHovered())
gui.Draw2D.DrawRectFilled(gui.CurrentNode.LayoutData.Rect, EditorStylePrefs.Instance.Hovering, (float)EditorStylePrefs.Instance.ButtonRoundness);
if (gui.IsNodePressed())
{
importer.characterRanges.Remove(range);
}
var pos = new Vector2(gui.CurrentNode.LayoutData.Rect.Right, ((gui.CurrentNode.LayoutData.Rect.Top + gui.CurrentNode.LayoutData.Rect.Bottom) / 2) - 15);
gui.Draw2D.DrawText($"{range.Start:X} - {range.End:X}", pos + new Vector2(8, 8), Color.white);
}
}
}
using (gui.Node("AddRange").ExpandWidth().Height(ItemSize).Enter())
{
gui.Draw2D.DrawRectFilled(gui.CurrentNode.LayoutData.Rect, EditorStylePrefs.Instance.LesserText * 0.8f, (float)EditorStylePrefs.Instance.ButtonRoundness);
gui.Draw2D.DrawText("Add Range", gui.CurrentNode.LayoutData.Rect, Color.white);
if (gui.IsNodeHovered())
gui.Draw2D.DrawRectFilled(gui.CurrentNode.LayoutData.Rect, EditorStylePrefs.Instance.Hovering, (float)EditorStylePrefs.Instance.ButtonRoundness);
if (gui.IsNodePressed())
gui.OpenPopup("AddRangePopup");
if (gui.BeginPopup("AddRangePopup", out var popupNode))
{
using (popupNode.Width(250).FitContentHeight().Layout(LayoutType.Column).Padding(5).Enter())
{
if (QuickButton("Add Basic Latin", popupNode.Parent))
importer.characterRanges.Add(Font.CharacterRange.BasicLatin);
if (QuickButton("Add Latin1 Supplement", popupNode.Parent))
importer.characterRanges.Add(Font.CharacterRange.Latin1Supplement);
if (QuickButton("Add Latin Extended A", popupNode.Parent))
importer.characterRanges.Add(Font.CharacterRange.LatinExtendedA);
if (QuickButton("Add Latin Extended B", popupNode.Parent))
importer.characterRanges.Add(Font.CharacterRange.LatinExtendedB);
if (QuickButton("Add Cyrillic", popupNode.Parent))
importer.characterRanges.Add(Font.CharacterRange.Cyrillic);
if (QuickButton("Add Cyrillic Supplement", popupNode.Parent))
importer.characterRanges.Add(Font.CharacterRange.CyrillicSupplement);
if (QuickButton("Add Hiragana", popupNode.Parent))
importer.characterRanges.Add(Font.CharacterRange.Hiragana);
if (QuickButton("Add Katakana", popupNode.Parent))
importer.characterRanges.Add(Font.CharacterRange.Katakana);
if (QuickButton("Add Greek", popupNode.Parent))
importer.characterRanges.Add(Font.CharacterRange.Greek);
if (QuickButton("Add Cjk Symbols And Punctuation", popupNode.Parent))
importer.characterRanges.Add(Font.CharacterRange.CjkSymbolsAndPunctuation);
if (QuickButton("Add Cjk Unified Ideographs", popupNode.Parent))
importer.characterRanges.Add(Font.CharacterRange.CjkUnifiedIdeographs);
if (QuickButton("Add Hangul Compatibility Jamo", popupNode.Parent))
importer.characterRanges.Add(Font.CharacterRange.HangulCompatibilityJamo);
if (QuickButton("Add Hangul Syllables", popupNode.Parent))
importer.characterRanges.Add(Font.CharacterRange.HangulSyllables);
}
}
}
if (QuickButton("Save", null))
{
(target as MetaFile).Save();
AssetDatabase.Reimport((target as MetaFile).AssetPath);
}
}
}