Exemplo n.º 1
0
        static object EnumField(object v)
        {
            var type       = v.GetType();
            var enumValues = Enum.GetValues(type).Cast <object>().ToList();

            var isFlag = type.GetCustomAttributes(typeof(FlagsAttribute), true).Any();

            if (isFlag)
            {
                var flagV = Convert.ToUInt64(Convert.ChangeType(v, type));
                enumValues.ForEach(value =>
                {
                    var flag = Convert.ToUInt64(value);
                    if (flag > 0)
                    {
                        var has = (flag & flagV) == flag;
                        has     = GUILayout.Toggle(has, value.ToString());

                        flagV = has ? (flagV | flag) : (flagV & ~flag);
                    }
                });

                v = Enum.ToObject(type, flagV);
            }
            else
            {
                var idx        = enumValues.IndexOf(v);
                var valueNames = enumValues.Select(value => value.ToString()).ToArray();

#if UNITY_EDITOR
                if (RGUILayoutUtility.IsInEditorWindow())
                {
                    idx = UnityEditor.EditorGUILayout.Popup(idx, valueNames);
                }
                else
#endif
                {
                    idx = SelectionPopup(idx, valueNames);
                }

                v = enumValues.ElementAtOrDefault(idx);
            }
            return(v);
        }
Exemplo n.º 2
0
        static object ColorField(object obj)
        {
            void ColorBox(Rect r, Color col)
            {
                using (new BackgroundColorScope(col))
                {
                    GUI.Box(r, "", Style.whiteRect);
                }
            }

            var color = (Color)obj;

#if UNITY_EDITOR
            if (RGUILayoutUtility.IsInEditorWindow())
            {
                var ret = EditorGUILayout.ColorField(color);
                return(ret);
            }
#endif

            var id = GUIUtility.GetControlID(FocusType.Passive);

            var rect = GUILayoutUtility.GetRect(new GUIContent(), GUI.skin.textField);

            // color bar
            ColorBox(rect, new Color(color.r, color.g, color.b, 1f));

            var alphaRect = rect;

            // alpha bar background(black)
            alphaRect.y      = alphaRect.yMax - ColorFieldSetting.alphaBarHeight;
            alphaRect.height = ColorFieldSetting.alphaBarHeight;
            ColorBox(alphaRect, Color.black);

            // alpha bar
            alphaRect.width *= color.a;
            ColorBox(alphaRect, Color.white);

            // label
            Color.RGBToHSV(color, out var h, out var s, out var v);
            var yuvY      = 0.299f * color.r + 0.587f * color.g + 0.114f * color.b;
            var fontColor = yuvY >= 0.4f ? ColorFieldSetting.labelColorDark : ColorFieldSetting.labelColorLight;
            using (new ColorScope(fontColor))
            {
                GUI.Label(rect, $" HSV {h:0.00} {s:0.00} {v:0.00}");
            }

            // button
            using (new ColorScope(Color.clear))
            {
                if (GUI.Button(rect, "", Style.whiteRect))
                {
                    colorPicker = new IMColorPicker(color);
                    colorPicker.SetWindowPosition(colorPickerLastPos ?? RGUIUtility.GetMouseScreenPos());
                    colorPickerControlId = id;
                }
            }


            if ((colorPicker != null) && (colorPickerControlId == id))
            {
                WindowInvoker.Add(colorPicker);

                if (colorPicker.destroy)
                {
                    colorPicker          = null;
                    colorPickerControlId = 0;
                }
                else
                {
                    color = colorPicker.color;
                    colorPickerLastPos = colorPicker.windowRect.position;
                }
            }

            return(color);
        }