Beispiel #1
0
        public override void Do()
        {
            if (MoveItTool.POProcessing > 0)
            {
                return;
            }

            MoveItTool.instance.m_lastInstance = null;
            m_clones                 = new HashSet <Instance>();
            m_origToCloneUpdate      = new Dictionary <Instance, Instance>();
            m_nodeOrigToClone        = new Dictionary <ushort, ushort>();
            m_stateToClone           = new Dictionary <InstanceState, Instance>();
            m_InstanceID_origToClone = new Dictionary <InstanceID, InstanceID>();

            matrix4x.SetTRS(center + moveDelta, Quaternion.AngleAxis(angleDelta * Mathf.Rad2Deg, Vector3.down), Vector3.one);

            // Clone nodes first
            foreach (InstanceState state in m_states)
            {
                if (state is NodeState)
                {
                    Instance clone = state.instance.Clone(state, ref matrix4x, moveDelta.y, angleDelta, center, followTerrain, m_nodeOrigToClone, this);

                    if (clone != null)
                    {
                        m_clones.Add(clone);
                        m_stateToClone.Add(state, clone);
                        m_InstanceID_origToClone.Add(state.instance.id, clone.id);
                        m_origToCloneUpdate.Add(state.instance.id, clone.id);
                        m_nodeOrigToClone.Add(state.instance.id.NetNode, clone.id.NetNode);
                    }
                }
            }

            // Clone everything else except PO
            foreach (InstanceState state in m_states)
            {
                if (!(state is NodeState || state is ProcState))
                {
                    Instance clone = state.instance.Clone(state, ref matrix4x, moveDelta.y, angleDelta, center, followTerrain, m_nodeOrigToClone, this);

                    if (clone == null)
                    {
                        Log.Debug($"Failed to clone {state}");
                        continue;
                    }

                    m_clones.Add(clone);
                    m_stateToClone.Add(state, clone);
                    m_InstanceID_origToClone.Add(state.instance.id, clone.id);
                    m_origToCloneUpdate.Add(state.instance.id, clone.id);
                    ;
                    if (state is SegmentState segmentState)
                    {
                        MoveItTool.NS.SetSegmentModifiers(clone.id.NetSegment, segmentState);
                        if (segmentState.LaneIDs != null)
                        {
                            // old version does not store lane ids
                            var clonedLaneIds = MoveableSegment.GetLaneIds(clone.id.NetSegment);
                            DebugUtils.AssertEq(clonedLaneIds.Count, segmentState.LaneIDs.Count, "clonedLaneIds.Count, segmentState.LaneIDs.Count");
                            for (int i = 0; i < clonedLaneIds.Count; ++i)
                            {
                                var lane0 = new InstanceID {
                                    NetLane = segmentState.LaneIDs[i]
                                };
                                var lane = new InstanceID {
                                    NetLane = clonedLaneIds[i]
                                };
                                // Log.Debug($"Mapping lane:{lane0.NetLane} to {lane.NetLane}");
                                m_InstanceID_origToClone.Add(lane0, lane);
                            }
                        }
                    }
                }
            }

            // Clone PO
            foreach (InstanceState state in m_states)
            {
                if (state is ProcState)
                {
                    _ = state.instance.Clone(state, ref matrix4x, moveDelta.y, angleDelta, center, followTerrain, m_nodeOrigToClone, this);
                }
            }

            if (m_origToClone != null)
            {
                Dictionary <Instance, Instance> toReplace = new Dictionary <Instance, Instance>();

                foreach (Instance key in m_origToClone.Keys)
                {
                    toReplace.Add(m_origToClone[key], m_origToCloneUpdate[key]);
                    DebugUtils.Log("To replace: " + m_origToClone[key].id.RawData + " -> " + m_origToCloneUpdate[key].id.RawData);
                }

                ActionQueue.instance.ReplaceInstancesForward(toReplace);
            }

            m_origToClone = m_origToCloneUpdate;

            // Select clones
            selection = m_clones;
            MoveItTool.m_debugPanel.UpdatePanel();

            UpdateArea(GetTotalBounds(false));
            try
            {
                MoveItTool.UpdatePillarMap();
            }
            catch (Exception e)
            {
                DebugUtils.Log("CloneActionBase.Do failed");
                DebugUtils.LogException(e);
            }
        }
Beispiel #2
0
        public override void Do()
        {
            if (MoveItTool.POProcessing > 0)
            {
                return;
            }

            MoveItTool.instance.m_lastInstance = null;
            m_clones            = new HashSet <Instance>();
            m_origToCloneUpdate = new Dictionary <Instance, Instance>();
            m_nodeOrigToClone   = new Dictionary <ushort, ushort>();
            var stateToClone           = new Dictionary <InstanceState, Instance>();
            var InstanceID_origToClone = new Dictionary <InstanceID, InstanceID>();

            matrix4x.SetTRS(center + moveDelta, Quaternion.AngleAxis(angleDelta * Mathf.Rad2Deg, Vector3.down), Vector3.one);

            // Clone nodes first
            foreach (InstanceState state in m_states)
            {
                if (state is NodeState)
                {
                    Instance clone = state.instance.Clone(state, ref matrix4x, moveDelta.y, angleDelta, center, followTerrain, m_nodeOrigToClone, this);

                    if (clone != null)
                    {
                        m_clones.Add(clone);
                        stateToClone.Add(state, clone);
                        InstanceID_origToClone.Add(state.instance.id, clone.id);
                        m_origToCloneUpdate.Add(state.instance.id, clone.id);
                        m_nodeOrigToClone.Add(state.instance.id.NetNode, clone.id.NetNode);
                    }
                }
            }

            // Clone everything else except PO
            foreach (InstanceState state in m_states)
            {
                if (!(state is NodeState || state is ProcState))
                {
                    Instance clone = state.instance.Clone(state, ref matrix4x, moveDelta.y, angleDelta, center, followTerrain, m_nodeOrigToClone, this);

                    if (clone == null)
                    {
                        Debug.Log($"Failed to clone {state}");
                        continue;
                    }

                    m_clones.Add(clone);
                    stateToClone.Add(state, clone);
                    InstanceID_origToClone.Add(state.instance.id, clone.id);
                    m_origToCloneUpdate.Add(state.instance.id, clone.id);

                    if (state is SegmentState segmentState)
                    {
                        MoveItTool.NS.SetSegmentModifiers(clone.id.NetSegment, segmentState);
                        var clonedLaneIds = MoveableSegment.GetLaneIds(clone.id.NetSegment);
                        DebugUtils.AssertEq(clonedLaneIds.Count, segmentState.LaneIDs.Count, "clonedLaneIds.Count, segmentState.LaneIDs.Count");
                        for (int i = 0; i < clonedLaneIds.Count; ++i)
                        {
                            var lane0 = new InstanceID {
                                NetLane = segmentState.LaneIDs[i]
                            };
                            var lane = new InstanceID {
                                NetLane = clonedLaneIds[i]
                            };
                            // Debug.Log($"Mapping lane:{lane0.NetLane} to {lane.NetLane}");
                            InstanceID_origToClone.Add(lane0, lane);
                        }
                    }
                }
            }

            // Clone NodeController after segments have been added.
            foreach (var item in stateToClone)
            {
                if (item.Key is NodeState nodeState)
                {
                    Instance clone  = item.Value;
                    ushort   nodeID = clone.id.NetNode;
                    MoveItTool.NodeController.PasteNode(nodeID, nodeState);
                }
            }

            // Clone TMPE rules
            foreach (var state in m_states)
            {
                if (state is NodeState nodeState)
                {
                    MoveItTool.TMPE.Paste(nodeState.TMPE_NodeRecord, InstanceID_origToClone);
                }
                else if (state is SegmentState segmentState)
                {
                    MoveItTool.TMPE.Paste(segmentState.TMPE_SegmentRecord, InstanceID_origToClone);
                    MoveItTool.TMPE.Paste(segmentState.TMPE_SegmentStartRecord, InstanceID_origToClone);
                    MoveItTool.TMPE.Paste(segmentState.TMPE_SegmentEndRecord, InstanceID_origToClone);
                }
            }

            // Clone PO
            foreach (InstanceState state in m_states)
            {
                if (state is ProcState)
                {
                    Instance clone = state.instance.Clone(state, ref matrix4x, moveDelta.y, angleDelta, center, followTerrain, m_nodeOrigToClone, this);
                }
            }

            if (m_origToClone != null)
            {
                Dictionary <Instance, Instance> toReplace = new Dictionary <Instance, Instance>();

                foreach (Instance key in m_origToClone.Keys)
                {
                    toReplace.Add(m_origToClone[key], m_origToCloneUpdate[key]);
                    DebugUtils.Log("To replace: " + m_origToClone[key].id.RawData + " -> " + m_origToCloneUpdate[key].id.RawData);
                }

                ActionQueue.instance.ReplaceInstancesForward(toReplace);
            }

            m_origToClone = m_origToCloneUpdate;

            // Select clones
            selection = m_clones;
            MoveItTool.m_debugPanel.UpdatePanel();

            UpdateArea(GetTotalBounds(false));
        }