Skip to content
This repository has been archived by the owner on Oct 2, 2022. It is now read-only.

Commit

Permalink
fix: custom help box, asset reference element
Browse files Browse the repository at this point in the history
  • Loading branch information
connffuused committed Dec 24, 2021
1 parent 0bf966b commit d316160
Show file tree
Hide file tree
Showing 6 changed files with 434 additions and 190 deletions.
78 changes: 74 additions & 4 deletions Editor/UI Toolkit/Components/Factory/AssetReference.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
using UnityEngine.UIElements;
using System;
using AssetLens.Reference;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using Object = UnityEngine.Object;

namespace AssetLens.UI.Component
{
public class AssetReference : TextElement
public class AssetReference : VisualElement
{
public new static readonly string ussClassName = "asset-reference";
private static readonly string ussClassName = "asset-reference";
private string guid;

private Button button;
private Image image;

public AssetReference() : this(string.Empty)
{
}
Expand All @@ -15,13 +23,75 @@ public AssetReference(string guid)
{
this.AddToClassList(AssetReference.ussClassName);
this.guid = guid;

this.button = new Button();
this.image = new Image();

this.Add(button);
this.button.Add(image);

EAssetCategory assetCategory = ReferenceUtil.GUID.GetAssetCategory(guid);

string assetPath = AssetDatabase.GUIDToAssetPath(guid);
var obj = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(assetPath);

switch (assetCategory)
{
case EAssetCategory.Object:
// @TODO : use uss style instead space
if (obj != null)
{
button.text = $" {obj.name} ({ReferenceUtil.AddSpacesToSentence(obj.GetType().Name)})";
button.tooltip = assetPath;
Texture img = EditorGUIUtility.ObjectContent(obj, obj.GetType()).image;
image.image = img;
image.AddToClassList("reference-view-image");
}
else
{
button.text = $" (null) (guid:{guid})";
button.tooltip = assetPath;
Texture img = EditorGUIUtility.ObjectContent(null, typeof(Object)).image;
image.image = img;
image.AddToClassList("reference-view-image");
}

break;

case EAssetCategory.DefaultResource:
button.text = "Default Resource";
button.SetEnabled(false);
break;

case EAssetCategory.BuiltInExtra:
button.text = "Built-in Resource";
button.SetEnabled(false);
break;

case EAssetCategory.Others:
button.text = "Other Internals";
button.SetEnabled(false);
break;

default:
throw new ArgumentOutOfRangeException();
}

// stylesheets
AddToClassList("reference-view-container");
this.button.AddToClassList("reference-view-button");

this.button.clickable.clicked += () =>
{
ReferenceUtil.Focus(obj);
};
}

public new class UxmlFactory : UnityEngine.UIElements.UxmlFactory<AssetReference, AssetReference.UxmlTraits>
{
}

public new class UxmlTraits : TextElement.UxmlTraits
public new class UxmlTraits : VisualElement.UxmlTraits
{
}
}
Expand Down
194 changes: 194 additions & 0 deletions Editor/UI Toolkit/Components/Factory/CustomHelpBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
using UnityEngine.UIElements;

namespace AssetLens.UI.Component
{
/// <summary>
/// User message types.
/// </summary>
public enum HelpBoxMessageType
{
/// <summary>
/// Neutral message.
/// </summary>
None = 0,
/// <summary>
/// Info message.
/// </summary>
Info = 1,
/// <summary>
/// Warning message.
/// </summary>
Warning = 2,
/// <summary>
/// Error message.
/// </summary>
Error = 3
}

/// <summary>
/// Makes a help box with a message to the user.
/// </summary>
/// <example>
/// <code>
/// public class HelpBoxExample : EditorWindow
/// {
/// [MenuItem("Example/Help Box")]
/// static void ShowWindow()
/// {
/// HelpBoxExample window = (HelpBoxExample)EditorWindow.GetWindow(typeof(HelpBoxExample));
/// window.Show();
/// }
///
/// void OnEnable()
/// {
/// rootVisualElement.Add(new HelpBox("This is a help box", HelpBoxMessageType.Info));
/// }
/// }
/// </code>
/// </example>
public class CustomHelpBox : VisualElement
{
/// <summary>
/// The USS class name for Elements of this type.
/// </summary>
public static readonly string ussClassName = "unity-help-box";
/// <summary>
/// The USS class name for labels in Elements of this type.
/// </summary>
public static readonly string labelUssClassName = ussClassName + "__label";
/// <summary>
/// The USS class name for images in Elements of this type.
/// </summary>
public static readonly string iconUssClassName = ussClassName + "__icon";
/// <summary>
/// The USS class name for the <see cref="HelpBoxMessageType.Info"/> state in Elements of this type.
/// </summary>
public static readonly string iconInfoUssClassName = iconUssClassName + "--info";
/// <summary>
/// The USS class name for the <see cref="HelpBoxMessageType.Warning"/> state in Elements of this type.
/// </summary>
public static readonly string iconwarningUssClassName = iconUssClassName + "--warning";
/// <summary>
/// The USS class name for the <see cref="HelpBoxMessageType.Error"/> state in Elements of this type.
/// </summary>
public static readonly string iconErrorUssClassName = iconUssClassName + "--error";

/// <summary>
/// Instantiates a <see cref="CustomHelpBox"/> with data from a UXML file.
/// </summary>
public new class UxmlFactory : UxmlFactory<CustomHelpBox, UxmlTraits> {}

/// <summary>
/// Defines <see cref="UxmlTraits"/> for the <see cref="CustomHelpBox"/>.
/// </summary>
public new class UxmlTraits : VisualElement.UxmlTraits
{
UxmlStringAttributeDescription m_Text = new UxmlStringAttributeDescription { name = "text" };
UxmlEnumAttributeDescription<HelpBoxMessageType> m_MessageType = new UxmlEnumAttributeDescription<HelpBoxMessageType>(){ name = "message-type", defaultValue = HelpBoxMessageType.None };

/// <summary>
/// Initializes <see cref="CustomHelpBox"/> properties with values from an attribute bag.
/// </summary>
/// <param name="ve">The Element to initialize.</param>
/// <param name="bag">The attribute bag.</param>
/// <param name="cc">The creation context; unused.</param>
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);

var helpBox = ve as CustomHelpBox;
helpBox.text = m_Text.GetValueFromBag(bag, cc);
helpBox.messageType = m_MessageType.GetValueFromBag(bag, cc);
}
}

HelpBoxMessageType m_HelpBoxMessageType;
VisualElement m_Icon;
string m_IconClass;
Label m_Label;

/// <summary>
/// The message text.
/// </summary>
public string text
{
get { return m_Label.text; }
set { m_Label.text = value; }
}

/// <summary>
/// The type of message.
/// </summary>
public HelpBoxMessageType messageType
{
get { return m_HelpBoxMessageType; }
set
{
if (value != m_HelpBoxMessageType)
{
m_HelpBoxMessageType = value;
UpdateIcon(value);
}
}
}

/// <summary>
/// Creates a new HelpBox.
/// </summary>
public CustomHelpBox() : this(string.Empty, HelpBoxMessageType.None) {}

/// <summary>
/// Creates a new HelpBox.
/// </summary>
/// <param name="text">The message text.</param>
/// <param name="messageType">The type of message.</param>
public CustomHelpBox(string text, HelpBoxMessageType messageType)
{
AddToClassList(ussClassName);

m_HelpBoxMessageType = messageType;
m_Label = new Label(text);
m_Label.AddToClassList(labelUssClassName);
Add(m_Label);

m_Icon = new VisualElement();
m_Icon.AddToClassList(iconUssClassName);
UpdateIcon(messageType);
}

string GetIconClass(HelpBoxMessageType messageType)
{
switch (messageType)
{
case HelpBoxMessageType.Info: return iconInfoUssClassName;
case HelpBoxMessageType.Warning: return iconwarningUssClassName;
case HelpBoxMessageType.Error: return iconErrorUssClassName;
}
return null;
}

void UpdateIcon(HelpBoxMessageType messageType)
{
// Remove the old style
if (!string.IsNullOrEmpty(m_IconClass))
{
m_Icon.RemoveFromClassList(m_IconClass);
}

m_IconClass = GetIconClass(messageType);

if (m_IconClass == null)
{
m_Icon.RemoveFromHierarchy();
}
else
{
m_Icon.AddToClassList(m_IconClass);
if (m_Icon.parent == null)
{
Insert(0, m_Icon);
}
}
}
}
}
3 changes: 3 additions & 0 deletions Editor/UI Toolkit/Components/Factory/CustomHelpBox.cs.meta

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

2 changes: 1 addition & 1 deletion Editor/UI Toolkit/Layouts/SubPanel/RV_Empty.uxml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" editor-extension-mode="True">
<ui:VisualElement name="root">
<ui:VisualElement name="box" style="height: 64px; max-height: 64px; min-height: 64px; flex-direction: row; align-items: center; background-color: rgb(113, 113, 113); border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; padding-left: 8px; padding-right: 8px; padding-top: 8px; padding-bottom: 8px;">
<ui:VisualElement name="icon" style="width: 36px; height: 36px; max-width: 36px; max-height: 36px; min-width: 36px; background-image: url(&apos;project://database/Packages/com.calci.assetlens/Contents/icons/[email protected]?fileID=2800000&amp;guid=c765d092b5f004c4e875f76a7f99f83c&amp;type=3#console.infoicon@2x&apos;); -unity-background-scale-mode: scale-and-crop;" />
<!-- <ui:VisualElement name="icon" style="width: 36px; height: 36px; max-width: 36px; max-height: 36px; min-width: 36px; background-image: url(&apos;project://database/Packages/com.calci.assetlens/Contents/icons/[email protected]?fileID=2800000&amp;guid=c765d092b5f004c4e875f76a7f99f83c&amp;type=3#console.infoicon@2x&apos;); -unity-background-scale-mode: scale-and-crop;" />-->
<ui:Label text="TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT " display-tooltip-when-elided="true" name="message" style="white-space: normal; color: rgba(255, 255, 255, 255);" />
</ui:VisualElement>
</ui:VisualElement>
Expand Down
Loading

0 comments on commit d316160

Please sign in to comment.