public override void update(float elapsedTime) { if (!mUseHover || !mUseGlobalTouch) { return; } int touchCount = Input.touchCount; // 多点触控和单点触控需要分开判断 if (touchCount >= 2) { // 此处未考虑到触点离开窗口时以及再次进入窗口所引起的结束多点触控和开始多点触控的判断 List <IMouseEventCollect> endMultiWindow = mListPool.newList(out endMultiWindow); mTempMultiTouchPoint.Clear(); mTempStartMultiWindow.Clear(); mTempMovingMultiWindow.Clear(); mTouchInWindow.Clear(); // 查找触点所在窗口 for (int i = 0; i < touchCount; ++i) { Touch touch = Input.GetTouch(i); mTempMultiTouchPoint.Add(touch.fingerId, touch); Vector3 touchPosition = new Vector3(touch.position.x, touch.position.y, 0.0f); IMouseEventCollect hoverWindow = getHoverWindow(ref touchPosition); if (hoverWindow != null) { if (!mTouchInWindow.ContainsKey(hoverWindow)) { mTouchInWindow.Add(hoverWindow, new Dictionary <int, Touch>()); } // 每个窗口最多有两个触点 if (mTouchInWindow[hoverWindow].Count < 2) { mTouchInWindow[hoverWindow].Add(touch.fingerId, touch); } } } // 判断哪些窗口上一帧还拥有两个触点,但是这一帧不足两个触点,则是 foreach (var item in mTouchInWindow) { var touchesInWindow = item.Value; if (touchesInWindow.Count == 2) { // 结束多点触控 bool isEnd = false; bool isStart = false; foreach (var touches in touchesInWindow) { // 有触点结束了,则认为多点触控已经结束 if (touches.Value.phase == TouchPhase.Ended || touches.Value.phase == TouchPhase.Canceled) { isEnd = true; } // 有触点刚开始,则认为开始多点触控 else if (touches.Value.phase == TouchPhase.Began) { isStart = true; } } if (isEnd) { endMultiWindow.Add(item.Key); } else if (isStart) { mTempStartMultiWindow.Add(item.Key, new List <Touch>(touchesInWindow.Values)); } else { mTempMovingMultiWindow.Add(item.Key, new List <Touch>(touchesInWindow.Values)); } } } // 新增的多点触控窗口 foreach (var item in mTempStartMultiWindow) { if (!mMultiTouchWindowList.ContainsKey(item.Key)) { MultiTouchInfo info; mClassPool.newClass(out info); info.mWindow = item.Key; info.mPhase = TouchPhase.Began; info.mFinger0 = item.Value[0].fingerId; info.mFinger1 = item.Value[1].fingerId; info.mStartPosition0 = item.Value[0].position; info.mStartPosition1 = item.Value[1].position; info.mCurPosition0 = info.mStartPosition0; info.mCurPosition1 = info.mStartPosition1; mMultiTouchWindowList.Add(item.Key, info); } item.Key.onMultiTouchStart(item.Value[0].position, item.Value[1].position); } // 更新已存在的多点触控窗口 foreach (var item in mMultiTouchWindowList) { IMouseEventCollect window = item.Key; if (mTempMovingMultiWindow.ContainsKey(window)) { Touch touch0 = mTempMultiTouchPoint[item.Value.mFinger0]; Touch touch1 = mTempMultiTouchPoint[item.Value.mFinger1]; if (!isVectorEqual(touch0.position, item.Value.mCurPosition0) || !isVectorEqual(touch1.position, item.Value.mCurPosition1)) { window.onMultiTouchMove(touch0.position, item.Value.mCurPosition0, touch1.position, item.Value.mCurPosition1); item.Value.mCurPosition0 = touch0.position; item.Value.mCurPosition1 = touch1.position; item.Value.mPhase = TouchPhase.Moved; } else { item.Value.mPhase = TouchPhase.Stationary; } } } // 结束多点触控的窗口 foreach (var item in endMultiWindow) { item.onMultiTouchEnd(); if (mMultiTouchWindowList.ContainsKey(item)) { mClassPool.destroyClass(mMultiTouchWindowList[item]); mMultiTouchWindowList.Remove(item); } } mListPool.destroyList(endMultiWindow); } else { // 手指操作触摸屏 if (touchCount == 1) { Touch touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Began) { notifyGlobalPress(true); } else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) { notifyGlobalPress(false); } } // 鼠标点击屏幕 else if (touchCount == 0) { if (mInputManager.getMouseCurrentDown(MOUSE_BUTTON.MB_LEFT)) { notifyGlobalPress(true); } else if (mInputManager.getMouseCurrentUp(MOUSE_BUTTON.MB_LEFT)) { notifyGlobalPress(false); } } // 模拟触摸时,更新触摸点 if (mSimulateTouch && mMousePressed) { mCurTouchPosition = Input.mousePosition; } // 鼠标移动检测 Vector3 curMousePosition = getCurMousePosition(); if (!isVectorEqual(ref mLastMousePosition, ref curMousePosition)) { // 检查当前悬停窗口 checkHoverWindow(ref curMousePosition, true); // 鼠标移动事件 Vector3 moveDelta = curMousePosition - mLastMousePosition; mHoverWindow?.onMouseMove(ref curMousePosition, ref moveDelta, elapsedTime); // 给鼠标按下时选中的所有窗口发送鼠标移动的消息 foreach (var item in mMouseDownWindowList) { if (item != mHoverWindow) { item.onMouseMove(ref curMousePosition, ref moveDelta, elapsedTime); } } mLastMousePosition = curMousePosition; // 如果在一个窗口上停留超过0.05秒没有移动,则触发停留在窗口上的事件 mStayTimer.start(); } else { if (mStayTimer.tickTimer(elapsedTime)) { mHoverWindow?.onMouseStay(curMousePosition); // 给鼠标按下时选中的所有窗口发送鼠标移动的消息 foreach (var item in mMouseDownWindowList) { item.onMouseStay(curMousePosition); } } } } // 检查摄像机是否被销毁 foreach (var item in mMouseCastWindowList) { if (item.mCamera != null && item.mCamera.isDestroied()) { logError("摄像机已销毁:" + item.mCamera.getName()); } } foreach (var item in mMouseCastObjectList) { if (item.mCamera != null && item.mCamera.isDestroied()) { logError("摄像机已销毁:" + item.mCamera.getName()); } } if (getKeyCurrentDown(KeyCode.F2)) { Vector3 mousePos = getMousePosition(); var resultList = getAllHoverWindow(ref mousePos, null, true); int resultCount = resultList.Count; for (int i = 0; i < resultCount; ++i) { UIDepth depth = resultList[i].getUIDepth(); logInfo("窗口:" + resultList[i].getName() + "深度:layout:" + depth.mPanelDepth + ", window:" + depth.mWindowDepth, LOG_LEVEL.LL_FORCE); } } }
public override void update(float elapsedTime) { if (!mUseHover || !mUseGlobalTouch) { return; } int touchCount = Input.touchCount; // 多点触控和单点触控需要分开判断 if (touchCount >= 2) { // 此处未考虑到触点离开窗口时以及再次进入窗口所引起的结束多点触控和开始多点触控的判断 LIST(out List <IMouseEventCollect> endMultiWindow); mTempMultiTouchPoint.Clear(); mTempStartMultiWindow.Clear(); mTempMovingMultiWindow.Clear(); mTouchInWindow.Clear(); // 查找触点所在窗口 for (int i = 0; i < touchCount; ++i) { Touch touch = Input.GetTouch(i); mTempMultiTouchPoint.Add(touch.fingerId, touch); Vector3 touchPosition = new Vector3(touch.position.x, touch.position.y, 0.0f); IMouseEventCollect hoverWindow = getHoverWindow(ref touchPosition); if (hoverWindow != null) { if (!mTouchInWindow.TryGetValue(hoverWindow, out Dictionary <int, Touch> touchList)) { touchList = new Dictionary <int, Touch>(); mTouchInWindow.Add(hoverWindow, touchList); } // 每个窗口最多有两个触点 if (touchList.Count < 2) { touchList.Add(touch.fingerId, touch); } } } // 判断哪些窗口上一帧还拥有两个触点,但是这一帧不足两个触点,则是 foreach (var item in mTouchInWindow) { var touchesInWindow = item.Value; if (touchesInWindow.Count == 2) { // 结束多点触控 bool isEnd = false; bool isStart = false; foreach (var touches in touchesInWindow) { // 有触点结束了,则认为多点触控已经结束 if (touches.Value.phase == TouchPhase.Ended || touches.Value.phase == TouchPhase.Canceled) { isEnd = true; } // 有触点刚开始,则认为开始多点触控 else if (touches.Value.phase == TouchPhase.Began) { isStart = true; } } if (isEnd) { endMultiWindow.Add(item.Key); } else if (isStart) { mTempStartMultiWindow.Add(item.Key, new List <Touch>(touchesInWindow.Values)); } else { mTempMovingMultiWindow.Add(item.Key, new List <Touch>(touchesInWindow.Values)); } } } // 新增的多点触控窗口 foreach (var item in mTempStartMultiWindow) { if (!mMultiTouchWindowList.ContainsKey(item.Key)) { CLASS(out MultiTouchInfo info); info.mWindow = item.Key; info.mPhase = TouchPhase.Began; info.mFinger0 = item.Value[0].fingerId; info.mFinger1 = item.Value[1].fingerId; info.mStartPosition0 = item.Value[0].position; info.mStartPosition1 = item.Value[1].position; info.mCurPosition0 = info.mStartPosition0; info.mCurPosition1 = info.mStartPosition1; mMultiTouchWindowList.Add(item.Key, info); } item.Key.onMultiTouchStart(item.Value[0].position, item.Value[1].position); } // 更新已存在的多点触控窗口 foreach (var item in mMultiTouchWindowList) { IMouseEventCollect window = item.Key; if (mTempMovingMultiWindow.ContainsKey(window)) { Touch touch0 = mTempMultiTouchPoint[item.Value.mFinger0]; Touch touch1 = mTempMultiTouchPoint[item.Value.mFinger1]; if (!isVectorEqual(touch0.position, item.Value.mCurPosition0) || !isVectorEqual(touch1.position, item.Value.mCurPosition1)) { window.onMultiTouchMove(touch0.position, item.Value.mCurPosition0, touch1.position, item.Value.mCurPosition1); item.Value.mCurPosition0 = touch0.position; item.Value.mCurPosition1 = touch1.position; item.Value.mPhase = TouchPhase.Moved; } else { item.Value.mPhase = TouchPhase.Stationary; } } } // 结束多点触控的窗口 int endCount = endMultiWindow.Count; for (int i = 0; i < endCount; ++i) { IMouseEventCollect item = endMultiWindow[i]; item.onMultiTouchEnd(); if (mMultiTouchWindowList.TryGetValue(item, out MultiTouchInfo info)) { UN_CLASS(info); mMultiTouchWindowList.Remove(item); } } UN_LIST(endMultiWindow); } else { // 手指操作触摸屏 if (touchCount == 1) { Touch touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Began) { notifyGlobalPress(true); } else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) { notifyGlobalPress(false); } } // 鼠标点击屏幕 else if (touchCount == 0) { if (mInputManager.getMouseCurrentDown(MOUSE_BUTTON.LEFT)) { notifyGlobalPress(true); } else if (mInputManager.getMouseCurrentUp(MOUSE_BUTTON.LEFT)) { notifyGlobalPress(false); } } // 模拟触摸时,更新触摸点 if (mSimulateTouch && mMousePressed) { mCurTouchPosition = Input.mousePosition; } // 鼠标移动检测 Vector3 curMousePosition = getCurMousePosition(); if (!isVectorEqual(ref mLastMousePosition, ref curMousePosition)) { // 检查当前悬停窗口 checkHoverWindow(ref curMousePosition, true); // 鼠标移动事件 Vector3 moveDelta = curMousePosition - mLastMousePosition; mHoverWindow?.onMouseMove(ref curMousePosition, ref moveDelta, elapsedTime); // 给鼠标按下时选中的所有窗口发送鼠标移动的消息 int count = mMouseDownWindowList.Count; for (int i = 0; i < count; ++i) { IMouseEventCollect item = mMouseDownWindowList[i]; if (item != mHoverWindow) { item.onMouseMove(ref curMousePosition, ref moveDelta, elapsedTime); } } mLastMousePosition = curMousePosition; // 如果在一个窗口上停留超过0.05秒没有移动,则触发停留在窗口上的事件 mStayTimer.start(); } else { if (mStayTimer.tickTimer(elapsedTime)) { mHoverWindow?.onMouseStay(curMousePosition); // 给鼠标按下时选中的所有窗口发送鼠标移动的消息 int count = mMouseDownWindowList.Count; for (int i = 0; i < count; ++i) { mMouseDownWindowList[i].onMouseStay(curMousePosition); } } } } // 检查摄像机是否被销毁 int windowCount = mMouseCastWindowList.Count; for (int i = 0; i < windowCount; ++i) { MouseCastWindowSet item = mMouseCastWindowList[i]; if (item.getCamera() != null && item.getCamera().isDestroy()) { logError("摄像机已销毁:" + item.getCamera().getName()); } } int objCount = mMouseCastObjectList.Count; for (int i = 0; i < objCount; ++i) { MouseCastObjectSet item = mMouseCastObjectList[i]; if (item.mCamera != null && item.mCamera.isDestroy()) { logError("摄像机已销毁:" + item.mCamera.getName()); } } }