Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.UI;
- namespace ParentHouse.UI
- {
- public class ActivityWheel : MonoBehaviour
- {
- [Header("Demo Settings")] public List<string> demoMessages = new();
- public Color foregroundColor;
- public Color backgroundColor;
- public float bgColorCellSizeRequirement;
- public TextMeshProUGUI textOutput;
- public GameObject demoPrefabObject;
- [Header("Settings")] public float radius = 280;
- public float vertialSquish = 0.35f;
- public float spinSpeed = 7;
- public bool targetCellOnTop = true;
- public Vector2 minimumCellSize = new Vector2(50, 50);
- public Vector2 cellSize = new Vector2(150, 150);
- [Header("Debugging")] [SerializeField] private int selectIndex;
- [SerializeField] private float targetAngle;
- [SerializeField] private float angleOffset;
- [SerializeField] private float facingAngle;
- [SerializeField] private bool needsToBeNormalized;
- [SerializeField] private float scrollInput;
- [SerializeField] private readonly List<ActivityWheelAction> wheelActions = new();
- [SerializeField] private List<GameObject> wheelDisplayObjectPool = new();
- private void OnEnable()
- {
- for (int i = 0; i < 20; i++)
- {
- wheelDisplayObjectPool.Add(Instantiate(demoPrefabObject, this.transform));
- wheelDisplayObjectPool[i].gameObject.name = $"Object {i}";
- wheelDisplayObjectPool[i].gameObject.SetActive(false);
- }
- Show();
- }
- private void Show()
- {
- ClearWheel();
- GenerateActivityWheelDisplays();
- if (wheelActions.Count == 0) return;
- wheelActions.Reverse();
- if (wheelActions.Count > 1)
- angleOffset = wheelActions[^2].storedAngle;
- else
- {
- targetAngle = 0;
- }
- CheckScrollInput(1);
- facingAngle = targetAngle;
- }
- private void ClearWheel()
- {
- facingAngle = 0;
- targetAngle = 0;
- selectIndex = 0;
- wheelActions.Clear();
- }
- private void InvokeSelectedActivityAction()
- {
- if (wheelActions.Count == 0) return;
- int offsetIndex = selectIndex + 1;
- offsetIndex = offsetIndex >= wheelActions.Count ? 0 :
- offsetIndex < 0 ? wheelActions.Count - 1 : offsetIndex;
- wheelActions[offsetIndex].WheelUnityAction.Invoke();
- }
- private void Update()
- {
- scrollInput = Input.mouseScrollDelta.y;
- if (Input.GetKeyDown(KeyCode.Space))
- InvokeSelectedActivityAction();
- CheckScrollInput(scrollInput);
- }
- private void FixedUpdate()
- {
- AnimateWheelDisplays();
- }
- private void CheckScrollInput(float input)
- {
- if (input == 0 || wheelActions.Count <= 1) return;
- if (needsToBeNormalized)
- {
- facingAngle = targetAngle = wheelActions[selectIndex].storedAngle;
- needsToBeNormalized = false;
- }
- input = Mathf.Clamp(input, -1, 1);
- selectIndex += (int) input;
- if (selectIndex >= wheelActions.Count)
- {
- targetAngle = -angleOffset;
- needsToBeNormalized = true;
- }
- else if (selectIndex < 0)
- {
- targetAngle = wheelActions[0].storedAngle + angleOffset;
- needsToBeNormalized = true;
- }
- else targetAngle = wheelActions[selectIndex].storedAngle;
- selectIndex = selectIndex >= wheelActions.Count ? 0 :
- selectIndex < 0 ? wheelActions.Count - 1 : selectIndex;
- int offsetIndex = selectIndex + 1;
- offsetIndex = offsetIndex >= wheelActions.Count ? 0 :
- offsetIndex < 0 ? wheelActions.Count - 1 : offsetIndex;
- }
- private void AnimateWheelDisplays()
- {
- for (int i = 0; i < wheelActions.Count; i++)
- {
- var rect = wheelActions[i].Obj.GetComponent<RectTransform>();
- float angle = (i * Mathf.PI * 2f / wheelActions.Count) + facingAngle;
- if (!wheelActions[i].hasAngle)
- {
- wheelActions[i].hasAngle = true;
- wheelActions[i].storedAngle = angle;
- }
- float y = (Mathf.Cos(angle) * radius) * vertialSquish;
- float x = Mathf.Sin(angle) * radius;
- rect.anchoredPosition = new Vector2(x, y);
- int verticalFlip = targetCellOnTop ? 1 : -1;
- float unscaledSize = Mathf.InverseLerp(-cellSize.x * verticalFlip, cellSize.y * verticalFlip, y);
- float scaledSize = Mathf.Clamp(cellSize.x * unscaledSize, minimumCellSize.x, cellSize.x);
- var size = new Vector2(scaledSize, scaledSize);
- rect.sizeDelta = size;
- var c = rect.GetComponent<Canvas>();
- c.overrideSorting = true;
- c.sortingOrder = (int) size.y;
- // For demonstrating depth
- wheelActions[i].DemoImage.color =
- scaledSize < bgColorCellSizeRequirement ? backgroundColor : foregroundColor;
- }
- if (wheelActions.Count <= 1) return;
- facingAngle = Mathf.Lerp(facingAngle, targetAngle, spinSpeed * Time.deltaTime);
- }
- private void GenerateActivityWheelDisplays()
- {
- for (var i = 0; i < demoMessages.Count; i++)
- {
- SetupWheelListObject(wheelDisplayObjectPool[i], demoMessages[i]);
- }
- selectIndex = selectIndex >= wheelActions.Count ? wheelActions.Count - 1 :
- selectIndex < 0 ? 0 : selectIndex;
- AnimateWheelDisplays();
- }
- private void SetupWheelListObject(GameObject displayObject, string message)
- {
- displayObject.SetActive(true);
- var WheelObject = new ActivityWheelAction
- {
- WheelUnityAction = delegate
- {
- print(message);
- textOutput.text = message;
- },
- Obj = displayObject,
- DemoImage = displayObject.GetComponent<Image>()
- };
- wheelActions.Add(WheelObject);
- }
- }
- public class ActivityWheelAction
- {
- public UnityAction WheelUnityAction;
- public GameObject Obj;
- public Image DemoImage;
- public float storedAngle;
- public bool hasAngle = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement