2026-07-14 11:14:13 +03:00

122 lines
3.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
public class StringView : MonoBehaviour, IView
{
[SerializeField] private GameObject TextSlotPrefab;
[SerializeField] private RectTransform Content;
public AudioClip clip;
[SerializeField] float blockWidth = 150f;
private Setting setting;
Vector2 targetPosition;
Vector2 velocity = Vector2.zero;
public UnityEvent OnChangeReady { get; }
Coroutine coroutine;
bool generated = false;
public GameObject left, right;
private void OnEnable()
{
if (coroutine == null)
{
coroutine = StartCoroutine(Display());
}
}
private void OnDisable()
{
StopAllCoroutines();
coroutine = null;
}
//Тут придется id настройки давать. А почему бы не везде так давать.
public void Initialize(string settingId)
{
setting = SettingsManager.Container.TryGetSetting(settingId);
if (setting.Type != SettingType.String)
{
Debug.LogError("Настройка не того типа (string)");
return;
}
GenerateLine();
StartCoroutine(Display());
}
public void ChangeValue(string newValue)
{
int index = setting.AllowedValues.IndexOf(setting.StringValue);
Vector2 newtargetPosition = new Vector2(-index * blockWidth, 0);
if(newtargetPosition != targetPosition)
{
targetPosition = newtargetPosition;
SoundManager.instance.playClip(clip);
}
SetupPointers(index);
}
private void SetupPointers(int index)
{
if (index == 0)
{
left.SetActive(false);
right.SetActive(true);
}
else if (index == setting.AllowedValues.Count - 1)
{
left.SetActive(true);
right.SetActive(false);
}
else
{
left.SetActive(true);
right.SetActive(true);
}
}
private void GenerateLine()
{
float XPosition = 0;
foreach (string text in setting.AllowedValues)
{
RectTransform rect = Instantiate(TextSlotPrefab, Content).GetComponent<RectTransform>();
rect.anchoredPosition = new Vector2(XPosition, 0);
XPosition += blockWidth;
TextMeshProUGUI label = rect.GetComponent<TextMeshProUGUI>();
label.text = text;
}
//переместиться на текущее значение
int index = setting.AllowedValues.IndexOf(setting.StringValue);
targetPosition = new Vector2(-index * blockWidth, 0);
SetupPointers(index);
generated = true;
}
IEnumerator Display()
{
if (!generated) yield break;
while (true)
{
Content.anchoredPosition = Vector2.SmoothDamp(current: Content.anchoredPosition,
target: targetPosition,
currentVelocity: ref velocity,
smoothTime: 0.1f);
yield return null;
}
}
}