Ejemplo n.º 1
0
void plInputManager::HandleWin32ControlEvent(UINT message, WPARAM Wparam, LPARAM Lparam, hsWindowHndl hWnd)
{
    if( !fhWnd )
        fhWnd = hWnd;

    bool bExtended;

    switch (message)
    {
    case SYSKEYDOWN:
    case KEYDOWN:
        {
            bExtended = Lparam >> 24 & 1;
            bool bRepeat = ((Lparam >> 29) & 0xf) != 0;
            for (int i=0; i<fInputDevices.Count(); i++)
                fInputDevices[i]->HandleKeyEvent( KEYDOWN, UntranslateKey((plKeyDef)Wparam, bExtended), true, bRepeat ); 
        }
        break;
    case SYSKEYUP:
    case KEYUP:
        {
            bExtended = Lparam >> 24 & 1;
            for (int i=0; i<fInputDevices.Count(); i++)
                fInputDevices[i]->HandleKeyEvent( KEYUP, UntranslateKey((plKeyDef)Wparam, bExtended), false, false ); 
        }
        break;
    case CHAR_MSG:
        {
            wchar_t ch = (wchar_t)Wparam;

            // These are handled by KEYUP/KEYDOWN and should not be sent
            // We don't like garbage getting in string fields
            if (std::iscntrl(ch, localeC))
                break;

            BYTE scan = (BYTE)(Lparam >> 16);
            UINT vkey = MapVirtualKey(scan, 1); //MAPVK_VSC_TO_VK

            bExtended = Lparam >> 24 & 1;
            bool bRepeat = ((Lparam >> 29) & 0xf) != 0;
            bool down = !(Lparam >> 31);
 
            for (int i=0; i<fInputDevices.Count(); i++)
                fInputDevices[i]->HandleKeyEvent( CHAR_MSG, (plKeyDef)vkey, down, bRepeat, ch );
        }
        break;
    case MOUSEWHEEL:
        {
            plMouseEventMsg* pMsg = new plMouseEventMsg;
            int zDelta = GET_WHEEL_DELTA_WPARAM(Wparam);
            pMsg->SetWheelDelta((float)zDelta);
            if (zDelta < 0)
                pMsg->SetButton(kWheelNeg);
            else
                pMsg->SetButton(kWheelPos);

            RECT rect;
            GetClientRect(hWnd, &rect);
            pMsg->SetXPos(LOWORD(Lparam) / (float)rect.right);
            pMsg->SetYPos(HIWORD(Lparam) / (float)rect.bottom);

            pMsg->Send();
        }
        break;
    case MOUSEMOVE:
    case L_BUTTONDN:
    case L_BUTTONUP:
    case R_BUTTONDN:
    case R_BUTTONUP:
    case L_BUTTONDBLCLK:
    case R_BUTTONDBLCLK:
    case M_BUTTONDN:
    case M_BUTTONUP:
        {
            
            RECT rect;
            GetClientRect(hWnd, &rect);
         
            plIMouseXEventMsg* pXMsg = new plIMouseXEventMsg;
            plIMouseYEventMsg* pYMsg = new plIMouseYEventMsg;
            plIMouseBEventMsg* pBMsg = new plIMouseBEventMsg;

            pXMsg->fWx = LOWORD(Lparam);
            pXMsg->fX = (float)LOWORD(Lparam) / (float)rect.right;
            pYMsg->fWy = HIWORD(Lparam);
            pYMsg->fY = (float)HIWORD(Lparam) / (float)rect.bottom;

            // Apply mouse scale
//          pXMsg->fX *= fMouseScale;
//          pYMsg->fY *= fMouseScale;
            
            if (Wparam & MK_LBUTTON && message != L_BUTTONUP)
            {
                pBMsg->fButton |= kLeftButtonDown;
            }
            else
            {
                pBMsg->fButton |= kLeftButtonUp;
            }
                        
            if (Wparam & MK_RBUTTON && message != R_BUTTONUP)
            {
                pBMsg->fButton |= kRightButtonDown;
            }
            else
            {
                pBMsg->fButton |= kRightButtonUp;
            }
            
            if (Wparam & MK_MBUTTON && message != M_BUTTONUP)
            {
                pBMsg->fButton |= kMiddleButtonDown;
            }
            else
            {
                pBMsg->fButton |= kMiddleButtonUp;
            }
            
            if( message == L_BUTTONDBLCLK )
                pBMsg->fButton |= kLeftButtonDblClk;        // We send the double clicks separately
            if( message == R_BUTTONDBLCLK )
                pBMsg->fButton |= kRightButtonDblClk;

            for (int i=0; i<fInputDevices.Count(); i++)
            {
                fInputDevices[i]->MsgReceive(pXMsg);
                fInputDevices[i]->MsgReceive(pYMsg);
                fInputDevices[i]->MsgReceive(pBMsg);
            }
            POINT pt;
            
            if (RecenterMouse() && (pXMsg->fX <= 0.1 || pXMsg->fX >= 0.9) )
            {       
                pt.x = (rect.right - rect.left) / 2;
                pt.y = HIWORD(Lparam);
                ClientToScreen(hWnd, &pt);
                SetCursorPos( pt.x, pt.y );
            }
            else
            if (RecenterMouse() && (pYMsg->fY <= 0.1 || pYMsg->fY >= 0.9) )
            {       
                pt.y = (rect.bottom - rect.top) / 2;
                pt.x = LOWORD(Lparam);
                ClientToScreen(hWnd, &pt);
                SetCursorPos( pt.x, pt.y );
            }
            if (RecenterMouse() && Lparam == 0)
            {
                pt.y = (rect.bottom - rect.top) / 2;
                pt.x = (rect.right - rect.left) / 2;
                ClientToScreen(hWnd, &pt);
                SetCursorPos( pt.x, pt.y );
            }
            delete(pXMsg);
            delete(pYMsg);
            delete(pBMsg);

            

        }
        break;
    case WM_ACTIVATE:
        {
            bool activated = ( LOWORD( Wparam ) == WA_INACTIVE ) ? false : true;
            Activate( activated );
        }
        break;
    }

}
Ejemplo n.º 2
0
bool QtInputService::eventFilter(QObject *obj, QEvent *event)
{
    switch(event->type())
    {
    case QEvent::KeyPress:
    {
        QKeyEvent *e = static_cast<QKeyEvent*>(event);

        KeyEvent keyEvent;
        keyEvent.keyCode = StripModifiersFromKey(e->key());
        keyEvent.keyPressCount = 1;
        keyEvent.modifiers = e->modifiers();
        keyEvent.text = e->text();
        keyEvent.sequence = QKeySequence(e->key() | e->modifiers()); ///\todo Track multi-key sequences.
        keyEvent.eventType = KeyEvent::KeyPressed;
//		keyEvent.otherHeldKeys = heldKeys; ///\todo
        keyEvent.handled = false;

        current_modifiers_ = e->modifiers(); // local tracking for mouse events

        // We only take key events from the main QGraphicsView.
        if (obj != qobject_cast<QObject*>(mainView))
            return false;

        std::map<Qt::Key, KeyPressInformation>::iterator keyRecord = heldKeys.find(keyEvent.keyCode);
        if (keyRecord != heldKeys.end())
        {
            if (e->isAutoRepeat()) // If this is a repeat, track the proper keyPressCount.
            {
                keyEvent.keyPressCount = ++keyRecord->second.keyPressCount;
                keyEvent.sequence = QKeySequence(); // Repeated keys do not trigger keysequences.
            }
        }
        else
        {
            KeyPressInformation info;
            info.keyPressCount = 1;
            info.keyState = KeyEvent::KeyPressed;
//            info.firstPressTime = now; ///\todo
            heldKeys[keyEvent.keyCode] = info;
        }

        // Queue up the press event for the polling API, independent of whether any Qt widget has keyboard focus.
        if (keyEvent.keyPressCount == 1) /// \todo The polling API does not get key repeats at all. Should it?
            newKeysPressedQueue.push_back(StripModifiersFromKey(e->key()));

        TriggerKeyEvent(keyEvent);

        return keyEvent.handled; // If we got true here, need to suppress this event from going to Qt.
    }

    case QEvent::KeyRelease:
    {
        // We only take key events from the main window.
        if (obj != qobject_cast<QObject*>(mainWindow))
            return false;

        QKeyEvent *e = static_cast<QKeyEvent *>(event);

        // Our input system policy: Key releases on repeated keys are not transmitted. This means
        // that the client gets always a sequences like "press (first), press(1st repeat), press(2nd repeat), release",
        // instead of "press(first), release, press(1st repeat), release, press(2nd repeat), release".
        if (e->isAutoRepeat())
            return false;

        HeldKeysMap::iterator existingKey = heldKeys.find(StripModifiersFromKey(e->key()));

        // If we received a release on an unknown key we haven't received a press for, don't pass it to the scene,
        // since we didn't pass the press to the scene either (or we've already passed the release before, so don't
        // pass duplicate releases).
        if (existingKey == heldKeys.end())
            return false;

        KeyEvent keyEvent;
        keyEvent.keyCode = StripModifiersFromKey(e->key());
        keyEvent.keyPressCount = existingKey->second.keyPressCount;
        keyEvent.modifiers = e->modifiers();
        keyEvent.text = e->text();
        keyEvent.eventType = KeyEvent::KeyReleased;
//		keyEvent.otherHeldKeys = heldKeys; ///\todo
        keyEvent.handled = false;

        heldKeys.erase(existingKey);
        current_modifiers_ = e->modifiers(); // local tracking for mouse events

        // Queue up the release event for the polling API, independent of whether any Qt widget has keyboard focus.
        if (keyEvent.keyPressCount == 1) /// \todo The polling API does not get key repeats at all. Should it?
            newKeysReleasedQueue.push_back(StripModifiersFromKey(e->key()));

        TriggerKeyEvent(keyEvent);

        return keyEvent.handled; // Suppress this event from going forward.
    }

    case QEvent::MouseButtonPress:
    case QEvent::MouseButtonRelease:
    {
        // We only take mouse button press and release events from the main QGraphicsView viewport.
        if (obj != qobject_cast<QObject*>(mainView->viewport()))
            return false;

        QMouseEvent *e = static_cast<QMouseEvent *>(event);
//		QGraphicsItem *itemUnderMouse = GetVisibleItemAtCoords(e->x(), e->y());
        /*
                // Update the flag that tracks whether the inworld scene or QGraphicsScene is grabbing mouse movement.
                if (event->type() == QEvent::MouseButtonPress)
                    sceneMouseCapture = (itemUnderMouse ? QtMouseCapture : SceneMouseCapture);
                else // event type == MouseButtonRelease
                    sceneMouseCapture = NoMouseCapture;
        */
        // We always update the global polled input states, independent of whether any the mouse cursor is
        // on top of any Qt widget.
        if (event->type() == QEvent::MouseButtonPress)
        {
            heldMouseButtons |= (MouseEvent::MouseButton)e->button();
            newMouseButtonsPressedQueue |= (MouseEvent::MouseButton)e->button();
        }
        else
        {
            if (lastMouseButtonReleaseTime.msecsTo(QTime::currentTime()) < 300)
            {
                doubleClickDetected = true;
            }
            heldMouseButtons &= ~(MouseEvent::MouseButton)e->button();
            newMouseButtonsReleasedQueue |= (MouseEvent::MouseButton)e->button();
            lastMouseButtonReleaseTime = QTime::currentTime();
        }

        // If there's a visible QGraphicsItem under the mouse and mouse is not in FPS mode,
        // the click's supposed to go there - don't send it at all to inworld scene.
//		if (itemUnderMouse && mouseCursorVisible)
//			return false;

        // The mouse coordinates we receive can come from different widgets, and we are interested only in the coordinates
        // in the QGraphicsView client area, so we need to remap them.
        QPoint mousePos = MapPointToMainGraphicsView(obj, e->pos());

        MouseEvent mouseEvent;
        mouseEvent.itemUnderMouse = GetVisibleItemAtCoords(e->x(), e->y());
        mouseEvent.origin = mouseEvent.itemUnderMouse ? MouseEvent::PressOriginQtWidget : MouseEvent::PressOriginScene;
        if ( !doubleClickDetected)
        {
            mouseEvent.eventType = (event->type() == QEvent::MouseButtonPress) ? MouseEvent::MousePressed : MouseEvent::MouseReleased;
        } else
        {
            mouseEvent.eventType = MouseEvent::MouseDoubleClicked;
        }
        mouseEvent.button = (MouseEvent::MouseButton)e->button();
        mouseEvent.x = mousePos.x();
        mouseEvent.y = mousePos.y();
        mouseEvent.z = 0;
        mouseEvent.relativeX = mouseEvent.x - lastMouseX;
        mouseEvent.relativeY = mouseEvent.y - lastMouseY;
        mouseEvent.modifiers = current_modifiers_;

        lastMouseX = mouseEvent.x;
        lastMouseY = mouseEvent.y;

        mouseEvent.globalX = e->globalX();
        mouseEvent.globalY = e->globalY();

        mouseEvent.otherButtons = e->buttons();

//		mouseEvent.heldKeys = heldKeys; ///\todo
        mouseEvent.handled = false;

        // The mouse press is going to the inworld scene - clear keyboard focus from the QGraphicsScene widget, if any had it so key events also go to inworld scene.
        if (event->type() == QEvent::MouseButtonPress && !mouseEvent.itemUnderMouse && mouseCursorVisible)
            mainView->scene()->clearFocus();

        TriggerMouseEvent(mouseEvent);

        return mouseEvent.handled;
    }

    case QEvent::MouseMove:
    {
        // If a mouse button is held down, we get the mouse drag events from the viewport widget.
        // If a mouse button is not held down, the application main window will get the events.
        // Duplicate events are not received, so no need to worry about filtering them here.

        QMouseEvent *e = static_cast<QMouseEvent *>(event);

        //QGraphicsItem *itemUnderMouse = GetVisibleItemAtCoords(e->x(), e->y());
        // If there is a graphicsItem under the mouse, don't pass the move message to the inworld scene, unless the inworld scene has captured it.
//        if (mouseCursorVisible)
//		    if ((itemUnderMouse && sceneMouseCapture != SceneMouseCapture) || sceneMouseCapture == QtMouseCapture)
//			    return false;

//        if (mouseCursorVisible && itemUnderMouse)
//            return false;

        MouseEvent mouseEvent;
        mouseEvent.eventType = MouseEvent::MouseMove;
        mouseEvent.button = (MouseEvent::MouseButton)e->button();
        mouseEvent.itemUnderMouse = GetVisibleItemAtCoords(e->x(), e->y());
        ///\todo Set whether the previous press originated over a Qt widget or scene.
        mouseEvent.origin = mouseEvent.itemUnderMouse ? MouseEvent::PressOriginQtWidget : MouseEvent::PressOriginScene;

        QWidget *sender = qobject_cast<QWidget*>(obj);
        assert(sender);

        // The mouse coordinates we receive can come from different widgets, and we are interested only in the coordinates
        // in the QGraphicsView client area, so we need to remap them.
        QPoint mousePos = MapPointToMainGraphicsView(obj, e->pos());

        if (mouseCursorVisible)
        {
            mouseEvent.x = mousePos.x();
            mouseEvent.y = mousePos.y();
        }
        else
        {
            // If mouse cursor is hidden, we're in relative "crosshair" mode. In this mode,
            // the mouse absolute coordinates are restricted to stay in the center of the screen.
            mouseEvent.x = mainView->size().width()/2;
            mouseEvent.y = mainView->size().height()/2;
        }
        mouseEvent.z = 0;
        mouseEvent.relativeX = mousePos.x() - lastMouseX;
        mouseEvent.relativeY = mousePos.y() - lastMouseY;

        // If there wasn't any change to the mouse relative coords in FPS mode, ignore this event.
        if (!mouseCursorVisible && mouseEvent.relativeX == 0 && mouseEvent.relativeY == 0)
            return true;

        mouseEvent.globalX = e->globalX(); // Note that these may "jitter" when mouse is in relative movement mode.
        mouseEvent.globalY = e->globalY();
        mouseEvent.otherButtons = e->buttons();
//		mouseEvent.heldKeys = heldKeys; ///\todo
        mouseEvent.handled = false;

        // Save the absolute coordinates to be able to compute the proper relative movement values in the next
        // mouse event.
        lastMouseX = mouseEvent.x;
        lastMouseY = mouseEvent.y;

        TriggerMouseEvent(mouseEvent);

        // In relative mouse movement mode, keep the mouse cursor hidden at screen center at all times.
        if (!mouseCursorVisible)
        {
            RecenterMouse();
            return true; // In relative mouse movement mode, the QGraphicsScene does not receive mouse movement at all.
        }

        return mouseEvent.handled;
    }

    case QEvent::Wheel:
    {
        // If this event did not originate from the application main window, we are not interested in it.
        if (obj != qobject_cast<QObject*>(mainWindow))
            return false;

        QObject *mv = qobject_cast<QObject*>(mainView);
        QObject *mw = qobject_cast<QObject*>(mainWindow);
        QWheelEvent *e = static_cast<QWheelEvent *>(event);
//		QGraphicsItem *itemUnderMouse = GetVisibleItemAtCoords(e->x(), e->y());
//		if (itemUnderMouse)
//			return false;

        MouseEvent mouseEvent;
        mouseEvent.eventType = MouseEvent::MouseScroll;
        mouseEvent.itemUnderMouse = GetVisibleItemAtCoords(e->x(), e->y());
        mouseEvent.origin = mouseEvent.itemUnderMouse ? MouseEvent::PressOriginQtWidget : MouseEvent::PressOriginScene;
        mouseEvent.button = MouseEvent::NoButton;
        mouseEvent.otherButtons = e->buttons();
        mouseEvent.x = e->x();
        mouseEvent.y = e->y();
        mouseEvent.z = 0; // Mouse wheel does not have an absolute z position, only relative.
        mouseEvent.relativeX = 0;
        mouseEvent.relativeY = 0;
        mouseEvent.relativeZ = e->delta();
        mouseEvent.globalX = e->globalX();
        mouseEvent.globalY = e->globalY();

        mouseEvent.otherButtons = e->buttons(); ///\todo Can this be trusted?

//		mouseEvent.heldKeys = heldKeys; ///\todo
        mouseEvent.handled = false;

        TriggerMouseEvent(mouseEvent);

        return mouseEvent.handled;
    }

    } // ~switch

    return QObject::eventFilter(obj, event);
}