HeavenStudioPlus/Assets/Scripts/LevelEditor/Timeline/ZoomComponent.cs
Braedon Lewis 495cc5ad4c
General editor improvements (#257)
* Spaceball cleanup and small bug fix

* Replace old hit sound in spaceball

* Camera filters

* added 9 new filters, including 3 types of sepia

* oops

* remark

* normalization of fade out and fade in on filters are by 100

* GenerateFilterTypeEnum comments

* Pure black and white filter

* Zooming

* Constant playback bar offset

* Prepare box selector rewrite

* Update icons, finalize
2023-02-04 22:05:36 -05:00

76 lines
No EOL
2.5 KiB
C#

using UnityEngine;
using UnityEngine.EventSystems;
using static TMPro.SpriteAssetUtilities.TexturePacker_JsonArray;
namespace HeavenStudio.Editor.Track
{
public class ZoomComponent : MonoBehaviour, IScrollHandler
{
[SerializeField] float _minimumScale = 0.5f;
[SerializeField] Vector2 _initialScale = Vector2.one;
[SerializeField] float _maximumScale = 3f;
[SerializeField] float _scaleIncrement = .5f;
[HideInInspector] Vector3 _scale;
RectTransform _thisTransform;
private void Awake()
{
_initialScale = transform.localScale;
_thisTransform = transform as RectTransform;
_scale.Set(_initialScale.x, _initialScale.y, 1);
_thisTransform.localScale = _scale;
}
public void OnScroll(PointerEventData eventData)
{
Vector2 relativeMousePosition;
RectTransformUtility.ScreenPointToLocalPointInRectangle(_thisTransform, Input.mousePosition, Editor.instance.EditorCamera, out relativeMousePosition);
float delta = eventData.scrollDelta.y;
delta = Mathf.Clamp(delta, -1f, 1f);
_scaleIncrement = 0.1f * _scale.x;
if (delta > 0 && _scale.x < _maximumScale)
{
ZoomIn(delta, relativeMousePosition);
}
else if (delta < 0 && _scale.x > _minimumScale)
{
ZoomOut(delta, relativeMousePosition);
}
}
public void ZoomIn(float delta, Vector2 relativeMousePosition)
{
float incre = _scaleIncrement * delta;
var newScale = Mathf.Clamp(_scale.x + incre, _minimumScale, _maximumScale);
_scale.Set(newScale, 1, 1);
_thisTransform.localScale = _scale;
relativeMousePosition = new Vector2(relativeMousePosition.x, 0);
_thisTransform.anchoredPosition -= (relativeMousePosition * incre);
}
public void ZoomOut(float delta, Vector2 relativeMousePosition)
{
float incre = _scaleIncrement * -delta;
var newScale = _scale.x - incre;
_scale.Set(newScale, 1, 1);
_thisTransform.localScale = _scale;
relativeMousePosition = new Vector2(relativeMousePosition.x, 0);
_thisTransform.anchoredPosition += (relativeMousePosition * incre);
}
public void ResetZoom()
{
_scale.Set(100, 1, 1);
_thisTransform.localScale = _scale;
}
}
}