176 lines
7.6 KiB
C#
176 lines
7.6 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using TMPro;
|
||
|
||
public class CarMarketUI : MonoBehaviour
|
||
{
|
||
[Header("Клавиша открытия планшета перекупа")]
|
||
[SerializeField] private KeyCode toggleKey = KeyCode.M;
|
||
|
||
[Header("Ссылки")]
|
||
[SerializeField] private CarMarketManager marketManager;
|
||
|
||
[Header("Кастомный UI на Canvas (необязательно)")]
|
||
[Tooltip("Если оставить пустым — скрипт сам нарисует готовое окно браузера авторынка")]
|
||
[SerializeField] private GameObject marketPanel;
|
||
[SerializeField] private Image photoImage;
|
||
[SerializeField] private TextMeshProUGUI titleText;
|
||
[SerializeField] private TextMeshProUGUI priceText;
|
||
[SerializeField] private TextMeshProUGUI specsText;
|
||
[SerializeField] private TextMeshProUGUI descriptionText;
|
||
[SerializeField] private Button prevButton;
|
||
[SerializeField] private Button nextButton;
|
||
[SerializeField] private Button arrangeMeetingButton;
|
||
[SerializeField] private TextMeshProUGUI arrangeButtonLabel;
|
||
|
||
private int currentIndex = 0;
|
||
private bool isOpen = false;
|
||
private PlayerLook playerLook;
|
||
|
||
private void Awake()
|
||
{
|
||
if (marketManager == null)
|
||
marketManager = FindAnyObjectByType<CarMarketManager>();
|
||
|
||
playerLook = FindAnyObjectByType<PlayerLook>();
|
||
|
||
if (marketPanel != null)
|
||
marketPanel.SetActive(false);
|
||
|
||
if (prevButton != null) prevButton.onClick.AddListener(ShowPrevious);
|
||
if (nextButton != null) nextButton.onClick.AddListener(ShowNext);
|
||
if (arrangeMeetingButton != null) arrangeMeetingButton.onClick.AddListener(OnArrangeMeetingClicked);
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
if (Input.GetKeyDown(toggleKey))
|
||
{
|
||
ToggleMarket(!isOpen);
|
||
}
|
||
}
|
||
|
||
public void ToggleMarket(bool open)
|
||
{
|
||
isOpen = open;
|
||
|
||
if (marketPanel != null)
|
||
marketPanel.SetActive(isOpen);
|
||
|
||
// Включаем курсор мыши и блокируем вращение головой игрока при открытом планшете
|
||
Cursor.lockState = isOpen ? CursorLockMode.None : CursorLockMode.Locked;
|
||
Cursor.visible = isOpen;
|
||
|
||
if (playerLook != null)
|
||
playerLook.enabled = !isOpen;
|
||
|
||
if (isOpen)
|
||
RefreshCurrentListingUI();
|
||
}
|
||
|
||
public void ShowPrevious()
|
||
{
|
||
if (marketManager == null || marketManager.Listings.Count == 0) return;
|
||
currentIndex = (currentIndex - 1 + marketManager.Listings.Count) % marketManager.Listings.Count;
|
||
RefreshCurrentListingUI();
|
||
}
|
||
|
||
public void ShowNext()
|
||
{
|
||
if (marketManager == null || marketManager.Listings.Count == 0) return;
|
||
currentIndex = (currentIndex + 1) % marketManager.Listings.Count;
|
||
RefreshCurrentListingUI();
|
||
}
|
||
|
||
public void OnArrangeMeetingClicked()
|
||
{
|
||
if (marketManager == null || marketManager.Listings.Count == 0) return;
|
||
|
||
CarListing current = marketManager.Listings[currentIndex];
|
||
marketManager.ArrangeMeeting(current);
|
||
RefreshCurrentListingUI();
|
||
|
||
// Закрываем планшет, чтобы игрок сразу увидел метку на карте и поехал на встречу
|
||
ToggleMarket(false);
|
||
}
|
||
|
||
private void RefreshCurrentListingUI()
|
||
{
|
||
if (marketManager == null || marketManager.Listings.Count == 0) return;
|
||
currentIndex = Mathf.Clamp(currentIndex, 0, marketManager.Listings.Count - 1);
|
||
|
||
CarListing item = marketManager.Listings[currentIndex];
|
||
|
||
if (photoImage != null) photoImage.sprite = item.photo;
|
||
if (titleText != null) titleText.text = $"{item.catalogItem.carTitle} ({item.year} г.)";
|
||
if (priceText != null) priceText.text = $"${item.price:N0}";
|
||
if (specsText != null)
|
||
specsText.text = $"Привод: {FormatDrive(item.driveType)} | Пробег: {item.mileageKm:N0} км | Объявление {currentIndex + 1}/{marketManager.Listings.Count}";
|
||
if (descriptionText != null) descriptionText.text = item.sellerDescription;
|
||
|
||
if (arrangeButtonLabel != null)
|
||
arrangeButtonLabel.text = item.isMeetingArranged ? "Показать на карте (Встреча назначена)" : "Договориться о встрече";
|
||
}
|
||
|
||
private string FormatDrive(DriveType dt)
|
||
{
|
||
return dt switch
|
||
{
|
||
DriveType.FWD => "Передний (FWD)",
|
||
DriveType.RWD => "Задний (RWD)",
|
||
DriveType.AWD => "Полный (AWD)",
|
||
_ => dt.ToString()
|
||
};
|
||
}
|
||
|
||
// ---------- Встроенный готовый интерфейс (работает сразу, если marketPanel не назначен) ----------
|
||
private void OnGUI()
|
||
{
|
||
if (!isOpen || marketPanel != null) return;
|
||
if (marketManager == null || marketManager.Listings.Count == 0) return;
|
||
|
||
CarListing item = marketManager.Listings[currentIndex];
|
||
|
||
float w = 720f;
|
||
float h = 540f;
|
||
float x = (Screen.width - w) * 0.5f;
|
||
float y = (Screen.height - h) * 0.5f;
|
||
|
||
GUI.Box(new Rect(x, y, w, h), $"АВТОРЫНОК — Объявление {currentIndex + 1} из {marketManager.Listings.Count} (Закрыть: [M])");
|
||
|
||
// 1. Фотография автомобиля с его реальным состоянием
|
||
Rect photoRect = new Rect(x + 30f, y + 40f, 440f, 248f);
|
||
if (item.photo != null && item.photo.texture != null)
|
||
{
|
||
GUI.DrawTexture(photoRect, item.photo.texture, ScaleMode.ScaleToFit);
|
||
}
|
||
|
||
// 2. Карточка характеристик справа от фото
|
||
float infoX = x + 490f;
|
||
GUIStyle headerStyle = new GUIStyle(GUI.skin.label) { fontSize = 20, fontStyle = FontStyle.Bold, wordWrap = true };
|
||
GUIStyle priceStyle = new GUIStyle(GUI.skin.label) { fontSize = 26, fontStyle = FontStyle.Bold };
|
||
priceStyle.normal.textColor = new Color(0.25f, 0.95f, 0.35f);
|
||
GUIStyle specStyle = new GUIStyle(GUI.skin.label) { fontSize = 15, wordWrap = true };
|
||
|
||
GUI.Label(new Rect(infoX, y + 45f, 210f, 55f), $"{item.catalogItem.carTitle}\n{item.year} год", headerStyle);
|
||
GUI.Label(new Rect(infoX, y + 110f, 210f, 40f), $"${item.price:N0}", priceStyle);
|
||
GUI.Label(new Rect(infoX, y + 160f, 210f, 30f), $"Привод: {FormatDrive(item.driveType)}", specStyle);
|
||
GUI.Label(new Rect(infoX, y + 190f, 210f, 30f), $"Пробег: {item.mileageKm:N0} км", specStyle);
|
||
|
||
// 3. Описание от продавца
|
||
GUIStyle descBoxStyle = new GUIStyle(GUI.skin.box) { alignment = TextAnchor.UpperLeft, fontSize = 15, wordWrap = true, padding = new RectOffset(12, 12, 10, 10) };
|
||
descBoxStyle.normal.textColor = Color.white;
|
||
GUI.Box(new Rect(x + 30f, y + 305f, w - 60f, 110f), $"Комментарий продавца:\n{item.sellerDescription}", descBoxStyle);
|
||
|
||
// 4. Кнопки листания и выезда на встречу
|
||
if (GUI.Button(new Rect(x + 30f, y + 435f, 160f, 45f), "◀ Предыдущее"))
|
||
ShowPrevious();
|
||
|
||
string meetBtnText = item.isMeetingArranged ? "✔ Встреча уже назначена (Обновить метку)" : "📞 Договориться о встрече";
|
||
if (GUI.Button(new Rect(x + 210f, y + 435f, 300f, 45f), meetBtnText))
|
||
OnArrangeMeetingClicked();
|
||
|
||
if (GUI.Button(new Rect(x + 530f, y + 435f, 160f, 45f), "Следующее ▶"))
|
||
ShowNext();
|
||
}
|
||
} |