public override bool Connect(Port port) { if (!port.IsStatePort) { return(false); } if (object.ReferenceEquals(port, this)) { return(false); } if (port.IsStateRootPort) { return(port.Connect(this)); } StatePort source = this; StatePort target = (StatePort)port; Connection existingTransition = connections.Find(t => object.ReferenceEquals(t.SourcePort, source) && object.ReferenceEquals(t.TargetPort, target)); if (existingTransition != null) { existingTransition.Disconnect(); return(true); } StateMachineChip targetRoot; StateMachineChip sourceRoot; { if (source.IsStateRootPort) { sourceRoot = (StateMachineChip)source.Node; } else { sourceRoot = ((StateChip)source.Node).StateMachine; } targetRoot = ((StateChip)target.Node).StateMachine; } if (sourceRoot != null && targetRoot != null && !object.ReferenceEquals(sourceRoot, targetRoot)) { return(false); } if (source.IsStateRootPort) { source.DisconnectAll(); } StateMachineTransition transition = new StateMachineTransition(source, target); connections.Add(transition); port.connections.Add(transition); EmitConnected(transition); return(true); }
private void StatePort_Disconnected(Connection connection) { if (!object.ReferenceEquals(connection.SourcePort, this)) { return; } StateMachineTransition transition = (StateMachineTransition)connection; StateMachineChip stateMachine; StateChip targetState = (StateChip)transition.TargetStatePort.Node; targetState.Active = false; if (transition.SourceStatePort.IsStateRootPort) { stateMachine = (StateMachineChip)transition.SourceStatePort.Node; } else { StateChip sourceState = (StateChip)transition.SourceStatePort.Node; stateMachine = sourceState.StateMachineUnchecked; } DebugUtils.Assert(object.ReferenceEquals(stateMachine, targetState.StateMachineUnchecked)); if (stateMachine == null) { if (!transition.SourceStatePort.IsStateRootPort) { ((StateChip)transition.SourceStatePort.Node).CheckStateMachine(); } targetState.CheckStateMachine(); return; } if (transition.TargetStatePort.connections.Count > 0) { stateMachine.UpdateConnectedStates(); } else { stateMachine.connectedStates.Remove(targetState); targetState.StateMachine = null; } if (!transition.SourceStatePort.IsStateRootPort) { ((StateChip)transition.SourceStatePort.Node).CheckStateMachine(); } targetState.CheckStateMachine(); }
public void Fill() { List <CircuitNode> nodes = RRCSManager.Instance.circuitManager.Nodes; graph = new StorageNode[nodes.Count]; for (int i = 0; i < graph.Length; i++) { CircuitNode node = nodes[i]; DebugUtils.Assert(node.RingEvaluationPriority == i); StorageNode storageNode = new StorageNode(); NodeUi ui = UiManager.GetUi(node); storageNode.uiType = ui.GetType().FullName; storageNode.typeParams = ui.GetParams(); storageNode.position = ui.transform.position; storageNode.settings = new NodeSettingContainer[node.settings.Length]; for (int j = 0; j < storageNode.settings.Length; j++) { NodeSetting setting = node.settings[j]; storageNode.settings[j] = new NodeSettingContainer { type = setting.type, value = setting.currentValue.ToString() }; } graph[i] = storageNode; } for (int nodeIndex = 0; nodeIndex < graph.Length; nodeIndex++) { CircuitNode node = nodes[nodeIndex]; StorageNode storageNode = graph[nodeIndex]; storageNode.connections = new NodeConnection[node.inputPorts.Length]; for (int portIndex = 0; portIndex < node.inputPorts.Length; portIndex++) { InputPort port = node.inputPorts[portIndex]; if (port.IsConnected) { NodeConnection connection = new NodeConnection(); OutputPort connectedPort = ((DataConnection)port.connections[0]).SourceDataPort; connection.nodeIndex = connectedPort.RingEvaluationPriority; connection.portIndex = Array.IndexOf(connectedPort.Node.outputPorts, connectedPort); storageNode.connections[portIndex] = connection; } } } for (int nodeIndex = 0; nodeIndex < graph.Length; nodeIndex++) { CircuitNode node = nodes[nodeIndex]; StorageNode storageNode = graph[nodeIndex]; if (node.statePort != null) { List <StateMachineTransition> outgoingTransitions = node.statePort.GetOutgoingTransitions().ToList(); storageNode.transitions = new NodeTransition[outgoingTransitions.Count]; for (int transitionIndex = 0; transitionIndex < storageNode.transitions.Length; transitionIndex++) { StateMachineTransition outgoingTransition = outgoingTransitions[transitionIndex]; NodeTransition transition = new NodeTransition { sourceNodeIndex = outgoingTransition.SourceStatePort.RingEvaluationPriority, targetNodeIndex = outgoingTransition.TargetStatePort.RingEvaluationPriority }; if (outgoingTransition.TransitionEnabledPort != null && outgoingTransition.TransitionEnabledPort.IsConnected) { OutputPort connectedPort = ((DataConnection)outgoingTransition.TransitionEnabledPort.connections[0]).SourceDataPort; NodeConnection connection = new NodeConnection { nodeIndex = connectedPort.RingEvaluationPriority, portIndex = Array.IndexOf(connectedPort.Node.outputPorts, connectedPort) }; transition.transitionEnabledConnection = connection; } storageNode.transitions[transitionIndex] = transition; } } else { storageNode.transitions = new NodeTransition[0]; } } }
public IEnumerator Restore(RRCSManager manager) { Dictionary <string, GameObject> typeMap = new Dictionary <string, GameObject>(); typeMap.Add(typeof(RRButtonUi).FullName, manager.NodeUiPrefabRoot.GetComponentInChildren <RRButtonUi>(true).gameObject); typeMap.Add(typeof(StageLightUi).FullName, manager.NodeUiPrefabRoot.GetComponentInChildren <StageLightUi>(true).gameObject); typeMap.Add(typeof(SignUi).FullName, manager.NodeUiPrefabRoot.GetComponentInChildren <SignUi>(true).gameObject); var chipPrefabs = manager.NodeUiPrefabRoot.GetComponentsInChildren <ChipUi>(true); foreach (var prefab in chipPrefabs) { typeMap.Add(Enum.GetName(typeof(ChipUi.ChipType), prefab.type), prefab.gameObject); } var gizmoPrefabs = manager.NodeUiPrefabRoot.GetComponentsInChildren <GizmoUi>(true); foreach (var prefab in gizmoPrefabs) { typeMap.Add(Enum.GetName(typeof(GizmoUi.GizmoType), prefab.type), prefab.gameObject); } Dictionary <NodeSetting.SettingType, int> settingsTypeIndex = new Dictionary <NodeSetting.SettingType, int>(); foreach (StorageNode storageNode in graph) { settingsTypeIndex.Clear(); var typeKey = storageNode.uiType; if (typeKey == typeof(ChipUi).FullName) { typeKey = storageNode.typeParams; } if (typeKey == typeof(GizmoUi).FullName) { typeKey = storageNode.typeParams; } if (!typeMap.ContainsKey(typeKey)) { throw new InvalidOperationException("Invalid node type " + typeKey + "."); } GameObject go = GameObject.Instantiate(typeMap[typeKey], manager.WorldCanvas.transform); go.transform.SetAsFirstSibling(); NodeUi ui = go.GetComponent <NodeUi>(); ui.ParseParams(storageNode.typeParams); go.transform.position = storageNode.position; CircuitNode node = ui.Node; foreach (NodeSettingContainer settingContainer in storageNode.settings) { int offset; settingsTypeIndex.TryGetValue(settingContainer.type, out offset); int index = Array.FindIndex(node.settings, offset, s => s.type == settingContainer.type); if (index < 0) { Debug.Log("Stored setting " + settingContainer.type + " for type " + typeKey + " not found with offset " + offset + "."); continue; } NodeSetting setting = node.settings[index]; DebugUtils.Assert(settingContainer.type == setting.type); setting.ParseValue(settingContainer.value); settingsTypeIndex[settingContainer.type] = index + 1; } } DebugUtils.Assert(manager.circuitManager.Nodes.Count == graph.Length); yield return(0); List <CircuitNode> nodes = manager.circuitManager.Nodes; for (int nodeIndex = 0; nodeIndex < graph.Length; nodeIndex++) { CircuitNode node = nodes[nodeIndex]; StorageNode storageNode = graph[nodeIndex]; for (int portIndex = 0; portIndex < storageNode.connections.Length; portIndex++) { NodeConnection connection = storageNode.connections[portIndex]; if (connection.nodeIndex >= 0 && connection.portIndex >= 0) { InputPort port = node.inputPorts[portIndex]; OutputPort connectedPort = nodes[connection.nodeIndex].outputPorts[connection.portIndex]; port.Connect(connectedPort); } } if (storageNode.transitions != null) { foreach (var transition in storageNode.transitions) { var sourceStatePort = nodes[transition.sourceNodeIndex].statePort; sourceStatePort.Connect(nodes[transition.targetNodeIndex].statePort); if (transition.transitionEnabledConnection != null && transition.transitionEnabledConnection.nodeIndex >= 0 && transition.transitionEnabledConnection.portIndex >= 0) { StateMachineTransition newTransition = (StateMachineTransition)sourceStatePort.connections[ sourceStatePort.connections.Count - 1]; var connectedNode = nodes[transition.transitionEnabledConnection.nodeIndex]; var connectedPort = connectedNode.outputPorts[transition.transitionEnabledConnection.portIndex]; newTransition.TransitionEnabledPort.Connect(connectedPort); } } } } }
private void StatePort_Connected(Connection connection) { DebugUtils.Assert(object.ReferenceEquals(connection.SourcePort, this)); DebugUtils.Assert(!connection.TargetPort.IsStateRootPort); StateMachineTransition transition = (StateMachineTransition)connection; StateMachineChip stateMachine; StateChip targetState = (StateChip)transition.TargetStatePort.Node; if (transition.SourceStatePort.IsStateRootPort) { stateMachine = (StateMachineChip)transition.SourceStatePort.Node; } else { StateChip sourceState = (StateChip)transition.SourceStatePort.Node; stateMachine = sourceState.StateMachineUnchecked; if (stateMachine == null) { stateMachine = targetState.StateMachineUnchecked; } } if (stateMachine == null) { if (!transition.SourceStatePort.IsStateRootPort) { ((StateChip)transition.SourceStatePort.Node).CheckStateMachine(); } targetState.CheckStateMachine(); return; } StateChip newState; if (targetState.StateMachineUnchecked == null) { newState = targetState; } else { newState = (StateChip)transition.SourceStatePort.Node; } if (newState.StateMachineUnchecked == null) { DebugUtils.Assert(newState.statePort.connections.Count > 0); if (newState.statePort.connections.Count > 1) { stateMachine.UpdateConnectedStates(); } else { DebugUtils.Assert(!stateMachine.connectedStates.Contains(newState)); stateMachine.connectedStates.Add(newState); newState.StateMachine = stateMachine; } } else { DebugUtils.Assert(object.ReferenceEquals(targetState.StateMachine, stateMachine)); } if (!transition.SourceStatePort.IsStateRootPort) { ((StateChip)transition.SourceStatePort.Node).CheckStateMachine(); } targetState.CheckStateMachine(); }