Add controller support for Linux and Win32 (#17990)

* Add controller support for Linux and Win32

* Fix iOS and Mac linker issue

* Fixed documentation. Removed unnecessary whitespace.
This commit is contained in:
Wilson E. Alvarez 2017-07-09 22:30:04 -04:00 committed by minggo
parent 1c55ad104d
commit 5dd81e758a
10 changed files with 5011 additions and 26 deletions

View File

@ -1277,6 +1277,8 @@
C5384A7E1C85314A00EC986E /* UITabControlTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C5384A7C1C8530E200EC986E /* UITabControlTest.cpp */; };
C5384A7F1C8531A700EC986E /* UITabControlTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C5384A7C1C8530E200EC986E /* UITabControlTest.cpp */; };
C5384A801C85339200EC986E /* UITabControlTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C5384A7C1C8530E200EC986E /* UITabControlTest.cpp */; };
C8DE9E471F0EACE400B48C6B /* GameController.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 50FC3FA01D74C15B001C936A /* GameController.framework */; };
C8DE9E491F0EAECD00B48C6B /* GameController.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C8DE9E481F0EAECD00B48C6B /* GameController.framework */; };
D0FD03641A3B546400825BB5 /* AllocatorTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D0FD03621A3B546400825BB5 /* AllocatorTest.cpp */; };
D0FD03651A3B546400825BB5 /* AllocatorTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D0FD03621A3B546400825BB5 /* AllocatorTest.cpp */; };
D60AE43417F7FFE100757E4B /* CoreMotion.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D60AE43317F7FFE100757E4B /* CoreMotion.framework */; };
@ -2486,6 +2488,7 @@
C08689C018D370C90093E810 /* background.caf */ = {isa = PBXFileReference; lastKnownFileType = file; name = background.caf; path = "../tests/cpp-tests/Resources/background.caf"; sourceTree = "<group>"; };
C5384A7C1C8530E200EC986E /* UITabControlTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = UITabControlTest.cpp; path = UITabControlTest/UITabControlTest.cpp; sourceTree = "<group>"; };
C5384A7D1C8530E200EC986E /* UITabControlTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = UITabControlTest.h; path = UITabControlTest/UITabControlTest.h; sourceTree = "<group>"; };
C8DE9E481F0EAECD00B48C6B /* GameController.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GameController.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS10.3.sdk/System/Library/Frameworks/GameController.framework; sourceTree = DEVELOPER_DIR; };
D0FD03621A3B546400825BB5 /* AllocatorTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = AllocatorTest.cpp; path = AllocatorTest/AllocatorTest.cpp; sourceTree = "<group>"; };
D0FD03631A3B546400825BB5 /* AllocatorTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AllocatorTest.h; path = AllocatorTest/AllocatorTest.h; sourceTree = "<group>"; };
D60AE43317F7FFE100757E4B /* CoreMotion.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreMotion.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/CoreMotion.framework; sourceTree = DEVELOPER_DIR; };
@ -2599,6 +2602,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
C8DE9E471F0EACE400B48C6B /* GameController.framework in Frameworks */,
185663581B4155D3009EF2AE /* Security.framework in Frameworks */,
185664951B4246C9009EF2AE /* libsqlite3.dylib in Frameworks */,
185664941B4246BA009EF2AE /* AppKit.framework in Frameworks */,
@ -2622,6 +2626,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
C8DE9E491F0EAECD00B48C6B /* GameController.framework in Frameworks */,
294D7D8F1D0E50B2002CE7B7 /* CoreText.framework in Frameworks */,
4B5C5BB11CA114C700D55A14 /* Security.framework in Frameworks */,
18FC4D5F1B4257CD00B76F95 /* libsqlite3.dylib in Frameworks */,
@ -4541,6 +4546,7 @@
29B97323FDCFA39411CA2CEA /* Frameworks */ = {
isa = PBXGroup;
children = (
C8DE9E481F0EAECD00B48C6B /* GameController.framework */,
50FC3FA01D74C15B001C936A /* GameController.framework */,
29DC5A1E1CFFE5E700CF2930 /* CoreText.framework */,
4B5C5BB41CA114DB00D55A14 /* Security.framework */,

View File

@ -423,17 +423,21 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\openssl\prebuilt\win32\*.*" "$(OutDir)"
<ClCompile Include="..\base\ccCArray.cpp" />
<ClCompile Include="..\base\CCConfiguration.cpp" />
<ClCompile Include="..\base\CCConsole.cpp" />
<ClCompile Include="..\base\CCController.cpp" />
<ClCompile Include="..\base\CCController-linux-win32.cpp" />
<ClCompile Include="..\base\CCData.cpp" />
<ClCompile Include="..\base\CCDataVisitor.cpp" />
<ClCompile Include="..\base\CCDirector.cpp" />
<ClCompile Include="..\base\CCEvent.cpp" />
<ClCompile Include="..\base\CCEventAcceleration.cpp" />
<ClCompile Include="..\base\CCEventController.cpp" />
<ClCompile Include="..\base\CCEventCustom.cpp" />
<ClCompile Include="..\base\CCEventDispatcher.cpp" />
<ClCompile Include="..\base\CCEventFocus.cpp" />
<ClCompile Include="..\base\CCEventKeyboard.cpp" />
<ClCompile Include="..\base\CCEventListener.cpp" />
<ClCompile Include="..\base\CCEventListenerAcceleration.cpp" />
<ClCompile Include="..\base\CCEventListenerController.cpp" />
<ClCompile Include="..\base\CCEventListenerCustom.cpp" />
<ClCompile Include="..\base\CCEventListenerFocus.cpp" />
<ClCompile Include="..\base\CCEventListenerKeyboard.cpp" />
@ -1019,17 +1023,20 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\openssl\prebuilt\win32\*.*" "$(OutDir)"
<ClInclude Include="..\base\ccConfig.h" />
<ClInclude Include="..\base\CCConfiguration.h" />
<ClInclude Include="..\base\CCConsole.h" />
<ClInclude Include="..\base\CCController.h" />
<ClInclude Include="..\base\CCData.h" />
<ClInclude Include="..\base\CCDataVisitor.h" />
<ClInclude Include="..\base\CCDirector.h" />
<ClInclude Include="..\base\CCEvent.h" />
<ClInclude Include="..\base\CCEventAcceleration.h" />
<ClInclude Include="..\base\CCEventController.h" />
<ClInclude Include="..\base\CCEventCustom.h" />
<ClInclude Include="..\base\CCEventDispatcher.h" />
<ClInclude Include="..\base\CCEventFocus.h" />
<ClInclude Include="..\base\CCEventKeyboard.h" />
<ClInclude Include="..\base\CCEventListener.h" />
<ClInclude Include="..\base\CCEventListenerAcceleration.h" />
<ClInclude Include="..\base\CCEventListenerController.h" />
<ClInclude Include="..\base\CCEventListenerCustom.h" />
<ClInclude Include="..\base\CCEventListenerFocus.h" />
<ClInclude Include="..\base\CCEventListenerKeyboard.h" />
@ -1423,4 +1430,4 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\openssl\prebuilt\win32\*.*" "$(OutDir)"
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

File diff suppressed because it is too large Load Diff

View File

@ -25,7 +25,7 @@
#include "base/CCController.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
#include "base/CCEventDispatcher.h"
#include "base/CCEventController.h"
@ -47,6 +47,19 @@ Controller* Controller::getControllerByTag(int tag)
return nullptr;
}
Controller* Controller::getControllerByDeviceId(int deviceId)
{
for (auto controller : Controller::s_allController)
{
if (controller->_deviceId == deviceId)
{
return controller;
}
}
return nullptr;
}
void Controller::init()
{
for (int key = Key::JOYSTICK_LEFT_X; key < Key::KEY_MAX; ++key)
@ -112,4 +125,4 @@ void Controller::onAxisEvent(int axisCode, float value, bool isAnalog)
NS_CC_END
#endif // (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
#endif // (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)

View File

@ -1,18 +1,18 @@
/****************************************************************************
Copyright (c) 2014-2017 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
@ -47,17 +47,17 @@ class EventDispatcher;
* @brief A Controller object represents a connected physical game controller.
* @js NA
*/
class Controller
class CC_DLL Controller
{
public:
/**
/**
* Controllers' standard key
* Controller receives only standard key which contained within enum Key by default.
*/
enum Key
{
KEY_NONE = 0,
JOYSTICK_LEFT_X = 1000,
JOYSTICK_LEFT_Y,
JOYSTICK_RIGHT_X,
@ -69,7 +69,7 @@ public:
BUTTON_X,
BUTTON_Y,
BUTTON_Z,
BUTTON_DPAD_UP,
BUTTON_DPAD_DOWN,
BUTTON_DPAD_LEFT,
@ -94,7 +94,7 @@ public:
/**
* @struct KeyStatus
*
*
*/
typedef struct _keyStatus
{
@ -103,12 +103,12 @@ public:
/** The value of key.This value is used in conjunction with the isPressed parameter. */
float value;
/** 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.
* If isAnalog is true, the key value might be a float from -1 to 1.
* If isAnalog is false, the key value would be contain one number: 0 or 1.
*/
bool isAnalog;
}KeyStatus;
static const int TAG_UNSET = -1;
/**
@ -116,7 +116,7 @@ public:
*/
static const std::vector<Controller*>& getAllController(){ return s_allController;}
/**
/**
* Gets a Controller object with tag.
*
* @param tag An identifier to find the controller.
@ -124,17 +124,25 @@ public:
*/
static Controller* getControllerByTag(int tag);
/**
* 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);
/**
* Start discovering new controllers.
*
* @warning The API only work on the IOS platform. Empty implementation on other platform.
* @warning The API has an empty implementation on Android.
*/
static void startDiscoveryController();
/**
/**
* Stop the discovery process.
*
* @warning The API only work on the IOS platform.Empty implementation on other platform.
* @warning The API has an empty implementation on Android.
*/
static void stopDiscoveryController();
@ -154,11 +162,11 @@ public:
bool isConnected() const;
/**
*
*
*/
const KeyStatus& getKeyStatus(int keyCode);
/**
/**
* Activate receives key event from external key. e.g. back,menu.
* Controller receives only standard key which contained within enum Key by default.
*
@ -169,7 +177,7 @@ public:
*/
void receiveExternalKeyEvent(int externalKeyCode,bool receive);
/**
/**
* Changes the tag that is used to identify the controller easily.
* @param tag A integer that identifies the controller.
*/
@ -211,6 +219,20 @@ private:
EventController *_keyEvent;
EventController *_axisEvent;
#if ( CC_TARGET_PLATFORM == CC_PLATFORM_LINUX || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 )
// 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 unecessarily 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.
std::unordered_map<int,int> _buttonInputMap;
std::unordered_map<int,int> _axisInputMap;
#endif
friend class ControllerImpl;
friend class EventListenerController;
};

View File

@ -43,7 +43,7 @@ class EventListenerController;
/** @class EventController
* @brief Controller event.
*/
class EventController : public Event
class CC_DLL EventController : public Event
{
public:
/** ControllerEventType Controller event type.*/

View File

@ -31,7 +31,7 @@
#include "base/CCEventListenerKeyboard.h"
#include "base/CCEventListenerCustom.h"
#include "base/CCEventListenerFocus.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
#include "base/CCEventListenerController.h"
#endif
#include "2d/CCScene.h"
@ -94,7 +94,7 @@ static EventListener::ListenerID __getListenerID(Event* event)
// return UNKNOWN instead.
CCASSERT(false, "Don't call this method if the event is for touch.");
break;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
case Event::Type::GAME_CONTROLLER:
ret = EventListenerController::LISTENER_ID;
break;

View File

@ -43,7 +43,7 @@ class Controller;
* @param Controller event listener.
* @js NA
*/
class EventListenerController : public EventListener
class CC_DLL EventListenerController : public EventListener
{
public:
static const std::string LISTENER_ID;

View File

@ -9,6 +9,10 @@ elseif(ANDROID)
base/CCUserDefault-android.cpp
base/CCController-android.cpp
)
elseif(LINUX)
set(COCOS_BASE_SPECIFIC_SRC
base/CCController-linux-win32.cpp
)
endif()
set(COCOS_BASE_SRC

View File

@ -75,8 +75,11 @@ THE SOFTWARE.
#include "base/CCEventListenerFocus.h"
#include "base/CCEventListenerKeyboard.h"
#include "base/CCEventListenerMouse.h"
#include "base/CCEventListenerController.h"
#include "base/CCEventListenerTouch.h"
#include "base/CCEventMouse.h"
#include "base/CCEventController.h"
#include "base/CCController.h"
#include "base/CCEventTouch.h"
#include "base/CCEventType.h"