This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: custom help box, asset reference element
- Loading branch information
1 parent
0bf966b
commit d316160
Showing
6 changed files
with
434 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('project://database/Packages/com.calci.assetlens/Contents/icons/[email protected]?fileID=2800000&guid=c765d092b5f004c4e875f76a7f99f83c&type=3#console.infoicon@2x'); -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('project://database/Packages/com.calci.assetlens/Contents/icons/[email protected]?fileID=2800000&guid=c765d092b5f004c4e875f76a7f99f83c&type=3#console.infoicon@2x'); -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> | ||
|
Oops, something went wrong.