axmol/core/base/CCController.h

254 lines
7.4 KiB
C
Raw Normal View History

/****************************************************************************
Copyright (c) 2014-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
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__
2022-07-15 19:17:01 +08:00
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID || AX_TARGET_PLATFORM == AX_PLATFORM_IOS || \
AX_TARGET_PLATFORM == AX_PLATFORM_MAC || AX_TARGET_PLATFORM == AX_PLATFORM_LINUX || \
2023-03-08 08:34:17 +08:00
defined(_WIN32))
2021-12-25 10:04:45 +08:00
# include "platform/CCPlatformMacros.h"
# include <string>
# include <vector>
# include <unordered_map>
NS_AX_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;
2015-03-24 14:12:58 +08:00
/**
* @addtogroup base
* @{
*/
2015-03-19 10:38:12 +08:00
/**
* @class Controller
* @brief A Controller object represents a connected physical game controller.
* @js NA
2015-03-19 10:38:12 +08:00
*/
2022-07-15 19:17:01 +08:00
class AX_DLL Controller
{
public:
/**
2015-03-19 10:38:12 +08:00
* Controllers' standard key
2014-07-11 10:56:09 +08:00
* Controller receives only standard key which contained within enum Key by default.
*/
2014-07-04 15:22:53 +08:00
enum Key
{
2015-12-01 09:01:42 +08:00
KEY_NONE = 0,
2014-07-04 15:22:53 +08:00
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,
2014-07-04 15:22:53 +08:00
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
};
2015-03-19 10:38:12 +08:00
/**
* @struct KeyStatus
*
2015-03-19 10:38:12 +08:00
*/
2014-07-04 15:22:53 +08:00
typedef struct _keyStatus
{
2015-03-19 10:38:12 +08:00
/** A Boolean value that indicates whether the key is considered pressed. */
2014-07-04 15:22:53 +08:00
bool isPressed;
2015-03-19 10:38:12 +08:00
/** The value of key.This value is used in conjunction with the isPressed parameter. */
2014-07-04 15:22:53 +08:00
float value;
2015-03-19 10:38:12 +08:00
/** A Boolean value that indicates whether the value of key is analog.
* If isAnalog is true, the key value might be a float from -1 to 1.
2015-03-19 10:38:12 +08:00
* If isAnalog is false, the key value would be contain one number: 0 or 1.
*/
2014-07-04 15:22:53 +08:00
bool isAnalog;
2021-12-25 10:04:45 +08:00
} KeyStatus;
2014-07-04 15:22:53 +08:00
static const int TAG_UNSET = -1;
2015-03-19 10:38:12 +08:00
/**
* Gets all Controller objects.
*/
2021-12-25 10:04:45 +08:00
static const std::vector<Controller*>& getAllController() { return s_allController; }
2015-03-19 10:38:12 +08:00
/**
2015-03-19 10:38:12 +08:00
* Gets a Controller object with tag.
*
2014-07-11 10:56:09 +08:00
* @param tag An identifier to find the controller.
2015-03-19 10:38:12 +08:00
* @return A Controller object.
2014-07-11 10:56:09 +08:00
*/
2014-07-04 15:22:53 +08:00
static Controller* getControllerByTag(int tag);
2015-03-19 10:38:12 +08:00
/**
* Gets a Controller object with device ID.
*
* @param deviceId A unique identifier to find the controller.
* @return A Controller object.
*/
static Controller* getControllerByDeviceId(int deviceId);
2015-03-19 10:38:12 +08:00
/**
* Start discovering new controllers.
*
* @warning The API has an empty implementation on Android.
2014-07-11 10:56:09 +08:00
*/
static void startDiscoveryController();
2015-03-19 10:38:12 +08:00
/**
2015-03-19 10:38:12 +08:00
* Stop the discovery process.
*
* @warning The API has an empty implementation on Android.
2014-07-11 10:56:09 +08:00
*/
static void stopDiscoveryController();
2015-03-19 10:38:12 +08:00
/**
* Gets the name of this Controller object.
*/
2021-12-26 23:26:34 +08:00
std::string_view getDeviceName() const { return _deviceName; }
2015-03-19 10:38:12 +08:00
/**
* Gets the Controller id.
*/
2021-12-25 10:04:45 +08:00
int getDeviceId() const { return _deviceId; }
2015-03-19 10:38:12 +08:00
/**
* Indicates whether the Controller is connected.
*/
2014-07-04 15:22:53 +08:00
bool isConnected() const;
2015-03-19 10:38:12 +08:00
/**
*
2015-03-19 10:38:12 +08:00
*/
2014-07-04 15:22:53 +08:00
const KeyStatus& getKeyStatus(int keyCode);
/**
2015-03-19 10:38:12 +08:00
* Activate receives key event from external key. e.g. back,menu.
2014-07-07 18:25:53 +08:00
* Controller receives only standard key which contained within enum Key by default.
2015-03-19 10:38:12 +08:00
*
2014-07-07 18:25:53 +08:00
* @warning The API only work on the android platform for support diversified game controller.
*
2015-03-19 10:38:12 +08:00
* @param externalKeyCode External key code.
* @param receive True if external key event on this controller should be receive, false otherwise.
2014-07-07 18:25:53 +08:00
*/
2021-12-25 10:04:45 +08:00
void receiveExternalKeyEvent(int externalKeyCode, bool receive);
2014-07-07 12:06:24 +08:00
/**
2015-03-19 10:38:12 +08:00
* Changes the tag that is used to identify the controller easily.
2014-07-11 10:56:09 +08:00
* @param tag A integer that identifies the controller.
*/
2021-12-25 10:04:45 +08:00
void setTag(int tag) { _controllerTag = tag; }
2015-03-19 10:38:12 +08:00
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.
*/
2021-12-25 10:04:45 +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;
2021-12-25 10:04:45 +08:00
EventController* _connectEvent;
EventController* _keyEvent;
EventController* _axisEvent;
2014-07-04 17:01:41 +08:00
2022-07-15 19:17:01 +08:00
# if (AX_TARGET_PLATFORM == AX_PLATFORM_LINUX || AX_TARGET_PLATFORM == AX_PLATFORM_WIN32)
2021-12-25 10:04:45 +08:00
// FIXME: Once GLFW 3.3 is bundled with cocos2d-x, remove these unordered
// maps. They won't be needed. We will only need to provide a mapping from
// the GLFW gamepad key codes to the Controller::Key.
// Attach the controller profiles from CCController-linux-win32.cpp to each
// of the Controller variables in order to minimize profile lookup time.
// Note: this increases memory usage unnecessarily since the same maps are
// already stored on ControllerImpl within the static member variable
// "s_controllerProfiles", but on these platforms the increase in memory
// usage is negligible. Peformance over memory optimization was
// consciously chosen.
2021-12-25 10:04:45 +08:00
std::unordered_map<int, int> _buttonInputMap;
std::unordered_map<int, int> _axisInputMap;
# endif
2014-07-04 15:22:53 +08:00
friend class ControllerImpl;
friend class EventListenerController;
};
2015-03-24 14:12:58 +08:00
// end group
/// @}
NS_AX_END
2017-10-23 11:18:14 +08:00
#endif
#endif /* defined(__cocos2d_libs__CCController__) */