Skip to content

Commit aa6eadc

Browse files
committed
Interactivity: added CleanUp for onSelect, onHoverIn and onHoverOut to ensure this nodes exist only onces pertarget node config
1 parent c947371 commit aa6eadc

3 files changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}

Runtime/Scripts/Interactivity/Export/CleanUp/PointerEventsReductionCleanUp.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Scripts/Interactivity/Schema/GltfInteractivityNode.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ public void SetFlowOut(string socketId, GltfInteractivityNode targetNode, string
4545
socket.Node = targetNode.Index;
4646
socket.Socket = targetSocketId;
4747
}
48+
49+
public void SetFlowOut(string socketId, int targetNode, string targetSocketId)
50+
{
51+
if (!FlowConnections.TryGetValue(socketId, out var socket))
52+
{
53+
socket = new FlowSocketData();
54+
FlowConnections.Add(socketId, socket);
55+
}
56+
57+
socket.Node = targetNode;
58+
socket.Socket = targetSocketId;
59+
}
4860

4961
public virtual void SetSchema(GltfInteractivityNodeSchema schema, bool applySocketDescriptors, bool clearExistingSocketData = true)
5062
{

0 commit comments

Comments
 (0)