Exemplo n.º 1
0
    public override void update(float elapsedTime)
    {
        if (!mUseHover || !mUseGlobalTouch)
        {
            return;
        }
        // 模拟触摸时,更新触摸点
        if (mSimulateTouch && mMousePressed)
        {
            mCurTouchPosition = Input.mousePosition;
        }
        // 鼠标移动检测
        Vector3 curMousePosition = getCurMousePosition();

        if (mLastMousePosition != curMousePosition)
        {
            // 计算鼠标当前所在最前端的窗口
            txUIObject newWindow = getHoverButton(curMousePosition);
            // 判断鼠标是否还在当前窗口内
            if (mHoverButton != null)
            {
                // 鼠标已经移动到了其他窗口中,发送鼠标离开的事件
                if (newWindow != mHoverButton)
                {
                    // 不过也许此时悬停窗口已经不接收输入事件了或者碰撞盒子被禁用了,需要判断一下
                    if (mHoverButton.getHandleInput() && mHoverButton.getBoxCollider().enabled)
                    {
                        hoverWindow(mHoverButton, false);
                    }
                    // 找到鼠标所在的新的窗口,给该窗口发送鼠标进入的事件
                    hoverWindow(newWindow, true);
                }
            }
            // 如果上一帧鼠标没有在任何窗口内,则计算这一帧鼠标所在的窗口
            else
            {
                // 发送鼠标进入的事件
                hoverWindow(newWindow, true);
            }
            mHoverButton = newWindow;
            if (mHoverButton != null)
            {
                Vector2 moveDelta = curMousePosition - mLastMousePosition;
                mHoverButton.onMouseMove(curMousePosition, moveDelta, getLength(moveDelta) / elapsedTime);
            }
            mLastMousePosition = curMousePosition;
            mCurStayTime       = 0.0f;
        }
        else
        {
            if (mCurStayTime >= 0.0f)
            {
                mCurStayTime += elapsedTime;
                if (mCurStayTime >= mStayTime)
                {
                    mCurStayTime = -1.0f;
                    if (mHoverButton != null)
                    {
                        mHoverButton.onMouseStay(curMousePosition);
                    }
                }
            }
        }
    }