Exemplo n.º 1
0
        private void DrawFieldGUI(Rect rect, string valueName, object record)
        {
            var current = Event.current;

            var value = masterController.GetValue(record, valueName);

            var valueType = masterController.GetValueType(valueName);

            Action <object> onUpdateValue = x =>
            {
                masterController.UpdateValue(record, valueName, x);

                if (onChangeRecord != null)
                {
                    onChangeRecord.OnNext(Unit.Default);
                }

                RefreshCustomRowHeights();
            };

            var color = masterController.IsChanged(record, valueName) ? EditedColor : Color.white;

            rect.height = EditorGUIUtility.singleLineHeight;

            using (new BackgroundColorScope(color))
            {
                if (EditorRecordFieldUtility.IsArrayType(valueType))
                {
                    var builder = new StringBuilder();

                    if (value != null)
                    {
                        foreach (var item in (IEnumerable)value)
                        {
                            builder.AppendFormat("{0},", item);
                        }
                    }

                    var text = builder.ToString().TrimEnd(',');

                    EditorGUI.LabelField(rect, text, EditorStyles.textField);

                    if (MasterController.CanEdit || !string.IsNullOrEmpty(text))
                    {
                        // メニュー表示と干渉するのでGUILayout.Buttonを使わない.
                        if (rect.Contains(current.mousePosition) && current.type == EventType.MouseDown && current.button == 0)
                        {
                            var mouseRect = new Rect(current.mousePosition, Vector2.one);

                            var arrayFieldPopupWindow = new ArrayFieldPopupWindow();

                            arrayFieldPopupWindow.SetContents(valueType, value);

                            arrayFieldPopupWindow.OnUpdateElementsAsObservable()
                            .Subscribe(x => onUpdateValue(x))
                            .AddTo(lifetimeDisposable.Disposable);

                            PopupWindow.Show(mouseRect, arrayFieldPopupWindow);

                            current.Use();
                        }
                    }
                }
                else
                {
                    if (valueType == typeof(string))
                    {
                        rect.height = EditorRecordFieldUtility.GetTextFieldHight(value as string);
                    }

                    EditorGUI.BeginChangeCheck();

                    try
                    {
                        value = EditorRecordFieldUtility.DrawField(rect, value, valueType);
                    }
                    catch (Exception e)
                    {
                        Debug.LogErrorFormat("Error: {0}\nValueName = {1}\nValueType = {2}\nValue = {3}\n", e.Message, valueName, valueType, value);
                    }

                    if (EditorGUI.EndChangeCheck() && MasterController.CanEdit)
                    {
                        onUpdateValue(value);
                    }
                }
            }

            // 右クリックでメニュー表示.
            if (rect.Contains(current.mousePosition) && current.type == EventType.MouseDown && current.button == 1)
            {
                if (masterController.IsChanged(record, valueName))
                {
                    var menu = new GenericMenu();

                    GenericMenu.MenuFunction onResetMenuClick = () =>
                    {
                        masterController.ResetValue(record, valueName);
                    };

                    menu.AddItem(new GUIContent("Reset"), false, onResetMenuClick);

                    menu.ShowAsContext();

                    GUI.FocusControl(string.Empty);

                    current.Use();
                }
            }
        }
Exemplo n.º 2
0
        public override void OnGUI(Rect rect)
        {
            if (elements == null)
            {
                return;
            }

            using (new EditorGUILayout.HorizontalScope(EditorStyles.toolbar, GUILayout.Height(12f)))
            {
                if (MasterController.CanEdit)
                {
                    if (GUILayout.Button(toolbarPlusIcon, EditorStyles.toolbarButton, GUILayout.Width(25f)))
                    {
                        elements.Add(elementType.GetDefaultValue());

                        OnUpdateElements();
                    }
                }
            }

            using (var scrollViewScope = new EditorGUILayout.ScrollViewScope(scrollPosition))
            {
                var removeIndexs = new List <int>();

                for (var i = 0; i < elements.Count; i++)
                {
                    using (new EditorGUILayout.HorizontalScope())
                    {
                        EditorGUI.BeginChangeCheck();

                        var fieldRect = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight);

                        elements[i] = EditorRecordFieldUtility.DrawField(fieldRect, elements[i], elementType);

                        if (EditorGUI.EndChangeCheck() && MasterController.CanEdit)
                        {
                            OnUpdateElements();
                        }

                        if (MasterController.CanEdit)
                        {
                            if (GUILayout.Button(toolbarMinusIcon, EditorStyles.miniButton, GUILayout.Width(25f)))
                            {
                                removeIndexs.Add(i);
                            }
                        }
                    }
                }

                if (removeIndexs.Any())
                {
                    foreach (var removeIndex in removeIndexs)
                    {
                        elements.RemoveAt(removeIndex);
                    }

                    OnUpdateElements();
                }

                scrollPosition = scrollViewScope.scrollPosition;
            }
        }