1- using UnityEngine ;
1+ using GBG . PlayableGraphMonitor . Editor . Node ;
2+ using System ;
3+ using System . Collections . Generic ;
4+ using UnityEngine ;
5+ using UnityEngine . Animations ;
26using UnityEngine . Playables ;
7+ using UnityEngine . UIElements ;
8+ using URandom = UnityEngine . Random ;
39
410namespace GBG . PlayableGraphMonitor . Editor . Utility
511{
612 public static class GraphTool
713 {
8- // Ensure edge is always visible.
9- private const float _colorAlphaFactor = 1f / 9 ;
10-
11-
12- public static Color GetPortColor ( PlayableOutput playableOutput )
14+ public static bool IsEqual ( ref PlayableGraph a , ref PlayableGraph b )
1315 {
14- var portColor = Color . white ;
15- portColor . a = 1 ;
16+ if ( ! a . IsValid ( ) )
17+ {
18+ return ! b . IsValid ( ) ;
19+ }
1620
17- var playableOutputType = playableOutput . GetPlayableOutputType ( ) ;
18- // todo set port color by playable output type
21+ if ( ! b . IsValid ( ) )
22+ {
23+ return false ;
24+ }
25+
26+ var nameA = a . GetEditorName ( ) ;
27+ var nameB = b . GetEditorName ( ) ;
1928
20- return portColor ;
29+ return nameA . Equals ( nameB ) ;
2130 }
2231
23- public static Color GetPortColor ( Playable playable , float weight )
32+
33+ public static void SetNodeStyle ( this GraphViewNode node , Color nodeColor ,
34+ float titleFontSize = 15 , Color ? titleColor = null )
2435 {
25- var portColor = Color . white ;
26- portColor . a = ( weight + _colorAlphaFactor ) / ( 1 + weight ) ;
36+ // title
37+ var titleLabel = node . Q ( "title-label" ) ;
38+ titleLabel . style . fontSize = titleFontSize ;
39+ titleLabel . style . color = titleColor ?? Color . black ;
40+ var titlePanel = node . Q ( "title" ) ;
41+ titlePanel . style . backgroundColor = nodeColor ;
42+ }
43+
44+
45+ // Ensure edge is always visible.
46+ public const float ColorAlphaFactor = 1f / 9 ;
2747
28- var playableType = playable . GetPlayableType ( ) ;
29- // todo set port color by playable type
3048
31- return portColor ;
49+ public static Color GetPortColor ( float weight )
50+ {
51+ var alpha = ( weight + ColorAlphaFactor ) / ( 1 + weight ) ;
52+ return new Color ( 1 , 1 , 1 , alpha ) ;
53+ }
54+
55+ public static Color GetNodeInvalidColor ( )
56+ {
57+ return Color . red ;
3258 }
3359
34- public static Color GetPortInvalidColor ( float weight )
60+ public static Color GetPlayableOutputNodeColor ( this ref PlayableOutput playableOutput )
3561 {
36- var portColor = Color . red ;
37- portColor . a = ( weight + _colorAlphaFactor ) / ( 1 + weight ) ;
62+ if ( playableOutput . IsPlayableOutputOfType < AnimationPlayableOutput > ( ) )
63+ {
64+ return new Color32 ( 0 , 255 , 255 , 255 ) ;
65+ }
66+
67+ if ( playableOutput . IsPlayableOutputOfType < ScriptPlayableOutput > ( ) )
68+ {
69+ return new Color32 ( 0 , 204 , 255 , 255 ) ;
70+ }
3871
39- return portColor ;
72+
73+ return GetRandomColorForType ( playableOutput . GetPlayableOutputType ( ) ) ;
4074 }
4175
42- public static bool IsEqual ( ref PlayableGraph a , ref PlayableGraph b )
76+ public static Color GetPlayableNodeColor ( this ref Playable playable )
4377 {
44- if ( ! a . IsValid ( ) )
78+ if ( playable . IsPlayableOfType < AnimationClipPlayable > ( ) )
4579 {
46- return ! b . IsValid ( ) ;
80+ return new Color32 ( 0 , 255 , 102 , 255 ) ;
4781 }
4882
49- if ( ! b . IsValid ( ) )
83+ if ( playable . IsPlayableOfType < AnimationMixerPlayable > ( ) )
5084 {
51- return false ;
85+ return new Color32 ( 0 , 255 , 153 , 255 ) ;
5286 }
5387
54- var nameA = a . GetEditorName ( ) ;
55- var nameB = b . GetEditorName ( ) ;
88+ if ( playable . IsPlayableOfType < AnimationLayerMixerPlayable > ( ) )
89+ {
90+ return new Color32 ( 0 , 255 , 204 , 255 ) ;
91+ }
5692
57- return nameA . Equals ( nameB ) ;
93+ return GetRandomColorForType ( playable . GetPlayableType ( ) ) ;
94+ }
95+
96+ public static Color GetRandomColorForType ( this Type type )
97+ {
98+ if ( _colorCache . TryGetValue ( type , out var color ) )
99+ {
100+ return color ;
101+ }
102+
103+ color = ColorPool [ URandom . Range ( 0 , ColorPool . Count ) ] ;
104+ _colorCache [ type ] = color ;
105+
106+ return color ;
107+ }
108+
109+
110+ public static readonly IReadOnlyList < Color32 > ColorPool = new Color32 [ ]
111+ {
112+ new Color32 ( 255 , 0 , 255 , 255 ) , new Color32 ( 255 , 0 , 153 , 255 ) ,
113+ new Color32 ( 153 , 255 , 0 , 255 ) , new Color32 ( 204 , 255 , 0 , 255 ) ,
114+ new Color32 ( 255 , 255 , 0 , 255 ) , new Color32 ( 255 , 204 , 0 , 255 ) ,
115+ new Color32 ( 255 , 153 , 0 , 255 ) , new Color32 ( 255 , 102 , 0 , 255 ) ,
116+ new Color32 ( 153 , 204 , 255 , 255 ) , new Color32 ( 153 , 255 , 102 , 255 ) ,
117+ new Color32 ( 153 , 255 , 153 , 255 ) , new Color32 ( 153 , 255 , 204 , 255 ) ,
118+ new Color32 ( 204 , 255 , 102 , 255 ) , new Color32 ( 204 , 255 , 255 , 255 ) ,
119+ } ;
120+
121+ private static readonly Dictionary < Type , Color32 > _colorCache = new Dictionary < Type , Color32 > ( ) ;
122+
123+
124+ public static void ClearGlobalCache ( )
125+ {
126+ _colorCache . Clear ( ) ;
58127 }
59128 }
60129}
0 commit comments