sea-battle/Assets/Scripts/UI/ButtonGridLayout.cs

64 lines
2.1 KiB
C#

using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// Вешается на Screen-панель.
/// Задаёт ряды кнопок для 2D-навигации в PlayerCursorUI:
/// Up / Down — переключение между рядами
/// Left / Right — переключение внутри ряда
///
/// Пример (главное меню):
/// Row 0 → кнопка «Правила»
/// Row 1 → кнопка «PvP», кнопка «PvB»
/// </summary>
public class ButtonGridLayout : MonoBehaviour
{
[System.Serializable]
public class Row
{
public Button[] buttons;
}
[SerializeField] public Row[] rows;
// ─── Query helpers ────────────────────────────────────────────
public bool TryFindButton(Button btn, out int row, out int col)
{
if (rows != null)
{
for (int r = 0; r < rows.Length; r++)
{
if (rows[r].buttons == null) continue;
for (int c = 0; c < rows[r].buttons.Length; c++)
{
if (rows[r].buttons[c] == btn) { row = r; col = c; return true; }
}
}
}
row = col = -1;
return false;
}
/// <summary>
/// Возвращает кнопку (row, col).
/// col зажимается к последнему существующему в строке (удобно при переходе между рядами разной длины).
/// </summary>
public Button GetButton(int row, int col)
{
if (rows == null || row < 0 || row >= rows.Length) return null;
var r = rows[row];
if (r.buttons == null || r.buttons.Length == 0) return null;
return r.buttons[Mathf.Clamp(col, 0, r.buttons.Length - 1)];
}
public int RowCount => rows?.Length ?? 0;
public int ColCount(int row)
{
if (rows == null || row < 0 || row >= rows.Length) return 0;
return rows[row].buttons?.Length ?? 0;
}
}