2026-08-11 20:38:39 +03:00
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
2026-08-18 01:15:31 +03:00
|
|
|
using TMPro;
|
2026-08-11 20:38:39 +03:00
|
|
|
|
|
|
|
|
public class FurniturePaletteUI : MonoBehaviour
|
|
|
|
|
{
|
2026-08-18 01:15:31 +03:00
|
|
|
[Header("UI Элементы: Палитра (Scroll View)")]
|
|
|
|
|
public GameObject palettePanel;
|
|
|
|
|
public Transform colorsContainer;
|
|
|
|
|
public GameObject colorBtnTemplate;
|
2026-08-11 20:38:39 +03:00
|
|
|
|
2026-08-18 01:15:31 +03:00
|
|
|
[Header("UI Элементы: Вкладки деталей")]
|
|
|
|
|
public GameObject tabsPanel;
|
|
|
|
|
public Transform tabsContainer;
|
|
|
|
|
public GameObject tabBtnTemplate;
|
|
|
|
|
|
|
|
|
|
private FurnitureObject _currentFurniture;
|
2026-08-11 20:38:39 +03:00
|
|
|
private FurnitureVisuals _currentVisuals;
|
2026-08-18 01:15:31 +03:00
|
|
|
private int _selectedSlotIndex = 0;
|
2026-08-11 20:38:39 +03:00
|
|
|
|
|
|
|
|
public void ShowColors(FurnitureObject furniture)
|
|
|
|
|
{
|
|
|
|
|
if (furniture == null || furniture.runtimeItemData == null) return;
|
|
|
|
|
|
|
|
|
|
_currentVisuals = furniture.GetComponent<FurnitureVisuals>();
|
2026-08-18 01:15:31 +03:00
|
|
|
if (_currentVisuals == null || furniture.runtimeItemData.configurableSlots.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
HideColors();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_currentFurniture = furniture;
|
|
|
|
|
_selectedSlotIndex = 0;
|
|
|
|
|
|
|
|
|
|
GenerateTabs();
|
|
|
|
|
ShowColorsForSlot(_selectedSlotIndex);
|
|
|
|
|
|
|
|
|
|
palettePanel.SetActive(true);
|
|
|
|
|
tabsPanel.SetActive(furniture.runtimeItemData.configurableSlots.Count > 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GenerateTabs()
|
|
|
|
|
{
|
|
|
|
|
foreach (Transform child in tabsContainer) Destroy(child.gameObject);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < _currentFurniture.runtimeItemData.configurableSlots.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
int slotIndex = i;
|
|
|
|
|
string slotName = _currentFurniture.runtimeItemData.configurableSlots[i].slotName;
|
|
|
|
|
|
|
|
|
|
GameObject tabObj = Instantiate(tabBtnTemplate, tabsContainer, false);
|
|
|
|
|
tabObj.transform.localScale = Vector3.one;
|
|
|
|
|
|
|
|
|
|
TextMeshProUGUI tabText = tabObj.GetComponentInChildren<TextMeshProUGUI>();
|
|
|
|
|
if (tabText != null) tabText.text = slotName;
|
|
|
|
|
|
|
|
|
|
Button btn = tabObj.GetComponent<Button>();
|
|
|
|
|
btn.onClick.AddListener(() =>
|
|
|
|
|
{
|
|
|
|
|
_selectedSlotIndex = slotIndex;
|
|
|
|
|
ShowColorsForSlot(_selectedSlotIndex);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ShowColorsForSlot(int slotIndex)
|
|
|
|
|
{
|
|
|
|
|
// 1. Очищаем старые кнопки
|
|
|
|
|
foreach (Transform child in colorsContainer) Destroy(child.gameObject);
|
|
|
|
|
|
|
|
|
|
var slotData = _currentFurniture.runtimeItemData.configurableSlots[slotIndex];
|
|
|
|
|
|
|
|
|
|
// Если для этой детали нет цветов - скрываем панель и выходим
|
|
|
|
|
if (slotData.availableStyles == null || slotData.availableStyles.Count == 0)
|
2026-08-11 20:38:39 +03:00
|
|
|
{
|
|
|
|
|
palettePanel.SetActive(false);
|
2026-08-18 01:15:31 +03:00
|
|
|
return;
|
2026-08-11 20:38:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
palettePanel.SetActive(true);
|
|
|
|
|
|
2026-08-18 01:15:31 +03:00
|
|
|
// 2. Создаем новые кнопки
|
|
|
|
|
foreach (AtlasStyleSO style in slotData.availableStyles)
|
2026-08-11 20:38:39 +03:00
|
|
|
{
|
2026-08-18 01:15:31 +03:00
|
|
|
if (style == null) continue;
|
2026-08-11 20:38:39 +03:00
|
|
|
|
2026-08-18 01:15:31 +03:00
|
|
|
GameObject btnObj = Instantiate(colorBtnTemplate, colorsContainer, false);
|
2026-08-15 21:12:47 +03:00
|
|
|
|
|
|
|
|
RectTransform rect = btnObj.GetComponent<RectTransform>();
|
|
|
|
|
rect.localScale = Vector3.one;
|
|
|
|
|
rect.localPosition = new Vector3(rect.localPosition.x, rect.localPosition.y, 0f);
|
2026-08-18 01:15:31 +03:00
|
|
|
rect.sizeDelta = new Vector2(100f, 100f);
|
2026-08-15 21:12:47 +03:00
|
|
|
|
2026-08-11 20:38:39 +03:00
|
|
|
Image img = btnObj.GetComponent<Image>();
|
2026-08-15 21:12:47 +03:00
|
|
|
if (img != null)
|
2026-08-11 20:38:39 +03:00
|
|
|
{
|
2026-08-18 01:15:31 +03:00
|
|
|
img.sprite = style.uiIcon;
|
|
|
|
|
img.color = new Color(img.color.r, img.color.g, img.color.b, 1f);
|
2026-08-11 20:38:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Button btn = btnObj.GetComponent<Button>();
|
2026-08-18 01:15:31 +03:00
|
|
|
AtlasStyleSO styleToApply = style;
|
2026-08-11 20:38:39 +03:00
|
|
|
|
|
|
|
|
btn.onClick.AddListener(() =>
|
|
|
|
|
{
|
2026-08-18 01:15:31 +03:00
|
|
|
if (_currentVisuals != null) _currentVisuals.ApplyStyleToSlot(slotIndex, styleToApply);
|
2026-08-11 20:38:39 +03:00
|
|
|
});
|
|
|
|
|
}
|
2026-08-15 19:32:01 +03:00
|
|
|
|
2026-08-18 01:15:31 +03:00
|
|
|
// 3. Вызываем умный перерасчет размера панели
|
|
|
|
|
ResizePalettePanel();
|
|
|
|
|
}
|
2026-08-15 21:12:47 +03:00
|
|
|
|
2026-08-18 01:15:31 +03:00
|
|
|
private void ResizePalettePanel()
|
|
|
|
|
{
|
|
|
|
|
// Заставляем Unity моментально пересчитать UI (решает баг с нулевым размером)
|
|
|
|
|
Canvas.ForceUpdateCanvases();
|
|
|
|
|
|
|
|
|
|
RectTransform contentRect = colorsContainer.GetComponent<RectTransform>();
|
|
|
|
|
LayoutRebuilder.ForceRebuildLayoutImmediate(contentRect);
|
|
|
|
|
|
|
|
|
|
// Узнаем, сколько места реально заняли все кнопки внутри Content
|
|
|
|
|
float totalButtonsWidth = contentRect.rect.width;
|
|
|
|
|
|
|
|
|
|
// Вычисляем максимум (например, 90% от ширины экрана)
|
2026-08-15 21:12:47 +03:00
|
|
|
RectTransform canvasRect = palettePanel.GetComponentInParent<Canvas>().GetComponent<RectTransform>();
|
|
|
|
|
float maxAllowedWidth = canvasRect.rect.width * 0.9f;
|
|
|
|
|
|
2026-08-18 01:15:31 +03:00
|
|
|
// Выбираем ширину: либо размер кнопок, либо максимум экрана
|
2026-08-15 21:12:47 +03:00
|
|
|
float finalPanelWidth = Mathf.Min(totalButtonsWidth, maxAllowedWidth);
|
|
|
|
|
|
2026-08-18 01:15:31 +03:00
|
|
|
// Меняем ширину Главной Панели (Scroll View)
|
2026-08-15 21:12:47 +03:00
|
|
|
RectTransform panelRect = palettePanel.GetComponent<RectTransform>();
|
|
|
|
|
panelRect.sizeDelta = new Vector2(finalPanelWidth, panelRect.sizeDelta.y);
|
|
|
|
|
|
2026-08-18 01:15:31 +03:00
|
|
|
// Включаем прокрутку ТОЛЬКО если кнопки не влезли
|
2026-08-15 21:12:47 +03:00
|
|
|
ScrollRect scroll = palettePanel.GetComponent<ScrollRect>();
|
|
|
|
|
if (scroll != null)
|
|
|
|
|
{
|
|
|
|
|
scroll.enabled = (totalButtonsWidth > maxAllowedWidth);
|
|
|
|
|
}
|
2026-08-11 20:38:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void HideColors()
|
|
|
|
|
{
|
|
|
|
|
palettePanel.SetActive(false);
|
2026-08-18 01:15:31 +03:00
|
|
|
tabsPanel.SetActive(false);
|
2026-08-11 20:38:39 +03:00
|
|
|
_currentVisuals = null;
|
2026-08-18 01:15:31 +03:00
|
|
|
_currentFurniture = null;
|
2026-08-11 20:38:39 +03:00
|
|
|
}
|
|
|
|
|
}
|