Skip to content

Commit

Permalink
feat: Unified Image creation method
Browse files Browse the repository at this point in the history
  • Loading branch information
SolarianZ committed Nov 6, 2024
1 parent 7c3f7af commit 918cb95
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 97 deletions.
21 changes: 21 additions & 0 deletions Editor/EditorMessageUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using UnityEditor;
using UnityEngine;
using UnityEngine.TextCore.Text;
using UnityEngine.UIElements;
using UObject = UnityEngine.Object;

namespace GBG.EditorMessages.Editor
Expand Down Expand Up @@ -94,6 +95,26 @@ public static Texture GetCustomDataIcon()
return EditorGUIUtility.IconContent("animation.play").image;
}


public static Image NewImage(Texture image = null, string tooltip = null, DisplayStyle display = DisplayStyle.Flex)
{
Image imageElement = new Image
{
tooltip = tooltip,
image = image,
style =
{
display = display,
alignSelf = Align.Center,
minWidth = GlobalIconSize,
maxWidth = GlobalIconSize,
minHeight = GlobalIconSize,
maxHeight = GlobalIconSize,
}
};
return imageElement;
}

#endregion


Expand Down
27 changes: 3 additions & 24 deletions Editor/MessageBanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,7 @@ public MessageBanner(IList<Message> messages, object source, string sourceName,

float iconSize = EditorMessageUtility.GlobalIconSize;

TypeImage = new Image
{
style =
{
alignSelf = Align.Center,
minWidth = iconSize,
maxWidth = iconSize,
minHeight = iconSize,
maxHeight = iconSize,
}
};
TypeImage = EditorMessageUtility.NewImage();
Add(TypeImage);

MessageLabel = new Label
Expand Down Expand Up @@ -130,19 +120,8 @@ public MessageBanner(IList<Message> messages, object source, string sourceName,

private Image CreateMessageTypeImage(Texture defaultIcon, float iconSize)
{
Image image = new Image
{
image = defaultIcon,
style =
{
display = ShowMessageTypeCount ? DisplayStyle.Flex : DisplayStyle.None,
alignSelf = Align.Center,
minWidth = iconSize,
maxWidth = iconSize,
minHeight = iconSize,
maxHeight = iconSize,
}
};
Image image = EditorMessageUtility.NewImage(defaultIcon,
display: ShowMessageTypeCount ? DisplayStyle.Flex : DisplayStyle.None);
return image;
}

Expand Down
14 changes: 1 addition & 13 deletions Editor/MessageDetailsTabElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,7 @@ public MessageDetailsTabElement(Texture texture, string tooltip, Action onClick)
style.height = iconSize;
this.tooltip = tooltip;

Icon = new Image
{
image = texture,
style =
{
alignSelf = Align.Center,
minWidth = iconSize,
maxWidth = iconSize,
minHeight = iconSize,
maxHeight = iconSize,
//backgroundColor = inactiveColor,
}
};
Icon = EditorMessageUtility.NewImage(texture);
Add(Icon);

RegisterCallback<ClickEvent>(evt => onClick?.Invoke());
Expand Down
38 changes: 3 additions & 35 deletions Editor/MessageElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,7 @@ public MessageElement()
};
Add(LineNumberLabel);

TypeImage = new Image
{
style =
{
alignSelf = Align.Center,
minWidth = iconSize,
maxWidth = iconSize,
minHeight = iconSize,
maxHeight = iconSize,
}
};
TypeImage = EditorMessageUtility.NewImage();
Add(TypeImage);

TimestampLabel = new Label
Expand Down Expand Up @@ -98,32 +88,10 @@ public MessageElement()
};
Add(MessageLabel);

ContextImage = new Image
{
tooltip = "This message has context.",
style =
{
alignSelf = Align.Center,
minWidth = iconSize,
maxWidth = iconSize,
minHeight = iconSize,
maxHeight = iconSize,
}
};
ContextImage = EditorMessageUtility.NewImage(tooltip: "This message has context.");
Add(ContextImage);

CustomDataImage = new Image
{
tooltip = "This message has custom data.",
style =
{
alignSelf = Align.Center,
minWidth = iconSize,
maxWidth = iconSize,
minHeight = iconSize,
maxHeight = iconSize,
}
};
CustomDataImage = EditorMessageUtility.NewImage(tooltip: "This message has custom data.");
Add(CustomDataImage);

RegisterCallback<ClickEvent>(OnClick);
Expand Down
12 changes: 1 addition & 11 deletions Editor/MessageTypeToggle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,7 @@ public MessageTypeToggle(bool value)

float iconSize = EditorMessageUtility.GlobalIconSize;

_typeImage = new Image
{
style =
{
alignSelf = Align.Center,
minWidth = iconSize,
maxWidth = iconSize,
minHeight = iconSize,
maxHeight = iconSize,
}
};
_typeImage = EditorMessageUtility.NewImage();
Insert(0, _typeImage);
}

Expand Down
23 changes: 10 additions & 13 deletions Editor/MessageViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,8 @@ private void CreateGUI()
toolbar.Add(_searchField);

// Regex Error Image
_regexErrorImage = new Image
{
image = EditorMessageUtility.GetErrorIcon(),
style =
{
alignSelf = Align.Center,
display = DisplayStyle.None,
minWidth = iconSize,
maxWidth = iconSize,
minHeight = iconSize,
maxHeight = iconSize,
}
};
_regexErrorImage = EditorMessageUtility.NewImage(EditorMessageUtility.GetErrorIcon(),
display: DisplayStyle.None);
toolbar.Add(_regexErrorImage);

// Regex Toggle
Expand Down Expand Up @@ -332,6 +321,14 @@ private void OnDisable()
}
}

private void ShowButton(Rect pos)
{
if (GUI.Button(pos, EditorGUIUtility.IconContent("_Help"), GUI.skin.FindStyle("IconButton")))
{
Application.OpenURL("https://github.com/SolarianZ/UnityEditorMessages");
}
}

#endregion


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.greenbamboogames.editormessages",
"version": "1.0.1",
"version": "1.0.2",
"displayName": "Editor Messages!",
"description": "Show custom messages in Unity Editor.",
"unity": "2022.3",
Expand Down

0 comments on commit 918cb95

Please sign in to comment.