1+ using BP . TextMotionPro ;
12using System ;
23using System . Collections . Generic ;
34using System . Linq ;
45using UnityEditor ;
6+ using UnityEditor . UIElements ;
57using UnityEngine ;
8+ using UnityEngine . UIElements ;
69
7- namespace BP . TMPA . Internal
10+ namespace BP . TextMotionProEditor
811{
9- [ CustomEditor ( typeof ( TextEffectsProfile ) ) ]
12+ [ CustomEditor ( typeof ( MotionProfile ) ) ]
1013 public class TextEffectsProfileEditor : Editor
1114 {
12- private TextEffectsProfile profile ;
15+ [ SerializeField ] private VisualTreeAsset mainTreeAsset ;
16+ [ SerializeField ] private VisualTreeAsset effectTreeAsset ;
17+
18+ private MotionProfile profile ;
1319
1420 private void OnEnable ( )
1521 {
16- profile = ( TextEffectsProfile ) target ;
22+ profile = ( MotionProfile ) target ;
1723 }
1824
19- public override void OnInspectorGUI ( )
25+ public override VisualElement CreateInspectorGUI ( )
2026 {
21- base . OnInspectorGUI ( ) ;
22- EditorGUI . BeginChangeCheck ( ) ;
27+ var root = new VisualElement ( ) ;
28+ mainTreeAsset . CloneTree ( root ) ;
29+
30+ var effectsList = root . Q ( "effects-list" ) ;
31+ UpdateEffectsList ( effectsList ) ;
2332
24- var effects = profile . textEffects ;
25- for ( int i = 0 ; i < effects . Count ; i ++ )
33+ // Setup the add button functionality.
34+ var addButton = root . Q < Button > ( "add-button" ) ;
35+ addButton . clicked += ( ) =>
36+ {
37+ var menu = new GenericMenu ( ) ;
38+ var availableTypes = GetAvailableEffectTypes ( ) ;
39+ foreach ( var effectTypeName in availableTypes )
40+ {
41+ string menuLabel = Type . GetType ( effectTypeName ) . Name ;
42+ menu . AddItem ( new GUIContent ( menuLabel ) , false , ( ) =>
43+ {
44+ AddEffect ( effectTypeName ) ;
45+ UpdateEffectsList ( effectsList ) ;
46+ } ) ;
47+ }
48+ menu . ShowAsContext ( ) ;
49+ } ;
50+
51+ return root ;
52+ }
53+
54+ private void UpdateEffectsList ( VisualElement parent )
55+ {
56+ parent . Clear ( ) ;
57+ foreach ( var effect in profile . textEffects )
2658 {
27- var effect = effects [ i ] ;
2859 if ( effect == null ) continue ;
2960
30- EditorGUI . indentLevel ++ ;
61+ var serObject = new SerializedObject ( effect ) ;
62+ var foldedProp = serObject . FindProperty ( "isFolded" ) ;
3163
32- // Create a custom editor for each component type
33- var effectEditor = CreateEditor ( effect ) ;
34- if ( effectEditor != null )
64+ var entry = effectTreeAsset . CloneTree ( ) ;
65+ var effectLabel = entry . Q < Label > ( "label" ) ;
66+ effectLabel . text = effect . GetType ( ) . Name ;
67+
68+ var content = entry . Q ( "content" ) ;
69+ content . style . display = foldedProp . boolValue ? DisplayStyle . None : DisplayStyle . Flex ;
70+ content . TrackPropertyValue ( foldedProp , ( prop ) =>
3571 {
36- effectEditor . OnInspectorGUI ( ) ;
37- DestroyImmediate ( effectEditor ) ;
38- }
72+ content . style . display = prop . boolValue ? DisplayStyle . None : DisplayStyle . Flex ;
73+ } ) ;
3974
40- // Add remove button for the component
41- if ( GUILayout . Button ( "Remove Component" ) )
75+ DrawEditor ( content , effect ) ;
76+
77+ // Clicking the label toggles folding.
78+ effectLabel . RegisterCallback < ClickEvent > ( evt =>
79+ {
80+ foldedProp . boolValue = ! foldedProp . boolValue ;
81+ serObject . ApplyModifiedProperties ( ) ;
82+ } ) ;
83+
84+ // Setup the options button to open a context menu with removal.
85+ var optionsButton = entry . Q < ToolbarButton > ( "options-button" ) ;
86+ if ( optionsButton != null )
4287 {
43- RemoveEffect ( effect . GetType ( ) ) ;
44- break ;
88+ optionsButton . clicked += ( ) =>
89+ {
90+ var menu = new GenericMenu ( ) ;
91+ menu . AddItem ( new GUIContent ( "Remove" ) , false , ( ) =>
92+ {
93+ RemoveEffect ( effect . GetType ( ) ) ;
94+ UpdateEffectsList ( parent ) ;
95+ } ) ;
96+ menu . ShowAsContext ( ) ;
97+ } ;
4598 }
4699
47- EditorGUI . indentLevel -- ;
100+ parent . Add ( entry ) ;
48101 }
102+ }
49103
50- EditorGUILayout . Space ( ) ;
104+ private void DrawEditor ( VisualElement root , UnityEngine . Object target )
105+ {
106+ var editor = CreateEditor ( target ) ;
107+ if ( editor == null ) return ;
51108
52- var effectTypes = GetAvailableEffectTypes ( ) ;
53- int selectedEffectIndex = EditorGUILayout . Popup ( "Add Effect" , - 1 , effectTypes ) ;
109+ var editorContentElement = editor . CreateInspectorGUI ( ) ;
54110
55- if ( selectedEffectIndex >= 0 )
111+ if ( editorContentElement != null )
56112 {
57- Type selectedEffectType = Type . GetType ( effectTypes [ selectedEffectIndex ] ) ;
58- if ( selectedEffectType != null )
113+ root . Add ( editorContentElement ) ;
114+ }
115+ else
116+ {
117+ var imgui = new IMGUIContainer ( ( ) =>
59118 {
60- AddEffect ( selectedEffectType ) ;
61- }
119+ editor . OnInspectorGUI ( ) ;
120+ } ) ;
121+ root . Add ( imgui ) ;
62122 }
123+ DestroyImmediate ( editor ) ;
63124 }
64125
65- private string [ ] GetAvailableEffectTypes ( ) => TextEffectRegistry . TextEffects . Where ( x => ! profile . HasTextEffect ( x . Type ) ) . Select ( x => x . Type . FullName ) . ToArray ( ) ;
66-
126+ private string [ ] GetAvailableEffectTypes ( ) => TextEffectRegistry . TextEffects
127+ . Where ( x => ! profile . HasTextEffect ( x . Type ) )
128+ . Select ( x => x . Type . AssemblyQualifiedName )
129+ . ToArray ( ) ;
67130
68131 private void RemoveEffect ( Type type )
69132 {
@@ -73,8 +136,11 @@ private void RemoveEffect(Type type)
73136 UpdateAssets ( ) ;
74137 }
75138
76- private void AddEffect ( Type effectType )
139+ private void AddEffect ( string effectTypeName )
77140 {
141+ Type effectType = Type . GetType ( effectTypeName ) ;
142+ if ( effectType == null ) return ;
143+
78144 Undo . RecordObject ( profile , "Added Effect" ) ;
79145 profile . AddTextEffect ( effectType ) ;
80146 EditorUtility . SetDirty ( profile ) ;
@@ -83,10 +149,10 @@ private void AddEffect(Type effectType)
83149
84150 public void UpdateAssets ( )
85151 {
86- //Path of the main file
152+ // Path of the main file.
87153 string path = AssetDatabase . GetAssetPath ( profile ) ;
88154
89- //Gets all components
155+ // Gets all components.
90156 var effects = profile . GetAllTextEffects ( ) ;
91157 var currentSubAssets = AssetDatabase . LoadAllAssetRepresentationsAtPath ( path ) ;
92158 var componentsToRemove = new List < UnityEngine . Object > ( currentSubAssets ) ;
0 commit comments