Beispiel #1
0
        void ComponentFilterContextMenu()
        {
            var menu = new GenericMenu();

            for (var i = 1; i < k_ProxyComponentsStrings.Length; i++)
            {
                var filterIndex = i;
                var content     = EditorGUIUtils.GetContent(k_ProxyComponentsStrings[filterIndex]);
                menu.AddItem(content, false, () =>
                {
                    m_MarsInspectorSharedSettings.ComponentTabSelection = filterIndex;
                });
            }

            menu.ShowAsContext();
        }
Beispiel #2
0
        internal static void ShowLandmarksMenu(ICalculateLandmarks landmarkSource, Action <LandmarkController> onLandmarkCreated = null)
        {
            if (landmarkSource == null)
            {
                return;
            }

            var landmarkMenu = new GenericMenu();
            var definitions  = landmarkSource.AvailableLandmarkDefinitions;

            if (definitions == null || definitions.Count == 0)
            {
                landmarkMenu.AddDisabledItem(styles.noLandmarksMenuItemContent);
            }
            else
            {
                for (var i = 0; i < definitions.Count; i++)
                {
                    var definition = definitions[i];
                    foreach (var outputType in definition.outputTypes)
                    {
                        var outputName  = TrimLandmarkOutputName(outputType.Name);
                        var subMenuName = definition.outputTypes.Length > 1 ? "/" + outputName : "";
                        var title       = EditorGUIUtils.GetContent(ObjectNames.NicifyVariableName(definition.name) + subMenuName);
                        landmarkMenu.AddItem(title, false, () =>
                        {
                            var landmark = landmarkSource.CreateLandmarkAsChild(definition, outputType);

                            var entityVisualsModule = ModuleLoaderCore.instance.GetModule <EntityVisualsModule>();
                            var marsEntity          = (landmarkSource as MonoBehaviour)?.GetComponentInParent <MARSEntity>();
                            if (entityVisualsModule != null && marsEntity != null)
                            {
                                entityVisualsModule.InvalidateVisual(marsEntity);
                            }

                            EditorGUIUtility.PingObject(landmark.gameObject);
                            onLandmarkCreated?.Invoke(landmark);
                        });
                    }
                }
            }

            landmarkMenu.ShowAsContext();
        }
Beispiel #3
0
        /// <summary>
        /// Draws the component editors in an inspector.
        /// </summary>
        public void OnGUI()
        {
            if (m_ComponentList == null)
            {
                return;
            }

            if (m_ComponentList.isDirty)
            {
                RefreshEditors();
                m_ComponentList.isDirty = false;
            }

            var isEditable = !UnityEditor.VersionControl.Provider.isActive ||
                             AssetDatabase.IsOpenForEdit(m_ComponentList.self, StatusQueryOptions.UseCachedIfPossible);

            if (!targetRequiresProxyMenu)
            {
                m_MarsInspectorSharedSettings.ComponentTabSelection = 0;
            }

            using (new EditorGUI.DisabledScope(!isEditable))
            {
                EditorGUILayout.LabelField(EditorGUIUtils.GetContent("Components"), EditorStyles.boldLabel);

                var availableWidth         = EditorGUIUtility.currentViewWidth;
                var isWideEnoughForAllTabs = availableWidth > k_MinWidthForAllTabs;
                if (isWideEnoughForAllTabs && targetRequiresProxyMenu)
                {
                    m_MarsInspectorSharedSettings.ComponentTabSelection
                        = GUILayout.Toolbar(m_MarsInspectorSharedSettings.ComponentTabSelection, k_ProxyComponentsStrings);
                }
                else if (targetRequiresProxyMenu)
                {
                    var tabIndex   = m_MarsInspectorSharedSettings.ComponentTabSelection;
                    var shortIndex = (tabIndex == 0) ? 0 : 1;
                    k_ProxyFilterStrings[1] = ((tabIndex == 0) ? k_ProxyFilter : k_ProxyComponentsStrings[tabIndex]) + k_DownArrowPostFix;
                    using (var check = new EditorGUI.ChangeCheckScope())
                    {
                        var selectedShortIndex = GUILayout.Toolbar(shortIndex, k_ProxyFilterStrings);
                        if (shortIndex != selectedShortIndex)
                        {
                            if (selectedShortIndex == 0)
                            {
                                m_MarsInspectorSharedSettings.ComponentTabSelection = 0;
                            }
                            else
                            {
                                ComponentFilterContextMenu();
                            }
                        }
                        else if (check.changed)
                        {
                            ComponentFilterContextMenu();
                        }
                    }
                }

                if (targetRequiresProxyMenu && (MarsInspectorSharedSettings.Instance.ComponentTabSelection == k_ForcesTab))
                {
                    Forces.EditorExtensions.ForcesMenuItemsRegions.DrawForcesInInspectorForMainProxy(m_BaseEditor.target);
                }

                // Override list
                for (var i = 0; i < m_Editors.Count; i++)
                {
                    var editor = m_Editors[i];

                    if (editor.activeProperty.serializedObject.targetObject == null)
                    {
                        continue;
                    }

                    if (CanDrawSelectedOption(editor.target))
                    {
                        var title = string.Format("{0}|{1}", editor.GetDisplayTitle(), editor.GetToolTip());
                        var id    = i; // Needed for closure capture below

                        EditorGUIUtils.DrawSplitter();
                        var displayContent = EditorGUIUtils.DrawHeader(
                            title,
                            editor.baseProperty,
                            editor.activeProperty,
                            () => ResetComponent(editor.target.GetType(), id),
                            () => RemoveComponent(id),
                            () => CopyComponent(id),
                            () => PasteComponent(id),
                            CanPasteComponent(id),
                            editor.HasDisplayProperties()
                            );

                        if (displayContent)
                        {
                            if (beforeDrawComponentInspector != null)
                            {
                                beforeDrawComponentInspector(editor);
                            }

                            using (new EditorGUI.DisabledScope(!editor.activeProperty.boolValue))
                            {
                                editor.OnInternalInspectorGUI();
                            }

                            if (afterDrawComponentInspector != null)
                            {
                                afterDrawComponentInspector(editor);
                            }
                        }
                    }
                }

                if (targetRequiresProxyMenu || (m_BaseEditor.target is ProxyGroup))
                {
                    if (m_Editors.Count > 0)
                    {
                        EditorGUIUtils.DrawSplitter();
                        EditorGUILayout.Space();
                    }
                    AddComponentMenuButton();
                    EditorGUILayout.Space();
                }
            }
        }
Beispiel #4
0
        protected override void AddComponentMenuButton()
        {
            var componentTabSelection = MarsInspectorSharedSettings.Instance.ComponentTabSelection;

            // Skip this button in the case of Forces, because they have their more complex button already and this button is redundant.
            if (componentTabSelection == k_ForcesTab)
            {
                return;
            }

            var tabIsAllOrSettings = componentTabSelection == k_AllTabs || componentTabSelection == k_SettingsTab;

            var addComponentText = "Add MARS Component...";

            if (!tabIsAllOrSettings)
            {
                switch (componentTabSelection)
                {
                case k_ConditionsTab:
                    addComponentText = "Add Condition...";
                    break;

                case k_ActionsTab:
                    addComponentText = "Add Action...";
                    break;

                case k_ForcesTab:
                    addComponentText = "Add Force...";
                    break;
                }
            }

            if (GUILayout.Button(addComponentText, EditorStyles.miniButton))
            {
                var menu    = new GenericMenu();
                var isGroup = (m_BaseEditor.target is ProxyGroup);

                foreach (var kvp in k_ComponentTypes)
                {
                    var type = kvp.Key;

                    if (isGroup && !(type.IsSubclassOf(typeof(Relation))))
                    {
                        continue;
                    }

                    if (!CanDrawSelectedOption(null, type, true))
                    {
                        continue;
                    }

                    // Check if this component cannot be added because another of its exclusive siblings is added
                    m_ConflictingBehaviors.Clear();
                    var componentClash = HasComponentClash(type, m_ComponentList, false, m_ConflictingBehaviors);

                    if (!componentClash)
                    {
                        componentClash = HasRequirementClash(type, m_ComponentList);
                    }
                    else if (m_ConflictingBehaviors.Count > 0)
                    {
                        m_ConflictingBehaviors.RemoveAll(alternate => HasRequirementClash(alternate.GetType(), m_ComponentList));
                    }

                    var menuItem = EditorGUIUtils.GetContent(kvp.Value.menuItem);

                    if (!tabIsAllOrSettings)
                    {
                        var modifiedMenuItem = new GUIContent(menuItem);
                        var text             = modifiedMenuItem.text;
                        // If we're in a filtered tab, don't show the first-layer menu (Conditions, Actions, Forces),
                        // because it would be redundant with the tab itself.
                        modifiedMenuItem.text = text.Substring(text.IndexOf("/", StringComparison.Ordinal) + 1);
                        menuItem = modifiedMenuItem;
                    }

                    if (!componentClash)
                    {
                        menu.AddItem(menuItem, false, () => AddComponent(type));
                    }
                    else if (m_ConflictingBehaviors.Count > 0)
                    {
                        var conflictingComponent = m_ConflictingBehaviors[0];

                        var conflictingTypeName = conflictingComponent.GetType().Name;
                        if (k_ComponentTypes.ContainsKey(conflictingComponent.GetType()))
                        {
                            conflictingTypeName = k_ComponentTypes[conflictingComponent.GetType()].menuItem;
                            if (conflictingTypeName.Contains("/"))
                            {
                                conflictingTypeName = conflictingTypeName.Substring(conflictingTypeName.IndexOf("/", StringComparison.Ordinal) + 1);
                            }
                        }

                        var insertText   = $"Replace/{conflictingTypeName} with";
                        var menuItemText = menuItem.text;
                        if (menuItemText.Contains("/"))
                        {
                            var lastIndex = menuItemText.IndexOf("/", StringComparison.Ordinal);
                            menuItemText = menuItemText.Insert(lastIndex, "/" + insertText);
                        }
                        else
                        {
                            menuItemText = insertText + "/" + menuItemText;
                        }

                        menu.AddItem(new GUIContent(menuItemText), false, () =>
                        {
                            Undo.DestroyObjectImmediate(conflictingComponent);
                            AddComponent(type);
                        });
                    }
                }

                menu.ShowAsContext();
            }
        }