NodeBadgeView.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEditor.UIElements;
  4. using UnityEngine.UIElements;
  5. using UnityEditor.Experimental.GraphView;
  6. namespace GraphProcessor
  7. {
  8. public class NodeBadgeView : IconBadge
  9. {
  10. Label label;
  11. Texture icon;
  12. Color color;
  13. bool isCustom;
  14. public NodeBadgeView(string message, NodeMessageType messageType)
  15. {
  16. switch (messageType)
  17. {
  18. case NodeMessageType.Warning:
  19. CreateCustom(message, EditorGUIUtility.IconContent("Collab.Warning").image, Color.yellow);
  20. break ;
  21. case NodeMessageType.Error:
  22. CreateCustom(message, EditorGUIUtility.IconContent("Collab.Warning").image, Color.red);
  23. break ;
  24. case NodeMessageType.Info:
  25. CreateCustom(message, EditorGUIUtility.IconContent("console.infoicon").image, Color.white);
  26. break ;
  27. default:
  28. case NodeMessageType.None:
  29. CreateCustom(message, null, Color.grey);
  30. break ;
  31. }
  32. }
  33. public NodeBadgeView(string message, Texture icon, Color color)
  34. {
  35. CreateCustom(message, icon, color);
  36. }
  37. void CreateCustom(string message, Texture icon, Color color)
  38. {
  39. badgeText = message;
  40. this.color = color;
  41. var image = this.Q< Image >("icon");
  42. image.image = icon;
  43. image.style.backgroundColor = color;
  44. style.color = color;
  45. // This will set a class name containing the hash code of the string
  46. // We use this little trick to retrieve the label once it is added to the graph
  47. visualStyle = badgeText.GetHashCode().ToString();
  48. }
  49. protected override void ExecuteDefaultAction(EventBase evt)
  50. {
  51. // When the mouse enter the icon, this will add the label to the hierarchy
  52. base.ExecuteDefaultAction(evt);
  53. if (evt.eventTypeId == MouseEnterEvent.TypeId())
  54. {
  55. // And then we can fetch it here:
  56. GraphView gv = GetFirstAncestorOfType<GraphView>();
  57. var label = gv.Q<Label>(classes: new string[]{"icon-badge__text--" + badgeText.GetHashCode()});
  58. if (label != null)
  59. label.style.color = color;
  60. }
  61. }
  62. }
  63. }