//Drop an item in the drop area void dropItemInDrop(int index) { //Setting trigger variables originSlot = draggedSlotIndex; destinationSlot = index; droppedItemIndex = draggedIndex; //If move in same box swap items and return if (itemOrigin == DragDropOrigin.DropBox) { dropSlots[draggedSlotIndex] = dropSlots[index]; dropSlots[index] = draggedIndex; //Setting trigger variables originIsDrag = false; destinationIsDrag = false; dropErrorCode = DropErrorCode.noError; gameObject.SendMessage("DropTrigger", this); return; } //If moves from drag to drop originIsDrag = true; destinationIsDrag = false; if (itemOrigin == DragDropOrigin.DragBox) { //If destination is not empty if (dropSlots[index] != -1) { Debug.Log("Valor " + dropSlots[index] + " index " + index); //If behavior is move to empty only... returns if (dropBehavior == DropBehavior.DropOnlyIfEmpty) { //Setting trigger variables dropErrorCode = DropErrorCode.notAllowedSlot; gameObject.SendMessage("DropTrigger", this); return; } //Look for an empty slot to move destination item int destIndex = 0; for (int i = 0; i < dropRows * dropCols; i++) { if (dropSlots[i] == -1) { destIndex = i; break; } } //Oops... no room for drop if (dropSlots[destIndex] != -1) //Setting trigger variables { dropErrorCode = DropErrorCode.noRoomForDrop; gameObject.SendMessage("DropTrigger", this); return; } //Move droped destination to new position dropSlots[destIndex] = dropSlots[index]; //Erase origin dragSlots[draggedSlotIndex] = -1; //Put drop in their slot dropSlots[index] = draggedIndex; //Setting trigger variables dropErrorCode = DropErrorCode.noError; gameObject.SendMessage("DropTrigger", this); return; } //Simpe drop dragSlots[draggedSlotIndex] = -1; dropSlots[index] = draggedIndex; //Setting trigger variables dropErrorCode = DropErrorCode.noError; gameObject.SendMessage("DropTrigger", this); return; } //This point should never be reached Debug.Log("Error in STATE dropping in drop area"); }
//Draw and manage drag and drop icons void DrawDragDropIcons() { Rect cellPos = new Rect(); //Background cell rect cellPos.width = cellSize; cellPos.height = cellSize; Rect iconPos = new Rect(); //Icon cell rect iconPos.width = cellSize * iconScale; iconPos.height = cellSize * iconScale; float delta = (cellPos.width - iconPos.width) / 2f; for (int i = 0; i < dragRows; i++) { cellPos.y = dragTitleSize + dragRect.y + cellPadding + (i * (cellSize + cellPadding)); iconPos.y = cellPos.y + delta; for (int j = 0; j < dragCols; j++) { cellPos.x = dragRect.x + cellPadding + (j * (cellSize + cellPadding)); iconPos.x = cellPos.x + delta; if (cellPos.Contains(Event.current.mousePosition) && Event.current.isMouse && Event.current.type == EventType.mouseUp && draggingItem) { dropItemInDrag((i * dragCols) + j); if (autosave) { saveSong(dragDropName); } draggingItem = false; draggedSlotIndex = -1; draggedIndex = -1; itemOrigin = DragDropOrigin.None; } //If dragging this item => draw empty and continue if (draggedSlotIndex == (i * dragCols) + j && itemOrigin == DragDropOrigin.DragBox) { GUI.DrawTexture(cellPos, dragEmptySlot); continue; } //If start dragging set the proper variables if (cellPos.Contains(Event.current.mousePosition) && dragSlots[(i * dragCols) + j] != -1) { if (Event.current.isMouse && Event.current.button == 0 && Event.current.type == EventType.mouseDrag && !draggingItem) { draggingItem = true; draggedSlotIndex = (i * dragCols) + j; draggedIndex = dragSlots[draggedSlotIndex]; itemOrigin = DragDropOrigin.DragBox; //Trigger variables } } //Draw the drag cell drawDragCell(cellPos, iconPos, (i * dragCols) + j); if (!draggingItem && cellPos.Contains(Event.current.mousePosition) && dragSlots[(i * dragCols) + j] != -1 && showHints) { tooltip = CreateTooltip(dragDropItems[dragSlots[(i * dragCols) + j]]); showTooltip = true; } if (tooltip == "") { showTooltip = false; } } } //Goes for drop slots for (int i = 0; i < dropRows; i++) { cellPos.y = dropTitleSize + dropRect.y + cellPadding + (i * (cellSize + cellPadding)); iconPos.y = cellPos.y + delta; for (int j = 0; j < dropCols; j++) { cellPos.x = dropRect.x + cellPadding + (j * (cellSize + cellPadding)); iconPos.x = cellPos.x + delta; if (cellPos.Contains(Event.current.mousePosition) && Event.current.isMouse && Event.current.type == EventType.mouseUp && draggingItem) { dropItemInDrop((i * dropCols) + j); if (autosave) { saveSong(dragDropName); } draggingItem = false; draggedSlotIndex = -1; draggedIndex = -1; itemOrigin = DragDropOrigin.None; } //If dragging this item => draw empty and continue if (draggedSlotIndex == (i * dropCols) + j && itemOrigin == DragDropOrigin.DropBox) { GUI.DrawTexture(cellPos, dropEmptySlot); continue; } //If start dragging set the proper variables if (cellPos.Contains(Event.current.mousePosition) && dropSlots[(i * dragCols) + j] != -1) { if (Event.current.isMouse && Event.current.button == 0 && Event.current.type == EventType.mouseDrag && !draggingItem) { draggingItem = true; draggedSlotIndex = (i * dropCols) + j; draggedIndex = dropSlots[draggedSlotIndex]; itemOrigin = DragDropOrigin.DropBox; } } //Draw the drop cell drawDropCell(cellPos, iconPos, (i * dropCols) + j); if (!draggingItem && cellPos.Contains(Event.current.mousePosition) && dropSlots[(i * dropCols) + j] != -1 && showHints) { tooltip = CreateTooltip(dragDropItems[dropSlots[(i * dropCols) + j]]); showTooltip = true; } if (tooltip == "") { showTooltip = false; } } } //Draw the dragged itemS if (draggingItem) { cellPos.x = Event.current.mousePosition.x - (cellSize / 2); cellPos.y = Event.current.mousePosition.y - (cellSize / 2); iconPos.x = cellPos.x + delta; iconPos.y = cellPos.y + delta; drawCell(itemOrigin, cellPos, iconPos, draggedSlotIndex); } //Mouse up in clear area... stops dragging if (Event.current.isMouse && Event.current.type == EventType.mouseUp && draggingItem) { //Setting trigger variables originSlot = draggedSlotIndex; destinationSlot = -1; droppedItemIndex = draggedIndex; originIsDrag = (itemOrigin == DragDropOrigin.DragBox)?true:false; destinationIsDrag = false; dropErrorCode = DropErrorCode.notAllowedArea; gameObject.SendMessage("DropTrigger", this); draggingItem = false; draggedSlotIndex = -1; draggedIndex = -1; itemOrigin = DragDropOrigin.None; } }
//Drop an item in the drag area void dropItemInDrag(int index) { //Setting trigger variables originSlot = draggedSlotIndex; destinationSlot = index; droppedItemIndex = draggedIndex; //If move in same box swap items and return if (itemOrigin == DragDropOrigin.DragBox) { dragSlots[draggedSlotIndex] = dragSlots[index]; dragSlots[index] = draggedIndex; //Setting trigger variables originIsDrag = true; destinationIsDrag = true; dropErrorCode = DropErrorCode.noError; gameObject.SendMessage("DropTrigger", this); return; } //If moves from drop to drag originIsDrag = false; destinationIsDrag = true; if (itemOrigin == DragDropOrigin.DropBox && direction == DragDropDirection.Bidirectional) { //If destination is not empty if (dragSlots[index] != -1) { //If behavior is move to empty only... returns if (dropBehavior == DropBehavior.DropOnlyIfEmpty) { //Setting trigger variables dropErrorCode = DropErrorCode.notAllowedSlot; gameObject.SendMessage("DropTrigger", this); return; } //Look for an empty slot to move destination item int destIndex = 0; for (int i = 0; i < dragRows * dragCols; i++) { if (dragSlots[i] == -1) { destIndex = i; break; } } //Oops... no room for drop if (dragSlots[destIndex] != -1) { //Setting trigger variables dropErrorCode = DropErrorCode.noRoomForDrop; gameObject.SendMessage("DropTrigger", this); return; } //Move droped destination to new position dragSlots[destIndex] = dragSlots[index]; //Erase origin in drop area dropSlots[draggedSlotIndex] = -1; //Put drop in their slot dragSlots[index] = draggedIndex; //Setting trigger variables dropErrorCode = DropErrorCode.noError; gameObject.SendMessage("DropTrigger", this); return; } //Simpe drop dropSlots[draggedSlotIndex] = -1; dragSlots[index] = draggedIndex; //Setting trigger variables dropErrorCode = DropErrorCode.noError; gameObject.SendMessage("DropTrigger", this); return; } //Setting trigger variables. Error no bidirectional dropErrorCode = DropErrorCode.notAllowedBox; gameObject.SendMessage("DropTrigger", this); }