/// <summary> /// Touch/mouse currently over UIItem. If exitting this button, but still overtop of another Button, this might be a parent. Checks if parent and /// does not exit. /// Only call manually if you need to simulate touch. /// </summary> public void CurrentOverUIItem(tk2dUIItem overUIItem) { if (overUIItem != this) { if (isPressed) { if (!isOverTouchDisabled) { //check if overButton is child bool isUIItemChild = CheckIsUIItemChildOfMe(overUIItem); if (!isUIItemChild) { Exit(); if (parentUIItem != null) { parentUIItem.CurrentOverUIItem(overUIItem); } } } } else { if (parentUIItem != null) { parentUIItem.CurrentOverUIItem(overUIItem); } } } }
//checks for inputs (multi-touch) private void CheckMultiTouchInputs() { bool isAnyPressBeganRecorded = false; int prevFingerID = -1; bool wasPrevTouchFound = false; bool isNewlyPressed = false; touchCounter = 0; if (inputEnabled) { if (Input.touchCount > 0) { foreach (Touch touch in Input.touches) { if (touchCounter < MAX_MULTI_TOUCH_COUNT) { allTouches[touchCounter] = new tk2dUITouch(touch); touchCounter++; } else { break; } } } else { if (Input.GetMouseButtonDown(0)) { allTouches[touchCounter] = new tk2dUITouch(TouchPhase.Began, tk2dUITouch.MOUSE_POINTER_FINGER_ID, Input.mousePosition, Vector2.zero, 0); mouseDownFirstPos = Input.mousePosition; touchCounter++; } else if (Input.GetMouseButton(0) || Input.GetMouseButtonUp(0)) { Vector2 deltaPosition = mouseDownFirstPos - new Vector2(Input.mousePosition.x, Input.mousePosition.y); TouchPhase mousePhase = TouchPhase.Moved; if (Input.GetMouseButtonUp(0)) { mousePhase = TouchPhase.Ended; } else if (deltaPosition == Vector2.zero) { mousePhase = TouchPhase.Stationary; } allTouches[touchCounter] = new tk2dUITouch(mousePhase, tk2dUITouch.MOUSE_POINTER_FINGER_ID, Input.mousePosition, deltaPosition, tk2dUITime.deltaTime); touchCounter++; } } } for (int p = 0; p < touchCounter; p++) { pressedUIItems[p] = RaycastForUIItem(allTouches[p].position); } //deals with all the previous presses for (int f = 0; f < prevPressedUIItemList.Count; f++) { prevPressedItem = prevPressedUIItemList[f]; if (prevPressedItem != null) { prevFingerID = prevPressedItem.Touch.fingerId; wasPrevTouchFound = false; for (int t = 0; t < touchCounter; t++) { currTouch = allTouches[t]; if (currTouch.fingerId == prevFingerID) { wasPrevTouchFound = true; currPressedItem = pressedUIItems[t]; if (currTouch.phase == TouchPhase.Began) { prevPressedItem.CurrentOverUIItem(currPressedItem); if (prevPressedItem != currPressedItem) { prevPressedItem.Release(); prevPressedUIItemList.RemoveAt(f); f--; } } else if (currTouch.phase == TouchPhase.Ended) { prevPressedItem.CurrentOverUIItem(currPressedItem); prevPressedItem.UpdateTouch(currTouch); prevPressedItem.Release(); prevPressedUIItemList.RemoveAt(f); f--; } else { prevPressedItem.CurrentOverUIItem(currPressedItem); prevPressedItem.UpdateTouch(currTouch); } break; } } if (!wasPrevTouchFound) { prevPressedItem.CurrentOverUIItem(null); prevPressedItem.Release(); prevPressedUIItemList.RemoveAt(f); f--; } } } for (int f = 0; f < touchCounter; f++) { currPressedItem = pressedUIItems[f]; currTouch = allTouches[f]; if (currTouch.phase == TouchPhase.Began) { if (currPressedItem != null) { isNewlyPressed = currPressedItem.Press(currTouch); if (isNewlyPressed) { prevPressedUIItemList.Add(currPressedItem); } } isAnyPressBeganRecorded = true; } } if (isAnyPressBeganRecorded) { if (OnAnyPress != null) { OnAnyPress(); } } }
//checks for inputs (non-multi-touch) private void CheckInputs() { bool isPrimaryTouchFound = false; bool isSecondaryTouchFound = false; bool isAnyPressBeganRecorded = false; primaryTouch = new tk2dUITouch(); secondaryTouch = new tk2dUITouch(); resultTouch = new tk2dUITouch(); hitUIItem = null; if (inputEnabled) { if (Input.touchCount > 0) { foreach (Touch touch in Input.touches) { if (touch.phase == TouchPhase.Began) { primaryTouch = new tk2dUITouch(touch); isPrimaryTouchFound = true; isAnyPressBeganRecorded = true; } else if (pressedUIItem != null && touch.fingerId == firstPressedUIItemTouch.fingerId) { secondaryTouch = new tk2dUITouch(touch); isSecondaryTouchFound = true; } } checkForHovers = false; } else { if (Input.GetMouseButtonDown(0)) { primaryTouch = new tk2dUITouch(TouchPhase.Began, tk2dUITouch.MOUSE_POINTER_FINGER_ID, Input.mousePosition, Vector2.zero, 0); isPrimaryTouchFound = true; isAnyPressBeganRecorded = true; } else if (Input.GetMouseButton(0) || Input.GetMouseButtonUp(0)) { Vector2 deltaPosition = Vector2.zero; TouchPhase mousePhase = TouchPhase.Moved; if (pressedUIItem != null) { deltaPosition = firstPressedUIItemTouch.position - new Vector2(Input.mousePosition.x, Input.mousePosition.y); } if (Input.GetMouseButtonUp(0)) { mousePhase = TouchPhase.Ended; } else if (deltaPosition == Vector2.zero) { mousePhase = TouchPhase.Stationary; } secondaryTouch = new tk2dUITouch(mousePhase, tk2dUITouch.MOUSE_POINTER_FINGER_ID, Input.mousePosition, deltaPosition, tk2dUITime.deltaTime); isSecondaryTouchFound = true; } } } if (isPrimaryTouchFound) { resultTouch = primaryTouch; } else if (isSecondaryTouchFound) { resultTouch = secondaryTouch; } if (isPrimaryTouchFound || isSecondaryTouchFound) //focus touch found { hitUIItem = RaycastForUIItem(resultTouch.position); if (resultTouch.phase == TouchPhase.Began) { if (pressedUIItem != null) { pressedUIItem.CurrentOverUIItem(hitUIItem); if (pressedUIItem != hitUIItem) { pressedUIItem.Release(); pressedUIItem = null; } else { firstPressedUIItemTouch = resultTouch; //just incase touch changed } } if (hitUIItem != null) { hitUIItem.Press(resultTouch); } pressedUIItem = hitUIItem; firstPressedUIItemTouch = resultTouch; } else if (resultTouch.phase == TouchPhase.Ended) { if (pressedUIItem != null) { pressedUIItem.CurrentOverUIItem(hitUIItem); pressedUIItem.UpdateTouch(resultTouch); pressedUIItem.Release(); pressedUIItem = null; } } else { if (pressedUIItem != null) { pressedUIItem.CurrentOverUIItem(hitUIItem); pressedUIItem.UpdateTouch(resultTouch); } } } else //no touches found { if (pressedUIItem != null) { pressedUIItem.CurrentOverUIItem(null); pressedUIItem.Release(); pressedUIItem = null; } } //only if hover events are enabled and only if no touch events have ever been recorded if (checkForHovers) { if (inputEnabled) //if input enabled and mouse button is not currently down { if (!isPrimaryTouchFound && !isSecondaryTouchFound && hitUIItem == null && !Input.GetMouseButton(0)) //if raycast for a button has not yet been done { hitUIItem = RaycastForUIItem(Input.mousePosition); } else if (Input.GetMouseButton(0)) //if mouse button is down clear it { hitUIItem = null; } } if (hitUIItem != null) { if (hitUIItem.isHoverEnabled) { bool wasPrevOverFound = hitUIItem.HoverOver(overUIItem); if (!wasPrevOverFound && overUIItem != null) { overUIItem.HoverOut(hitUIItem); } overUIItem = hitUIItem; } else { if (overUIItem != null) { overUIItem.HoverOut(null); } } } else { if (overUIItem != null) { overUIItem.HoverOut(null); } } } if (isAnyPressBeganRecorded) { if (OnAnyPress != null) { OnAnyPress(); } } }
//checks for inputs (multi-touch) private void CheckMultiTouchInputs() { bool isAnyPressBeganRecorded = false; int prevFingerID = -1; bool wasPrevTouchFound = false; bool isNewlyPressed = false; touchCounter = 0; if (inputEnabled) { if (Input.touchCount > 0) { foreach (Touch touch in Input.touches) { if (touchCounter < MAX_MULTI_TOUCH_COUNT) { allTouches[touchCounter] = new tk2dUITouch(touch); touchCounter++; } else { break; } } } else { if (Input.GetMouseButtonDown(0)) { allTouches[touchCounter] = new tk2dUITouch(TouchPhase.Began, tk2dUITouch.MOUSE_POINTER_FINGER_ID, Input.mousePosition, Vector2.zero, 0); mouseDownFirstPos = Input.mousePosition; touchCounter++; } else if (Input.GetMouseButton(0) || Input.GetMouseButtonUp(0)) { Vector2 deltaPosition = mouseDownFirstPos - new Vector2(Input.mousePosition.x, Input.mousePosition.y); TouchPhase mousePhase = TouchPhase.Moved; if (Input.GetMouseButtonUp(0)) { mousePhase = TouchPhase.Ended; } else if (deltaPosition == Vector2.zero) { mousePhase = TouchPhase.Stationary; } allTouches[touchCounter] = new tk2dUITouch(mousePhase, tk2dUITouch.MOUSE_POINTER_FINGER_ID, Input.mousePosition, deltaPosition, tk2dUITime.deltaTime); touchCounter++; } } } for (int p = 0; p < touchCounter; p++) { pressedUIItems[p] = RaycastForUIItem(allTouches[p].position); } //deals with all the previous presses for (int f=0; f<prevPressedUIItemList.Count; f++) { prevPressedItem = prevPressedUIItemList[f]; if (prevPressedItem != null) { prevFingerID = prevPressedItem.Touch.fingerId; wasPrevTouchFound=false; for (int t = 0; t < touchCounter; t++) { currTouch = allTouches[t]; if (currTouch.fingerId == prevFingerID) { wasPrevTouchFound=true; currPressedItem = pressedUIItems[t]; if (currTouch.phase == TouchPhase.Began) { prevPressedItem.CurrentOverUIItem(currPressedItem); if (prevPressedItem != currPressedItem) { prevPressedItem.Release(); prevPressedUIItemList.RemoveAt(f); f--; } } else if (currTouch.phase == TouchPhase.Ended) { prevPressedItem.CurrentOverUIItem(currPressedItem); prevPressedItem.UpdateTouch(currTouch); prevPressedItem.Release(); prevPressedUIItemList.RemoveAt(f); f--; } else { prevPressedItem.CurrentOverUIItem(currPressedItem); prevPressedItem.UpdateTouch(currTouch); } break; } } if(!wasPrevTouchFound) { prevPressedItem.CurrentOverUIItem(null); prevPressedItem.Release(); prevPressedUIItemList.RemoveAt(f); f--; } } } for (int f = 0; f < touchCounter; f++) { currPressedItem = pressedUIItems[f]; currTouch = allTouches[f]; if (currTouch.phase == TouchPhase.Began) { if (currPressedItem != null) { isNewlyPressed = currPressedItem.Press(currTouch); if (isNewlyPressed) { prevPressedUIItemList.Add(currPressedItem); } } isAnyPressBeganRecorded = true; } } if (isAnyPressBeganRecorded) { if (OnAnyPress != null) { OnAnyPress(); } } }
//checks for inputs (multi-touch) private bool CheckMultiTouchInputs() { bool claimTouches = false; bool isAnyPressBeganRecorded = false; int prevFingerID = -1; bool wasPrevTouchFound = false; bool isNewlyPressed = false; touchCounter = 0; if (IsEnabledInputTouches) { if (Input.touchCount > 0) { foreach (Touch touch in Input.touches) { if (touchCounter < MAX_MULTI_TOUCH_COUNT) { allTouches[touchCounter] = new tk2dUITouch(touch); touchCounter++; } else { break; } } } else { if (Input.GetMouseButtonDown(0)) { allTouches[touchCounter] = new tk2dUITouch(TouchPhase.Began, tk2dUITouch.MOUSE_POINTER_FINGER_ID, Input.mousePosition, Vector2.zero, 0); mouseDownFirstPos = Input.mousePosition; touchCounter++; } else if (Input.GetMouseButton(0) || Input.GetMouseButtonUp(0)) { Vector2 deltaPosition = mouseDownFirstPos - new Vector2(Input.mousePosition.x, Input.mousePosition.y); TouchPhase mousePhase = TouchPhase.Moved; if (Input.GetMouseButtonUp(0)) { mousePhase = TouchPhase.Ended; } else if (deltaPosition == Vector2.zero) { mousePhase = TouchPhase.Stationary; } allTouches[touchCounter] = new tk2dUITouch(mousePhase, tk2dUITouch.MOUSE_POINTER_FINGER_ID, Input.mousePosition, deltaPosition, tk2dUITime.deltaTime); touchCounter++; } } } for (int p = 0; p < touchCounter; p++) { tk2dUITouch touch = allTouches[p]; pressedUIItems[p] = RaycastForUIItem(touch.position); } //deals with all the previous presses for (int f = prevPressedUIItemList.Count - 1; f >= 0; f--) { tk2dUIItem prevPressedItem = prevPressedUIItemList[f]; if ((prevPressedItem != null) && (prevPressedItem.isActiveAndEnabled)) { prevFingerID = prevPressedItem.Touch.fingerId; wasPrevTouchFound = false; for (int t = 0; t < touchCounter; t++) { currTouch = allTouches[t]; if (currTouch.fingerId == prevFingerID) { wasPrevTouchFound = true; currPressedItem = pressedUIItems[t]; if (currPressedItem != null && currPressedItem.isActiveAndEnabled && prevPressedItem != null) { /*if (currTouch.phase == TouchPhase.Began) * { * prevPressedItem.CurrentOverUIItem(currPressedItem); * * if (prevPressedItem != currPressedItem) * { * prevPressedItem.Release(); * * temp.Add(prevPressedItem); * // if (prevPressedUIItemList.Contains(prevPressedItem)) * // { * // prevPressedUIItemList.Remove(prevPressedItem); * // } * // prevPressedItem = null; * * if (touchCounter > 1) * { * CustomDebug.Log("Began!"); * } * } * } * else */if (currTouch.phase == TouchPhase.Ended) { prevPressedItem.CurrentOverUIItem(currPressedItem); prevPressedItem.UpdateTouch(currTouch); prevPressedItem.Release(); if (prevPressedUIItemList.Contains(prevPressedItem)) { prevPressedUIItemList.Remove(prevPressedItem); } // prevPressedItem = null; } else if (currTouch.phase == TouchPhase.Canceled) { prevPressedItem.CurrentOverUIItem(currPressedItem); prevPressedItem.UpdateTouch(currTouch); prevPressedItem.Release(); if (prevPressedUIItemList.Contains(prevPressedItem)) { prevPressedUIItemList.Remove(prevPressedItem); } // prevPressedItem = null; } else { prevPressedItem.CurrentOverUIItem(currPressedItem); prevPressedItem.UpdateTouch(currTouch); } } break; } } if (!wasPrevTouchFound && (prevPressedItem != null)) { prevPressedItem.CurrentOverUIItem(null); prevPressedItem.Release(); if (prevPressedUIItemList.Contains(prevPressedItem)) { prevPressedUIItemList.Remove(prevPressedItem); } } } else { prevPressedUIItemList.RemoveAt(f); } } for (int f = 0; f < touchCounter; f++) { currPressedItem = pressedUIItems[f]; currTouch = allTouches[f]; if (currTouch.phase == TouchPhase.Began) { if (currPressedItem != null && currPressedItem.isActiveAndEnabled) { claimTouches = true; isNewlyPressed = currPressedItem.Press(currTouch); if (isNewlyPressed && !prevPressedUIItemList.Contains(currPressedItem)) { prevPressedUIItemList.Add(currPressedItem); } } isAnyPressBeganRecorded = true; } } if (isAnyPressBeganRecorded) { if (OnAnyPress != null) { OnAnyPress(); } } return(claimTouches); }