116 lines
3.5 KiB
C#
116 lines
3.5 KiB
C#
using System.Buffers.Text;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class SettingsScreen : MonoBehaviour
|
|
{
|
|
[Header("Ссылки")]
|
|
[SerializeField] private SettingGroup GroupPrefab;
|
|
|
|
private bool Generated = false;
|
|
private int activeSetting;
|
|
|
|
private List<BaseSettingView> settings;
|
|
|
|
[SerializeField] RectTransform testPointer;
|
|
[SerializeField] ScrollRect scrollRect;
|
|
|
|
|
|
//====================
|
|
[Header("Управление")]
|
|
[SerializeField] private Axis switchAxis = Axis.Vertical;
|
|
[SerializeField] private Axis interactAxis = Axis.Horizontal;
|
|
[SerializeField] private PlayerAction intractButton = PlayerAction.action;
|
|
|
|
[SerializeField] private RectTransform Content;
|
|
|
|
private void Start()
|
|
{
|
|
Generate();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
int switchDirection = -In.GetAxis(switchAxis, JoystickSide.Both);
|
|
|
|
activeSetting = (activeSetting + switchDirection + settings.Count) % settings.Count;
|
|
|
|
var activePivot = settings[activeSetting].pivotPoint;
|
|
ScrollToElementVertical(activePivot);
|
|
testPointer.position = activePivot.position;
|
|
|
|
int interactDirection = In.GetAxis(interactAxis, JoystickSide.Both, InteractMode.Continious);
|
|
settings[activeSetting].TakeInput(interactDirection, false);
|
|
}
|
|
|
|
public void Generate()
|
|
{
|
|
ClearContent();
|
|
settings = new List<BaseSettingView>();
|
|
|
|
foreach (var group in SettingsManager.currentSchemeDTO.groups)
|
|
settings.AddRange(GenerateGroup(group).settings);
|
|
|
|
activeSetting = 0;
|
|
Generated = true;
|
|
}
|
|
|
|
private SettingGroup GenerateGroup(SettingsGroupDTO groupData)
|
|
{
|
|
var group = Instantiate(GroupPrefab, Content);
|
|
group.Configure(groupData);
|
|
return group;
|
|
}
|
|
|
|
private void ClearContent()
|
|
{
|
|
for (int i = Content.childCount - 1; i >= 0; i--)
|
|
Destroy(Content.GetChild(i).gameObject);
|
|
}
|
|
|
|
public void ScrollToElementVertical(RectTransform targetRect)
|
|
{
|
|
if (scrollRect == null || targetRect == null)
|
|
{
|
|
Debug.LogWarning("ScrollRect или Target не назначены");
|
|
return;
|
|
}
|
|
|
|
LayoutRebuilder.ForceRebuildLayoutImmediate(scrollRect.content);
|
|
|
|
RectTransform viewport = scrollRect.viewport;
|
|
RectTransform content = scrollRect.content;
|
|
|
|
if (viewport == null || content == null)
|
|
{
|
|
Debug.LogWarning("Viewport или Content не назначены");
|
|
return;
|
|
}
|
|
|
|
Vector2 targetLocalPos = content.InverseTransformPoint(targetRect.position);
|
|
Vector2 viewportLocalPos = content.InverseTransformPoint(viewport.position);
|
|
|
|
float targetHeight = targetRect.rect.height;
|
|
float viewportHeight = viewport.rect.height;
|
|
float contentHeight = content.rect.height;
|
|
|
|
if (contentHeight <= viewportHeight)
|
|
{
|
|
scrollRect.verticalNormalizedPosition = 1f;
|
|
return;
|
|
}
|
|
|
|
float offsetY = (targetLocalPos.y - viewportLocalPos.y) * -1f;
|
|
|
|
float targetCenterY = offsetY + targetHeight * 0.5f;
|
|
float viewportCenterY = viewportHeight * 0.5f;
|
|
|
|
float deltaY = (targetCenterY - viewportCenterY) / (contentHeight - viewportHeight);
|
|
|
|
float newPosition = Mathf.Clamp01(scrollRect.verticalNormalizedPosition - deltaY);
|
|
|
|
scrollRect.verticalNormalizedPosition = newPosition;
|
|
}
|
|
|
|
} |