Bez popisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IsometricRuleTileEditor.cs 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using UnityEngine;
  2. using UnityEngine.Tilemaps;
  3. namespace UnityEditor
  4. {
  5. /// <summary>
  6. /// The Editor for an IsometricRuleTile.
  7. /// </summary>
  8. [CustomEditor(typeof(IsometricRuleTile), true)]
  9. [CanEditMultipleObjects]
  10. public class IsometricRuleTileEditor : RuleTileEditor
  11. {
  12. /// <summary>
  13. /// Gets the index for a Rule with the IsometricRuleTileEditor to display an arrow.
  14. /// </summary>
  15. /// <param name="position">The adjacent position of the arrow.</param>
  16. /// <returns>Returns the index for a Rule with the IsometricRuleTileEditor to display an arrow</returns>
  17. private static readonly int[] s_Arrows = { 3, 0, 1, 6, -1, 2, 7, 8, 5 };
  18. /// <summary>
  19. /// Gets the index for a Rule with the HexagonalRuleTile to display an arrow.
  20. /// </summary>
  21. /// <param name="position">The adjacent position of the arrow.</param>
  22. /// <returns>Returns the index for a Rule with the HexagonalRuleTile to display an arrow.</returns>
  23. public override int GetArrowIndex(Vector3Int position)
  24. {
  25. return s_Arrows[base.GetArrowIndex(position)];
  26. }
  27. /// <summary>
  28. /// Gets the GUI matrix size for a Rule of a IsometricRuleTile
  29. /// </summary>
  30. /// <param name="bounds">Cell bounds of the Rule.</param>
  31. /// <returns>Returns the GUI matrix size for a Rule of a IsometricRuleTile.</returns>
  32. public override Vector2 GetMatrixSize(BoundsInt bounds)
  33. {
  34. float p = Mathf.Pow(2, 0.5f);
  35. float w = (bounds.size.x / p + bounds.size.y / p) * k_SingleLineHeight;
  36. return new Vector2(w, w);
  37. }
  38. /// <summary>
  39. /// Draws a Rule Matrix for the given Rule for a IsometricRuleTile.
  40. /// </summary>
  41. /// <param name="tile">Tile to draw rule for.</param>
  42. /// <param name="rect">GUI Rect to draw rule at.</param>
  43. /// <param name="bounds">Cell bounds of the Rule.</param>
  44. /// <param name="tilingRule">Rule to draw Rule Matrix for.</param>
  45. public override void RuleMatrixOnGUI(RuleTile tile, Rect rect, BoundsInt bounds, RuleTile.TilingRule tilingRule)
  46. {
  47. Handles.color = EditorGUIUtility.isProSkin ? new Color(1f, 1f, 1f, 0.2f) : new Color(0f, 0f, 0f, 0.2f);
  48. float w = rect.width / bounds.size.x;
  49. float h = rect.height / bounds.size.y;
  50. // Grid
  51. float d = rect.width / (bounds.size.x + bounds.size.y);
  52. for (int y = 0; y <= bounds.size.y; y++)
  53. {
  54. float left = rect.xMin + d * y;
  55. float top = rect.yMin + d * y;
  56. float right = rect.xMax - d * (bounds.size.y - y);
  57. float bottom = rect.yMax - d * (bounds.size.y - y);
  58. Handles.DrawLine(new Vector3(left, bottom), new Vector3(right, top));
  59. }
  60. for (int x = 0; x <= bounds.size.x; x++)
  61. {
  62. float left = rect.xMin + d * x;
  63. float top = rect.yMax - d * x;
  64. float right = rect.xMax - d * (bounds.size.x - x);
  65. float bottom = rect.yMin + d * (bounds.size.x - x);
  66. Handles.DrawLine(new Vector3(left, bottom), new Vector3(right, top));
  67. }
  68. Handles.color = Color.white;
  69. var neighbors = tilingRule.GetNeighbors();
  70. // Icons
  71. float iconSize = (rect.width - d) / (bounds.size.x + bounds.size.y - 1);
  72. float iconScale = Mathf.Pow(2, 0.5f);
  73. for (int y = bounds.yMin; y < bounds.yMax; y++)
  74. {
  75. for (int x = bounds.xMin; x < bounds.xMax; x++)
  76. {
  77. Vector3Int pos = new Vector3Int(x, y, 0);
  78. Vector3Int offset = new Vector3Int(pos.x - bounds.xMin, pos.y - bounds.yMin, 0);
  79. Rect r = new Rect(
  80. rect.xMin + rect.size.x - iconSize * (offset.y - offset.x + 0.5f + bounds.size.x),
  81. rect.yMin + rect.size.y - iconSize * (offset.y + offset.x + 1.5f),
  82. iconSize, iconSize
  83. );
  84. Vector2 center = r.center;
  85. r.size *= iconScale;
  86. r.center = center;
  87. RuleMatrixIconOnGUI(tilingRule, neighbors, pos, r);
  88. }
  89. }
  90. }
  91. /// <summary>
  92. /// Determines the current mouse position is within the given Rect.
  93. /// </summary>
  94. /// <param name="rect">Rect to test mouse position for.</param>
  95. /// <returns>True if the current mouse position is within the given Rect. False otherwise.</returns>
  96. public override bool ContainsMousePosition(Rect rect)
  97. {
  98. var center = rect.center;
  99. var halfWidth = rect.width / 2;
  100. var halfHeight = rect.height / 2;
  101. var mouseFromCenter = Event.current.mousePosition - center;
  102. var xAbs = Mathf.Abs(Vector2.Dot(mouseFromCenter, Vector2.right));
  103. var yAbs = Mathf.Abs(Vector2.Dot(mouseFromCenter, Vector2.up));
  104. return (xAbs / halfWidth + yAbs / halfHeight) <= 1;
  105. }
  106. /// <summary>
  107. /// Updates preview settings for the IsometricRuleTile.
  108. /// </summary>
  109. public override void OnPreviewSettings()
  110. {
  111. base.OnPreviewSettings();
  112. if (m_PreviewGrid)
  113. {
  114. float height = EditorGUILayout.FloatField("Cell Height", m_PreviewGrid.cellSize.y);
  115. m_PreviewGrid.cellSize = new Vector3(1f, Mathf.Max(height, 0), 1f);
  116. }
  117. }
  118. /// <summary>
  119. /// Creates a Preview for the IsometricRuleTile.
  120. /// </summary>
  121. protected override void CreatePreview()
  122. {
  123. base.CreatePreview();
  124. m_PreviewGrid.cellSize = new Vector3(1f, 0.5f, 1f);
  125. m_PreviewGrid.cellLayout = GridLayout.CellLayout.Isometric;
  126. foreach (var tilemapRenderer in m_PreviewTilemapRenderers)
  127. tilemapRenderer.sortOrder = TilemapRenderer.SortOrder.TopRight;
  128. m_PreviewTilemapRenderers[0].sortingOrder = 0;
  129. m_PreviewTilemapRenderers[1].sortingOrder = -1;
  130. m_PreviewTilemapRenderers[2].sortingOrder = 1;
  131. m_PreviewTilemapRenderers[3].sortingOrder = 0;
  132. }
  133. }
  134. }