axmol/cocos/base/CCController.h

170 lines
5.0 KiB
C
Raw Normal View History

/****************************************************************************
Copyright (c) 2014 cocos2d-x.org
Copyright (c) 2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __cocos2d_libs__CCController__
#define __cocos2d_libs__CCController__
2014-09-10 08:41:49 +08:00
#include "platform/CCPlatformMacros.h"
#include <string>
#include <vector>
2014-07-04 15:22:53 +08:00
#include <unordered_map>
NS_CC_BEGIN
class ControllerImpl;
2014-07-04 15:22:53 +08:00
class EventListenerController;
2014-07-04 17:01:41 +08:00
class EventController;
class EventDispatcher;
class Controller
{
public:
2014-07-11 10:56:09 +08:00
/** Controllers' standard key
* Controller receives only standard key which contained within enum Key by default.
*/
2014-07-04 15:22:53 +08:00
enum Key
{
JOYSTICK_LEFT_X = 1000,
JOYSTICK_LEFT_Y,
JOYSTICK_RIGHT_X,
JOYSTICK_RIGHT_Y,
BUTTON_A,
BUTTON_B,
BUTTON_C,
BUTTON_X,
BUTTON_Y,
BUTTON_Z,
BUTTON_DPAD_UP,
BUTTON_DPAD_DOWN,
BUTTON_DPAD_LEFT,
BUTTON_DPAD_RIGHT,
BUTTON_DPAD_CENTER,
BUTTON_LEFT_SHOULDER,
BUTTON_RIGHT_SHOULDER,
AXIS_LEFT_TRIGGER,
AXIS_RIGHT_TRIGGER,
BUTTON_LEFT_THUMBSTICK,
BUTTON_RIGHT_THUMBSTICK,
BUTTON_START,
BUTTON_SELECT,
2014-07-04 17:01:41 +08:00
BUTTON_PAUSE,
2014-07-04 15:22:53 +08:00
KEY_MAX
};
typedef struct _keyStatus
{
bool isPressed;
float value;
bool isAnalog;
}KeyStatus;
static const int TAG_UNSET = -1;
static const std::vector<Controller*>& getAllController(){ return s_allController;}
2014-07-11 10:56:09 +08:00
/** Gets a controller with its tag
* @param tag An identifier to find the controller.
*/
2014-07-04 15:22:53 +08:00
static Controller* getControllerByTag(int tag);
2014-07-11 10:56:09 +08:00
/** To start discovering new controllers
* @warning The API only work on the IOS platform.Empty implementation on Android
*/
static void startDiscoveryController();
2014-07-11 10:56:09 +08:00
/** End the discovery process
* @warning The API only work on the IOS platform.Empty implementation on Android
*/
static void stopDiscoveryController();
const std::string& getDeviceName() const { return _deviceName;}
2014-07-04 15:22:53 +08:00
int getDeviceId() const { return _deviceId;}
2014-07-04 15:22:53 +08:00
bool isConnected() const;
2014-07-04 15:22:53 +08:00
const KeyStatus& getKeyStatus(int keyCode);
2014-07-07 18:25:53 +08:00
/** Activate receives key event from external key. e.g. back,menu.
* Controller receives only standard key which contained within enum Key by default.
* @warning The API only work on the android platform for support diversified game controller.
*
* @param externalKeyCode external key code
* @param receive true if external key event on this controller should be receive, false otherwise.
*/
2014-07-07 12:06:24 +08:00
void receiveExternalKeyEvent(int externalKeyCode,bool receive);
2014-07-11 10:56:09 +08:00
/** Changes the tag that is used to identify the controller easily.
* @param tag A integer that identifies the controller.
*/
2014-07-04 15:22:53 +08:00
void setTag(int tag) { _controllerTag = tag;}
2014-07-11 10:56:09 +08:00
/**
* Returns a tag that is used to identify the controller easily.
*
* @return An integer that identifies the controller.
*/
2014-07-04 15:22:53 +08:00
int getTag() const { return _controllerTag;}
2014-07-04 15:22:53 +08:00
private:
static std::vector<Controller*> s_allController;
Controller();
virtual ~Controller();
2014-07-04 17:01:41 +08:00
void init();
void onConnected();
void onDisconnected();
void onButtonEvent(int keyCode, bool isPressed, float value, bool isAnalog);
void onAxisEvent(int axisCode, float value, bool isAnalog);
void registerListeners();
2014-07-04 17:01:41 +08:00
2014-07-04 15:22:53 +08:00
std::unordered_map<int, KeyStatus> _allKeyStatus;
std::unordered_map<int, KeyStatus> _allKeyPrevStatus;
2014-07-04 15:22:53 +08:00
std::string _deviceName;
int _deviceId;
int _controllerTag;
ControllerImpl* _impl;
2014-07-04 15:22:53 +08:00
2014-07-04 17:01:41 +08:00
EventDispatcher* _eventDispatcher;
EventController *_connectEvent;
EventController *_keyEvent;
EventController *_axisEvent;
2014-07-04 15:22:53 +08:00
friend class ControllerImpl;
friend class EventListenerController;
};
NS_CC_END
#endif /* defined(__cocos2d_libs__CCController__) */