Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adds Gamepad class to gameplay.

Stores Gamepad objects in Game.
Adds Gamepad* createGamepad(const char* gamepadFormPath) to Game.
Adds Gamepad* getGamepad(unsigned int playerIndex=0) to Game.
Adds void gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int index) event callback to Game.
Adds const char* getType() on Control and has each child of Control implement the function, returning the name of the class as a string in camel-casing.
Adds concept of contact index at the Control level. This allows for more than one Control to be active/in focus at once. Had to refactor some Control's touchEvents to handle this. Setting the contactIndex on a TOUCH_PRESS event, and then resetting it to INVALID_CONTACT_INDEX on TOUCH_RELEASE.
Fixes memory leak in FrameBuffer.
Code cleanup, removes some warnings.
  • Loading branch information...
commit a6c810d4175193a573064d50f19971002026f0f2 1 parent dae121c
@kcunney kcunney authored
Showing with 835 additions and 849 deletions.
  1. +41 −36 gameplay-samples/sample03-character/game.dxt.config
  2. +40 −35 gameplay-samples/sample03-character/game.png.config
  3. +40 −35 gameplay-samples/sample03-character/game.pvrtc.config
  4. +2 −2 gameplay-samples/sample03-character/sample03-character.vcxproj
  5. +6 −6 gameplay-samples/sample03-character/sample03-character.vcxproj.filters
  6. +55 −78 gameplay-samples/sample03-character/src/CharacterGame.cpp
  7. +8 −3 gameplay-samples/sample03-character/src/CharacterGame.h
  8. +0 −342 gameplay-samples/sample03-character/src/Gamepad.cpp
  9. +0 −218 gameplay-samples/sample03-character/src/Gamepad.h
  10. +2 −4 gameplay/gameplay.vcxproj
  11. +2 −8 gameplay/gameplay.vcxproj.filters
  12. +16 −3 gameplay/src/Button.cpp
  13. +9 −1 gameplay/src/Button.h
  14. +5 −0 gameplay/src/CheckBox.cpp
  15. +6 −1 gameplay/src/CheckBox.h
  16. +8 −8 gameplay/src/Container.cpp
  17. +7 −2 gameplay/src/Container.h
  18. +18 −7 gameplay/src/Control.cpp
  19. +19 −1 gameplay/src/Control.h
  20. +6 −0 gameplay/src/Form.cpp
  21. +6 −1 gameplay/src/Form.h
  22. +1 −0  gameplay/src/FrameBuffer.cpp
  23. +76 −2 gameplay/src/Game.cpp
  24. +33 −0 gameplay/src/Game.h
  25. +11 −1 gameplay/src/Game.inl
  26. +181 −0 gameplay/src/Gamepad.cpp
  27. +113 −0 gameplay/src/Gamepad.h
  28. +65 −47 gameplay/src/Joystick.cpp
  29. +7 −1 gameplay/src/Joystick.h
  30. +6 −1 gameplay/src/Label.cpp
  31. +6 −1 gameplay/src/Label.h
  32. +6 −1 gameplay/src/RadioButton.cpp
  33. +6 −1 gameplay/src/RadioButton.h
  34. +5 −0 gameplay/src/Slider.cpp
  35. +5 −0 gameplay/src/Slider.h
  36. +11 −2 gameplay/src/TextBox.cpp
  37. +6 −1 gameplay/src/TextBox.h
  38. +1 −0  gameplay/src/gameplay.h
View
77 gameplay-samples/sample03-character/game.dxt.config
@@ -1,43 +1,48 @@
window
{
- title = Character
- width = 1280
- height = 720
- fullscreen = false
+title = Character
+width = 1280
+height = 720
+fullscreen = false
}
aliases
{
- backboard = res/dxt/backboard.dds
- basketball = res/dxt/basketball.dds
- basketballnet = res/dxt/basketballnet.dds
- book = res/dxt/book.dds
- bookshelf = res/dxt/bookshelf.dds
- character = res/dxt/character.dds
- decals = res/dxt/decals.dds
- door = res/dxt/door.dds
- doorframe = res/dxt/doorframe.dds
- easel = res/dxt/easel.dds
- floor = res/dxt/floor.dds
- floortiles = res/dxt/floortiles.dds
- gamepad = res/dxt/gamepad.dds
- playtable = res/dxt/playtable.dds
- shadow = res/dxt/shadow.dds
- storageorganizer = res/dxt/storageorganizer.dds
- tableleg1 = res/dxt/tableleg1.dds
- tableleg2 = res/dxt/tableleg2.dds
- tableleg3 = res/dxt/tableleg3.dds
- tableleg4 = res/dxt/tableleg4.dds
- tabletop = res/dxt/tabletop.dds
- tiles = res/dxt/tiles.dds
- tilesn = res/dxt/tilesn.dds
- toybox = res/dxt/toybox.dds
- walleast = res/dxt/walleast.dds
- wallnorth = res/dxt/wallnorth.dds
- walloverhang = res/dxt/walloverhang.dds
- wallsouth = res/dxt/wallsouth.dds
- wallwest = res/dxt/wallwest.dds
- windowledge = res/dxt/windowledge.dds
- wood = res/dxt/wood.dds
- woodn = res/dxt/woodn.dds
+backboard = res/dxt/backboard.dds
+basketball = res/dxt/basketball.dds
+basketballnet = res/dxt/basketballnet.dds
+book = res/dxt/book.dds
+bookshelf = res/dxt/bookshelf.dds
+character = res/dxt/character.dds
+decals = res/dxt/decals.dds
+door = res/dxt/door.dds
+doorframe = res/dxt/doorframe.dds
+easel = res/dxt/easel.dds
+floor = res/dxt/floor.dds
+floortiles = res/dxt/floortiles.dds
+gamepad = res/dxt/gamepad.dds
+playtable = res/dxt/playtable.dds
+shadow = res/dxt/shadow.dds
+storageorganizer = res/dxt/storageorganizer.dds
+tableleg1 = res/dxt/tableleg1.dds
+tableleg2 = res/dxt/tableleg2.dds
+tableleg3 = res/dxt/tableleg3.dds
+tableleg4 = res/dxt/tableleg4.dds
+tabletop = res/dxt/tabletop.dds
+tiles = res/dxt/tiles.dds
+tilesn = res/dxt/tilesn.dds
+toybox = res/dxt/toybox.dds
+walleast = res/dxt/walleast.dds
+wallnorth = res/dxt/wallnorth.dds
+walloverhang = res/dxt/walloverhang.dds
+wallsouth = res/dxt/wallsouth.dds
+wallwest = res/dxt/wallwest.dds
+windowledge = res/dxt/windowledge.dds
+wood = res/dxt/wood.dds
+woodn = res/dxt/woodn.dds
+}
+
+gamepad
+{
+form = res/common/gamepad.form
}
View
75 gameplay-samples/sample03-character/game.png.config
@@ -1,35 +1,40 @@
-aliases
-{
- backboard = res/png/backboard.png
- basketball = res/png/basketball.png
- basketballnet = res/png/basketballnet.png
- book = res/png/book.png
- bookshelf = res/png/bookshelf.png
- character = res/png/character.png
- decals = res/png/decals.png
- door = res/png/door.png
- doorframe = res/png/doorframe.png
- easel = res/png/easel.png
- floor = res/png/floor.png
- floortiles = res/png/floortiles.png
- gamepad = res/png/gamepad.png
- playtable = res/png/playtable.png
- shadow = res/png/shadow.png
- storageorganizer = res/png/storageorganizer.png
- tableleg1 = res/png/tableleg1.png
- tableleg2 = res/png/tableleg2.png
- tableleg3 = res/png/tableleg3.png
- tableleg4 = res/png/tableleg4.png
- tabletop = res/png/tabletop.png
- tiles = res/png/tiles.png
- tilesn = res/png/tilesn.png
- toybox = res/png/toybox.png
- walleast = res/png/walleast.png
- wallnorth = res/png/wallnorth.png
- walloverhang = res/png/walloverhang.png
- wallsouth = res/png/wallsouth.png
- wallwest = res/png/wallwest.png
- windowledge = res/png/windowledge.png
- wood = res/png/wood.png
- woodn = res/png/woodn.png
-}
+aliases
+{
+backboard = res/png/backboard.png
+basketball = res/png/basketball.png
+basketballnet = res/png/basketballnet.png
+book = res/png/book.png
+bookshelf = res/png/bookshelf.png
+character = res/png/character.png
+decals = res/png/decals.png
+door = res/png/door.png
+doorframe = res/png/doorframe.png
+easel = res/png/easel.png
+floor = res/png/floor.png
+floortiles = res/png/floortiles.png
+gamepad = res/png/gamepad.png
+playtable = res/png/playtable.png
+shadow = res/png/shadow.png
+storageorganizer = res/png/storageorganizer.png
+tableleg1 = res/png/tableleg1.png
+tableleg2 = res/png/tableleg2.png
+tableleg3 = res/png/tableleg3.png
+tableleg4 = res/png/tableleg4.png
+tabletop = res/png/tabletop.png
+tiles = res/png/tiles.png
+tilesn = res/png/tilesn.png
+toybox = res/png/toybox.png
+walleast = res/png/walleast.png
+wallnorth = res/png/wallnorth.png
+walloverhang = res/png/walloverhang.png
+wallsouth = res/png/wallsouth.png
+wallwest = res/png/wallwest.png
+windowledge = res/png/windowledge.png
+wood = res/png/wood.png
+woodn = res/png/woodn.png
+}
+
+gamepad
+{
+form = res/common/gamepad.form
+}
View
75 gameplay-samples/sample03-character/game.pvrtc.config
@@ -1,35 +1,40 @@
-aliases
-{
- backboard = res/pvrtc/backboard.pvr
- basketball = res/pvrtc/basketball.pvr
- basketballnet = res/pvrtc/basketballnet.pvr
- book = res/pvrtc/book.pvr
- bookshelf = res/pvrtc/bookshelf.pvr
- character = res/pvrtc/character.pvr
- decals = res/pvrtc/decals.pvr
- door = res/pvrtc/door.pvr
- doorframe = res/pvrtc/doorframe.pvr
- easel = res/pvrtc/easel.pvr
- floor = res/pvrtc/floor.pvr
- floortiles = res/pvrtc/floortiles.pvr
- gamepad = res/pvrtc/gamepad.pvr
- playtable = res/pvrtc/playtable.pvr
- shadow = res/pvrtc/shadow.pvr
- storageorganizer = res/pvrtc/storageorganizer.pvr
- tableleg1 = res/pvrtc/tableleg1.pvr
- tableleg2 = res/pvrtc/tableleg2.pvr
- tableleg3 = res/pvrtc/tableleg3.pvr
- tableleg4 = res/pvrtc/tableleg4.pvr
- tabletop = res/pvrtc/tabletop.pvr
- tiles = res/pvrtc/tiles.pvr
- tilesn = res/pvrtc/tilesn.pvr
- toybox = res/pvrtc/toybox.pvr
- walleast = res/pvrtc/walleast.pvr
- wallnorth = res/pvrtc/wallnorth.pvr
- walloverhang = res/pvrtc/walloverhang.pvr
- wallsouth = res/pvrtc/wallsouth.pvr
- wallwest = res/pvrtc/wallwest.pvr
- windowledge = res/pvrtc/windowledge.pvr
- wood = res/pvrtc/wood.pvr
- woodn = res/pvrtc/woodn.pvr
-}
+aliases
+{
+backboard = res/pvrtc/backboard.pvr
+basketball = res/pvrtc/basketball.pvr
+basketballnet = res/pvrtc/basketballnet.pvr
+book = res/pvrtc/book.pvr
+bookshelf = res/pvrtc/bookshelf.pvr
+character = res/pvrtc/character.pvr
+decals = res/pvrtc/decals.pvr
+door = res/pvrtc/door.pvr
+doorframe = res/pvrtc/doorframe.pvr
+easel = res/pvrtc/easel.pvr
+floor = res/pvrtc/floor.pvr
+floortiles = res/pvrtc/floortiles.pvr
+gamepad = res/pvrtc/gamepad.pvr
+playtable = res/pvrtc/playtable.pvr
+shadow = res/pvrtc/shadow.pvr
+storageorganizer = res/pvrtc/storageorganizer.pvr
+tableleg1 = res/pvrtc/tableleg1.pvr
+tableleg2 = res/pvrtc/tableleg2.pvr
+tableleg3 = res/pvrtc/tableleg3.pvr
+tableleg4 = res/pvrtc/tableleg4.pvr
+tabletop = res/pvrtc/tabletop.pvr
+tiles = res/pvrtc/tiles.pvr
+tilesn = res/pvrtc/tilesn.pvr
+toybox = res/pvrtc/toybox.pvr
+walleast = res/pvrtc/walleast.pvr
+wallnorth = res/pvrtc/wallnorth.pvr
+walloverhang = res/pvrtc/walloverhang.pvr
+wallsouth = res/pvrtc/wallsouth.pvr
+wallwest = res/pvrtc/wallwest.pvr
+windowledge = res/pvrtc/windowledge.pvr
+wood = res/pvrtc/wood.pvr
+woodn = res/pvrtc/woodn.pvr
+}
+
+gamepad
+{
+form = res/common/gamepad.form
+}
View
4 gameplay-samples/sample03-character/sample03-character.vcxproj
@@ -156,17 +156,17 @@
<None Include="game.dxt.config" />
<None Include="icon.png" />
<None Include="res\common\boy.animation" />
+ <None Include="res\common\gamepad.form" />
+ <None Include="res\common\gamepad.theme" />
<None Include="res\common\scene.material" />
<None Include="res\common\scene.physics" />
<None Include="res\common\scene.scene" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\CharacterGame.cpp" />
- <ClCompile Include="src\Gamepad.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\CharacterGame.h" />
- <ClInclude Include="src\Gamepad.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
View
12 gameplay-samples/sample03-character/sample03-character.vcxproj.filters
@@ -25,21 +25,21 @@
<Filter>res</Filter>
</None>
<None Include="game.dxt.config" />
+ <None Include="res\common\gamepad.form">
+ <Filter>res</Filter>
+ </None>
+ <None Include="res\common\gamepad.theme">
+ <Filter>res</Filter>
+ </None>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\CharacterGame.cpp">
<Filter>src</Filter>
</ClCompile>
- <ClCompile Include="src\Gamepad.cpp">
- <Filter>src</Filter>
- </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\CharacterGame.h">
<Filter>src</Filter>
</ClInclude>
- <ClInclude Include="src\Gamepad.h">
- <Filter>src</Filter>
- </ClInclude>
</ItemGroup>
</Project>
View
133 gameplay-samples/sample03-character/src/CharacterGame.cpp
@@ -19,7 +19,7 @@ CharacterGame game;
CharacterGame::CharacterGame()
: _font(NULL), _scene(NULL), _character(NULL), _characterMeshNode(NULL), _characterShadowNode(NULL),
_animation(NULL), _currentClip(NULL), _rotateX(0), _materialParameterAlpha(NULL),
- _keyFlags(0), _drawDebug(0), _buttonReleased(true)
+ _keyFlags(0), _drawDebug(0), _buttonReleased(true), _gamepad(NULL)
{
}
@@ -99,30 +99,15 @@ void CharacterGame::initializeCharacter()
void CharacterGame::initializeGamepad()
{
- _gamepad = new Gamepad("@gamepad", 1, 1);
-
- float scale = getHeight() / 720.0f;
-
- // Add a single gamepad joystick
- _gamepad->setJoystick(0,
- Rectangle(48.0f * scale, getHeight() - 248.0f * scale, 200.0f * scale, 200.0f * scale),
- Rectangle(256, 0, 256, 256),
- Rectangle(48.0f * scale, getHeight() - 248.0f * scale, 200.0f * scale, 200.0f * scale),
- Rectangle(0, 0, 256, 256),
- 64.0f);
-
- // Add a single gamepad button
- _gamepad->setButton(0,
- Rectangle(getWidth() - scale * (176.0f), getHeight() - 176.0f * scale, 128.0f * scale, 128.0f * scale),
- Rectangle(40, 310, 160, 160),
- Rectangle(300, 310, 160, 160));
+ // Get the gamepad loaded for player1 from game.config.
+ _gamepad = Game::getInstance()->getGamepad(0);
+ GP_ASSERT(_gamepad);
}
void CharacterGame::finalize()
{
SAFE_RELEASE(_scene);
SAFE_RELEASE(_font);
- SAFE_DELETE(_gamepad);
}
void CharacterGame::drawSplash(void* param)
@@ -190,53 +175,38 @@ bool CharacterGame::isOnFloor() const
void CharacterGame::update(float elapsedTime)
{
- Vector2 direction;
-
- if (_gamepad->getButtonState(0) == Gamepad::BUTTON_PRESSED)
- {
- if (_buttonReleased)
- {
- _buttonReleased = false;
- // Jump while the gamepad button is being pressed
- jump();
- }
- }
- else
- {
- _buttonReleased = true;
- }
-
- if (_gamepad->isJoystickActive(0))
- {
- // Get joystick direction
- direction = _gamepad->getJoystickState(0);
- }
- else
+ if (!_gamepad->isJoystickActive(0) && !_gamepad->isJoystickActive(1))
{
// Construct direction vector from keyboard input
if (_keyFlags & NORTH)
- direction.y = 1;
+ _currentDirection.y = 1;
else if (_keyFlags & SOUTH)
- direction.y = -1;
+ _currentDirection.y = -1;
+ else
+ _currentDirection.y = 0;
+
if (_keyFlags & EAST)
- direction.x = 1;
+ _currentDirection.x = 1;
else if (_keyFlags & WEST)
- direction.x = -1;
+ _currentDirection.x = -1;
+ else
+ _currentDirection.x = 0;
- direction.normalize();
+
+ _currentDirection.normalize();
if ((_keyFlags & RUNNING) == 0)
- direction *= 0.5f;
+ _currentDirection *= 0.5f;
}
// Update character animation and velocity
- if (direction.isZero())
+ if (_currentDirection.isZero())
{
play("idle", true);
_character->setVelocity(Vector3::zero());
}
else
{
- bool running = (direction.lengthSquared() > 0.75f);
+ bool running = (_currentDirection.lengthSquared() > 0.75f);
float speed = running ? RUN_SPEED : WALK_SPEED;
play(running ? "running" : "walking", true, 1.0f);
@@ -251,7 +221,7 @@ void CharacterGame::update(float elapsedTime)
Vector3 currentHeading(-_characterMeshNode->getForwardVectorWorld());
// Construct a new forward vector for the mesh node
- Vector3 newHeading(cameraForward * direction.y + cameraRight * direction.x);
+ Vector3 newHeading(cameraForward * _currentDirection.y + cameraRight * _currentDirection.x);
// Compute the rotation amount based on the difference between the current and new vectors
float angle = atan2f(newHeading.x, newHeading.z) - atan2f(currentHeading.x, currentHeading.z);
@@ -302,9 +272,6 @@ void CharacterGame::render(float elapsedTime)
break;
}
- // Draw gamepad for touch devices.
- _gamepad->draw(Vector4(1.0f, 1.0f, 1.0f, 0.7f));
-
// Draw FPS
_font->begin();
char fps[32];
@@ -384,33 +351,28 @@ void CharacterGame::keyEvent(Keyboard::KeyEvent evt, int key)
void CharacterGame::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
{
- // Send the touch event to the gamepad.
- _gamepad->touchEvent(evt, x, y, contactIndex);
-
- if (!_gamepad->isJoystickActive(0) || contactIndex != _gamepad->getJoystickContactIndex(0))
+ // This should only be called if the gamepad did not handle the touch event.
+ switch (evt)
{
- switch (evt)
+ case Touch::TOUCH_PRESS:
{
- case Touch::TOUCH_PRESS:
- {
- _rotateX = x;
- }
- break;
- case Touch::TOUCH_RELEASE:
- {
- _rotateX = 0;
- }
- break;
- case Touch::TOUCH_MOVE:
- {
- int deltaX = x - _rotateX;
- _rotateX = x;
- _character->getNode()->rotateY(-MATH_DEG_TO_RAD(deltaX * 0.5f));
- }
- break;
- default:
- break;
+ _rotateX = x;
+ }
+ break;
+ case Touch::TOUCH_RELEASE:
+ {
+ _rotateX = 0;
+ }
+ break;
+ case Touch::TOUCH_MOVE:
+ {
+ int deltaX = x - _rotateX;
+ _rotateX = x;
+ _character->getNode()->rotateY(-MATH_DEG_TO_RAD(deltaX * 0.5f));
}
+ break;
+ default:
+ break;
}
}
@@ -484,7 +446,22 @@ void CharacterGame::adjustCamera(float elapsedTime)
distance = 0;
}
-void CharacterGame::animationEvent(AnimationClip* clip, EventType type)
+void CharacterGame::animationEvent(AnimationClip* clip, AnimationClip::Listener::EventType type)
{
clip->crossFade(_currentClip, 150);
}
+
+void CharacterGame::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int index)
+{
+ switch(evt)
+ {
+ case Gamepad::BUTTON_EVENT:
+ if (gamepad->getButtonState(index) == Gamepad::BUTTON_PRESSED)
+ jump();
+ break;
+ case Gamepad::JOYSTICK_EVENT:
+ _currentDirection = gamepad->getJoystickValue(index);
+ break;
+ }
+ return;
+}
View
11 gameplay-samples/sample03-character/src/CharacterGame.h
@@ -2,7 +2,6 @@
#define CHARACTERGAME_H_
#include "gameplay.h"
-#include "Gamepad.h"
using namespace gameplay;
/**
@@ -30,7 +29,12 @@ class CharacterGame: public Game, public AnimationClip::Listener
/**
* @see AnimationClip::Listener::animationEvent
*/
- void animationEvent(AnimationClip* clip, EventType type);
+ void animationEvent(AnimationClip* clip, AnimationClip::Listener::EventType type);
+
+ /**
+ * @see Game::gamepadEvent
+ */
+ void gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int index);
protected:
@@ -76,11 +80,12 @@ class CharacterGame: public Game, public AnimationClip::Listener
AnimationClip* _currentClip;
AnimationClip* _jumpClip;
int _rotateX;
- Gamepad* _gamepad;
MaterialParameter* _materialParameterAlpha;
unsigned int _keyFlags;
int _drawDebug;
bool _buttonReleased;
+ Gamepad* _gamepad;
+ Vector2 _currentDirection;
};
#endif
View
342 gameplay-samples/sample03-character/src/Gamepad.cpp
@@ -1,342 +0,0 @@
-#include "Base.h"
-#include "Gamepad.h"
-#include "Texture.h"
-
-#define INVALID_CONTACT_INDEX ((unsigned int) -1)
-
-Gamepad::Gamepad(const char* texturePath, unsigned int joysticks, unsigned int buttons)
- : _joystickCount(joysticks), _buttonCount(buttons), _texture(NULL), _spriteBatch(NULL)
-{
- assert (texturePath);
- assert(joysticks <= _joystickCount);
- assert(buttons <= _buttonCount);
-
- // Load the gamepad texture.
- if (strlen(texturePath) == 0)
- {
- printError("Error loading Gamepad: No valid texture path specified, in %s", texturePath);
- }
-
- _texture = Texture::create(texturePath, false);
- assert(_texture);
-
- // TODO: determine the "actual" number of sprites required when parsing the .gamepad file.
- _spriteBatch = SpriteBatch::create(_texture, 0, _buttonCount + _joystickCount);
-
- // Create the joystick objects.
- _joysticks = new Joystick*[_joystickCount];
- for (unsigned int i = 0; i < _joystickCount; i++)
- {
- _joysticks[i] = new Joystick();
- }
-
- // Create the button objects.
- _buttons = new Button*[_buttonCount];
- for (unsigned int i = 0; i < _buttonCount; i++)
- {
- _buttons[i] = new Button();
- }
-}
-
-Gamepad::Gamepad(const Gamepad* g)
-{
-}
-
-Gamepad::~Gamepad()
-{
- SAFE_RELEASE(_texture);
- SAFE_DELETE(_spriteBatch);
-
- for (unsigned int i = 0; i < _joystickCount; i++)
- {
- delete _joysticks[i];
- }
- SAFE_DELETE_ARRAY(_joysticks);
-
- for (unsigned int i = 0; i < _buttonCount; i++)
- {
- delete _buttons[i];
- }
- SAFE_DELETE_ARRAY(_buttons);
-}
-
-Gamepad::Button::Button()
- : _pressed(BUTTON_RELEASED), _defaultTextureEnabled(false), _focusTextureEnabled(false), _contactIndex(INVALID_CONTACT_INDEX)
-{
-
-}
-
-Gamepad::Button::Button(const Button& button)
-{
-}
-
-Gamepad::Button::~Button()
-{
-}
-
-Gamepad::Joystick::Joystick()
- : _displacement(Vector2::zero()), _direction(Vector2::zero()), _radius(0.0f),
- _defaultTextureInnerEnabled(false), _defaultTextureOuterEnabled(false), _contactIndex(INVALID_CONTACT_INDEX)
-{
-}
-
-Gamepad::Joystick::Joystick(const Joystick& joystick)
-{
-}
-
-Gamepad::Joystick::~Joystick()
-{
-}
-
-void Gamepad::setButton(unsigned int buttonId, const Rectangle& screenRegion, const Rectangle& releasedTextureRegion, const Rectangle& pressedTextureRegion)
-{
- assert(buttonId < _buttonCount);
-
- _buttons[buttonId]->_region.x = screenRegion.x;
- _buttons[buttonId]->_region.y = screenRegion.y;
- _buttons[buttonId]->_region.width = screenRegion.width;
- _buttons[buttonId]->_region.height = screenRegion.height;
-
- _buttons[buttonId]->_defaultTexCoord.u1 = (releasedTextureRegion.x / _texture->getWidth());
- _buttons[buttonId]->_defaultTexCoord.v1 = 1.0f - (releasedTextureRegion.y / _texture->getHeight());
- _buttons[buttonId]->_defaultTexCoord.u2 = _buttons[buttonId]->_defaultTexCoord.u1 + (releasedTextureRegion.width / _texture->getWidth());
- _buttons[buttonId]->_defaultTexCoord.v2 = _buttons[buttonId]->_defaultTexCoord.v1 - (releasedTextureRegion.height / _texture->getHeight());
- _buttons[buttonId]->_defaultTextureEnabled = true;
-
- _buttons[buttonId]->_focusTexCoord.u1 = (pressedTextureRegion.x / _texture->getWidth());
- _buttons[buttonId]->_focusTexCoord.v1 = 1.0f - (pressedTextureRegion.y / _texture->getHeight());
- _buttons[buttonId]->_focusTexCoord.u2 = _buttons[buttonId]->_focusTexCoord.u1 + (pressedTextureRegion.width / _texture->getWidth());
- _buttons[buttonId]->_focusTexCoord.v2 = _buttons[buttonId]->_focusTexCoord.v1 - (pressedTextureRegion.height / _texture->getHeight());
- _buttons[buttonId]->_focusTextureEnabled = true;
-}
-
-Gamepad::ButtonState Gamepad::getButtonState(unsigned int index) const
-{
- assert(index < _buttonCount);
-
- return _buttons[index]->_pressed;
-}
-
-void Gamepad::setJoystick(unsigned int joystickId, const Rectangle& thumbScreenRegion, const Rectangle& thumbTextureRegion, const Rectangle& dockScreenRegion, const Rectangle& dockTextureRegion, float radius)
-{
- assert (joystickId < _joystickCount);
-
- _joysticks[joystickId]->_radius = radius;
-
- _joysticks[joystickId]->_regionInner.x = thumbScreenRegion.x;
- _joysticks[joystickId]->_regionInner.y = thumbScreenRegion.y;
- _joysticks[joystickId]->_regionInner.width = thumbScreenRegion.width;
- _joysticks[joystickId]->_regionInner.height = thumbScreenRegion.height;
-
- _joysticks[joystickId]->_defaultTexCoordInner.u1 = (thumbTextureRegion.x / _texture->getWidth());
- _joysticks[joystickId]->_defaultTexCoordInner.v1 = 1.0f - (thumbTextureRegion.y / _texture->getHeight());
- _joysticks[joystickId]->_defaultTexCoordInner.u2 = _joysticks[joystickId]->_defaultTexCoordInner.u1 + (thumbTextureRegion.width / _texture->getWidth());
- _joysticks[joystickId]->_defaultTexCoordInner.v2 = _joysticks[joystickId]->_defaultTexCoordInner.v1 - (thumbTextureRegion.height / _texture->getHeight());
- _joysticks[joystickId]->_defaultTextureInnerEnabled = true;
-
- _joysticks[joystickId]->_regionOuter.x = dockScreenRegion.x;
- _joysticks[joystickId]->_regionOuter.y = dockScreenRegion.y;
- _joysticks[joystickId]->_regionOuter.width = dockScreenRegion.width;
- _joysticks[joystickId]->_regionOuter.height = dockScreenRegion.height;
-
- _joysticks[joystickId]->_defaultTexCoordOuter.u1 = (dockTextureRegion.x / _texture->getWidth());
- _joysticks[joystickId]->_defaultTexCoordOuter.v1 = 1.0f - (dockTextureRegion.y / _texture->getHeight());
- _joysticks[joystickId]->_defaultTexCoordOuter.u2 = _joysticks[joystickId]->_defaultTexCoordOuter.u1 + (dockTextureRegion.width / _texture->getWidth());
- _joysticks[joystickId]->_defaultTexCoordOuter.v2 = _joysticks[joystickId]->_defaultTexCoordOuter.v1 - (dockTextureRegion.height / _texture->getHeight());
- _joysticks[joystickId]->_defaultTextureOuterEnabled = true;
-}
-
-bool Gamepad::isJoystickActive(unsigned int joystickId) const
-{
- assert(joystickId < _joystickCount);
-
- return _joysticks[joystickId]->_contactIndex != INVALID_CONTACT_INDEX;
-}
-
-const Vector2& Gamepad::getJoystickState(unsigned int joystickId) const
-{
- assert(joystickId < _joystickCount);
-
- return _joysticks[joystickId]->_direction;
-}
-
-unsigned int Gamepad::getJoystickContactIndex(unsigned int joystickId) const
-{
- assert(joystickId < _joystickCount);
-
- return _joysticks[joystickId]->_contactIndex;
-}
-
-void Gamepad::setSpriteBatch(SpriteBatch* spriteBatch)
-{
- _spriteBatch = spriteBatch;
-}
-
-void Gamepad::draw(const Vector4& color)
-{
- _spriteBatch->begin();
-
- // Draw buttons.
- for (unsigned int i = 0; i < _buttonCount; ++i)
- {
- if ((_buttons[i]->_pressed == BUTTON_PRESSED) && (_buttons[i]->_focusTextureEnabled))
- {
- _spriteBatch->draw(_buttons[i]->_region.x, _buttons[i]->_region.y, _buttons[i]->_region.width, _buttons[i]->_region.height,
- _buttons[i]->_focusTexCoord.u1, _buttons[i]->_focusTexCoord.v1, _buttons[i]->_focusTexCoord.u2, _buttons[i]->_focusTexCoord.v2,
- color);
- continue;
- }
- else if (_buttons[i]->_defaultTextureEnabled)
- {
- _spriteBatch->draw(_buttons[i]->_region.x, _buttons[i]->_region.y, _buttons[i]->_region.width, _buttons[i]->_region.height,
- _buttons[i]->_defaultTexCoord.u1, _buttons[i]->_defaultTexCoord.v1, _buttons[i]->_defaultTexCoord.u2, _buttons[i]->_defaultTexCoord.v2,
- color);
- }
- }
-
- // Draw joysticks.
- for (unsigned int i = 0; i < _joystickCount; ++i)
- {
- float x, y, width, height;
-
- if (_joysticks[i]->_defaultTextureOuterEnabled)
- {
- // Draw Outer joggle.
- x = _joysticks[i]->_regionOuter.x;
- y = _joysticks[i]->_regionOuter.y;
- width = _joysticks[i]->_regionOuter.width;
- height = _joysticks[i]->_regionOuter.height;
-
- _spriteBatch->draw(x, y, width, height, _joysticks[i]->_defaultTexCoordOuter.u1, _joysticks[i]->_defaultTexCoordOuter.v1, _joysticks[i]->_defaultTexCoordOuter.u2, _joysticks[i]->_defaultTexCoordOuter.v2, color);
- }
-
- if (_joysticks[i]->_defaultTextureInnerEnabled)
- {
- // Draw Inner joggle.
- x = _joysticks[i]->_regionInner.x;
- y = _joysticks[i]->_regionInner.y;
- width = _joysticks[i]->_regionInner.width;
- height = _joysticks[i]->_regionInner.height;
- float radius = _joysticks[i]->_radius;
- float displacementX = _joysticks[i]->_displacement.x;
- float displacementY = _joysticks[i]->_displacement.y;
-
- // Move position to reflect displacement.
- if (((displacementX * displacementX) + (displacementY * displacementY)) <= (radius * radius))
- {
- x += displacementX;
- y += displacementY;
- }
- else
- {
- // Find the point on the joystick's circular bound where
- // the vector intersects. This would be the position of the
- // inner joggle.
- Vector2 delta = Vector2(displacementX, displacementY);
- delta.normalize();
- delta.scale(radius);
- x += delta.x;
- y += delta.y;
- }
-
- _spriteBatch->draw(x, y, width, height, _joysticks[i]->_defaultTexCoordInner.u1, _joysticks[i]->_defaultTexCoordInner.v1, _joysticks[i]->_defaultTexCoordInner.u2, _joysticks[i]->_defaultTexCoordInner.v2, color);
- }
- }
- _spriteBatch->end();
-}
-
-void Gamepad::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
-{
- for (unsigned int i = 0; i < _buttonCount; ++i)
- {
- switch (evt)
- {
- case Touch::TOUCH_PRESS:
- {
- if (_buttons[i]->_contactIndex == INVALID_CONTACT_INDEX)
- {
- if ((x >= _buttons[i]->_region.x) && (x <= (_buttons[i]->_region.x + _buttons[i]->_region.width)) &&
- (y >= _buttons[i]->_region.y) && (y <= (_buttons[i]->_region.y + _buttons[i]->_region.height)))
- {
- _buttons[i]->_contactIndex = contactIndex;
- _buttons[i]->_pressed = BUTTON_PRESSED;
- }
- }
- }
- break;
- case Touch::TOUCH_RELEASE:
- {
- if (_buttons[i]->_contactIndex == contactIndex)
- {
- _buttons[i]->_contactIndex = -1;
- _buttons[i]->_pressed = BUTTON_RELEASED;
- }
- }
- break;
- }
- }
-
- for (unsigned int i = 0; i < _joystickCount; ++i)
- {
- switch (evt)
- {
- case Touch::TOUCH_PRESS:
- {
- // Activate the joystick only when it is touched inside the
- // outer joggle.
- float bx = _joysticks[i]->_regionInner.x + _joysticks[i]->_regionInner.width * 0.5f;
- float by = _joysticks[i]->_regionInner.y + _joysticks[i]->_regionInner.height * 0.5f;
- float bradius = _joysticks[i]->_radius;
-
- if ((x >= (bx - bradius)) && (x <= (bx + bradius)) &&
- (y >= (by - bradius)) && (y <= (by + bradius)) && _joysticks[i]->_contactIndex == INVALID_CONTACT_INDEX)
- {
- _joysticks[i]->_contactIndex = contactIndex;
- _joysticks[i]->_displacement.set(0.0f, 0.0f);
- _joysticks[i]->_direction.set(0.0f, 0.0f);
- }
- }
- case Touch::TOUCH_MOVE:
- {
- if (_joysticks[i]->_contactIndex == contactIndex)
- {
- float displacementX = x - (_joysticks[i]->_regionInner.x + _joysticks[i]->_regionInner.width * 0.5f);
- float displacementY = y - (_joysticks[i]->_regionInner.y + _joysticks[i]->_regionInner.height * 0.5f);
- float radius = _joysticks[i]->_radius;
-
- // Determine the position of the finger. If finger moves beyond the joystick radius,
- // clamp the direction vector to bounding circle.
- if (((displacementX * displacementX) + (displacementY * displacementY)) <= (radius * radius))
- {
- // Negate the y in the direction vector so that y is positive up.
- _joysticks[i]->_direction.set(displacementX, -displacementY);
- _joysticks[i]->_direction.scale(1.0f / radius);
- }
- else
- {
- // Negate the displacementY here so that y is positive up.
- Vector2 delta = Vector2(displacementX, -displacementY);
- delta.normalize();
- delta.scale(radius);
- delta.normalize();
- _joysticks[i]->_direction.set(delta);
- }
-
- _joysticks[i]->_displacement.set(displacementX, displacementY);
- }
- }
- break;
- case Touch::TOUCH_RELEASE:
- {
- if (_joysticks[i]->_contactIndex == contactIndex)
- {
- // Reset displacement and direction vectors.
- _joysticks[i]->_contactIndex = -1;
- _joysticks[i]->_displacement.set(0.0f, 0.0f);
- _joysticks[i]->_direction.set(0.0f, 0.0f);
- }
- }
- break;
- }
- }
-}
View
218 gameplay-samples/sample03-character/src/Gamepad.h
@@ -1,218 +0,0 @@
-#ifndef GAMEPAD_H_
-#define GAMEPAD_H_
-
-#include "Keyboard.h"
-#include "Properties.h"
-#include "SpriteBatch.h"
-#include "Touch.h"
-#include "Vector2.h"
-
-using namespace gameplay;
-
-/**
- * Defines a class for accessing gamepad controls: Buttons and Joysticks.
- */
-class Gamepad
-{
-public:
-
- /**
- * The button state's.
- */
- enum ButtonState
- {
- BUTTON_RELEASED,
- BUTTON_PRESSED
- };
-
- /**
- * Constructor.
- *
- * @param texturePath The path to the gamepad's texture atlas.
- * @param joysticks The number of joysticks the game pad will have.
- * @param buttons The number of buttons the game pad will have.
- */
- Gamepad(const char* texturePath, unsigned int joystickCount, unsigned int buttonCount);
-
- /**
- * Destructor.
- */
- ~Gamepad();
-
- /**
- * Sets the screen region and textures to use for the specified button on the gamepad.
- *
- * @param buttonId The unique integer ID of the button to set.
- * @param screenRegion The region on the screen to draw the button.
- * @param releasedTextureRegion The region on the gamepad's texture atlas to draw as the BUTTON_RELEASED state.
- * @param pressedTextureRegion The region on the gamepad's texture atlas to draw as the BUTTON_PRESSED state.
- */
- void setButton(unsigned int buttonId, const Rectangle& screenRegion, const Rectangle& releasedTextureRegion, const Rectangle& pressedTextureRegion);
-
- /**
- * Returns the current state of the specified button.
- *
- * @param buttonId The ID of the button on the gamepad.
- * @return The state of the specified button. BUTTON_PRESSED if the button is pressed; BUTTON_RELEASED otherwise.
- */
- ButtonState getButtonState(unsigned int buttonId) const;
-
- /**
- * Sets the screen region and textures to use for the specified joystick on the gamepad.
- *
- * @param joystickId The unique integer ID of the joystick to set.
- * @param thumbScreenRegion The region on the screen to draw the thumb stick of the joystick.
- * @param thumbTextureRegion The region on the gamepad's texture atlas to draw as the thumb stick of the joystick.
- * @param dockScreenRegion The region on the screen to draw the dock that the thumb stick sits in.
- * @param dockTextureRegion The region on the gamepad's texture atlas to draw as the dock for the thumb stick of the joystick.
- * @param radius The maximum radius that the thumb stick will revolve around the dock region.
- */
- void setJoystick(unsigned int joystickId, const Rectangle& thumbScreenRegion, const Rectangle& thumbTextureRegion, const Rectangle& dockScreenRegion, const Rectangle& dockTextureRegion, float radius);
-
- /**
- * Gets whether the specified joystick is active or not.
- *
- * @param joystickId The unique integer ID of the joystick to set.
- * @return Whether the given joystick is active or not.
- */
- bool isJoystickActive(unsigned int joystickId) const;
-
- /**
- * Returns the specified joystick's state as a Vector2.
- *
- * @param joystickId The unique integer ID of the joystick to set.
- * @return A Vector2 of the joystick displacement for the specified joystick.
- */
- const Vector2& getJoystickState(unsigned int joystickId) const;
-
- /**
- * Returns the current touch contact index for the specified joystick.
- *
- * @return The contact index for the specified joystick.
- */
- unsigned int getJoystickContactIndex(unsigned int joystickId) const;
-
- /**
- * Draws the gamepad.
- *
- * @param color
- */
- void draw(const Vector4& color = Vector4::one());
-
- /**
- * Input callback on keyPress events.
- *
- * @param key The key code pressed.
- * @param keyEvent The key event that occured.
- *
- * @see Input::Key
- * @see Input::KeyEvent
- */
- void keyPress(int key, int keyEvent);
-
- /**
- * Input callback on touch events.
- *
- * @param x The x position of the touch.
- * @param y The y position of the touch.
- * @param touchEvent The touch event that occurred.
- *
- * @see Input::TouchEvent
- */
- void touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
-
-private:
-
- /**
- * Hidden copy constructor.
- */
- Gamepad(const Gamepad* gamepad);
-
- /**
- * Sets a spritebatch to this gamepad.
- */
- void setSpriteBatch(SpriteBatch* spriteBatch);
-
- /**
- * Structure for holding texture coordinates for a rectangle.
- */
- struct TexCoords
- {
- float u1;
- float u2;
- float v1;
- float v2;
- };
-
- /**
- * Defines a gamepad button.
- */
- class Button
- {
- public:
- /**
- * Constructor.
- */
- Button();
-
- /**
- * Copy constructor.
- */
- Button(const Button& button);
-
- /**
- * Destructor.
- */
- ~Button();
-
- Gamepad::ButtonState _pressed;
- Rectangle _region;
- bool _defaultTextureEnabled;
- TexCoords _defaultTexCoord;
- bool _focusTextureEnabled;
- TexCoords _focusTexCoord;
- unsigned int _contactIndex;
- };
-
- /**
- * Defines a gamepad joystick.
- */
- class Joystick
- {
- public:
- /**
- * Constructor.
- */
- Joystick();
-
- /**
- * Copy constructor.
- */
- Joystick(const Joystick& joystick);
-
- /**
- * Destructor.
- */
- ~Joystick();
-
- Vector2 _displacement;
- Vector2 _direction;
- float _radius;
- Rectangle _regionInner;
- bool _defaultTextureInnerEnabled;
- TexCoords _defaultTexCoordInner;
- Rectangle _regionOuter;
- bool _defaultTextureOuterEnabled;
- TexCoords _defaultTexCoordOuter;
- unsigned int _contactIndex;
- };
-
- unsigned int _joystickCount;
- unsigned int _buttonCount;
- Button** _buttons;
- Joystick** _joysticks;
- Texture* _texture;
- SpriteBatch* _spriteBatch;
-};
-
-#endif
View
6 gameplay/gameplay.vcxproj
@@ -35,7 +35,6 @@
<ClCompile Include="src\Curve.cpp" />
<ClCompile Include="src\DebugNew.cpp" />
<ClCompile Include="src\DepthStencilTarget.cpp" />
- <ClCompile Include="src\DropDownList.cpp" />
<ClCompile Include="src\Effect.cpp" />
<ClCompile Include="src\FileSystem.cpp" />
<ClCompile Include="src\FlowLayout.cpp" />
@@ -44,6 +43,7 @@
<ClCompile Include="src\FrameBuffer.cpp" />
<ClCompile Include="src\Frustum.cpp" />
<ClCompile Include="src\Game.cpp" />
+ <ClCompile Include="src\Gamepad.cpp" />
<ClCompile Include="src\gameplay-main-android.cpp" />
<ClCompile Include="src\gameplay-main-qnx.cpp" />
<ClCompile Include="src\gameplay-main-win32.cpp" />
@@ -53,7 +53,6 @@
<ClCompile Include="src\Label.cpp" />
<ClCompile Include="src\Layout.cpp" />
<ClCompile Include="src\Light.cpp" />
- <ClCompile Include="src\List.cpp" />
<ClCompile Include="src\Material.cpp" />
<ClCompile Include="src\MeshBatch.cpp" />
<ClCompile Include="src\Pass.cpp" />
@@ -129,7 +128,6 @@
<ClInclude Include="src\Curve.h" />
<ClInclude Include="src\DebugNew.h" />
<ClInclude Include="src\DepthStencilTarget.h" />
- <ClInclude Include="src\DropDownList.h" />
<ClInclude Include="src\Effect.h" />
<ClInclude Include="src\FileSystem.h" />
<ClInclude Include="src\FlowLayout.h" />
@@ -138,6 +136,7 @@
<ClInclude Include="src\FrameBuffer.h" />
<ClInclude Include="src\Frustum.h" />
<ClInclude Include="src\Game.h" />
+ <ClInclude Include="src\Gamepad.h" />
<ClInclude Include="src\gameplay.h" />
<ClInclude Include="src\Image.h" />
<ClInclude Include="src\Joint.h" />
@@ -146,7 +145,6 @@
<ClInclude Include="src\Label.h" />
<ClInclude Include="src\Layout.h" />
<ClInclude Include="src\Light.h" />
- <ClInclude Include="src\List.h" />
<ClInclude Include="src\Material.h" />
<ClInclude Include="src\MathUtil.h" />
<ClInclude Include="src\MeshBatch.h" />
View
10 gameplay/gameplay.vcxproj.filters
@@ -279,10 +279,7 @@
<ClCompile Include="src\Joystick.cpp">
<Filter>src</Filter>
</ClCompile>
- <ClCompile Include="src\DropDownList.cpp">
- <Filter>src</Filter>
- </ClCompile>
- <ClCompile Include="src\List.cpp">
+ <ClCompile Include="src\Gamepad.cpp">
<Filter>src</Filter>
</ClCompile>
</ItemGroup>
@@ -563,10 +560,7 @@
<ClInclude Include="src\MathUtil.h">
<Filter>src</Filter>
</ClInclude>
- <ClInclude Include="src\DropDownList.h">
- <Filter>src</Filter>
- </ClInclude>
- <ClInclude Include="src\List.h">
+ <ClInclude Include="src\Gamepad.h">
<Filter>src</Filter>
</ClInclude>
</ItemGroup>
View
19 gameplay/src/Button.cpp
@@ -5,11 +5,14 @@ namespace gameplay
{
Button::Button()
+ : _gamepadButtonIndex(NULL)
{
}
Button::~Button()
{
+ if (_gamepadButtonIndex)
+ SAFE_DELETE(_gamepadButtonIndex);
}
Button* Button::create(const char* id, Theme::Style* style)
@@ -33,9 +36,7 @@ Button* Button::create(Theme::Style* style, Properties* properties)
bool Button::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
{
if (!isEnabled())
- {
return false;
- }
switch (evt)
{
@@ -43,8 +44,12 @@ bool Button::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contac
if (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
{
+ _contactIndex = (int) contactIndex;
+
setState(Control::ACTIVE);
+
notifyListeners(Listener::PRESS);
+
return _consumeInputEvents;
}
else
@@ -54,12 +59,15 @@ bool Button::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contac
break;
case Touch::TOUCH_RELEASE:
+ _contactIndex = INVALID_CONTACT_INDEX;
notifyListeners(Listener::RELEASE);
if (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
{
setState(Control::FOCUS);
+
notifyListeners(Listener::CLICK);
+
return _consumeInputEvents;
}
else
@@ -75,4 +83,9 @@ bool Button::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contac
return false;
}
-}
+const char* Button::getType() const
+{
+ return "button";
+}
+
+}
View
10 gameplay/src/Button.h
@@ -32,6 +32,7 @@ namespace gameplay
class Button : public Label
{
friend class Container;
+ friend class Gamepad;
public:
@@ -81,14 +82,21 @@ class Button : public Label
*/
bool touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
+ /**
+ * @see Control::getType
+ */
+ const char* getType() const;
+
private:
/**
* Constructor.
*/
Button(const Button& copy);
+
+ int* _gamepadButtonIndex;
};
}
-#endif
+#endif
View
5 gameplay/src/CheckBox.cpp
@@ -171,4 +171,9 @@ void CheckBox::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
spriteBatch->draw(pos.x, pos.y, size.x, size.y, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color, _viewportClipBounds);
}
+const char* CheckBox::getType() const
+{
+ return "checkBox";
+}
+
}
View
7 gameplay/src/CheckBox.h
@@ -77,6 +77,11 @@ class CheckBox : public Button
const Vector2& getImageSize() const;
/**
+ * @see Control::getType
+ */
+ const char* getType() const;
+
+ /**
* Add a listener to be notified of specific events affecting
* this control. Event types can be OR'ed together.
* E.g. To listen to touch-press and touch-release events,
@@ -166,4 +171,4 @@ class CheckBox : public Button
}
-#endif
+#endif
View
16 gameplay/src/Container.cpp
@@ -11,7 +11,6 @@
#include "Slider.h"
#include "TextBox.h"
#include "Joystick.h"
-#include "List.h"
#include "Game.h"
namespace gameplay
@@ -158,10 +157,6 @@ void Container::addControls(Theme* theme, Properties* properties)
{
control = Joystick::create(controlStyle, controlSpace);
}
- else if (controlName == "LIST")
- {
- control = List::create(controlStyle, controlSpace);
- }
else
{
GP_ERROR("Failed to create control; unrecognized control name '%s'.", controlName.c_str());
@@ -338,6 +333,11 @@ Animation* Container::getAnimation(const char* id) const
return NULL;
}
+const char* Container::getType() const
+{
+ return "container";
+}
+
void Container::update(const Control* container, const Vector2& offset)
{
// Update this container's viewport.
@@ -578,7 +578,7 @@ bool Container::keyEvent(Keyboard::KeyEvent evt, int key)
return false;
}
-bool Container::isContainer()
+bool Container::isContainer() const
{
return true;
}
@@ -975,7 +975,7 @@ bool Container::pointerEvent(bool mouse, char evt, int x, int y, int data)
Control::State currentState = control->getState();
if ((control->isContainer() && currentState == Control::FOCUS) ||
- currentState != Control::NORMAL ||
+ (currentState != Control::NORMAL && control->_contactIndex == data) ||
((evt == Touch::TOUCH_PRESS ||
evt == Mouse::MOUSE_PRESS_LEFT_BUTTON ||
evt == Mouse::MOUSE_PRESS_MIDDLE_BUTTON ||
@@ -1001,7 +1001,7 @@ bool Container::pointerEvent(bool mouse, char evt, int x, int y, int data)
if (!eventConsumed && _scroll != SCROLL_NONE)
{
- if (mouse && mouseEventScroll((Mouse::MouseEvent)evt, x - xPos, y - yPos, data) ||
+ if ((mouse && mouseEventScroll((Mouse::MouseEvent)evt, x - xPos, y - yPos, data)) ||
(!mouse && touchEventScroll((Touch::TouchEvent)evt, x - xPos, y - yPos, (unsigned int)data)))
{
_dirty = true;
View
9 gameplay/src/Container.h
@@ -178,6 +178,11 @@ class Container : public Control
Animation* getAnimation(const char* id = NULL) const;
/**
+ * @see Control::getType
+ */
+ const char* getType() const;
+
+ /**
* @see AnimationTarget#getAnimationPropertyComponentCount
*/
virtual unsigned int getAnimationPropertyComponentCount(int propertyId) const;
@@ -284,10 +289,10 @@ class Container : public Control
/**
* Returns whether this control is a container.
- *
+ *
* @return true if this is a container, false if not.
*/
- bool isContainer();
+ bool isContainer() const;
/**
* Returns whether this container or any of its controls have been modified and require an update.
View
25 gameplay/src/Control.cpp
@@ -6,8 +6,9 @@ namespace gameplay
{
Control::Control()
- : _id(""), _state(Control::NORMAL), _bounds(Rectangle::empty()), _clipBounds(Rectangle::empty()), _viewportClipBounds(Rectangle::empty()),
- _dirty(true), _consumeInputEvents(true), _listeners(NULL), _styleOverridden(false), _skin(NULL), _clearBounds(Rectangle::empty())
+ : _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),
+ _styleOverridden(false), _skin(NULL)
{
}
@@ -706,9 +707,7 @@ void Control::addSpecificListener(Control::Listener* listener, Listener::EventTy
bool Control::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
{
if (!isEnabled())
- {
return false;
- }
switch (evt)
{
@@ -719,17 +718,24 @@ bool Control::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int conta
if (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
{
+ _contactIndex = (int) contactIndex;
+
notifyListeners(Listener::PRESS);
+
return _consumeInputEvents;
}
else
{
// If this control was in focus, it's not any more.
_state = NORMAL;
+ _contactIndex = INVALID_CONTACT_INDEX;
}
break;
case Touch::TOUCH_RELEASE:
+
+ _contactIndex = INVALID_CONTACT_INDEX;
+
// Always trigger Listener::RELEASE
notifyListeners(Listener::RELEASE);
@@ -738,8 +744,9 @@ bool Control::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int conta
y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
{
// Leave this control in the FOCUS state.
- notifyListeners(Listener::CLICK);
+ notifyListeners(Listener::CLICK);
}
+
return _consumeInputEvents;
}
@@ -991,7 +998,7 @@ bool Control::isDirty()
return _dirty;
}
-bool Control::isContainer()
+bool Control::isContainer() const
{
return false;
}
@@ -1035,6 +1042,11 @@ Theme::ThemeImage* Control::getImage(const char* id, State state)
return imageList->getImage(id);
}
+const char* Control::getType() const
+{
+ return "control";
+}
+
// Implementation of AnimationHandler
unsigned int Control::getAnimationPropertyComponentCount(int propertyId) const
{
@@ -1122,7 +1134,6 @@ void Control::setAnimationPropertyValue(int propertyId, AnimationValue* value, f
break;
case ANIMATE_OPACITY:
_dirty = true;
- default:
break;
}
}
View
20 gameplay/src/Control.h
@@ -25,6 +25,7 @@ class Control : public Ref, public AnimationTarget
friend class AbsoluteLayout;
friend class VerticalLayout;
friend class FlowLayout;
+ friend class Gamepad;
public:
@@ -710,6 +711,13 @@ class Control : public Ref, public AnimationTarget
virtual void addListener(Control::Listener* listener, int eventFlags);
/**
+ * Gets the type of the Control and returns it as a string.
+ *
+ * @return The string of the Control type, all in lower-case.
+ */
+ virtual const char* getType() const;
+
+ /**
* @see AnimationTarget#getAnimationPropertyComponentCount
*/
virtual unsigned int getAnimationPropertyComponentCount(int propertyId) const;
@@ -727,6 +735,11 @@ class Control : public Ref, public AnimationTarget
protected:
/**
+ * Constant value representing an unset or invalid contact index.
+ */
+ static const int INVALID_CONTACT_INDEX = -1;
+
+ /**
* Constructor.
*/
Control();
@@ -843,7 +856,7 @@ class Control : public Ref, public AnimationTarget
*
* @return true if this object is of class Container, false otherwise.
*/
- virtual bool isContainer();
+ virtual bool isContainer() const;
/**
* Returns whether this control has been modified and requires an update.
@@ -977,6 +990,11 @@ class Control : public Ref, public AnimationTarget
int _zIndex;
/**
+ * The contact index assigned to this control.
+ */
+ int _contactIndex;
+
+ /**
* The focus order of the control.
*/
int _focusIndex;
View
6 gameplay/src/Form.cpp
@@ -57,6 +57,7 @@ Form* Form::create(const char* id, Theme::Style* style, Layout::Type layoutType)
break;
default:
GP_ERROR("Unsupported layout type '%d'.", layoutType);
+ break;
}
Form* form = new Form();
@@ -521,6 +522,11 @@ void Form::draw()
}
}
+const char* Form::getType() const
+{
+ return "form";
+}
+
void Form::initializeQuad(Mesh* mesh)
{
// Release current model.
View
7 gameplay/src/Form.h
@@ -156,6 +156,11 @@ class Form : public Container
*/
void draw();
+ /**
+ * @see Control::getType
+ */
+ const char* getType() const;
+
private:
/**
@@ -237,4 +242,4 @@ class Form : public Container
}
-#endif
+#endif
View
1  gameplay/src/FrameBuffer.cpp
@@ -86,6 +86,7 @@ FrameBuffer* FrameBuffer::create(const char* id, unsigned int width, unsigned in
frameBuffer->_renderTargets = renderTargets;
frameBuffer->setRenderTarget(renderTarget, 0);
+ SAFE_RELEASE(renderTarget);
__frameBuffers.push_back(frameBuffer);
View
78 gameplay/src/Game.cpp
@@ -20,7 +20,8 @@ Game::Game()
: _initialized(false), _state(UNINITIALIZED),
_frameLastFPS(0), _frameCount(0), _frameRate(0),
_clearDepth(1.0f), _clearStencil(0), _properties(NULL),
- _animationController(NULL), _audioController(NULL), _physicsController(NULL), _audioListener(NULL)
+ _animationController(NULL), _audioController(NULL), _physicsController(NULL), _audioListener(NULL),
+ _gamepadCount(0), _gamepads(NULL)
{
GP_ASSERT(__gameInstance == NULL);
__gameInstance = this;
@@ -95,7 +96,7 @@ bool Game::startup()
setViewport(Rectangle(0.0f, 0.0f, (float)_width, (float)_height));
RenderState::initialize();
FrameBuffer::initialize();
-
+
_animationController = new AnimationController();
_animationController->initialize();
@@ -105,6 +106,8 @@ bool Game::startup()
_physicsController = new PhysicsController();
_physicsController->initialize();
+ loadGamepad();
+
_state = RUNNING;
return true;
@@ -122,6 +125,15 @@ void Game::shutdown()
Platform::signalShutdown();
finalize();
+ if (_gamepads)
+ {
+ for (unsigned int i = 0; i < _gamepadCount; i++)
+ {
+ SAFE_DELETE(_gamepads[i]);
+ }
+ SAFE_DELETE_ARRAY(_gamepads);
+ }
+
_animationController->finalize();
SAFE_DELETE(_animationController);
@@ -207,6 +219,12 @@ void Game::frame()
// Update the physics.
_physicsController->update(elapsedTime);
+ if (_gamepads)
+ {
+ for (unsigned int i = 0; i < _gamepadCount; i++)
+ _gamepads[i]->update();
+ }
+
// Application Update.
update(elapsedTime);
@@ -215,6 +233,12 @@ void Game::frame()
// Graphics Rendering.
render(elapsedTime);
+
+ if (_gamepads)
+ {
+ for (unsigned int i = 0; i < _gamepadCount; i++)
+ _gamepads[i]->render();
+ }
// Update FPS.
++_frameCount;
@@ -305,6 +329,10 @@ void Game::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactI
{
}
+void Game::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int index)
+{
+}
+
void Game::schedule(float timeOffset, TimeListener* timeListener, void* cookie)
{
GP_ASSERT(_timeEvents);
@@ -386,4 +414,50 @@ bool Game::TimeEvent::operator<(const TimeEvent& v) const
return time > v.time;
}
+Gamepad* Game::createGamepad(const char* gamepadFormPath)
+{
+ GP_ASSERT(gamepadFormPath);
+
+ Gamepad* gamepad = new Gamepad(gamepadFormPath);
+ GP_ASSERT(gamepad);
+
+ if (!_gamepads)
+ {
+ _gamepadCount++;
+ _gamepads = new Gamepad*[_gamepadCount];
+ _gamepads[0] = gamepad;
+ }
+ else
+ {
+ int oldSize = _gamepadCount;
+ _gamepadCount++;
+ Gamepad** tempGamepads = new Gamepad*[_gamepadCount];
+ memcpy(tempGamepads, _gamepads, sizeof(Gamepad*) * oldSize);
+ tempGamepads[oldSize] = gamepad;
+
+ SAFE_DELETE_ARRAY(_gamepads);
+ _gamepads = tempGamepads;
+ }
+
+ return gamepad;
+}
+
+void Game::loadGamepad()
+{
+ if (_properties)
+ {
+ // Check if there is a virtual keyboard included in the .config file.
+ // If there is, try to create it and assign it to "player one".
+ Properties* gamepadProperties = _properties->getNamespace("gamepad", true);
+ if (gamepadProperties && gamepadProperties->exists("form"))
+ {
+ const char* gamepadFormPath = gamepadProperties->getString("form");
+ GP_ASSERT(gamepadFormPath);
+
+ Gamepad* gamepad = createGamepad(gamepadFormPath);
+ GP_ASSERT(gamepad);
+ }
+ }
+}
+
}
View
33 gameplay/src/Game.h
@@ -13,6 +13,7 @@
#include "Rectangle.h"
#include "Vector4.h"
#include "TimeListener.h"
+#include "Gamepad.h"
namespace gameplay
{
@@ -22,6 +23,7 @@ namespace gameplay
*/
class Game
{
+
public:
/**
@@ -268,6 +270,15 @@ class Game
virtual bool mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta);
/**
+ * Gamepad callback on gamepad events.
+ *
+ * @param evt The gamepad event that occured.
+ * @param gamepad the gamepad the event occured on
+ * @param index The joystick or button index that triggered the event.
+ */
+ virtual void gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int index);
+
+ /**
* Sets muli-touch is to be enabled/disabled. Default is disabled.
*
* @param enabled true sets multi-touch is enabled, false to be disabled.
@@ -299,6 +310,21 @@ class Game
*/
void schedule(float timeOffset, TimeListener* timeListener, void* cookie = 0);
+ /**
+ * Creates a Gamepad object from a .form file.
+ *
+ * @param playerIndex
+ * @param formPath
+ */
+ Gamepad* createGamepad(const char* gamepadFormPath);
+
+ /**
+ * Gets the gamepad for the specified player index.
+ *
+ * @param playerIndex The player index to get the gamepad for (0 <= playerIndex <= 3)
+ */
+ inline Gamepad* getGamepad(unsigned int playerIndex = 0);
+
protected:
/**
@@ -405,6 +431,11 @@ class Game
*/
void loadConfig();
+ /**
+ * Loads a gamepad from the configuration file.
+ */
+ void loadGamepad();
+
bool _initialized; // If game has initialized yet.
State _state; // The game state.
static double _pausedTimeLast; // The last time paused.
@@ -423,6 +454,8 @@ class Game
AudioController* _audioController; // Controls audio sources that are playing in the game.
PhysicsController* _physicsController; // Controls the simulation of a physics scene and entities.
AudioListener* _audioListener; // The audio listener in 3D space.
+ unsigned int _gamepadCount;
+ Gamepad** _gamepads;
std::priority_queue<TimeEvent, std::vector<TimeEvent>, std::less<TimeEvent> >* _timeEvents; // Contains the scheduled time events.
// Note: Do not add STL object member variables on the stack; this will cause false memory leaks to be reported.
View
12 gameplay/src/Game.inl
@@ -72,4 +72,14 @@ inline void Game::displayKeyboard(bool display)
Platform::displayKeyboard(display);
}
-}
+inline Gamepad* Game::getGamepad(unsigned int playerIndex)
+{
+ GP_ASSERT(playerIndex < _gamepadCount);
+
+ if (_gamepads)
+ return _gamepads[playerIndex];
+ else
+ return NULL;
+}
+
+}
View
181 gameplay/src/Gamepad.cpp
@@ -0,0 +1,181 @@
+#include "Base.h"
+#include "Gamepad.h"
+#include "Joystick.h"
+#include "Game.h"
+
+namespace gameplay
+{
+
+Gamepad::Gamepad()
+ : _playerIndex(-1), _joystickValues(NULL), _joystickCount(0), _buttonStates(NULL), _buttonCount(0), _gamepadForm(NULL)
+{
+}
+
+Gamepad::Gamepad(const char* formPath)
+ : _playerIndex(-1), _joystickValues(NULL), _joystickCount(0), _buttonStates(NULL), _buttonCount(0), _gamepadForm(NULL)
+{
+ GP_ASSERT(formPath);
+
+ _gamepadForm = Form::create(formPath);
+ GP_ASSERT(_gamepadForm);
+
+ _gamepadForm->setConsumeInputEvents(false);
+
+ getGamepadControls(_gamepadForm);
+
+ if (_joystickCount > 0)
+ {
+ _joystickValues = new Vector2*[_joystickCount];
+ for (unsigned int i = 0; i < _joystickCount; i++)
+ {
+ _joystickValues[i] = new Vector2();
+ }
+ }
+ if (_buttonCount > 0)
+ {
+ _buttonStates = new ButtonState*[_buttonCount];
+ for (unsigned int i = 0; i < _buttonCount; i++)
+ {
+ _buttonStates[i] = new ButtonState();
+ }
+ }
+}
+
+void Gamepad::getGamepadControls(Form* form)
+{
+ std::vector<Control*> controls = form->getControls();
+ std::vector<Control*>::iterator itr = controls.begin();
+
+ for (; itr != controls.end(); itr++)
+ {
+ Control* control = *itr;
+ GP_ASSERT(control);
+
+ if (std::strcmp("container", control->getType()) == 0)
+ {
+ 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;
+ _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;
+ _buttonCount++;
+ }
+ }
+}
+
+Gamepad::~Gamepad()
+{
+ if (_joystickValues)
+ {
+ for (unsigned int i = 0; i < _joystickCount; i++)
+ SAFE_DELETE(_joystickValues[i]);
+ SAFE_DELETE_ARRAY(_joystickValues);
+ }
+
+ if (_buttonStates)
+ {
+ for (unsigned int i = 0; i < _buttonCount; i++)
+ SAFE_DELETE(_buttonStates[i]);
+ SAFE_DELETE_ARRAY(_buttonStates);
+ }
+
+ if (_gamepadForm)
+ SAFE_RELEASE(_gamepadForm);
+}
+
+void Gamepad::update()
+{
+ if (_gamepadForm && _gamepadForm->isEnabled())
+ {
+ _gamepadForm->update();
+ }
+}
+
+void Gamepad::render()
+{
+ if (_gamepadForm && _gamepadForm->isEnabled())
+ {
+ _gamepadForm->draw();
+ }
+}
+
+void Gamepad::controlEvent(Control* control, Control::Listener::EventType evt)
+{
+ if (_gamepadForm && _gamepadForm->isEnabled())
+ {
+ if (std::strcmp("joystick", control->getType()) == 0)
+ {
+ int joystickIndex = *(((Joystick*) control)->_gamepadJoystickIndex);
+ 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);
+ }
+ else if (std::strcmp("button", control->getType()) == 0)
+ {
+ int buttonIndex = *(((Button*) control)->_gamepadButtonIndex);
+ 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);
+ }
+ }
+}
+
+Gamepad::ButtonState Gamepad::getButtonState(unsigned int buttonId) const
+{
+ GP_ASSERT(buttonId < _buttonCount);
+
+ return *_buttonStates[buttonId];
+}
+
+bool Gamepad::isJoystickActive(unsigned int joystickId) const
+{
+ GP_ASSERT(joystickId < _joystickCount);
+
+ return !(_joystickValues[joystickId]->isZero());
+}
+
+const Vector2& Gamepad::getJoystickValue(unsigned int joystickId) const
+{
+ GP_ASSERT(joystickId < _joystickCount);
+
+ return *_joystickValues[joystickId];
+}
+
+bool Gamepad::isVirtual() const
+{
+ return (_gamepadForm && _gamepadForm->isEnabled());
+}
+
+}
View
113 gameplay/src/Gamepad.h
@@ -0,0 +1,113 @@
+#ifndef GAMEPAD_H_
+#define GAMEPAD_H_
+
+#include "Form.h"
+#include "Button.h"
+
+namespace gameplay
+{
+
+/**
+ * Defines an interface for handling gamepad support.
+ */
+class Gamepad : public Control::Listener
+{
+ friend class Game;
+
+public:
+
+ enum GamepadEvent
+ {
+ JOYSTICK_EVENT,
+ BUTTON_EVENT
+ };
+
+ enum ButtonState
+ {
+ BUTTON_PRESSED = gameplay::Button::Listener::PRESS,
+ BUTTON_RELEASED = gameplay::Button::Listener::RELEASE
+ };
+
+ /**
+ * Gets the current state of the specified button.
+ */
+ 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.
+ * @return Whether the given joystick is active or not.
+ */
+ bool isJoystickActive(unsigned int joystickId) const;
+
+ /**
+ * Returns the specified joystick's value as a Vector2.
+ *
+ * @param joystickId The unique integer ID of the joystick to set.
+ * @return A Vector2 of the joystick displacement for the specified joystick.
+ */
+ const Vector2& getJoystickValue(unsigned int joystickId) const;
+
+ /**
+ * Listener for Control events on the gamepads Joystick or Buttons.
+ */
+ void controlEvent(Control* control, Control::Listener::EventType evt);
+
+ /**
+ * Returns whether the gamepad is represented with a UI form or not.
+ */
+ bool isVirtual() const;
+
+private:
+
+ /**
+ * Constructor.
+ */
+ Gamepad();
+
+ /**
+ * Constructor.
+ * Create a gamepad from the specified formPath.
+ *
+ * @param formPath
+ */
+ Gamepad(const char* formPath);
+
+ /**
+ * Copy constructor.
+ */
+ Gamepad(const Gamepad& copy);
+
+ /**
+ * Destructor.
+ */
+ virtual ~Gamepad();
+
+ /**
+ * Gets the Joystick and Button Control object's from the specified form.
+ */
+ void getGamepadControls(Form* form);
+
+ /**
+ * Updates the gamepad.
+ */
+ void update();
+
+ /**
+ * 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;
+};
+
+}
+
+#endif
View
112 gameplay/src/Joystick.cpp
@@ -1,12 +1,10 @@
#include "Base.h"
#include "Joystick.h"
-#define INVALID_CONTACT_INDEX ((unsigned int)-1)
-
namespace gameplay
{
-Joystick::Joystick() : _contactIndex(INVALID_CONTACT_INDEX), _absolute(true)
+Joystick::Joystick() : _absolute(true), _gamepadJoystickIndex(NULL)
{
}
@@ -16,6 +14,8 @@ Joystick::Joystick(const Joystick& copy)
Joystick::~Joystick()
{
+ if (_gamepadJoystickIndex)
+ SAFE_DELETE(_gamepadJoystickIndex);
}
Joystick* Joystick::create(const char* id, Theme::Style* style)
@@ -52,6 +52,11 @@ void Joystick::initialize(Theme::Style* style, Properties* properties)
}
_radius = properties->getFloat("radius");
+ if (properties->exists("absolute"))
+ {
+ setAbsolute(properties->getBool("absolute"));
+ }
+
Vector4 v;
if (properties->getVector4("region", &v))
{
@@ -96,10 +101,12 @@ bool Joystick::touchEvent(Touch::TouchEvent touchEvent, int x, int y, unsigned i
_region.y = y + _bounds.y - _region.height * 0.5f;
}
- if ((dx >= -_radius && dx <= _radius) && (dy >= -_radius && dy <= _radius) &&
- _contactIndex == INVALID_CONTACT_INDEX)
+ if ((dx >= -_radius && dx <= _radius) && (dy >= -_radius && dy <= _radius))
{
- _contactIndex = contactIndex;
+ _contactIndex = (int) contactIndex;
+
+ notifyListeners(Listener::PRESS);
+
_displacement.set(0.0f, 0.0f);
Vector2 value(0.0f, 0.0f);
@@ -111,67 +118,71 @@ bool Joystick::touchEvent(Touch::TouchEvent touchEvent, int x, int y, unsigned i
}
_state = ACTIVE;
+
+ return _consumeInputEvents;
+ }
+ else
+ {
+ _state = NORMAL;
}
+ break;
}
case Touch::TOUCH_MOVE:
{
- if (_contactIndex == contactIndex)
+ float dx = x - ((!_absolute) ? _region.x - _bounds.x : 0.0f) - _region.width * 0.5f;
+ float dy = -(y - ((!_absolute) ? _region.y - _bounds.y : 0.0f) - _region.height * 0.5f);
+ if (((dx * dx) + (dy * dy)) <= (_radius * _radius))
{
- float dx = x - ((!_absolute) ? _region.x - _bounds.x : 0.0f) - _region.width * 0.5f;
- float dy = -(y - ((!_absolute) ? _region.y - _bounds.y : 0.0f) - _region.height * 0.5f);
- if (((dx * dx) + (dy * dy)) <= (_radius * _radius))
- {
- GP_ASSERT(_radius);
- Vector2 value(dx, dy);
- value.scale(1.0f / _radius);
- if (_value != value)
- {
- _value.set(value);
- _dirty = true;
- notifyListeners(Control::Listener::VALUE_CHANGED);
- }
- }
- else
+ GP_ASSERT(_radius);
+ Vector2 value(dx, dy);
+ value.scale(1.0f / _radius);
+ if (_value != value)
{
- Vector2 value(dx, dy);
- value.normalize();
- value.scale(_radius);
- value.normalize();
- if (_value != value)
- {
- _value.set(value);
- _dirty = true;
- notifyListeners(Control::Listener::VALUE_CHANGED);
- }
+ _value.set(value);
+ _dirty = true;
+ notifyListeners(Control::Listener::VALUE_CHANGED);
}
-
- _displacement.set(dx, dy);
}
- }
- break;
- case Touch::TOUCH_RELEASE:
- {
- if (_contactIndex == contactIndex)
+ else
{
- // Reset displacement and direction vectors.
- _contactIndex = INVALID_CONTACT_INDEX;
- _displacement.set(0.0f, 0.0f);
-
- Vector2 value(0.0f, 0.0f);
+ Vector2 value(dx, dy);
+ value.normalize();
+ value.scale(_radius);
+ value.normalize();
if (_value != value)
{
_value.set(value);
_dirty = true;
notifyListeners(Control::Listener::VALUE_CHANGED);
}
+ }
- _state = NORMAL;
+ _displacement.set(dx, dy);
+
+ return _consumeInputEvents;
+ }
+ case Touch::TOUCH_RELEASE:
+ {
+ _contactIndex = INVALID_CONTACT_INDEX;
+
+ // Reset displacement and direction vectors.
+ _displacement.set(0.0f, 0.0f);
+
+ Vector2 value(0.0f, 0.0f);
+ if (_value != value)
+ {
+ _value.set(value);
+ _dirty = true;
+ notifyListeners(Control::Listener::VALUE_CHANGED);
}
+
+ _state = NORMAL;
+
+ return _consumeInputEvents;
}
- break;
}
- return Control::touchEvent(touchEvent, x, y, contactIndex);
+ return false;
}
void Joystick::update(const Control* container, const Vector2& offset)
@@ -238,4 +249,11 @@ void Joystick::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
spriteBatch->end();
}
+const char* Joystick::getType() const
+{
+ return "joystick";
+}
+
+
+
}
View
8 gameplay/src/Joystick.h
@@ -12,6 +12,7 @@ namespace gameplay
class Joystick : public Control
{
friend class Container;
+ friend class Gamepad;
public:
@@ -79,6 +80,11 @@ class Joystick : public Control
*/
inline bool isAbsolute() const;
+ /**
+ * @see Control::getType
+ */
+ const char* getType() const;
+
protected:
/**
@@ -145,11 +151,11 @@ class Joystick : public Control
Joystick(const Joystick& copy);
float _radius;
- unsigned int _contactIndex;
bool _absolute;
Vector2 _displacement;
Vector2 _value;
Rectangle _region;
+ int* _gamepadJoystickIndex;
};
}
View
7 gameplay/src/Label.cpp
@@ -112,4 +112,9 @@ void Label::drawText(const Rectangle& clip)
_dirty = false;
}
-}
+const char* Label::getType() const
+{
+ return "label";
+}
+
+}
View
7 gameplay/src/Label.h
@@ -58,6 +58,11 @@ class Label : public Control
const char* getText();
/**
+ * @see Control::getType
+ */
+ const char* getType() const;
+
+ /**
* Add a listener to be notified of specific events affecting
* this control. Event types can be OR'ed together.
* E.g. To listen to touch-press and touch-release events,
@@ -142,4 +147,4 @@ class Label : public Control
}
-#endif
+#endif
View
7 gameplay/src/RadioButton.cpp
@@ -198,4 +198,9 @@ void RadioButton::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
spriteBatch->draw(pos.x, pos.y, size.x, size.y, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color, _viewportClipBounds);
}
-}
+const char* RadioButton::getType() const
+{
+ return "radioButton";
+}
+
+}
View
7 gameplay/src/RadioButton.h
@@ -72,6 +72,11 @@ class RadioButton : public Button
const Vector2& getImageSize() const;
/**
+ * @see Control::getType
+ */
+ const char* getType() const;
+
+ /**
* Add a listener to be notified of specific events affecting
* this control. Event types can be OR'ed together.
* E.g. To listen to touch-press and touch-release events,
@@ -170,4 +175,4 @@ class RadioButton : public Button
}