using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; namespace SoftKitty.InventoryEngine { /// /// This module is used to drag items around. /// public class ItemDragManager : MonoBehaviour { #region Variables public static bool isDragging = false; public static Vector3 DropPos; public ItemIcon DragItem; public RectTransform Rect; private static ItemDragManager instance; public static ItemIcon DraggingSource; public static bool DropReceived = true; public static bool SplitMode = false; public static InventoryStack SplitData; private bool playingDeleteAnimation = false; private float dropSpeed = 9.8F; private float rotateSpeed = 90F; #endregion #region MonoBehaviour void Start() { DragItem.Fav.GetComponent().color = InventorySkin.instance.FavoriteColor; } void Update() { if (playingDeleteAnimation) { dropSpeed += 100F * Time.deltaTime; if (DragItem.transform.position.y > -2000F) { DragItem.transform.position += Vector3.down * 120F * dropSpeed * Time.deltaTime; DragItem.transform.localEulerAngles = new Vector3(0F, 0F, DragItem.transform.localEulerAngles.z + Time.deltaTime * rotateSpeed); } else { Destroy(gameObject); } return; } else { dropSpeed = 9.8F; } if (isDragging) { if (!DragItem.gameObject.activeSelf) DragItem.gameObject.SetActive(true); DragItem.Rect.position = TransferPos(InputProxy.mousePosition, Rect, GetComponentInParent().renderMode== RenderMode.ScreenSpaceCamera? GetComponentInParent().worldCamera:null); if (InputProxy.GetMouseButtonUp(0)) { EndDrag(); } } else { if (DragItem.gameObject.activeSelf) DragItem.gameObject.SetActive(false); } } #endregion #region Internal Methods public void BeginPlayDeleteAnimation(InventoryItem _source, Vector3 _pos, int _overrideNum) { DragItem.SetAppearance(_source.StackData.Item.icon, _source.StackData.Item.GetTypeColor(), _source.StackData.Item.GetQualityColor(), true, true); DragItem.SetUpgradeLevel(_source.StackData.Item.upgradeLevel); DragItem.SetItemNumber(_overrideNum != 0 ? _overrideNum : _source.StackData.Number); DragItem.SetFavorate(_source.Fav.activeSelf); DragItem.transform.position = _pos; DragItem.transform.localScale = Vector3.one * InventorySkin.instance.InventorySlotScale; DragItem.gameObject.SetActive(true); rotateSpeed = Random.Range(-180F, 180F); playingDeleteAnimation = true; SoundManager.Play2D("ItemDelete"); } public void BeginDrag(ItemIcon _source, int _overrideNum) { playingDeleteAnimation = false; DragItem.transform.localEulerAngles = Vector3.zero; DragItem.transform.localScale = Vector3.one * InventorySkin.instance.InventorySlotScale; if (EventSystem.current != null) EventSystem.current.SetSelectedGameObject(DragItem.gameObject); if (_overrideNum != 0) { SplitData = _source.GetStackData().Split(_overrideNum); DragItem.SetAppearance(SplitData.Item.icon, SplitData.Item.GetTypeColor(), SplitData.Item.GetQualityColor(), true, true); DragItem.SetUpgradeLevel(SplitData.Item.upgradeLevel); DragItem.SetItemNumber(SplitData.Number); } else { DragItem.SetAppearance(_source.GetItem().icon, _source.GetItem().GetTypeColor(), _source.GetItem().GetQualityColor(), true, true); DragItem.SetUpgradeLevel(_source.GetItem().upgradeLevel); DragItem.SetItemNumber(_source.GetNumber()); } DragItem.SetFavorate(_source.Fav.activeSelf); DraggingSource = _source; DraggingSource.ToggleOutline(true); SoundManager.Play2D("ItemDrag"); } private void EndDrag() { DropPos = DragItem.transform.position; DraggingSource.ToggleOutline(false); DropReceived = false; isDragging = false; DraggingSource.EndDrag(SplitMode ? SplitData.Number : 0); SoundManager.Play2D("ItemDrop"); Destroy(gameObject); } #endregion /// /// Wehther the system is visible, meaning wehther something being dragged. /// /// public static bool isVisible() { return instance!=null && (isDragging || !DropReceived); } /// /// Plays the delete animation for an item icon, causing the icon to fall to the bottom of the screen. /// /// /// /// public static void PlayDeleteAnimation(InventoryItem _source,Vector3 _pos, int _overrideNum = 0) { if (instance == null) { GameObject newObj = Instantiate(Resources.Load("InventoryEngine/ItemDragManager"), WindowsManager.GetMainCanvas(_source.gameObject).transform); newObj.transform.SetAsLastSibling(); newObj.transform.localPosition = Vector3.zero; newObj.transform.localScale = Vector3.one; instance = newObj.GetComponent(); } instance.BeginPlayDeleteAnimation(_source, _pos, _overrideNum); } /// /// Starts dragging an item. After the drag-and-drop action, the _source will be called by EndDrag(). /// /// /// /// public static void StartDragging(ItemIcon _source, RectTransform _rect,int _overrideNum=0) { if (instance == null) { GameObject newObj = Instantiate(Resources.Load("InventoryEngine/ItemDragManager"), WindowsManager.GetMainCanvas(_rect.gameObject).transform); newObj.transform.SetAsLastSibling(); newObj.transform.localPosition = Vector3.zero; newObj.transform.localScale = Vector3.one; instance = newObj.GetComponent(); } SplitMode = (_overrideNum>0); instance.BeginDrag(_source, _overrideNum); DropPos = _source.transform.position; isDragging = true; } /// /// Transfrom mouse position to rect position. /// /// /// /// /// public static Vector3 TransferPos(Vector3 _pos, RectTransform _parentTransform,Camera _camera) { Vector2 localPosition = Vector2.zero; RectTransformUtility.ScreenPointToLocalPointInRectangle( _parentTransform, _pos, _camera, out localPosition); return _parentTransform.TransformPoint(localPosition); } } }