| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- #include "Base.h"
- #include "Gamepad.h"
- #include "Game.h"
- #include "GamepadButton.h"
- namespace gameplay
- {
- static std::vector<Gamepad*> __gamepads;
- unsigned int Gamepad::getIndexFromMapping(Gamepad::ButtonMapping mapping)
- {
- // Determine which bit is set in the mapping.
- unsigned int index = 0;
- bool done = false;
- do
- {
- if (mapping == (1 << index))
- done = true;
- else
- ++index;
- } while (!done);
- GP_ASSERT(index < 20);
- return index;
- }
- Gamepad::Gamepad(const char* formPath)
- : _id(""), _handle(0), _vendorId(0), _productId(0), _buttonCount(0), _joystickCount(0), _triggerCount(0), _form(NULL)
- {
- GP_ASSERT(formPath);
- _form = Form::create(formPath);
- GP_ASSERT(_form);
- _form->setConsumeInputEvents(false);
- _id = _form->getId();
- _vendorString = "GamePlay";
- _productString = "Virtual Gamepad";
- bindGamepadControls(_form);
- }
- Gamepad::Gamepad(const char* id, GamepadHandle handle, unsigned int buttonCount, unsigned int joystickCount, unsigned int triggerCount,
- unsigned int vendorId, unsigned int productId, char* vendorString, char* productString)
- : _id(id), _handle(handle), _vendorId(vendorId), _productId(productId), _vendorString(vendorString), _productString(productString),
- _buttonCount(buttonCount), _joystickCount(joystickCount), _triggerCount(triggerCount), _form(NULL)
- {
- }
- Gamepad* Gamepad::add(const char* id, GamepadHandle handle, unsigned int buttonCount, unsigned int joystickCount, unsigned int triggerCount,
- unsigned int vendorId, unsigned int productId, char* vendorString, char* productString)
- {
- Gamepad* gamepad = new Gamepad(id, handle, buttonCount, joystickCount, triggerCount, vendorId, productId, vendorString, productString);
- __gamepads.push_back(gamepad);
- Game::getInstance()->gamepadEvent(CONNECTED_EVENT, gamepad);
- return gamepad;
- }
- Gamepad* Gamepad::add(const char* formPath)
- {
- Gamepad* gamepad = new Gamepad(formPath);
- __gamepads.push_back(gamepad);
- Game::getInstance()->gamepadEvent(CONNECTED_EVENT, gamepad);
- return gamepad;
- }
- void Gamepad::remove(GamepadHandle handle)
- {
- std::vector<Gamepad*>::iterator it = __gamepads.begin();
- do
- {
- Gamepad* gamepad = *it;
- if (gamepad->_handle == handle)
- {
- Game::getInstance()->gamepadEvent(DISCONNECTED_EVENT, gamepad);
- it = __gamepads.erase(it);
- }
- else
- {
- it++;
- }
- } while (it != __gamepads.end());
- }
- void Gamepad::remove(Gamepad* gamepad)
- {
- std::vector<Gamepad*>::iterator it = __gamepads.begin();
- do
- {
- Gamepad* g = *it;
- if (g == gamepad)
- {
- Game::getInstance()->gamepadEvent(DISCONNECTED_EVENT, g);
- it = __gamepads.erase(it);
- }
- else
- {
- it++;
- }
- } while (it != __gamepads.end());
- }
- void Gamepad::bindGamepadControls(Container* container)
- {
- std::vector<Control*> controls = container->getControls();
- std::vector<Control*>::iterator itr = controls.begin();
- for (; itr != controls.end(); itr++)
- {
- Control* control = *itr;
- GP_ASSERT(control);
- if (control->isContainer())
- {
- bindGamepadControls((Container*) control);
- }
- else if (std::strcmp("joystick", control->getType()) == 0)
- {
- Joystick* joystick = (Joystick*)control;
- _uiJoysticks[joystick->getIndex()] = joystick;
- _joystickCount++;
- }
- else if (std::strcmp("gamepadButton", control->getType()) == 0)
- {
- GamepadButton* button = (GamepadButton*)control;
- unsigned int index = getIndexFromMapping(button->getMapping());
- _uiButtons[index] = button;
- _buttonCount++;
- }
- }
- }
- Gamepad::~Gamepad()
- {
- if (_form)
- {
- SAFE_RELEASE(_form);
- }
- }
- Gamepad* Gamepad::getGamepad(GamepadHandle handle)
- {
- std::vector<Gamepad*>::const_iterator it;
- for (it = __gamepads.begin(); it != __gamepads.end(); it++)
- {
- Gamepad* gamepad = *it;
- if (!gamepad->isVirtual() && gamepad->_handle == handle)
- {
- return gamepad;
- }
- }
- return NULL;
- }
- std::vector<Gamepad*>* Gamepad::getGamepads()
- {
- return &__gamepads;
- }
- Gamepad::ButtonMapping Gamepad::getButtonMappingFromString(const char* string)
- {
- if (strcmp(string, "A") == 0 || strcmp(string, "BUTTON_A") == 0)
- return BUTTON_A;
- else if (strcmp(string, "B") == 0 || strcmp(string, "BUTTON_B") == 0)
- return BUTTON_B;
- else if (strcmp(string, "C") == 0 || strcmp(string, "BUTTON_C") == 0)
- return BUTTON_C;
- else if (strcmp(string, "X") == 0 || strcmp(string, "BUTTON_X") == 0)
- return BUTTON_X;
- else if (strcmp(string, "Y") == 0 || strcmp(string, "BUTTON_Y") == 0)
- return BUTTON_Y;
- else if (strcmp(string, "Z") == 0 || strcmp(string, "BUTTON_Z") == 0)
- return BUTTON_Z;
- else if (strcmp(string, "MENU1") == 0 || strcmp(string, "BUTTON_MENU1") == 0)
- return BUTTON_MENU1;
- else if (strcmp(string, "MENU2") == 0 || strcmp(string, "BUTTON_MENU2") == 0)
- return BUTTON_MENU2;
- else if (strcmp(string, "MENU3") == 0 || strcmp(string, "BUTTON_MENU3") == 0)
- return BUTTON_MENU3;
- else if (strcmp(string, "MENU4") == 0 || strcmp(string, "BUTTON_MENU4") == 0)
- return BUTTON_MENU4;
- else if (strcmp(string, "L1") == 0 || strcmp(string, "BUTTON_L1") == 0)
- return BUTTON_L1;
- else if (strcmp(string, "L2") == 0 || strcmp(string, "BUTTON_L2") == 0)
- return BUTTON_L2;
- else if (strcmp(string, "L3") == 0 || strcmp(string, "BUTTON_L3") == 0)
- return BUTTON_L3;
- else if (strcmp(string, "R1") == 0 || strcmp(string, "BUTTON_R1") == 0)
- return BUTTON_R1;
- else if (strcmp(string, "R2") == 0 || strcmp(string, "BUTTON_R2") == 0)
- return BUTTON_R2;
- else if (strcmp(string, "R3") == 0 || strcmp(string, "BUTTON_R3") == 0)
- return BUTTON_R3;
- else if (strcmp(string, "UP") == 0 || strcmp(string, "BUTTON_UP") == 0)
- return BUTTON_UP;
- else if (strcmp(string, "DOWN") == 0 || strcmp(string, "BUTTON_DOWN") == 0)
- return BUTTON_DOWN;
- else if (strcmp(string, "LEFT") == 0 || strcmp(string, "BUTTON_LEFT") == 0)
- return BUTTON_LEFT;
- else if (strcmp(string, "RIGHT") == 0 || strcmp(string, "BUTTON_RIGHT") == 0)
- return BUTTON_RIGHT;
- GP_WARN("Unknown GamepadButton string.");
- return BUTTON_A;
- }
- const char* Gamepad::getId() const
- {
- return _id.c_str();
- }
- const unsigned int Gamepad::getVendorId() const
- {
- return _vendorId;
- }
- const unsigned int Gamepad::getProductId() const
- {
- return _productId;
- }
- const char* Gamepad::getVendorString() const
- {
- return _vendorString.c_str();
- }
- const char* Gamepad::getProductString() const
- {
- return _productString.c_str();
- }
- void Gamepad::update(float elapsedTime)
- {
- if (_form && _form->isEnabled())
- {
- _form->update(elapsedTime);
- }
- else
- {
- Platform::pollGamepadState(this);
- }
- }
- void Gamepad::draw()
- {
- if (_form && _form->isEnabled())
- {
- _form->draw();
- }
- }
- unsigned int Gamepad::getButtonCount() const
- {
- return _buttonCount;
- }
- bool Gamepad::isButtonDown(ButtonMapping mapping) const
- {
- if (_form)
- {
- unsigned int index = getIndexFromMapping(mapping);
- if (index < _buttonCount)
- {
- GamepadButton* button = _uiButtons[index];
- if (button)
- {
- return (button->getState() == Control::ACTIVE);
- }
- }
- else
- {
- return false;
- }
- }
- else if (_buttons & mapping)
- {
- return true;
- }
- return false;
- }
- unsigned int Gamepad::getJoystickCount() const
- {
- return _joystickCount;
- }
- void Gamepad::getJoystickValues(unsigned int joystickId, Vector2* outValue) const
- {
- GP_ASSERT(joystickId < _joystickCount);
- if (_form)
- {
- Joystick* joystick = _uiJoysticks[joystickId];
- if (joystick)
- {
- const Vector2& value = joystick->getValue();
- outValue->set(value.x, value.y);
- }
- else
- {
- outValue->set(0.0f, 0.0f);
- }
- }
- else
- {
- outValue->set(_joysticks[joystickId]);
- }
- }
- unsigned int Gamepad::getTriggerCount() const
- {
- return _triggerCount;
- }
- float Gamepad::getTriggerValue(unsigned int triggerId) const
- {
- GP_ASSERT(triggerId < _triggerCount);
- if (_form)
- {
- // Triggers are currently not available for virtual gamepads.
- return 0.0f;
- }
- else
- {
- return _triggers[triggerId];
- }
- }
- bool Gamepad::isVirtual() const
- {
- return _form;
- }
- Form* Gamepad::getForm() const
- {
- return _form;
- }
- }
|