Hexagon.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using UnityEngine;
  3. namespace MPUIKIT {
  4. /// <summary>
  5. /// Hexagon shape with two opposite parallel equal sides. It is basically a rectangle where two
  6. /// of it's sides are broken into halves and pulled out of the shape that creates two triangle tips
  7. /// left and right of the shape.
  8. /// </summary>
  9. [Serializable]
  10. public struct Hexagon: IMPUIComponent {
  11. [SerializeField] private Vector4 m_CornerRadius;
  12. [SerializeField] private bool m_UniformCornerRadius;
  13. [SerializeField] private Vector2 m_TipSize;
  14. [SerializeField] private bool m_UniformTipSize;
  15. [SerializeField] private Vector2 m_TipRadius;
  16. [SerializeField] private bool m_UniformTipRadius;
  17. /// <summary>
  18. /// Sizes of the two tips (the triangular part sticking out of the rectangular part of the shape)
  19. /// <para>x => left tip, y => right tip</para>
  20. /// </summary>
  21. public Vector2 TipSize {
  22. get => m_TipSize;
  23. set {
  24. m_TipSize = Vector2.Max(value, Vector2.one);
  25. if (ShouldModifySharedMat) {
  26. SharedMat.SetVector(SpHexagonTipSizes, m_TipSize);
  27. }
  28. OnComponentSettingsChanged?.Invoke(this, EventArgs.Empty);
  29. }
  30. }
  31. /// <summary>
  32. /// Radius of the corner of the tips.
  33. /// <para>x => left tip's corner, y => right tip's corner</para>
  34. /// </summary>
  35. public Vector2 TipRadius {
  36. get => m_TipRadius;
  37. set {
  38. m_TipRadius = Vector2.Max(value, Vector2.one);
  39. if (ShouldModifySharedMat) {
  40. SharedMat.SetVector(SpHexagonTipRadius, m_TipRadius);
  41. }
  42. OnComponentSettingsChanged?.Invoke(this, EventArgs.Empty);
  43. }
  44. }
  45. /// <summary>
  46. /// <para>Radius of the four corners of the rectangular part of the shape.
  47. /// Counter-Clockwise from bottom-left</para>
  48. /// <para>x => bottom-left, y => bottom-right</para>
  49. /// <para>z => top-right, w => top-left</para>
  50. /// </summary>
  51. public Vector4 CornerRadius {
  52. get => m_CornerRadius;
  53. set {
  54. m_CornerRadius = Vector4.Max(value, Vector4.one);
  55. if (ShouldModifySharedMat) {
  56. SharedMat.SetVector(SpHexagonRectCornerRadius, m_CornerRadius);
  57. }
  58. OnComponentSettingsChanged?.Invoke(this, EventArgs.Empty);
  59. }
  60. }
  61. private static readonly int SpHexagonTipSizes = Shader.PropertyToID("_HexagonTipSize");
  62. private static readonly int SpHexagonTipRadius = Shader.PropertyToID("_HexagonTipRadius");
  63. private static readonly int SpHexagonRectCornerRadius = Shader.PropertyToID("_HexagonCornerRadius");
  64. public Material SharedMat { get; set; }
  65. public bool ShouldModifySharedMat { get; set; }
  66. public RectTransform RectTransform { get; set; }
  67. public void Init(Material sharedMat, Material renderMat, RectTransform rectTransform) {
  68. SharedMat = sharedMat;
  69. ShouldModifySharedMat = sharedMat == renderMat;
  70. RectTransform = rectTransform;
  71. TipRadius = m_TipRadius;
  72. TipSize = m_TipSize;
  73. }
  74. public event EventHandler OnComponentSettingsChanged;
  75. public void OnValidate() {
  76. CornerRadius = m_CornerRadius;
  77. TipSize = m_TipSize;
  78. TipRadius = m_TipRadius;
  79. }
  80. public void InitValuesFromMaterial(ref Material material) {
  81. m_CornerRadius = material.GetVector(SpHexagonRectCornerRadius);
  82. m_TipRadius = material.GetVector(SpHexagonTipRadius);
  83. m_TipSize = material.GetVector(SpHexagonTipSizes);
  84. }
  85. public void ModifyMaterial(ref Material material, params object[] otherProperties) {
  86. material.SetVector(SpHexagonTipSizes, m_TipSize);
  87. material.SetVector(SpHexagonTipRadius, m_TipRadius);
  88. material.SetVector(SpHexagonRectCornerRadius, m_CornerRadius);
  89. }
  90. }
  91. }