/// <summary> /// Destroy GameObject and remove from cache specific instance of DropObject /// </summary> /// <param name="dropObject"></param> public void Destroy(DropObject dropObject) { if (!DropObjectsCache.ContainsKey(dropObject.Id)) { Destroy(dropObject.gameObject); return; } Destroy(DropObjectsCache[dropObject.Id].gameObject); DropObjectsCache.Remove(dropObject.Id); }
public void PlaceOnDropObject(DropObject dropObject) { TransformCache.SetParent(dropObject.TransformCache); TransformCache.localPosition = Vector2.zero; isClonning = false; if (lastDropObject != null) { lastDropObject.isEmpty = true; } dropObject.isEmpty = false; lastDropObject = dropObject; }
/// <summary> /// Attach events and Cache specific DropObject in <see cref="DropObjectsCache"/> /// </summary> /// <param name="dropObject"></param> public void CacheDropObject(DropObject dropObject) { PrepareDropEvents(dropObject); Guid id = dropObject.Id; if (!DropObjectsCache.ContainsKey(id)) { DropObjectsCache.Add(id, null); } DropObjectsCache[id] = dropObject; DropObjectsCache[id].IsCached = true; }
/// <summary> /// Attach on specific DropObject all officially drop events. /// </summary> /// <param name="dropObject">Specific DropObject for preparing.</param> private void PrepareDropEvents(DropObject dropObject) { dropObject.OnPointerEnterCallback = () => { LoadPointerEnterEvents(dropObject); }; dropObject.OnPointerExitCallback = () => { LoadPointerExitEvents(); }; void LoadPointerEnterEvents(DropObject оbject) { HoveredDropObject = оbject; } void LoadPointerExitEvents() { HoveredDropObject = null; } }
/// <summary> /// Add new GameObject with attached DropObject on specific place in hierarchy. /// </summary> /// <param name="parent">Parent for new GameObject.</param> /// <param name="prefab">Original object to copy.</param> /// <param name="name">Specific name for new Game object.</param> /// <returns>Created instance of DropObject.</returns> public DropObject AddDropObject(Transform parent, DropObject prefab = null, string name = "NewDropObject") { DropObject dropObject; if (prefab == null) { dropObject = new GameObject(name, typeof(RectTransform), typeof(DropObject), typeof(Image)).GetComponent <DropObject>(); } else { dropObject = Instantiate(prefab, parent); dropObject.name = name; } dropObject.transform.SetParent(parent); CacheDropObject(dropObject); return(dropObject); }
/// <summary> /// Attach on specific DragElement all officially drag events. /// </summary> /// <param name="dragElement">Specific DragElement for preparing.</param> private void PrepareDragEvents(DragElement dragElement) { dragElement.OnBeginDragCallback = () => { LoadBeginDragEvents(dragElement); }; dragElement.OnDragCallback = () => LoadDragEvents(); dragElement.OnEndDragCallback = () => LoadEndDragEvents(); void LoadBeginDragEvents(DragElement element) { SelectedDragElement = element; SelectedDragElement.TransformCache.SetParent(transform); // Make all DragElements transparent foreach (var entry in DropObjectsCache) { DropObject dropObject = entry.Value; dropObject.GetComponent <Graphic>().raycastTarget = false; } onBeginDrag.Invoke(); } void LoadDragEvents() { onDrag.Invoke(); // Revert all DragElements transparent foreach (var entry in DropObjectsCache) { DropObject dropObject = entry.Value; dropObject.GetComponent <Graphic>().raycastTarget = true; } } void LoadEndDragEvents() { if (SelectedDragElement.isClonning) { if (HoveredDropObject != null) { if (HoveredDropObject.isEmpty) { SelectedDragElement.PlaceOnDropObject(HoveredDropObject); Debug.Log("Cloning: HoveredDropObject : Empty"); } else { SelectedDragElement.DestroyWithAnimation(); Debug.Log("Cloning: HoveredDropObject : Not empty"); } } else if (HoveredDropObject == null) { SelectedDragElement.DestroyWithAnimation(); Debug.Log("Cloning: HoveredDropObject : NULL"); } } else { if (HoveredDropObject != null) { if (HoveredDropObject.isEmpty) { SelectedDragElement.PlaceOnDropObject(HoveredDropObject); Debug.Log("Grid: HoveredDropObject : Empty"); } else { SelectedDragElement.ReturnWithAnimation(); Debug.Log("Grid: HoveredDropObject : Not empty"); } } else if (HoveredDropObject == null) { SelectedDragElement.DestroyWithoutAnimation(); Debug.Log("Grid: HoveredDropObject : NULL"); } } LastSelectedDragElement = SelectedDragElement; SelectedDragElement = null; onEndDrag.Invoke(); } }