Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/JasonMa0012/LWGUI
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonMa0012 committed Sep 25, 2022
2 parents fcfad24 + 9a7d700 commit bd783f7
Show file tree
Hide file tree
Showing 14 changed files with 723 additions and 58 deletions.
164 changes: 131 additions & 33 deletions Editor/DrawerUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ internal enum SearchMode
internal class LWGUI : ShaderGUI
{
// EditorGUI.kSingleLineHeight
public static readonly float SingleLineHeight = 18f;
public static readonly float singleLineHeight = 18f;
public static readonly float helpboxSingleLineHeight = 12.5f;

public MaterialProperty[] props;
public MaterialEditor materialEditor;
Expand All @@ -49,18 +50,23 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro
this.eventType = RevertableHelper.InitAndHasShaderChanged(shader) ? EventType.Init : EventType.Repaint;

// drawer register metadata
if (eventType == EventType.Init && Event.current.type != UnityEngine.EventType.Repaint)
if (eventType == EventType.Init)
{
// reset all caches
MetaDataHelper.ClearCaches(shader);
searchResult = MetaDataHelper.SearchProperties(shader, props, String.Empty, SearchMode.All);
lastSearchingText = searchingText = string.Empty;
lastSearchMode = searchMode = SearchMode.All;
updateSearchMode = false;
foreach (var prop in props)

if (Event.current.type != UnityEngine.EventType.Repaint)
{
var height = materialEditor.GetPropertyHeight(prop, prop.displayName);
var rect = EditorGUILayout.GetControlRect(true, height, EditorStyles.layerMaskField);
materialEditor.ShaderProperty(rect, prop, prop.displayName);
foreach (var prop in props)
{
var height = materialEditor.GetPropertyHeight(prop, prop.displayName);
var rect = EditorGUILayout.GetControlRect(true, height, EditorStyles.layerMaskField);
materialEditor.ShaderProperty(rect, prop, prop.displayName);
}
}
}
// draw with metadata and searchingText
Expand Down Expand Up @@ -98,9 +104,26 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro
if ((prop.flags & MaterialProperty.PropFlags.HideInInspector) == 0 && searchResult[prop.name])
{
var height = materialEditor.GetPropertyHeight(prop, prop.displayName);

// ignored when in Folding Group
if (height <= 0) continue;

// helpbox
int lineCount;
var helpboxStr = MetaDataHelper.GetPropertyHelpbox(shader, prop, out lineCount);
if (!string.IsNullOrEmpty(helpboxStr))
{
var helpboxRect = EditorGUILayout.GetControlRect(true, 30f + Mathf.Max(0, lineCount - 2f) * helpboxSingleLineHeight);
if (MetaDataHelper.IsSubProperty(shader, prop))
{
EditorGUI.indentLevel++;
helpboxRect = EditorGUI.IndentedRect(helpboxRect);
EditorGUI.indentLevel--;
}
helpboxRect.xMax -= RevertableHelper.revertButtonWidth;
EditorGUI.HelpBox(helpboxRect, helpboxStr, MessageType.Info);
}

var rect = EditorGUILayout.GetControlRect(true, height, EditorStyles.layerMaskField);
var revertButtonRect = RevertableHelper.GetRevertButtonRect(prop, rect);
rect.xMax -= RevertableHelper.revertButtonWidth;
Expand All @@ -118,7 +141,7 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro
}

RevertableHelper.DrawRevertableProperty(revertButtonRect, prop, materialEditor, shader);
var label = new GUIContent(prop.displayName, "Property Name: " + prop.name);
var label = new GUIContent(prop.displayName, MetaDataHelper.GetPropertyTooltip(shader, prop));
materialEditor.ShaderProperty(rect, prop, label);
}
}
Expand All @@ -139,10 +162,6 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro
EditorGUILayout.Space();
Helper.DrawLogo();
}
else
{
Debug.LogError("Unknown Event Type");
}
}

/// <summary>
Expand Down Expand Up @@ -177,6 +196,7 @@ internal class GroupStateHelper
// Used to Conditional Display, key: keyword, value: is activated
private static Dictionary<Object, Dictionary<string, bool>> _keywords = new Dictionary<Object, Dictionary<string, bool>>();

// TODO: clear, reset to default, expand all, collapse all
private static void InitPoolPerMaterial(Object material)
{
if (!_groups.ContainsKey(material)) _groups.Add(material, new Dictionary<string, bool>());
Expand Down Expand Up @@ -280,7 +300,7 @@ internal class RevertableHelper
_defaultProps = new Dictionary<Shader, Dictionary<string, MaterialProperty>>();

private static Dictionary<Shader, int> _initTimers = new Dictionary<Shader, int>();
private const int INIT_PER_FRAMES = 15;
private const int INIT_PER_FRAMES = 0;


#region Init
Expand All @@ -304,10 +324,14 @@ public static bool InitAndHasShaderChanged(Shader shader)
else
_initTimers[shader] = 0;

if (_initTimers[shader] > INIT_PER_FRAMES
|| !_defaultProps.ContainsKey(shader)
|| ShaderUtil.GetPropertyCount(shader) != _defaultProps[shader].Count)
// fast refresh can better capture the shadeer modification
if (_initTimers[shader] > INIT_PER_FRAMES
|| !_defaultProps.ContainsKey(shader)
|| ShaderUtil.GetPropertyCount(shader) != _defaultProps[shader].Count
)
{
_initTimers[shader] = 0;
}
else
return false;

Expand Down Expand Up @@ -342,7 +366,7 @@ public static bool InitAndHasShaderChanged(Shader shader)

public static Rect GetRevertButtonRect(MaterialProperty prop, Rect rect, bool isCallInDrawer = false)
{
float defaultHeightWithoutDrawers = LWGUI.SingleLineHeight;
float defaultHeightWithoutDrawers = LWGUI.singleLineHeight;
return GetRevertButtonRect(defaultHeightWithoutDrawers, rect, isCallInDrawer);
}

Expand Down Expand Up @@ -741,7 +765,7 @@ public static bool DrawSearchField(ref string searchingText, ref SearchMode sear
EditorGUI.BeginChangeCheck();

var rect = EditorGUILayout.GetControlRect();
var revertButtonRect = RevertableHelper.GetRevertButtonRect(LWGUI.SingleLineHeight, rect);
var revertButtonRect = RevertableHelper.GetRevertButtonRect(LWGUI.singleLineHeight, rect);
rect.xMax -= RevertableHelper.revertButtonWidth;
// Get internal TextField ControlID
int controlId = GUIUtility.GetControlID(s_TextFieldHash, FocusType.Keyboard, rect) + 1;
Expand Down Expand Up @@ -1034,11 +1058,22 @@ internal class MetaDataHelper
{
private static Dictionary<Shader, Dictionary<string /*MainProp*/, List<string /*SubProp*/>>> _mainSubDic = new Dictionary<Shader, Dictionary<string, List<string>>>();
private static Dictionary<Shader, Dictionary<string /*GroupName*/, string /*MainProp*/>> _mainGroupNameDic = new Dictionary<Shader, Dictionary<string, string>>();
private static Dictionary<Shader, Dictionary<string /*PropName*/, string /*GroupName*/>> _propParentDic = new Dictionary<Shader, Dictionary<string, string>>();

private static Dictionary<Shader, Dictionary<string /*PropName*/, List<string /*ExtraPropName*/>>>_extraPropDic = new Dictionary<Shader, Dictionary<string, List<string>>>();
private static Dictionary<Shader, Dictionary<string /*PropName*/, List<string /*Tooltip*/>>> _tooltipDic = new Dictionary<Shader, Dictionary<string, List<string>>>();
private static Dictionary<Shader, Dictionary<string /*PropName*/, List<string /*Helpbox*/>>> _HelpboxDic = new Dictionary<Shader, Dictionary<string, List<string>>>();
private static Dictionary<Shader, Dictionary<string /*PropName*/, List<string /*Helpbox*/>>> _helpboxDic = new Dictionary<Shader, Dictionary<string, List<string>>>();

public static void ClearCaches(Shader shader)
{
if (_mainSubDic.ContainsKey(shader)) _mainSubDic[shader].Clear();
if (_mainGroupNameDic.ContainsKey(shader)) _mainGroupNameDic[shader].Clear();
if (_propParentDic.ContainsKey(shader)) _propParentDic[shader].Clear();
if (_extraPropDic.ContainsKey(shader)) _extraPropDic[shader].Clear();
if (_tooltipDic.ContainsKey(shader)) _tooltipDic[shader].Clear();
if (_helpboxDic.ContainsKey(shader)) _helpboxDic[shader].Clear();
}

public static void RegisterMainProp(Shader shader, MaterialProperty prop, string group)
{
if (_mainSubDic.ContainsKey(shader))
Expand Down Expand Up @@ -1070,9 +1105,9 @@ public static void RegisterMainProp(Shader shader, MaterialProperty prop, string

public static void RegisterSubProp(Shader shader, MaterialProperty prop, string group, MaterialProperty[] extraProps = null)
{
// add to _mainSubDic
if (!string.IsNullOrEmpty(group) && group != "_")
{
// add to _mainSubDic
if (_mainGroupNameDic.ContainsKey(shader))
{
var groupName = _mainGroupNameDic[shader].Keys.First((s => group.Contains(s)));
Expand All @@ -1086,13 +1121,20 @@ public static void RegisterSubProp(Shader shader, MaterialProperty prop, string
}
else
Debug.LogError($"Unregistered Main Property:{mainPropName}");

// add to _propParentDic
if (!_propParentDic.ContainsKey(shader))
_propParentDic.Add(shader, new Dictionary<string, string>());
if (!_propParentDic[shader].ContainsKey(prop.name))
_propParentDic[shader].Add(prop.name, groupName);
}
else
Debug.LogError($"Unregistered Main Group Name:{group}");
}
else
Debug.LogError($"Unregistered Shader:{shader.name}");
}

// add to _extraPropDic
if (extraProps != null)
{
Expand All @@ -1111,6 +1153,55 @@ public static void RegisterSubProp(Shader shader, MaterialProperty prop, string
}
}

private static void RegisterPropertyString(Shader shader, MaterialProperty prop, string str, Dictionary<Shader, Dictionary<string, List<string>>> dst)
{
if (!dst.ContainsKey(shader))
dst.Add(shader, new Dictionary<string, List<string>>());
if (!dst[shader].ContainsKey(prop.name))
dst[shader].Add(prop.name, new List<string>());
dst[shader][prop.name].Add(str);
}

private static string GetPropertyString(Shader shader, MaterialProperty prop, Dictionary<Shader, Dictionary<string, List<string>>> src, out int lineCount)
{
var str = string.Empty;
lineCount = 0;
if (src.ContainsKey(shader) && src[shader].ContainsKey(prop.name))
{
foreach (var tooltip in src[shader][prop.name])
{
str += tooltip + "\n";
lineCount++;
}
}
return str;
}

public static void RegisterPropertyTooltip(Shader shader, MaterialProperty prop, string tooltip)
{
RegisterPropertyString(shader, prop, tooltip, _tooltipDic);
}

public static string GetPropertyTooltip(Shader shader, MaterialProperty prop)
{
var str = GetPropertyString(shader, prop, _tooltipDic, out _);
if (!string.IsNullOrEmpty(str))
str += "\n\n";
str += "Property Name: " + prop.name;
return str;
}

public static void RegisterPropertyHelpbox(Shader shader, MaterialProperty prop, string tooltip)
{
RegisterPropertyString(shader, prop, tooltip, _helpboxDic);
}

public static string GetPropertyHelpbox(Shader shader, MaterialProperty prop, out int lineCount)
{
var str = GetPropertyString(shader, prop, _helpboxDic, out lineCount);
return str;
}

public static Dictionary<string, bool> SearchProperties(Shader shader, MaterialProperty[] props, string searchingText, SearchMode searchMode)
{
var result = new Dictionary<string, bool>();
Expand Down Expand Up @@ -1151,22 +1242,21 @@ public static Dictionary<string, bool> SearchProperties(Shader shader, MaterialP
}
}

// fuzzy search
var name = prop.displayName.ToLower();
// whole word match search
var isMatch = false;
var displayName = prop.displayName.ToLower();
var name = prop.name.ToLower();
searchingText = searchingText.ToLower();
foreach (var searchingChar in searchingText)

var keywords = searchingText.Split(' ', ',', ';', '|', '*', '&');// Some possible separators

foreach (var keyword in keywords)
{
var index = name.IndexOf(searchingChar);
if (index < 0)
{
contains = false;
break;
}
else
{
name = name.Remove(index, 1);
}
isMatch |= displayName.Contains(keyword);
isMatch |= name.Contains(keyword);
contains &= isMatch;
}

result.Add(prop.name, contains);
}

Expand Down Expand Up @@ -1196,6 +1286,14 @@ public static Dictionary<string, bool> SearchProperties(Shader shader, MaterialP

return result;
}

public static bool IsSubProperty(Shader shader, MaterialProperty prop)
{
var isSubProp = false;
if (_propParentDic.ContainsKey(shader) && _propParentDic[shader].ContainsKey(prop.name))
isSubProp = true;
return isSubProp;
}
}

} //namespace LWGUI
41 changes: 41 additions & 0 deletions Editor/ShaderDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l
/// Similar to Header()
/// group:father group name, support suffix keyword for conditional display (Default: none)
/// header: string to display, "SpaceLine" or "_" = none (Default: none)
/// tips: Modifying the Decorator parameters in Shader requires manually refreshing the GUI instance by throwing an exception
/// </summary>
internal class TitleDecorator : SubDrawer
{
Expand All @@ -937,4 +938,44 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l
}
}

/// <summary>
/// Tooltip, describes the details of the property. (Default: "Property Name:" + property.name)
/// tooltip:a single-line string to display, you can use more than one Tooltip() to represent multiple lines. (Default: Newline)
/// tips: Modifying Decorator parameters in Shader requires refreshing the cache by modifying the Property default value
/// </summary>
internal class TooltipDecorator : SubDrawer
{
private string _tooltip;

public TooltipDecorator() { }
public TooltipDecorator(string tooltip) { this._tooltip = tooltip; }

protected override float GetVisibleHeight(MaterialProperty prop) { return 0; }

public override void Init(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor)
{
MetaDataHelper.RegisterPropertyTooltip(shader, prop, _tooltip);
}

public override void DrawProp(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor) { }
}

/// <summary>
/// Display a Helpbox on the property
/// message:a single-line string to display, you can use more than one Helpbox() to represent multiple lines. (Default: Newline)
/// tips: Modifying Decorator parameters in Shader requires refreshing the cache by modifying the Property default value
/// </summary>
internal class HelpboxDecorator : TooltipDecorator
{
private string _message;

public HelpboxDecorator() { }
public HelpboxDecorator(string message) { this._message = message; }

public override void Init(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor)
{
MetaDataHelper.RegisterPropertyHelpbox(shader, prop, _message);
}
}

} //namespace LWGUI
23 changes: 12 additions & 11 deletions Editor/Test/SampleDrawer 1.shader
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ Shader "Hidden"
[Space]
[Title(Channel Samples)]
[Channel] _textureChannelMask("Texture Channel Mask (Default G)", Vector) = (0,1,0,0)

[Title(Metadata Samples)]
[Tooltip(Test multiline Tooltip)]
[Tooltip()]
[Tooltip(Line 3)]
[Tooltip()]
[Tooltip(Line 5)]
_float_tooltip ("Float with Tooltips", float) = 0
[Helpbox(Test multiline Helpbox)]
[Helpbox(Line2)]
[Helpbox(Line3)]
_float_helpbox ("Float with Helpbox", float) = 2


[Space]
Expand All @@ -57,15 +69,6 @@ Shader "Hidden"
[HideInInspector] _mColor2 (" ", Color) = (0, 1, 0, 1)
[HideInInspector] [HDR] _mColor3 (" ", Color) = (0, 0, 1, 1)

[Tex(_, _color2)] _tex_color2 ("Tex with Color", 2D) = "white" { }
[HideInInspector] _color2 (" ", Color) = (1, 0, 0, 1)
[Tex(_, _float5)] _tex_float2 ("Tex with Float", 2D) = "white" { }
[HideInInspector] _float5 (" ", float) = 0
[Tex(_, _range2)] _tex_range2 ("Tex with Range", 2D) = "white" { }
[HideInInspector] _range2 (" ", Range(0,1)) = 0
[Tex(_, _textureChannelMask2)] _tex_channel2 ("Tex with Channel", 2D) = "white" { }
[HideInInspector] _textureChannelMask2(" ", Vector) = (0,0,0,1)

[Space]
[Title(Ramp Samples)]
[Ramp] _Ramp ("Ramp Map", 2D) = "white" { }
Expand All @@ -76,8 +79,6 @@ Shader "Hidden"
[MinMaxSlider(_rangeStart, _rangeEnd)] _minMaxSlider("Min Max Slider (0 - 1)", Range(0.0, 1.0)) = 1.0
_rangeStart("Range Start", Range(0.0, 0.5)) = 0.0
[PowerSlider(10)] _rangeEnd("Range End PowerSlider", Range(0.5, 1.0)) = 1.0


}

HLSLINCLUDE
Expand Down
Loading

0 comments on commit bd783f7

Please sign in to comment.