1+ using System . Linq ;
2+ using UnityGLTF . Interactivity . Schema ;
3+
4+ namespace UnityGLTF . Interactivity . Export
5+ {
6+ public class PointerEventsReductionCleanUp : ICleanUp
7+ {
8+ #if UNITY_EDITOR
9+ [ UnityEditor . InitializeOnLoadMethod ]
10+ #else
11+ [ RuntimeInitializeOnLoadMethod ]
12+ #endif
13+ private static void Register ( )
14+ {
15+ CleanUpRegistry . RegisterCleanUp ( new PointerEventsReductionCleanUp ( ) ) ;
16+ }
17+
18+ public void OnCleanUp ( CleanUpTask task )
19+ {
20+ var onSelectGroups = task . context . Nodes . FindAll (
21+ node => node . Schema is Event_OnSelectNode )
22+ . GroupBy ( n => ( int ) n . Configuration [ "nodeIndex" ] . Value ) . Where ( g => g . Count ( ) > 1 ) . ToArray ( ) ;
23+
24+ Reduce ( onSelectGroups ) ;
25+
26+ var onHoverInGroups = task . context . Nodes . FindAll (
27+ node => node . Schema is Event_OnHoverInNode )
28+ . GroupBy ( n => ( int ) n . Configuration [ "nodeIndex" ] . Value ) . Where ( g => g . Count ( ) > 1 ) . ToArray ( ) ;
29+
30+ Reduce ( onHoverInGroups ) ;
31+
32+ var onHoverOutGroups = task . context . Nodes . FindAll (
33+ node => node . Schema is Event_OnHoverOutNode )
34+ . GroupBy ( n => ( int ) n . Configuration [ "nodeIndex" ] . Value ) . Where ( g => g . Count ( ) > 1 ) . ToArray ( ) ;
35+
36+ Reduce ( onHoverOutGroups ) ;
37+
38+ void Reduce ( IGrouping < int , GltfInteractivityExportNode > [ ] nodeGroup )
39+ {
40+ foreach ( var group in nodeGroup )
41+ {
42+ var first = group . First ( ) ;
43+ var firstTargetNode = first . FlowOut ( ) . socket . Value . Node ;
44+ var firstTargetSocket = first . FlowOut ( ) . socket . Value . Socket ;
45+
46+ var sequence = new GltfInteractivityExportNode < Flow_SequenceNode > ( ) ;
47+ sequence . Index = task . context . Nodes . Count ;
48+ task . context . Nodes . Add ( sequence ) ;
49+
50+ first . FlowOut ( ) . ConnectToFlowDestination ( sequence . FlowIn ( ) ) ;
51+
52+ if ( firstTargetNode != null )
53+ sequence . SetFlowOut ( "s" + sequence . FlowConnections . Count . ToString ( ) , firstTargetNode . Value ,
54+ firstTargetSocket ) ;
55+
56+ var groupNodes = group . Skip ( 1 ) . ToArray ( ) ;
57+ foreach ( var node in groupNodes )
58+ {
59+ var targetNode = node . FlowOut ( ) . socket . Value . Node ;
60+ var targetSocket = node . FlowOut ( ) . socket . Value . Socket ;
61+ if ( targetNode != null )
62+ sequence . SetFlowOut ( "s" + sequence . FlowConnections . Count . ToString ( ) , targetNode . Value ,
63+ targetSocket ) ;
64+ }
65+
66+ // Rewire all value connections to the first node, because all others in this group will be removed
67+ foreach ( var n in task . context . Nodes )
68+ {
69+ foreach ( var value in n . ValueInConnection )
70+ {
71+ if ( value . Value . Node != null && groupNodes . Any ( gn => gn . Index == value . Value . Node . Value ) )
72+ value . Value . Node = first . Index ;
73+ }
74+ }
75+
76+ foreach ( var node in groupNodes )
77+ {
78+ task . RemoveNode ( node ) ;
79+ }
80+
81+ }
82+ }
83+
84+ }
85+ }
86+ }
0 commit comments