Exemple #1
0
        /// <summary>
        /// Destroy GameObject and remove from cache specific instance of DragElement
        /// </summary>
        /// <param name="dragElement"></param>
        public void Destroy(DragElement dragElement)
        {
            if (!DragElementsCache.ContainsKey(dragElement.Id))
            {
                Destroy(dragElement.gameObject);
                return;
            }

            Destroy(DragElementsCache[dragElement.Id].gameObject);
            DragElementsCache.Remove(dragElement.Id);
        }
Exemple #2
0
        /// <summary>
        /// Attach events and Cache specific DragElement in <see cref="DragElementsCache"/>
        /// </summary>
        /// <param name="dragElement"></param>
        public void CacheDragElement(DragElement dragElement)
        {
            PrepareDragEvents(dragElement);

            if (!DragElementsCache.ContainsKey(dragElement.Id))
            {
                DragElementsCache.Add(dragElement.Id, null);
            }

            DragElementsCache[dragElement.Id]          = dragElement;
            DragElementsCache[dragElement.Id].IsCached = true;
        }
Exemple #3
0
        /// <summary>
        /// Add new GameObject with attached DragElement 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 DragElement.</returns>
        public DragElement AddDragElement(Transform parent, DragElement prefab = null, string name = "NewDragElement")
        {
            DragElement dragElement;

            if (prefab == null)
            {
                dragElement = new GameObject(name, typeof(RectTransform), typeof(DragElement)).GetComponent <DragElement>();
            }
            else
            {
                dragElement      = Instantiate(prefab, parent);
                dragElement.name = name;
            }

            dragElement.transform.SetParent(parent);
            CacheDragElement(dragElement);

            return(dragElement);
        }
Exemple #4
0
        /// <summary>
        /// Trigger event when drag is beginning.
        /// </summary>
        /// <param name="eventData">Data from that event.</param>
        public void OnBeginDrag(PointerEventData eventData)
        {
            if (cloneOnDragging)
            {
                // This element is in the toolbox
                DragElement cloned = Instantiate(this.gameObject, this.TransformCache.parent).GetComponent <DragElement>();
                cloned.TransformCache.SetSiblingIndex(this.TransformCache.GetSiblingIndex());
                cloned.name = this.name;

                // This element is the dragging element
                this.cloneOnDragging = false;
                this.isClonning      = true;
            }

            this.IsDragging   = true;
            this.LastPosition = this.TransformCache.position;
            this.GetComponent <Graphic>().raycastTarget = false;

            this.OnBeginDragCallback?.Invoke();
        }
Exemple #5
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();
            }
        }