Skip to content

Commit

Permalink
feat: add Use HDR Color Picker option for project settings (editor)
Browse files Browse the repository at this point in the history
close #290
  • Loading branch information
2394425147 authored and mob-sakai committed Jan 7, 2025
1 parent 9799069 commit 1be0cd4
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 15 deletions.
72 changes: 63 additions & 9 deletions Packages/src/Editor/UIEffectEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using UnityEngine;
using System.Linq;
using System;
using System.Reflection;
using Object = UnityEngine.Object;

namespace Coffee.UIEffects.Editors
Expand All @@ -13,6 +14,17 @@ namespace Coffee.UIEffects.Editors
[CanEditMultipleObjects]
public class UIEffect2Editor : Editor
{
private static readonly PropertyInfo s_PiGradient = typeof(SerializedProperty)
.GetProperty("gradientValue", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

private static readonly Func<SerializedProperty, Gradient> s_GetGradient =
(Func<SerializedProperty, Gradient>)Delegate.CreateDelegate(typeof(Func<SerializedProperty, Gradient>),
s_PiGradient.GetMethod);

private static readonly Action<SerializedProperty, Gradient> s_SetGradient =
(Action<SerializedProperty, Gradient>)Delegate.CreateDelegate(typeof(Action<SerializedProperty, Gradient>),
s_PiGradient.SetMethod);

private SerializedProperty _toneFilter;
private SerializedProperty _toneIntensity;

Expand Down Expand Up @@ -162,7 +174,7 @@ public void DrawProperties()
{
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(_colorIntensity);
DrawColor(_colorFilter, _color, prevColorFilter);
DrawColor(_colorFilter, _color, prevColorFilter, false);
EditorGUILayout.PropertyField(_colorGlow);
EditorGUI.indentLevel--;
}
Expand Down Expand Up @@ -260,7 +272,7 @@ public void DrawProperties()
}

EditorGUILayout.PropertyField(_shadowColorFilter);
EditorGUILayout.PropertyField(_shadowColor);
DrawColorPickerField(_shadowColor, false);
EditorGUILayout.PropertyField(_shadowColorGlow);
EditorGUILayout.PropertyField(_shadowFade);

Expand All @@ -286,15 +298,16 @@ public void DrawProperties()
case GradationMode.HorizontalGradient:
case GradationMode.VerticalGradient:
case GradationMode.AngleGradient:
EditorGUILayout.PropertyField(_gradationGradient);
DrawGradientField(_gradationGradient);
break;
default:
EditorGUILayout.PropertyField(_gradationColor1);
DrawColorPickerField(_gradationColor1);
var r = EditorGUILayout.GetControlRect();
r.width -= 20;
EditorGUI.PropertyField(r, _gradationColor2);
r.width -= 24;
r.height = EditorGUIUtility.singleLineHeight;
DrawColorPickerField(r, _gradationColor2);

r.x += r.width;
r.x += r.width + 4;
r.width = 20;
// Swap colors
if (GUI.Button(r, EditorGUIUtility.IconContent("preaudioloopoff"), "iconbutton"))
Expand Down Expand Up @@ -326,7 +339,48 @@ public void DrawProperties()
}
}

private static void DrawColor(SerializedProperty filter, SerializedProperty color, ColorFilter prevFilter)
private static void DrawColorPickerField(SerializedProperty color, bool showAlpha = true)
{
var r = EditorGUILayout.GetControlRect();
r.height = EditorGUIUtility.singleLineHeight;
DrawColorPickerField(r, color, showAlpha);
}

private static void DrawColorPickerField(Rect rect, SerializedProperty color, bool showAlpha = true)
{
var label = EditorGUIUtility.TrTempContent(color.displayName);
label.tooltip = color.tooltip;
var hdr = UIEffectProjectSettings.useHdrColorPicker;
EditorGUI.showMixedValue = color.hasMultipleDifferentValues;

EditorGUI.BeginChangeCheck();
var colorValue = EditorGUI.ColorField(rect, label, color.colorValue, true, showAlpha, hdr);
if (EditorGUI.EndChangeCheck())
{
color.colorValue = colorValue;
}
}

private static void DrawGradientField(SerializedProperty gradient)
{
var r = EditorGUILayout.GetControlRect();
r.height = EditorGUIUtility.singleLineHeight;

var label = EditorGUIUtility.TrTempContent(gradient.displayName);
label.tooltip = gradient.tooltip;
var hdr = UIEffectProjectSettings.useHdrColorPicker;
EditorGUI.showMixedValue = gradient.hasMultipleDifferentValues;

EditorGUI.BeginChangeCheck();
var gradientValue = EditorGUI.GradientField(r, label, s_GetGradient(gradient), hdr);
if (EditorGUI.EndChangeCheck())
{
s_SetGradient(gradient, gradientValue);
}
}

private static void DrawColor(SerializedProperty filter, SerializedProperty color, ColorFilter prevFilter,
bool showAlpha = true)
{
if (filter.intValue == (int)ColorFilter.None)
{
Expand Down Expand Up @@ -361,7 +415,7 @@ private static void DrawColor(SerializedProperty filter, SerializedProperty colo
color.colorValue = Color.white;
}

EditorGUILayout.PropertyField(color);
DrawColorPickerField(color, showAlpha);
}
}

Expand Down
10 changes: 10 additions & 0 deletions Packages/src/Editor/UIEffectProjectSettingsEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Coffee.UIEffects.Editors
public class UIEffectProjectSettingsEditor : Editor
{
private ReorderableList _reorderableList;
private SerializedProperty _useHDRColorPicker;
private SerializedProperty _transformSensitivity;
private bool _isInitialized;
private ShaderVariantRegistryEditor _shaderVariantRegistryEditor;
Expand All @@ -18,6 +19,7 @@ private void InitializeIfNeeded()
if (_isInitialized) return;

_transformSensitivity = serializedObject.FindProperty("m_TransformSensitivity");
_useHDRColorPicker = serializedObject.FindProperty("m_UseHDRColorPicker");
var runtimePresets = serializedObject.FindProperty("m_RuntimePresets");
_reorderableList = new ReorderableList(serializedObject, runtimePresets, true, true, true, true);
_reorderableList.drawHeaderCallback = rect => EditorGUI.LabelField(rect, "Runtime Presets");
Expand Down Expand Up @@ -63,9 +65,17 @@ public override void OnInspectorGUI()
InitializeIfNeeded();

// Settings
// Transform sensitivity.
EditorGUILayout.PropertyField(_transformSensitivity);

// Runtime Presets
_reorderableList.DoLayoutList();

// Editor
// Use HDR color pickers.
EditorGUILayout.PropertyField(_useHDRColorPicker);

// Shader
// Shader registry
EditorGUILayout.Space();
EditorGUILayout.LabelField("Shader", EditorStyles.boldLabel);
Expand Down
6 changes: 0 additions & 6 deletions Packages/src/Runtime/UIEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class UIEffect : UIEffectBase
[SerializeField]
protected float m_ColorIntensity = 1;

[ColorUsage(false, true)]
[SerializeField]
protected Color m_Color = Color.white;

Expand Down Expand Up @@ -81,7 +80,6 @@ public class UIEffect : UIEffectBase
[SerializeField]
protected ColorFilter m_TransitionColorFilter = ColorFilter.MultiplyAdditive;

[ColorUsage(true, true)]
[SerializeField]
protected Color m_TransitionColor = new Color(0f, 0.5f, 1.0f, 1.0f);

Expand All @@ -91,7 +89,6 @@ public class UIEffect : UIEffectBase
[SerializeField]
protected TargetMode m_TargetMode = TargetMode.None;

[ColorUsage(false, false)]
[SerializeField]
protected Color m_TargetColor = Color.white;

Expand Down Expand Up @@ -137,7 +134,6 @@ public class UIEffect : UIEffectBase
[SerializeField]
protected ColorFilter m_ShadowColorFilter = ColorFilter.Replace;

[ColorUsage(false, true)]
[SerializeField]
protected Color m_ShadowColor = Color.white;

Expand All @@ -148,11 +144,9 @@ public class UIEffect : UIEffectBase
protected GradationMode m_GradationMode = GradationMode.None;

[SerializeField]
[ColorUsage(true, true)]
protected Color m_GradationColor1 = Color.white;

[SerializeField]
[ColorUsage(true, true)]
protected Color m_GradationColor2 = Color.white;

[SerializeField]
Expand Down
10 changes: 10 additions & 0 deletions Packages/src/Runtime/UIEffectProjectSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public class UIEffectProjectSettings : PreloadedProjectSettings<UIEffectProjectS
[SerializeField]
internal List<UIEffect> m_RuntimePresets = new List<UIEffect>();

[Header("Editor")]
[Tooltip("Use HDR color pickers on color fields.")]
[SerializeField] private bool m_UseHDRColorPicker = true;

[HideInInspector]
[SerializeField]
internal ShaderVariantCollection m_ShaderVariantCollection;
Expand All @@ -38,6 +42,12 @@ public static TransformSensitivity transformSensitivity
set => instance.m_TransformSensitivity = value;
}

public static bool useHdrColorPicker
{
get => instance.m_UseHDRColorPicker;
set => instance.m_UseHDRColorPicker = value;
}

public static void RegisterRuntimePreset(UIEffect effect)
{
// Already registered.
Expand Down

0 comments on commit 1be0cd4

Please sign in to comment.