private bool TryGetDisplayValue(Color color, SerializedProperty property, out Vector3 output)
        {
            output = Vector3.zero;

            if (!m_TrackballMethods.TryGetValue(property.name, out var method))
            {
                var field = ReflectionUtils.GetFieldInfoFromPath(property.serializedObject.targetObject, property.propertyPath);

                if (!field.IsDefined(typeof(TrackballAttribute), false))
                {
                    return(false);
                }

                var attr = (TrackballAttribute)field.GetCustomAttributes(typeof(TrackballAttribute), false)[0];
                const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
                method = typeof(ColorGradingComponent).GetMethod(attr.method, flags);
                m_TrackballMethods.Add(property.name, method);
            }

            if (method == null)
            {
                return(false);
            }

            output = (Vector3)method.Invoke(property.serializedObject.targetObject, new object[] { color });
            return(true);
        }
예제 #2
0
        static void PasteSettings(SerializedProperty settings)
        {
            Undo.RecordObject(settings.serializedObject.targetObject, "Paste effect settings");
            var field  = ReflectionUtils.GetFieldInfoFromPath(settings.serializedObject.targetObject, settings.propertyPath);
            var json   = EditorGUIUtility.systemCopyBuffer.Substring(field.FieldType.ToString().Length + 1);
            var obj    = JsonUtility.FromJson(json, field.FieldType);
            var parent = ReflectionUtils.GetParentObject(settings.propertyPath, settings.serializedObject.targetObject);

            field.SetValue(parent, obj, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, CultureInfo.CurrentCulture);
        }
예제 #3
0
        static bool CanPaste(SerializedProperty settings)
        {
            var data = EditorGUIUtility.systemCopyBuffer;

            if (string.IsNullOrEmpty(data))
            {
                return(false);
            }

            var parts = data.Split('|');

            if (string.IsNullOrEmpty(parts[0]))
            {
                return(false);
            }

            var field = ReflectionUtils.GetFieldInfoFromPath(settings.serializedObject.targetObject, settings.propertyPath);

            return(parts[0] == field.FieldType.ToString());
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (s_Material == null)
            {
                s_Material = new Material(Shader.Find("Hidden/Post FX/UI/Trackball"))
                {
                    hideFlags = HideFlags.HideAndDontSave
                }
            }
            ;

            position = new Rect(position.x, position.y, position.width / 3f, position.height);
            int size = m_Size;

            position.x += 5f;

            var enumerator = property.GetEnumerator();

            while (enumerator.MoveNext())
            {
                var prop = enumerator.Current as SerializedProperty;
                if (prop == null || prop.propertyType != SerializedPropertyType.Color)
                {
                    continue;
                }

                OnWheelGUI(position, size, prop.Copy());
                position.x += position.width;
            }
        }

        void OnWheelGUI(Rect position, int size, SerializedProperty property)
        {
            if (Event.current.type == EventType.Layout)
            {
                return;
            }

            var   value  = property.colorValue;
            float offset = value.a;

            var wheelDrawArea = position;

            wheelDrawArea.height = size;

            if (wheelDrawArea.width > wheelDrawArea.height)
            {
                wheelDrawArea.x    += (wheelDrawArea.width - wheelDrawArea.height) / 2.0f;
                wheelDrawArea.width = position.height;
            }

            wheelDrawArea.width = wheelDrawArea.height;

            float   hsize  = size / 2f;
            float   radius = 0.38f * size;
            Vector3 hsv;

            Color.RGBToHSV(value, out hsv.x, out hsv.y, out hsv.z);

            if (Event.current.type == EventType.Repaint)
            {
                float scale = EditorGUIUtility.pixelsPerPoint;

                // Wheel texture
                var oldRT = RenderTexture.active;
                var rt    = RenderTexture.GetTemporary((int)(size * scale), (int)(size * scale), 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
                s_Material.SetFloat("_Offset", offset);
                s_Material.SetFloat("_DisabledState", GUI.enabled ? 1f : 0.5f);
                s_Material.SetVector("_Resolution", new Vector2(size * scale, size * scale / 2f));
                Graphics.Blit(null, rt, s_Material, EditorGUIUtility.isProSkin ? 0 : 1);
                RenderTexture.active = oldRT;

                GUI.DrawTexture(wheelDrawArea, rt);
                RenderTexture.ReleaseTemporary(rt);

                // Thumb
                var   thumbPos = Vector2.zero;
                float theta    = hsv.x * (Mathf.PI * 2f);
                float len      = hsv.y * radius;
                thumbPos.x = Mathf.Cos(theta + (Mathf.PI / 2f));
                thumbPos.y = Mathf.Sin(theta - (Mathf.PI / 2f));
                thumbPos  *= len;
                var thumbSize  = FxStyles.wheelThumbSize;
                var thumbSizeH = thumbSize / 2f;
                FxStyles.wheelThumb.Draw(new Rect(wheelDrawArea.x + hsize + thumbPos.x - thumbSizeH.x, wheelDrawArea.y + hsize + thumbPos.y - thumbSizeH.y, thumbSize.x, thumbSize.y), false, false, false, false);
            }

            var bounds = wheelDrawArea;

            bounds.x    += hsize - radius;
            bounds.y    += hsize - radius;
            bounds.width = bounds.height = radius * 2f;
            hsv          = GetInput(bounds, hsv, radius);
            value        = Color.HSVToRGB(hsv.x, hsv.y, 1f);
            value.a      = offset;

            // Luminosity booster
            position = wheelDrawArea;
            float oldX = position.x;
            float oldW = position.width;

            position.y     += position.height + 4f;
            position.x     += (position.width - (position.width * 0.75f)) / 2f;
            position.width  = position.width * 0.75f;
            position.height = EditorGUIUtility.singleLineHeight;
            value.a         = GUI.HorizontalSlider(position, value.a, -1f, 1f);

            // Advanced controls
            var data = Vector3.zero;

            if (TryGetDisplayValue(value, property, out data))
            {
                position.x     = oldX;
                position.y    += position.height;
                position.width = oldW / 3f;

                using (new EditorGUI.DisabledGroupScope(true))
                {
                    GUI.Label(position, data.x.ToString("F2"), EditorStyles.centeredGreyMiniLabel);
                    position.x += position.width;
                    GUI.Label(position, data.y.ToString("F2"), EditorStyles.centeredGreyMiniLabel);
                    position.x += position.width;
                    GUI.Label(position, data.z.ToString("F2"), EditorStyles.centeredGreyMiniLabel);
                    position.x += position.width;
                }
            }

            // Title
            position.x     = oldX;
            position.y    += position.height;
            position.width = oldW;
            GUI.Label(position, property.displayName, EditorStyles.centeredGreyMiniLabel);

            if (m_ResetState)
            {
                value        = Color.clear;
                m_ResetState = false;
            }

            property.colorValue = value;
        }

        bool TryGetDisplayValue(Color color, SerializedProperty property, out Vector3 output)
        {
            output = Vector3.zero;
            MethodInfo method;

            if (!m_TrackballMethods.TryGetValue(property.name, out method))
            {
                var field = ReflectionUtils.GetFieldInfoFromPath(property.serializedObject.targetObject, property.propertyPath);

                if (!field.IsDefined(typeof(TrackballAttribute), false))
                {
                    return(false);
                }

                var attr = (TrackballAttribute)field.GetCustomAttributes(typeof(TrackballAttribute), false)[0];
                const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
                method = typeof(ColorGradingComponent).GetMethod(attr.method, flags);
                m_TrackballMethods.Add(property.name, method);
            }

            if (method == null)
            {
                return(false);
            }

            output = (Vector3)method.Invoke(property.serializedObject.targetObject, new object[] { color });
            return(true);
        }
예제 #5
0
        public static bool Header(string title, SerializedProperty group, SerializedProperty enabledField, System.Action resetAction)
        {
            var          field  = ReflectionUtils.GetFieldInfoFromPath(enabledField.serializedObject.targetObject, enabledField.propertyPath);
            object       parent = null;
            PropertyInfo prop   = null;

            if (field != null && field.IsDefined(typeof(GetSetAttribute), false))
            {
                var attr = (GetSetAttribute)field.GetCustomAttributes(typeof(GetSetAttribute), false)[0];
                parent = ReflectionUtils.GetParentObject(enabledField.propertyPath, enabledField.serializedObject.targetObject);
                prop   = parent.GetType().GetProperty(attr.name);
            }

            var display = group == null || group.isExpanded;
            var enabled = enabledField.boolValue;

            var rect = GUILayoutUtility.GetRect(16f, 22f, FxStyles.header);

            GUI.Box(rect, title, FxStyles.header);

            var toggleRect = new Rect(rect.x + 4f, rect.y + 4f, 13f, 13f);
            var e          = Event.current;

            var popupRect = new Rect(rect.x + rect.width - FxStyles.paneOptionsIcon.width - 5f, rect.y + FxStyles.paneOptionsIcon.height / 2f + 1f, FxStyles.paneOptionsIcon.width, FxStyles.paneOptionsIcon.height);

            GUI.DrawTexture(popupRect, FxStyles.paneOptionsIcon);

            if (e.type == EventType.Repaint)
            {
                FxStyles.headerCheckbox.Draw(toggleRect, false, false, enabled, false);
            }

            if (e.type == EventType.MouseDown)
            {
                const float kOffset = 2f;
                toggleRect.x      -= kOffset;
                toggleRect.y      -= kOffset;
                toggleRect.width  += kOffset * 2f;
                toggleRect.height += kOffset * 2f;

                if (toggleRect.Contains(e.mousePosition))
                {
                    enabledField.boolValue = !enabledField.boolValue;

                    if (prop != null)
                    {
                        prop.SetValue(parent, enabledField.boolValue, null);
                    }

                    e.Use();
                }
                else if (popupRect.Contains(e.mousePosition))
                {
                    var popup = new GenericMenu();
                    popup.AddItem(GetContent("Reset"), false, () => resetAction());
                    popup.AddSeparator(string.Empty);
                    popup.AddItem(GetContent("Copy Settings"), false, () => CopySettings(group));

                    if (CanPaste(group))
                    {
                        popup.AddItem(GetContent("Paste Settings"), false, () => PasteSettings(group));
                    }
                    else
                    {
                        popup.AddDisabledItem(GetContent("Paste Settings"));
                    }

                    popup.ShowAsContext();
                }
                else if (rect.Contains(e.mousePosition) && group != null)
                {
                    display          = !display;
                    group.isExpanded = !group.isExpanded;
                    e.Use();
                }
            }

            return(display);
        }