//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Copyright (c) 2022 Martin Bustos @FronkonGames // // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated // documentation files (the "Software"), to deal in the Software without restriction, including without limitation the // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all copies or substantial portions of // the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// using UnityEngine; using FronkonGames.TinyTween; /// /// Drag card. /// [RequireComponent(typeof(Collider))] public sealed class CardDrag : MonoBehaviour, IDrag { public bool IsDraggable { get; private set; } = true; public bool Dragging { get; set; } [SerializeField] private Ease riseEaseIn = Ease.Linear; [SerializeField] private Ease riseEaseOut = Ease.Linear; [SerializeField, Range(0.0f, 5.0f)] private float riseDuration = 0.2f; [SerializeField] private Ease dropEaseIn = Ease.Linear; [SerializeField] private Ease dropEaseOut = Ease.Linear; [SerializeField, Range(0.0f, 5.0f)] private float dropDuration = 0.2f; [SerializeField] private Ease invalidDropEase = Ease.Linear; [SerializeField, Range(0.0f, 5.0f)] private float invalidDropDuration = 0.2f; private Vector3 dragOriginPosition; public void OnPointerEnter(Vector3 position) { } public void OnPointerExit(Vector3 position) { } public void OnBeginDrag(Vector3 position) { dragOriginPosition = transform.position; IsDraggable = false; TweenFloat.Create() .Origin(dragOriginPosition.y) .Destination(position.y) .Duration(riseDuration) .EasingIn(riseEaseIn) .EasingOut(riseEaseOut) .OnUpdate(tween => transform.position = new Vector3(transform.position.x, tween.Value, transform.position.z)) .OnEnd(_ => IsDraggable = true) .Owner(this) .Start(); } public void OnDrag(Vector3 deltaPosition, IDrop droppable) { deltaPosition.y = 0.0f; transform.position += deltaPosition; } public void OnEndDrag(Vector3 position, IDrop droppable) { if (droppable is { IsDroppable: true } && droppable.AcceptDrop(this) == true) TweenFloat.Create() .Origin(transform.position.y) .Destination(position.y) .Duration(dropDuration) .EasingIn(dropEaseIn) .EasingOut(dropEaseOut) .OnUpdate(tween => transform.position = new Vector3(transform.position.x, tween.Value, transform.position.z)) .Owner(this) .Start(); else { IsDraggable = false; transform.TweenMove(dragOriginPosition, invalidDropDuration, invalidDropEase).OnEnd(_ => IsDraggable = true); } } private void OnEnable() { dragOriginPosition = transform.position; } }