void OnListElementDragBegin(ActionListDraggableElement source) { // Change the container of the UI Element (make it in front of all the other elements) onDrag = true; CreateTempDragContainer(); source.transform.SetParent(tempDragContainer.transform); }
void OnListElementDragEnd(ActionListDraggableElement source) { // Check if the UI element is included in the rect of another placeHolder // if this placeHolder is different, swap the two elements in the list onDrag = false; RectTransform sourceRect = source.gameObject.GetComponent <RectTransform>(); Debug.Log("On drag End, source: " + source.gameObject.name + " position: " + sourceRect); int originalIndex = toDoList.Items.FindIndex(n => n == source.GetAction()); for (int index = 0; index < placeHolders.Count; index++) { if (index == originalIndex) { continue; } RectTransform placeHolderRect = placeHolders[index].GetComponent <RectTransform>(); if (placeHolderRect.Overlaps(sourceRect, true)) { DestroyTempDragContainer(); MoveListElementTo(originalIndex, index); return; } } source.gameObject.transform.SetParent(placeHolders[originalIndex].transform); source.ResetPosition(); DestroyTempDragContainer(); }
// TODO: to optimise private void UpdateTaskListUI() { ClearTaskListUI(); uiElements = new List <GameObject>(); if (toDoList != null && placeHolders != null) { int index = 0; int nActions = toDoList.Items.Count; foreach (GameObject placeHolder in placeHolders) { if (index >= nActions) { break; } Action action = toDoList.Items[index]; if (onDrag && DraggedElement != null && DraggedElement.GetAction() == action) { // if the action if on the dragged element, do nothing } else { GameObject obj = Instantiate(actionListElementPrefab, placeHolder.transform); ActionListDraggableElement actionListElement = obj.GetComponent <ActionListDraggableElement>(); actionListElement.SetAction(action); uiElements.Add(obj); // store object reference } index++; } } }