فهرست منبع

For gameplay:
Adds CONNECTED_EVENT and DISCONNECTED_EVENT to the Gamepad::GamepadEvent enum.
Adds Form* getForm() const to Gamepad.
Adds unsigned int Game::getGamepadCount() const to Game.
Makes controlEvent() method on Gamepad protected.
Removes gamepad data about the button index from Button.
Removes gamepad data about the joystick index from Joystick.
Adds a data binding in Control for specifying the control type (JOYSTICK OR BUTTON) and button/joystick index for each control in a Gamepad.

For sample03-character:
Removes unwanted style none from gamepad.theme.
Not required to specified a style "none" in gamepad.form.

Kieran Cunney 13 سال پیش
والد
کامیت
9e5d7743b9

+ 0 - 3
gameplay/src/Button.cpp

@@ -5,14 +5,11 @@ namespace gameplay
 {
 
 Button::Button()
-    : _gamepadButtonIndex(NULL)
 {
 }
 
 Button::~Button()
 {
-    if (_gamepadButtonIndex)
-        SAFE_DELETE(_gamepadButtonIndex);
 }
 
 Button* Button::create(const char* id, Theme::Style* style)

+ 0 - 1
gameplay/src/Button.h

@@ -94,7 +94,6 @@ private:
      */
     Button(const Button& copy);
 
-    int* _gamepadButtonIndex;
 };
 
 }

+ 6 - 1
gameplay/src/Control.cpp

@@ -7,7 +7,7 @@ namespace gameplay
 
 Control::Control()
     : _id(""), _state(Control::NORMAL), _bounds(Rectangle::empty()), _clipBounds(Rectangle::empty()), _viewportClipBounds(Rectangle::empty()),
-    _clearBounds(Rectangle::empty()), _dirty(true), _consumeInputEvents(true), _listeners(NULL), _contactIndex(INVALID_CONTACT_INDEX),
+    _clearBounds(Rectangle::empty()), _dirty(true), _consumeInputEvents(true), _listeners(NULL), _contactIndex(INVALID_CONTACT_INDEX), _data(NULL),
     _styleOverridden(false), _skin(NULL)
 {
 }
@@ -32,6 +32,11 @@ Control::~Control()
     {
         SAFE_DELETE(_style);
     }
+
+    if (_data)
+    {
+        SAFE_DELETE(_data);
+    }
 }
 
 void Control::initialize(Theme::Style* style, Properties* properties)

+ 5 - 0
gameplay/src/Control.h

@@ -999,6 +999,11 @@ protected:
      */
     int _focusIndex;
 
+    /**
+     * Internal pointer to some data binding.
+     */
+    void* _data;
+
 private:
 
     /*

+ 10 - 3
gameplay/src/Game.h

@@ -272,8 +272,8 @@ public:
     /**
      * Gamepad callback on gamepad events.
      *
-     * @param evt The gamepad event that occured.
-     * @param gamepad the gamepad the event occured on
+     * @param evt The gamepad event that occurred.
+     * @param gamepad the gamepad the event occurred on
      * @param index The joystick or button index that triggered the event.
      */
     virtual void gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int index);
@@ -323,7 +323,14 @@ public:
      *
      * @param playerIndex The player index to get the gamepad for (0 <= playerIndex <= 3) 
      */
-    inline Gamepad* getGamepad(unsigned int playerIndex = 0);
+    inline Gamepad* getGamepad(unsigned int playerIndex = 0) const;
+
+    /**
+     * Gets the number of gamepad's connected to the game.
+     * 
+     * @return the number of gamepad's connected to the game.
+     */
+    inline unsigned int getGamepadCount() const;
 
 protected:
 

+ 6 - 1
gameplay/src/Game.inl

@@ -72,7 +72,7 @@ inline void Game::displayKeyboard(bool display)
     Platform::displayKeyboard(display);
 }
 
-inline Gamepad* Game::getGamepad(unsigned int playerIndex)
+inline Gamepad* Game::getGamepad(unsigned int playerIndex) const
 {
     GP_ASSERT(playerIndex < _gamepadCount);
 
@@ -82,4 +82,9 @@ inline Gamepad* Game::getGamepad(unsigned int playerIndex)
         return NULL;
 }
 
+inline unsigned int Game::getGamepadCount() const
+{
+    return _gamepadCount;
+}
+
 }

+ 38 - 38
gameplay/src/Gamepad.cpp

@@ -51,32 +51,22 @@ void Gamepad::getGamepadControls(Form* form)
         Control* control = *itr;
         GP_ASSERT(control);
 
-        if (std::strcmp("container", control->getType()) == 0)
+        if (control->isContainer())
         {
             getGamepadControls((Form*) control);
         }
         else if (std::strcmp("joystick", control->getType()) == 0)
         {
             control->addListener(this, Control::Listener::PRESS | Control::Listener::VALUE_CHANGED | Control::Listener::RELEASE);
-            Joystick* joystick = (Joystick*) control;
-            
-            if (!joystick->_gamepadJoystickIndex)
-                joystick->_gamepadJoystickIndex = new int[1];
-            
-            *joystick->_gamepadJoystickIndex = _joystickCount;
+            control->_data = (void*) new GamepadData(GamepadData::JOYSTICK, _joystickCount);
             _joystickCount++;
         }
         else if (std::strcmp("button", control->getType()) == 0)
         {
             control->addListener(this, Control::Listener::PRESS | Control::Listener::RELEASE);
-            Button* button = (Button*) control;
-            
-            if (!button->_gamepadButtonIndex)
-                button->_gamepadButtonIndex = new int[1];
-
-            *button->_gamepadButtonIndex = _buttonCount;
+            control->_data = (void*) new GamepadData(GamepadData::BUTTON, _buttonCount);
             _buttonCount++;
-        }
+        }   
     }
 }
 
@@ -118,36 +108,41 @@ void Gamepad::render()
 
 void Gamepad::controlEvent(Control* control, Control::Listener::EventType evt)
 {
-    if (_gamepadForm && _gamepadForm->isEnabled())
+    if (_gamepadForm && _gamepadForm->isEnabled() && control->_data)
     {
-        if (std::strcmp("joystick", control->getType()) == 0)
+        switch(((GamepadData*)control->_data)->_controlType)
         {
-            int joystickIndex = *(((Joystick*) control)->_gamepadJoystickIndex);
-            switch(evt)
+            case GamepadData::JOYSTICK:
             {
-                case Control::Listener::PRESS:
-                case Control::Listener::VALUE_CHANGED:
-                    _joystickValues[joystickIndex]->set(((Joystick*)control)->getValue());
-                    break;
-                case Control::Listener::RELEASE:
-                    _joystickValues[joystickIndex]->set(0.0f, 0.0f);
-                    break;
+                int joystickIndex = ((GamepadData*)control->_data)->_controlIndex;
+                switch (evt)
+                {
+                    case Control::Listener::PRESS:
+                    case Control::Listener::VALUE_CHANGED:
+                        _joystickValues[joystickIndex]->set(((Joystick*)control)->getValue());
+                        break;
+                    case Control::Listener::RELEASE:
+                        _joystickValues[joystickIndex]->set(0.0f, 0.0f);
+                        break;
+                }
+                Game::getInstance()->gamepadEvent(JOYSTICK_EVENT, this, joystickIndex);
             }
-            Game::getInstance()->gamepadEvent(JOYSTICK_EVENT, this, joystickIndex);
-        }
-        else if (std::strcmp("button", control->getType()) == 0)
-        {
-            int buttonIndex = *(((Button*) control)->_gamepadButtonIndex);
-            switch(evt)
+            break;
+            case GamepadData::BUTTON:
             {
-                case Control::Listener::PRESS:
-                    *_buttonStates[buttonIndex] = BUTTON_PRESSED;
-                    break;
-                case Control::Listener::RELEASE:
-                    *_buttonStates[buttonIndex] = BUTTON_RELEASED;
-                    break;
+                int buttonIndex = ((GamepadData*)control->_data)->_controlIndex;
+                switch(evt)
+                {
+                    case Control::Listener::PRESS:
+                        *_buttonStates[buttonIndex] = BUTTON_PRESSED;
+                        break;
+                    case Control::Listener::RELEASE:
+                        *_buttonStates[buttonIndex] = BUTTON_RELEASED;
+                        break;
+                }
+                Game::getInstance()->gamepadEvent(BUTTON_EVENT, this, buttonIndex);
             }
-            Game::getInstance()->gamepadEvent(BUTTON_EVENT, this, buttonIndex);
+            break;
         }
     }
 }
@@ -178,4 +173,9 @@ bool Gamepad::isVirtual() const
     return (_gamepadForm && _gamepadForm->isEnabled());
 }
 
+Form* Gamepad::getForm() const
+{
+    return _gamepadForm;
+}
+
 }

+ 47 - 9
gameplay/src/Gamepad.h

@@ -8,20 +8,29 @@ namespace gameplay
 {
 
 /**
- * Defines an interface for handling gamepad support.
+ * Defines an interface for handling gamepad input.
  */
 class Gamepad : public Control::Listener
 {
     friend class Game;
+    friend class Control;
 
 public:
 
+    /**
+     *  Gamepad events.
+     */
     enum GamepadEvent
     {
+        CONNECTED_EVENT,
+        DISCONNECTED_EVENT,
         JOYSTICK_EVENT,
         BUTTON_EVENT
     };
 
+    /**
+     * Gamepad button states.
+     */
     enum ButtonState
     {
         BUTTON_PRESSED = gameplay::Button::Listener::PRESS, 
@@ -30,13 +39,16 @@ public:
     
     /** 
      * Gets the current state of the specified button.
+     *
+     * @param buttonId The index of the button on the gamepad to get the state for.
+     * @return whether the button is currently pressed or not.
      */
     ButtonState getButtonState(unsigned int buttonId) const;
 
     /**
      * Gets whether the specified joystick's state is active or not.
      * 
-     * @param joystickId The unique integer ID of the joystick to set.
+     * @param joystickId The index of the joystick on the gamepad to get state for.
      * @return Whether the given joystick is active or not.
      */
     bool isJoystickActive(unsigned int joystickId) const;
@@ -49,17 +61,44 @@ public:
      */
     const Vector2& getJoystickValue(unsigned int joystickId) const;
 
-    /** 
-     * Listener for Control events on the gamepads Joystick or Buttons.
+    /**
+     * Returns whether the gamepad is currently represented with a UI form or not.
+     *
+     * @return true if the gamepad is currently represented by a UI form; false if the gamepad is
+     *      not represented by a UI form
      */
-    void controlEvent(Control* control, Control::Listener::EventType evt);
+    bool isVirtual() const;
 
     /**
-     * Returns whether the gamepad is represented with a UI form or not.
+     * Gets the Form used to represent this gamepad.
+     *
+     * Note: What if the user decides to add gamepad controls (joysticks, buttons) to the gamepad form? How do we handle new/deleted controls?
+     *
+     * @return the Form used to represent this gamepad. NULL if the gamepad is not reprented with a Form.
      */
-    bool isVirtual() const;
+    Form* getForm() const;
+
+protected:
+
+    /**
+     * @see Control::Listener::controlEvent
+     */
+    void controlEvent(Control* control, Control::Listener::EventType evt);
 
 private:
+    
+    struct GamepadData
+    {
+        enum {JOYSTICK, BUTTON};
+
+        GamepadData(unsigned int controlType, unsigned int controlIndex)
+            : _controlType(controlType), _controlIndex(controlIndex) {}
+
+        ~GamepadData();
+
+        unsigned int _controlType;
+        unsigned int _controlIndex;
+    };
 
     /**
      * Constructor.
@@ -98,13 +137,12 @@ private:
      * Renders the gamepad if it is based on a form and if the form is enabled.
      */
     void render();
-
+    
     int _playerIndex;
     Vector2** _joystickValues;
     unsigned int _joystickCount;
     ButtonState** _buttonStates;
     unsigned int _buttonCount;
-
     Form* _gamepadForm;
 };
 

+ 1 - 3
gameplay/src/Joystick.cpp

@@ -4,7 +4,7 @@
 namespace gameplay
 {
 
-Joystick::Joystick() : _absolute(true), _gamepadJoystickIndex(NULL)
+Joystick::Joystick() : _absolute(true)
 {
 }
 
@@ -14,8 +14,6 @@ Joystick::Joystick(const Joystick& copy)
 
 Joystick::~Joystick()
 {
-    if (_gamepadJoystickIndex)
-        SAFE_DELETE(_gamepadJoystickIndex);
 }
 
 Joystick* Joystick::create(const char* id, Theme::Style* style)

+ 0 - 1
gameplay/src/Joystick.h

@@ -155,7 +155,6 @@ private:
     Vector2 _displacement;
     Vector2 _value;
     Rectangle _region;
-    int* _gamepadJoystickIndex;
 };
 
 }