Pro_Unit

CirclePanel

Apr 10th, 2021 (edited)
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections.Generic;
  4. using UniRx;
  5.  
  6. namespace AnimalSimulator.PaintMode.UI.ColorPanel
  7. {
  8.     public class CirclePanel : MonoBehaviour
  9.     {
  10.         [SerializeField] private float _sensitivity = 2f;
  11.         [SerializeField] private bool _invert;
  12.         [SerializeField] private FloatReactiveProperty _angle = new FloatReactiveProperty(10f);
  13.         [SerializeField] private float _radius = 340f;
  14.         [SerializeField] private float _itemSize = 50f;
  15.         [SerializeField] private Transform _contentContainer;
  16.        
  17.         [SerializeField] private DragHandler _dragHandler;
  18.        
  19.  
  20.         [SerializeField] private GridLayoutGroup.Axis _axis;
  21.         private readonly List<Transform> _objects = new List<Transform>();
  22.  
  23.         [SerializeField] private float _debugAngle = 45f;
  24.  
  25.         private void OnEnable()
  26.         {
  27.             _angle.TakeUntilDisable(this).Subscribe(f => CalculatePositions());
  28.             _dragHandler.OnDrag.TakeUntilDisable(this).Subscribe(data => Rotate(data.delta));
  29.         }
  30.  
  31.         public void AddObject(GameObject @object, bool reCalculatePositions = false)
  32.             => AddObject(@object.transform, reCalculatePositions);
  33.  
  34.         public void AddObject(Transform @object, bool reCalculatePositions = false)
  35.         {
  36.             @object.parent = _contentContainer;
  37.             _objects.Add(@object);
  38.  
  39.             if (reCalculatePositions)
  40.                 CalculatePositions();
  41.         }
  42.  
  43.         public void CalculatePositions()
  44.         {
  45.             Quaternion rotationStep = Quaternion.Euler(0, 0, _angle.Value);
  46.             Vector3 localPosition = rotationStep * Vector3.up * _radius;
  47.  
  48.             foreach (Transform child in _contentContainer)
  49.             {
  50.                 localPosition = rotationStep * localPosition;
  51.                 child.localPosition = localPosition;
  52.             }
  53.         }
  54.  
  55.         private void Rotate(Vector2 delta)
  56.         {
  57.             bool isHorizontal = _axis == GridLayoutGroup.Axis.Horizontal;
  58.  
  59.             float angle = isHorizontal ? delta.x : delta.y;
  60.  
  61.             int invert = _invert ? -1 : 1;
  62.  
  63.             angle *= _sensitivity * invert;
  64.  
  65.             var position = transform.position;
  66.  
  67.             transform.RotateAround(position, Vector3.forward, angle);
  68.         }
  69.  
  70.         private void OnDrawGizmos()
  71.         {
  72.             Gizmos.color = Color.red;
  73.             var position = transform.position;
  74.             Vector3 offsetPosition = Quaternion.Euler(0, 0, z: _debugAngle) * Vector3.up * _radius;
  75.             Vector3 targetPosition = position + offsetPosition;
  76.             Gizmos.DrawLine(position, targetPosition);
  77.  
  78.             Gizmos.color = Color.green;
  79.             Gizmos.DrawWireSphere(targetPosition, _itemSize);
  80.         }
  81.     }
  82. }
Add Comment
Please, Sign In to add comment