Skip to content

Commit

Permalink
close #72: Add shadow effect component
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed May 29, 2018
1 parent 7b3314f commit 9073d31
Show file tree
Hide file tree
Showing 4 changed files with 419 additions and 0 deletions.
116 changes: 116 additions & 0 deletions Assets/UIEffect/Editor/UIShadowEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace Coffee.UIExtensions
{
/// <summary>
/// UIShadow editor.
/// </summary>
[CustomEditor(typeof(UIShadow))]
[CanEditMultipleObjects]
public class UIShadowEditor : Editor
{
ReorderableList roAdditionalShadows;
SerializedProperty spAdditionalShadows;
UIEffect uiEffect;

void OnEnable()
{
uiEffect = (target as UIShadow).GetComponent<UIEffect>();
spAdditionalShadows = serializedObject.FindProperty("m_AdditionalShadows");

roAdditionalShadows = new ReorderableList(serializedObject, spAdditionalShadows, true, true, true, true);
roAdditionalShadows.drawElementCallback = DrawElementCallback;
roAdditionalShadows.drawHeaderCallback = (rect) => EditorGUI.LabelField(rect, "Additional Shadows");
roAdditionalShadows.onAddCallback = OnAddCallback;
roAdditionalShadows.elementHeightCallback = ElementHeightCallback;

}

void OnAddCallback(ReorderableList ro)
{
spAdditionalShadows.InsertArrayElementAtIndex(ro.count);
var element = spAdditionalShadows.GetArrayElementAtIndex(ro.count - 1);
element.FindPropertyRelative("style").intValue = (int)UIShadow.ShadowStyle.Shadow;
element.FindPropertyRelative("effectColor").colorValue = Color.black;
element.FindPropertyRelative("effectDistance").vector2Value = new Vector2(1f, -1f);
element.FindPropertyRelative("useGraphicAlpha").boolValue = true;
element.FindPropertyRelative("blur").floatValue = 0.25f;
}

float ElementHeightCallback(int index)
{
var element = spAdditionalShadows.GetArrayElementAtIndex(index);
if (element.FindPropertyRelative("style").intValue == (int)UIShadow.ShadowStyle.None)
return 16;

return (uiEffect && uiEffect.blurMode != UIEffect.BlurMode.None ? 84 : 64) + (EditorGUIUtility.wideMode ? 0 : 18);
}

/// <summary>
///
/// </summary>
void DrawElementCallback(Rect rect, int index, bool isActive, bool isFocused)
{
var sp = roAdditionalShadows.serializedProperty.GetArrayElementAtIndex(index);

Rect r = new Rect(rect);
r.height = EditorGUIUtility.singleLineHeight;
var spMode = sp.FindPropertyRelative("style");
EditorGUI.PropertyField(r, spMode);
if (spMode.intValue == (int)UIShadow.ShadowStyle.None)
return;

r.y += r.height;
EditorGUI.PropertyField(r, sp.FindPropertyRelative("effectColor"));
r.y += r.height;
EditorGUI.PropertyField(r, sp.FindPropertyRelative("effectDistance"));
r.y += EditorGUIUtility.wideMode ? r.height : r.height * 2;
EditorGUI.PropertyField(r, sp.FindPropertyRelative("useGraphicAlpha"));

if (uiEffect && uiEffect.blurMode != UIEffect.BlurMode.None)
{
r.y += r.height;
EditorGUI.PropertyField(r, sp.FindPropertyRelative("blur"));
}
}

/// <summary>
/// Implement this function to make a custom inspector.
/// </summary>
public override void OnInspectorGUI()
{

serializedObject.Update();

//================
// Shadow setting.
//================
var spShadowMode = serializedObject.FindProperty("m_Style");
EditorGUILayout.PropertyField(spShadowMode);

// When shadow is enable, show parameters.
if (spShadowMode.intValue != (int)UIShadow.ShadowStyle.None)
{
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_EffectDistance"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_EffectColor"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_UseGraphicAlpha"));

if (uiEffect && uiEffect.blurMode != UIEffect.BlurMode.None)
{
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Blur"));
}
EditorGUI.indentLevel--;
}

//================
// Additional shadow setting.
//================
roAdditionalShadows.DoLayoutList();

serializedObject.ApplyModifiedProperties();
}
}
}
12 changes: 12 additions & 0 deletions Assets/UIEffect/Editor/UIShadowEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9073d31

Please sign in to comment.