Merge pull request #7313 from Dhilan007/v3-gc-refactor
issue #4689: Refactoring game controller API.
|
@ -1025,23 +1025,12 @@
|
|||
373B912A187891FB00198F86 /* CCComBase.h in Headers */ = {isa = PBXBuildFile; fileRef = 373B910718787C0B00198F86 /* CCComBase.h */; };
|
||||
3E6176681960F89B00DE83F5 /* CCController-iOS.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3E6176551960F89B00DE83F5 /* CCController-iOS.mm */; };
|
||||
3E6176691960F89B00DE83F5 /* CCController.h in Headers */ = {isa = PBXBuildFile; fileRef = 3E6176561960F89B00DE83F5 /* CCController.h */; };
|
||||
3E61766A1960F89B00DE83F5 /* CCControllerAxisInput.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3E6176571960F89B00DE83F5 /* CCControllerAxisInput.cpp */; };
|
||||
3E61766B1960F89B00DE83F5 /* CCControllerAxisInput.h in Headers */ = {isa = PBXBuildFile; fileRef = 3E6176581960F89B00DE83F5 /* CCControllerAxisInput.h */; };
|
||||
3E61766C1960F89B00DE83F5 /* CCControllerButtonInput.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3E6176591960F89B00DE83F5 /* CCControllerButtonInput.cpp */; };
|
||||
3E61766D1960F89B00DE83F5 /* CCControllerButtonInput.h in Headers */ = {isa = PBXBuildFile; fileRef = 3E61765A1960F89B00DE83F5 /* CCControllerButtonInput.h */; };
|
||||
3E61766E1960F89B00DE83F5 /* CCControllerDirectionPad.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3E61765B1960F89B00DE83F5 /* CCControllerDirectionPad.cpp */; };
|
||||
3E61766F1960F89B00DE83F5 /* CCControllerDirectionPad.h in Headers */ = {isa = PBXBuildFile; fileRef = 3E61765C1960F89B00DE83F5 /* CCControllerDirectionPad.h */; };
|
||||
3E6176701960F89B00DE83F5 /* CCControllerElement.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3E61765D1960F89B00DE83F5 /* CCControllerElement.cpp */; };
|
||||
3E6176711960F89B00DE83F5 /* CCControllerElement.h in Headers */ = {isa = PBXBuildFile; fileRef = 3E61765E1960F89B00DE83F5 /* CCControllerElement.h */; };
|
||||
3E6176721960F89B00DE83F5 /* CCControllerThumbstick.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3E61765F1960F89B00DE83F5 /* CCControllerThumbstick.cpp */; };
|
||||
3E6176731960F89B00DE83F5 /* CCControllerThumbstick.h in Headers */ = {isa = PBXBuildFile; fileRef = 3E6176601960F89B00DE83F5 /* CCControllerThumbstick.h */; };
|
||||
3E6176741960F89B00DE83F5 /* CCEventController.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3E6176611960F89B00DE83F5 /* CCEventController.cpp */; };
|
||||
3E6176751960F89B00DE83F5 /* CCEventController.h in Headers */ = {isa = PBXBuildFile; fileRef = 3E6176621960F89B00DE83F5 /* CCEventController.h */; };
|
||||
3E6176761960F89B00DE83F5 /* CCEventListenerController.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3E6176631960F89B00DE83F5 /* CCEventListenerController.cpp */; };
|
||||
3E6176771960F89B00DE83F5 /* CCEventListenerController.h in Headers */ = {isa = PBXBuildFile; fileRef = 3E6176641960F89B00DE83F5 /* CCEventListenerController.h */; };
|
||||
3E6176781960F89B00DE83F5 /* CCGameController.h in Headers */ = {isa = PBXBuildFile; fileRef = 3E6176651960F89B00DE83F5 /* CCGameController.h */; };
|
||||
3E6176791960F89B00DE83F5 /* CCGamepad.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3E6176661960F89B00DE83F5 /* CCGamepad.cpp */; };
|
||||
3E61767A1960F89B00DE83F5 /* CCGamepad.h in Headers */ = {isa = PBXBuildFile; fileRef = 3E6176671960F89B00DE83F5 /* CCGamepad.h */; };
|
||||
3E61781D1966A5A300DE83F5 /* CCController.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3E61781C1966A5A300DE83F5 /* CCController.cpp */; };
|
||||
3EA0FB6B191C841D00B170C8 /* UIVideoPlayer.h in Headers */ = {isa = PBXBuildFile; fileRef = 3EA0FB69191C841D00B170C8 /* UIVideoPlayer.h */; };
|
||||
3EA0FB6C191C841D00B170C8 /* UIVideoPlayerIOS.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3EA0FB6A191C841D00B170C8 /* UIVideoPlayerIOS.mm */; };
|
||||
3EA47870195478E00068D9D1 /* CCBundleReader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3EA4786E195478E00068D9D1 /* CCBundleReader.cpp */; };
|
||||
|
@ -2370,23 +2359,12 @@
|
|||
37936A3E1869B76800E974DD /* writer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = writer.h; sourceTree = "<group>"; };
|
||||
3E6176551960F89B00DE83F5 /* CCController-iOS.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = "CCController-iOS.mm"; path = "../base/CCController-iOS.mm"; sourceTree = "<group>"; };
|
||||
3E6176561960F89B00DE83F5 /* CCController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCController.h; path = ../base/CCController.h; sourceTree = "<group>"; };
|
||||
3E6176571960F89B00DE83F5 /* CCControllerAxisInput.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CCControllerAxisInput.cpp; path = ../base/CCControllerAxisInput.cpp; sourceTree = "<group>"; };
|
||||
3E6176581960F89B00DE83F5 /* CCControllerAxisInput.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCControllerAxisInput.h; path = ../base/CCControllerAxisInput.h; sourceTree = "<group>"; };
|
||||
3E6176591960F89B00DE83F5 /* CCControllerButtonInput.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CCControllerButtonInput.cpp; path = ../base/CCControllerButtonInput.cpp; sourceTree = "<group>"; };
|
||||
3E61765A1960F89B00DE83F5 /* CCControllerButtonInput.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCControllerButtonInput.h; path = ../base/CCControllerButtonInput.h; sourceTree = "<group>"; };
|
||||
3E61765B1960F89B00DE83F5 /* CCControllerDirectionPad.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CCControllerDirectionPad.cpp; path = ../base/CCControllerDirectionPad.cpp; sourceTree = "<group>"; };
|
||||
3E61765C1960F89B00DE83F5 /* CCControllerDirectionPad.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCControllerDirectionPad.h; path = ../base/CCControllerDirectionPad.h; sourceTree = "<group>"; };
|
||||
3E61765D1960F89B00DE83F5 /* CCControllerElement.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CCControllerElement.cpp; path = ../base/CCControllerElement.cpp; sourceTree = "<group>"; };
|
||||
3E61765E1960F89B00DE83F5 /* CCControllerElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCControllerElement.h; path = ../base/CCControllerElement.h; sourceTree = "<group>"; };
|
||||
3E61765F1960F89B00DE83F5 /* CCControllerThumbstick.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CCControllerThumbstick.cpp; path = ../base/CCControllerThumbstick.cpp; sourceTree = "<group>"; };
|
||||
3E6176601960F89B00DE83F5 /* CCControllerThumbstick.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCControllerThumbstick.h; path = ../base/CCControllerThumbstick.h; sourceTree = "<group>"; };
|
||||
3E6176611960F89B00DE83F5 /* CCEventController.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CCEventController.cpp; path = ../base/CCEventController.cpp; sourceTree = "<group>"; };
|
||||
3E6176621960F89B00DE83F5 /* CCEventController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCEventController.h; path = ../base/CCEventController.h; sourceTree = "<group>"; };
|
||||
3E6176631960F89B00DE83F5 /* CCEventListenerController.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CCEventListenerController.cpp; path = ../base/CCEventListenerController.cpp; sourceTree = "<group>"; };
|
||||
3E6176641960F89B00DE83F5 /* CCEventListenerController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCEventListenerController.h; path = ../base/CCEventListenerController.h; sourceTree = "<group>"; };
|
||||
3E6176651960F89B00DE83F5 /* CCGameController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCGameController.h; path = ../base/CCGameController.h; sourceTree = "<group>"; };
|
||||
3E6176661960F89B00DE83F5 /* CCGamepad.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CCGamepad.cpp; path = ../base/CCGamepad.cpp; sourceTree = "<group>"; };
|
||||
3E6176671960F89B00DE83F5 /* CCGamepad.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CCGamepad.h; path = ../base/CCGamepad.h; sourceTree = "<group>"; };
|
||||
3E61781C1966A5A300DE83F5 /* CCController.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CCController.cpp; path = ../base/CCController.cpp; sourceTree = "<group>"; };
|
||||
3EA0FB69191C841D00B170C8 /* UIVideoPlayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIVideoPlayer.h; sourceTree = "<group>"; };
|
||||
3EA0FB6A191C841D00B170C8 /* UIVideoPlayerIOS.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = UIVideoPlayerIOS.mm; sourceTree = "<group>"; };
|
||||
3EA4786E195478E00068D9D1 /* CCBundleReader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCBundleReader.cpp; sourceTree = "<group>"; };
|
||||
|
@ -3187,25 +3165,14 @@
|
|||
1A5700A2180BC5E60088DEC7 /* base */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
3E61781C1966A5A300DE83F5 /* CCController.cpp */,
|
||||
3E6176551960F89B00DE83F5 /* CCController-iOS.mm */,
|
||||
3E6176561960F89B00DE83F5 /* CCController.h */,
|
||||
3E6176571960F89B00DE83F5 /* CCControllerAxisInput.cpp */,
|
||||
3E6176581960F89B00DE83F5 /* CCControllerAxisInput.h */,
|
||||
3E6176591960F89B00DE83F5 /* CCControllerButtonInput.cpp */,
|
||||
3E61765A1960F89B00DE83F5 /* CCControllerButtonInput.h */,
|
||||
3E61765B1960F89B00DE83F5 /* CCControllerDirectionPad.cpp */,
|
||||
3E61765C1960F89B00DE83F5 /* CCControllerDirectionPad.h */,
|
||||
3E61765D1960F89B00DE83F5 /* CCControllerElement.cpp */,
|
||||
3E61765E1960F89B00DE83F5 /* CCControllerElement.h */,
|
||||
3E61765F1960F89B00DE83F5 /* CCControllerThumbstick.cpp */,
|
||||
3E6176601960F89B00DE83F5 /* CCControllerThumbstick.h */,
|
||||
3E6176611960F89B00DE83F5 /* CCEventController.cpp */,
|
||||
3E6176621960F89B00DE83F5 /* CCEventController.h */,
|
||||
3E6176631960F89B00DE83F5 /* CCEventListenerController.cpp */,
|
||||
3E6176641960F89B00DE83F5 /* CCEventListenerController.h */,
|
||||
3E6176651960F89B00DE83F5 /* CCGameController.h */,
|
||||
3E6176661960F89B00DE83F5 /* CCGamepad.cpp */,
|
||||
3E6176671960F89B00DE83F5 /* CCGamepad.h */,
|
||||
299754F2193EC95400A54AC3 /* ObjectFactory.cpp */,
|
||||
299754F3193EC95400A54AC3 /* ObjectFactory.h */,
|
||||
50ABBDC11925AB6E00A911A9 /* atitc.cpp */,
|
||||
|
@ -5571,7 +5538,6 @@
|
|||
1A570068180BC5A10088DEC7 /* CCActionCamera.h in Headers */,
|
||||
1A57006C180BC5A10088DEC7 /* CCActionCatmullRom.h in Headers */,
|
||||
5034CA3A191D591100CE6051 /* ccShader_PositionColorLengthTexture.frag in Headers */,
|
||||
3E61766B1960F89B00DE83F5 /* CCControllerAxisInput.h in Headers */,
|
||||
50ABBEC41925AB6F00A911A9 /* CCVector.h in Headers */,
|
||||
50ABBE501925AB6F00A911A9 /* CCEventCustom.h in Headers */,
|
||||
1A570070180BC5A10088DEC7 /* CCActionEase.h in Headers */,
|
||||
|
@ -5608,7 +5574,6 @@
|
|||
50ABC0101926664800A911A9 /* CCFileUtils.h in Headers */,
|
||||
2905FA4318CF08D100240AA3 /* CocosGUI.h in Headers */,
|
||||
5034CA30191D591100CE6051 /* ccShader_PositionTexture.vert in Headers */,
|
||||
3E6176711960F89B00DE83F5 /* CCControllerElement.h in Headers */,
|
||||
50E6D33718E174130051CA34 /* UIHBox.h in Headers */,
|
||||
1A570111180BC8EE0088DEC7 /* CCDrawingPrimitives.h in Headers */,
|
||||
50E6D33F18E174130051CA34 /* UIVBox.h in Headers */,
|
||||
|
@ -5676,7 +5641,6 @@
|
|||
5034CA2C191D591100CE6051 /* ccShader_PositionTextureA8Color.vert in Headers */,
|
||||
50ABBE981925AB6F00A911A9 /* CCProtocols.h in Headers */,
|
||||
2905FA8B18CF08D100240AA3 /* UITextField.h in Headers */,
|
||||
3E6176731960F89B00DE83F5 /* CCControllerThumbstick.h in Headers */,
|
||||
50FCEBA618C72017004AD434 /* ListViewReader.h in Headers */,
|
||||
50ABBD431925AB0000A911A9 /* CCMathBase.h in Headers */,
|
||||
50ABBE441925AB6F00A911A9 /* CCDirector.h in Headers */,
|
||||
|
@ -5695,7 +5659,6 @@
|
|||
50ABBE741925AB6F00A911A9 /* CCEventListenerMouse.h in Headers */,
|
||||
2905FA8F18CF08D100240AA3 /* UIWidget.h in Headers */,
|
||||
50FCEB9A18C72017004AD434 /* CheckBoxReader.h in Headers */,
|
||||
3E61766D1960F89B00DE83F5 /* CCControllerButtonInput.h in Headers */,
|
||||
1A5702CB180BCE370088DEC7 /* CCTextFieldTTF.h in Headers */,
|
||||
2905FA7F18CF08D100240AA3 /* UIText.h in Headers */,
|
||||
1A5702ED180BCE750088DEC7 /* CCTileMapAtlas.h in Headers */,
|
||||
|
@ -5708,7 +5671,6 @@
|
|||
1A570303180BCE890088DEC7 /* CCParallaxNode.h in Headers */,
|
||||
50ABBE2A1925AB6F00A911A9 /* CCAutoreleasePool.h in Headers */,
|
||||
1A57030F180BCF190088DEC7 /* CCComponent.h in Headers */,
|
||||
3E61766F1960F89B00DE83F5 /* CCControllerDirectionPad.h in Headers */,
|
||||
1A570313180BCF190088DEC7 /* CCComponentContainer.h in Headers */,
|
||||
0634A4DF194B19E400E608AF /* CCNodeReader.h in Headers */,
|
||||
1A087AEB1860400400196EF5 /* edtaa3func.h in Headers */,
|
||||
|
@ -5815,7 +5777,6 @@
|
|||
1AD71ED0180E26E600808F54 /* Skeleton.h in Headers */,
|
||||
50ABBE541925AB6F00A911A9 /* CCEventDispatcher.h in Headers */,
|
||||
1AD71ED4180E26E600808F54 /* SkeletonData.h in Headers */,
|
||||
3E61767A1960F89B00DE83F5 /* CCGamepad.h in Headers */,
|
||||
1AD71ED8180E26E600808F54 /* SkeletonJson.h in Headers */,
|
||||
1A12775A18DFCC4F0005F345 /* CCTweenFunction.h in Headers */,
|
||||
1AD71EDC180E26E600808F54 /* Skin.h in Headers */,
|
||||
|
@ -6752,7 +6713,6 @@
|
|||
3E6176681960F89B00DE83F5 /* CCController-iOS.mm in Sources */,
|
||||
2905FA8D18CF08D100240AA3 /* UIWidget.cpp in Sources */,
|
||||
29E99D1F1957BA7000046604 /* CocoLoader.cpp in Sources */,
|
||||
3E61766C1960F89B00DE83F5 /* CCControllerButtonInput.cpp in Sources */,
|
||||
B29594B51926D5EC003EEF37 /* CCMeshCommand.cpp in Sources */,
|
||||
0634A4D5194B19E400E608AF /* CCActionTimelineCache.cpp in Sources */,
|
||||
50ABBE7E1925AB6F00A911A9 /* CCEventTouch.cpp in Sources */,
|
||||
|
@ -6792,6 +6752,7 @@
|
|||
1A57019E180BCB590088DEC7 /* CCFont.cpp in Sources */,
|
||||
503DD8E21926736A00CD74DD /* CCCommon.mm in Sources */,
|
||||
1A5701A2180BCB590088DEC7 /* CCFontAtlas.cpp in Sources */,
|
||||
3E61781D1966A5A300DE83F5 /* CCController.cpp in Sources */,
|
||||
50ABC00E1926664800A911A9 /* CCFileUtils.cpp in Sources */,
|
||||
50ABBE241925AB6F00A911A9 /* base64.cpp in Sources */,
|
||||
1A5701A6180BCB590088DEC7 /* CCFontAtlasCache.cpp in Sources */,
|
||||
|
@ -6831,13 +6792,11 @@
|
|||
1A570215180BCBF40088DEC7 /* CCRenderTexture.cpp in Sources */,
|
||||
1A570222180BCC1A0088DEC7 /* CCParticleBatchNode.cpp in Sources */,
|
||||
1A570226180BCC1A0088DEC7 /* CCParticleExamples.cpp in Sources */,
|
||||
3E6176791960F89B00DE83F5 /* CCGamepad.cpp in Sources */,
|
||||
1A57022A180BCC1A0088DEC7 /* CCParticleSystem.cpp in Sources */,
|
||||
B24AA98A195A675C007B4522 /* CCFastTMXTiledMap.cpp in Sources */,
|
||||
B24AA986195A675C007B4522 /* CCFastTMXLayer.cpp in Sources */,
|
||||
1A57022E180BCC1A0088DEC7 /* CCParticleSystemQuad.cpp in Sources */,
|
||||
50ABBD901925AB4100A911A9 /* CCGLProgramCache.cpp in Sources */,
|
||||
3E6176701960F89B00DE83F5 /* CCControllerElement.cpp in Sources */,
|
||||
2905FA5718CF08D100240AA3 /* UILayout.cpp in Sources */,
|
||||
2905FA7D18CF08D100240AA3 /* UIText.cpp in Sources */,
|
||||
50E6D33D18E174130051CA34 /* UIVBox.cpp in Sources */,
|
||||
|
@ -6853,7 +6812,6 @@
|
|||
1A5702C9180BCE370088DEC7 /* CCTextFieldTTF.cpp in Sources */,
|
||||
1A5702EB180BCE750088DEC7 /* CCTileMapAtlas.cpp in Sources */,
|
||||
1A5702EF180BCE750088DEC7 /* CCTMXLayer.cpp in Sources */,
|
||||
3E61766A1960F89B00DE83F5 /* CCControllerAxisInput.cpp in Sources */,
|
||||
1A5702F3180BCE750088DEC7 /* CCTMXObjectGroup.cpp in Sources */,
|
||||
50ABBD3D1925AB0000A911A9 /* CCGeometry.cpp in Sources */,
|
||||
50ABBECC1925AB6F00A911A9 /* s3tc.cpp in Sources */,
|
||||
|
@ -6966,7 +6924,6 @@
|
|||
50ABBE9A1925AB6F00A911A9 /* CCRef.cpp in Sources */,
|
||||
3E6176741960F89B00DE83F5 /* CCEventController.cpp in Sources */,
|
||||
50ABBE361925AB6F00A911A9 /* CCConsole.cpp in Sources */,
|
||||
3E61766E1960F89B00DE83F5 /* CCControllerDirectionPad.cpp in Sources */,
|
||||
503DD8E51926736A00CD74DD /* CCDirectorCaller.mm in Sources */,
|
||||
50ABBD5D1925AB0000A911A9 /* Vec3.cpp in Sources */,
|
||||
50ABC0121926664800A911A9 /* CCGLViewProtocol.cpp in Sources */,
|
||||
|
@ -7005,7 +6962,6 @@
|
|||
1A8C59D4180E930E00EF57C3 /* CCDecorativeDisplay.cpp in Sources */,
|
||||
1A8C59D8180E930E00EF57C3 /* CCDisplayFactory.cpp in Sources */,
|
||||
46C02E0818E91123004B7456 /* xxhash.c in Sources */,
|
||||
3E6176721960F89B00DE83F5 /* CCControllerThumbstick.cpp in Sources */,
|
||||
50ABBED01925AB6F00A911A9 /* TGAlib.cpp in Sources */,
|
||||
2905FA4118CF08D100240AA3 /* CocosGUI.cpp in Sources */,
|
||||
1A01C68518F57BE800EFE3A6 /* CCArray.cpp in Sources */,
|
||||
|
|
|
@ -827,8 +827,6 @@
|
|||
3E6177351960FB4000DE83F5 /* AppController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3E6176DE1960FA6300DE83F5 /* AppController.mm */; };
|
||||
3E6177361960FB5A00DE83F5 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E6176EE1960FA6300DE83F5 /* main.m */; };
|
||||
3E6177371960FB5E00DE83F5 /* RootViewController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3E6176F01960FA6300DE83F5 /* RootViewController.mm */; };
|
||||
3E6177391960FBB800DE83F5 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3E6177381960FBB800DE83F5 /* SystemConfiguration.framework */; };
|
||||
3E61773B1960FBC300DE83F5 /* CFNetwork.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3E61773A1960FBC300DE83F5 /* CFNetwork.framework */; };
|
||||
3E61773D1960FBD200DE83F5 /* GameController.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3E61773C1960FBD100DE83F5 /* GameController.framework */; };
|
||||
3E6177F41960FEFE00DE83F5 /* fonts in Resources */ = {isa = PBXBuildFile; fileRef = 3E6177F01960FEFE00DE83F5 /* fonts */; };
|
||||
3E6177F51960FEFE00DE83F5 /* ipad in Resources */ = {isa = PBXBuildFile; fileRef = 3E6177F11960FEFE00DE83F5 /* ipad */; };
|
||||
|
@ -2132,8 +2130,6 @@
|
|||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
3E61773D1960FBD200DE83F5 /* GameController.framework in Frameworks */,
|
||||
3E61773B1960FBC300DE83F5 /* CFNetwork.framework in Frameworks */,
|
||||
3E6177391960FBB800DE83F5 /* SystemConfiguration.framework in Frameworks */,
|
||||
3E6177211960FAED00DE83F5 /* libchipmunk iOS.a in Frameworks */,
|
||||
3E6177221960FAED00DE83F5 /* libcocos2dx iOS.a in Frameworks */,
|
||||
3E6177231960FAED00DE83F5 /* libCocosDenshion iOS.a in Frameworks */,
|
||||
|
@ -5728,6 +5724,7 @@
|
|||
"$(inherited)",
|
||||
CC_TARGET_OS_IPHONE,
|
||||
);
|
||||
GCC_PREPROCESSOR_DEFINITIONS_NOT_USED_IN_PRECOMPS = "";
|
||||
INFOPLIST_FILE = "$(SRCROOT)/../tests/game-controller-test/proj.ios/Info.plist";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 5.1.1;
|
||||
PRODUCT_NAME = "game-controller-test IOS";
|
||||
|
@ -5745,6 +5742,7 @@
|
|||
"$(inherited)",
|
||||
CC_TARGET_OS_IPHONE,
|
||||
);
|
||||
GCC_PREPROCESSOR_DEFINITIONS_NOT_USED_IN_PRECOMPS = "";
|
||||
INFOPLIST_FILE = "$(SRCROOT)/../tests/game-controller-test/proj.ios/Info.plist";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 5.1.1;
|
||||
PRODUCT_NAME = "game-controller-test IOS";
|
||||
|
|
|
@ -141,13 +141,8 @@ base/ccUTF8.cpp \
|
|||
base/ccUtils.cpp \
|
||||
base/etc1.cpp \
|
||||
base/s3tc.cpp \
|
||||
base/CCController.cpp \
|
||||
base/CCController-android.cpp \
|
||||
base/CCControllerAxisInput.cpp \
|
||||
base/CCControllerButtonInput.cpp \
|
||||
base/CCControllerDirectionPad.cpp \
|
||||
base/CCControllerElement.cpp \
|
||||
base/CCControllerThumbstick.cpp \
|
||||
base/CCGamepad.cpp \
|
||||
base/ObjectFactory.cpp \
|
||||
renderer/CCBatchCommand.cpp \
|
||||
renderer/CCCustomCommand.cpp \
|
||||
|
|
|
@ -26,258 +26,93 @@
|
|||
#include "CCController.h"
|
||||
|
||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
||||
|
||||
#include "CCGamepad.h"
|
||||
#include "CCControllerDirectionPad.h"
|
||||
#include "CCControllerButtonInput.h"
|
||||
#include "CCControllerAxisInput.h"
|
||||
#include "CCControllerThumbstick.h"
|
||||
|
||||
#include <functional>
|
||||
#include "ccMacros.h"
|
||||
#include "CCEventDispatcher.h"
|
||||
#include "CCEventController.h"
|
||||
#include "CCEventListenerController.h"
|
||||
#include "CCDirector.h"
|
||||
|
||||
#include "jni/JniHelper.h"
|
||||
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
enum class AndroidControllerCode
|
||||
{
|
||||
THUMBSTICK_LEFT_X = 100,
|
||||
THUMBSTICK_LEFT_Y = 101,
|
||||
THUMBSTICK_RIGHT_X = 102,
|
||||
THUMBSTICK_RIGHT_Y = 103,
|
||||
BUTTON_A = 110,
|
||||
BUTTON_B = 111,
|
||||
BUTTON_C = 112,
|
||||
BUTTON_X = 113,
|
||||
BUTTON_Y = 114,
|
||||
BUTTON_Z = 115,
|
||||
BUTTON_LEFT_SHOULDER = 120,
|
||||
BUTTON_RIGHT_SHOULDER = 121,
|
||||
BUTTON_LEFT_TRIGGER = 122,
|
||||
BUTTON_RIGHT_TRIGGER = 123,
|
||||
BUTTON_DPAD_UP = 130,
|
||||
BUTTON_DPAD_DOWN = 131,
|
||||
BUTTON_DPAD_LEFT = 132,
|
||||
BUTTON_DPAD_RIGHT = 133,
|
||||
BUTTON_DPAD_CENTER = 134,
|
||||
BUTTON_LEFT_THUMBSTICK = 140,
|
||||
BUTTON_RIGHT_THUMBSTICK = 141,
|
||||
BUTTON_START = 150,
|
||||
BUTTON_SELECT = 151,
|
||||
};
|
||||
|
||||
class ControllerImpl
|
||||
{
|
||||
public:
|
||||
ControllerImpl(Controller* controller)
|
||||
: _controller(controller)
|
||||
, _controllerID(-1)
|
||||
{
|
||||
}
|
||||
|
||||
static std::vector<Controller*>::iterator findController(const std::string& vendorName, int controllerID)
|
||||
static std::vector<Controller*>::iterator findController(const std::string& deviceName, int deviceId)
|
||||
{
|
||||
auto iter = std::find_if(Controller::_controllers.begin(), Controller::_controllers.end(), [&](Controller* controller){
|
||||
return (vendorName == controller->getVendorName()) && (controllerID == controller->_impl->_controllerID);
|
||||
auto iter = std::find_if(Controller::s_allController.begin(), Controller::s_allController.end(), [&](Controller* controller){
|
||||
return (deviceName == controller->_deviceName) && (deviceId == controller->_deviceId);
|
||||
});
|
||||
}
|
||||
|
||||
static void onConnected(const std::string& vendorName, int controllerID)
|
||||
static void onConnected(const std::string& deviceName, int deviceId)
|
||||
{
|
||||
// Check whether the controller is already connected.
|
||||
auto iter = findController(vendorName, controllerID);
|
||||
if (iter != Controller::_controllers.end())
|
||||
log("onConnected %s,%d", deviceName.c_str(),deviceId);
|
||||
|
||||
auto iter = findController(deviceName, deviceId);
|
||||
if (iter != Controller::s_allController.end())
|
||||
return;
|
||||
|
||||
log("onConnected new device");
|
||||
// It's a new controller being connected.
|
||||
auto controller = new cocos2d::Controller();
|
||||
controller->_vendorName = vendorName;
|
||||
Controller::_controllers.push_back(controller);
|
||||
controller->_impl->_controllerID = controllerID;
|
||||
EventController evt(EventController::ControllerEventType::CONNECTION, controller, true);
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&evt);
|
||||
controller->_deviceId = deviceId;
|
||||
controller->_deviceName = deviceName;
|
||||
Controller::s_allController.push_back(controller);
|
||||
|
||||
controller->onConnected();
|
||||
}
|
||||
|
||||
static void onDisconnected(const std::string& vendorName, int controllerID)
|
||||
static void onDisconnected(const std::string& deviceName, int deviceId)
|
||||
{
|
||||
auto iter = findController(vendorName, controllerID);
|
||||
if (iter == Controller::_controllers.end())
|
||||
log("onDisconnected %s,%d", deviceName.c_str(),deviceId);
|
||||
auto iter = findController(deviceName, deviceId);
|
||||
if (iter == Controller::s_allController.end())
|
||||
{
|
||||
log("Could not find the controller!");
|
||||
CCLOGERROR("Could not find the controller!");
|
||||
return;
|
||||
}
|
||||
|
||||
EventController evt(EventController::ControllerEventType::CONNECTION, *iter, false);
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&evt);
|
||||
|
||||
Controller::_controllers.erase(iter);
|
||||
(*iter)->onDisconnected();
|
||||
Controller::s_allController.erase(iter);
|
||||
}
|
||||
|
||||
void sendEventButton(ControllerButtonInput* button, bool isPressed, float value, bool isAnalog)
|
||||
static void onButtonEvent(const std::string& deviceName, int deviceId, int keyCode, bool isPressed, float value, bool isAnalog)
|
||||
{
|
||||
button->setPressed(isPressed);
|
||||
button->setValue(value);
|
||||
button->setAnalog(isAnalog);
|
||||
EventController evt(EventController::ControllerEventType::BUTTON_STATUS_CHANGED, _controller, button);
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&evt);
|
||||
log("onButtonEvent %s,%d", deviceName.c_str(),deviceId);
|
||||
auto iter = findController(deviceName, deviceId);
|
||||
if (iter == Controller::s_allController.end())
|
||||
{
|
||||
log("onButtonEvent new connect");
|
||||
onConnected(deviceName, deviceId);
|
||||
iter = findController(deviceName, deviceId);
|
||||
}
|
||||
|
||||
(*iter)->onButtonEvent(keyCode, isPressed, value, isAnalog);
|
||||
}
|
||||
|
||||
void sendEventAxis(ControllerAxisInput* axis, float value, bool isAnalog)
|
||||
static void onAxisEvent(const std::string& deviceName, int deviceId, int axisCode, float value, bool isAnalog)
|
||||
{
|
||||
axis->setValue(value);
|
||||
axis->setAnalog(isAnalog);
|
||||
EventController evt(EventController::ControllerEventType::AXIS_STATUS_CHANGED, _controller, axis);
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&evt);
|
||||
}
|
||||
|
||||
static void onButtonEvent(const std::string& vendorName, int controllerID, AndroidControllerCode btnCode, bool isPressed, float value, bool isAnalog)
|
||||
{
|
||||
auto iter = findController(vendorName, controllerID);
|
||||
if (iter == Controller::_controllers.end())
|
||||
auto iter = findController(deviceName, deviceId);
|
||||
if (iter == Controller::s_allController.end())
|
||||
{
|
||||
onConnected(vendorName, controllerID);
|
||||
iter = findController(vendorName, controllerID);
|
||||
onConnected(deviceName, deviceId);
|
||||
iter = findController(deviceName, deviceId);
|
||||
}
|
||||
|
||||
auto gamepad = (*iter)->getGamepad();
|
||||
auto thiz = (*iter)->getImpl();
|
||||
switch(btnCode)
|
||||
{
|
||||
case AndroidControllerCode::BUTTON_A:
|
||||
{
|
||||
thiz->sendEventButton(gamepad->getButtonA(), isPressed, value, isAnalog);
|
||||
}
|
||||
break;
|
||||
case AndroidControllerCode::BUTTON_B:
|
||||
{
|
||||
thiz->sendEventButton(gamepad->getButtonB(), isPressed, value, isAnalog);
|
||||
}
|
||||
break;
|
||||
case AndroidControllerCode::BUTTON_X:
|
||||
{
|
||||
thiz->sendEventButton(gamepad->getButtonX(), isPressed, value, isAnalog);
|
||||
}
|
||||
break;
|
||||
case AndroidControllerCode::BUTTON_Y:
|
||||
{
|
||||
thiz->sendEventButton(gamepad->getButtonY(), isPressed, value, isAnalog);
|
||||
}
|
||||
break;
|
||||
case AndroidControllerCode::BUTTON_LEFT_SHOULDER:
|
||||
{
|
||||
thiz->sendEventButton(gamepad->getLeftShoulder(), isPressed, value, isAnalog);
|
||||
}
|
||||
break;
|
||||
case AndroidControllerCode::BUTTON_RIGHT_SHOULDER:
|
||||
{
|
||||
thiz->sendEventButton(gamepad->getRightShoulder(), isPressed, value, isAnalog);
|
||||
}
|
||||
break;
|
||||
case AndroidControllerCode::BUTTON_LEFT_TRIGGER:
|
||||
{
|
||||
thiz->sendEventButton(gamepad->getLeftTrigger(), isPressed, value, isAnalog);
|
||||
}
|
||||
break;
|
||||
case AndroidControllerCode::BUTTON_RIGHT_TRIGGER:
|
||||
{
|
||||
thiz->sendEventButton(gamepad->getRightTrigger(), isPressed, value, isAnalog);
|
||||
}
|
||||
break;
|
||||
case AndroidControllerCode::BUTTON_DPAD_UP:
|
||||
{
|
||||
thiz->sendEventButton(gamepad->getDirectionPad()->getUp(), isPressed, value, isAnalog);
|
||||
}
|
||||
break;
|
||||
case AndroidControllerCode::BUTTON_DPAD_DOWN:
|
||||
{
|
||||
thiz->sendEventButton(gamepad->getDirectionPad()->getDown(), isPressed, value, isAnalog);
|
||||
}
|
||||
break;
|
||||
case AndroidControllerCode::BUTTON_DPAD_LEFT:
|
||||
{
|
||||
thiz->sendEventButton(gamepad->getDirectionPad()->getLeft(), isPressed, value, isAnalog);
|
||||
}
|
||||
break;
|
||||
case AndroidControllerCode::BUTTON_DPAD_RIGHT:
|
||||
{
|
||||
thiz->sendEventButton(gamepad->getDirectionPad()->getRight(), isPressed, value, isAnalog);
|
||||
}
|
||||
break;
|
||||
case AndroidControllerCode::BUTTON_START:
|
||||
{
|
||||
thiz->sendEventButton(gamepad->getButtonStart(), isPressed, value, isAnalog);
|
||||
}
|
||||
break;
|
||||
case AndroidControllerCode::BUTTON_SELECT:
|
||||
{
|
||||
thiz->sendEventButton(gamepad->getButtonSelect(), isPressed, value, isAnalog);
|
||||
}
|
||||
break;
|
||||
case AndroidControllerCode::BUTTON_LEFT_THUMBSTICK:
|
||||
{
|
||||
thiz->sendEventButton(gamepad->getLeftThumbstick()->getButton(), isPressed, value, isAnalog);
|
||||
}
|
||||
break;
|
||||
case AndroidControllerCode::BUTTON_RIGHT_THUMBSTICK:
|
||||
{
|
||||
thiz->sendEventButton(gamepad->getRightThumbstick()->getButton(), isPressed, value, isAnalog);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
// CCASSERT(false, "Invalid controller button code!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void onAxisEvent(const std::string& vendorName, int controllerID, AndroidControllerCode axisCode, float value, bool isAnalog)
|
||||
{
|
||||
// log("vendorName: %s, controller id: %d, axis: %d, value: %f", vendorName.c_str(), controllerID, axisCode, value);
|
||||
auto iter = findController(vendorName, controllerID);
|
||||
if (iter == Controller::_controllers.end())
|
||||
{
|
||||
onConnected(vendorName, controllerID);
|
||||
iter = findController(vendorName, controllerID);
|
||||
}
|
||||
auto gamepad = (*iter)->getGamepad();
|
||||
auto thiz = (*iter)->getImpl();
|
||||
switch (axisCode)
|
||||
{
|
||||
case AndroidControllerCode::THUMBSTICK_LEFT_X:
|
||||
thiz->sendEventAxis(gamepad->getLeftThumbstick()->getAxisX(), value, isAnalog);
|
||||
break;
|
||||
case AndroidControllerCode::THUMBSTICK_LEFT_Y:
|
||||
thiz->sendEventAxis(gamepad->getLeftThumbstick()->getAxisY(), value, isAnalog);
|
||||
break;
|
||||
case AndroidControllerCode::THUMBSTICK_RIGHT_X:
|
||||
thiz->sendEventAxis(gamepad->getRightThumbstick()->getAxisX(), value, isAnalog);
|
||||
break;
|
||||
case AndroidControllerCode::THUMBSTICK_RIGHT_Y:
|
||||
thiz->sendEventAxis(gamepad->getRightThumbstick()->getAxisY(), value, isAnalog);
|
||||
break;
|
||||
default:
|
||||
CCASSERT(false, "Invalid controller axis code!");
|
||||
break;
|
||||
}
|
||||
(*iter)->onAxisEvent(axisCode, value, isAnalog);
|
||||
}
|
||||
|
||||
private:
|
||||
Controller* _controller;
|
||||
int _controllerID;
|
||||
};
|
||||
|
||||
|
||||
std::vector<Controller*> Controller::_controllers;
|
||||
|
||||
const std::vector<Controller*>& Controller::getControllers()
|
||||
{
|
||||
return _controllers;
|
||||
}
|
||||
|
||||
void Controller::startDiscoveryController()
|
||||
{
|
||||
// Empty implementation on Android
|
||||
|
@ -288,9 +123,17 @@ void Controller::stopDiscoveryController()
|
|||
// Empty implementation on Android
|
||||
}
|
||||
|
||||
const std::string& Controller::getVendorName()
|
||||
Controller::~Controller()
|
||||
{
|
||||
delete _impl;
|
||||
|
||||
delete _connectEvent;
|
||||
delete _keyEvent;
|
||||
delete _axisEvent;
|
||||
}
|
||||
|
||||
void Controller::registerListeners()
|
||||
{
|
||||
return _vendorName;
|
||||
}
|
||||
|
||||
bool Controller::isConnected() const
|
||||
|
@ -301,61 +144,40 @@ bool Controller::isConnected() const
|
|||
return true;
|
||||
}
|
||||
|
||||
int Controller::getPlayerIndex() const
|
||||
{
|
||||
return _playerIndex;
|
||||
}
|
||||
|
||||
void Controller::setPlayerIndex(int playerIndex)
|
||||
{
|
||||
_playerIndex = playerIndex;
|
||||
}
|
||||
|
||||
Gamepad* Controller::getGamepad() const
|
||||
{
|
||||
return _gamepad;
|
||||
}
|
||||
|
||||
Controller::Controller()
|
||||
: _playerIndex(PLAYER_INDEX_UNSET)
|
||||
, _gamepad(new Gamepad)
|
||||
: _controllerTag(TAG_UNSET)
|
||||
, _impl(new ControllerImpl(this))
|
||||
, _connectEvent(nullptr)
|
||||
, _keyEvent(nullptr)
|
||||
, _axisEvent(nullptr)
|
||||
{
|
||||
_gamepad->_controller = this;
|
||||
}
|
||||
|
||||
Controller::~Controller()
|
||||
{
|
||||
CC_SAFE_DELETE(_impl);
|
||||
CC_SAFE_DELETE(_gamepad);
|
||||
init();
|
||||
}
|
||||
|
||||
NS_CC_END
|
||||
|
||||
extern "C" {
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_GameControllerAdapter_nativeControllerConnected(JNIEnv* env, jobject thiz, jstring vendorName, jint controllerID)
|
||||
void Java_org_cocos2dx_lib_GameControllerAdapter_nativeControllerConnected(JNIEnv* env, jobject thiz, jstring deviceName, jint controllerID)
|
||||
{
|
||||
CCLOG("controller id: %d connected!", controllerID);
|
||||
cocos2d::ControllerImpl::onConnected(cocos2d::JniHelper::jstring2string(vendorName), controllerID);
|
||||
cocos2d::ControllerImpl::onConnected(cocos2d::JniHelper::jstring2string(deviceName), controllerID);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_GameControllerAdapter_nativeControllerDisconnected(JNIEnv* env, jobject thiz, jstring vendorName, jint controllerID)
|
||||
void Java_org_cocos2dx_lib_GameControllerAdapter_nativeControllerDisconnected(JNIEnv* env, jobject thiz, jstring deviceName, jint controllerID)
|
||||
{
|
||||
CCLOG("controller id: %d disconnected!", controllerID);
|
||||
cocos2d::ControllerImpl::onDisconnected(cocos2d::JniHelper::jstring2string(vendorName), controllerID);
|
||||
cocos2d::ControllerImpl::onDisconnected(cocos2d::JniHelper::jstring2string(deviceName), controllerID);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_GameControllerAdapter_nativeControllerButtonEvent(JNIEnv* env, jobject thiz, jstring vendorName, jint controllerID, jint button, jboolean isPressed, jfloat value, jboolean isAnalog)
|
||||
void Java_org_cocos2dx_lib_GameControllerAdapter_nativeControllerButtonEvent(JNIEnv* env, jobject thiz, jstring deviceName, jint controllerID, jint button, jboolean isPressed, jfloat value, jboolean isAnalog)
|
||||
{
|
||||
CCLOG("controller id: %d, btn code: %d, isPressed: %d, value: %f, isAnalog:%d", controllerID, button, (int)isPressed, value, (int)isAnalog);
|
||||
cocos2d::ControllerImpl::onButtonEvent(cocos2d::JniHelper::jstring2string(vendorName), controllerID, static_cast<cocos2d::AndroidControllerCode>(button), isPressed, value, isAnalog);
|
||||
cocos2d::ControllerImpl::onButtonEvent(cocos2d::JniHelper::jstring2string(deviceName), controllerID, button, isPressed, value, isAnalog);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_GameControllerAdapter_nativeControllerAxisEvent(JNIEnv* env, jobject thiz, jstring vendorName, jint controllerID, jint axis, jfloat value, jboolean isAnalog)
|
||||
void Java_org_cocos2dx_lib_GameControllerAdapter_nativeControllerAxisEvent(JNIEnv* env, jobject thiz, jstring deviceName, jint controllerID, jint axis, jfloat value, jboolean isAnalog)
|
||||
{
|
||||
// CCLOG("controller id: %d, axis code: %d, value: %f, isAnalog:%d", controllerID, axis, value, (int)isAnalog);
|
||||
cocos2d::ControllerImpl::onAxisEvent(cocos2d::JniHelper::jstring2string(vendorName), controllerID, static_cast<cocos2d::AndroidControllerCode>(axis), value, isAnalog);
|
||||
cocos2d::ControllerImpl::onAxisEvent(cocos2d::JniHelper::jstring2string(deviceName), controllerID, axis, value, isAnalog);
|
||||
}
|
||||
|
||||
} // extern "C" {
|
||||
|
|
|
@ -27,17 +27,12 @@
|
|||
#include "base/CCPlatformConfig.h"
|
||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
||||
|
||||
#include "CCGamepad.h"
|
||||
#include "CCControllerDirectionPad.h"
|
||||
#include "CCControllerButtonInput.h"
|
||||
#include "CCControllerAxisInput.h"
|
||||
#include "CCControllerThumbstick.h"
|
||||
|
||||
#include "ccMacros.h"
|
||||
#include "CCEventDispatcher.h"
|
||||
#include "CCEventController.h"
|
||||
#include "CCEventListenerController.h"
|
||||
#include "CCDirector.h"
|
||||
#include "CCLabel.h"
|
||||
|
||||
#import <GameController/GameController.h>
|
||||
|
||||
|
@ -105,90 +100,51 @@ static GCControllerConnectionEventHandler* __instance = nil;
|
|||
|
||||
NS_CC_BEGIN
|
||||
|
||||
#define sendEventButton(dstID, srcID) \
|
||||
dstID->setPressed(srcID.isPressed); \
|
||||
dstID->setValue(srcID.value); \
|
||||
dstID->setAnalog(srcID.isAnalog); \
|
||||
EventController evt(EventController::ControllerEventType::BUTTON_STATUS_CHANGED, _gamepad->_controller, dstID); \
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&evt);
|
||||
|
||||
|
||||
#define sendEventAxis(dstID, srcID) \
|
||||
\
|
||||
dstID->setValue(srcID.value); \
|
||||
dstID->setAnalog(srcID.isAnalog); \
|
||||
\
|
||||
EventController evt(EventController::ControllerEventType::AXIS_STATUS_CHANGED, _gamepad->_controller, dstID); \
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&evt);
|
||||
|
||||
class ControllerImpl
|
||||
{
|
||||
public:
|
||||
ControllerImpl(Controller* controller)
|
||||
: _controller(controller)
|
||||
, _gcController(nil)
|
||||
, _lazyRegisterListener(true)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
~ControllerImpl()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Controller* _controller;
|
||||
GCController* _gcController;
|
||||
bool _lazyRegisterListener;
|
||||
};
|
||||
|
||||
std::vector<Controller*> Controller::_controllers;
|
||||
|
||||
const std::vector<Controller*>& Controller::getControllers()
|
||||
{
|
||||
return _controllers;
|
||||
}
|
||||
std::vector<Controller*> Controller::s_allController;
|
||||
|
||||
void Controller::startDiscoveryController()
|
||||
{
|
||||
[GCController startWirelessControllerDiscoveryWithCompletionHandler: nil];
|
||||
|
||||
[[GCControllerConnectionEventHandler getInstance] observerConnection: ^(GCController* gcController) {
|
||||
|
||||
auto controller = new Controller();
|
||||
controller->_impl->_gcController = gcController;
|
||||
controller->_deviceName = [gcController.vendorName UTF8String];
|
||||
|
||||
gcController.controllerPausedHandler = ^(GCController* gcCon){
|
||||
|
||||
auto iter = std::find_if(_controllers.begin(), _controllers.end(), [gcCon](Controller* c){ return c->_impl->_gcController == gcCon; });
|
||||
|
||||
CCASSERT(iter != _controllers.end(), "Could not find the controller");
|
||||
|
||||
auto button = (*iter)->getGamepad()->getButtonPause();
|
||||
button->setPressed(true);
|
||||
EventController evt(EventController::ControllerEventType::BUTTON_STATUS_CHANGED, (*iter), button);
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&evt);
|
||||
|
||||
// Reset the pause button status to unpressed.
|
||||
button->setPressed(false);
|
||||
};
|
||||
s_allController.push_back(controller);
|
||||
|
||||
_controllers.push_back(controller);
|
||||
controller->registerListeners();
|
||||
controller->getDeviceName();
|
||||
|
||||
|
||||
EventController evt(EventController::ControllerEventType::CONNECTION, controller, true);
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&evt);
|
||||
controller->onConnected();
|
||||
|
||||
} disconnection: ^(GCController* gcController) {
|
||||
auto iter = std::find_if(s_allController.begin(), s_allController.end(), [gcController](Controller* c){ return c->_impl->_gcController == gcController; });
|
||||
|
||||
auto iter = std::find_if(_controllers.begin(), _controllers.end(), [gcController](Controller* c){ return c->_impl->_gcController == gcController; });
|
||||
if(iter == s_allController.end())
|
||||
{
|
||||
log("disconnect:Could not find the controller");
|
||||
return;
|
||||
}
|
||||
|
||||
CCASSERT(iter != _controllers.end(), "Could not find the controller");
|
||||
(*iter)->onDisconnected();
|
||||
s_allController.erase(iter);
|
||||
|
||||
EventController evt(EventController::ControllerEventType::CONNECTION, *iter, false);
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&evt);
|
||||
|
||||
delete (*iter);
|
||||
_controllers.erase(iter);
|
||||
}];
|
||||
}
|
||||
|
||||
|
@ -198,26 +154,156 @@ void Controller::stopDiscoveryController()
|
|||
}
|
||||
|
||||
Controller::Controller()
|
||||
: _controllerTag(TAG_UNSET)
|
||||
, _impl(new ControllerImpl(this))
|
||||
, _connectEvent(nullptr)
|
||||
, _keyEvent(nullptr)
|
||||
, _axisEvent(nullptr)
|
||||
, _deviceId(0)
|
||||
{
|
||||
_playerIndex = PLAYER_INDEX_UNSET;
|
||||
_gamepad = new Gamepad();
|
||||
_gamepad->_controller = this;
|
||||
_impl = new ControllerImpl(this);
|
||||
init();
|
||||
}
|
||||
|
||||
Controller::~Controller()
|
||||
{
|
||||
CC_SAFE_DELETE(_impl);
|
||||
CC_SAFE_DELETE(_gamepad);
|
||||
delete _impl;
|
||||
|
||||
delete _connectEvent;
|
||||
delete _keyEvent;
|
||||
delete _axisEvent;
|
||||
}
|
||||
|
||||
const std::string& Controller::getVendorName()
|
||||
void Controller::registerListeners()
|
||||
{
|
||||
if (_vendorName.empty())
|
||||
if (_impl->_gcController.extendedGamepad != nil)
|
||||
{
|
||||
_vendorName = [_impl->_gcController.vendorName UTF8String];
|
||||
_impl->_gcController.extendedGamepad.dpad.up.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
||||
onButtonEvent(Key::BUTTON_DPAD_UP, pressed, value, button.isAnalog);
|
||||
};
|
||||
|
||||
_impl->_gcController.extendedGamepad.dpad.down.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
||||
onButtonEvent(Key::BUTTON_DPAD_DOWN, pressed, value, button.isAnalog);
|
||||
};
|
||||
|
||||
_impl->_gcController.extendedGamepad.dpad.left.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
||||
onButtonEvent(Key::BUTTON_DPAD_LEFT, pressed, value, button.isAnalog);
|
||||
};
|
||||
|
||||
_impl->_gcController.extendedGamepad.dpad.right.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
||||
onButtonEvent(Key::BUTTON_DPAD_RIGHT, pressed, value, button.isAnalog);
|
||||
};
|
||||
|
||||
_impl->_gcController.extendedGamepad.leftThumbstick.xAxis.valueChangedHandler = ^(GCControllerAxisInput *axis, float value){
|
||||
onAxisEvent(Key::JOYSTICK_LEFT_X, value, axis.isAnalog);
|
||||
};
|
||||
|
||||
_impl->_gcController.extendedGamepad.leftThumbstick.yAxis.valueChangedHandler = ^(GCControllerAxisInput *axis, float value){
|
||||
onAxisEvent(Key::JOYSTICK_LEFT_Y, value, axis.isAnalog);
|
||||
};
|
||||
|
||||
_impl->_gcController.extendedGamepad.rightThumbstick.xAxis.valueChangedHandler = ^(GCControllerAxisInput *axis, float value){
|
||||
onAxisEvent(Key::JOYSTICK_RIGHT_X, value, axis.isAnalog);
|
||||
};
|
||||
|
||||
_impl->_gcController.extendedGamepad.rightThumbstick.yAxis.valueChangedHandler = ^(GCControllerAxisInput *axis, float value){
|
||||
onAxisEvent(Key::JOYSTICK_RIGHT_Y, value, axis.isAnalog);
|
||||
};
|
||||
|
||||
_impl->_gcController.extendedGamepad.valueChangedHandler = ^(GCExtendedGamepad *gamepad, GCControllerElement *element){
|
||||
if (element == gamepad.buttonA)
|
||||
{
|
||||
onButtonEvent(Key::BUTTON_A, gamepad.buttonA.isPressed, gamepad.buttonA.value, gamepad.buttonA.isAnalog);
|
||||
}
|
||||
else if (element == gamepad.buttonB)
|
||||
{
|
||||
onButtonEvent(Key::BUTTON_B, gamepad.buttonB.isPressed, gamepad.buttonB.value, gamepad.buttonB.isAnalog);
|
||||
}
|
||||
else if (element == gamepad.buttonX)
|
||||
{
|
||||
onButtonEvent(Key::BUTTON_X, gamepad.buttonX.isPressed, gamepad.buttonX.value, gamepad.buttonX.isAnalog);
|
||||
}
|
||||
else if (element == gamepad.buttonY)
|
||||
{
|
||||
onButtonEvent(Key::BUTTON_Y, gamepad.buttonY.isPressed, gamepad.buttonY.value, gamepad.buttonY.isAnalog);
|
||||
}
|
||||
else if (element == gamepad.leftShoulder)
|
||||
{
|
||||
onButtonEvent(Key::BUTTON_LEFT_SHOULDER, gamepad.leftShoulder.isPressed, gamepad.leftShoulder.value, gamepad.leftShoulder.isAnalog);
|
||||
}
|
||||
else if (element == gamepad.rightShoulder)
|
||||
{
|
||||
onButtonEvent(Key::BUTTON_RIGHT_SHOULDER, gamepad.rightShoulder.isPressed, gamepad.rightShoulder.value, gamepad.rightShoulder.isAnalog);
|
||||
}
|
||||
else if (element == gamepad.leftTrigger)
|
||||
{
|
||||
onAxisEvent(Key::AXIS_LEFT_TRIGGER, gamepad.leftTrigger.value, gamepad.leftTrigger.isAnalog);
|
||||
}
|
||||
else if (element == gamepad.rightTrigger)
|
||||
{
|
||||
onAxisEvent(Key::AXIS_RIGHT_TRIGGER, gamepad.rightTrigger.value, gamepad.rightTrigger.isAnalog);
|
||||
}
|
||||
};
|
||||
}
|
||||
return _vendorName;
|
||||
else
|
||||
{
|
||||
_impl->_gcController.gamepad.dpad.up.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
||||
onButtonEvent(Key::BUTTON_DPAD_UP, pressed, value, button.isAnalog);
|
||||
};
|
||||
|
||||
_impl->_gcController.gamepad.dpad.down.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
||||
onButtonEvent(Key::BUTTON_DPAD_DOWN, pressed, value, button.isAnalog);
|
||||
};
|
||||
|
||||
_impl->_gcController.gamepad.dpad.left.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
||||
onButtonEvent(Key::BUTTON_DPAD_LEFT, pressed, value, button.isAnalog);
|
||||
};
|
||||
|
||||
_impl->_gcController.gamepad.dpad.right.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
||||
onButtonEvent(Key::BUTTON_DPAD_RIGHT, pressed, value, button.isAnalog);
|
||||
};
|
||||
|
||||
_impl->_gcController.gamepad.valueChangedHandler = ^(GCGamepad *gamepad, GCControllerElement *element){
|
||||
|
||||
if (element == gamepad.buttonA)
|
||||
{
|
||||
onButtonEvent(Key::BUTTON_A, gamepad.buttonA.isPressed, gamepad.buttonA.value, gamepad.buttonA.isAnalog);
|
||||
}
|
||||
else if (element == gamepad.buttonB)
|
||||
{
|
||||
onButtonEvent(Key::BUTTON_B, gamepad.buttonB.isPressed, gamepad.buttonB.value, gamepad.buttonB.isAnalog);
|
||||
}
|
||||
else if (element == gamepad.buttonX)
|
||||
{
|
||||
onButtonEvent(Key::BUTTON_X, gamepad.buttonX.isPressed, gamepad.buttonX.value, gamepad.buttonX.isAnalog);
|
||||
}
|
||||
else if (element == gamepad.buttonY)
|
||||
{
|
||||
onButtonEvent(Key::BUTTON_Y, gamepad.buttonY.isPressed, gamepad.buttonY.value, gamepad.buttonY.isAnalog);
|
||||
}
|
||||
else if (element == gamepad.leftShoulder)
|
||||
{
|
||||
onButtonEvent(Key::BUTTON_LEFT_SHOULDER, gamepad.leftShoulder.isPressed, gamepad.leftShoulder.value, gamepad.leftShoulder.isAnalog);
|
||||
}
|
||||
else if (element == gamepad.rightShoulder)
|
||||
{
|
||||
onButtonEvent(Key::BUTTON_RIGHT_SHOULDER, gamepad.rightShoulder.isPressed, gamepad.rightShoulder.value, gamepad.rightShoulder.isAnalog);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
_impl->_gcController.controllerPausedHandler = ^(GCController* gcCon){
|
||||
|
||||
auto iter = std::find_if(s_allController.begin(), s_allController.end(), [gcCon](Controller* c){ return c->_impl->_gcController == gcCon; });
|
||||
|
||||
if(iter == s_allController.end())
|
||||
{
|
||||
log("Could not find the controller");
|
||||
return;
|
||||
}
|
||||
|
||||
onButtonEvent(Key::BUTTON_PAUSE, true, 1.0f, false);
|
||||
onButtonEvent(Key::BUTTON_PAUSE, false, 0.0f, false);
|
||||
};
|
||||
}
|
||||
|
||||
bool Controller::isConnected() const
|
||||
|
@ -225,147 +311,6 @@ bool Controller::isConnected() const
|
|||
return _impl->_gcController.isAttachedToDevice == YES;
|
||||
}
|
||||
|
||||
int Controller::getPlayerIndex() const
|
||||
{
|
||||
return _playerIndex;
|
||||
}
|
||||
|
||||
void Controller::setPlayerIndex(int playerIndex)
|
||||
{
|
||||
_playerIndex = playerIndex;
|
||||
}
|
||||
|
||||
Gamepad* Controller::getGamepad() const
|
||||
{
|
||||
if (_impl->_gcController == nil)
|
||||
return nullptr;
|
||||
if(_impl->_gcController.gamepad == nil && _impl->_gcController.extendedGamepad == nil)
|
||||
{
|
||||
CCASSERT(false, "No gamepad was found!");
|
||||
}
|
||||
else if (_impl->_lazyRegisterListener)
|
||||
{
|
||||
_impl->_lazyRegisterListener = false;
|
||||
if (_impl->_gcController.extendedGamepad != nil)
|
||||
{
|
||||
_impl->_gcController.extendedGamepad.dpad.up.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
||||
sendEventButton(_gamepad->getDirectionPad()->getUp(), button);
|
||||
};
|
||||
|
||||
_impl->_gcController.extendedGamepad.dpad.down.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
||||
sendEventButton(_gamepad->getDirectionPad()->getDown(), button);
|
||||
};
|
||||
|
||||
_impl->_gcController.extendedGamepad.dpad.left.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
||||
sendEventButton(_gamepad->getDirectionPad()->getLeft(), button);
|
||||
};
|
||||
|
||||
_impl->_gcController.extendedGamepad.dpad.right.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
||||
sendEventButton(_gamepad->getDirectionPad()->getRight(), button);
|
||||
};
|
||||
|
||||
_impl->_gcController.extendedGamepad.leftThumbstick.xAxis.valueChangedHandler = ^(GCControllerAxisInput *axis, float value){
|
||||
sendEventAxis(_gamepad->getLeftThumbstick()->getAxisX(), axis);
|
||||
};
|
||||
|
||||
_impl->_gcController.extendedGamepad.leftThumbstick.yAxis.valueChangedHandler = ^(GCControllerAxisInput *axis, float value){
|
||||
sendEventAxis(_gamepad->getLeftThumbstick()->getAxisY(), axis);
|
||||
};
|
||||
|
||||
_impl->_gcController.extendedGamepad.rightThumbstick.xAxis.valueChangedHandler = ^(GCControllerAxisInput *axis, float value){
|
||||
sendEventAxis(_gamepad->getRightThumbstick()->getAxisX(), axis);
|
||||
};
|
||||
|
||||
_impl->_gcController.extendedGamepad.rightThumbstick.yAxis.valueChangedHandler = ^(GCControllerAxisInput *axis, float value){
|
||||
sendEventAxis(_gamepad->getRightThumbstick()->getAxisY(), axis);
|
||||
};
|
||||
|
||||
_impl->_gcController.extendedGamepad.valueChangedHandler = ^(GCExtendedGamepad *gamepad, GCControllerElement *element){
|
||||
if (element == gamepad.buttonA)
|
||||
{
|
||||
sendEventButton(_gamepad->getButtonA(), gamepad.buttonA);
|
||||
}
|
||||
else if (element == gamepad.buttonB)
|
||||
{
|
||||
sendEventButton(_gamepad->getButtonB(), gamepad.buttonB);
|
||||
}
|
||||
else if (element == gamepad.buttonX)
|
||||
{
|
||||
sendEventButton(_gamepad->getButtonX(), gamepad.buttonX);
|
||||
}
|
||||
else if (element == gamepad.buttonY)
|
||||
{
|
||||
sendEventButton(_gamepad->getButtonY(), gamepad.buttonY);
|
||||
}
|
||||
else if (element == gamepad.leftShoulder)
|
||||
{
|
||||
sendEventButton(_gamepad->getLeftShoulder(), gamepad.leftShoulder);
|
||||
}
|
||||
else if (element == gamepad.rightShoulder)
|
||||
{
|
||||
sendEventButton(_gamepad->getRightShoulder(), gamepad.rightShoulder);
|
||||
}
|
||||
else if (element == gamepad.leftTrigger)
|
||||
{
|
||||
sendEventButton(_gamepad->getLeftTrigger(), gamepad.leftTrigger);
|
||||
}
|
||||
else if (element == gamepad.rightTrigger)
|
||||
{
|
||||
sendEventButton(_gamepad->getRightTrigger(), gamepad.rightTrigger);
|
||||
}
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
_impl->_gcController.gamepad.dpad.up.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
||||
sendEventButton(_gamepad->getDirectionPad()->getUp(), button);
|
||||
};
|
||||
|
||||
_impl->_gcController.gamepad.dpad.down.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
||||
sendEventButton(_gamepad->getDirectionPad()->getDown(), button);
|
||||
};
|
||||
|
||||
_impl->_gcController.gamepad.dpad.left.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
||||
sendEventButton(_gamepad->getDirectionPad()->getLeft(), button);
|
||||
};
|
||||
|
||||
_impl->_gcController.gamepad.dpad.right.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
||||
sendEventButton(_gamepad->getDirectionPad()->getRight(), button);
|
||||
};
|
||||
|
||||
_impl->_gcController.gamepad.valueChangedHandler = ^(GCGamepad *gamepad, GCControllerElement *element){
|
||||
|
||||
if (element == gamepad.buttonA)
|
||||
{
|
||||
sendEventButton(_gamepad->getButtonA(), gamepad.buttonA);
|
||||
}
|
||||
else if (element == gamepad.buttonB)
|
||||
{
|
||||
sendEventButton(_gamepad->getButtonB(), gamepad.buttonB);
|
||||
}
|
||||
else if (element == gamepad.buttonX)
|
||||
{
|
||||
sendEventButton(_gamepad->getButtonX(), gamepad.buttonX);
|
||||
}
|
||||
else if (element == gamepad.buttonY)
|
||||
{
|
||||
sendEventButton(_gamepad->getButtonY(), gamepad.buttonY);
|
||||
}
|
||||
else if (element == gamepad.leftShoulder)
|
||||
{
|
||||
sendEventButton(_gamepad->getLeftShoulder(), gamepad.leftShoulder);
|
||||
}
|
||||
else if (element == gamepad.rightShoulder)
|
||||
{
|
||||
sendEventButton(_gamepad->getRightShoulder(), gamepad.rightShoulder);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return _gamepad;
|
||||
}
|
||||
|
||||
NS_CC_END
|
||||
|
||||
#endif // #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
||||
|
|
|
@ -0,0 +1,117 @@
|
|||
/****************************************************************************
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#include "CCController.h"
|
||||
|
||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
||||
|
||||
#include "ccMacros.h"
|
||||
#include "CCEventDispatcher.h"
|
||||
#include "CCEventController.h"
|
||||
#include "CCEventListenerController.h"
|
||||
#include "CCDirector.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
std::vector<Controller*> Controller::s_allController;
|
||||
|
||||
Controller* Controller::getControllerByTag(int tag)
|
||||
{
|
||||
for (auto controller:Controller::s_allController)
|
||||
{
|
||||
if (controller->_controllerTag == tag)
|
||||
{
|
||||
return controller;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void Controller::init()
|
||||
{
|
||||
for (int key = Key::JOYSTICK_LEFT_X; key < Key::KEY_MAX; ++key)
|
||||
{
|
||||
_allKeyStatus[key].isPressed = false;
|
||||
_allKeyStatus[key].value = 0.0f;
|
||||
|
||||
_allKeyPrevStatus[key].isPressed = false;
|
||||
_allKeyPrevStatus[key].value = 0.0f;
|
||||
}
|
||||
|
||||
_eventDispatcher = Director::getInstance()->getEventDispatcher();
|
||||
_connectEvent = new EventController(EventController::ControllerEventType::CONNECTION, this, false);
|
||||
_keyEvent = new EventController(EventController::ControllerEventType::BUTTON_STATUS_CHANGED, this, 0);
|
||||
_axisEvent = new EventController(EventController::ControllerEventType::AXIS_STATUS_CHANGED, this, 0);
|
||||
}
|
||||
|
||||
const Controller::KeyStatus& Controller::getKeyStatus(int keyCode)
|
||||
{
|
||||
if (_allKeyStatus.find(keyCode) == _allKeyStatus.end())
|
||||
{
|
||||
_allKeyStatus[keyCode].isPressed = false;
|
||||
_allKeyStatus[keyCode].value = 0.0f;
|
||||
}
|
||||
|
||||
return _allKeyStatus[keyCode];
|
||||
}
|
||||
|
||||
void Controller::onConnected()
|
||||
{
|
||||
_connectEvent->setConnectStatus(true);
|
||||
_eventDispatcher->dispatchEvent(_connectEvent);
|
||||
}
|
||||
|
||||
void Controller::onDisconnected()
|
||||
{
|
||||
_connectEvent->setConnectStatus(false);
|
||||
_eventDispatcher->dispatchEvent(_connectEvent);
|
||||
|
||||
delete this;
|
||||
}
|
||||
|
||||
void Controller::onButtonEvent(int keyCode, bool isPressed, float value, bool isAnalog)
|
||||
{
|
||||
_allKeyPrevStatus[keyCode] = _allKeyStatus[keyCode];
|
||||
_allKeyStatus[keyCode].isPressed = isPressed;
|
||||
_allKeyStatus[keyCode].value = value;
|
||||
_allKeyStatus[keyCode].isAnalog = isAnalog;
|
||||
|
||||
_keyEvent->setKeyCode(keyCode);
|
||||
_eventDispatcher->dispatchEvent(_keyEvent);
|
||||
}
|
||||
|
||||
void Controller::onAxisEvent(int axisCode, float value, bool isAnalog)
|
||||
{
|
||||
_allKeyPrevStatus[axisCode] = _allKeyStatus[axisCode];
|
||||
_allKeyStatus[axisCode].value = value;
|
||||
_allKeyStatus[axisCode].isAnalog = isAnalog;
|
||||
|
||||
_axisEvent->setKeyCode(axisCode);
|
||||
_eventDispatcher->dispatchEvent(_axisEvent);
|
||||
}
|
||||
|
||||
NS_CC_END
|
||||
|
||||
#endif // #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
|
@ -27,50 +27,112 @@
|
|||
#define __cocos2d_libs__CCController__
|
||||
|
||||
#include "CCPlatformMacros.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
class Gamepad;
|
||||
class ControllerImpl;
|
||||
class EventListenerController;
|
||||
class EventController;
|
||||
class EventDispatcher;
|
||||
|
||||
class Controller
|
||||
{
|
||||
public:
|
||||
static const std::vector<Controller*>& getControllers();
|
||||
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,
|
||||
|
||||
BUTTON_PAUSE,
|
||||
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;}
|
||||
static Controller* getControllerByTag(int tag);
|
||||
|
||||
static void startDiscoveryController();
|
||||
static void stopDiscoveryController();
|
||||
|
||||
const std::string& getVendorName();
|
||||
const std::string& getDeviceName() const { return _deviceName;}
|
||||
int getDeviceId() const { return _deviceId;}
|
||||
|
||||
bool isConnected() const;
|
||||
|
||||
static const int PLAYER_INDEX_UNSET = -1;
|
||||
const KeyStatus& getKeyStatus(int keyCode);
|
||||
|
||||
void setTag(int tag) { _controllerTag = tag;}
|
||||
int getTag() const { return _controllerTag;}
|
||||
|
||||
int getPlayerIndex() const;
|
||||
void setPlayerIndex(int playerIndex);
|
||||
|
||||
Gamepad* getGamepad() const;
|
||||
|
||||
// For internal use only
|
||||
inline ControllerImpl* getImpl() const { return _impl; };
|
||||
private:
|
||||
static std::vector<Controller*> s_allController;
|
||||
|
||||
Controller();
|
||||
virtual ~Controller();
|
||||
|
||||
private:
|
||||
static std::vector<Controller*> _controllers;
|
||||
void init();
|
||||
|
||||
std::string _vendorName;
|
||||
int _playerIndex;
|
||||
Gamepad* _gamepad;
|
||||
void onConnected();
|
||||
void onDisconnected();
|
||||
void onButtonEvent(int keyCode, bool isPressed, float value, bool isAnalog);
|
||||
void onAxisEvent(int axisCode, float value, bool isAnalog);
|
||||
void registerListeners();
|
||||
|
||||
std::unordered_map<int, KeyStatus> _allKeyStatus;
|
||||
std::unordered_map<int, KeyStatus> _allKeyPrevStatus;
|
||||
|
||||
std::string _deviceName;
|
||||
int _deviceId;
|
||||
|
||||
int _controllerTag;
|
||||
|
||||
ControllerImpl* _impl;
|
||||
|
||||
EventDispatcher* _eventDispatcher;
|
||||
EventController *_connectEvent;
|
||||
EventController *_keyEvent;
|
||||
EventController *_axisEvent;
|
||||
|
||||
friend class ControllerImpl;
|
||||
ControllerImpl* _impl;
|
||||
friend class EventListenerController;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -1,52 +0,0 @@
|
|||
/****************************************************************************
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#include "CCControllerAxisInput.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
ControllerAxisInput::ControllerAxisInput()
|
||||
: _value(0.0f)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ControllerAxisInput::~ControllerAxisInput()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
float ControllerAxisInput::getValue() const
|
||||
{
|
||||
return _value;
|
||||
}
|
||||
|
||||
void ControllerAxisInput::setValue(float value)
|
||||
{
|
||||
_value = value;
|
||||
}
|
||||
|
||||
|
||||
NS_CC_END
|
|
@ -1,55 +0,0 @@
|
|||
/****************************************************************************
|
||||
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__CCControllerAxisInput__
|
||||
#define __cocos2d_libs__CCControllerAxisInput__
|
||||
|
||||
#include "CCPlatformMacros.h"
|
||||
#include "CCControllerElement.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
class ControllerAxisInput : public ControllerElement
|
||||
{
|
||||
public:
|
||||
float getValue() const;
|
||||
|
||||
protected:
|
||||
ControllerAxisInput();
|
||||
virtual ~ControllerAxisInput();
|
||||
|
||||
void setValue(float value);
|
||||
|
||||
float _value;
|
||||
|
||||
friend class Controller;
|
||||
friend class ControllerImpl;
|
||||
friend class ControllerDirectionPad;
|
||||
friend class ControllerThumbstick;
|
||||
};
|
||||
|
||||
NS_CC_END
|
||||
|
||||
#endif /* defined(__cocos2d_libs__CCControllerAxisInput__) */
|
|
@ -1,69 +0,0 @@
|
|||
/****************************************************************************
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#include "CCControllerButtonInput.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
ControllerButtonInput::ControllerButtonInput()
|
||||
: _value(0.0f)
|
||||
, _isPressed(false)
|
||||
, _isPrevStatusPressed(false)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ControllerButtonInput::~ControllerButtonInput()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
float ControllerButtonInput::getValue() const
|
||||
{
|
||||
return _value;
|
||||
}
|
||||
|
||||
bool ControllerButtonInput::isPressed() const
|
||||
{
|
||||
return _isPressed;
|
||||
}
|
||||
|
||||
bool ControllerButtonInput::isPrevStatusPressed() const
|
||||
{
|
||||
return _isPrevStatusPressed;
|
||||
}
|
||||
|
||||
void ControllerButtonInput::setValue(float value)
|
||||
{
|
||||
_value = value;
|
||||
}
|
||||
|
||||
void ControllerButtonInput::setPressed(bool isPressed)
|
||||
{
|
||||
_isPrevStatusPressed = _isPressed;
|
||||
_isPressed = isPressed;
|
||||
}
|
||||
|
||||
NS_CC_END
|
|
@ -1,63 +0,0 @@
|
|||
/****************************************************************************
|
||||
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__CCControllerButtonInput__
|
||||
#define __cocos2d_libs__CCControllerButtonInput__
|
||||
|
||||
#include "CCPlatformMacros.h"
|
||||
#include "CCControllerElement.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
class ControllerButtonInput : public ControllerElement
|
||||
{
|
||||
public:
|
||||
float getValue() const;
|
||||
bool isPressed() const;
|
||||
bool isPrevStatusPressed() const;
|
||||
|
||||
protected:
|
||||
|
||||
ControllerButtonInput();
|
||||
virtual ~ControllerButtonInput();
|
||||
|
||||
void setValue(float value);
|
||||
void setPressed(bool isPressed);
|
||||
|
||||
float _value;
|
||||
bool _isPressed;
|
||||
|
||||
bool _isPrevStatusPressed; // Whether previous status is pressed
|
||||
|
||||
friend class Controller;
|
||||
friend class ControllerImpl;
|
||||
friend class ControllerDirectionPad;
|
||||
friend class ControllerThumbstick;
|
||||
friend class Gamepad;
|
||||
};
|
||||
|
||||
NS_CC_END
|
||||
|
||||
#endif /* defined(__cocos2d_libs__CCControllerButtonInput__) */
|
|
@ -1,74 +0,0 @@
|
|||
/****************************************************************************
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#include "CCControllerDirectionPad.h"
|
||||
#include "CCControllerAxisInput.h"
|
||||
#include "CCControllerButtonInput.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
ControllerDirectionPad::ControllerDirectionPad()
|
||||
: _up(new ControllerButtonInput())
|
||||
, _down(new ControllerButtonInput())
|
||||
, _left(new ControllerButtonInput())
|
||||
, _right(new ControllerButtonInput())
|
||||
{
|
||||
_up->setCollection(this);
|
||||
_down->setCollection(this);
|
||||
_left->setCollection(this);
|
||||
_right->setCollection(this);
|
||||
}
|
||||
|
||||
ControllerDirectionPad::~ControllerDirectionPad()
|
||||
{
|
||||
CC_SAFE_DELETE(_up);
|
||||
CC_SAFE_DELETE(_down);
|
||||
CC_SAFE_DELETE(_left);
|
||||
CC_SAFE_DELETE(_right);
|
||||
}
|
||||
|
||||
ControllerButtonInput* ControllerDirectionPad::getUp() const
|
||||
{
|
||||
return _up;
|
||||
}
|
||||
|
||||
ControllerButtonInput* ControllerDirectionPad::getDown() const
|
||||
{
|
||||
return _down;
|
||||
}
|
||||
|
||||
ControllerButtonInput* ControllerDirectionPad::getLeft() const
|
||||
{
|
||||
return _left;
|
||||
}
|
||||
|
||||
ControllerButtonInput* ControllerDirectionPad::getRight() const
|
||||
{
|
||||
return _right;
|
||||
}
|
||||
|
||||
|
||||
|
||||
NS_CC_END
|
|
@ -1,61 +0,0 @@
|
|||
/****************************************************************************
|
||||
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__CCControllerDirectionPad__
|
||||
#define __cocos2d_libs__CCControllerDirectionPad__
|
||||
|
||||
#include "CCPlatformMacros.h"
|
||||
#include "CCControllerElement.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
class ControllerAxisInput;
|
||||
class ControllerButtonInput;
|
||||
|
||||
class ControllerDirectionPad : public ControllerElement
|
||||
{
|
||||
public:
|
||||
ControllerButtonInput* getUp() const;
|
||||
ControllerButtonInput* getDown() const;
|
||||
ControllerButtonInput* getLeft() const;
|
||||
ControllerButtonInput* getRight() const;
|
||||
|
||||
protected:
|
||||
|
||||
friend class Controller;
|
||||
friend class Gamepad;
|
||||
|
||||
ControllerDirectionPad();
|
||||
virtual ~ControllerDirectionPad();
|
||||
|
||||
ControllerButtonInput* _up;
|
||||
ControllerButtonInput* _down;
|
||||
ControllerButtonInput* _left;
|
||||
ControllerButtonInput* _right;
|
||||
};
|
||||
|
||||
NS_CC_END
|
||||
|
||||
#endif /* defined(__cocos2d_libs__CCControllerDirectionPad__) */
|
|
@ -1,63 +0,0 @@
|
|||
/****************************************************************************
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#include "CCControllerElement.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
ControllerElement::ControllerElement()
|
||||
: _isAnalog(false)
|
||||
, _collection(nullptr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ControllerElement::~ControllerElement()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool ControllerElement::isAnalog() const
|
||||
{
|
||||
return _isAnalog;
|
||||
}
|
||||
|
||||
void ControllerElement::setAnalog(bool isAnalog)
|
||||
{
|
||||
_isAnalog = isAnalog;
|
||||
}
|
||||
|
||||
ControllerElement* ControllerElement::getCollection()
|
||||
{
|
||||
return _collection;
|
||||
}
|
||||
|
||||
void ControllerElement::setCollection(ControllerElement* collection)
|
||||
{
|
||||
_collection = collection;
|
||||
}
|
||||
|
||||
|
||||
NS_CC_END
|
|
@ -1,64 +0,0 @@
|
|||
/****************************************************************************
|
||||
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__CCControllerElement__
|
||||
#define __cocos2d_libs__CCControllerElement__
|
||||
|
||||
#include "CCPlatformMacros.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
/**
|
||||
Every controller element knows which collection it belongs to and whether its input value is analog or digital.
|
||||
*/
|
||||
class ControllerElement
|
||||
{
|
||||
public:
|
||||
/**
|
||||
Each element can be part of a wider collection of inputs that map to a single logical element. A directional pad (dpad)
|
||||
is a logical collection of two axis inputs and thus each axis belongs to the same collection element - the dpad.
|
||||
*/
|
||||
ControllerElement * getCollection();
|
||||
|
||||
/**
|
||||
Check if the element can support more than just digital values, such as decimal ranges between 0 and 1.
|
||||
*/
|
||||
bool isAnalog() const;
|
||||
|
||||
protected:
|
||||
|
||||
ControllerElement();
|
||||
virtual ~ControllerElement();
|
||||
|
||||
void setAnalog(bool isAnalog);
|
||||
void setCollection(ControllerElement* collection);
|
||||
|
||||
bool _isAnalog;
|
||||
ControllerElement* _collection;
|
||||
};
|
||||
|
||||
NS_CC_END
|
||||
|
||||
#endif /* defined(__cocos2d_libs__CCControllerElement__) */
|
|
@ -1,66 +0,0 @@
|
|||
/****************************************************************************
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#include "CCControllerThumbstick.h"
|
||||
#include "CCControllerAxisInput.h"
|
||||
#include "CCControllerButtonInput.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
ControllerThumbstick::ControllerThumbstick()
|
||||
: _axisX(new ControllerAxisInput())
|
||||
, _axisY(new ControllerAxisInput())
|
||||
, _button(new ControllerButtonInput())
|
||||
{
|
||||
_axisX->setCollection(this);
|
||||
_axisY->setCollection(this);
|
||||
_button->setCollection(this);
|
||||
}
|
||||
|
||||
ControllerThumbstick::~ControllerThumbstick()
|
||||
{
|
||||
CC_SAFE_DELETE(_axisX);
|
||||
CC_SAFE_DELETE(_axisY);
|
||||
CC_SAFE_DELETE(_button);
|
||||
}
|
||||
|
||||
ControllerAxisInput* ControllerThumbstick::getAxisX() const
|
||||
{
|
||||
return _axisX;
|
||||
}
|
||||
|
||||
ControllerAxisInput* ControllerThumbstick::getAxisY() const
|
||||
{
|
||||
return _axisY;
|
||||
}
|
||||
|
||||
ControllerButtonInput* ControllerThumbstick::getButton() const
|
||||
{
|
||||
return _button;
|
||||
}
|
||||
|
||||
|
||||
|
||||
NS_CC_END
|
|
@ -1,61 +0,0 @@
|
|||
/****************************************************************************
|
||||
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__CCControllerThumbStick__
|
||||
#define __cocos2d_libs__CCControllerThumbStick__
|
||||
|
||||
#include "CCPlatformMacros.h"
|
||||
#include "CCControllerElement.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
class ControllerAxisInput;
|
||||
class ControllerButtonInput;
|
||||
|
||||
class ControllerThumbstick : public ControllerElement
|
||||
{
|
||||
public:
|
||||
ControllerAxisInput* getAxisX() const;
|
||||
ControllerAxisInput* getAxisY() const;
|
||||
|
||||
ControllerButtonInput* getButton() const;
|
||||
|
||||
protected:
|
||||
|
||||
friend class Controller;
|
||||
friend class Gamepad;
|
||||
|
||||
ControllerThumbstick();
|
||||
virtual ~ControllerThumbstick();
|
||||
|
||||
ControllerAxisInput* _axisX;
|
||||
ControllerAxisInput* _axisY;
|
||||
|
||||
ControllerButtonInput* _button;
|
||||
};
|
||||
|
||||
NS_CC_END
|
||||
|
||||
#endif /* defined(__cocos2d_libs__CCControllerThumbStick__) */
|
|
@ -27,10 +27,10 @@
|
|||
|
||||
NS_CC_BEGIN
|
||||
|
||||
EventController::EventController(ControllerEventType type, Controller* controller, ControllerElement* element)
|
||||
EventController::EventController(ControllerEventType type, Controller* controller, int keyCode)
|
||||
: Event(Type::GAME_CONTROLLER)
|
||||
, _controllerEventType(type)
|
||||
, _element(element)
|
||||
, _keyCode(keyCode)
|
||||
, _controller(controller)
|
||||
, _isConnected(true)
|
||||
{
|
||||
|
@ -40,31 +40,11 @@ EventController::EventController(ControllerEventType type, Controller* controlle
|
|||
EventController::EventController(ControllerEventType type, Controller* controller, bool isConnected)
|
||||
: Event(Type::GAME_CONTROLLER)
|
||||
, _controllerEventType(type)
|
||||
, _element(nullptr)
|
||||
, _controller(controller)
|
||||
, _isConnected(isConnected)
|
||||
, _keyCode(0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
EventController::ControllerEventType EventController::getControllerEventType() const
|
||||
{
|
||||
return _controllerEventType;
|
||||
}
|
||||
|
||||
Controller* EventController::getController() const
|
||||
{
|
||||
return _controller;
|
||||
}
|
||||
|
||||
ControllerElement* EventController::getControllerElement() const
|
||||
{
|
||||
return _element;
|
||||
}
|
||||
|
||||
bool EventController::isConnected() const
|
||||
{
|
||||
return _isConnected;
|
||||
}
|
||||
|
||||
NS_CC_END
|
||||
|
|
|
@ -31,10 +31,8 @@
|
|||
|
||||
NS_CC_BEGIN
|
||||
|
||||
class ControllerButtonInput;
|
||||
class ControllerAxisInput;
|
||||
class ControllerElement;
|
||||
class Controller;
|
||||
class EventListenerController;
|
||||
|
||||
class EventController : public Event
|
||||
{
|
||||
|
@ -46,19 +44,25 @@ public:
|
|||
AXIS_STATUS_CHANGED,
|
||||
};
|
||||
|
||||
EventController(ControllerEventType type, Controller* controller, ControllerElement* element);
|
||||
EventController(ControllerEventType type, Controller* controller, int keyCode);
|
||||
EventController(ControllerEventType type, Controller* controller, bool isConnected);
|
||||
|
||||
ControllerEventType getControllerEventType() const;
|
||||
Controller* getController() const;
|
||||
ControllerElement* getControllerElement() const;
|
||||
bool isConnected() const;
|
||||
ControllerEventType getControllerEventType() const { return _controllerEventType; }
|
||||
Controller* getController() const { return _controller; }
|
||||
|
||||
int getKeyCode() const{ return _keyCode; }
|
||||
void setKeyCode(int keyCode) { _keyCode = keyCode;}
|
||||
|
||||
void setConnectStatus(bool isConnected) {_isConnected = isConnected;}
|
||||
bool isConnected() const { return _isConnected; }
|
||||
|
||||
protected:
|
||||
ControllerEventType _controllerEventType;
|
||||
ControllerElement* _element;
|
||||
Controller* _controller;
|
||||
int _keyCode;
|
||||
bool _isConnected;
|
||||
|
||||
friend class EventListenerController;
|
||||
};
|
||||
|
||||
NS_CC_END
|
||||
|
|
|
@ -25,9 +25,8 @@
|
|||
|
||||
#include "CCEventListenerController.h"
|
||||
#include "CCEventController.h"
|
||||
#include "CCControllerButtonInput.h"
|
||||
#include "CCControllerAxisInput.h"
|
||||
#include "ccMacros.h"
|
||||
#include "base/CCController.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
|
@ -51,8 +50,9 @@ bool EventListenerController::init()
|
|||
{
|
||||
auto listener = [this](Event* event){
|
||||
auto evtController = static_cast<EventController*>(event);
|
||||
if (evtController->getControllerEventType() == EventController::ControllerEventType::CONNECTION)
|
||||
switch (evtController->getControllerEventType())
|
||||
{
|
||||
case EventController::ControllerEventType::CONNECTION:
|
||||
if (evtController->isConnected())
|
||||
{
|
||||
if (this->onConnected)
|
||||
|
@ -63,45 +63,40 @@ bool EventListenerController::init()
|
|||
if (this->onDisconnected)
|
||||
this->onDisconnected(evtController->getController(), event);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (evtController->getControllerEventType()) {
|
||||
case EventController::ControllerEventType::BUTTON_STATUS_CHANGED:
|
||||
{
|
||||
auto button = static_cast<ControllerButtonInput*>(evtController->getControllerElement());
|
||||
break;
|
||||
case EventController::ControllerEventType::BUTTON_STATUS_CHANGED:
|
||||
{
|
||||
const auto& keyStatus = evtController->_controller->_allKeyStatus[evtController->_keyCode];
|
||||
const auto& keyPrevStatus = evtController->_controller->_allKeyPrevStatus[evtController->_keyCode];
|
||||
|
||||
if (this->onButtonPressed && button->isPressed() && !button->isPrevStatusPressed())
|
||||
{
|
||||
this->onButtonPressed(evtController->getController(), button, event);
|
||||
}
|
||||
else if (this->onButtonReleased && !button->isPressed() && button->isPrevStatusPressed())
|
||||
{
|
||||
this->onButtonReleased(evtController->getController(), button, event);
|
||||
}
|
||||
|
||||
if (this->onButtonValueChanged)
|
||||
{
|
||||
this->onButtonValueChanged(evtController->getController(), button, event);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case EventController::ControllerEventType::AXIS_STATUS_CHANGED:
|
||||
{
|
||||
if (this->onAxisValueChanged)
|
||||
{
|
||||
auto axis = static_cast<ControllerAxisInput*>(evtController->getControllerElement());
|
||||
this->onAxisValueChanged(evtController->getController(), axis, event);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
CCASSERT(false, "Invalid EventController type");
|
||||
break;
|
||||
if (this->onKeyDown && keyStatus.isPressed && !keyPrevStatus.isPressed)
|
||||
{
|
||||
this->onKeyDown(evtController->_controller, evtController->_keyCode, event);
|
||||
}
|
||||
else if (this->onKeyUp && !keyStatus.isPressed && keyPrevStatus.isPressed)
|
||||
{
|
||||
this->onKeyUp(evtController->_controller, evtController->_keyCode, event);
|
||||
}
|
||||
else if (this->onKeyRepeat && keyStatus.isPressed && keyPrevStatus.isPressed)
|
||||
{
|
||||
this->onKeyRepeat(evtController->_controller, evtController->_keyCode, event);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case EventController::ControllerEventType::AXIS_STATUS_CHANGED:
|
||||
{
|
||||
if (this->onAxisEvent)
|
||||
{
|
||||
this->onAxisEvent(evtController->_controller, evtController->_keyCode, event);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
CCASSERT(false, "Invalid EventController type");
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
if (EventListener::init(EventListener::Type::GAME_CONTROLLER, LISTENER_ID, listener))
|
||||
{
|
||||
return true;
|
||||
|
|
|
@ -33,8 +33,6 @@ NS_CC_BEGIN
|
|||
|
||||
class Event;
|
||||
class Controller;
|
||||
class ControllerButtonInput;
|
||||
class ControllerAxisInput;
|
||||
|
||||
class EventListenerController : public EventListener
|
||||
{
|
||||
|
@ -50,11 +48,11 @@ public:
|
|||
std::function<void(Controller*, Event*)> onConnected;
|
||||
std::function<void(Controller*, Event*)> onDisconnected;
|
||||
|
||||
std::function<void(Controller*, ControllerButtonInput*, Event*)> onButtonPressed;
|
||||
std::function<void(Controller*, ControllerButtonInput*, Event*)> onButtonReleased;
|
||||
std::function<void(Controller*, ControllerButtonInput*, Event*)> onButtonValueChanged;
|
||||
std::function<void(Controller*, int, Event*)> onKeyDown;
|
||||
std::function<void(Controller*, int, Event*)> onKeyUp;
|
||||
std::function<void(Controller*, int, Event*)> onKeyRepeat;
|
||||
|
||||
std::function<void(Controller*, ControllerAxisInput*, Event*)> onAxisValueChanged;
|
||||
std::function<void(Controller*, int, Event*)> onAxisEvent;
|
||||
|
||||
protected:
|
||||
bool init();
|
||||
|
|
|
@ -26,12 +26,6 @@
|
|||
#ifndef __cocos2d_libs__CCGameController__
|
||||
#define __cocos2d_libs__CCGameController__
|
||||
|
||||
#include "CCControllerElement.h"
|
||||
#include "CCControllerButtonInput.h"
|
||||
#include "CCControllerAxisInput.h"
|
||||
#include "CCControllerDirectionPad.h"
|
||||
#include "CCControllerThumbstick.h"
|
||||
#include "CCGamepad.h"
|
||||
#include "CCController.h"
|
||||
#include "CCEventController.h"
|
||||
#include "CCEventListenerController.h"
|
||||
|
|
|
@ -1,146 +0,0 @@
|
|||
/****************************************************************************
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#include "CCGamepad.h"
|
||||
#include "CCControllerDirectionPad.h"
|
||||
#include "CCControllerButtonInput.h"
|
||||
#include "CCControllerThumbstick.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
Gamepad::Gamepad()
|
||||
: _controller(nullptr)
|
||||
, _directionPad(new ControllerDirectionPad())
|
||||
, _buttonA(new ControllerButtonInput())
|
||||
, _buttonB(new ControllerButtonInput())
|
||||
, _buttonX(new ControllerButtonInput())
|
||||
, _buttonY(new ControllerButtonInput())
|
||||
, _buttonSelect(new ControllerButtonInput())
|
||||
, _buttonStart(new ControllerButtonInput())
|
||||
, _buttonPause(new ControllerButtonInput())
|
||||
, _leftShoulder(new ControllerButtonInput())
|
||||
, _rightShoulder(new ControllerButtonInput())
|
||||
, _leftThumbstick(new ControllerThumbstick())
|
||||
, _rightThumbstick(new ControllerThumbstick())
|
||||
, _leftTrigger(new ControllerButtonInput())
|
||||
, _rightTrigger(new ControllerButtonInput())
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Gamepad::~Gamepad()
|
||||
{
|
||||
CC_SAFE_DELETE(_directionPad);
|
||||
CC_SAFE_DELETE(_buttonA);
|
||||
CC_SAFE_DELETE(_buttonB);
|
||||
CC_SAFE_DELETE(_buttonX);
|
||||
CC_SAFE_DELETE(_buttonY);
|
||||
CC_SAFE_DELETE(_buttonSelect);
|
||||
CC_SAFE_DELETE(_buttonStart);
|
||||
CC_SAFE_DELETE(_buttonPause);
|
||||
CC_SAFE_DELETE(_leftShoulder);
|
||||
CC_SAFE_DELETE(_rightShoulder);
|
||||
CC_SAFE_DELETE(_leftThumbstick);
|
||||
CC_SAFE_DELETE(_rightThumbstick);
|
||||
CC_SAFE_DELETE(_leftTrigger);
|
||||
CC_SAFE_DELETE(_rightTrigger);
|
||||
}
|
||||
|
||||
Controller* Gamepad::getController()
|
||||
{
|
||||
return _controller;
|
||||
}
|
||||
|
||||
ControllerDirectionPad* Gamepad::getDirectionPad() const
|
||||
{
|
||||
return _directionPad;
|
||||
}
|
||||
|
||||
ControllerButtonInput* Gamepad::getButtonA() const
|
||||
{
|
||||
return _buttonA;
|
||||
}
|
||||
|
||||
ControllerButtonInput* Gamepad::getButtonB() const
|
||||
{
|
||||
return _buttonB;
|
||||
}
|
||||
|
||||
ControllerButtonInput* Gamepad::getButtonX() const
|
||||
{
|
||||
return _buttonX;
|
||||
}
|
||||
|
||||
ControllerButtonInput* Gamepad::getButtonY() const
|
||||
{
|
||||
return _buttonY;
|
||||
}
|
||||
|
||||
ControllerButtonInput* Gamepad::getButtonPause() const
|
||||
{
|
||||
return _buttonPause;
|
||||
}
|
||||
|
||||
ControllerButtonInput* Gamepad::getButtonSelect() const
|
||||
{
|
||||
return _buttonSelect;
|
||||
}
|
||||
|
||||
ControllerButtonInput* Gamepad::getButtonStart() const
|
||||
{
|
||||
return _buttonStart;
|
||||
}
|
||||
|
||||
ControllerButtonInput* Gamepad::getLeftShoulder() const
|
||||
{
|
||||
return _leftShoulder;
|
||||
}
|
||||
|
||||
ControllerButtonInput* Gamepad::getRightShoulder() const
|
||||
{
|
||||
return _rightShoulder;
|
||||
}
|
||||
|
||||
ControllerThumbstick* Gamepad::getLeftThumbstick() const
|
||||
{
|
||||
return _leftThumbstick;
|
||||
}
|
||||
|
||||
ControllerThumbstick* Gamepad::getRightThumbstick() const
|
||||
{
|
||||
return _rightThumbstick;
|
||||
}
|
||||
|
||||
ControllerButtonInput* Gamepad::getLeftTrigger() const
|
||||
{
|
||||
return _leftTrigger;
|
||||
}
|
||||
|
||||
ControllerButtonInput* Gamepad::getRightTrigger() const
|
||||
{
|
||||
return _rightTrigger;
|
||||
}
|
||||
|
||||
NS_CC_END
|
|
@ -1,122 +0,0 @@
|
|||
/****************************************************************************
|
||||
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__CCGamepad__
|
||||
#define __cocos2d_libs__CCGamepad__
|
||||
|
||||
#include "CCPlatformMacros.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
class ControllerButtonInput;
|
||||
class ControllerDirectionPad;
|
||||
class ControllerThumbstick;
|
||||
class Controller;
|
||||
|
||||
class Gamepad
|
||||
{
|
||||
public:
|
||||
/**
|
||||
A profile keeps a reference to the controller that this profile is mapping input from.
|
||||
*/
|
||||
Controller* getController();
|
||||
|
||||
// API for Standard / Extended Gamepad
|
||||
|
||||
ControllerDirectionPad* getDirectionPad() const;
|
||||
|
||||
/**
|
||||
All face buttons are required to be analog in the Extended profile. These must be arranged
|
||||
in the diamond pattern given below:
|
||||
Y
|
||||
/ \
|
||||
X B
|
||||
\ /
|
||||
A
|
||||
*/
|
||||
ControllerButtonInput* getButtonA() const;
|
||||
ControllerButtonInput* getButtonB() const;
|
||||
ControllerButtonInput* getButtonX() const;
|
||||
ControllerButtonInput* getButtonY() const;
|
||||
|
||||
// Select button
|
||||
ControllerButtonInput* getButtonSelect() const;
|
||||
|
||||
// Start button
|
||||
ControllerButtonInput* getButtonStart() const;
|
||||
|
||||
// Pause button
|
||||
ControllerButtonInput* getButtonPause() const;
|
||||
|
||||
// Shoulder buttons
|
||||
ControllerButtonInput* getLeftShoulder() const;
|
||||
ControllerButtonInput* getRightShoulder() const;
|
||||
|
||||
// API for Extended Gamepad only
|
||||
|
||||
/**
|
||||
A thumbstick is a 2-axis control that is physically required to be analog. All the elements of this directional input are thus analog.
|
||||
*/
|
||||
ControllerThumbstick* getLeftThumbstick() const;
|
||||
ControllerThumbstick* getRightThumbstick() const;
|
||||
|
||||
/**
|
||||
If Trigger button supports analog inputs, it could be used as acceleration and decelleration in a driving game for example.
|
||||
*/
|
||||
ControllerButtonInput* getLeftTrigger() const;
|
||||
ControllerButtonInput* getRightTrigger() const;
|
||||
|
||||
protected:
|
||||
|
||||
Gamepad();
|
||||
virtual ~Gamepad();
|
||||
|
||||
friend class Controller;
|
||||
Controller* _controller;
|
||||
|
||||
ControllerDirectionPad* _directionPad;
|
||||
|
||||
ControllerButtonInput* _buttonA;
|
||||
ControllerButtonInput* _buttonB;
|
||||
ControllerButtonInput* _buttonX;
|
||||
ControllerButtonInput* _buttonY;
|
||||
|
||||
ControllerButtonInput* _buttonSelect;
|
||||
ControllerButtonInput* _buttonStart;
|
||||
ControllerButtonInput* _buttonPause;
|
||||
|
||||
ControllerButtonInput* _leftShoulder;
|
||||
ControllerButtonInput* _rightShoulder;
|
||||
|
||||
ControllerThumbstick* _leftThumbstick;
|
||||
ControllerThumbstick* _rightThumbstick;
|
||||
|
||||
ControllerButtonInput* _leftTrigger;
|
||||
ControllerButtonInput* _rightTrigger;
|
||||
};
|
||||
|
||||
NS_CC_END
|
||||
|
||||
#endif /* defined(__cocos2d_libs__CCGamepad__) */
|
|
@ -35,6 +35,7 @@ import android.view.InputDevice;
|
|||
import android.view.KeyEvent;
|
||||
import android.view.MotionEvent;
|
||||
import android.util.Log;
|
||||
import android.util.SparseArray;
|
||||
|
||||
public abstract class GameControllerActivity extends Cocos2dxActivity implements InputDeviceListener {
|
||||
// ===========================================================
|
||||
|
@ -189,14 +190,16 @@ public abstract class GameControllerActivity extends Cocos2dxActivity implements
|
|||
if (mControllerNibiru != null) {
|
||||
handled |= mControllerNibiru.dispatchKeyEvent(event);
|
||||
}
|
||||
if (mControllerMoga != null) {
|
||||
if (handled == false && mControllerMoga != null) {
|
||||
handled |= mControllerMoga.dispatchKeyEvent(event);
|
||||
}
|
||||
if (mControllerOuya != null) {
|
||||
if (handled == false && mControllerOuya != null) {
|
||||
handled |= mControllerOuya.dispatchKeyEvent(event);
|
||||
}
|
||||
|
||||
handled |= mControllerHelper.dispatchKeyEvent(event);
|
||||
if (handled == false) {
|
||||
handled |= mControllerHelper.dispatchKeyEvent(event);
|
||||
}
|
||||
|
||||
return handled || super.dispatchKeyEvent(event);
|
||||
}
|
||||
|
@ -207,31 +210,44 @@ public abstract class GameControllerActivity extends Cocos2dxActivity implements
|
|||
if (mControllerNibiru != null) {
|
||||
handled |= mControllerNibiru.dispatchGenericMotionEvent(event);
|
||||
}
|
||||
if (mControllerMoga != null) {
|
||||
if (handled == false && mControllerMoga != null) {
|
||||
handled |= mControllerMoga.dispatchGenericMotionEvent(event);
|
||||
}
|
||||
if (mControllerOuya != null) {
|
||||
if (handled == false && mControllerOuya != null) {
|
||||
handled |= mControllerOuya.dispatchGenericMotionEvent(event);
|
||||
}
|
||||
|
||||
handled |= mControllerHelper.dispatchGenericMotionEvent(event);
|
||||
if (handled == false) {
|
||||
handled |= mControllerHelper.dispatchGenericMotionEvent(event);
|
||||
}
|
||||
|
||||
return handled || super.dispatchGenericMotionEvent(event);
|
||||
}
|
||||
|
||||
protected SparseArray<String> mGameController = null;
|
||||
|
||||
@Override
|
||||
public void onInputDeviceAdded(int deviceId) {
|
||||
|
||||
public void onInputDeviceAdded(int deviceId) {
|
||||
Log.d(TAG,"onInputDeviceAdded:" + deviceId);
|
||||
|
||||
InputDevice device = InputDevice.getDevice(deviceId);
|
||||
int deviceSource = device.getSources();
|
||||
|
||||
if ( ((deviceSource & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
|
||||
|| ((deviceSource & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) )
|
||||
{
|
||||
GameControllerAdapter.onConnected("Standard", deviceId);
|
||||
try {
|
||||
InputDevice device = InputDevice.getDevice(deviceId);
|
||||
int deviceSource = device.getSources();
|
||||
|
||||
if ( ((deviceSource & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
|
||||
|| ((deviceSource & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) )
|
||||
{
|
||||
if (mGameController == null) {
|
||||
mGameController = new SparseArray<String>();
|
||||
}
|
||||
String deviceName = device.getName();
|
||||
mGameController.append(deviceId, deviceName);
|
||||
GameControllerAdapter.onConnected(deviceName, deviceId);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* This is an unusual case. Input devices don't typically change, but they
|
||||
|
@ -244,7 +260,7 @@ public abstract class GameControllerActivity extends Cocos2dxActivity implements
|
|||
*/
|
||||
@Override
|
||||
public void onInputDeviceChanged(int deviceId) {
|
||||
Log.d(TAG,"onInputDeviceChanged:" + deviceId);
|
||||
Log.w(TAG,"onInputDeviceChanged:" + deviceId);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -258,13 +274,9 @@ public abstract class GameControllerActivity extends Cocos2dxActivity implements
|
|||
public void onInputDeviceRemoved(int deviceId) {
|
||||
Log.d(TAG,"onInputDeviceRemoved:" + deviceId);
|
||||
|
||||
InputDevice device = InputDevice.getDevice(deviceId);
|
||||
int deviceSource = device.getSources();
|
||||
|
||||
if ( ((deviceSource & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
|
||||
|| ((deviceSource & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) )
|
||||
{
|
||||
GameControllerAdapter.onDisconnected("Standard", deviceId);
|
||||
if (mGameController != null && mGameController.get(deviceId) != null) {
|
||||
GameControllerAdapter.onDisconnected(mGameController.get(deviceId), deviceId);
|
||||
mGameController.delete(deviceId);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -77,86 +77,67 @@ public class GameControllerHelper {
|
|||
{
|
||||
if (event.getAction() == MotionEvent.ACTION_MOVE) {
|
||||
int devicedId = event.getDeviceId();
|
||||
String deviceName = event.getDevice().getName();
|
||||
|
||||
float newAXIS_LX = event.getAxisValue(AXIS_X);
|
||||
if (Float.compare(newAXIS_LX , mOldLeftThumbstickX) != 0) {
|
||||
GameControllerAdapter.onAxisEvent(StandardControllerName, devicedId, GameControllerDelegate.THUMBSTICK_LEFT_X, newAXIS_LX, true);
|
||||
GameControllerAdapter.onAxisEvent(deviceName, devicedId, GameControllerDelegate.THUMBSTICK_LEFT_X, newAXIS_LX, true);
|
||||
mOldLeftThumbstickX = newAXIS_LX;
|
||||
handled = true;
|
||||
}
|
||||
|
||||
float newAXIS_LY = event.getAxisValue(AXIS_Y);
|
||||
if (Float.compare(newAXIS_LY , mOldLeftThumbstickY) != 0) {
|
||||
GameControllerAdapter.onAxisEvent(StandardControllerName, devicedId, GameControllerDelegate.THUMBSTICK_LEFT_Y, newAXIS_LY, true);
|
||||
GameControllerAdapter.onAxisEvent(deviceName, devicedId, GameControllerDelegate.THUMBSTICK_LEFT_Y, newAXIS_LY, true);
|
||||
mOldLeftThumbstickY = newAXIS_LY;
|
||||
handled = true;
|
||||
}
|
||||
|
||||
float newAXIS_RX = event.getAxisValue(AXIS_Z);
|
||||
if (Float.compare(newAXIS_RX , mOldRightThumbstickX) != 0) {
|
||||
GameControllerAdapter.onAxisEvent(StandardControllerName, devicedId, GameControllerDelegate.THUMBSTICK_RIGHT_X, newAXIS_RX, true);
|
||||
GameControllerAdapter.onAxisEvent(deviceName, devicedId, GameControllerDelegate.THUMBSTICK_RIGHT_X, newAXIS_RX, true);
|
||||
mOldRightThumbstickX = newAXIS_RX;
|
||||
handled = true;
|
||||
}
|
||||
|
||||
float newAXIS_RY = event.getAxisValue(AXIS_RZ);
|
||||
if (Float.compare(newAXIS_RY , mOldRightThumbstickY) != 0) {
|
||||
GameControllerAdapter.onAxisEvent(StandardControllerName, devicedId, GameControllerDelegate.THUMBSTICK_RIGHT_Y, newAXIS_RY, true);
|
||||
GameControllerAdapter.onAxisEvent(deviceName, devicedId, GameControllerDelegate.THUMBSTICK_RIGHT_Y, newAXIS_RY, true);
|
||||
mOldRightThumbstickY = newAXIS_RY;
|
||||
handled = true;
|
||||
}
|
||||
|
||||
float newAXIS_LTRIGGER = event.getAxisValue(AXIS_LTRIGGER);
|
||||
if (Float.compare(newAXIS_LTRIGGER , mOldLeftTrigger) != 0) {
|
||||
if (Float.compare(newAXIS_LTRIGGER, 0.0f) == 0) {
|
||||
GameControllerAdapter.onButtonEvent(StandardControllerName, devicedId, GameControllerDelegate.BUTTON_LEFT_TRIGGER, false, 0.0f, true);
|
||||
}else {
|
||||
GameControllerAdapter.onButtonEvent(StandardControllerName, devicedId, GameControllerDelegate.BUTTON_LEFT_TRIGGER, true, newAXIS_LTRIGGER, true);
|
||||
}
|
||||
GameControllerAdapter.onAxisEvent(deviceName, devicedId, GameControllerDelegate.BUTTON_LEFT_TRIGGER, newAXIS_LTRIGGER, true);
|
||||
mOldLeftTrigger = newAXIS_LTRIGGER;
|
||||
handled = true;
|
||||
}
|
||||
|
||||
float newAXIS_RTRIGGER = event.getAxisValue(AXIS_RTRIGGER);
|
||||
if (Float.compare(newAXIS_RTRIGGER , mOldRightTrigger) != 0) {
|
||||
if (Float.compare(newAXIS_RTRIGGER, 0.0f) == 0) {
|
||||
GameControllerAdapter.onButtonEvent(StandardControllerName, devicedId, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, false, 0.0f, true);
|
||||
}else {
|
||||
GameControllerAdapter.onButtonEvent(StandardControllerName, devicedId, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, true, newAXIS_RTRIGGER, true);
|
||||
}
|
||||
GameControllerAdapter.onAxisEvent(deviceName, devicedId, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, newAXIS_RTRIGGER, true);
|
||||
mOldRightTrigger = newAXIS_RTRIGGER;
|
||||
handled = true;
|
||||
}
|
||||
|
||||
float newAXIS_BRAKE = event.getAxisValue(AXIS_BRAKE);
|
||||
if (Float.compare(newAXIS_BRAKE , mOldBrake) != 0) {
|
||||
if (Float.compare(newAXIS_BRAKE, 0.0f) == 0) {
|
||||
GameControllerAdapter.onButtonEvent(StandardControllerName, devicedId, GameControllerDelegate.BUTTON_LEFT_TRIGGER, false, 0.0f, true);
|
||||
}else {
|
||||
GameControllerAdapter.onButtonEvent(StandardControllerName, devicedId, GameControllerDelegate.BUTTON_LEFT_TRIGGER, true, newAXIS_BRAKE, true);
|
||||
}
|
||||
GameControllerAdapter.onAxisEvent(deviceName, devicedId, GameControllerDelegate.BUTTON_LEFT_TRIGGER, newAXIS_BRAKE, true);
|
||||
mOldBrake = newAXIS_BRAKE;
|
||||
handled = true;
|
||||
}
|
||||
|
||||
float newAXIS_THROTTLE = event.getAxisValue(AXIS_THROTTLE);
|
||||
if (Float.compare(newAXIS_THROTTLE , mOldThrottle) != 0) {
|
||||
if (Float.compare(newAXIS_THROTTLE, 0.0f) == 0) {
|
||||
GameControllerAdapter.onButtonEvent(StandardControllerName, devicedId, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, false, 0.0f, true);
|
||||
}else {
|
||||
GameControllerAdapter.onButtonEvent(StandardControllerName, devicedId, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, true, newAXIS_THROTTLE, true);
|
||||
}
|
||||
GameControllerAdapter.onAxisEvent(deviceName, devicedId, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, newAXIS_THROTTLE, true);
|
||||
mOldThrottle = newAXIS_THROTTLE;
|
||||
handled = true;
|
||||
}
|
||||
|
||||
float newAXIS_GAS = event.getAxisValue(AXIS_GAS);
|
||||
if (Float.compare(newAXIS_GAS , mOldGas) != 0) {
|
||||
if (Float.compare(newAXIS_GAS, 0.0f) == 0) {
|
||||
GameControllerAdapter.onButtonEvent(StandardControllerName, devicedId, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, false, 0.0f, true);
|
||||
}else {
|
||||
GameControllerAdapter.onButtonEvent(StandardControllerName, devicedId, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, true, newAXIS_GAS, true);
|
||||
}
|
||||
GameControllerAdapter.onAxisEvent(deviceName, devicedId, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, newAXIS_GAS, true);
|
||||
mOldGas = newAXIS_GAS;
|
||||
handled = true;
|
||||
}
|
||||
|
@ -170,19 +151,19 @@ public class GameControllerHelper {
|
|||
boolean handled = false;
|
||||
|
||||
int eventSource = event.getSource();
|
||||
int controllerKey = ControllerKeyMap.get(event.getKeyCode());
|
||||
int keyCode = event.getKeyCode();
|
||||
int controllerKey = ControllerKeyMap.get(keyCode);
|
||||
|
||||
if (controllerKey != 0 && (((eventSource & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
|
||||
|| ((eventSource & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK)
|
||||
|| ((eventSource & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD)))
|
||||
|| ((eventSource & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) ))
|
||||
{
|
||||
int action = event.getAction();
|
||||
if (action == KeyEvent.ACTION_DOWN) {
|
||||
handled = true;
|
||||
GameControllerAdapter.onButtonEvent(StandardControllerName,event.getDeviceId(), controllerKey,true, 1.0f, false);
|
||||
GameControllerAdapter.onButtonEvent(event.getDevice().getName(),event.getDeviceId(), controllerKey,true, 1.0f, false);
|
||||
}else if (action == KeyEvent.ACTION_UP) {
|
||||
handled = true;
|
||||
GameControllerAdapter.onButtonEvent(StandardControllerName,event.getDeviceId(), controllerKey,false, 0.0f, false);
|
||||
GameControllerAdapter.onButtonEvent(event.getDevice().getName(),event.getDeviceId(), controllerKey,false, 0.0f, false);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -129,28 +129,18 @@ public class GameControllerMoga implements ControllerListener, GameControllerDel
|
|||
|
||||
float newLeftTrigger = event.getAxisValue(MotionEvent.AXIS_LTRIGGER);
|
||||
if (newLeftTrigger != mOldLeftTrigger) {
|
||||
boolean isPressed = true;
|
||||
if (Float.compare(newLeftTrigger, 0.0f) == 0) {
|
||||
isPressed = false;
|
||||
}
|
||||
mControllerEventListener.onButtonEvent(mVendorName,
|
||||
mControllerEventListener.onAxisEvent(mVendorName,
|
||||
controllerId,
|
||||
GameControllerDelegate.BUTTON_LEFT_TRIGGER,
|
||||
isPressed,
|
||||
newLeftTrigger, true);
|
||||
mOldLeftTrigger = newLeftTrigger;
|
||||
}
|
||||
|
||||
float newRightTrigger = event.getAxisValue(MotionEvent.AXIS_RTRIGGER);
|
||||
if (newRightTrigger != mOldRightTrigger) {
|
||||
boolean isPressed = true;
|
||||
if (Float.compare(newRightTrigger, 0.0f) == 0) {
|
||||
isPressed = false;
|
||||
}
|
||||
mControllerEventListener.onButtonEvent(mVendorName,
|
||||
mControllerEventListener.onAxisEvent(mVendorName,
|
||||
controllerId,
|
||||
GameControllerDelegate.BUTTON_RIGHT_TRIGGER,
|
||||
isPressed,
|
||||
newRightTrigger, true);
|
||||
mOldRightTrigger = newRightTrigger;
|
||||
}
|
||||
|
|
|
@ -66,21 +66,13 @@ public class GameControllerOuya implements GameControllerDelegate{
|
|||
|
||||
float newLeftTrigger = c.getAxisValue(OuyaController.AXIS_L2);
|
||||
if (Float.compare(newLeftTrigger, mOldLeftTrigger) != 0) {
|
||||
if (Float.compare(newLeftTrigger, 0.0f) == 0) {
|
||||
mControllerEventListener.onButtonEvent(sVendorName, controllerID, GameControllerDelegate.BUTTON_LEFT_TRIGGER, false, 0.0f, true);
|
||||
}else {
|
||||
mControllerEventListener.onButtonEvent(sVendorName, controllerID, GameControllerDelegate.BUTTON_LEFT_TRIGGER, true, newLeftTrigger, true);
|
||||
}
|
||||
mControllerEventListener.onAxisEvent(sVendorName, controllerID, GameControllerDelegate.BUTTON_LEFT_TRIGGER, newLeftTrigger, true);
|
||||
mOldLeftTrigger = newLeftTrigger;
|
||||
}
|
||||
|
||||
float newRightTrigger = c.getAxisValue(OuyaController.AXIS_R2);
|
||||
if (Float.compare(newRightTrigger, mOldRightTrigger) != 0) {
|
||||
if (Float.compare(newRightTrigger, 0.0f) == 0) {
|
||||
mControllerEventListener.onButtonEvent(sVendorName, controllerID, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, false, 0.0f, true);
|
||||
}else {
|
||||
mControllerEventListener.onButtonEvent(sVendorName, controllerID, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, true, newRightTrigger, true);
|
||||
}
|
||||
mControllerEventListener.onAxisEvent(sVendorName, controllerID, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, newRightTrigger, true);
|
||||
mOldRightTrigger = newRightTrigger;
|
||||
}
|
||||
|
||||
|
|
|
@ -6,34 +6,36 @@ import android.view.MotionEvent;
|
|||
|
||||
public interface GameControllerDelegate {
|
||||
|
||||
public static final int THUMBSTICK_LEFT_X = 100;
|
||||
public static final int THUMBSTICK_LEFT_Y = 101;
|
||||
public static final int THUMBSTICK_RIGHT_X = 102;
|
||||
public static final int THUMBSTICK_RIGHT_Y = 103;
|
||||
static final int KEY_BASE = 1000;
|
||||
|
||||
public static final int THUMBSTICK_LEFT_X = KEY_BASE;
|
||||
public static final int THUMBSTICK_LEFT_Y = KEY_BASE + 1;
|
||||
public static final int THUMBSTICK_RIGHT_X = KEY_BASE + 2;
|
||||
public static final int THUMBSTICK_RIGHT_Y = KEY_BASE + 3;
|
||||
|
||||
public static final int BUTTON_A = 110;
|
||||
public static final int BUTTON_B = 111;
|
||||
public static final int BUTTON_C = 112;
|
||||
public static final int BUTTON_X = 113;
|
||||
public static final int BUTTON_Y = 114;
|
||||
public static final int BUTTON_Z = 115;
|
||||
public static final int BUTTON_A = KEY_BASE + 4;
|
||||
public static final int BUTTON_B = KEY_BASE + 5;
|
||||
public static final int BUTTON_C = KEY_BASE + 6;
|
||||
public static final int BUTTON_X = KEY_BASE + 7;
|
||||
public static final int BUTTON_Y = KEY_BASE + 8;
|
||||
public static final int BUTTON_Z = KEY_BASE + 9;
|
||||
|
||||
public static final int BUTTON_LEFT_SHOULDER = 120;
|
||||
public static final int BUTTON_RIGHT_SHOULDER = 121;
|
||||
public static final int BUTTON_LEFT_TRIGGER = 122;
|
||||
public static final int BUTTON_RIGHT_TRIGGER = 123;
|
||||
public static final int BUTTON_DPAD_UP = KEY_BASE + 10;
|
||||
public static final int BUTTON_DPAD_DOWN = KEY_BASE + 11;
|
||||
public static final int BUTTON_DPAD_LEFT = KEY_BASE + 12;
|
||||
public static final int BUTTON_DPAD_RIGHT = KEY_BASE + 13;
|
||||
public static final int BUTTON_DPAD_CENTER = KEY_BASE + 14;
|
||||
|
||||
public static final int BUTTON_DPAD_UP = 130;
|
||||
public static final int BUTTON_DPAD_DOWN = 131;
|
||||
public static final int BUTTON_DPAD_LEFT = 132;
|
||||
public static final int BUTTON_DPAD_RIGHT = 133;
|
||||
public static final int BUTTON_DPAD_CENTER = 134;
|
||||
public static final int BUTTON_LEFT_SHOULDER = KEY_BASE + 15;
|
||||
public static final int BUTTON_RIGHT_SHOULDER = KEY_BASE + 16;
|
||||
public static final int BUTTON_LEFT_TRIGGER = KEY_BASE + 17;
|
||||
public static final int BUTTON_RIGHT_TRIGGER = KEY_BASE + 18;
|
||||
|
||||
public static final int BUTTON_LEFT_THUMBSTICK = 140;
|
||||
public static final int BUTTON_RIGHT_THUMBSTICK = 141;
|
||||
public static final int BUTTON_LEFT_THUMBSTICK = KEY_BASE + 19;
|
||||
public static final int BUTTON_RIGHT_THUMBSTICK = KEY_BASE + 20;
|
||||
|
||||
public static final int BUTTON_START = 150;
|
||||
public static final int BUTTON_SELECT = 151;
|
||||
public static final int BUTTON_START = KEY_BASE + 21;
|
||||
public static final int BUTTON_SELECT = KEY_BASE + 22;
|
||||
|
||||
void onCreate(Context context);
|
||||
void onPause();
|
||||
|
|
|
@ -18,23 +18,16 @@ AppDelegate::~AppDelegate()
|
|||
}
|
||||
|
||||
bool AppDelegate::applicationDidFinishLaunching() {
|
||||
// initialize director
|
||||
auto director = Director::getInstance();
|
||||
auto glview = director->getOpenGLView();
|
||||
if(!glview) {
|
||||
glview = GLView::create("Cpp Empty Test");
|
||||
glview = GLView::create("Game Controller Test");
|
||||
director->setOpenGLView(glview);
|
||||
}
|
||||
|
||||
director->setOpenGLView(glview);
|
||||
|
||||
// Set the design resolution
|
||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
|
||||
// a bug in DirectX 11 level9-x on the device prevents ResolutionPolicy::NO_BORDER from working correctly
|
||||
glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::SHOW_ALL);
|
||||
#else
|
||||
glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);
|
||||
#endif
|
||||
|
||||
Size frameSize = glview->getFrameSize();
|
||||
|
||||
|
@ -53,33 +46,19 @@ bool AppDelegate::applicationDidFinishLaunching() {
|
|||
director->setContentScaleFactor(MIN(largeResource.size.height/designResolutionSize.height, largeResource.size.width/designResolutionSize.width));
|
||||
}
|
||||
// if the frame's height is larger than the height of small resource size, select medium resource.
|
||||
else if (frameSize.height > smallResource.size.height)
|
||||
else
|
||||
{
|
||||
searchPath.push_back(mediumResource.directory);
|
||||
|
||||
director->setContentScaleFactor(MIN(mediumResource.size.height/designResolutionSize.height, mediumResource.size.width/designResolutionSize.width));
|
||||
}
|
||||
// if the frame's height is smaller than the height of medium resource size, select small resource.
|
||||
else
|
||||
{
|
||||
searchPath.push_back(smallResource.directory);
|
||||
|
||||
director->setContentScaleFactor(MIN(smallResource.size.height/designResolutionSize.height, smallResource.size.width/designResolutionSize.width));
|
||||
}
|
||||
|
||||
// set searching path
|
||||
FileUtils::getInstance()->setSearchPaths(searchPath);
|
||||
|
||||
// turn on display FPS
|
||||
director->setDisplayStats(true);
|
||||
|
||||
// set FPS. the default value is 1.0/60 if you don't call this
|
||||
director->setAnimationInterval(1.0 / 60);
|
||||
auto scene = Scene::create();
|
||||
GameControllerTest *gameLayer = GameControllerTest::create();
|
||||
scene->addChild(gameLayer);
|
||||
|
||||
// create a scene. it's an autorelease object
|
||||
auto scene = GameControllerTest::scene();
|
||||
|
||||
// run
|
||||
director->runWithScene(scene);
|
||||
|
||||
return true;
|
||||
|
@ -88,15 +67,9 @@ bool AppDelegate::applicationDidFinishLaunching() {
|
|||
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
|
||||
void AppDelegate::applicationDidEnterBackground() {
|
||||
Director::getInstance()->stopAnimation();
|
||||
|
||||
// if you use SimpleAudioEngine, it must be pause
|
||||
// SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
|
||||
}
|
||||
|
||||
// this function will be called when the app is active again
|
||||
void AppDelegate::applicationWillEnterForeground() {
|
||||
Director::getInstance()->startAnimation();
|
||||
|
||||
// if you use SimpleAudioEngine, it must resume here
|
||||
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
|
||||
}
|
||||
|
|
|
@ -23,32 +23,16 @@
|
|||
[Note] Normally, developer just need to define one design resolution(e.g. 960x640) with one or more resources.
|
||||
*/
|
||||
|
||||
#define DESIGN_RESOLUTION_480X320 0
|
||||
#define DESIGN_RESOLUTION_1024X768 1
|
||||
#define DESIGN_RESOLUTION_2048X1536 2
|
||||
|
||||
/* If you want to switch design resolution, change next line */
|
||||
#define TARGET_DESIGN_RESOLUTION_SIZE DESIGN_RESOLUTION_480X320
|
||||
|
||||
typedef struct tagResource
|
||||
{
|
||||
cocos2d::Size size;
|
||||
char directory[100];
|
||||
}Resource;
|
||||
|
||||
static Resource smallResource = { cocos2d::Size(480, 320), "iphone" };
|
||||
static Resource mediumResource = { cocos2d::Size(1024, 768), "ipad" };
|
||||
static Resource largeResource = { cocos2d::Size(2048, 1536), "ipadhd" };
|
||||
static Resource mediumResource = { cocos2d::Size(960, 540), "ipad" };
|
||||
static Resource largeResource = { cocos2d::Size(1920, 1080), "ipadhd" };
|
||||
|
||||
#if (TARGET_DESIGN_RESOLUTION_SIZE == DESIGN_RESOLUTION_480X320)
|
||||
static cocos2d::Size designResolutionSize = cocos2d::Size(480, 320);
|
||||
#elif (TARGET_DESIGN_RESOLUTION_SIZE == DESIGN_RESOLUTION_1024X768)
|
||||
static cocos2d::Size designResolutionSize = cocos2d::Size(1024, 768);
|
||||
#elif (TARGET_DESIGN_RESOLUTION_SIZE == DESIGN_RESOLUTION_2048X1536)
|
||||
static cocos2d::Size designResolutionSize = cocos2d::Size(2048, 1536);
|
||||
#else
|
||||
#error unknown target design resolution!
|
||||
#endif
|
||||
static cocos2d::Size designResolutionSize = cocos2d::Size(1920, 1080);
|
||||
|
||||
// The font size 24 is designed for small resolution, so we should change it to fit for current design resolution
|
||||
#define TITLE_FONT_SIZE (cocos2d::Director::getInstance()->getOpenGLView()->getDesignResolutionSize().width / smallResource.size.width * 24)
|
||||
|
|
|
@ -1,252 +1,421 @@
|
|||
#include "GameControllerTest.h"
|
||||
#include "AppMacros.h"
|
||||
#include "ui/CocosGUI.h"
|
||||
|
||||
USING_NS_CC;
|
||||
|
||||
|
||||
Scene* GameControllerTest::scene()
|
||||
{
|
||||
auto scene = Scene::create();
|
||||
GameControllerTest *layer = GameControllerTest::create();
|
||||
scene->addChild(layer);
|
||||
|
||||
return scene;
|
||||
}
|
||||
|
||||
GameControllerTest::~GameControllerTest()
|
||||
{
|
||||
Controller::stopDiscoveryController();
|
||||
}
|
||||
|
||||
bool GameControllerTest::init()
|
||||
void GameControllerTest::registerControllerListener()
|
||||
{
|
||||
if ( !Layer::init() )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
auto visibleSize = Director::getInstance()->getVisibleSize();
|
||||
auto origin = Director::getInstance()->getVisibleOrigin();
|
||||
|
||||
auto tmpPos = Vec2(visibleSize / 2) + origin;
|
||||
_actor = Sprite::create("CloseNormal.png");
|
||||
_actor->setPosition(tmpPos);
|
||||
this->addChild(_actor,10);
|
||||
|
||||
tmpPos.y -= 35;
|
||||
_statusLabel = Label::createWithTTF("status", "fonts/Marker Felt.ttf", 40);
|
||||
_statusLabel->setPosition(tmpPos);
|
||||
this->addChild(_statusLabel, 0, 100);
|
||||
|
||||
tmpPos.y += 65;
|
||||
_leftTriggerLabel = Label::createWithTTF("left trigger", "fonts/Marker Felt.ttf", 40);
|
||||
_leftTriggerLabel->setPosition(tmpPos);
|
||||
this->addChild(_leftTriggerLabel, 0, 100);
|
||||
|
||||
tmpPos.y += 40;
|
||||
_rightTriggerLabel = Label::createWithTTF("right trigger", "fonts/Marker Felt.ttf", 40);
|
||||
_rightTriggerLabel->setPosition(tmpPos);
|
||||
this->addChild(_rightTriggerLabel, 0, 100);
|
||||
|
||||
_listener = EventListenerController::create();
|
||||
_listener->onConnected = [=](Controller* controller, Event* event){
|
||||
_player1 = controller;
|
||||
_statusLabel->setString("controller connected!");
|
||||
};
|
||||
|
||||
_listener->onDisconnected = [=](Controller* controller, Event* event){
|
||||
_player1 = nullptr;
|
||||
_statusLabel->setString("controller disconnected!");
|
||||
};
|
||||
_listener->onConnected = CC_CALLBACK_2(GameControllerTest::onConnectController,this);
|
||||
_listener->onDisconnected = CC_CALLBACK_2(GameControllerTest::onDisconnectedController,this);
|
||||
_listener->onKeyDown = CC_CALLBACK_3(GameControllerTest::onKeyDown, this);
|
||||
_listener->onKeyUp = CC_CALLBACK_3(GameControllerTest::onKeyUp, this);
|
||||
_listener->onAxisEvent = CC_CALLBACK_3(GameControllerTest::onAxisEvent, this);
|
||||
|
||||
_listener->onButtonPressed = CC_CALLBACK_3(GameControllerTest::onButtonPressed, this);
|
||||
_listener->onButtonReleased = CC_CALLBACK_3(GameControllerTest::onButtonReleased, this);
|
||||
_listener->onAxisValueChanged = CC_CALLBACK_3(GameControllerTest::onAxisValueChanged, this);
|
||||
|
||||
_eventDispatcher->addEventListenerWithSceneGraphPriority(_listener, this);
|
||||
|
||||
Controller::startDiscoveryController();
|
||||
|
||||
|
||||
//get game pad status in polling mode
|
||||
//scheduleUpdate();
|
||||
}
|
||||
|
||||
void GameControllerTest::onConnectController(Controller* controller, Event* event)
|
||||
{
|
||||
if (controller == nullptr || controller == _firstHolder.controller || controller == _secondHolder.controller)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_firstHolder.controller == nullptr && _secondHolder.controller == nullptr)
|
||||
{
|
||||
char deviceId[20];
|
||||
sprintf(deviceId,"device id:%d",controller->getDeviceId());
|
||||
|
||||
if (_firstHolder._holderNode)
|
||||
{
|
||||
_firstHolder.controller = controller;
|
||||
_firstHolder._deviceIdLabel->setString(deviceId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_secondHolder.controller = controller;
|
||||
_secondHolder._deviceIdLabel->setString(deviceId);
|
||||
}
|
||||
}
|
||||
else if(_secondHolder.controller == nullptr)
|
||||
{
|
||||
_secondHolder.controller = controller;
|
||||
if (_secondHolder._holderNode == nullptr)
|
||||
{
|
||||
createControllerSprite(_secondHolder);
|
||||
|
||||
_firstHolder._holderNode->runAction(ScaleTo::create(0.3f,0.5f,0.5f));
|
||||
_firstHolder._holderNode->runAction(MoveTo::create(0.3f,Vec2(_visibleQuarterX, _visibleCentreY)));
|
||||
|
||||
_secondHolder._holderNode->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
_secondHolder._holderNode->setScale(0.1f);
|
||||
_secondHolder._holderNode->runAction(ScaleTo::create(0.3f,0.5f,0.5f));
|
||||
_secondHolder._holderNode->setPosition(Vec2(_visibleThreeQuarterX, _visibleCentreY));
|
||||
this->addChild(_secondHolder._holderNode);
|
||||
}
|
||||
|
||||
char deviceId[20];
|
||||
sprintf(deviceId,"device id:%d",controller->getDeviceId());
|
||||
_secondHolder._deviceIdLabel->setString(deviceId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_firstHolder.controller = controller;
|
||||
if (_firstHolder._holderNode == nullptr)
|
||||
{
|
||||
createControllerSprite(_firstHolder);
|
||||
|
||||
_secondHolder._holderNode->runAction(ScaleTo::create(0.3f,0.5f,0.5f));
|
||||
_secondHolder._holderNode->runAction(MoveTo::create(0.3f,Vec2(_visibleQuarterX, _visibleCentreY)));
|
||||
|
||||
_firstHolder._holderNode->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
_firstHolder._holderNode->setScale(0.1f);
|
||||
_firstHolder._holderNode->runAction(ScaleTo::create(0.3f,0.5f,0.5f));
|
||||
_firstHolder._holderNode->setPosition(Vec2(_visibleThreeQuarterX, _visibleCentreY));
|
||||
this->addChild(_firstHolder._holderNode);
|
||||
}
|
||||
char deviceId[20];
|
||||
sprintf(deviceId,"device id:%d",controller->getDeviceId());
|
||||
_firstHolder._deviceIdLabel->setString(deviceId);
|
||||
}
|
||||
}
|
||||
|
||||
void GameControllerTest::onDisconnectedController(Controller* controller, Event* event)
|
||||
{
|
||||
log("onDisconnectedController:%d",controller->getDeviceId());
|
||||
if (_firstHolder.controller == controller)
|
||||
{
|
||||
_firstHolder.controller = nullptr;
|
||||
|
||||
if (_secondHolder.controller != nullptr)
|
||||
{
|
||||
_firstHolder._holderNode->runAction(MoveBy::create(0.3f,Vec2(0,_visibleSize.height)));
|
||||
|
||||
_secondHolder._holderNode->runAction(MoveTo::create(0.3f,Vec2(_visibleCentreX,_visibleCentreY) ));
|
||||
_secondHolder._holderNode->runAction(ScaleTo::create(0.3f,1.0f,1.0f));
|
||||
}
|
||||
else
|
||||
{
|
||||
resetControllerHolderState(_firstHolder);
|
||||
}
|
||||
}
|
||||
else if (_secondHolder.controller == controller)
|
||||
{
|
||||
_secondHolder.controller = nullptr;
|
||||
|
||||
if (_firstHolder.controller != nullptr)
|
||||
{
|
||||
_secondHolder._holderNode->runAction(MoveBy::create(0.3f,Vec2(0,_visibleSize.height)));
|
||||
|
||||
_firstHolder._holderNode->runAction(MoveTo::create(0.3f,Vec2(_visibleCentreX,_visibleCentreY) ));
|
||||
_firstHolder._holderNode->runAction(ScaleTo::create(0.3f,1.0f,1.0f));
|
||||
}
|
||||
else
|
||||
{
|
||||
resetControllerHolderState(_secondHolder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GameControllerTest::resetControllerHolderState(ControllerHolder& holder)
|
||||
{
|
||||
holder._buttonA->setColor(Color3B::WHITE);
|
||||
holder._buttonB->setColor(Color3B::WHITE);
|
||||
holder._buttonX->setColor(Color3B::WHITE);
|
||||
holder._buttonY->setColor(Color3B::WHITE);
|
||||
holder._dpadUp->setColor(Color3B::WHITE);
|
||||
holder._dpadDown->setColor(Color3B::WHITE);
|
||||
holder._dpadLeft->setColor(Color3B::WHITE);
|
||||
holder._dpadRight->setColor(Color3B::WHITE);
|
||||
holder._buttonL1->setColor(Color3B::WHITE);
|
||||
holder._buttonR1->setColor(Color3B::WHITE);
|
||||
|
||||
holder._leftJoystick->setPosition(Vec2(238,460));
|
||||
holder._rightJoystick->setPosition(Vec2(606,293));
|
||||
holder._deviceIdLabel->setString("Disconnected");
|
||||
}
|
||||
|
||||
void GameControllerTest::showButtonState(cocos2d::Controller *controller, int keyCode, bool isPressed)
|
||||
{
|
||||
onConnectController(controller,nullptr);
|
||||
ControllerHolder* holder = nullptr;
|
||||
if (controller == _firstHolder.controller)
|
||||
holder = &_firstHolder;
|
||||
else if(controller == _secondHolder.controller)
|
||||
holder = &_secondHolder;
|
||||
else
|
||||
return;
|
||||
|
||||
if (isPressed)
|
||||
{
|
||||
switch (keyCode)
|
||||
{
|
||||
case Controller::Key::BUTTON_A:
|
||||
holder->_buttonA->setColor(Color3B(250,103,93));
|
||||
break;
|
||||
case Controller::Key::BUTTON_B:
|
||||
holder->_buttonB->setColor(Color3B(92,214,183));
|
||||
break;
|
||||
case Controller::Key::BUTTON_X:
|
||||
holder->_buttonX->setColor(Color3B(96,113,192));
|
||||
break;
|
||||
case Controller::Key::BUTTON_Y:
|
||||
holder->_buttonY->setColor(Color3B(199,222,118));
|
||||
break;
|
||||
case Controller::Key::BUTTON_DPAD_UP:
|
||||
holder->_dpadUp->setColor(Color3B(0,115,158));
|
||||
break;
|
||||
case Controller::Key::BUTTON_DPAD_DOWN:
|
||||
holder->_dpadDown->setColor(Color3B(0,115,158));
|
||||
break;
|
||||
case Controller::Key::BUTTON_DPAD_LEFT:
|
||||
holder->_dpadLeft->setColor(Color3B(170,216,0));
|
||||
break;
|
||||
case Controller::Key::BUTTON_DPAD_RIGHT:
|
||||
holder->_dpadRight->setColor(Color3B(170,216,0));
|
||||
break;
|
||||
case Controller::Key::BUTTON_LEFT_SHOULDER:
|
||||
holder->_buttonL1->setColor(Color3B(19,231,238));
|
||||
break;
|
||||
case Controller::Key::BUTTON_RIGHT_SHOULDER:
|
||||
holder->_buttonR1->setColor(Color3B(19,231,238));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (keyCode)
|
||||
{
|
||||
case Controller::Key::BUTTON_A:
|
||||
holder->_buttonA->setColor(Color3B::WHITE);
|
||||
break;
|
||||
case Controller::Key::BUTTON_B:
|
||||
holder->_buttonB->setColor(Color3B::WHITE);
|
||||
break;
|
||||
case Controller::Key::BUTTON_X:
|
||||
holder->_buttonX->setColor(Color3B::WHITE);
|
||||
break;
|
||||
case Controller::Key::BUTTON_Y:
|
||||
holder->_buttonY->setColor(Color3B::WHITE);
|
||||
break;
|
||||
case Controller::Key::BUTTON_DPAD_UP:
|
||||
holder->_dpadUp->setColor(Color3B::WHITE);
|
||||
break;
|
||||
case Controller::Key::BUTTON_DPAD_DOWN:
|
||||
holder->_dpadDown->setColor(Color3B::WHITE);
|
||||
break;
|
||||
case Controller::Key::BUTTON_DPAD_LEFT:
|
||||
holder->_dpadLeft->setColor(Color3B::WHITE);
|
||||
break;
|
||||
case Controller::Key::BUTTON_DPAD_RIGHT:
|
||||
holder->_dpadRight->setColor(Color3B::WHITE);
|
||||
break;
|
||||
case Controller::Key::BUTTON_LEFT_SHOULDER:
|
||||
holder->_buttonL1->setColor(Color3B::WHITE);
|
||||
break;
|
||||
case Controller::Key::BUTTON_RIGHT_SHOULDER:
|
||||
holder->_buttonR1->setColor(Color3B::WHITE);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GameControllerTest::onKeyDown(cocos2d::Controller *controller, int keyCode, cocos2d::Event *event)
|
||||
{
|
||||
showButtonState(controller, keyCode, true);
|
||||
}
|
||||
|
||||
void GameControllerTest::onKeyUp(cocos2d::Controller *controller, int keyCode, cocos2d::Event *event)
|
||||
{
|
||||
showButtonState(controller, keyCode, false);
|
||||
}
|
||||
|
||||
void GameControllerTest::onAxisEvent(cocos2d::Controller* controller, int keyCode, cocos2d::Event* event)
|
||||
{
|
||||
//onConnectController(controller,nullptr);
|
||||
ControllerHolder* holder = nullptr;
|
||||
if (controller == _firstHolder.controller)
|
||||
holder = &_firstHolder;
|
||||
else if(controller == _secondHolder.controller)
|
||||
holder = &_secondHolder;
|
||||
else
|
||||
return;
|
||||
|
||||
const auto& ketStatus = controller->getKeyStatus(keyCode);
|
||||
switch (keyCode)
|
||||
{
|
||||
case Controller::Key::JOYSTICK_LEFT_X:
|
||||
holder->_leftJoystick->setPositionX(238 + ketStatus.value * 24);
|
||||
break;
|
||||
case Controller::Key::JOYSTICK_LEFT_Y:
|
||||
holder->_leftJoystick->setPositionY(460 - ketStatus.value * 24);
|
||||
break;
|
||||
case Controller::Key::JOYSTICK_RIGHT_X:
|
||||
holder->_rightJoystick->setPositionX(606 + ketStatus.value * 24);
|
||||
break;
|
||||
case Controller::Key::JOYSTICK_RIGHT_Y:
|
||||
holder->_rightJoystick->setPositionY(293 - ketStatus.value * 24);
|
||||
break;
|
||||
case Controller::Key::AXIS_LEFT_TRIGGER:
|
||||
holder->_buttonL2->setOpacity(200 * controller->getKeyStatus(keyCode).value);
|
||||
break;
|
||||
case Controller::Key::AXIS_RIGHT_TRIGGER:
|
||||
holder->_buttonR2->setOpacity(200 * controller->getKeyStatus(keyCode).value);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool GameControllerTest::init()
|
||||
{
|
||||
if ( !Layer::init() )
|
||||
return false;
|
||||
|
||||
_currControllerCount = 0;
|
||||
|
||||
_visibleSize = Director::getInstance()->getVisibleSize();
|
||||
_visibleOrigin = Director::getInstance()->getVisibleOrigin();
|
||||
|
||||
|
||||
auto tmpPos = Vec2(_visibleSize / 2) + _visibleOrigin;
|
||||
|
||||
_visibleCentreX = _visibleOrigin.x + _visibleSize.width / 2;
|
||||
_visibleCentreY = _visibleOrigin.y + _visibleSize.height / 2;
|
||||
_visibleQuarterX = _visibleOrigin.x + _visibleSize.width / 4;
|
||||
_visibleThreeQuarterX = _visibleOrigin.x + _visibleSize.width * 0.75f;
|
||||
|
||||
_firstHolder.controller = nullptr;
|
||||
_secondHolder.controller = nullptr;
|
||||
|
||||
createControllerSprite(_firstHolder);
|
||||
_firstHolder._holderNode->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
_firstHolder._holderNode->setPosition(tmpPos);
|
||||
this->addChild(_firstHolder._holderNode);
|
||||
|
||||
auto closeItem = MenuItemImage::create("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(GameControllerTest::menuCloseCallback, this));
|
||||
closeItem->setPosition(origin + visibleSize - closeItem->getContentSize() / 2);
|
||||
|
||||
closeItem->setPosition(_visibleOrigin + _visibleSize - closeItem->getContentSize() / 2);
|
||||
|
||||
auto menu = Menu::create(closeItem,nullptr);
|
||||
menu->setPosition(Vec2::ZERO);
|
||||
this->addChild(menu);
|
||||
|
||||
//get game pad status in polling mode
|
||||
scheduleUpdate();
|
||||
|
||||
registerControllerListener();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void GameControllerTest::onButtonPressed(cocos2d::Controller *controller, cocos2d::ControllerButtonInput *button, cocos2d::Event *event)
|
||||
void GameControllerTest::createControllerSprite(ControllerHolder& holder)
|
||||
{
|
||||
if (controller == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_player1 = controller;
|
||||
holder._holderNode = Node::create();
|
||||
holder._holderNode->setContentSize(Size(998,1000));
|
||||
|
||||
auto gamePad = controller->getGamepad();
|
||||
auto controllerBg1 = Sprite::create("controller-1.png");
|
||||
controllerBg1->setPosition(Vec2::ZERO);
|
||||
controllerBg1->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
|
||||
holder._holderNode->addChild(controllerBg1);
|
||||
|
||||
if (button == gamePad->getButtonA())
|
||||
{
|
||||
_statusLabel->setString("button A pressed!");
|
||||
}
|
||||
auto controllerBg2 = Sprite::create("controller-2.png");
|
||||
controllerBg2->setPosition(Vec2(499,1000));
|
||||
controllerBg2->setAnchorPoint(Vec2::ANCHOR_MIDDLE_TOP);
|
||||
holder._holderNode->addChild(controllerBg2);
|
||||
|
||||
if (button == gamePad->getButtonB())
|
||||
{
|
||||
_statusLabel->setString("button B pressed!");
|
||||
}
|
||||
holder._leftJoystick = Sprite::create("joystick.png");
|
||||
holder._leftJoystick->setPosition(Vec2(238,460));
|
||||
holder._holderNode->addChild(holder._leftJoystick);
|
||||
|
||||
if (button == gamePad->getButtonX())
|
||||
{
|
||||
_statusLabel->setString("button X pressed!");
|
||||
}
|
||||
holder._rightJoystick = Sprite::create("joystick.png");
|
||||
holder._rightJoystick->setPosition(Vec2(606,293));
|
||||
holder._holderNode->addChild(holder._rightJoystick);
|
||||
|
||||
if (button == gamePad->getButtonY())
|
||||
{
|
||||
_statusLabel->setString("button Y pressed!");
|
||||
}
|
||||
holder._deviceIdLabel = Label::createWithTTF("Disconnected","fonts/Marker Felt.ttf",36);
|
||||
holder._deviceIdLabel->setPosition(Vec2(499,460));
|
||||
holder._deviceIdLabel->setTextColor(Color4B::RED);
|
||||
holder._holderNode->addChild(holder._deviceIdLabel);
|
||||
//-----------------------------------------------------------------
|
||||
//371,294 64
|
||||
auto dPadTexture = Director::getInstance()->getTextureCache()->addImage("dPad.png");
|
||||
|
||||
if (button == gamePad->getDirectionPad()->getUp())
|
||||
{
|
||||
_statusLabel->setString("Dpad up pressed!");
|
||||
}
|
||||
auto dPadCenter = Sprite::createWithTexture(dPadTexture,Rect(60,60,68,68));
|
||||
dPadCenter->setPosition(Vec2(371,294));
|
||||
holder._holderNode->addChild(dPadCenter);
|
||||
|
||||
if (button == gamePad->getDirectionPad()->getDown())
|
||||
{
|
||||
_statusLabel->setString("Dpad down pressed!");
|
||||
}
|
||||
holder._dpadLeft = Sprite::createWithTexture(dPadTexture,Rect(0,60,60,60));
|
||||
holder._dpadLeft->setPosition(Vec2(371 - 64,296));
|
||||
holder._holderNode->addChild(holder._dpadLeft);
|
||||
|
||||
if (button == gamePad->getDirectionPad()->getLeft())
|
||||
{
|
||||
_statusLabel->setString("Dpad left pressed!");
|
||||
}
|
||||
holder._dpadRight = Sprite::createWithTexture(dPadTexture,Rect(128,60,60,60));
|
||||
holder._dpadRight->setPosition(Vec2(371 + 64,296));
|
||||
holder._holderNode->addChild(holder._dpadRight);
|
||||
|
||||
if (button == gamePad->getDirectionPad()->getRight())
|
||||
{
|
||||
_statusLabel->setString("Dpad right pressed!");
|
||||
}
|
||||
holder._dpadUp = Sprite::createWithTexture(dPadTexture,Rect(60,0,60,60));
|
||||
holder._dpadUp->setPosition(Vec2(369,294 + 64));
|
||||
holder._holderNode->addChild(holder._dpadUp);
|
||||
|
||||
if (button == gamePad->getLeftShoulder())
|
||||
{
|
||||
_statusLabel->setString("Left shoulder pressed!");
|
||||
}
|
||||
holder._dpadDown = Sprite::createWithTexture(dPadTexture,Rect(60,128,60,60));
|
||||
holder._dpadDown->setPosition(Vec2(369,294 - 64));
|
||||
holder._holderNode->addChild(holder._dpadDown);
|
||||
//-----------------------------------------------------------------
|
||||
holder._buttonL1 = Sprite::create("L1.png");
|
||||
holder._buttonL1->setPosition(Vec2(290,792));
|
||||
holder._holderNode->addChild(holder._buttonL1);
|
||||
|
||||
if (button == gamePad->getRightShoulder())
|
||||
{
|
||||
_statusLabel->setString("Right shoulder pressed!");
|
||||
}
|
||||
}
|
||||
holder._buttonR1 = Sprite::create("R1.png");
|
||||
holder._buttonR1->setPosition(Vec2(998 - 290,792));
|
||||
holder._holderNode->addChild(holder._buttonR1);
|
||||
|
||||
void GameControllerTest::onButtonReleased(cocos2d::Controller *controller, cocos2d::ControllerButtonInput *button, cocos2d::Event *event)
|
||||
{
|
||||
if (controller == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_player1 = controller;
|
||||
auto buttonL2 = Sprite::create("L2.png");
|
||||
buttonL2->setPosition(Vec2(220,910));
|
||||
holder._holderNode->addChild(buttonL2);
|
||||
|
||||
auto gamePad = controller->getGamepad();
|
||||
auto buttonR2 = Sprite::create("R2.png");
|
||||
buttonR2->setPosition(Vec2(998-220,910));
|
||||
holder._holderNode->addChild(buttonR2);
|
||||
|
||||
if (button == gamePad->getButtonA())
|
||||
{
|
||||
_statusLabel->setString("button A released!");
|
||||
}
|
||||
holder._buttonL2 = Sprite::create("L2.png");
|
||||
holder._buttonL2->setOpacity(0);
|
||||
holder._buttonL2->setColor(Color3B::RED);
|
||||
holder._buttonL2->setPosition(Vec2(220,910));
|
||||
holder._holderNode->addChild(holder._buttonL2);
|
||||
|
||||
if (button == gamePad->getButtonB())
|
||||
{
|
||||
_statusLabel->setString("button B released!");
|
||||
}
|
||||
holder._buttonR2 = Sprite::create("R2.png");
|
||||
holder._buttonR2->setOpacity(0);
|
||||
holder._buttonR2->setColor(Color3B::RED);
|
||||
holder._buttonR2->setPosition(Vec2(998-220,910));
|
||||
holder._holderNode->addChild(holder._buttonR2);
|
||||
//-----------------------------------------------------------------
|
||||
//750 460,70
|
||||
holder._buttonX = Sprite::create("X.png");
|
||||
holder._buttonX->setPosition(Vec2(750 - 70,460));
|
||||
holder._holderNode->addChild(holder._buttonX);
|
||||
|
||||
if (button == gamePad->getButtonX())
|
||||
{
|
||||
_statusLabel->setString("button X released!");
|
||||
}
|
||||
holder._buttonY = Sprite::create("Y.png");
|
||||
holder._buttonY->setPosition(Vec2(750,460 + 70));
|
||||
holder._holderNode->addChild(holder._buttonY);
|
||||
|
||||
if (button == gamePad->getButtonY())
|
||||
{
|
||||
_statusLabel->setString("button Y released!");
|
||||
}
|
||||
holder._buttonA = Sprite::create("A.png");
|
||||
holder._buttonA->setPosition(Vec2(750,460 - 70));
|
||||
holder._holderNode->addChild(holder._buttonA);
|
||||
|
||||
if (button == gamePad->getDirectionPad()->getUp())
|
||||
{
|
||||
_statusLabel->setString("Dpad up released!");
|
||||
}
|
||||
|
||||
if (button == gamePad->getDirectionPad()->getDown())
|
||||
{
|
||||
_statusLabel->setString("Dpad down released!");
|
||||
}
|
||||
|
||||
if (button == gamePad->getDirectionPad()->getLeft())
|
||||
{
|
||||
_statusLabel->setString("Dpad left released!");
|
||||
}
|
||||
|
||||
if (button == gamePad->getDirectionPad()->getRight())
|
||||
{
|
||||
_statusLabel->setString("Dpad right released!");
|
||||
}
|
||||
|
||||
if (button == gamePad->getLeftShoulder())
|
||||
{
|
||||
_statusLabel->setString("Left shoulder released!");
|
||||
}
|
||||
|
||||
if (button == gamePad->getRightShoulder())
|
||||
{
|
||||
_statusLabel->setString("Right shoulder released!");
|
||||
}
|
||||
}
|
||||
|
||||
void GameControllerTest::onAxisValueChanged(cocos2d::Controller* controller, cocos2d::ControllerAxisInput* axis, cocos2d::Event* event)
|
||||
{
|
||||
if (controller == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_player1 = controller;
|
||||
|
||||
auto moveDelta = axis->getValue();
|
||||
Vec2 newPos = _actor->getPosition();
|
||||
auto gamePad = controller->getGamepad();
|
||||
|
||||
if (axis == gamePad->getLeftThumbstick()->getAxisX() || axis == gamePad->getRightThumbstick()->getAxisX())
|
||||
{
|
||||
newPos.x += moveDelta;
|
||||
}
|
||||
else if (axis == gamePad->getLeftThumbstick()->getAxisY() || axis == gamePad->getRightThumbstick()->getAxisY())
|
||||
{
|
||||
newPos.y -= moveDelta;
|
||||
}
|
||||
_actor->setPosition(newPos);
|
||||
}
|
||||
|
||||
void GameControllerTest::update(float dt)
|
||||
{
|
||||
if (_player1 && _player1->isConnected())
|
||||
{
|
||||
Vec2 newPos = _actor->getPosition();
|
||||
auto gamePad = _player1->getGamepad();
|
||||
|
||||
newPos.x += gamePad->getLeftThumbstick()->getAxisX()->getValue();
|
||||
newPos.y -= gamePad->getLeftThumbstick()->getAxisY()->getValue();
|
||||
|
||||
newPos.x += gamePad->getRightThumbstick()->getAxisX()->getValue();
|
||||
newPos.y -= gamePad->getRightThumbstick()->getAxisY()->getValue();
|
||||
|
||||
_actor->setPosition(newPos);
|
||||
|
||||
char triggerStatus[50];
|
||||
sprintf(triggerStatus,"left trigger:%f",gamePad->getLeftTrigger()->getValue());
|
||||
_leftTriggerLabel->setString(triggerStatus);
|
||||
sprintf(triggerStatus,"right trigger:%f",gamePad->getRightTrigger()->getValue());
|
||||
_rightTriggerLabel->setString(triggerStatus);
|
||||
}
|
||||
holder._buttonB = Sprite::create("B.png");
|
||||
holder._buttonB->setPosition(Vec2(750 + 70,460));
|
||||
holder._holderNode->addChild(holder._buttonB);
|
||||
}
|
||||
|
||||
void GameControllerTest::menuCloseCallback(Ref* sender)
|
||||
|
|
|
@ -4,32 +4,75 @@
|
|||
#include "cocos2d.h"
|
||||
#include "base/CCGameController.h"
|
||||
|
||||
USING_NS_CC;
|
||||
|
||||
class GameControllerTest : public cocos2d::Layer
|
||||
{
|
||||
public:
|
||||
virtual ~GameControllerTest();
|
||||
|
||||
virtual bool init();
|
||||
|
||||
static cocos2d::Scene* scene();
|
||||
|
||||
// a selector callback
|
||||
void menuCloseCallback(Ref* sender);
|
||||
|
||||
// implement the "static node()" method manually
|
||||
CREATE_FUNC(GameControllerTest);
|
||||
|
||||
void update(float dt);
|
||||
void onButtonPressed(cocos2d::Controller* controller, cocos2d::ControllerButtonInput* button, cocos2d::Event* event);
|
||||
void onButtonReleased(cocos2d::Controller* controller, cocos2d::ControllerButtonInput* button, cocos2d::Event* event);
|
||||
void onAxisValueChanged(cocos2d::Controller* controller, cocos2d::ControllerAxisInput* axis, cocos2d::Event* event);
|
||||
virtual bool init();
|
||||
|
||||
void onKeyDown(cocos2d::Controller* controller, int keyCode, cocos2d::Event* event);
|
||||
void onKeyUp(cocos2d::Controller* controller, int keyCode, cocos2d::Event* event);
|
||||
void onAxisEvent(cocos2d::Controller* controller, int keyCode, cocos2d::Event* event);
|
||||
|
||||
void menuCloseCallback(Ref* sender);
|
||||
|
||||
private:
|
||||
cocos2d::Controller* _player1;
|
||||
cocos2d::Sprite* _actor;
|
||||
cocos2d::Label* _statusLabel;
|
||||
cocos2d::Label* _leftTriggerLabel;
|
||||
cocos2d::Label* _rightTriggerLabel;
|
||||
float _visibleCentreX;
|
||||
float _visibleCentreY;
|
||||
float _visibleQuarterX;
|
||||
float _visibleThreeQuarterX;
|
||||
|
||||
int _currControllerCount;
|
||||
|
||||
typedef struct controllerHolder
|
||||
{
|
||||
cocos2d::Controller *controller;
|
||||
|
||||
cocos2d::Node* _holderNode;
|
||||
|
||||
cocos2d::Sprite* _leftJoystick;
|
||||
cocos2d::Sprite* _rightJoystick;
|
||||
|
||||
cocos2d::Sprite* _dpadLeft;
|
||||
cocos2d::Sprite* _dpadRight;
|
||||
cocos2d::Sprite* _dpadUp;
|
||||
cocos2d::Sprite* _dpadDown;
|
||||
|
||||
cocos2d::Sprite* _buttonX;
|
||||
cocos2d::Sprite* _buttonY;
|
||||
cocos2d::Sprite* _buttonA;
|
||||
cocos2d::Sprite* _buttonB;
|
||||
|
||||
cocos2d::Sprite* _buttonL1;
|
||||
cocos2d::Sprite* _buttonR1;
|
||||
cocos2d::Sprite* _buttonL2;
|
||||
cocos2d::Sprite* _buttonR2;
|
||||
|
||||
cocos2d::Label* _deviceIdLabel;
|
||||
}ControllerHolder;
|
||||
|
||||
ControllerHolder _firstHolder;
|
||||
ControllerHolder _secondHolder;
|
||||
|
||||
cocos2d::EventListenerController* _listener;
|
||||
|
||||
cocos2d::Size _visibleSize;
|
||||
cocos2d::Vec2 _visibleOrigin;
|
||||
|
||||
void registerControllerListener();
|
||||
void showButtonState(cocos2d::Controller *controller, int keyCode, bool isPressed);
|
||||
|
||||
void createControllerSprite(ControllerHolder& holder);
|
||||
|
||||
void onConnectController(Controller* controller, Event* event);
|
||||
void onDisconnectedController(Controller* controller, Event* event);
|
||||
|
||||
void resetControllerHolderState(ControllerHolder& holder);
|
||||
};
|
||||
|
||||
#endif // __GAMECONTROLLER_TEST_H__
|
||||
|
|
After Width: | Height: | Size: 4.5 KiB |
After Width: | Height: | Size: 4.4 KiB |
After Width: | Height: | Size: 4.5 KiB |
After Width: | Height: | Size: 5.0 KiB |
After Width: | Height: | Size: 4.6 KiB |
After Width: | Height: | Size: 5.0 KiB |
After Width: | Height: | Size: 4.5 KiB |
After Width: | Height: | Size: 4.4 KiB |
After Width: | Height: | Size: 41 KiB |
After Width: | Height: | Size: 16 KiB |
After Width: | Height: | Size: 6.1 KiB |
After Width: | Height: | Size: 1.6 KiB |
After Width: | Height: | Size: 2.5 KiB |
After Width: | Height: | Size: 5.6 KiB |
After Width: | Height: | Size: 5.6 KiB |
After Width: | Height: | Size: 5.6 KiB |
After Width: | Height: | Size: 6.8 KiB |
After Width: | Height: | Size: 5.8 KiB |
After Width: | Height: | Size: 6.8 KiB |
After Width: | Height: | Size: 5.6 KiB |
After Width: | Height: | Size: 5.4 KiB |
After Width: | Height: | Size: 76 KiB |
After Width: | Height: | Size: 28 KiB |
After Width: | Height: | Size: 12 KiB |
After Width: | Height: | Size: 6.4 KiB |