public override void OnGUI(Rect _Position, SerializedProperty _Item, GUIContent _Label)
        {
            Rect rect = new Rect(_Position);

            rect.height = MuffinDevGUI.LINE_HEIGHT;
            MuffinDevGUI.ComputeLabelledFieldRects(rect, out Rect labelRect, out Rect fieldRect);

            // Key field
            SetKey(_Item, EditorGUI.TextField(labelRect, GetKey(_Item)));
            // Type label
            EditorGUI.LabelField(fieldRect, $"({ValueType.Name})");

            BlackboardAssetDemo.PlayerData data = GetValue(_Item);

            EditorGUI.indentLevel++;
            {
                // Name field
                rect.y   += rect.height + MuffinDevGUI.VERTICAL_MARGIN;
                rect      = EditorGUI.IndentedRect(rect);
                data.name = EditorGUI.TextField(rect, "Name", data.name);

                // Score field
                rect.y    += rect.height + MuffinDevGUI.VERTICAL_MARGIN;
                data.score = EditorGUI.IntField(rect, "Score", data.score);
            }
            EditorGUI.indentLevel--;

            // Save changes
            SetValue(_Item, data);
        }
        public override void OnGUI(Rect _Position, SerializedProperty _Item, GUIContent _Label)
        {
            MuffinDevGUI.ComputeLabelledFieldRects(_Position, out Rect labelRect, out Rect fieldRect);

            // Key field
            SetKey(_Item, EditorGUI.TextField(labelRect, GetKey(_Item)));
            // Value field
            Vector2 currentValue = GetValue(_Item);
            Vector2 newValue     = EditorGUI.Vector2Field(fieldRect, string.Empty, currentValue);

            if (currentValue != newValue)
            {
                SetValue(_Item, newValue);
            }
        }
        private void OnGUI()
        {
            EditorGUILayout.HelpBox("The following field has been drawn with MuffinDevGUI.ExtendedObjectField().\nUse the added controls to, from left to right:\n\t- Show/hide informations about the selected asset\n\t- Lock/unlock the selected asset\n\t- Focus the asset in the project view\n\t- Create a new asset if applicable", MessageType.Info);
            EditorGUILayout.Space();

            bool locked             = m_Object != null && m_Locked;
            bool canChangeFoldState = m_Object != null;
            bool canFocus           = m_Object != null;
            bool canCreate          = m_Object != null ? m_Object is ScriptableObject : false;

            GUI.enabled = !locked;
            m_Object    = MuffinDevGUI.ExtendedObjectField("Selected Asset", m_Object, typeof(Object), true, new ExtendedObjectFieldButton[]
            {
                // Add "Fold/Unfold" button
                new ExtendedObjectFieldButton(m_Folded ? EEditorIcon.Unfold : EEditorIcon.Fold, ExtendedObjectFieldButton.EPosition.BeforeLabel, m_Folded ? "Hide informations" : "Show informations", () =>
                {
                    m_Folded = !m_Folded;
                }, canChangeFoldState),

                // Add "Focus" button
                new ExtendedObjectFieldButton(locked ? EEditorIcon.Lock : EEditorIcon.Unlock, ExtendedObjectFieldButton.EPosition.BeforeField, locked ? "Locked" : "Unlocked", locked ? "Unlock selected asset" : "Lock selected asset", () =>
                {
                    m_Locked = !m_Locked;
                }, m_Object != null),

                // Add "Focus" button
                new ExtendedObjectFieldButton(EEditorIcon.Focus, ExtendedObjectFieldButton.EPosition.AfterField, "Focus asset", () =>
                {
                    EditorHelpers.FocusAsset(m_Object, true, true);
                }, canFocus),

                // Add "Create" button
                new ExtendedObjectFieldButton(EEditorIcon.Add, ExtendedObjectFieldButton.EPosition.AfterField, $"Create new {(m_Object != null ? m_Object.GetType().Name : "Object")} asset", () =>
                {
                    if (m_Object != null)
                    {
                        EditorHelpers.CreateAssetPanel(m_Object.GetType(), out Object newAsset, $"Create new {m_Object.GetType().Name} asset", $"New{m_Object.GetType().Name}", "", "asset", false);
                        if (newAsset != null)
                        {
                            m_Object = newAsset;
                        }
                    }
                }, canCreate)
示例#4
0
 /// <summary>
 /// Draws the GUI of the opened asset.
 /// Note that this method is called from DrawGUI(), and by default the asset is not null at this step.
 /// </summary>
 protected virtual void DrawAssetGUI()
 {
     MuffinDevGUI.DrawDefaultInspector(Asset);
 }
示例#5
0
        /// <summary>
        /// Draws the Assets List GUI, using Layout methods.
        /// </summary>
        public void DrawLayout()
        {
            if (!string.IsNullOrEmpty(InfoMessage))
            {
                EditorGUILayout.HelpBox(InfoMessage, InfoMessageVerbosity);
            }

            // Draw title and "Refresh" button
            EditorGUILayout.Space();
            using (new GUILayout.HorizontalScope())
            {
                EditorGUILayout.LabelField(Title, EditorStyles.largeLabel);
                if (GUILayout.Button(EditorGUIUtility.IconContent("Refresh"), GUILayout.Width(EditorGUIUtility.singleLineHeight * 2)))
                {
                    Refresh();
                }
            }

            // Draw the assets list
            T assetToOpen = null;

            using (new GUILayout.VerticalScope(EditorStyles.helpBox))
            {
                // Draw table headers
                using (new GUILayout.HorizontalScope())
                {
                    EditorGUILayout.GetControlRect(GUILayout.Width(BUTTONS_WIDTH));
                    EditorGUILayout.LabelField("Name", GUILayout.MaxWidth(ASSET_NAME_MAX_WIDTH));
                    EditorGUILayout.LabelField("Path");
                }
                MuffinDevGUI.HorizontalLine();

                // For each asset of the target type in the project...
                foreach (T asset in Assets)
                {
                    // Draw the "Open Asset" button, the name and the project path of the current asset
                    using (new GUILayout.HorizontalScope())
                    {
                        if (GUILayout.Button(OpenAssetButtonLabel, GUILayout.Width(BUTTONS_WIDTH)))
                        {
                            assetToOpen = asset;
                        }

                        EditorGUILayout.LabelField(asset.name, EditorStyles.boldLabel, GUILayout.MaxWidth(ASSET_NAME_MAX_WIDTH));
                        EditorGUILayout.LabelField(AssetDatabase.GetAssetPath(asset));
                    }
                }

                // If "Allow Create" option is enabled, draw "Create New Asset" button
                if (m_AllowCreate && GUILayout.Button(CreateAssetButtonLabel, GUILayout.Width(BUTTONS_WIDTH)))
                {
                    AssetCreationResult result = EditorHelpers.CreateAssetPanel <T>($"Create new {DisplayableTypeName} asset", $"New{TypeName}", EditorHelpers.ASSETS_FOLDER, EditorHelpers.DEFAULT_ASSET_EXTENSION, false);
                    if (result && m_AutoOpenCreatedAsset)
                    {
                        assetToOpen = result.GetAsset <T>();
                        Refresh();
                    }
                }
            }

            // Open the selected asset if required
            if (assetToOpen != null)
            {
                if (m_AutoSelectOpenedAsset)
                {
                    EditorUtility.FocusProjectWindow();
                    EditorHelpers.FocusAsset(assetToOpen);
                }

                if (OnOpenAsset != null)
                {
                    OnOpenAsset.Invoke(assetToOpen);
                }
            }
        }
        /// <summary>
        /// Draws the Blackboard property GUI.
        /// </summary>
        public override void OnGUI(Rect _Position, SerializedProperty _Property, GUIContent _Label)
        {
            // Avoid strange behaviours when multi-selecting elements
            if (_Property.hasMultipleDifferentValues)
            {
                EditorGUI.PropertyField(_Position, _Property);
                return;
            }

            SerializedProperty serializedDataList = _Property.FindPropertyRelative(SERIALIZED_DATA_LIST_PROP);

            // Draw box and header
            Rect rect = new Rect(_Position);

            GUI.Box(_Position, "", MuffinDevGUI.ReorderableListBoxStyle);
            rect.height = MuffinDevGUI.LINE_HEIGHT;
            GUI.Box(rect, _Property.displayName, MuffinDevGUI.ReorderableListHeaderStyle);

            Rect buttonRect = new Rect(rect);

            buttonRect.width   = Mathf.Clamp(rect.width * 40 / 100, MIN_ADD_ENTRY_BUTTON_WIDTH, MAX_ADD_ENTRY_BUTTON_WIDTH);
            buttonRect.x       = rect.x + rect.width - MuffinDevGUI.HORIZONTAL_MARGIN - buttonRect.width;
            buttonRect.height -= MuffinDevGUI.VERTICAL_MARGIN;
            buttonRect.y      += MuffinDevGUI.VERTICAL_MARGIN;
            DrawAddEntryButton(buttonRect, serializedDataList);

            // Setup layout
            rect.height = MuffinDevGUI.LINE_HEIGHT;
            rect.y     += MuffinDevGUI.LINE_HEIGHT + MuffinDevGUI.VERTICAL_MARGIN * 2;
            rect.width -= MuffinDevGUI.HORIZONTAL_MARGIN * 2;
            rect.x     += MuffinDevGUI.HORIZONTAL_MARGIN;

            // For each Blackboard entry
            for (int i = 0; i < serializedDataList.arraySize; i++)
            {
                Rect itemRect = new Rect(rect);
                itemRect.width = itemRect.height;
                // Draw "remove entry" button
                if (GUI.Button(itemRect, EditorIcons.IconContent(EEditorIcon.Close, "Deletes this entry from the Blackboard"), MuffinDevGUI.PropertyFieldButtonStyle))
                {
                    serializedDataList.DeleteArrayElementAtIndex(i);
                    return;
                }

                itemRect.x    += itemRect.width + MuffinDevGUI.HORIZONTAL_MARGIN;
                itemRect.width = rect.width - itemRect.width - MuffinDevGUI.HORIZONTAL_MARGIN;

                SerializedProperty item = serializedDataList.GetArrayElementAtIndex(i);
                Type dataType           = Type.GetType(item.FindPropertyRelative(DATA_TYPE_NAME_PROP).stringValue);

                // Display a warning field if the entry's type can't be found
                if (dataType == null)
                {
                    EditorGUI.HelpBox(itemRect, "Invalid Type", MessageType.Warning);
                    rect.y += itemRect.height + MuffinDevGUI.VERTICAL_MARGIN;
                    continue;
                }

                // If an editor exists for the entry's type, draw its GUI
                if (VALUE_EDITORS.TryGetValue(dataType, out IBlackboardValueEditor editor))
                {
                    itemRect.height = editor.GetPropertyHeight(item, new GUIContent(item.FindPropertyRelative("m_Key").stringValue));
                    editor.OnGUI(itemRect, item, new GUIContent(item.FindPropertyRelative("m_Key").stringValue));
                }
                // Else, draw the key field and display property type as a label
                else
                {
                    SerializedProperty keyProperty = item.FindPropertyRelative(KEY_PROP);
                    MuffinDevGUI.ComputeLabelledFieldRects(itemRect, out Rect labelRect, out Rect fieldRect);
                    keyProperty.stringValue = EditorGUI.TextField(labelRect, keyProperty.stringValue);
                    EditorGUI.LabelField(fieldRect, $"No editor for type {dataType.Name}", new GUIStyle(EditorStyles.helpBox).WordWrap(false));
                }
                rect.y     += itemRect.height + MuffinDevGUI.VERTICAL_MARGIN;
                rect.height = MuffinDevGUI.LINE_HEIGHT;
            }

            // Draw "is empty" label if the list is empty
            if (serializedDataList.arraySize == 0)
            {
                EditorGUI.LabelField(rect, "This Blackboard is empty...");
            }
        }