Exemple #1
0
        public static void CreateSymmetry(Part symmetricRoot, List <Part> counterparts)
        {
            if (symmetricRoot == null)
            {
                throw new ArgumentNullException("symmetricRoot");
            }

            if (counterparts == null)
            {
                throw new ArgumentNullException("counterparts");
            }

            // The symmetric root's method is always radial.
            symmetricRoot.symMethod = SymmetryMethod.Radial;

            // Clear out the current list of counterparts, we'll recreate this as we go along.
            symmetricRoot.symmetryCounterparts.Clear();

            // Update all the counterparts...
            for (int index = 0; index < counterparts.Count; index++)
            {
                Part counterpart = counterparts[index];

                // Each counterpart must be added to the symmetric root's list.
                symmetricRoot.symmetryCounterparts.Add(counterpart);
                // Clear the counterpart's list.
                counterpart.symmetryCounterparts.Clear();
                // Set the appropriate symmetry method.
                counterpart.symMethod = SymmetryMethod.Radial;
                // Add the symmetrical root part as a counterpart.
                counterpart.symmetryCounterparts.Add(symmetricRoot);

                // Add all the *other* counterparts.
                for (int siblingIndex = 0; siblingIndex < counterparts.Count; siblingIndex++)
                {
                    if (index != siblingIndex)
                    {
                        counterpart.symmetryCounterparts.Add(counterparts[siblingIndex]);
                    }
                }
            }

            // Now we must deal with the children...if they have symmetry.
            foreach (Part child in symmetricRoot.children)
            {
                if (PartWizard.HasSymmetry(child))
                {
                    // A list to hold the relevant counterparts. We only care about the counterparts that descend from the counterparts we've been given.
                    List <Part> childSymmetricCounterparts = new List <Part>();

                    // Look through all the child's counterparts to find the ones relevant to our counterparts...
                    foreach (Part childSymmetricCounterpart in child.symmetryCounterparts)
                    {
                        if (counterparts.Contains(childSymmetricCounterpart.parent))
                        {
                            // ...and add those to the list.
                            childSymmetricCounterparts.Add(childSymmetricCounterpart);
                        }
                    }

                    // We now have a part and a list of parts to make symmetrical, which we already know how to do!
                    PartWizard.CreateSymmetry(child, childSymmetricCounterparts);
                }
            }

            // Poke the staging logic to sort out any changes due to modifying the symmetry of this part.
            Staging.SortIcons();
        }
Exemple #2
0
        public override void OnRender()
        {
            try
            {
                GUILayout.BeginVertical();

                this.scrollPosition = GUILayout.BeginScrollView(scrollPosition, false, false);

                this.highlight.BeginTracking();

                if (this.mouseOver)
                {
                    this.highlight.Add(this.part, Configuration.HighlightColorSymmetryEditor, Configuration.HighlightColorSymmetryEditor);
                }

                for (int index = 0; index < this.symmetryGroups.Count; index++)
                {
                    PartGroup group = this.symmetryGroups[index];

                    GUIControls.BeginMouseOverVertical(GUIControls.PanelStyle);

                    GUILayout.BeginHorizontal();

                    GUILayout.Label(new GUIContent(string.Format(CultureInfo.CurrentCulture, Localized.GroupLabelText, index + 1)));

                    // Don't allow group removal if there is only one group.
                    GUI.enabled = (this.symmetryGroups.Count > 1);

                    if (GUILayout.Button(SymmetryEditorWindow.RemoveGroupButtonText))
                    {
                        // If there's a group above, use it. If not, then use the one below.
                        PartGroup destinationGroup = (index > 0) ? this.symmetryGroups[index - 1] : this.symmetryGroups[index + 1];

                        destinationGroup.MergeFrom(group);

                        this.symmetryGroups.Remove(group);

                        break;
                    }

                    GUILayout.EndHorizontal();

                    GUI.enabled = true;

                    bool mouseOverPart = false;

                    foreach (Part groupPart in group.Parts)
                    {
                        GUIControls.BeginMouseOverHorizontal();

                        GUILayout.Label(new GUIContent(groupPart.partInfo.title));

                        GUI.enabled = index < this.symmetryGroups.Count - 1;

                        if (GUILayout.Button(SymmetryEditorWindow.MoveDownButtonText, Configuration.PartActionButtonWidth))
                        {
                            PartGroup nextGroup = this.symmetryGroups[index + 1];

                            group.MoveTo(groupPart, nextGroup);

                            break;
                        }

                        GUI.enabled = index > 0;

                        if (GUILayout.Button(SymmetryEditorWindow.MoveUpButtonText, Configuration.PartActionButtonWidth))
                        {
                            PartGroup previousGroup = this.symmetryGroups[index - 1];

                            group.MoveTo(groupPart, previousGroup);

                            break;
                        }

                        GUI.enabled = true;

                        bool mouseOverPartArea = false;
                        GUIControls.EndMouseOverVertical(out mouseOverPartArea);

                        if (mouseOverPartArea)
                        {
                            // First add the group with the child part color, recursively.
                            this.highlight.Add(group, Configuration.HighlightColorEditableSymmetryChildParts, true);
                            // Next add the group with the counterparts highlighted, non-recursively.
                            this.highlight.Add(group, Configuration.HighlightColorCounterparts, false);
                            // Last add the specific part, non-recursively.
                            this.highlight.Add(groupPart, Configuration.HighlightColorSinglePart, false);

                            mouseOverPart = true;
                        }
                    }

                    bool groupMouseOver = false;
                    GUIControls.EndMouseOverVertical(out groupMouseOver);

                    if (!mouseOverPart && groupMouseOver)
                    {
                        this.highlight.Add(group, Configuration.HighlightColorEditableSymmetryCounterparts, true);
                    }
                }

                // Enable the Add Group button only if there is enough symmetrical parts to fill it.
                GUI.enabled = (this.symmetryGroups.Count < (this.part.symmetryCounterparts.Count + 1));

                if (GUILayout.Button(SymmetryEditorWindow.AddGroupButtonText))
                {
                    this.symmetryGroups.Add(new PartGroup());
                }

                GUI.enabled = true;

                GUILayout.EndScrollView();

                GUILayout.Space(4);

                GUILayout.BeginHorizontal();

                #region OK Button

                if (GUILayout.Button(Localized.OK))
                {
                    int symmetricGroupsCreated = 0;
                    int partsProcessed         = 0;

                    foreach (PartGroup group in this.symmetryGroups)
                    {
                        if (group.Parts.Count > 0)
                        {
                            partsProcessed += group.Count;

                            Part symmetricRoot = group.Extract(0);

                            PartWizard.CreateSymmetry(symmetricRoot, group.Parts);

                            symmetricGroupsCreated++;

#if DEBUG
                            Log.WriteSymmetryReport(symmetricRoot);
#endif
                        }
                    }

                    Log.Write("Modified symmetry for {0}, creating {1} symmetric group(s) from {2} parts.", part.name, symmetricGroupsCreated, partsProcessed);

                    this.Hide();
                }

                #endregion

                #region Cancel Button

                if (GUILayout.Button(Localized.Cancel))
                {
                    this.Hide();
                }

                #endregion

                GUILayout.EndHorizontal();

                GUILayout.EndVertical();
            }
            catch (Exception)
            {
                highlight.CancelTracking();

                throw;
            }
            finally
            {
                GUI.DragWindow();

                if (this.Visible && this.mouseOver)
                {
                    this.highlight.EndTracking();
                }
                else
                {
                    this.highlight.CancelTracking();
                }
            }
        }