Esempio n. 1
0
 static void onAxisEvent(const std::string& vendorName, int controllerID, AndroidControllerCode axisCode, float value, bool isAnalog)
 {
     // log("vendorName: %s, controller id: %d, axis: %d, value: %f", vendorName.c_str(), controllerID, axisCode, value);
     auto iter = findController(vendorName, controllerID);
     if (iter == Controller::_controllers.end())
     {
         onConnected(vendorName, controllerID);
         iter = findController(vendorName, controllerID);
     }
     auto gamepad = (*iter)->getGamepad();
     auto thiz = (*iter)->getImpl();
     switch (axisCode)
     {
         case AndroidControllerCode::THUMBSTICK_LEFT_X:
             thiz->sendEventAxis(gamepad->getLeftThumbstick()->getAxisX(), value, isAnalog);
             break;
         case AndroidControllerCode::THUMBSTICK_LEFT_Y:
             thiz->sendEventAxis(gamepad->getLeftThumbstick()->getAxisY(), value, isAnalog);
             break;
         case AndroidControllerCode::THUMBSTICK_RIGHT_X:
             thiz->sendEventAxis(gamepad->getRightThumbstick()->getAxisX(), value, isAnalog);
             break;
         case AndroidControllerCode::THUMBSTICK_RIGHT_Y:
             thiz->sendEventAxis(gamepad->getRightThumbstick()->getAxisY(), value, isAnalog);
             break;
         default:
             CCASSERT(false, "Invalid controller axis code!");
             break;
     }
 }
    static void onAxisEvent(const std::string& deviceName, int deviceId, int axisCode, float value, bool isAnalog)
    {
        auto iter = findController(deviceName, deviceId);
        if (iter == Controller::s_allController.end())
        {
            onConnected(deviceName, deviceId);
            iter = findController(deviceName, deviceId);
        }

        (*iter)->onAxisEvent(axisCode, value, isAnalog);
    }
    static void onButtonEvent(const std::string& deviceName, int deviceId, int keyCode, bool isPressed, float value, bool isAnalog)
    {
        auto iter = findController(deviceName, deviceId);
        if (iter == Controller::s_allController.end())
        {
            CCLOG("onButtonEvent:connect new controller.");
            onConnected(deviceName, deviceId);
            iter = findController(deviceName, deviceId);
        }

        (*iter)->onButtonEvent(keyCode, isPressed, value, isAnalog);
    }
    static void onDisconnected(const std::string& deviceName, int deviceId)
    {
        log("onDisconnected %s,%d", deviceName.c_str(),deviceId);
        auto iter = findController(deviceName, deviceId);
        if (iter == Controller::s_allController.end())
        {
            log("Could not find the controller!");
            CCLOGERROR("Could not find the controller!");
            return;
        }

        (*iter)->onDisconnected();
        Controller::s_allController.erase(iter);
    }
Esempio n. 5
0
    static void onDisconnected(const std::string& vendorName, int controllerID)
    {
        auto iter = findController(vendorName, controllerID);
        if (iter == Controller::_controllers.end())
        {
            CCLOGERROR("Could not find the controller!");
            return;
        }

        EventController evt(EventController::ControllerEventType::CONNECTION, *iter, false);
        Director::getInstance()->getEventDispatcher()->dispatchEvent(&evt);

        Controller::_controllers.erase(iter);
    }
Esempio n. 6
0
    static void onConnected(const std::string& vendorName, int controllerID)
    {
        // Check whether the controller is already connected.
        auto iter = findController(vendorName, controllerID);
        if (iter != Controller::_controllers.end())
            return;

        // It's a new controller being connected.
        auto controller = new cocos2d::Controller();
        controller->_vendorName = vendorName;
        Controller::_controllers.push_back(controller);
        controller->_impl->_controllerID = controllerID;
        EventController evt(EventController::ControllerEventType::CONNECTION, controller, true);
        Director::getInstance()->getEventDispatcher()->dispatchEvent(&evt);
    }
    static void onConnected(const std::string& deviceName, int deviceId)
    {
        // Check whether the controller is already connected.
        CCLOG("onConnected %s,%d", deviceName.c_str(),deviceId);

        auto iter = findController(deviceName, deviceId);
        if (iter != Controller::s_allController.end())
            return;

        // It's a new controller being connected.
        auto controller = new cocos2d::Controller();
        controller->_deviceId = deviceId;
        controller->_deviceName = deviceName;
        Controller::s_allController.push_back(controller);

        controller->onConnected();
    }
Esempio n. 8
0
    static void onButtonEvent(const std::string& vendorName, int controllerID, AndroidControllerCode btnCode, bool isPressed, float value, bool isAnalog)
    {
        auto iter = findController(vendorName, controllerID);
        if (iter == Controller::_controllers.end())
        {
            onConnected(vendorName, controllerID);
            iter = findController(vendorName, controllerID);
        }

        auto gamepad = (*iter)->getGamepad();
        auto thiz = (*iter)->getImpl();
        switch(btnCode)
        {
            case AndroidControllerCode::BUTTON_A:
                {
                    thiz->sendEventButton(gamepad->getButtonA(), isPressed, value, isAnalog);
                }
                break;
            case AndroidControllerCode::BUTTON_B:
                {
                    thiz->sendEventButton(gamepad->getButtonB(), isPressed, value, isAnalog);
                }
                break;
            case AndroidControllerCode::BUTTON_X:
                {
                    thiz->sendEventButton(gamepad->getButtonX(), isPressed, value, isAnalog);
                }
                break;
            case AndroidControllerCode::BUTTON_Y:
                {
                    thiz->sendEventButton(gamepad->getButtonY(), isPressed, value, isAnalog);
                }
                break;
            case AndroidControllerCode::BUTTON_LEFT_SHOULDER:
                {
                    thiz->sendEventButton(gamepad->getLeftShoulder(), isPressed, value, isAnalog);
                }
                break;
            case AndroidControllerCode::BUTTON_RIGHT_SHOULDER:
                {
                    thiz->sendEventButton(gamepad->getRightShoulder(), isPressed, value, isAnalog);
                }
                break;
            case AndroidControllerCode::BUTTON_LEFT_TRIGGER:
                {
                    thiz->sendEventButton(gamepad->getLeftTrigger(), isPressed, value, isAnalog);
                }
                break;
            case AndroidControllerCode::BUTTON_RIGHT_TRIGGER:
                {
                    thiz->sendEventButton(gamepad->getRightTrigger(), isPressed, value, isAnalog);
                }
                break;
            case AndroidControllerCode::BUTTON_DPAD_UP:
                {
                    thiz->sendEventButton(gamepad->getDirectionPad()->getUp(), isPressed, value, isAnalog);
                }
                break;
            case AndroidControllerCode::BUTTON_DPAD_DOWN:
                {
                    thiz->sendEventButton(gamepad->getDirectionPad()->getDown(), isPressed, value, isAnalog);
                }
                break;
            case AndroidControllerCode::BUTTON_DPAD_LEFT:
                {
                    thiz->sendEventButton(gamepad->getDirectionPad()->getLeft(), isPressed, value, isAnalog);
                }
                break;
            case AndroidControllerCode::BUTTON_DPAD_RIGHT:
                {
                    thiz->sendEventButton(gamepad->getDirectionPad()->getRight(), isPressed, value, isAnalog);
                }
                break;
            case AndroidControllerCode::BUTTON_START:
                {
                    thiz->sendEventButton(gamepad->getButtonStart(), isPressed, value, isAnalog);
                }
                break;
            case AndroidControllerCode::BUTTON_SELECT:
                {
                    thiz->sendEventButton(gamepad->getButtonSelect(), isPressed, value, isAnalog);
                }
                break;
            case AndroidControllerCode::BUTTON_LEFT_THUMBSTICK:
                {
                    thiz->sendEventButton(gamepad->getLeftThumbstick()->getButton(), isPressed, value, isAnalog);
                }
                break;
            case AndroidControllerCode::BUTTON_RIGHT_THUMBSTICK:
                {
                    thiz->sendEventButton(gamepad->getRightThumbstick()->getButton(), isPressed, value, isAnalog);
                }
                break;
            default:
                //                CCASSERT(false, "Invalid controller button code!");
                break;
        }
    }