public virtual void ItemLeft(GameObject item) { // release the item from this object's hierarchy item.transform.SetParent(JohnnyUITools.GetMyCanvas(gameObject).transform); currentItem = null; }
/// <summary> /// find mouse position under the gameobject's canvas /// </summary> /// <param name="gameObject"></param> /// <returns>Vector2: mouse position</returns> public static Vector2 GetMousePosInMyCanvas(GameObject gameObject) { // find the canvas I'm currently under Canvas myCanvas = JohnnyUITools.GetMyCanvas(gameObject); // calculate mouse's position using this canvas' coordinates (anchor = lower left corner) float mousePosX = Input.mousePosition.x; float mousePosY = Input.mousePosition.y; float canvasWidth = myCanvas.GetComponent <RectTransform>().sizeDelta.x; float canvasHeight = myCanvas.GetComponent <RectTransform>().sizeDelta.y; float mousePosInCanvasX = mousePosX / Screen.width * canvasWidth; float mousePosInCanvasY = mousePosY / Screen.height * canvasHeight; return(new Vector2(mousePosInCanvasX, mousePosInCanvasY)); }
public virtual bool InTriggerRange(GameObject item) { // get item position (center line) Vector2 center = JohnnyUITools.GetCenterCanvasCoord(item); // compare item position with header Vector2 headerCenter = JohnnyUITools.GetCenterCanvasCoord(myHeader); Vector2 headerTop = JohnnyUITools.GetTopCanvasCoord(myHeader); if (center.y > headerCenter.y && center.y <= headerTop.y) { return(false); } else { return(true); } }
// system messages ////////////////////////////////////////////////////////////////////// public virtual void LateUpdate() { if (hoveringSlot && hoveringSlot.GetComponent <IDroppable>().IsOccupied()) { hoveringSlot = null; } // if there's an item hoverring above this panel if (guardProbed /*&& !myItems.Contains(DragDropManager.instance.currentlyDraggedItem)*/) { // skip if panel is full already if (!PanelHasEnoughSpace()) { // TODO: fill in the alarming behavior here Debug.Log("panel full!"); guardProbed = false; return; } // if there's an item currently being dragged if (DragDropManager.instance.currentlyDraggedItem) { // in case of empty if (mySlots.Count == 0) { hoveringSlot = FormatNewSlot(0); } else { bool matched = false; bool above = false; // position check foreach (GameObject slot in mySlots) { GameObject slotItem = slot.GetComponent <IDroppable>().GetCurrentItem(); Vector2 slotBottom = JohnnyUITools.GetCanvasCoord(slot); Vector2 slotTop = JohnnyUITools.GetCanvasCoord(slot) + slot.GetComponent <RectTransform>().sizeDelta; Vector2 itemBottom = JohnnyUITools.GetCanvasCoord(DragDropManager.instance.currentlyDraggedItem); Vector2 itemCenter = JohnnyUITools.GetCenterCanvasCoord(DragDropManager.instance.currentlyDraggedItem); // move or create hovering slot here if (slotBottom.y <= itemCenter.y && slotTop.y >= itemCenter.y) { matched = true; if (slot == hoveringSlot) { break; } // TODO: Check also if the center of current block is in some range of the subpanel else if (slot.GetComponent <IDroppable>().GetCurrentItem().GetComponent <ISubPanel>() != null && slot.GetComponent <IDroppable>().GetCurrentItem().GetComponent <ISubPanel>().InTriggerRange(DragDropManager.instance.currentlyDraggedItem)) { // clear hovering slot if (hoveringSlot) { RemoveSlot(hoveringSlot); hoveringSlot = null; } // forward the call ISubPanel subPanel = slot.GetComponent <IDroppable>().GetCurrentItem().GetComponent <ISubPanel>(); subPanel.IsOccupied(); // update subpanel reference lastSubPanel = slot.GetComponent <IDroppable>().GetCurrentItem(); break; } // create new hover slot if (!hoveringSlot) { hoveringSlot = FormatNewSlot(mySlots.IndexOf(slot)); } // rearrange slots else { int newIndex; // above current slot if (itemCenter.y - slotBottom.y < slotTop.y - itemCenter.y) { newIndex = (mySlots.IndexOf(hoveringSlot) < mySlots.IndexOf(slot)) ? mySlots.IndexOf(slot) : mySlots.IndexOf(slot) + 1; } // below current slot else { newIndex = (mySlots.IndexOf(hoveringSlot) > mySlots.IndexOf(slot)) ? mySlots.IndexOf(slot) : mySlots.IndexOf(slot) - 1; } ReorderSlot(newIndex, hoveringSlot); } break; } else if (slotBottom.y < itemBottom.y) { above = true; } } if (!matched && above) { if (hoveringSlot) { ReorderSlot(0, hoveringSlot); } else { hoveringSlot = FormatNewSlot(0); } } else if (!matched) { // move hover slot to bottom if (hoveringSlot) { ReorderSlot(mySlots.Count - 1, hoveringSlot); } else { hoveringSlot = FormatNewSlot(mySlots.Count); } } } } } else if (hoveringSlot) { RemoveSlot(hoveringSlot); hoveringSlot = null; } guardProbed = false; }