BoardSurface.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace DevilLink2.Presentation
  4. {
  5. /// <summary>Inset surface details stay inside the board's existing padding and never intercept input.</summary>
  6. [DisallowMultipleComponent]
  7. public sealed class BoardSurface : BaseMeshEffect
  8. {
  9. private BoardLayout layout;
  10. private int columns, rows;
  11. public void SetGrid(BoardLayout value, int columnCount, int rowCount)
  12. {
  13. if (columns == columnCount && rows == rowCount && layout.Size == value.Size && layout.TileSize == value.TileSize) return;
  14. layout = value; columns = columnCount; rows = rowCount; graphic.SetVerticesDirty();
  15. }
  16. public override void ModifyMesh(VertexHelper mesh)
  17. {
  18. if (!IsActive()) return;
  19. var rect = ((RectTransform)transform).rect;
  20. mesh.Clear();
  21. // Gameplay uses the one shared outer stage. The matrix surface contributes
  22. // only per-cell slots; no matrix-sized rectangle, fill or inner frame remains.
  23. if (columns <= 0 || rows <= 0) {
  24. // Preserve the standalone legacy surface, which has no configured grid/stage.
  25. Quad(mesh, rect, new Color32(18,20,37,255), new Color32(11,14,27,255));
  26. Frame(mesh, rect, new Color32(110,94,62,255));
  27. var inner = new Rect(rect.x + 4, rect.y + 4, rect.width - 8, rect.height - 8);
  28. if (inner.width > 2 && inner.height > 2) Frame(mesh, inner, new Color32(37,40,62,255));
  29. return;
  30. }
  31. if (columns > 0 && rows > 0) {
  32. for (int r = 0; r < rows; r++) for (int c = 0; c < columns; c++) {
  33. var p = layout.TilePosition(r, c);
  34. var cell = new Rect(rect.xMin + p.x, rect.yMax + p.y - layout.TileSize, layout.TileSize, layout.TileSize);
  35. Quad(mesh, cell, new Color32(32, 25, 46, 255), new Color32(23, 19, 35, 255));
  36. Frame(mesh, cell, new Color32(48, 39, 59, 255));
  37. }
  38. }
  39. }
  40. private static void Frame(VertexHelper mesh, Rect rect, Color32 color)
  41. {
  42. if (rect.width < 2 || rect.height < 2) return;
  43. Quad(mesh, new Rect(rect.x, rect.y, rect.width, 1), color, color);
  44. Quad(mesh, new Rect(rect.x, rect.yMax - 1, rect.width, 1), color, color);
  45. Quad(mesh, new Rect(rect.x, rect.y + 1, 1, rect.height - 2), color, color);
  46. Quad(mesh, new Rect(rect.xMax - 1, rect.y + 1, 1, rect.height - 2), color, color);
  47. }
  48. private static void Quad(VertexHelper mesh, Rect rect, Color32 top, Color32 bottom)
  49. {
  50. int start = mesh.currentVertCount;
  51. mesh.AddVert(new Vector3(rect.xMin, rect.yMin), bottom, Vector2.zero);
  52. mesh.AddVert(new Vector3(rect.xMin, rect.yMax), top, Vector2.zero);
  53. mesh.AddVert(new Vector3(rect.xMax, rect.yMax), top, Vector2.zero);
  54. mesh.AddVert(new Vector3(rect.xMax, rect.yMin), bottom, Vector2.zero);
  55. mesh.AddTriangle(start, start + 1, start + 2);
  56. mesh.AddTriangle(start + 2, start + 3, start);
  57. }
  58. }
  59. }