HeavenStudio/Assets/Scripts/LevelEditor/Timeline/ZoomComponent.cs
Braedon Lewis b45c4315b5
Editor bug fixes (#261)
* 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

* Bug fixes
2023-02-05 14:48:49 -05:00

82 lines
2.8 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)
{
ZoomIn(delta, relativeMousePosition);
}
else if (delta < 0)
{
ZoomOut(delta, relativeMousePosition);
}
}
public void ZoomIn(float delta, Vector2 relativeMousePosition)
{
if (!(_scale.x < _maximumScale)) return;
if (Timeline.instance.InteractingWithEvents() || SpecialTimeline.instance.InteractingWithEvents()) return;
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)
{
if (!(_scale.x > _minimumScale)) return;
if (Timeline.instance.InteractingWithEvents() || SpecialTimeline.instance.InteractingWithEvents()) return;
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;
}
}
}