Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
0
        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;
        }
Exemplo n.º 3
0
        /// <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;
        }
Exemplo n.º 4
0
        /// <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;
            }
        }
Exemplo n.º 5
0
        /// <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);
        }
Exemplo n.º 6
0
        /// <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();
            }
        }