From 1d47737f45eea16fee774e6f990b0e14d5ae0963 Mon Sep 17 00:00:00 2001 From: boyu0 Date: Mon, 9 Sep 2013 10:29:02 +0800 Subject: [PATCH 01/28] issue #2771: add physics files and entries. --- .../project.pbxproj.REMOVED.git-id | 2 +- cocos2dx/base_nodes/CCNode.cpp | 22 ++++ cocos2dx/base_nodes/CCNode.h | 35 ++++++ .../CCScene.cpp | 19 ++- .../layers_scenes_transitions_nodes/CCScene.h | 14 ++- cocos2dx/physics/CCPhysicsBody.cpp | 24 ++++ cocos2dx/physics/CCPhysicsBody.h | 100 +++++++++++++++ cocos2dx/physics/CCPhysicsContactDelegate.cpp | 23 ++++ cocos2dx/physics/CCPhysicsContactDelegate.h | 48 ++++++++ cocos2dx/physics/CCPhysicsFixture.cpp | 23 ++++ cocos2dx/physics/CCPhysicsFixture.h | 59 +++++++++ cocos2dx/physics/CCPhysicsJoint.cpp | 23 ++++ cocos2dx/physics/CCPhysicsJoint.h | 114 ++++++++++++++++++ cocos2dx/physics/CCPhysicsSetting.h | 40 ++++++ cocos2dx/physics/CCPhysicsWorld.cpp | 93 ++++++++++++++ cocos2dx/physics/CCPhysicsWorld.h | 72 +++++++++++ .../ArmatureTest/ArmatureScene.cpp | 6 +- 17 files changed, 710 insertions(+), 7 deletions(-) create mode 100644 cocos2dx/physics/CCPhysicsBody.cpp create mode 100644 cocos2dx/physics/CCPhysicsBody.h create mode 100644 cocos2dx/physics/CCPhysicsContactDelegate.cpp create mode 100644 cocos2dx/physics/CCPhysicsContactDelegate.h create mode 100644 cocos2dx/physics/CCPhysicsFixture.cpp create mode 100644 cocos2dx/physics/CCPhysicsFixture.h create mode 100644 cocos2dx/physics/CCPhysicsJoint.cpp create mode 100644 cocos2dx/physics/CCPhysicsJoint.h create mode 100644 cocos2dx/physics/CCPhysicsSetting.h create mode 100644 cocos2dx/physics/CCPhysicsWorld.cpp create mode 100644 cocos2dx/physics/CCPhysicsWorld.h diff --git a/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id b/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id index ff4b7a32dd..1eaf467207 100644 --- a/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -fd0dee451420604712c1327679395cd1faca91b6 \ No newline at end of file +cbb5290ea17f6020e0249818c08c9d94a17dd073 \ No newline at end of file diff --git a/cocos2dx/base_nodes/CCNode.cpp b/cocos2dx/base_nodes/CCNode.cpp index ef111dfbce..bc8aa7c95f 100644 --- a/cocos2dx/base_nodes/CCNode.cpp +++ b/cocos2dx/base_nodes/CCNode.cpp @@ -120,6 +120,7 @@ Node::Node(void) , _isTransitionFinished(false) , _updateScriptHandler(0) , _componentContainer(NULL) +, _scene(nullptr) { // set default scheduler and actionManager Director *director = Director::getInstance(); @@ -588,6 +589,7 @@ void Node::addChild(Node *child, int zOrder, int tag) child->_tag = tag; child->setParent(this); + child->setScene(_scene); child->setOrderOfArrival(s_globalOrderOfArrival++); if( _running ) @@ -1271,6 +1273,26 @@ void Node::removeAllComponents() _componentContainer->removeAll(); } +void Node::setScene(Scene* scene) +{ + _scene = scene; +} + +Scene* Node::getScene() +{ + return _scene; +} + +void Node::setPhysicsBody(PhysicsBody* body) +{ + _physicsBody = body; +} + +PhysicsBody* Node::getPhysicsBody() +{ + return _physicsBody; +} + // NodeRGBA NodeRGBA::NodeRGBA() : _displayedOpacity(255) diff --git a/cocos2dx/base_nodes/CCNode.h b/cocos2dx/base_nodes/CCNode.h index 60c9f0ee11..389218453a 100644 --- a/cocos2dx/base_nodes/CCNode.h +++ b/cocos2dx/base_nodes/CCNode.h @@ -52,6 +52,8 @@ class ActionManager; class Component; class Dictionary; class ComponentContainer; +class Scene; +class PhysicsBody; /** * @addtogroup base_nodes @@ -1300,6 +1302,35 @@ public: */ virtual void removeAllComponents(); /// @} end of component functions + + /// @{ + /// @name scene functions + + /** + * set the scene the node belongs to + * DO NOT call it manually + */ + virtual void setScene(Scene* scene); + + /** + * get the scene the node belongs to + */ + virtual Scene* getScene(); + /// @} end of scene functions + + /// @{ + /// @name physics functions + + /** + * set the PhysicsBody that let the node effect with physics + */ + virtual void setPhysicsBody(PhysicsBody* body); + + /** + * get the PhysicsBody the node have + */ + PhysicsBody* getPhysicsBody(); + /// @} end of physics functions protected: /// lazy allocs @@ -1378,6 +1409,9 @@ protected: ccScriptType _scriptType; ///< type of script binding, lua or javascript ComponentContainer *_componentContainer; ///< Dictionary of components + + Scene* _scene; ///< the scene the node belongs to + PhysicsBody* _physicsBody; ///< the physicsBody the node have }; @@ -1425,6 +1459,7 @@ protected: Color3B _realColor; bool _cascadeColorEnabled; bool _cascadeOpacityEnabled; + }; // end of base_node group diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp index 4a144f9b80..3c3e2faf1a 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp @@ -53,7 +53,7 @@ bool Scene::init() return bRet; } -Scene *Scene::create() +Scene *Scene::create(bool usePhysics/* = false*/) { Scene *pRet = new Scene(); if (pRet && pRet->init()) @@ -68,4 +68,21 @@ Scene *Scene::create() } } +void Scene::addChild(Node* child) +{ + Node::addChild(child); +} + +void Scene::addChild(Node* child, int zOrder) +{ + Node::addChild(child, zOrder); +} + +void Scene::addChild(Node* child, int zOrder, int tag) +{ + Node::addChild(child, zOrder, tag); + child->setScene(this); +} + + NS_CC_END diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCScene.h b/cocos2dx/layers_scenes_transitions_nodes/CCScene.h index 2cd710773e..c084852513 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCScene.h +++ b/cocos2dx/layers_scenes_transitions_nodes/CCScene.h @@ -31,6 +31,8 @@ THE SOFTWARE. NS_CC_BEGIN +class PhysicsWorld; + /** * @addtogroup scene * @{ @@ -50,13 +52,21 @@ class CC_DLL Scene : public Node { public: /** creates a new Scene object */ - static Scene *create(void); + static Scene *create(bool usePhysics = false); Scene(); virtual ~Scene(); bool init(); - + + virtual void addChild(Node* child) override; + virtual void addChild(Node* child, int zOrder) override; + virtual void addChild(Node* child, int zOrder, int tag) override; + + inline PhysicsWorld* getPhysicsWorld() { return _physicsWorld; } + +protected: + PhysicsWorld* _physicsWorld; }; // end of scene group diff --git a/cocos2dx/physics/CCPhysicsBody.cpp b/cocos2dx/physics/CCPhysicsBody.cpp new file mode 100644 index 0000000000..e4b81264af --- /dev/null +++ b/cocos2dx/physics/CCPhysicsBody.cpp @@ -0,0 +1,24 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "CCPhysicsBody.h" \ No newline at end of file diff --git a/cocos2dx/physics/CCPhysicsBody.h b/cocos2dx/physics/CCPhysicsBody.h new file mode 100644 index 0000000000..682c6d7e03 --- /dev/null +++ b/cocos2dx/physics/CCPhysicsBody.h @@ -0,0 +1,100 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 __CCPHYSICS_BODY_H__ +#define __CCPHYSICS_BODY_H__ + +#include "CCObject.h" +#include "CCGeometry.h" +#include "CCPhysicsSetting.h" + +NS_CC_BEGIN +class Node; +class PhysicsWorld; +class PhysicsFixture; + +class PhysicsBody : public Object +{ +public: + static PhysicsBody* createCircle(Point centre, float radius); + static PhysicsBody* createRectangle(Rect rect); + static PhysicsBody* createPolygon(Array* points); + + static PhysicsBody* createEdgeSegment(Point x, Point y); + static PhysicsBody* createEdgeCircle(Point centre, float radius); + static PhysicsBody* createEdgeRectangle(Rect rect); + static PhysicsBody* createEdgePolygon(Array* points); + static PhysicsBody* createEdgeChain(Array* points); + + virtual void applyForce(Point force); + virtual void applyForce(Point force, Point point); + virtual void applyImpulse(Point impulse); + virtual void applyImpulse(Point impulse, Point point); + virtual void applyTorque(float torque); + virtual void applyAngularImpulse(float impulse); + + void addFixture(PhysicsFixture* fixture); + inline Array* getFixtures() { return _fixtures; } + void removeFixture(PhysicsFixture* fixture); + void removeAllFixtures(); + + inline PhysicsWorld* getWorld() { return _physicsWorld; } + inline Array* getJoints() { return _joints; } + + void setCategoryBitmask(int bitmask); + inline int getCategoryBitmask() { return _categoryBitmask; } + void setContactTestBitmask(int bitmask); + inline int getContactTestBitmask() { return _contactTestBitmask; } + void setCollisionBitmask(int bitmask); + inline int getCollisionBitmask() { return _collisionBitmask; } + +protected: + bool init(); + +protected: + PhysicsBody(); + virtual ~PhysicsBody(); + +private: + float _mass; + float _density; + float _area; + float _friction; + Node* _node; + Point _velocity; + float _angularVelocity; + bool _resting; + + int _categoryBitmask; + int _contactTestBitmask; + int _collisionBitmask; + + Array* _joints; + Array* _fixtures; + PhysicsWorld* _physicsWorld; +}; + +NS_CC_END + +#endif // __CCPHYSICS_BODY_H__ \ No newline at end of file diff --git a/cocos2dx/physics/CCPhysicsContactDelegate.cpp b/cocos2dx/physics/CCPhysicsContactDelegate.cpp new file mode 100644 index 0000000000..3dba18f438 --- /dev/null +++ b/cocos2dx/physics/CCPhysicsContactDelegate.cpp @@ -0,0 +1,23 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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. + ****************************************************************************/ \ No newline at end of file diff --git a/cocos2dx/physics/CCPhysicsContactDelegate.h b/cocos2dx/physics/CCPhysicsContactDelegate.h new file mode 100644 index 0000000000..0e5f137ef8 --- /dev/null +++ b/cocos2dx/physics/CCPhysicsContactDelegate.h @@ -0,0 +1,48 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 __CCPHYSICS_CONTACTDELEGATE_H__ +#define __CCPHYSICS_CONTACTDELEGATE_H__ + +#include "CCObject.h" +#include "CCGeometry.h" +#include "CCPhysicsSetting.h" + +NS_CC_BEGIN + +class PhysicsBody; + +class PhysicsContactDelegate : public Object +{ +public: + virtual void onContactBegin(PhysicsBody* bodyA, PhysicsBody* bodyB, float collisionImpulse, Point contactPoint); + virtual void onContactEnd(PhysicsBody* bodyA, PhysicsBody* bodyB, float collisionImpulse, Point contactPoint); + +protected: + PhysicsContactDelegate(); + virtual ~PhysicsContactDelegate(); +}; + +NS_CC_END +#endif //__CCPHYSICS_CONTACTDELEGATE_H__ \ No newline at end of file diff --git a/cocos2dx/physics/CCPhysicsFixture.cpp b/cocos2dx/physics/CCPhysicsFixture.cpp new file mode 100644 index 0000000000..3dba18f438 --- /dev/null +++ b/cocos2dx/physics/CCPhysicsFixture.cpp @@ -0,0 +1,23 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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. + ****************************************************************************/ \ No newline at end of file diff --git a/cocos2dx/physics/CCPhysicsFixture.h b/cocos2dx/physics/CCPhysicsFixture.h new file mode 100644 index 0000000000..40b914cc38 --- /dev/null +++ b/cocos2dx/physics/CCPhysicsFixture.h @@ -0,0 +1,59 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 __CCPHYSICS_FIXTURE_H__ +#define __CCPHYSICS_FIXTURE_H__ + +#include "CCObject.h" +#include "CCGeometry.h" +#include "CCPhysicsSetting.h" + +NS_CC_BEGIN + +class PhysicsFixture : public Object +{ +public: + static PhysicsFixture* createCircle(Point centre, float radius); + static PhysicsFixture* createRectangle(Rect rect); + static PhysicsFixture* createPolygon(Array* points); + + static PhysicsFixture* createEdgeSegment(Point x, Point y); + static PhysicsFixture* createEdgeCircle(Point centre, float radius); + static PhysicsFixture* createEdgeRectangle(Rect rect); + static PhysicsFixture* createEdgePolygon(Array* points); + +protected: + bool init(); + +protected: + PhysicsFixture(); + virtual ~PhysicsFixture(); + +protected: + float _density; + float _friction; +}; + +NS_CC_END +#endif // __CCPHYSICS_FIXTURE_H__ \ No newline at end of file diff --git a/cocos2dx/physics/CCPhysicsJoint.cpp b/cocos2dx/physics/CCPhysicsJoint.cpp new file mode 100644 index 0000000000..3dba18f438 --- /dev/null +++ b/cocos2dx/physics/CCPhysicsJoint.cpp @@ -0,0 +1,23 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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. + ****************************************************************************/ \ No newline at end of file diff --git a/cocos2dx/physics/CCPhysicsJoint.h b/cocos2dx/physics/CCPhysicsJoint.h new file mode 100644 index 0000000000..5258e1d2d5 --- /dev/null +++ b/cocos2dx/physics/CCPhysicsJoint.h @@ -0,0 +1,114 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 __CCPHYSICS_JOINT_H__ +#define __CCPHYSICS_JOINT_H__ + +#include "CCObject.h" +#include "CCGeometry.h" +#include "CCPhysicsSetting.h" + +NS_CC_BEGIN + +class PhysicsBody; + +class PhysicsJoint : public Object +{ +protected: + PhysicsJoint(); + virtual ~PhysicsJoint(); + +private: + PhysicsBody* _bodyA; + PhysicsBody* _bodyB; +}; + +class PhysicsJointFixed : public PhysicsJoint +{ +public: + PhysicsJointFixed* create(); + +protected: + bool init(); + +protected: + PhysicsJointFixed(); + ~PhysicsJointFixed(); +}; + +class PhysicsJointSliding : public PhysicsJoint +{ +public: + PhysicsJointSliding* create(); + +protected: + bool init(); + +protected: + PhysicsJointSliding(); + ~PhysicsJointSliding(); +}; + +class PhysicsJointSpring : public PhysicsJoint +{ +public: + PhysicsJointSpring* create(); + +protected: + bool init(); + +protected: + PhysicsJointSpring(); + ~PhysicsJointSpring(); +}; + +class PhysicsJointLimit : public PhysicsJoint +{ +public: + PhysicsJointLimit* create(); + +protected: + bool init(); + +protected: + PhysicsJointLimit(); + ~PhysicsJointLimit(); +}; + +class PhysicsJointPin : public PhysicsJoint +{ +public: + PhysicsJointPin* create(); + +protected: + bool init(); + +protected: + PhysicsJointPin(); + ~PhysicsJointPin(); +}; + +NS_CC_END + +#endif // __CCPHYSICS_JOINT_H__ \ No newline at end of file diff --git a/cocos2dx/physics/CCPhysicsSetting.h b/cocos2dx/physics/CCPhysicsSetting.h new file mode 100644 index 0000000000..8bcc65e7b2 --- /dev/null +++ b/cocos2dx/physics/CCPhysicsSetting.h @@ -0,0 +1,40 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 __CCPHYSICS_SETTING_H__ +#define __CCPHYSICS_SETTING_H__ + +#define CC_PHYSICS_UNKNOWN 0 +#define CC_PHYSICS_BOX2D 1 +#define CC_PHYSICS_CHIPMUNK 2 + +#define CC_PHYSICS_ENGINE CC_PHYSICS_CHIPMUNK + +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) +#include "Box2D.h" +#elif (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) +#include "chipmunk.h" +#endif + +#endif // __CCPHYSICS_SETTING_H__ \ No newline at end of file diff --git a/cocos2dx/physics/CCPhysicsWorld.cpp b/cocos2dx/physics/CCPhysicsWorld.cpp new file mode 100644 index 0000000000..8f19b94cc6 --- /dev/null +++ b/cocos2dx/physics/CCPhysicsWorld.cpp @@ -0,0 +1,93 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "CCPhysicsWorld.h" + +NS_CC_BEGIN + +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) + +class PhysicsWorldInfo +{ +public: + cpSpace* space; + +public: + PhysicsWorldInfo(); + ~PhysicsWorldInfo(); +}; + +PhysicsWorldInfo::PhysicsWorldInfo() +{ + space = cpSpaceNew(); +} + +PhysicsWorldInfo::~PhysicsWorldInfo() +{ + cpSpaceFree(space); +} + +bool PhysicsWorld::init() +{ + _worldInfo = new PhysicsWorldInfo(); + + cpSpaceSetGravity(_worldInfo->space, cpv(_gravity.x, _gravity.y)); + + return true; +} + +#elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) + +struct PhysicsInfo +{ + +}; +#endif + +PhysicsWorld* PhysicsWorld::create() +{ + PhysicsWorld * physicsWorld = new PhysicsWorld(); + if(physicsWorld && physicsWorld->init()) + { + physicsWorld->autorelease(); + return physicsWorld; + } + + CC_SAFE_DELETE(physicsWorld); + return NULL; +} + +PhysicsWorld::PhysicsWorld() : +_gravity(Point(0, -9.8)), +_speed(1.0) +{ + +} + +PhysicsWorld::~PhysicsWorld() +{ + CC_SAFE_DELETE(_worldInfo); +} + +NS_CC_END \ No newline at end of file diff --git a/cocos2dx/physics/CCPhysicsWorld.h b/cocos2dx/physics/CCPhysicsWorld.h new file mode 100644 index 0000000000..cf5babaf0e --- /dev/null +++ b/cocos2dx/physics/CCPhysicsWorld.h @@ -0,0 +1,72 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 __CCPHYSICS_WORLD_H__ +#define __CCPHYSICS_WORLD_H__ + +#include "CCObject.h" +#include "CCGeometry.h" +#include "CCPhysicsSetting.h" + +NS_CC_BEGIN + +class PhysicsBody; +class PhysicsJoint; +class PhysicsWorldInfo; +class PhysicsContactDelegate; + +class PhysicsWorld : public Object +{ +public: + static PhysicsWorld* create(); + void addChild(PhysicsBody* body); + + void addJoint(PhysicsJoint* joint); + void removeJoint(PhysicsJoint* joint); + void removeAllJoints(); + + Array* bodysAlongRay(Point start, Point end); + Array* bodysAtPoint(Point point); + Array* bodysInRect(Rect rect); + Array* getAllBody(); + + void registerContactDelegate(PhysicsContactDelegate* delegate); + +protected: + bool init(); + +protected: + Point _gravity; + float _speed; + + class PhysicsWorldInfo* _worldInfo; + +protected: + PhysicsWorld(); + virtual ~PhysicsWorld(); +}; + +NS_CC_END + +#endif // __CCPHYSICS_WORLD_H__ \ No newline at end of file diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/ArmatureTest/ArmatureScene.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/ArmatureTest/ArmatureScene.cpp index d70d0a7e5d..d09a11254f 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/ArmatureTest/ArmatureScene.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/ArmatureTest/ArmatureScene.cpp @@ -531,7 +531,7 @@ void TestBox2DDetector::onEnter() armature2->setPosition(Point(VisibleRect::right().x - 30, VisibleRect::left().y)); addChild(armature2); - PhysicsWorld::sharedPhysicsWorld()->BoneColliderSignal.connect(this, &TestBox2DDetector::onHit); + extension::armature::PhysicsWorld::sharedPhysicsWorld()->BoneColliderSignal.connect(this, &TestBox2DDetector::onHit); } std::string TestBox2DDetector::title() { @@ -543,7 +543,7 @@ void TestBox2DDetector::draw() kmGLPushMatrix(); - PhysicsWorld::sharedPhysicsWorld()->drawDebug(); + extension::armature::PhysicsWorld::sharedPhysicsWorld()->drawDebug(); kmGLPopMatrix(); @@ -551,7 +551,7 @@ void TestBox2DDetector::draw() void TestBox2DDetector::update(float delta) { armature2->setVisible(true); - PhysicsWorld::sharedPhysicsWorld()->update(delta); + extension::armature::PhysicsWorld::sharedPhysicsWorld()->update(delta); } void TestBox2DDetector::onHit(Bone *bone, Bone *bone2) { From a6f9533a94d7f56cd5bbcf567bcf788eade465cb Mon Sep 17 00:00:00 2001 From: boyu0 Date: Mon, 9 Sep 2013 10:34:03 +0800 Subject: [PATCH 02/28] issue #2771: add Node::_physicsBody to Node init list --- cocos2dx/base_nodes/CCNode.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/cocos2dx/base_nodes/CCNode.cpp b/cocos2dx/base_nodes/CCNode.cpp index bc8aa7c95f..b6ab5fa20e 100644 --- a/cocos2dx/base_nodes/CCNode.cpp +++ b/cocos2dx/base_nodes/CCNode.cpp @@ -121,6 +121,7 @@ Node::Node(void) , _updateScriptHandler(0) , _componentContainer(NULL) , _scene(nullptr) +, _physicsBody(nullptr) { // set default scheduler and actionManager Director *director = Director::getInstance(); From dce205f6884c49869cc826d861e13192262100b3 Mon Sep 17 00:00:00 2001 From: boyu0 Date: Mon, 9 Sep 2013 10:40:31 +0800 Subject: [PATCH 03/28] issue #2771: add new line to the end of line. --- cocos2dx/physics/CCPhysicsBody.cpp | 2 +- cocos2dx/physics/CCPhysicsBody.h | 2 +- cocos2dx/physics/CCPhysicsContactDelegate.cpp | 2 +- cocos2dx/physics/CCPhysicsFixture.cpp | 2 +- cocos2dx/physics/CCPhysicsFixture.h | 2 +- cocos2dx/physics/CCPhysicsJoint.cpp | 2 +- cocos2dx/physics/CCPhysicsJoint.h | 2 +- cocos2dx/physics/CCPhysicsSetting.h | 2 +- cocos2dx/physics/CCPhysicsWorld.cpp | 2 +- cocos2dx/physics/CCPhysicsWorld.h | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/cocos2dx/physics/CCPhysicsBody.cpp b/cocos2dx/physics/CCPhysicsBody.cpp index e4b81264af..eb7296843e 100644 --- a/cocos2dx/physics/CCPhysicsBody.cpp +++ b/cocos2dx/physics/CCPhysicsBody.cpp @@ -21,4 +21,4 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ -#include "CCPhysicsBody.h" \ No newline at end of file +#include "CCPhysicsBody.h" diff --git a/cocos2dx/physics/CCPhysicsBody.h b/cocos2dx/physics/CCPhysicsBody.h index 682c6d7e03..f880a7d36f 100644 --- a/cocos2dx/physics/CCPhysicsBody.h +++ b/cocos2dx/physics/CCPhysicsBody.h @@ -97,4 +97,4 @@ private: NS_CC_END -#endif // __CCPHYSICS_BODY_H__ \ No newline at end of file +#endif // __CCPHYSICS_BODY_H__ diff --git a/cocos2dx/physics/CCPhysicsContactDelegate.cpp b/cocos2dx/physics/CCPhysicsContactDelegate.cpp index 3dba18f438..39b3eedbc5 100644 --- a/cocos2dx/physics/CCPhysicsContactDelegate.cpp +++ b/cocos2dx/physics/CCPhysicsContactDelegate.cpp @@ -20,4 +20,4 @@ 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. - ****************************************************************************/ \ No newline at end of file + ****************************************************************************/ diff --git a/cocos2dx/physics/CCPhysicsFixture.cpp b/cocos2dx/physics/CCPhysicsFixture.cpp index 3dba18f438..39b3eedbc5 100644 --- a/cocos2dx/physics/CCPhysicsFixture.cpp +++ b/cocos2dx/physics/CCPhysicsFixture.cpp @@ -20,4 +20,4 @@ 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. - ****************************************************************************/ \ No newline at end of file + ****************************************************************************/ diff --git a/cocos2dx/physics/CCPhysicsFixture.h b/cocos2dx/physics/CCPhysicsFixture.h index 40b914cc38..4753304ea2 100644 --- a/cocos2dx/physics/CCPhysicsFixture.h +++ b/cocos2dx/physics/CCPhysicsFixture.h @@ -56,4 +56,4 @@ protected: }; NS_CC_END -#endif // __CCPHYSICS_FIXTURE_H__ \ No newline at end of file +#endif // __CCPHYSICS_FIXTURE_H__ diff --git a/cocos2dx/physics/CCPhysicsJoint.cpp b/cocos2dx/physics/CCPhysicsJoint.cpp index 3dba18f438..39b3eedbc5 100644 --- a/cocos2dx/physics/CCPhysicsJoint.cpp +++ b/cocos2dx/physics/CCPhysicsJoint.cpp @@ -20,4 +20,4 @@ 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. - ****************************************************************************/ \ No newline at end of file + ****************************************************************************/ diff --git a/cocos2dx/physics/CCPhysicsJoint.h b/cocos2dx/physics/CCPhysicsJoint.h index 5258e1d2d5..8e687d8ab9 100644 --- a/cocos2dx/physics/CCPhysicsJoint.h +++ b/cocos2dx/physics/CCPhysicsJoint.h @@ -111,4 +111,4 @@ protected: NS_CC_END -#endif // __CCPHYSICS_JOINT_H__ \ No newline at end of file +#endif // __CCPHYSICS_JOINT_H__ diff --git a/cocos2dx/physics/CCPhysicsSetting.h b/cocos2dx/physics/CCPhysicsSetting.h index 8bcc65e7b2..745c93b77b 100644 --- a/cocos2dx/physics/CCPhysicsSetting.h +++ b/cocos2dx/physics/CCPhysicsSetting.h @@ -37,4 +37,4 @@ #include "chipmunk.h" #endif -#endif // __CCPHYSICS_SETTING_H__ \ No newline at end of file +#endif // __CCPHYSICS_SETTING_H__ diff --git a/cocos2dx/physics/CCPhysicsWorld.cpp b/cocos2dx/physics/CCPhysicsWorld.cpp index 8f19b94cc6..e53958286b 100644 --- a/cocos2dx/physics/CCPhysicsWorld.cpp +++ b/cocos2dx/physics/CCPhysicsWorld.cpp @@ -90,4 +90,4 @@ PhysicsWorld::~PhysicsWorld() CC_SAFE_DELETE(_worldInfo); } -NS_CC_END \ No newline at end of file +NS_CC_END diff --git a/cocos2dx/physics/CCPhysicsWorld.h b/cocos2dx/physics/CCPhysicsWorld.h index cf5babaf0e..82981dcd7e 100644 --- a/cocos2dx/physics/CCPhysicsWorld.h +++ b/cocos2dx/physics/CCPhysicsWorld.h @@ -69,4 +69,4 @@ protected: NS_CC_END -#endif // __CCPHYSICS_WORLD_H__ \ No newline at end of file +#endif // __CCPHYSICS_WORLD_H__ From cb08f2baf2026662a05e8458de526e2e1b1ad87f Mon Sep 17 00:00:00 2001 From: boyu0 Date: Mon, 9 Sep 2013 16:36:19 +0800 Subject: [PATCH 04/28] issue #2771: Change _physicsBody from node to sprite. Reimplement Scene::addChild. Adjust some API. --- cocos2dx/base_nodes/CCNode.cpp | 23 ------------- cocos2dx/base_nodes/CCNode.h | 33 ------------------- .../CCScene.cpp | 29 +++++++++++++++- cocos2dx/physics/CCPhysicsBody.h | 26 +++++++++------ cocos2dx/physics/CCPhysicsContactDelegate.h | 10 +++--- cocos2dx/physics/CCPhysicsJoint.h | 2 +- cocos2dx/physics/CCPhysicsWorld.cpp | 1 - cocos2dx/physics/CCPhysicsWorld.h | 24 +++++++++----- cocos2dx/sprite_nodes/CCSprite.cpp | 13 +++++++- cocos2dx/sprite_nodes/CCSprite.h | 12 +++++++ 10 files changed, 89 insertions(+), 84 deletions(-) diff --git a/cocos2dx/base_nodes/CCNode.cpp b/cocos2dx/base_nodes/CCNode.cpp index b6ab5fa20e..ef111dfbce 100644 --- a/cocos2dx/base_nodes/CCNode.cpp +++ b/cocos2dx/base_nodes/CCNode.cpp @@ -120,8 +120,6 @@ Node::Node(void) , _isTransitionFinished(false) , _updateScriptHandler(0) , _componentContainer(NULL) -, _scene(nullptr) -, _physicsBody(nullptr) { // set default scheduler and actionManager Director *director = Director::getInstance(); @@ -590,7 +588,6 @@ void Node::addChild(Node *child, int zOrder, int tag) child->_tag = tag; child->setParent(this); - child->setScene(_scene); child->setOrderOfArrival(s_globalOrderOfArrival++); if( _running ) @@ -1274,26 +1271,6 @@ void Node::removeAllComponents() _componentContainer->removeAll(); } -void Node::setScene(Scene* scene) -{ - _scene = scene; -} - -Scene* Node::getScene() -{ - return _scene; -} - -void Node::setPhysicsBody(PhysicsBody* body) -{ - _physicsBody = body; -} - -PhysicsBody* Node::getPhysicsBody() -{ - return _physicsBody; -} - // NodeRGBA NodeRGBA::NodeRGBA() : _displayedOpacity(255) diff --git a/cocos2dx/base_nodes/CCNode.h b/cocos2dx/base_nodes/CCNode.h index 389218453a..d3ea08952d 100644 --- a/cocos2dx/base_nodes/CCNode.h +++ b/cocos2dx/base_nodes/CCNode.h @@ -1302,35 +1302,6 @@ public: */ virtual void removeAllComponents(); /// @} end of component functions - - /// @{ - /// @name scene functions - - /** - * set the scene the node belongs to - * DO NOT call it manually - */ - virtual void setScene(Scene* scene); - - /** - * get the scene the node belongs to - */ - virtual Scene* getScene(); - /// @} end of scene functions - - /// @{ - /// @name physics functions - - /** - * set the PhysicsBody that let the node effect with physics - */ - virtual void setPhysicsBody(PhysicsBody* body); - - /** - * get the PhysicsBody the node have - */ - PhysicsBody* getPhysicsBody(); - /// @} end of physics functions protected: /// lazy allocs @@ -1409,10 +1380,6 @@ protected: ccScriptType _scriptType; ///< type of script binding, lua or javascript ComponentContainer *_componentContainer; ///< Dictionary of components - - Scene* _scene; ///< the scene the node belongs to - PhysicsBody* _physicsBody; ///< the physicsBody the node have - }; //#pragma mark - NodeRGBA diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp index 3c3e2faf1a..a2bc6bb4ed 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp @@ -26,6 +26,9 @@ THE SOFTWARE. #include "CCScene.h" #include "CCDirector.h" +#include "CCLayer.h" +#include "sprite_nodes/CCSprite.h" +#include "physics/CCPhysicsWorld.h" NS_CC_BEGIN @@ -81,7 +84,31 @@ void Scene::addChild(Node* child, int zOrder) void Scene::addChild(Node* child, int zOrder, int tag) { Node::addChild(child, zOrder, tag); - child->setScene(this); + + auto addToPhysicsWorldFunc = [this](Object* node) -> void + { + if (typeid(Sprite).hash_code() == typeid(node).hash_code()) + { + Sprite* sp = dynamic_cast(node); + + if (sp && sp->getPhysicsBody()) + { + _physicsWorld->addChild(sp->getPhysicsBody()); + } + } + }; + + if(typeid(Layer).hash_code() == typeid(child).hash_code()) + { + Object* subChild = nullptr; + CCARRAY_FOREACH(child->getChildren(), subChild) + { + addToPhysicsWorldFunc(subChild); + } + }else + { + addToPhysicsWorldFunc(child); + } } diff --git a/cocos2dx/physics/CCPhysicsBody.h b/cocos2dx/physics/CCPhysicsBody.h index f880a7d36f..ee0adeba0e 100644 --- a/cocos2dx/physics/CCPhysicsBody.h +++ b/cocos2dx/physics/CCPhysicsBody.h @@ -28,13 +28,15 @@ #include "CCObject.h" #include "CCGeometry.h" #include "CCPhysicsSetting.h" +#include NS_CC_BEGIN -class Node; +class Sprite; class PhysicsWorld; +class PhysicsJoint; class PhysicsFixture; -class PhysicsBody : public Object +class PhysicsBody : public Object, public Clonable { public: static PhysicsBody* createCircle(Point centre, float radius); @@ -55,19 +57,23 @@ public: virtual void applyAngularImpulse(float impulse); void addFixture(PhysicsFixture* fixture); - inline Array* getFixtures() { return _fixtures; } + inline Array* getFixtures() const { return _fixtures; } void removeFixture(PhysicsFixture* fixture); void removeAllFixtures(); - inline PhysicsWorld* getWorld() { return _physicsWorld; } - inline Array* getJoints() { return _joints; } + inline PhysicsWorld* getWorld() const { return _physicsWorld; } + inline const std::vector* getJoints() const { return &_joints; } + + inline Sprite* getOwner() const { return _owner; } void setCategoryBitmask(int bitmask); - inline int getCategoryBitmask() { return _categoryBitmask; } + inline int getCategoryBitmask() const { return _categoryBitmask; } void setContactTestBitmask(int bitmask); - inline int getContactTestBitmask() { return _contactTestBitmask; } + inline int getContactTestBitmask() const { return _contactTestBitmask; } void setCollisionBitmask(int bitmask); - inline int getCollisionBitmask() { return _collisionBitmask; } + inline int getCollisionBitmask() const { return _collisionBitmask; } + + virtual Clonable* clone() const override; protected: bool init(); @@ -81,7 +87,7 @@ private: float _density; float _area; float _friction; - Node* _node; + Sprite* _owner; Point _velocity; float _angularVelocity; bool _resting; @@ -90,7 +96,7 @@ private: int _contactTestBitmask; int _collisionBitmask; - Array* _joints; + std::vector _joints; Array* _fixtures; PhysicsWorld* _physicsWorld; }; diff --git a/cocos2dx/physics/CCPhysicsContactDelegate.h b/cocos2dx/physics/CCPhysicsContactDelegate.h index 0e5f137ef8..1bead362d7 100644 --- a/cocos2dx/physics/CCPhysicsContactDelegate.h +++ b/cocos2dx/physics/CCPhysicsContactDelegate.h @@ -33,15 +33,15 @@ NS_CC_BEGIN class PhysicsBody; -class PhysicsContactDelegate : public Object +class PhysicsContactDelegate { public: - virtual void onContactBegin(PhysicsBody* bodyA, PhysicsBody* bodyB, float collisionImpulse, Point contactPoint); - virtual void onContactEnd(PhysicsBody* bodyA, PhysicsBody* bodyB, float collisionImpulse, Point contactPoint); - -protected: PhysicsContactDelegate(); virtual ~PhysicsContactDelegate(); + +public: + virtual void onContactBegin(PhysicsBody* bodyA, PhysicsBody* bodyB, float collisionImpulse, Point contactPoint) = 0; + virtual void onContactEnd(PhysicsBody* bodyA, PhysicsBody* bodyB, float collisionImpulse, Point contactPoint) = 0; }; NS_CC_END diff --git a/cocos2dx/physics/CCPhysicsJoint.h b/cocos2dx/physics/CCPhysicsJoint.h index 8e687d8ab9..964f718135 100644 --- a/cocos2dx/physics/CCPhysicsJoint.h +++ b/cocos2dx/physics/CCPhysicsJoint.h @@ -33,7 +33,7 @@ NS_CC_BEGIN class PhysicsBody; -class PhysicsJoint : public Object +class PhysicsJoint { protected: PhysicsJoint(); diff --git a/cocos2dx/physics/CCPhysicsWorld.cpp b/cocos2dx/physics/CCPhysicsWorld.cpp index e53958286b..7ca586a2b1 100644 --- a/cocos2dx/physics/CCPhysicsWorld.cpp +++ b/cocos2dx/physics/CCPhysicsWorld.cpp @@ -70,7 +70,6 @@ PhysicsWorld* PhysicsWorld::create() PhysicsWorld * physicsWorld = new PhysicsWorld(); if(physicsWorld && physicsWorld->init()) { - physicsWorld->autorelease(); return physicsWorld; } diff --git a/cocos2dx/physics/CCPhysicsWorld.h b/cocos2dx/physics/CCPhysicsWorld.h index 82981dcd7e..bd450e49c2 100644 --- a/cocos2dx/physics/CCPhysicsWorld.h +++ b/cocos2dx/physics/CCPhysicsWorld.h @@ -29,6 +29,7 @@ #include "CCGeometry.h" #include "CCPhysicsSetting.h" + NS_CC_BEGIN class PhysicsBody; @@ -36,35 +37,40 @@ class PhysicsJoint; class PhysicsWorldInfo; class PhysicsContactDelegate; -class PhysicsWorld : public Object +class Sprite; +class Scene; + +class PhysicsWorld { public: - static PhysicsWorld* create(); - void addChild(PhysicsBody* body); - void addJoint(PhysicsJoint* joint); void removeJoint(PhysicsJoint* joint); void removeAllJoints(); - Array* bodysAlongRay(Point start, Point end); - Array* bodysAtPoint(Point point); - Array* bodysInRect(Rect rect); - Array* getAllBody(); + Array* getBodysAlongRay(Point start, Point end) const; + Array* getBodysAtPoint(Point point) const; + Array* getBodysInRect(Rect rect) const; + Array* getAllBody() const; void registerContactDelegate(PhysicsContactDelegate* delegate); protected: + static PhysicsWorld* create(); bool init(); + virtual void addChild(PhysicsBody* body); protected: Point _gravity; float _speed; - class PhysicsWorldInfo* _worldInfo; + PhysicsWorldInfo* _worldInfo; protected: PhysicsWorld(); virtual ~PhysicsWorld(); + + friend class Sprite; + friend class Scene; }; NS_CC_END diff --git a/cocos2dx/sprite_nodes/CCSprite.cpp b/cocos2dx/sprite_nodes/CCSprite.cpp index 272605d861..ed8f0460c3 100644 --- a/cocos2dx/sprite_nodes/CCSprite.cpp +++ b/cocos2dx/sprite_nodes/CCSprite.cpp @@ -295,7 +295,8 @@ Sprite* Sprite::initWithCGImage(CGImageRef pImage, const char *pszKey) Sprite::Sprite(void) : _shouldBeHidden(false) -, _texture(NULL) +, _texture(nullptr) +, _physicsBody(nullptr) { } @@ -446,6 +447,16 @@ void Sprite::setTextureCoords(Rect rect) } } +void Sprite::setPhysicsBody(PhysicsBody* body) +{ + _physicsBody = body; +} + +PhysicsBody* Sprite::getPhysicsBody() const +{ + return _physicsBody; +} + void Sprite::updateTransform(void) { CCASSERT(_batchNode, "updateTransform is only valid when Sprite is being rendered using an SpriteBatchNode"); diff --git a/cocos2dx/sprite_nodes/CCSprite.h b/cocos2dx/sprite_nodes/CCSprite.h index 20872e33a9..16162b810b 100644 --- a/cocos2dx/sprite_nodes/CCSprite.h +++ b/cocos2dx/sprite_nodes/CCSprite.h @@ -443,6 +443,16 @@ public: */ void setFlipY(bool bFlipY); + /** + * set the PhysicsBody that let the sprite effect with physics + */ + virtual void setPhysicsBody(PhysicsBody* body); + + /** + * get the PhysicsBody the sprite have + */ + PhysicsBody* getPhysicsBody() const; + /// @} End of Sprite properties getter/setters @@ -539,6 +549,8 @@ protected: // image is flipped bool _flipX; /// Whether the sprite is flipped horizaontally or not. bool _flipY; /// Whether the sprite is flipped vertically or not. + + PhysicsBody* _physicsBody; ///< the physicsBody the node have }; From 6585fd2162638e84328e668fa54c573c0722b920 Mon Sep 17 00:00:00 2001 From: boyu0 Date: Mon, 9 Sep 2013 16:58:55 +0800 Subject: [PATCH 05/28] issue #2771: Fix CI compile errors. --- cocos2dx/physics/CCPhysicsBody.h | 4 ++-- cocos2dx/physics/CCPhysicsContactDelegate.h | 4 ++-- cocos2dx/physics/CCPhysicsFixture.h | 4 ++-- cocos2dx/physics/CCPhysicsJoint.h | 4 ++-- cocos2dx/physics/CCPhysicsWorld.h | 4 ++-- cocos2dx/sprite_nodes/CCSprite.cpp | 2 ++ 6 files changed, 12 insertions(+), 10 deletions(-) diff --git a/cocos2dx/physics/CCPhysicsBody.h b/cocos2dx/physics/CCPhysicsBody.h index ee0adeba0e..04e0fa6acf 100644 --- a/cocos2dx/physics/CCPhysicsBody.h +++ b/cocos2dx/physics/CCPhysicsBody.h @@ -25,8 +25,8 @@ #ifndef __CCPHYSICS_BODY_H__ #define __CCPHYSICS_BODY_H__ -#include "CCObject.h" -#include "CCGeometry.h" +#include "cocoa/CCObject.h" +#include "cocoa/CCGeometry.h" #include "CCPhysicsSetting.h" #include diff --git a/cocos2dx/physics/CCPhysicsContactDelegate.h b/cocos2dx/physics/CCPhysicsContactDelegate.h index 1bead362d7..7c41756ae8 100644 --- a/cocos2dx/physics/CCPhysicsContactDelegate.h +++ b/cocos2dx/physics/CCPhysicsContactDelegate.h @@ -25,8 +25,8 @@ #ifndef __CCPHYSICS_CONTACTDELEGATE_H__ #define __CCPHYSICS_CONTACTDELEGATE_H__ -#include "CCObject.h" -#include "CCGeometry.h" +#include "cocoa/CCObject.h" +#include "cocoa/CCGeometry.h" #include "CCPhysicsSetting.h" NS_CC_BEGIN diff --git a/cocos2dx/physics/CCPhysicsFixture.h b/cocos2dx/physics/CCPhysicsFixture.h index 4753304ea2..76fc367e07 100644 --- a/cocos2dx/physics/CCPhysicsFixture.h +++ b/cocos2dx/physics/CCPhysicsFixture.h @@ -25,8 +25,8 @@ #ifndef __CCPHYSICS_FIXTURE_H__ #define __CCPHYSICS_FIXTURE_H__ -#include "CCObject.h" -#include "CCGeometry.h" +#include "cocoa/CCObject.h" +#include "cocoa/CCGeometry.h" #include "CCPhysicsSetting.h" NS_CC_BEGIN diff --git a/cocos2dx/physics/CCPhysicsJoint.h b/cocos2dx/physics/CCPhysicsJoint.h index 964f718135..5f3aaf011e 100644 --- a/cocos2dx/physics/CCPhysicsJoint.h +++ b/cocos2dx/physics/CCPhysicsJoint.h @@ -25,8 +25,8 @@ #ifndef __CCPHYSICS_JOINT_H__ #define __CCPHYSICS_JOINT_H__ -#include "CCObject.h" -#include "CCGeometry.h" +#include "cocoa/CCObject.h" +#include "cocoa/CCGeometry.h" #include "CCPhysicsSetting.h" NS_CC_BEGIN diff --git a/cocos2dx/physics/CCPhysicsWorld.h b/cocos2dx/physics/CCPhysicsWorld.h index bd450e49c2..1f092e5ae4 100644 --- a/cocos2dx/physics/CCPhysicsWorld.h +++ b/cocos2dx/physics/CCPhysicsWorld.h @@ -25,8 +25,8 @@ #ifndef __CCPHYSICS_WORLD_H__ #define __CCPHYSICS_WORLD_H__ -#include "CCObject.h" -#include "CCGeometry.h" +#include "cocoa/CCObject.h" +#include "cocoa/CCGeometry.h" #include "CCPhysicsSetting.h" diff --git a/cocos2dx/sprite_nodes/CCSprite.cpp b/cocos2dx/sprite_nodes/CCSprite.cpp index ed8f0460c3..0d75e93348 100644 --- a/cocos2dx/sprite_nodes/CCSprite.cpp +++ b/cocos2dx/sprite_nodes/CCSprite.cpp @@ -44,6 +44,8 @@ THE SOFTWARE. #include "cocoa/CCAffineTransform.h" #include "support/TransformUtils.h" #include "support/CCProfiling.h" +#include "physics/CCPhysicsBody.h" +#include "physics/CCPhysicsWorld.h" // external #include "kazmath/GL/matrix.h" From 0806c5fe15e97f0653c6ba9a8c9d08c1fd3baae9 Mon Sep 17 00:00:00 2001 From: boyu0 Date: Mon, 9 Sep 2013 17:13:59 +0800 Subject: [PATCH 06/28] issue #2771: Fix CI compile errors. --- cocos2dx/proj.linux/Makefile | 5 +++++ cocos2dx/sprite_nodes/CCSprite.cpp | 2 -- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/cocos2dx/proj.linux/Makefile b/cocos2dx/proj.linux/Makefile index dc539d17ec..556dc75627 100644 --- a/cocos2dx/proj.linux/Makefile +++ b/cocos2dx/proj.linux/Makefile @@ -74,6 +74,11 @@ SOURCES = ../actions/CCAction.cpp \ ../particle_nodes/CCParticleSystem.cpp \ ../particle_nodes/CCParticleSystemQuad.cpp \ ../particle_nodes/CCParticleBatchNode.cpp \ +../physics/CCPhysicsBody.cpp \ +../physics/CCPhysicsContactDelegate.cpp \ +../physics/CCPhysicsFixture.cpp \ +../physics/CCPhysicsJoint.cpp \ +../physics/CCPhysicsWorld.cpp \ ../platform/CCSAXParser.cpp \ ../platform/CCThread.cpp \ ../platform/CCEGLViewProtocol.cpp \ diff --git a/cocos2dx/sprite_nodes/CCSprite.cpp b/cocos2dx/sprite_nodes/CCSprite.cpp index 0d75e93348..ed8f0460c3 100644 --- a/cocos2dx/sprite_nodes/CCSprite.cpp +++ b/cocos2dx/sprite_nodes/CCSprite.cpp @@ -44,8 +44,6 @@ THE SOFTWARE. #include "cocoa/CCAffineTransform.h" #include "support/TransformUtils.h" #include "support/CCProfiling.h" -#include "physics/CCPhysicsBody.h" -#include "physics/CCPhysicsWorld.h" // external #include "kazmath/GL/matrix.h" From 862a3bd6a4e682ac6b3bdb4acb9ba4af9af17a41 Mon Sep 17 00:00:00 2001 From: boyu0 Date: Tue, 10 Sep 2013 10:06:51 +0800 Subject: [PATCH 07/28] issue #2771: Fix CI compile errors. --- cocos2dx/include/cocos2d.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cocos2dx/include/cocos2d.h b/cocos2dx/include/cocos2d.h index 01e5739d96..d8ee1e9099 100755 --- a/cocos2dx/include/cocos2d.h +++ b/cocos2dx/include/cocos2d.h @@ -124,6 +124,13 @@ THE SOFTWARE. #include "particle_nodes/CCParticleExamples.h" #include "particle_nodes/CCParticleSystemQuad.h" +// physics +#include "physics/CCPhysicsBody.h" +#include "physics/CCPhysicsContactDelegate.h" +#include "physics/CCPhysicsFixture.h" +#include "physics/CCPhysicsJoint.h" +#include "physics/CCPhysicsWorld.h" + // platform #include "platform/CCDevice.h" #include "platform/CCCommon.h" From bd10d92460d0a935b79aa4b4970b6b3ee372af71 Mon Sep 17 00:00:00 2001 From: boyu0 Date: Tue, 10 Sep 2013 14:15:19 +0800 Subject: [PATCH 08/28] issue #2771: Add macros to control use physics engine or not --- cocos2dx/physics/CCPhysicsBody.h | 6 +++++- cocos2dx/physics/CCPhysicsContactDelegate.h | 8 ++++++-- cocos2dx/physics/CCPhysicsFixture.h | 6 +++++- cocos2dx/physics/CCPhysicsJoint.h | 6 +++++- cocos2dx/physics/CCPhysicsSetting.h | 4 ++++ cocos2dx/physics/CCPhysicsWorld.h | 7 +++++-- 6 files changed, 30 insertions(+), 7 deletions(-) diff --git a/cocos2dx/physics/CCPhysicsBody.h b/cocos2dx/physics/CCPhysicsBody.h index 04e0fa6acf..1871565c3d 100644 --- a/cocos2dx/physics/CCPhysicsBody.h +++ b/cocos2dx/physics/CCPhysicsBody.h @@ -22,12 +22,14 @@ THE SOFTWARE. ****************************************************************************/ +#include "CCPhysicsSetting.h" +#ifdef CC_USE_PHYSICS_ENGINE + #ifndef __CCPHYSICS_BODY_H__ #define __CCPHYSICS_BODY_H__ #include "cocoa/CCObject.h" #include "cocoa/CCGeometry.h" -#include "CCPhysicsSetting.h" #include NS_CC_BEGIN @@ -104,3 +106,5 @@ private: NS_CC_END #endif // __CCPHYSICS_BODY_H__ + +#endif // CC_USE_PHYSICS_ENGINE diff --git a/cocos2dx/physics/CCPhysicsContactDelegate.h b/cocos2dx/physics/CCPhysicsContactDelegate.h index 7c41756ae8..e96e563139 100644 --- a/cocos2dx/physics/CCPhysicsContactDelegate.h +++ b/cocos2dx/physics/CCPhysicsContactDelegate.h @@ -22,12 +22,14 @@ THE SOFTWARE. ****************************************************************************/ +#include "CCPhysicsSetting.h" +#ifdef CC_USE_PHYSICS_ENGINE + #ifndef __CCPHYSICS_CONTACTDELEGATE_H__ #define __CCPHYSICS_CONTACTDELEGATE_H__ #include "cocoa/CCObject.h" #include "cocoa/CCGeometry.h" -#include "CCPhysicsSetting.h" NS_CC_BEGIN @@ -45,4 +47,6 @@ public: }; NS_CC_END -#endif //__CCPHYSICS_CONTACTDELEGATE_H__ \ No newline at end of file +#endif //__CCPHYSICS_CONTACTDELEGATE_H__ + +#endif // CC_USE_PHYSICS_ENGINE diff --git a/cocos2dx/physics/CCPhysicsFixture.h b/cocos2dx/physics/CCPhysicsFixture.h index 76fc367e07..210f58472d 100644 --- a/cocos2dx/physics/CCPhysicsFixture.h +++ b/cocos2dx/physics/CCPhysicsFixture.h @@ -22,12 +22,14 @@ THE SOFTWARE. ****************************************************************************/ +#include "CCPhysicsSetting.h" +#ifdef CC_USE_PHYSICS_ENGINE + #ifndef __CCPHYSICS_FIXTURE_H__ #define __CCPHYSICS_FIXTURE_H__ #include "cocoa/CCObject.h" #include "cocoa/CCGeometry.h" -#include "CCPhysicsSetting.h" NS_CC_BEGIN @@ -57,3 +59,5 @@ protected: NS_CC_END #endif // __CCPHYSICS_FIXTURE_H__ + +#endif // CC_USE_PHYSICS_ENGINE diff --git a/cocos2dx/physics/CCPhysicsJoint.h b/cocos2dx/physics/CCPhysicsJoint.h index 5f3aaf011e..03e0515336 100644 --- a/cocos2dx/physics/CCPhysicsJoint.h +++ b/cocos2dx/physics/CCPhysicsJoint.h @@ -22,12 +22,14 @@ THE SOFTWARE. ****************************************************************************/ +#include "CCPhysicsSetting.h" +#ifdef CC_USE_PHYSICS_ENGINE + #ifndef __CCPHYSICS_JOINT_H__ #define __CCPHYSICS_JOINT_H__ #include "cocoa/CCObject.h" #include "cocoa/CCGeometry.h" -#include "CCPhysicsSetting.h" NS_CC_BEGIN @@ -112,3 +114,5 @@ protected: NS_CC_END #endif // __CCPHYSICS_JOINT_H__ + +#endif // CC_USE_PHYSICS_ENGINE diff --git a/cocos2dx/physics/CCPhysicsSetting.h b/cocos2dx/physics/CCPhysicsSetting.h index 745c93b77b..4edd004e11 100644 --- a/cocos2dx/physics/CCPhysicsSetting.h +++ b/cocos2dx/physics/CCPhysicsSetting.h @@ -31,6 +31,10 @@ #define CC_PHYSICS_ENGINE CC_PHYSICS_CHIPMUNK +#if (CC_PHYSICS_ENGINE != CC_PHYSICS_UNKNOWN) +#define CC_USE_PHYSICS_ENGINE +#endif + #if (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) #include "Box2D.h" #elif (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) diff --git a/cocos2dx/physics/CCPhysicsWorld.h b/cocos2dx/physics/CCPhysicsWorld.h index 1f092e5ae4..2b81983f77 100644 --- a/cocos2dx/physics/CCPhysicsWorld.h +++ b/cocos2dx/physics/CCPhysicsWorld.h @@ -22,13 +22,14 @@ THE SOFTWARE. ****************************************************************************/ +#include "CCPhysicsSetting.h" +#ifdef CC_USE_PHYSICS_ENGINE + #ifndef __CCPHYSICS_WORLD_H__ #define __CCPHYSICS_WORLD_H__ #include "cocoa/CCObject.h" #include "cocoa/CCGeometry.h" -#include "CCPhysicsSetting.h" - NS_CC_BEGIN @@ -76,3 +77,5 @@ protected: NS_CC_END #endif // __CCPHYSICS_WORLD_H__ + +#endif // CC_USE_PHYSICS_ENGINE From bcab90ddc438f06a36c4c38643ffe4b602fb78bf Mon Sep 17 00:00:00 2001 From: boyu0 Date: Tue, 10 Sep 2013 17:36:49 +0800 Subject: [PATCH 09/28] issue #2771: Change CC_USE_PHYSICS_ENGINE to CC_USE_PHYSICS. Add some implements. --- cocos2dx/physics/CCPhysicsBody.cpp | 96 +++++++++++++++++++++ cocos2dx/physics/CCPhysicsBody.h | 13 +-- cocos2dx/physics/CCPhysicsContactDelegate.h | 4 +- cocos2dx/physics/CCPhysicsFixture.h | 4 +- cocos2dx/physics/CCPhysicsJoint.h | 4 +- cocos2dx/physics/CCPhysicsSetting.h | 2 +- cocos2dx/physics/CCPhysicsWorld.cpp | 22 +++-- cocos2dx/physics/CCPhysicsWorld.h | 7 +- 8 files changed, 130 insertions(+), 22 deletions(-) diff --git a/cocos2dx/physics/CCPhysicsBody.cpp b/cocos2dx/physics/CCPhysicsBody.cpp index eb7296843e..0b2309a1c1 100644 --- a/cocos2dx/physics/CCPhysicsBody.cpp +++ b/cocos2dx/physics/CCPhysicsBody.cpp @@ -22,3 +22,99 @@ THE SOFTWARE. ****************************************************************************/ #include "CCPhysicsBody.h" + +#ifdef CC_USE_PHYSICS + +NS_CC_BEGIN + + +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) + +class PhysicsBodyInfo +{ +public: + PhysicsBodyInfo(); + ~PhysicsBodyInfo(); + +public: + cpBody* body; + cpShape* shape; +}; + +PhysicsBodyInfo::PhysicsBodyInfo() +: body(nullptr) +, shape(nullptr) +{ +} + +PhysicsBodyInfo::~PhysicsBodyInfo() +{ + if (body) cpBodyFree(body); + if (shape) cpShapeFree(shape); +} + +PhysicsBody::PhysicsBody() +{ +} + +PhysicsBody::~PhysicsBody() +{ + CC_SAFE_DELETE(_info); +} + +PhysicsBody* PhysicsBody::create() +{ + PhysicsBody * body = new PhysicsBody(); + if(body && body->init()) + { + return body; + } + + CC_SAFE_DELETE(body); + return nullptr; +} + +bool PhysicsBody::init() +{ + do + { + _info = new PhysicsBodyInfo(); + CC_BREAK_IF(_info == nullptr); + + _info->body = cpBodyNew(1.0, 1.0); + CC_BREAK_IF(_info->body == nullptr); + + return true; + } while (false); + + return false; +} + +PhysicsBody* PhysicsBody::createCircle(Point centre, float radius) +{ + PhysicsBody* body = PhysicsBody::create(); + + if (body == nullptr) + { + return nullptr; + } + + cpBodySetPos(body->_info->body, cpv(centre.x, centre.y)); + body->_info->shape = cpCircleShapeNew(body->_info->body, radius, cpvzero); + + if (body->_info->shape == nullptr) + { + return nullptr; + } + + return body; +} + +#elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) + + +#endif + +NS_CC_END + +#endif // CC_USE_PHYSICS diff --git a/cocos2dx/physics/CCPhysicsBody.h b/cocos2dx/physics/CCPhysicsBody.h index 1871565c3d..64df98ec3f 100644 --- a/cocos2dx/physics/CCPhysicsBody.h +++ b/cocos2dx/physics/CCPhysicsBody.h @@ -23,7 +23,7 @@ ****************************************************************************/ #include "CCPhysicsSetting.h" -#ifdef CC_USE_PHYSICS_ENGINE +#ifdef CC_USE_PHYSICS #ifndef __CCPHYSICS_BODY_H__ #define __CCPHYSICS_BODY_H__ @@ -37,6 +37,7 @@ class Sprite; class PhysicsWorld; class PhysicsJoint; class PhysicsFixture; +class PhysicsBodyInfo; class PhysicsBody : public Object, public Clonable { @@ -63,7 +64,7 @@ public: void removeFixture(PhysicsFixture* fixture); void removeAllFixtures(); - inline PhysicsWorld* getWorld() const { return _physicsWorld; } + inline PhysicsWorld* getWorld() const { return _world; } inline const std::vector* getJoints() const { return &_joints; } inline Sprite* getOwner() const { return _owner; } @@ -78,13 +79,14 @@ public: virtual Clonable* clone() const override; protected: + static PhysicsBody* create(); bool init(); protected: PhysicsBody(); virtual ~PhysicsBody(); -private: +protected: float _mass; float _density; float _area; @@ -100,11 +102,12 @@ private: std::vector _joints; Array* _fixtures; - PhysicsWorld* _physicsWorld; + PhysicsWorld* _world; + PhysicsBodyInfo* _info; }; NS_CC_END #endif // __CCPHYSICS_BODY_H__ -#endif // CC_USE_PHYSICS_ENGINE +#endif // CC_USE_PHYSICS diff --git a/cocos2dx/physics/CCPhysicsContactDelegate.h b/cocos2dx/physics/CCPhysicsContactDelegate.h index e96e563139..a371b6e044 100644 --- a/cocos2dx/physics/CCPhysicsContactDelegate.h +++ b/cocos2dx/physics/CCPhysicsContactDelegate.h @@ -23,7 +23,7 @@ ****************************************************************************/ #include "CCPhysicsSetting.h" -#ifdef CC_USE_PHYSICS_ENGINE +#ifdef CC_USE_PHYSICS #ifndef __CCPHYSICS_CONTACTDELEGATE_H__ #define __CCPHYSICS_CONTACTDELEGATE_H__ @@ -49,4 +49,4 @@ public: NS_CC_END #endif //__CCPHYSICS_CONTACTDELEGATE_H__ -#endif // CC_USE_PHYSICS_ENGINE +#endif // CC_USE_PHYSICS diff --git a/cocos2dx/physics/CCPhysicsFixture.h b/cocos2dx/physics/CCPhysicsFixture.h index 210f58472d..e743ab3c44 100644 --- a/cocos2dx/physics/CCPhysicsFixture.h +++ b/cocos2dx/physics/CCPhysicsFixture.h @@ -23,7 +23,7 @@ ****************************************************************************/ #include "CCPhysicsSetting.h" -#ifdef CC_USE_PHYSICS_ENGINE +#ifdef CC_USE_PHYSICS #ifndef __CCPHYSICS_FIXTURE_H__ #define __CCPHYSICS_FIXTURE_H__ @@ -60,4 +60,4 @@ protected: NS_CC_END #endif // __CCPHYSICS_FIXTURE_H__ -#endif // CC_USE_PHYSICS_ENGINE +#endif // CC_USE_PHYSICS diff --git a/cocos2dx/physics/CCPhysicsJoint.h b/cocos2dx/physics/CCPhysicsJoint.h index 03e0515336..c7e659ab75 100644 --- a/cocos2dx/physics/CCPhysicsJoint.h +++ b/cocos2dx/physics/CCPhysicsJoint.h @@ -23,7 +23,7 @@ ****************************************************************************/ #include "CCPhysicsSetting.h" -#ifdef CC_USE_PHYSICS_ENGINE +#ifdef CC_USE_PHYSICS #ifndef __CCPHYSICS_JOINT_H__ #define __CCPHYSICS_JOINT_H__ @@ -115,4 +115,4 @@ NS_CC_END #endif // __CCPHYSICS_JOINT_H__ -#endif // CC_USE_PHYSICS_ENGINE +#endif // CC_USE_PHYSICS diff --git a/cocos2dx/physics/CCPhysicsSetting.h b/cocos2dx/physics/CCPhysicsSetting.h index 4edd004e11..b68da7cf93 100644 --- a/cocos2dx/physics/CCPhysicsSetting.h +++ b/cocos2dx/physics/CCPhysicsSetting.h @@ -32,7 +32,7 @@ #define CC_PHYSICS_ENGINE CC_PHYSICS_CHIPMUNK #if (CC_PHYSICS_ENGINE != CC_PHYSICS_UNKNOWN) -#define CC_USE_PHYSICS_ENGINE +#define CC_USE_PHYSICS #endif #if (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) diff --git a/cocos2dx/physics/CCPhysicsWorld.cpp b/cocos2dx/physics/CCPhysicsWorld.cpp index 7ca586a2b1..ccfc1ea788 100644 --- a/cocos2dx/physics/CCPhysicsWorld.cpp +++ b/cocos2dx/physics/CCPhysicsWorld.cpp @@ -57,6 +57,11 @@ bool PhysicsWorld::init() return true; } +void PhysicsWorld::addChild(PhysicsBody* body) +{ + +} + #elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) struct PhysicsInfo @@ -67,19 +72,20 @@ struct PhysicsInfo PhysicsWorld* PhysicsWorld::create() { - PhysicsWorld * physicsWorld = new PhysicsWorld(); - if(physicsWorld && physicsWorld->init()) + PhysicsWorld * world = new PhysicsWorld(); + if(world && world->init()) { - return physicsWorld; + return world; } - CC_SAFE_DELETE(physicsWorld); - return NULL; + CC_SAFE_DELETE(world); + return nullptr; } -PhysicsWorld::PhysicsWorld() : -_gravity(Point(0, -9.8)), -_speed(1.0) +PhysicsWorld::PhysicsWorld() +: _gravity(Point(0, -9.8)) +, _speed(1.0) +, _worldInfo(nullptr) { } diff --git a/cocos2dx/physics/CCPhysicsWorld.h b/cocos2dx/physics/CCPhysicsWorld.h index 2b81983f77..5bf807c159 100644 --- a/cocos2dx/physics/CCPhysicsWorld.h +++ b/cocos2dx/physics/CCPhysicsWorld.h @@ -23,7 +23,7 @@ ****************************************************************************/ #include "CCPhysicsSetting.h" -#ifdef CC_USE_PHYSICS_ENGINE +#ifdef CC_USE_PHYSICS #ifndef __CCPHYSICS_WORLD_H__ #define __CCPHYSICS_WORLD_H__ @@ -55,6 +55,9 @@ public: void registerContactDelegate(PhysicsContactDelegate* delegate); + inline Point getGravity() { return _gravity; } + inline void setGravity(Point gravity) { _gravity = gravity; } + protected: static PhysicsWorld* create(); bool init(); @@ -78,4 +81,4 @@ NS_CC_END #endif // __CCPHYSICS_WORLD_H__ -#endif // CC_USE_PHYSICS_ENGINE +#endif // CC_USE_PHYSICS From 7bba5f1b01e275d64d7bd66def2a3d0c393de3fe Mon Sep 17 00:00:00 2001 From: boyu0 Date: Tue, 10 Sep 2013 17:38:47 +0800 Subject: [PATCH 10/28] issue #2771: Add method Scene::createWithPhysics() end Scene::initWithPhysics() --- cocos2dx/base_nodes/CCNode.h | 2 - .../CCScene.cpp | 76 ++++++++++++++----- .../layers_scenes_transitions_nodes/CCScene.h | 15 +++- cocos2dx/sprite_nodes/CCSprite.h | 5 ++ 4 files changed, 75 insertions(+), 23 deletions(-) diff --git a/cocos2dx/base_nodes/CCNode.h b/cocos2dx/base_nodes/CCNode.h index d3ea08952d..dd6384e724 100644 --- a/cocos2dx/base_nodes/CCNode.h +++ b/cocos2dx/base_nodes/CCNode.h @@ -52,8 +52,6 @@ class ActionManager; class Component; class Dictionary; class ComponentContainer; -class Scene; -class PhysicsBody; /** * @addtogroup base_nodes diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp index a2bc6bb4ed..589a77d60b 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp @@ -33,6 +33,9 @@ THE SOFTWARE. NS_CC_BEGIN Scene::Scene() +#ifdef _physicsWorld +: _physicsWorld(nullptr) +#endif { _ignoreAnchorPointForPosition = true; setAnchorPoint(Point(0.5f, 0.5f)); @@ -56,7 +59,7 @@ bool Scene::init() return bRet; } -Scene *Scene::create(bool usePhysics/* = false*/) +Scene *Scene::create() { Scene *pRet = new Scene(); if (pRet && pRet->init()) @@ -71,6 +74,38 @@ Scene *Scene::create(bool usePhysics/* = false*/) } } +#ifdef CC_USE_PHYSICS +Scene *Scene::createWithPhysics() +{ + Scene *pRet = new Scene(); + if (pRet && pRet->initWithPhysics()) + { + pRet->autorelease(); + return pRet; + } + else + { + CC_SAFE_DELETE(pRet); + return NULL; + } +} + +bool Scene::initWithPhysics() +{ + bool bRet = false; + do + { + Director * pDirector; + CC_BREAK_IF( ! (pDirector = Director::getInstance()) ); + this->setContentSize(pDirector->getWinSize()); + CC_BREAK_IF(! (_physicsWorld = PhysicsWorld::create())); + // success + bRet = true; + } while (0); + return bRet; +} +#endif + void Scene::addChild(Node* child) { Node::addChild(child); @@ -85,30 +120,35 @@ void Scene::addChild(Node* child, int zOrder, int tag) { Node::addChild(child, zOrder, tag); - auto addToPhysicsWorldFunc = [this](Object* node) -> void +#ifdef CC_USE_PHYSICS + if (_physicsWorld) { - if (typeid(Sprite).hash_code() == typeid(node).hash_code()) + auto addToPhysicsWorldFunc = [this](Object* node) -> void { - Sprite* sp = dynamic_cast(node); - - if (sp && sp->getPhysicsBody()) + if (typeid(Sprite).hash_code() == typeid(node).hash_code()) { - _physicsWorld->addChild(sp->getPhysicsBody()); + Sprite* sp = dynamic_cast(node); + + if (sp && sp->getPhysicsBody()) + { + _physicsWorld->addChild(sp->getPhysicsBody()); + } } - } - }; - - if(typeid(Layer).hash_code() == typeid(child).hash_code()) - { - Object* subChild = nullptr; - CCARRAY_FOREACH(child->getChildren(), subChild) + }; + + if(typeid(Layer).hash_code() == typeid(child).hash_code()) { - addToPhysicsWorldFunc(subChild); + Object* subChild = nullptr; + CCARRAY_FOREACH(child->getChildren(), subChild) + { + addToPhysicsWorldFunc(subChild); + } + }else + { + addToPhysicsWorldFunc(child); } - }else - { - addToPhysicsWorldFunc(child); } +#endif } diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCScene.h b/cocos2dx/layers_scenes_transitions_nodes/CCScene.h index c084852513..4b5b27f4a4 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCScene.h +++ b/cocos2dx/layers_scenes_transitions_nodes/CCScene.h @@ -28,11 +28,10 @@ THE SOFTWARE. #define __CCSCENE_H__ #include "base_nodes/CCNode.h" +#include "physics/CCPhysicsWorld.h" NS_CC_BEGIN -class PhysicsWorld; - /** * @addtogroup scene * @{ @@ -52,21 +51,31 @@ class CC_DLL Scene : public Node { public: /** creates a new Scene object */ - static Scene *create(bool usePhysics = false); + static Scene *create(); +#ifdef CC_USE_PHYSICS + static Scene *createWithPhysics(); +#endif Scene(); virtual ~Scene(); bool init(); +#ifdef CC_USE_PHYSICS + bool initWithPhysics(); +#endif virtual void addChild(Node* child) override; virtual void addChild(Node* child, int zOrder) override; virtual void addChild(Node* child, int zOrder, int tag) override; +#ifdef CC_USE_PHYSICS inline PhysicsWorld* getPhysicsWorld() { return _physicsWorld; } +#endif protected: +#ifdef CC_USE_PHYSICS PhysicsWorld* _physicsWorld; +#endif }; // end of scene group diff --git a/cocos2dx/sprite_nodes/CCSprite.h b/cocos2dx/sprite_nodes/CCSprite.h index 16162b810b..4d4d0e7bdf 100644 --- a/cocos2dx/sprite_nodes/CCSprite.h +++ b/cocos2dx/sprite_nodes/CCSprite.h @@ -36,6 +36,7 @@ THE SOFTWARE. #ifdef EMSCRIPTEN #include "base_nodes/CCGLBufferedNode.h" #endif // EMSCRIPTEN +#include "physics/CCPhysicsBody.h" NS_CC_BEGIN @@ -443,6 +444,7 @@ public: */ void setFlipY(bool bFlipY); +#ifdef CC_USE_PHYSICS /** * set the PhysicsBody that let the sprite effect with physics */ @@ -452,6 +454,7 @@ public: * get the PhysicsBody the sprite have */ PhysicsBody* getPhysicsBody() const; +#endif /// @} End of Sprite properties getter/setters @@ -550,7 +553,9 @@ protected: bool _flipX; /// Whether the sprite is flipped horizaontally or not. bool _flipY; /// Whether the sprite is flipped vertically or not. +#ifdef CC_USE_PHYSICS PhysicsBody* _physicsBody; ///< the physicsBody the node have +#endif }; From d161c6a5760a3f254a3155e6cb548ac34d709886 Mon Sep 17 00:00:00 2001 From: boyu0 Date: Tue, 10 Sep 2013 17:41:06 +0800 Subject: [PATCH 11/28] issue #2771: Add PhysicsTest to TestCpp. Add initTest to TestScene, move Scene::init() from constructor to TestScene::initTest() --- .../Classes/PhysicsTest/PhysicsTest.cpp | 108 ++++++++++++++++++ .../TestCpp/Classes/PhysicsTest/PhysicsTest.h | 29 +++++ samples/Cpp/TestCpp/Classes/controller.cpp | 3 +- samples/Cpp/TestCpp/Classes/testBasic.cpp | 7 +- samples/Cpp/TestCpp/Classes/testBasic.h | 1 + samples/Cpp/TestCpp/Classes/tests.h | 1 + .../project.pbxproj.REMOVED.git-id | 2 +- 7 files changed, 147 insertions(+), 4 deletions(-) create mode 100644 samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp create mode 100644 samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h diff --git a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp new file mode 100644 index 0000000000..a1d9180b72 --- /dev/null +++ b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp @@ -0,0 +1,108 @@ +#include "PhysicsTest.h" +#include "../testResource.h" +#include "cocos-ext.h" +USING_NS_CC_EXT; + +PhysicsTestLayer::PhysicsTestLayer() +: _spriteTexture(NULL) +{ +#ifdef CC_USE_PHYSICS + //Set up sprite +#if 1 + // Use batch node. Faster + auto parent = SpriteBatchNode::create("Images/blocks.png", 100); + _spriteTexture = parent->getTexture(); +#else + // doesn't use batch node. Slower + _spriteTexture = TextureCache::getInstance()->addImage("Images/blocks.png"); + auto parent = Node::create(); +#endif + +#else + auto label = LabelTTF::create("Should define CC_ENABLE_BOX2D_INTEGRATION=1\n to run this test case", + "Arial", + 18); + auto size = Director::getInstance()->getWinSize(); + label->setPosition(Point(size.width/2, size.height/2)); + + addChild(label); +#endif +} + +PhysicsTestLayer::~PhysicsTestLayer() +{ +} + +void PhysicsTestLayer::initPhysics() +{ +} + +void PhysicsTestLayer::createResetButton() +{ + auto reset = MenuItemImage::create("Images/r1.png", "Images/r2.png", [](Object *sender) { + auto s = new PhysicsTestScene(); + auto child = new PhysicsTestLayer(); + s->addChild(child); + child->release(); + Director::getInstance()->replaceScene(s); + s->release(); + }); + + auto menu = Menu::create(reset, NULL); + + menu->setPosition(Point(VisibleRect::bottom().x, VisibleRect::bottom().y + 30)); + this->addChild(menu, -1); + +} + +void PhysicsTestLayer::addNewSpriteAtPosition(Point p) +{ + CCLOG("Add sprite %0.2f x %02.f",p.x,p.y); + +#if CC_ENABLE_BOX2D_INTEGRATION + auto parent = this->getChildByTag(kTagParentNode); + + //We have a 64x64 sprite sheet with 4 different 32x32 images. The following code is + //just randomly picking one of the images + int idx = (CCRANDOM_0_1() > .5 ? 0:1); + int idy = (CCRANDOM_0_1() > .5 ? 0:1); + auto sprite = PhysicsSprite::createWithTexture(_spriteTexture,Rect(32 * idx,32 * idy,32,32)); + parent->addChild(sprite); + sprite->setB2Body(body); + sprite->setPTMRatio(PTM_RATIO); + sprite->setPosition( Point( p.x, p.y) ); +#endif +} + +void PhysicsTestLayer::ccTouchesEnded(Set* touches, Event* event) +{ + //Add a new body/atlas sprite at the touched location + SetIterator it; + Touch* touch; + + for( it = touches->begin(); it != touches->end(); it++) + { + touch = static_cast(*it); + + if(!touch) + break; + + auto location = touch->getLocation(); + + addNewSpriteAtPosition( location ); + } +} + +bool PhysicsTestScene::initTest() +{ + return TestScene::initWithPhysics(); +} + +void PhysicsTestScene::runThisTest() +{ + auto layer = new PhysicsTestLayer(); + addChild(layer); + layer->release(); + + Director::getInstance()->replaceScene(this); +} diff --git a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h new file mode 100644 index 0000000000..8140015488 --- /dev/null +++ b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h @@ -0,0 +1,29 @@ +#ifndef _PHYSICS_TEST_H_ +#define _PHYSICS_TEST_H_ + +#include "cocos2d.h" +#include "../testBasic.h" + +class PhysicsTestLayer : public Layer +{ + Texture2D* _spriteTexture; // weak ref + +public: + PhysicsTestLayer(); + ~PhysicsTestLayer(); + + void initPhysics(); + void createResetButton(); + + void addNewSpriteAtPosition(Point p); + virtual void ccTouchesEnded(Set* touches, Event* event); +} ; + +class PhysicsTestScene : public TestScene +{ +public: + virtual bool initTest() override; + virtual void runThisTest(); +}; + +#endif diff --git a/samples/Cpp/TestCpp/Classes/controller.cpp b/samples/Cpp/TestCpp/Classes/controller.cpp index 26971f6452..946458bb57 100644 --- a/samples/Cpp/TestCpp/Classes/controller.cpp +++ b/samples/Cpp/TestCpp/Classes/controller.cpp @@ -65,6 +65,7 @@ struct { { "ParallaxTest", [](){return new ParallaxTestScene(); } }, { "ParticleTest", [](){return new ParticleTestScene(); } }, { "PerformanceTest", []() { return new PerformanceTestScene(); } }, + { "PhysicsTest", []() { return new PhysicsTestScene(); } }, { "RenderTextureTest", [](){return new RenderTextureScene(); } }, { "RotateWorldTest", [](){return new RotateWorldTestScene(); } }, { "SceneTest", [](){return new SceneTestScene();} }, @@ -142,7 +143,7 @@ void TestController::menuCallback(Object * sender) // create the test scene and run it auto scene = g_aTestNames[idx].callback(); - if (scene) + if (scene && scene->initTest()) { scene->runThisTest(); scene->release(); diff --git a/samples/Cpp/TestCpp/Classes/testBasic.cpp b/samples/Cpp/TestCpp/Classes/testBasic.cpp index c1c7b22bad..9600d3b067 100644 --- a/samples/Cpp/TestCpp/Classes/testBasic.cpp +++ b/samples/Cpp/TestCpp/Classes/testBasic.cpp @@ -3,8 +3,11 @@ TestScene::TestScene(bool bPortrait) { - - Scene::init(); +} + +bool TestScene::initTest() +{ + return Scene::init(); } void TestScene::onEnter() diff --git a/samples/Cpp/TestCpp/Classes/testBasic.h b/samples/Cpp/TestCpp/Classes/testBasic.h index f4be332a7d..7bc95ab05c 100644 --- a/samples/Cpp/TestCpp/Classes/testBasic.h +++ b/samples/Cpp/TestCpp/Classes/testBasic.h @@ -11,6 +11,7 @@ class TestScene : public Scene { public: TestScene(bool bPortrait = false); + virtual bool initTest(); virtual void onEnter(); virtual void runThisTest() = 0; diff --git a/samples/Cpp/TestCpp/Classes/tests.h b/samples/Cpp/TestCpp/Classes/tests.h index ea26d0223c..455caf02b8 100644 --- a/samples/Cpp/TestCpp/Classes/tests.h +++ b/samples/Cpp/TestCpp/Classes/tests.h @@ -63,5 +63,6 @@ #include "TexturePackerEncryptionTest/TextureAtlasEncryptionTest.h" #include "DataVisitorTest/DataVisitorTest.h" #include "ConfigurationTest/ConfigurationTest.h" +#include "PhysicsTest/PhysicsTest.h" #endif diff --git a/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id b/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id index 42c17a6004..c276a3fc9e 100644 --- a/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -d36d2542f8c085962b01d856a008b085589e62e6 \ No newline at end of file +aa2d7f7606e4895b0fa0d8aa4031609822b30eaf \ No newline at end of file From 2c66f1b95e25d4bc787a9f83a16cba77127c7ff7 Mon Sep 17 00:00:00 2001 From: boyu0 Date: Mon, 16 Sep 2013 21:22:22 +0800 Subject: [PATCH 12/28] issue #2771: Implement the minimum set of physical API. Improve the PhysicsTest --- .../project.pbxproj.REMOVED.git-id | 2 +- cocos2dx/include/cocos2d.h | 4 +- .../CCLayer.cpp | 25 + .../layers_scenes_transitions_nodes/CCLayer.h | 7 + .../CCScene.cpp | 25 +- .../layers_scenes_transitions_nodes/CCScene.h | 18 +- .../CCPhysicsBodyInfo.cpp} | 16 + cocos2dx/physics/Box2D/CCPhysicsBodyInfo.h | 43 ++ .../physics/Box2D/CCPhysicsContactInfo.cpp | 39 ++ cocos2dx/physics/Box2D/CCPhysicsContactInfo.h | 43 ++ .../CCPhysicsHelper.h} | 36 +- .../CCPhysicsJointInfo.cpp} | 16 + cocos2dx/physics/Box2D/CCPhysicsJointInfo.h | 43 ++ cocos2dx/physics/Box2D/CCPhysicsShapeInfo.cpp | 39 ++ cocos2dx/physics/Box2D/CCPhysicsShapeInfo.h | 43 ++ cocos2dx/physics/Box2D/CCPhysicsWorldInfo.cpp | 39 ++ cocos2dx/physics/Box2D/CCPhysicsWorldInfo.h | 43 ++ cocos2dx/physics/CCPhysicsBody.cpp | 346 ++++++++++++-- cocos2dx/physics/CCPhysicsBody.h | 96 ++-- cocos2dx/physics/CCPhysicsContact.cpp | 137 ++++++ cocos2dx/physics/CCPhysicsContact.h | 136 ++++++ cocos2dx/physics/CCPhysicsJoint.cpp | 195 ++++++++ cocos2dx/physics/CCPhysicsJoint.h | 49 +- cocos2dx/physics/CCPhysicsSetting.h | 8 + cocos2dx/physics/CCPhysicsShape.cpp | 426 +++++++++++++++++ cocos2dx/physics/CCPhysicsShape.h | 182 +++++++ cocos2dx/physics/CCPhysicsWorld.cpp | 275 ++++++++++- cocos2dx/physics/CCPhysicsWorld.h | 53 ++- .../physics/chipmunk/CCPhysicsBodyInfo.cpp | 45 ++ .../CCPhysicsBodyInfo.h} | 32 +- .../physics/chipmunk/CCPhysicsContactInfo.cpp | 39 ++ .../physics/chipmunk/CCPhysicsContactInfo.h | 49 ++ cocos2dx/physics/chipmunk/CCPhysicsHelper.h | 68 +++ .../physics/chipmunk/CCPhysicsJointInfo.cpp | 43 ++ .../physics/chipmunk/CCPhysicsJointInfo.h | 50 ++ .../physics/chipmunk/CCPhysicsShapeInfo.cpp | 70 +++ .../physics/chipmunk/CCPhysicsShapeInfo.h | 61 +++ .../physics/chipmunk/CCPhysicsWorldInfo.cpp | 40 ++ .../physics/chipmunk/CCPhysicsWorldInfo.h | 48 ++ cocos2dx/physics/chipmunk/ChipmunkDebugDraw.c | 446 ++++++++++++++++++ cocos2dx/physics/chipmunk/ChipmunkDebugDraw.h | 50 ++ cocos2dx/sprite_nodes/CCSprite.cpp | 61 ++- cocos2dx/sprite_nodes/CCSprite.h | 2 + .../Classes/PhysicsTest/PhysicsTest.cpp | 114 +++-- .../TestCpp/Classes/PhysicsTest/PhysicsTest.h | 4 +- 45 files changed, 3379 insertions(+), 227 deletions(-) rename cocos2dx/physics/{CCPhysicsContactDelegate.cpp => Box2D/CCPhysicsBodyInfo.cpp} (84%) create mode 100644 cocos2dx/physics/Box2D/CCPhysicsBodyInfo.h create mode 100644 cocos2dx/physics/Box2D/CCPhysicsContactInfo.cpp create mode 100644 cocos2dx/physics/Box2D/CCPhysicsContactInfo.h rename cocos2dx/physics/{CCPhysicsFixture.h => Box2D/CCPhysicsHelper.h} (60%) rename cocos2dx/physics/{CCPhysicsFixture.cpp => Box2D/CCPhysicsJointInfo.cpp} (84%) create mode 100644 cocos2dx/physics/Box2D/CCPhysicsJointInfo.h create mode 100644 cocos2dx/physics/Box2D/CCPhysicsShapeInfo.cpp create mode 100644 cocos2dx/physics/Box2D/CCPhysicsShapeInfo.h create mode 100644 cocos2dx/physics/Box2D/CCPhysicsWorldInfo.cpp create mode 100644 cocos2dx/physics/Box2D/CCPhysicsWorldInfo.h create mode 100644 cocos2dx/physics/CCPhysicsContact.cpp create mode 100644 cocos2dx/physics/CCPhysicsContact.h create mode 100644 cocos2dx/physics/CCPhysicsShape.cpp create mode 100644 cocos2dx/physics/CCPhysicsShape.h create mode 100644 cocos2dx/physics/chipmunk/CCPhysicsBodyInfo.cpp rename cocos2dx/physics/{CCPhysicsContactDelegate.h => chipmunk/CCPhysicsBodyInfo.h} (69%) create mode 100644 cocos2dx/physics/chipmunk/CCPhysicsContactInfo.cpp create mode 100644 cocos2dx/physics/chipmunk/CCPhysicsContactInfo.h create mode 100644 cocos2dx/physics/chipmunk/CCPhysicsHelper.h create mode 100644 cocos2dx/physics/chipmunk/CCPhysicsJointInfo.cpp create mode 100644 cocos2dx/physics/chipmunk/CCPhysicsJointInfo.h create mode 100644 cocos2dx/physics/chipmunk/CCPhysicsShapeInfo.cpp create mode 100644 cocos2dx/physics/chipmunk/CCPhysicsShapeInfo.h create mode 100644 cocos2dx/physics/chipmunk/CCPhysicsWorldInfo.cpp create mode 100644 cocos2dx/physics/chipmunk/CCPhysicsWorldInfo.h create mode 100644 cocos2dx/physics/chipmunk/ChipmunkDebugDraw.c create mode 100644 cocos2dx/physics/chipmunk/ChipmunkDebugDraw.h diff --git a/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id b/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id index 1eaf467207..ac85a298cf 100644 --- a/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -cbb5290ea17f6020e0249818c08c9d94a17dd073 \ No newline at end of file +f64b17366179be63ab250b6a65922b95efbd9712 \ No newline at end of file diff --git a/cocos2dx/include/cocos2d.h b/cocos2dx/include/cocos2d.h index d8ee1e9099..a841f7fa5c 100755 --- a/cocos2dx/include/cocos2d.h +++ b/cocos2dx/include/cocos2d.h @@ -126,8 +126,8 @@ THE SOFTWARE. // physics #include "physics/CCPhysicsBody.h" -#include "physics/CCPhysicsContactDelegate.h" -#include "physics/CCPhysicsFixture.h" +#include "physics/CCPhysicsContact.h" +#include "physics/CCPhysicsShape.h" #include "physics/CCPhysicsJoint.h" #include "physics/CCPhysicsWorld.h" diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp index c00cf03903..04043c332e 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp @@ -38,6 +38,7 @@ THE SOFTWARE. // extern #include "kazmath/GL/matrix.h" #include "keyboard_dispatcher/CCKeyboardDispatcher.h" +#include "CCScene.h" NS_CC_BEGIN @@ -499,6 +500,30 @@ void Layer::ccTouchesCancelled(Set *pTouches, Event *pEvent) CC_UNUSED_PARAM(pEvent); } + +#ifdef CC_USE_PHYSICS +void Layer::addChild(Node* child) +{ + Node::addChild(child); +} + +void Layer::addChild(Node* child, int zOrder) +{ + Node::addChild(child, zOrder); +} + +void Layer::addChild(Node* child, int zOrder, int tag) +{ + Node::addChild(child, zOrder, tag); + + if (this->getParent() && + dynamic_cast(this->getParent()) != nullptr) + { + dynamic_cast(this->getParent())->addChildToPhysicsWorld(child); + } +} +#endif + // LayerRGBA LayerRGBA::LayerRGBA() : _displayedOpacity(255) diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h index 32e8991b75..1c7792a29a 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h @@ -36,6 +36,7 @@ THE SOFTWARE. #ifdef EMSCRIPTEN #include "base_nodes/CCGLBufferedNode.h" #endif // EMSCRIPTEN +#include "physics/CCPhysicsSetting.h" NS_CC_BEGIN @@ -138,6 +139,12 @@ public: virtual void onEnter() override; virtual void onExit() override; virtual void onEnterTransitionDidFinish() override; + +#ifdef CC_USE_PHYSICS + virtual void addChild(Node* child) override; + virtual void addChild(Node* child, int zOrder) override; + virtual void addChild(Node* child, int zOrder, int tag) override; +#endif // CC_USE_PHYSICS protected: bool _touchEnabled; diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp index 589a77d60b..36f2947dc6 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCScene.cpp @@ -99,12 +99,14 @@ bool Scene::initWithPhysics() CC_BREAK_IF( ! (pDirector = Director::getInstance()) ); this->setContentSize(pDirector->getWinSize()); CC_BREAK_IF(! (_physicsWorld = PhysicsWorld::create())); + _physicsWorld->setScene(this); + + this->scheduleUpdate(); // success bRet = true; } while (0); return bRet; } -#endif void Scene::addChild(Node* child) { @@ -120,23 +122,27 @@ void Scene::addChild(Node* child, int zOrder, int tag) { Node::addChild(child, zOrder, tag); -#ifdef CC_USE_PHYSICS + addChildToPhysicsWorld(child); +} + +void Scene::addChildToPhysicsWorld(Node* child) +{ if (_physicsWorld) { auto addToPhysicsWorldFunc = [this](Object* node) -> void { - if (typeid(Sprite).hash_code() == typeid(node).hash_code()) + if (dynamic_cast(node) != nullptr) { Sprite* sp = dynamic_cast(node); - if (sp && sp->getPhysicsBody()) + if (sp->getPhysicsBody()) { _physicsWorld->addChild(sp->getPhysicsBody()); } } }; - if(typeid(Layer).hash_code() == typeid(child).hash_code()) + if(dynamic_cast(child) != nullptr) { Object* subChild = nullptr; CCARRAY_FOREACH(child->getChildren(), subChild) @@ -148,8 +154,15 @@ void Scene::addChild(Node* child, int zOrder, int tag) addToPhysicsWorldFunc(child); } } -#endif } +void Scene::update(float delta) +{ + Node::update(delta); + + _physicsWorld->update(delta); +} +#endif + NS_CC_END diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCScene.h b/cocos2dx/layers_scenes_transitions_nodes/CCScene.h index 4b5b27f4a4..217d01c756 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCScene.h +++ b/cocos2dx/layers_scenes_transitions_nodes/CCScene.h @@ -60,22 +60,30 @@ public: virtual ~Scene(); bool init(); + #ifdef CC_USE_PHYSICS +public: bool initWithPhysics(); -#endif virtual void addChild(Node* child) override; virtual void addChild(Node* child, int zOrder) override; virtual void addChild(Node* child, int zOrder, int tag) override; -#ifdef CC_USE_PHYSICS + /* + * Update method will be called automatically every frame if "scheduleUpdate" is called, and the node is "live" + */ + virtual void update(float delta) override; + inline PhysicsWorld* getPhysicsWorld() { return _physicsWorld; } -#endif protected: -#ifdef CC_USE_PHYSICS + virtual void addChildToPhysicsWorld(Node* child); + +protected: PhysicsWorld* _physicsWorld; -#endif +#endif // CC_USE_PHYSICS + + friend class Layer; }; // end of scene group diff --git a/cocos2dx/physics/CCPhysicsContactDelegate.cpp b/cocos2dx/physics/Box2D/CCPhysicsBodyInfo.cpp similarity index 84% rename from cocos2dx/physics/CCPhysicsContactDelegate.cpp rename to cocos2dx/physics/Box2D/CCPhysicsBodyInfo.cpp index 39b3eedbc5..bfa20b616a 100644 --- a/cocos2dx/physics/CCPhysicsContactDelegate.cpp +++ b/cocos2dx/physics/Box2D/CCPhysicsBodyInfo.cpp @@ -21,3 +21,19 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ + +#include "CCPhysicsBodyInfo.h" + +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) +NS_CC_BEGIN + +PhysicsBodyInfo::PhysicsBodyInfo() +{ +} + +PhysicsBodyInfo::~PhysicsBodyInfo() +{ +} + +NS_CC_END +#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D diff --git a/cocos2dx/physics/Box2D/CCPhysicsBodyInfo.h b/cocos2dx/physics/Box2D/CCPhysicsBodyInfo.h new file mode 100644 index 0000000000..730837ed9d --- /dev/null +++ b/cocos2dx/physics/Box2D/CCPhysicsBodyInfo.h @@ -0,0 +1,43 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "../CCPhysicsSetting.h" +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) + +#ifndef __CCPHYSICS_BODY_INFO_H__ +#define __CCPHYSICS_BODY_INFO_H__ +#include "platform/CCPlatformMacros.h" +NS_CC_BEGIN + +class PhysicsBodyInfo +{ +public: + PhysicsBodyInfo(); + ~PhysicsBodyInfo(); +}; + +NS_CC_END +#endif // __CCPHYSICS_BODY_INFO_H__ + +#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D diff --git a/cocos2dx/physics/Box2D/CCPhysicsContactInfo.cpp b/cocos2dx/physics/Box2D/CCPhysicsContactInfo.cpp new file mode 100644 index 0000000000..41eaa4f831 --- /dev/null +++ b/cocos2dx/physics/Box2D/CCPhysicsContactInfo.cpp @@ -0,0 +1,39 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "CCPhysicsContactInfo.h" + +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) +NS_CC_BEGIN + +PhysicsContactInfo::PhysicsContactInfo() +{ +} + +PhysicsContactInfo::~PhysicsContactInfo() +{ +} + +NS_CC_END +#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D diff --git a/cocos2dx/physics/Box2D/CCPhysicsContactInfo.h b/cocos2dx/physics/Box2D/CCPhysicsContactInfo.h new file mode 100644 index 0000000000..b592caa0af --- /dev/null +++ b/cocos2dx/physics/Box2D/CCPhysicsContactInfo.h @@ -0,0 +1,43 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "../CCPhysicsSetting.h" +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) + +#ifndef __CCPHYSICS_CONTACT_INFO_H__ +#define __CCPHYSICS_CONTACT_INFO_H__ +#include "platform/CCPlatformMacros.h" +NS_CC_BEGIN + +class PhysicsContactInfo +{ +public: + PhysicsContactInfo(); + ~PhysicsContactInfo(); +}; + +NS_CC_END +#endif // __CCPHYSICS_CONTACT_INFO_H__ + +#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D diff --git a/cocos2dx/physics/CCPhysicsFixture.h b/cocos2dx/physics/Box2D/CCPhysicsHelper.h similarity index 60% rename from cocos2dx/physics/CCPhysicsFixture.h rename to cocos2dx/physics/Box2D/CCPhysicsHelper.h index e743ab3c44..86ef7d74ee 100644 --- a/cocos2dx/physics/CCPhysicsFixture.h +++ b/cocos2dx/physics/Box2D/CCPhysicsHelper.h @@ -22,42 +22,22 @@ THE SOFTWARE. ****************************************************************************/ -#include "CCPhysicsSetting.h" -#ifdef CC_USE_PHYSICS +#include "../CCPhysicsSetting.h" +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) -#ifndef __CCPHYSICS_FIXTURE_H__ -#define __CCPHYSICS_FIXTURE_H__ +#ifndef __CCPHYSICS_HELPER_H__ +#define __CCPHYSICS_HELPER_H__ -#include "cocoa/CCObject.h" +#include "platform/CCPlatformMacros.h" #include "cocoa/CCGeometry.h" NS_CC_BEGIN -class PhysicsFixture : public Object +class PhysicsHelper { -public: - static PhysicsFixture* createCircle(Point centre, float radius); - static PhysicsFixture* createRectangle(Rect rect); - static PhysicsFixture* createPolygon(Array* points); - - static PhysicsFixture* createEdgeSegment(Point x, Point y); - static PhysicsFixture* createEdgeCircle(Point centre, float radius); - static PhysicsFixture* createEdgeRectangle(Rect rect); - static PhysicsFixture* createEdgePolygon(Array* points); - -protected: - bool init(); - -protected: - PhysicsFixture(); - virtual ~PhysicsFixture(); - -protected: - float _density; - float _friction; }; NS_CC_END -#endif // __CCPHYSICS_FIXTURE_H__ +#endif // __CCPHYSICS_HELPER_H__ -#endif // CC_USE_PHYSICS +#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D diff --git a/cocos2dx/physics/CCPhysicsFixture.cpp b/cocos2dx/physics/Box2D/CCPhysicsJointInfo.cpp similarity index 84% rename from cocos2dx/physics/CCPhysicsFixture.cpp rename to cocos2dx/physics/Box2D/CCPhysicsJointInfo.cpp index 39b3eedbc5..753ba41534 100644 --- a/cocos2dx/physics/CCPhysicsFixture.cpp +++ b/cocos2dx/physics/Box2D/CCPhysicsJointInfo.cpp @@ -21,3 +21,19 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ + +#include "CCPhysicsJointInfo.h" + +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) +NS_CC_BEGIN + +PhysicsJointInfo::PhysicsJointInfo() +{ +} + +PhysicsJointInfo::~PhysicsJointInfo() +{ +} + +NS_CC_END +#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D diff --git a/cocos2dx/physics/Box2D/CCPhysicsJointInfo.h b/cocos2dx/physics/Box2D/CCPhysicsJointInfo.h new file mode 100644 index 0000000000..abcf089387 --- /dev/null +++ b/cocos2dx/physics/Box2D/CCPhysicsJointInfo.h @@ -0,0 +1,43 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "../CCPhysicsSetting.h" +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) + +#ifndef __CCPHYSICS_JOINT_INFO_H__ +#define __CCPHYSICS_JOINT_INFO_H__ +#include "platform/CCPlatformMacros.h" +NS_CC_BEGIN + +class PhysicsJointInfo +{ +public: + PhysicsJointInfo(); + ~PhysicsJointInfo(); +}; + +NS_CC_END +#endif // __CCPHYSICS_JOINT_INFO_H__ + +#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D diff --git a/cocos2dx/physics/Box2D/CCPhysicsShapeInfo.cpp b/cocos2dx/physics/Box2D/CCPhysicsShapeInfo.cpp new file mode 100644 index 0000000000..a89ba7dae7 --- /dev/null +++ b/cocos2dx/physics/Box2D/CCPhysicsShapeInfo.cpp @@ -0,0 +1,39 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "CCPhysicsShapeInfo.h" + +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) +NS_CC_BEGIN + +PhysicsShapeInfo::PhysicsShapeInfo() +{ +} + +PhysicsShapeInfo::~PhysicsShapeInfo() +{ +} + +NS_CC_END +#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D diff --git a/cocos2dx/physics/Box2D/CCPhysicsShapeInfo.h b/cocos2dx/physics/Box2D/CCPhysicsShapeInfo.h new file mode 100644 index 0000000000..9091001887 --- /dev/null +++ b/cocos2dx/physics/Box2D/CCPhysicsShapeInfo.h @@ -0,0 +1,43 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "../CCPhysicsSetting.h" +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) + +#ifndef __CCPHYSICS_SHAPE_INFO_H__ +#define __CCPHYSICS_SHAPE_INFO_H__ +#include "platform/CCPlatformMacros.h" +NS_CC_BEGIN + +class PhysicsShapeInfo +{ +public: + PhysicsShapeInfo(); + ~PhysicsShapeInfo(); +}; + +NS_CC_END +#endif // __CCPHYSICS_SHAPE_INFO_H__ + +#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D diff --git a/cocos2dx/physics/Box2D/CCPhysicsWorldInfo.cpp b/cocos2dx/physics/Box2D/CCPhysicsWorldInfo.cpp new file mode 100644 index 0000000000..0de6c00d82 --- /dev/null +++ b/cocos2dx/physics/Box2D/CCPhysicsWorldInfo.cpp @@ -0,0 +1,39 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "CCPhysicsWorldInfo.h" + +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) +NS_CC_BEGIN + +PhysicsWorldInfo::PhysicsWorldInfo() +{ +} + +PhysicsWorldInfo::~PhysicsWorldInfo() +{ +} + +NS_CC_END +#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D diff --git a/cocos2dx/physics/Box2D/CCPhysicsWorldInfo.h b/cocos2dx/physics/Box2D/CCPhysicsWorldInfo.h new file mode 100644 index 0000000000..bcc09124db --- /dev/null +++ b/cocos2dx/physics/Box2D/CCPhysicsWorldInfo.h @@ -0,0 +1,43 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "../CCPhysicsSetting.h" +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) + +#ifndef __CCPHYSICS_WORLD_INFO_H__ +#define __CCPHYSICS_WORLD_INFO_H__ +#include "platform/CCPlatformMacros.h" +NS_CC_BEGIN + +class PhysicsWorldInfo +{ +public: + PhysicsWorldInfo(); + ~PhysicsWorldInfo(); +}; + +NS_CC_END +#endif // __CCPHYSICS_WORLD_INFO_H__ + +#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D diff --git a/cocos2dx/physics/CCPhysicsBody.cpp b/cocos2dx/physics/CCPhysicsBody.cpp index 0b2309a1c1..588586dded 100644 --- a/cocos2dx/physics/CCPhysicsBody.cpp +++ b/cocos2dx/physics/CCPhysicsBody.cpp @@ -22,51 +22,74 @@ THE SOFTWARE. ****************************************************************************/ #include "CCPhysicsBody.h" - #ifdef CC_USE_PHYSICS +#include "CCPhysicsShape.h" +#include "CCPhysicsJoint.h" +#include "CCPhysicsWorld.h" + +#include "chipmunk/CCPhysicsBodyInfo.h" +#include "Box2D/CCPhysicsBodyInfo.h" +#include "chipmunk/CCPhysicsJointInfo.h" +#include "Box2D/CCPhysicsJointInfo.h" +#include "chipmunk/CCPhysicsWorldInfo.h" +#include "Box2D/CCPhysicsWorldInfo.h" + +#include "chipmunk/CCPhysicsHelper.h" +#include "Box2D/CCPhysicsHelper.h" + NS_CC_BEGIN #if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) -class PhysicsBodyInfo +namespace { -public: - PhysicsBodyInfo(); - ~PhysicsBodyInfo(); - -public: - cpBody* body; - cpShape* shape; -}; - -PhysicsBodyInfo::PhysicsBodyInfo() -: body(nullptr) -, shape(nullptr) -{ -} - -PhysicsBodyInfo::~PhysicsBodyInfo() -{ - if (body) cpBodyFree(body); - if (shape) cpShapeFree(shape); + static const float MASS_DEFAULT = 1.0; + static const float DENSITY_DEFAULT = 1.0; + static const float ANGULARDAMPING_DEFAULT = 200; } PhysicsBody::PhysicsBody() +: _owner(nullptr) +, _world(nullptr) +, _info(nullptr) +, _dynamic(false) +, _massDefault(true) +, _angularDampingDefault(true) +, _mass(MASS_DEFAULT) +, _area(0.0) +, _density(DENSITY_DEFAULT) +, _angularDamping(ANGULARDAMPING_DEFAULT) { } PhysicsBody::~PhysicsBody() { CC_SAFE_DELETE(_info); + + for (auto it = _shapes.begin(); it != _shapes.end(); ++it) + { + delete *it; + } + + for (auto it = _joints.begin(); it != _joints.end(); ++it) + { + PhysicsJoint* joint = *it; + PhysicsBody* other = joint->getBodyA() == this ? joint->getBodyA() : joint->getBodyB(); + + other->_joints.erase(std::find(other->_joints.begin(), other->_joints.end(), joint)); + delete joint; + } } -PhysicsBody* PhysicsBody::create() +PhysicsBody* PhysicsBody::createCircle(float radius) { - PhysicsBody * body = new PhysicsBody(); - if(body && body->init()) + PhysicsBody* body = new PhysicsBody(); + if (body && body->init()) { + body->addCircle(radius); + body->autorelease(); return body; } @@ -74,6 +97,156 @@ PhysicsBody* PhysicsBody::create() return nullptr; } +PhysicsBody* PhysicsBody::createBox(Size size) +{ + PhysicsBody* body = new PhysicsBody(); + if (body && body->init()) + { + body->addBox(size); + body->autorelease(); + return body; + } + + CC_SAFE_DELETE(body); + return nullptr; +} + +PhysicsBody* PhysicsBody::createPolygon(Point* points, int count) +{ + PhysicsBody* body = new PhysicsBody(); + if (body && body->init()) + { + body->addPolygon(points, count); + body->autorelease(); + return body; + } + + CC_SAFE_DELETE(body); + return nullptr; +} + +PhysicsBody* PhysicsBody::createEdgeSegment(Point a, Point b, float border/* = 1*/) +{ + PhysicsBody* body = new PhysicsBody(); + if (body && body->initStatic()) + { + body->addEdgeSegment(a, b, border); + body->autorelease(); + return body; + } + + CC_SAFE_DELETE(body); + return nullptr; +} + +PhysicsBody* PhysicsBody::createEdgeBox(Size size, float border/* = 1*/) +{ + PhysicsBody* body = new PhysicsBody(); + if (body && body->initStatic()) + { + body->addEdgeBox(size, border); + body->autorelease(); + return body; + } + + CC_SAFE_DELETE(body); + + return nullptr; +} + +PhysicsBody* PhysicsBody::createEdgePolygon(Point* points, int count, float border/* = 1*/) +{ + PhysicsBody* body = new PhysicsBody(); + if (body && body->initStatic()) + { + body->addEdgePolygon(points, count, border); + body->autorelease(); + return body; + } + + CC_SAFE_DELETE(body); + + return nullptr; +} + +PhysicsBody* PhysicsBody::createEdgeChain(Point* points, int count, float border/* = 1*/) +{ + PhysicsBody* body = new PhysicsBody(); + if (body && body->initStatic()) + { + body->addEdgeChain(points, count); + body->autorelease(); + return body; + } + + CC_SAFE_DELETE(body); + + return nullptr; +} + +PhysicsShapeCircle* PhysicsBody::addCircle(float radius, Point offset/* = Point(0, 0)*/) +{ + _area = PhysicsHelper::cpfloat2float(cpAreaForCircle(0, radius)); + setMass((_massDefault ? 0 : getMass()) + _area * _density); + + setAngularDamping((_angularDampingDefault ? 0 : getAngularDamping()) + + PhysicsHelper::cpfloat2float(cpMomentForCircle(getMass(), 0, radius, PhysicsHelper::point2cpv(offset)))); + + return PhysicsShapeCircle::create(this, radius, offset); +} + +PhysicsShapeBox* PhysicsBody::addBox(Size size, Point offset/* = Point(0, 0)*/) +{ + cpVect cpOffset = PhysicsHelper::size2cpv(size); + cpVect vec[4] = {}; + vec[0] = cpv(0, 0); + vec[1] = cpv(0, cpOffset.y); + vec[2] = cpv(cpOffset.x, cpOffset.y); + vec[3] = cpv(cpOffset.x, 0); + + _area = PhysicsHelper::cpfloat2float(cpAreaForPoly(4, vec)); + setMass((_massDefault ? 0 : getMass()) + _area * _density); + + setAngularDamping((_angularDampingDefault ? 0 : getAngularDamping()) + + PhysicsHelper::cpfloat2float(cpMomentForBox(getMass(), PhysicsHelper::float2cpfloat(size.width), PhysicsHelper::float2cpfloat(size.height)))); + + return PhysicsShapeBox::create(this, size, offset); +} + +PhysicsShapePolygon* PhysicsBody::addPolygon(Point* points, int count, Point offset/* = Point(0, 0)*/) +{ + cpVect* vec = new cpVect[count]; + PhysicsHelper::points2cpvs(points, vec, count); + _area = PhysicsHelper::cpfloat2float(cpAreaForPoly(count, vec)); + + setAngularDamping((_angularDampingDefault ? 0 : getAngularDamping()) + + PhysicsHelper::cpfloat2float(cpMomentForPoly(getMass(), count, vec, PhysicsHelper::point2cpv(offset)))); + + delete[] vec; + + return PhysicsShapePolygon::create(this, points, count, offset); +} + +PhysicsShapeEdgeSegment* PhysicsBody::addEdgeSegment(Point a, Point b, float border/* = 1*/) +{ + return PhysicsShapeEdgeSegment::create(this, a, b, border); +} + +PhysicsShapeEdgeBox* PhysicsBody::addEdgeBox(Size size, float border/* = 1*/, Point offset/* = Point(0, 0)*/) +{ + return PhysicsShapeEdgeBox::create(this, size, border, offset); +} + +PhysicsShapeEdgePolygon* PhysicsBody::addEdgePolygon(Point* points, int count, float border/* = 1*/) +{ + return PhysicsShapeEdgePolygon::create(this, points, count); +} + +PhysicsShapeEdgeChain* PhysicsBody::addEdgeChain(Point* points, int count, float border/* = 1*/) +{ + return PhysicsShapeEdgeChain::create(this, points, count, border); +} + bool PhysicsBody::init() { do @@ -81,8 +254,9 @@ bool PhysicsBody::init() _info = new PhysicsBodyInfo(); CC_BREAK_IF(_info == nullptr); - _info->body = cpBodyNew(1.0, 1.0); + _info->body = cpBodyNew(PhysicsHelper::float2cpfloat(_mass), PhysicsHelper::float2cpfloat(_angularDamping)); CC_BREAK_IF(_info->body == nullptr); + _dynamic = true; return true; } while (false); @@ -90,26 +264,118 @@ bool PhysicsBody::init() return false; } -PhysicsBody* PhysicsBody::createCircle(Point centre, float radius) +void PhysicsBody::setDynamic(bool dynamic) { - PhysicsBody* body = PhysicsBody::create(); - - if (body == nullptr) + _dynamic = dynamic; + if (_world != nullptr && cpBodyIsStatic(_info->body) == (cpBool)_dynamic) { - return nullptr; + if (dynamic) + { + cpSpaceConvertBodyToDynamic(_world->_info->space, _info->body, _mass, _angularDamping); + }else + { + cpSpaceConvertBodyToStatic(_world->_info->space, _info->body); + } } - - cpBodySetPos(body->_info->body, cpv(centre.x, centre.y)); - body->_info->shape = cpCircleShapeNew(body->_info->body, radius, cpvzero); - - if (body->_info->shape == nullptr) - { - return nullptr; - } - - return body; } +bool PhysicsBody::initStatic() +{ + do + { + _info = new PhysicsBodyInfo(); + CC_BREAK_IF(_info == nullptr); + + _info->body = cpBodyNewStatic(); + CC_BREAK_IF(_info->body == nullptr); + _dynamic = false; + + return true; + } while (false); + + return false; +} + +void PhysicsBody::setPosition(Point position) +{ + cpBodySetPos(_info->body, PhysicsHelper::point2cpv(position)); +} + +void PhysicsBody::setRotation(float rotation) +{ + cpBodySetAngle(_info->body, PhysicsHelper::float2cpfloat(rotation)); +} + +Point PhysicsBody::getPosition() const +{ + cpVect vec = cpBodyGetPos(_info->body); + return PhysicsHelper::cpv2point(vec); +} + +float PhysicsBody::getRotation() const +{ + return -PhysicsHelper::cpfloat2float(cpBodyGetAngle(_info->body) / 3.14f * 180.0f); +} + +void PhysicsBody::addShape(PhysicsShape* shape) +{ + if (shape == nullptr) return; + + _shapes.push_back(shape); + + if (_world != nullptr) _world->addShape(shape); +} + +void PhysicsBody::applyForce(Point force) +{ + applyForce(force, Point()); +} + +void PhysicsBody::applyForce(Point force, Point offset) +{ + cpBodyApplyForce(_info->body, PhysicsHelper::point2cpv(force), PhysicsHelper::point2cpv(offset)); +} + +void PhysicsBody::applyImpulse(Point impulse) +{ + applyImpulse(impulse, Point()); +} + +void PhysicsBody::applyImpulse(Point impulse, Point offset) +{ + cpBodyApplyImpulse(_info->body, PhysicsHelper::point2cpv(impulse), PhysicsHelper::point2cpv(offset)); +} + +void PhysicsBody::applyTorque(float torque) +{ + cpBodySetTorque(_info->body, PhysicsHelper::float2cpfloat(torque)); +} + +void PhysicsBody::setMass(float mass) +{ + _mass = mass; + _massDefault = false; + + cpBodySetMass(_info->body, PhysicsHelper::float2cpfloat(_mass)); +} + +void PhysicsBody::setAngularDamping(float angularDamping) +{ + _angularDamping = angularDamping; + _angularDampingDefault = false; + + cpBodySetMoment(_info->body, _angularDamping); +} + +//Clonable* PhysicsBody::clone() const +//{ +// PhysicsBody* body = new PhysicsBody(); +// +// body->autorelease(); +// +// return body; +//} + #elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) diff --git a/cocos2dx/physics/CCPhysicsBody.h b/cocos2dx/physics/CCPhysicsBody.h index 64df98ec3f..08fffb8722 100644 --- a/cocos2dx/physics/CCPhysicsBody.h +++ b/cocos2dx/physics/CCPhysicsBody.h @@ -36,33 +36,48 @@ NS_CC_BEGIN class Sprite; class PhysicsWorld; class PhysicsJoint; -class PhysicsFixture; +class PhysicsShape; +class PhysicsShapeCircle; +class PhysicsShapeBox; +class PhysicsShapePolygon; +class PhysicsShapeEdgeSegment; +class PhysicsShapeEdgeBox; +class PhysicsShapeEdgePolygon; +class PhysicsShapeEdgeChain; + class PhysicsBodyInfo; -class PhysicsBody : public Object, public Clonable +class PhysicsBody : public Object//, public Clonable { public: - static PhysicsBody* createCircle(Point centre, float radius); - static PhysicsBody* createRectangle(Rect rect); - static PhysicsBody* createPolygon(Array* points); + static PhysicsBody* createCircle(float radius); + static PhysicsBody* createBox(Size size); + static PhysicsBody* createPolygon(Point* points, int count); - static PhysicsBody* createEdgeSegment(Point x, Point y); - static PhysicsBody* createEdgeCircle(Point centre, float radius); - static PhysicsBody* createEdgeRectangle(Rect rect); - static PhysicsBody* createEdgePolygon(Array* points); - static PhysicsBody* createEdgeChain(Array* points); + static PhysicsBody* createEdgeSegment(Point a, Point b, float border = 1); + static PhysicsBody* createEdgeBox(Size size, float border = 1); + static PhysicsBody* createEdgePolygon(Point* points, int count, float border = 1); + static PhysicsBody* createEdgeChain(Point* points, int count, float border = 1); + + virtual PhysicsShapeCircle* addCircle(float radius, Point offset = Point(0, 0)); + virtual PhysicsShapeBox* addBox(Size size, Point offset = Point(0, 0)); + virtual PhysicsShapePolygon* addPolygon(Point* points, int count, Point offset = Point(0, 0)); + + virtual PhysicsShapeEdgeSegment* addEdgeSegment(Point a, Point b, float border = 1); + virtual PhysicsShapeEdgeBox* addEdgeBox(Size size, float border = 1, Point offset = Point(0, 0)); + virtual PhysicsShapeEdgePolygon* addEdgePolygon(Point* points, int count, float border = 1); + virtual PhysicsShapeEdgeChain* addEdgeChain(Point* points, int count, float border = 1); virtual void applyForce(Point force); - virtual void applyForce(Point force, Point point); + virtual void applyForce(Point force, Point offset); virtual void applyImpulse(Point impulse); - virtual void applyImpulse(Point impulse, Point point); + virtual void applyImpulse(Point impulse, Point offset); virtual void applyTorque(float torque); - virtual void applyAngularImpulse(float impulse); - void addFixture(PhysicsFixture* fixture); - inline Array* getFixtures() const { return _fixtures; } - void removeFixture(PhysicsFixture* fixture); - void removeAllFixtures(); + inline std::vector& getShapes() { return _shapes; } + inline PhysicsShape* getShape() { return _shapes.size() >= 1 ? _shapes.front() : nullptr; } + void removeShape(PhysicsShape* shape); + void removeAllShapes(); inline PhysicsWorld* getWorld() const { return _world; } inline const std::vector* getJoints() const { return &_joints; } @@ -76,34 +91,55 @@ public: void setCollisionBitmask(int bitmask); inline int getCollisionBitmask() const { return _collisionBitmask; } - virtual Clonable* clone() const override; + Point getPosition() const; + float getRotation() const; + + inline bool isDynamic() { return _dynamic; } + void setDynamic(bool dynamic); + + void setMass(float mass); + inline float getMass() { return _mass; } + + void setAngularDamping(float angularDamping); + inline float getAngularDamping() { return _angularDamping; } + + //virtual Clonable* clone() const override; protected: - static PhysicsBody* create(); + bool init(); + bool initStatic(); + + virtual void setPosition(Point position); + virtual void setRotation(float rotation); + virtual void addShape(PhysicsShape* shape); protected: PhysicsBody(); virtual ~PhysicsBody(); protected: - float _mass; - float _density; - float _area; - float _friction; Sprite* _owner; - Point _velocity; - float _angularVelocity; - bool _resting; + std::vector _joints; + std::vector _shapes; + PhysicsWorld* _world; + PhysicsBodyInfo* _info; + bool _dynamic; + bool _massDefault; + bool _angularDampingDefault; + float _mass; + float _area; + float _density; + float _angularDamping; int _categoryBitmask; int _contactTestBitmask; int _collisionBitmask; - std::vector _joints; - Array* _fixtures; - PhysicsWorld* _world; - PhysicsBodyInfo* _info; + friend class PhysicsWorld; + friend class PhysicsShape; + friend class PhysicsJoint; + friend class Sprite; }; NS_CC_END diff --git a/cocos2dx/physics/CCPhysicsContact.cpp b/cocos2dx/physics/CCPhysicsContact.cpp new file mode 100644 index 0000000000..d76b93f9d8 --- /dev/null +++ b/cocos2dx/physics/CCPhysicsContact.cpp @@ -0,0 +1,137 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "CCPhysicsContact.h" +#ifdef CC_USE_PHYSICS +#include "chipmunk/CCPhysicsContactInfo.h" +#include "Box2D/CCPhysicsContactInfo.h" + +NS_CC_BEGIN + +PhysicsContact::PhysicsContact() +: _shapeA(nullptr) +, _shapeB(nullptr) +, _info(nullptr) +, _data(nullptr) +{ + +} + +PhysicsContact::~PhysicsContact() +{ + CC_SAFE_DELETE(_info); +} + +PhysicsContact* PhysicsContact::create(PhysicsShape* a, PhysicsShape* b) +{ + PhysicsContact * contact = new PhysicsContact(); + if(contact && contact->init(a, b)) + { + return contact; + } + + CC_SAFE_DELETE(contact); + return nullptr; +} + +bool PhysicsContact::init(PhysicsShape* a, PhysicsShape* b) +{ + do + { + CC_BREAK_IF(a == nullptr || b == nullptr); + + CC_BREAK_IF(!(_info = new PhysicsContactInfo(this))); + + _shapeA = a; + _shapeB = b; + + return true; + } while(false); + + return false; +} + +// PhysicsContactPreSolve implementation +PhysicsContactPreSolve::PhysicsContactPreSolve() +{ + +} + +PhysicsContactPreSolve::~PhysicsContactPreSolve() +{ + +} + +PhysicsContactPreSolve* PhysicsContactPreSolve::create() +{ + PhysicsContactPreSolve * solve = new PhysicsContactPreSolve(); + if(solve && solve->init()) + { + return solve; + } + + CC_SAFE_DELETE(solve); + return nullptr; +} + +bool PhysicsContactPreSolve::init() +{ + return true; +} + + +// PhysicsContactPostSolve implementation +PhysicsContactPostSolve::PhysicsContactPostSolve() +{ + +} + +PhysicsContactPostSolve::~PhysicsContactPostSolve() +{ + +} + +PhysicsContactPostSolve* PhysicsContactPostSolve::create() +{ + PhysicsContactPostSolve * solve = new PhysicsContactPostSolve(); + if(solve && solve->init()) + { + return solve; + } + + CC_SAFE_DELETE(solve); + return nullptr; +} + +bool PhysicsContactPostSolve::init() +{ + return true; +} + + +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) +#elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) +#endif + +NS_CC_END +#endif // CC_USE_PHYSICS diff --git a/cocos2dx/physics/CCPhysicsContact.h b/cocos2dx/physics/CCPhysicsContact.h new file mode 100644 index 0000000000..6db1fd47f7 --- /dev/null +++ b/cocos2dx/physics/CCPhysicsContact.h @@ -0,0 +1,136 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "CCPhysicsSetting.h" +#ifdef CC_USE_PHYSICS + +#ifndef __CCPHYSICS_CONTACT_H__ +#define __CCPHYSICS_CONTACT_H__ + +#include "cocoa/CCObject.h" +#include "cocoa/CCGeometry.h" + +NS_CC_BEGIN + +class PhysicsShape; +class PhysicsWorld; + +namespace PhysicsInnerCallbackFunctions +{ +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) + int collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data); + int collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); + void collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); + void collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); +#endif +} + +class PhysicsContactInfo; + +class PhysicsContact +{ +public: + inline PhysicsShape* getShapeA() { return _shapeA; } + inline PhysicsShape* getShapeB() { return _shapeB; } + inline void* getData() { return _data; } + +private: + static PhysicsContact* create(PhysicsShape* a, PhysicsShape* b); + bool init(PhysicsShape* a, PhysicsShape* b); + +private: + PhysicsContact(); + ~PhysicsContact(); + +private: + PhysicsShape* _shapeA; + PhysicsShape* _shapeB; + PhysicsContactInfo* _info; + void* _data; + + friend class PhysicsWorld; + +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) + friend int PhysicsInnerCallbackFunctions::collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data); + friend int PhysicsInnerCallbackFunctions::collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); + friend void PhysicsInnerCallbackFunctions::collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); + friend void PhysicsInnerCallbackFunctions::collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); +#endif +}; + +class PhysicsContactPreSolve +{ +private: + PhysicsContactPreSolve(); + ~PhysicsContactPreSolve(); + + static PhysicsContactPreSolve* create(); + bool init(); + + friend class PhysicsWorld; + +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) + friend int PhysicsInnerCallbackFunctions::collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data); + friend int PhysicsInnerCallbackFunctions::collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); + friend void PhysicsInnerCallbackFunctions::collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); + friend void PhysicsInnerCallbackFunctions::collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); +#endif +}; + +class PhysicsContactPostSolve +{ +private: + PhysicsContactPostSolve(); + ~PhysicsContactPostSolve(); + + static PhysicsContactPostSolve* create(); + bool init(); + + friend class PhysicsWorld; + +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) + friend int PhysicsInnerCallbackFunctions::collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data); + friend int PhysicsInnerCallbackFunctions::collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); + friend void PhysicsInnerCallbackFunctions::collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); + friend void PhysicsInnerCallbackFunctions::collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); +#endif +}; + +class PhysicsContactDelegate +{ +public: + PhysicsContactDelegate(); + virtual ~PhysicsContactDelegate(); + +public: + virtual bool onContactBegin(const PhysicsContact& contact) = 0; + virtual bool onContactPreSolve(const PhysicsContact& contact, const PhysicsContactPreSolve& solve) = 0; + virtual void onContactPostSove(const PhysicsContact& contact, const PhysicsContactPostSolve& solve)= 0; + virtual void onContactEnd(const PhysicsContact& contact) = 0; +}; + +NS_CC_END +#endif //__CCPHYSICS_CONTACT_H__ + +#endif // CC_USE_PHYSICS diff --git a/cocos2dx/physics/CCPhysicsJoint.cpp b/cocos2dx/physics/CCPhysicsJoint.cpp index 39b3eedbc5..572e682fa8 100644 --- a/cocos2dx/physics/CCPhysicsJoint.cpp +++ b/cocos2dx/physics/CCPhysicsJoint.cpp @@ -21,3 +21,198 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ + +#include "CCPhysicsJoint.h" +#ifdef CC_USE_PHYSICS +#include "CCPhysicsBody.h" + +#include "chipmunk/CCPhysicsJointInfo.h" +#include "Box2D/CCPhysicsJointInfo.h" +#include "chipmunk/CCPhysicsBodyInfo.h" +#include "Box2D/CCPhysicsBodyInfo.h" +#include "chipmunk/CCPhysicsHelper.h" +#include "Box2D/CCPhysicsHelper.h" + +NS_CC_BEGIN + +PhysicsJoint::PhysicsJoint() +: _bodyA(nullptr) +, _bodyB(nullptr) +, _info(nullptr) +{ + +} + +PhysicsJoint::~PhysicsJoint() +{ + CC_SAFE_DELETE(_info); + + CC_SAFE_RELEASE(_bodyA); + CC_SAFE_RELEASE(_bodyB); +} + +bool PhysicsJoint::init(cocos2d::PhysicsBody *a, cocos2d::PhysicsBody *b) +{ + do + { + CC_BREAK_IF(a == nullptr || b == nullptr); + + CC_BREAK_IF(!(_info = new PhysicsJointInfo())); + + _bodyA = a; + _bodyA->retain(); + _bodyA->_joints.push_back(this); + _bodyB = b; + _bodyB->retain(); + _bodyB->_joints.push_back(this); + + return true; + } while (false); + + return false; +} + + +PhysicsJointPin::PhysicsJointPin() +{ + +} + +PhysicsJointPin::~PhysicsJointPin() +{ + +} + +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) +PhysicsBodyInfo* PhysicsJoint::bodyInfo(PhysicsBody* body) const +{ + return body->_info; +} + + + +PhysicsJointFixed* PhysicsJointFixed::create(PhysicsBody* a, PhysicsBody* b, const Point& anchr) +{ + PhysicsJointFixed* joint = new PhysicsJointFixed(); + + if (joint && joint->init(a, b, anchr)) + { + joint->autorelease(); + return joint; + } + + CC_SAFE_DELETE(joint); + return nullptr; +} + +bool PhysicsJointFixed::init(PhysicsBody* a, PhysicsBody* b, const Point& anchr) +{ + do + { + CC_BREAK_IF(!PhysicsJoint::init(a, b)); + + _info->joint = cpPivotJointNew(bodyInfo(a)->body, bodyInfo(b)->body, + PhysicsHelper::point2cpv(anchr)); + + return true; + } while (false); + + return false; +} + +PhysicsJointPin* PhysicsJointPin::create(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2) +{ + PhysicsJointPin* joint = new PhysicsJointPin(); + + if (joint && joint->init(a, b, anchr1, anchr2)) + { + joint->autorelease(); + return joint; + } + + CC_SAFE_DELETE(joint); + return nullptr; +} + +bool PhysicsJointPin::init(PhysicsBody *a, PhysicsBody *b, const Point& anchr1, const Point& anchr2) +{ + do + { + CC_BREAK_IF(!PhysicsJoint::init(a, b)); + + _info->joint = cpPinJointNew(bodyInfo(a)->body, bodyInfo(b)->body, PhysicsHelper::point2cpv(anchr1), PhysicsHelper::point2cpv(anchr2)); + + return true; + } while (false); + + return false; +} + +PhysicsJointSliding* PhysicsJointSliding::create(PhysicsBody* a, PhysicsBody* b, const Point& grooveA, const Point& grooveB, const Point& anchr) +{ + PhysicsJointSliding* joint = new PhysicsJointSliding(); + + if (joint && joint->init(a, b, grooveA, grooveB, anchr)) + { + return joint; + } + + CC_SAFE_DELETE(joint); + return nullptr; +} + +bool PhysicsJointSliding::init(PhysicsBody* a, PhysicsBody* b, const Point& grooveA, const Point& grooveB, const Point& anchr) +{ + do + { + CC_BREAK_IF(!PhysicsJoint::init(a, b)); + + _info->joint = cpGrooveJointNew(bodyInfo(a)->body, bodyInfo(b)->body, + PhysicsHelper::point2cpv(grooveA), + PhysicsHelper::point2cpv(grooveB), + PhysicsHelper::point2cpv(anchr)); + + return true; + } while (false); + + return false; +} + + +PhysicsJointLimit* PhysicsJointLimit::create(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2, float min, float max) +{ + PhysicsJointLimit* joint = new PhysicsJointLimit(); + + if (joint && joint->init(a, b, anchr1, anchr2, min, max)) + { + return joint; + } + + CC_SAFE_DELETE(joint); + return nullptr; +} + +bool PhysicsJointLimit::init(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2, float min, float max) +{ + do + { + CC_BREAK_IF(!PhysicsJoint::init(a, b)); + + _info->joint = cpSlideJointNew(bodyInfo(a)->body, bodyInfo(b)->body, + PhysicsHelper::point2cpv(anchr1), + PhysicsHelper::point2cpv(anchr2), + PhysicsHelper::float2cpfloat(min), + PhysicsHelper::float2cpfloat(max)); + + return true; + } while (false); + + return false; +} + +#elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) + +#endif + +NS_CC_END +#endif // CC_USE_PHYSICS diff --git a/cocos2dx/physics/CCPhysicsJoint.h b/cocos2dx/physics/CCPhysicsJoint.h index c7e659ab75..ab0315d690 100644 --- a/cocos2dx/physics/CCPhysicsJoint.h +++ b/cocos2dx/physics/CCPhysicsJoint.h @@ -34,42 +34,59 @@ NS_CC_BEGIN class PhysicsBody; +class PhysicsJointInfo; +class PhysicsBodyInfo; -class PhysicsJoint +class PhysicsJoint : public Object { protected: PhysicsJoint(); - virtual ~PhysicsJoint(); + virtual ~PhysicsJoint() = 0; + +public: + PhysicsBody* getBodyA() { return _bodyA; } + PhysicsBody* getBodyB() { return _bodyB; } -private: +protected: + bool init(PhysicsBody* a, PhysicsBody* b); + + /** + * PhysicsShape is PhysicsBody's friend class, but all the subclasses isn't. so this method is use for subclasses to catch the bodyInfo from PhysicsBody. + */ + PhysicsBodyInfo* bodyInfo(PhysicsBody* body) const; + +protected: PhysicsBody* _bodyA; PhysicsBody* _bodyB; + PhysicsJointInfo* _info; + + friend class PhysicsBody; }; class PhysicsJointFixed : public PhysicsJoint { public: - PhysicsJointFixed* create(); + PhysicsJointFixed* create(PhysicsBody* a, PhysicsBody* b, const Point& anchr); protected: - bool init(); + bool init(PhysicsBody* a, PhysicsBody* b, const Point& anchr); protected: PhysicsJointFixed(); - ~PhysicsJointFixed(); + virtual ~PhysicsJointFixed(); }; class PhysicsJointSliding : public PhysicsJoint { public: - PhysicsJointSliding* create(); + PhysicsJointSliding* create(PhysicsBody* a, PhysicsBody* b, const Point& grooveA, const Point& grooveB, const Point& anchr); protected: - bool init(); + bool init(PhysicsBody* a, PhysicsBody* b, const Point& grooveA, const Point& grooveB, const Point& anchr); protected: PhysicsJointSliding(); - ~PhysicsJointSliding(); + virtual ~PhysicsJointSliding(); }; class PhysicsJointSpring : public PhysicsJoint @@ -82,33 +99,33 @@ protected: protected: PhysicsJointSpring(); - ~PhysicsJointSpring(); + virtual ~PhysicsJointSpring(); }; class PhysicsJointLimit : public PhysicsJoint { public: - PhysicsJointLimit* create(); + PhysicsJointLimit* create(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2, float min, float max); protected: - bool init(); + bool init(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2, float min, float max); protected: PhysicsJointLimit(); - ~PhysicsJointLimit(); + virtual ~PhysicsJointLimit(); }; class PhysicsJointPin : public PhysicsJoint { public: - PhysicsJointPin* create(); + static PhysicsJointPin* create(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2); protected: - bool init(); + bool init(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2); protected: PhysicsJointPin(); - ~PhysicsJointPin(); + virtual ~PhysicsJointPin(); }; NS_CC_END diff --git a/cocos2dx/physics/CCPhysicsSetting.h b/cocos2dx/physics/CCPhysicsSetting.h index b68da7cf93..3d3aaaaf55 100644 --- a/cocos2dx/physics/CCPhysicsSetting.h +++ b/cocos2dx/physics/CCPhysicsSetting.h @@ -29,7 +29,15 @@ #define CC_PHYSICS_BOX2D 1 #define CC_PHYSICS_CHIPMUNK 2 +#define CC_USE_CHIPMUNK + +#ifdef CC_USE_BOX2D +#define CC_PHYSICS_ENGINE CC_PHYSICS_BOX2D +#elif defined(CC_USE_CHIPMUNK) #define CC_PHYSICS_ENGINE CC_PHYSICS_CHIPMUNK +#else +#define CC_PHYSICS_ENGINE CC_PHYSICS_UNKNOWN +#endif #if (CC_PHYSICS_ENGINE != CC_PHYSICS_UNKNOWN) #define CC_USE_PHYSICS diff --git a/cocos2dx/physics/CCPhysicsShape.cpp b/cocos2dx/physics/CCPhysicsShape.cpp new file mode 100644 index 0000000000..002c848eff --- /dev/null +++ b/cocos2dx/physics/CCPhysicsShape.cpp @@ -0,0 +1,426 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "CCPhysicsShape.h" +#ifdef CC_USE_PHYSICS + +#include "CCPhysicsBody.h" + +#include "chipmunk/CCPhysicsBodyInfo.h" +#include "Box2D/CCPhysicsBodyInfo.h" +#include "chipmunk/CCPhysicsShapeInfo.h" +#include "Box2D/CCPhysicsShapeInfo.h" +#include "chipmunk/CCPhysicsHelper.h" + +NS_CC_BEGIN + +PhysicsShape::PhysicsShape() +: _body(nullptr) +, _info(nullptr) +, _type(Type::UNKNOWN) +{ + +} + +PhysicsShape::~PhysicsShape() +{ + CC_SAFE_DELETE(_info); +} + +bool PhysicsShape::init(PhysicsBody* body, Type type) +{ + if (body == nullptr) return false; + + _body = body; + + _info = new PhysicsShapeInfo(this); + if (_info == nullptr) return false; + + _type = type; + + return true; +} + +void PhysicsShape::addToBody() +{ + if(_body != nullptr) _body->addShape(this); +} + +PhysicsBodyInfo* PhysicsShape::bodyInfo() const +{ + return _body->_info; +} + +PhysicsShapeCircle::PhysicsShapeCircle() +{ + +} + +PhysicsShapeCircle::~PhysicsShapeCircle() +{ + +} + +PhysicsShapeBox::PhysicsShapeBox() +{ + +} + +PhysicsShapeBox::~PhysicsShapeBox() +{ + +} + +PhysicsShapePolygon::PhysicsShapePolygon() +{ + +} + +PhysicsShapePolygon::~PhysicsShapePolygon() +{ + +} + +PhysicsShapeEdgeBox::PhysicsShapeEdgeBox() +{ + +} + +PhysicsShapeEdgeBox::~PhysicsShapeEdgeBox() +{ + +} + +PhysicsShapeEdgeChain::PhysicsShapeEdgeChain() +{ + +} + +PhysicsShapeEdgeChain::~PhysicsShapeEdgeChain() +{ + +} + +PhysicsShapeEdgePolygon::PhysicsShapeEdgePolygon() +{ + +} + +PhysicsShapeEdgePolygon::~PhysicsShapeEdgePolygon() +{ + +} + +PhysicsShapeEdgeSegment::PhysicsShapeEdgeSegment() +{ + +} + +PhysicsShapeEdgeSegment::~PhysicsShapeEdgeSegment() +{ + +} + +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) + +// PhysicsShapeCircle +PhysicsShapeCircle* PhysicsShapeCircle::create(PhysicsBody* body, float radius, Point offset/* = Point(0, 0)*/) +{ + PhysicsShapeCircle* shape = new PhysicsShapeCircle(); + if (shape && shape->init(body, radius, offset)) + { + return shape; + } + + CC_SAFE_DELETE(shape); + return nullptr; +} + +bool PhysicsShapeCircle::init(PhysicsBody* body, float radius, Point offset /*= Point(0, 0)*/) +{ + do + { + CC_BREAK_IF(!PhysicsShape::init(body, Type::CIRCLE)); + + cpShape* shape = cpCircleShapeNew(bodyInfo()->body, radius, PhysicsHelper::point2cpv(offset)); + + CC_BREAK_IF(shape == nullptr); + + _info->add(shape); + addToBody(); + + return true; + } while (false); + + return false; +} + +// PhysicsShapeEdgeSegment +PhysicsShapeEdgeSegment* PhysicsShapeEdgeSegment::create(PhysicsBody* body, Point a, Point b, float border/* = 1*/) +{ + PhysicsShapeEdgeSegment* shape = new PhysicsShapeEdgeSegment(); + if (shape && shape->init(body, a, b, border)) + { + return shape; + } + + CC_SAFE_DELETE(shape); + return nullptr; +} + +bool PhysicsShapeEdgeSegment::init(PhysicsBody* body, Point a, Point b, float border/* = 1*/) +{ + do + { + CC_BREAK_IF(!PhysicsShape::init(body, Type::EDGESEGMENT)); + + cpShape* shape = cpSegmentShapeNew(bodyInfo()->body, + PhysicsHelper::point2cpv(a), + PhysicsHelper::point2cpv(b), + PhysicsHelper::float2cpfloat(border)); + + CC_BREAK_IF(shape == nullptr); + + _info->add(shape); + addToBody(); + + return true; + } while (false); + + return false; +} + +// PhysicsShapeBox +PhysicsShapeBox* PhysicsShapeBox::create(PhysicsBody* body, Size size, Point offset/* = Point(0, 0)*/) +{ + PhysicsShapeBox* shape = new PhysicsShapeBox(); + if (shape && shape->init(body, size, offset)) + { + return shape; + } + + CC_SAFE_DELETE(shape); + return nullptr; +} + +bool PhysicsShapeBox::init(PhysicsBody* body, Size size, Point offset /*= Point(0, 0)*/) +{ + do + { + CC_BREAK_IF(!PhysicsShape::init(body, Type::BOX)); + + cpShape* shape = cpBoxShapeNew(bodyInfo()->body, PhysicsHelper::float2cpfloat(size.width), PhysicsHelper::float2cpfloat(size.height)); + + CC_BREAK_IF(shape == nullptr); + + _info->add(shape); + addToBody(); + + return true; + } while (false); + + return false; +} + +// PhysicsShapeCircle +PhysicsShapePolygon* PhysicsShapePolygon::create(PhysicsBody* body, Point* points, int count, Point offset/* = Point(0, 0)*/) +{ + PhysicsShapePolygon* shape = new PhysicsShapePolygon(); + if (shape && shape->init(body, points, count, offset)) + { + return shape; + } + + CC_SAFE_DELETE(shape); + return nullptr; +} + +bool PhysicsShapePolygon::init(PhysicsBody* body, Point* points, int count, Point offset /*= Point(0, 0)*/) +{ + do + { + CC_BREAK_IF(!PhysicsShape::init(body, Type::POLYGEN)); + + cpVect* vecs = new cpVect[count]; + PhysicsHelper::points2cpvs(points, vecs, count); + cpShape* shape = cpPolyShapeNew(bodyInfo()->body, count, vecs, PhysicsHelper::point2cpv(offset)); + + CC_BREAK_IF(shape == nullptr); + + _info->add(shape); + addToBody(); + + return true; + } while (false); + + return false; +} + +// PhysicsShapeEdgeBox +PhysicsShapeEdgeBox* PhysicsShapeEdgeBox::create(PhysicsBody* body, Size size, float border/* = 1*/, Point offset/* = Point(0, 0)*/) +{ + PhysicsShapeEdgeBox* shape = new PhysicsShapeEdgeBox(); + if (shape && shape->init(body, size, border, offset)) + { + return shape; + } + + CC_SAFE_DELETE(shape); + return nullptr; +} + +bool PhysicsShapeEdgeBox::init(PhysicsBody* body, Size size, float border/* = 1*/, Point offset/*= Point(0, 0)*/) +{ + do + { + CC_BREAK_IF(!PhysicsShape::init(body, Type::EDGEBOX)); + + Point bodyPos = body->getPosition(); + + cpVect vec[4] = {}; + vec[0] = PhysicsHelper::point2cpv(Point(bodyPos.x-size.width/2+offset.x, bodyPos.y-size.height/2+offset.y)); + vec[1] = PhysicsHelper::point2cpv(Point(bodyPos.x+size.width/2+offset.x, bodyPos.y-size.height/2+offset.y)); + vec[2] = PhysicsHelper::point2cpv(Point(bodyPos.x+size.width/2+offset.x, bodyPos.y+size.height/2+offset.y)); + vec[3] = PhysicsHelper::point2cpv(Point(bodyPos.x-size.width/2+offset.x, bodyPos.y+size.height/2+offset.y)); + + int i = 0; + for (; i < 4; ++i) + { + cpShape* shape = cpSegmentShapeNew(bodyInfo()->body, vec[i], vec[(i+1)%4], + PhysicsHelper::float2cpfloat(border)); + CC_BREAK_IF(shape == nullptr); + cpShapeSetElasticity(shape, 1.0f); + cpShapeSetFriction(shape, 1.0f); + _info->add(shape); + } + CC_BREAK_IF(i < 4); + + addToBody(); + + return true; + } while (false); + + return false; +} + +// PhysicsShapeEdgeBox +PhysicsShapeEdgePolygon* PhysicsShapeEdgePolygon::create(PhysicsBody* body, Point* points, int count, float border/* = 1*/) +{ + PhysicsShapeEdgePolygon* shape = new PhysicsShapeEdgePolygon(); + if (shape && shape->init(body, points, count, border)) + { + return shape; + } + + CC_SAFE_DELETE(shape); + return nullptr; +} + +bool PhysicsShapeEdgePolygon::init(PhysicsBody* body, Point* points, int count, float border/* = 1*/) +{ + cpVect* vec = nullptr; + do + { + CC_BREAK_IF(!PhysicsShape::init(body, Type::EDGEPOLYGEN)); + + vec = new cpVect[count]; + PhysicsHelper::points2cpvs(points, vec, count); + + int i = 0; + for (; i < count; ++i) + { + cpShape* shape = cpSegmentShapeNew(bodyInfo()->body, vec[i], vec[(i+1)%count], + PhysicsHelper::float2cpfloat(border)); + CC_BREAK_IF(shape == nullptr); + cpShapeSetElasticity(shape, 1.0f); + cpShapeSetFriction(shape, 1.0f); + _info->add(shape); + } + CC_BREAK_IF(i < count); + + addToBody(); + + if (vec != nullptr) delete[] vec; + return true; + } while (false); + + if (vec != nullptr) delete[] vec; + + return false; +} + +// PhysicsShapeEdgeChain +PhysicsShapeEdgeChain* PhysicsShapeEdgeChain::create(PhysicsBody* body, Point* points, int count, float border/* = 1*/) +{ + PhysicsShapeEdgeChain* shape = new PhysicsShapeEdgeChain(); + if (shape && shape->init(body, points, count, border)) + { + return shape; + } + + CC_SAFE_DELETE(shape); + return nullptr; +} + +bool PhysicsShapeEdgeChain::init(PhysicsBody* body, Point* points, int count, float border/* = 1*/) +{ + cpVect* vec = nullptr; + do + { + CC_BREAK_IF(!PhysicsShape::init(body, Type::EDGECHAIN)); + + vec = new cpVect[count]; + PhysicsHelper::points2cpvs(points, vec, count); + + int i = 0; + for (; i < count - 1; ++i) + { + cpShape* shape = cpSegmentShapeNew(bodyInfo()->body, vec[i], vec[i+1], + PhysicsHelper::float2cpfloat(border)); + CC_BREAK_IF(shape == nullptr); + cpShapeSetElasticity(shape, 1.0f); + cpShapeSetFriction(shape, 1.0f); + _info->add(shape); + } + CC_BREAK_IF(i < count); + + addToBody(); + + if (vec != nullptr) delete[] vec; + return true; + } while (false); + + if (vec != nullptr) delete[] vec; + + return false; +} + + +#elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) + +#endif + +NS_CC_END + +#endif // CC_USE_PHYSICS \ No newline at end of file diff --git a/cocos2dx/physics/CCPhysicsShape.h b/cocos2dx/physics/CCPhysicsShape.h new file mode 100644 index 0000000000..64049c2947 --- /dev/null +++ b/cocos2dx/physics/CCPhysicsShape.h @@ -0,0 +1,182 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "CCPhysicsSetting.h" +#ifdef CC_USE_PHYSICS + +#ifndef __CCPHYSICS_SHAPE_H__ +#define __CCPHYSICS_SHAPE_H__ + +#include "cocoa/CCObject.h" +#include "cocoa/CCGeometry.h" + +NS_CC_BEGIN + +class PhysicsShapeInfo; +class PhysicsBody; +class PhysicsBodyInfo; + +class PhysicsShape : public Object +{ +public: + enum class Type + { + UNKNOWN, + CIRCLE, + BOX, + POLYGEN, + EDGESEGMENT, + EDGEBOX, + EDGEPOLYGEN, + EDGECHAIN, + }; + +public: + inline PhysicsBody* getBody(){ return _body; } + inline Type getType() { return _type; } + +protected: + bool init(PhysicsBody* body, Type type); + + /** + * PhysicsShape is PhysicsBody's friend class, but all the subclasses isn't. so this method is use for subclasses to catch the bodyInfo from PhysicsBody. + */ + PhysicsBodyInfo* bodyInfo() const; + + void addToBody(); + +protected: + PhysicsShape(); + virtual ~PhysicsShape(); + +protected: + PhysicsBody* _body; + PhysicsShapeInfo* _info; + Type _type; + + friend class PhysicsWorld; + friend class PhysicsBody; +}; + +class PhysicsShapeCircle : public PhysicsShape +{ +protected: + static PhysicsShapeCircle* create(PhysicsBody* body, float radius, Point offset = Point(0, 0)); + bool init(PhysicsBody* body, float radius, Point offset = Point(0, 0)); + +protected: + PhysicsShapeCircle(); + virtual ~PhysicsShapeCircle(); + + friend class PhysicsBody; +}; + +class PhysicsShapeBox : public PhysicsShape +{ +protected: + static PhysicsShapeBox* create(PhysicsBody* body, Size size, Point offset = Point(0, 0)); + bool init(PhysicsBody* body, Size size, Point offset = Point(0, 0)); + +protected: + PhysicsShapeBox(); + virtual ~PhysicsShapeBox(); + + friend class PhysicsBody; +}; + +class PhysicsShapePolygon : public PhysicsShape +{ +protected: + static PhysicsShapePolygon* create(PhysicsBody* body, Point* points, int count, Point offset = Point(0, 0)); + bool init(PhysicsBody* body, Point* points, int count, Point offset = Point(0, 0)); + +protected: + PhysicsShapePolygon(); + virtual ~PhysicsShapePolygon(); + + friend class PhysicsBody; +}; + +class PhysicsShapeEdgeSegment : public PhysicsShape +{ +protected: + static PhysicsShapeEdgeSegment* create(PhysicsBody* body, Point a, Point b, float border = 1); + bool init(PhysicsBody* body, Point a, Point b, float border = 1); + +protected: + PhysicsShapeEdgeSegment(); + virtual ~PhysicsShapeEdgeSegment(); + + friend class PhysicsBody; +}; + +class PhysicsShapeEdgeBox : public PhysicsShape +{ +public: + static PhysicsShapeEdgeBox* create(PhysicsBody* body, Size size, float border = 0, Point offset = Point(0, 0)); + +protected: + bool init(PhysicsBody* body, Size size, float border = 1, Point offset = Point(0, 0)); + +protected: + PhysicsShapeEdgeBox(); + virtual ~PhysicsShapeEdgeBox(); + + friend class PhysicsBody; +}; + +class PhysicsShapeEdgePolygon : public PhysicsShape +{ +public: + static PhysicsShapeEdgePolygon* create(PhysicsBody* body, Point* points, int count, float border = 1); + +protected: + bool init(PhysicsBody* body, Point* points, int count, float border = 1); + +protected: + PhysicsShapeEdgePolygon(); + virtual ~PhysicsShapeEdgePolygon(); + + friend class PhysicsBody; +}; + +class PhysicsShapeEdgeChain : public PhysicsShape +{ +public: + static PhysicsShapeEdgeChain* create(PhysicsBody* body, Point* points, int count, float border = 1); + +protected: + bool init(PhysicsBody* body, Point* points, int count, float border = 1); + +protected: + PhysicsShapeEdgeChain(); + virtual ~PhysicsShapeEdgeChain(); + + friend class PhysicsBody; +}; + +NS_CC_END +#endif // __CCPHYSICS_FIXTURE_H__ + +#endif // CC_USE_PHYSICS diff --git a/cocos2dx/physics/CCPhysicsWorld.cpp b/cocos2dx/physics/CCPhysicsWorld.cpp index ccfc1ea788..78a6f3b806 100644 --- a/cocos2dx/physics/CCPhysicsWorld.cpp +++ b/cocos2dx/physics/CCPhysicsWorld.cpp @@ -23,51 +23,265 @@ ****************************************************************************/ #include "CCPhysicsWorld.h" +#ifdef CC_USE_PHYSICS + +#include "CCPhysicsBody.h" +#include "CCPhysicsShape.h" +#include "CCPhysicsContact.h" + +#include "chipmunk/CCPhysicsWorldInfo.h" +#include "Box2D/CCPhysicsWorldInfo.h" +#include "chipmunk/CCPhysicsBodyInfo.h" +#include "Box2D/CCPhysicsBodyInfo.h" +#include "chipmunk/CCPhysicsShapeInfo.h" +#include "Box2D/CCPhysicsShapeInfo.h" +#include "chipmunk/CCPhysicsContactInfo.h" +#include "Box2D/CCPhysicsContactInfo.h" +#include "chipmunk/CCPhysicsHelper.h" + +#include "draw_nodes/CCDrawNode.h" +#include "cocoa/CCArray.h" +#include "layers_scenes_transitions_nodes/CCScene.h" +#include "CCDirector.h" NS_CC_BEGIN #if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) -class PhysicsWorldInfo +namespace PhysicsInnerCallbackFunctions { -public: - cpSpace* space; + int collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data) + { + PhysicsWorld* world = static_cast(data); + + CP_ARBITER_GET_SHAPES(arb, a, b); + + auto ita = PhysicsShapeInfo::map.find(a); + auto itb = PhysicsShapeInfo::map.find(b); + CC_ASSERT(ita != PhysicsShapeInfo::map.end() && itb != PhysicsShapeInfo::map.end()); + + PhysicsContact* contact = PhysicsContact::create(ita->second->shape, itb->second->shape); + arb->data = contact; + + return world->collisionBeginCallback(*static_cast(arb->data)); + } -public: - PhysicsWorldInfo(); - ~PhysicsWorldInfo(); -}; - -PhysicsWorldInfo::PhysicsWorldInfo() -{ - space = cpSpaceNew(); -} - -PhysicsWorldInfo::~PhysicsWorldInfo() -{ - cpSpaceFree(space); + int collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data) + { + PhysicsWorld* world = static_cast(data); + return world->collisionPreSolveCallback(*static_cast(arb->data), + PhysicsContactPreSolve()); + } + + void collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data) + { + PhysicsWorld* world = static_cast(data); + world->collisionPostSolveCallback(*static_cast(arb->data), + PhysicsContactPostSolve()); + } + + void collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data) + { + PhysicsWorld* world = static_cast(data); + PhysicsContact* contact = static_cast(arb->data); + + world->collisionSeparateCallback(*contact); + + delete contact; + } } bool PhysicsWorld::init() { - _worldInfo = new PhysicsWorldInfo(); + _info = new PhysicsWorldInfo(); - cpSpaceSetGravity(_worldInfo->space, cpv(_gravity.x, _gravity.y)); + cpSpaceSetGravity(_info->space, PhysicsHelper::point2cpv(_gravity)); + + cpSpaceSetDefaultCollisionHandler(_info->space, + PhysicsInnerCallbackFunctions::collisionBeginCallbackFunc, + PhysicsInnerCallbackFunctions::collisionPreSolveCallbackFunc, + PhysicsInnerCallbackFunctions::collisionPostSolveCallbackFunc, + PhysicsInnerCallbackFunctions::collisionSeparateCallbackFunc, + this); return true; } +void PhysicsWorld::addShape(PhysicsShape* shape) +{ + for (auto it = shape->_info->shapes.begin(); it != shape->_info->shapes.end(); it++) + { + if (cpBodyIsStatic(shape->getBody()->_info->body)) + { + cpSpaceAddStaticShape(_info->space, *it); + }else + { + cpSpaceAddShape(_info->space, *it); + } + } +} + void PhysicsWorld::addChild(PhysicsBody* body) { + auto shapes = body->getShapes(); + // add body to space + if (body->isDynamic()) + { + cpSpaceAddBody(_info->space, body->_info->body); + } + + // add shapes to space + for (auto it = shapes.begin(); it != shapes.end(); it++) + { + addShape(*it); + } + + if (_bodys == nullptr) + { + _bodys = Array::create(body, NULL); + _bodys->retain(); + }else + { + _bodys->addObject(body); + } +} + +void PhysicsWorld::update(float delta) +{ + cpSpaceStep(_info->space, delta); + + if (_drawNode) + { + _drawNode->removeFromParent(); + _drawNode = nullptr; + } + + if (_debugDraw) + { + debugDraw(); + } +} + +void PhysicsWorld::debugDraw() +{ + if (_debugDraw && _bodys != nullptr) + { + _drawNode= DrawNode::create(); + + Object* child = nullptr; + CCARRAY_FOREACH(_bodys, child) + { + PhysicsBody* body = dynamic_cast(child); + + std::vector shapes = body->getShapes(); + + for (auto it = shapes.begin(); it != shapes.end(); ++it) + { + drawWithShape(_drawNode, *it); + } + } + + if (_scene) + { + _scene->addChild(_drawNode); + } + } +} + +void PhysicsWorld::setScene(Scene *scene) +{ + _scene = scene; + scene->retain(); +} + +void PhysicsWorld::drawWithShape(DrawNode* node, PhysicsShape* shape) +{ + for (auto it = shape->_info->shapes.begin(); it != shape->_info->shapes.end(); ++it) + { + cpShape *shape = *it; + + switch ((*it)->klass_private->type) + { + case CP_CIRCLE_SHAPE: + { + float radius = PhysicsHelper::cpfloat2float(cpCircleShapeGetRadius(shape)); + Point centre = PhysicsHelper::cpv2point(cpBodyGetPos(cpShapeGetBody(shape))) + + PhysicsHelper::cpv2point(cpCircleShapeGetOffset(shape)); + + Point seg[4] = {}; + seg[0] = Point(centre.x - radius, centre.y - radius); + seg[1] = Point(centre.x - radius, centre.y + radius); + seg[2] = Point(centre.x + radius, centre.y + radius); + seg[3] = Point(centre.x + radius, centre.y - radius); + node->drawPolygon(seg, 4, Color4F(), 1, Color4F(1, 0, 0, 1)); + break; + } + case CP_SEGMENT_SHAPE: + { + cpSegmentShape *seg = (cpSegmentShape *)shape; + node->drawSegment(PhysicsHelper::cpv2point(seg->ta), + PhysicsHelper::cpv2point(seg->tb), + PhysicsHelper::cpfloat2float(seg->r==0 ? 1 : seg->r), Color4F(1, 0, 0, 1)); + break; + } + case CP_POLY_SHAPE: + { + cpPolyShape* poly = (cpPolyShape*)shape; + int num = poly->numVerts; + Point* seg = new Point[num]; + + PhysicsHelper::cpvs2points(poly->tVerts, seg, num); + + node->drawPolygon(seg, num, Color4F(1, 0, 0, 0.3), 1, Color4F(1, 0, 0, 1)); + + delete[] seg; + break; + } + default: + break; + } + } +} + +int PhysicsWorld::collisionBeginCallback(const PhysicsContact& contact) +{ + if (_delegate) + { + return _delegate->onContactBegin(contact); + } + + return true; +} + +int PhysicsWorld::collisionPreSolveCallback(const PhysicsContact& contact, const PhysicsContactPreSolve& solve) +{ + if (_delegate) + { + return _delegate->onContactPreSolve(contact, solve); + } + + return true; +} + +void PhysicsWorld::collisionPostSolveCallback(const PhysicsContact& contact, const PhysicsContactPostSolve& solve) +{ + if (_delegate) + { + _delegate->onContactPostSove(contact, solve); + } +} + +void PhysicsWorld::collisionSeparateCallback(const PhysicsContact& contact) +{ + if (_delegate) + { + _delegate->onContactEnd(contact); + } } #elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) -struct PhysicsInfo -{ - -}; #endif PhysicsWorld* PhysicsWorld::create() @@ -83,16 +297,25 @@ PhysicsWorld* PhysicsWorld::create() } PhysicsWorld::PhysicsWorld() -: _gravity(Point(0, -9.8)) -, _speed(1.0) -, _worldInfo(nullptr) +: _gravity(Point(0.0f, -98.0f)) +, _speed(1.0f) +, _info(nullptr) +, _delegate(nullptr) +, _bodys(nullptr) +, _scene(nullptr) +, _debugDraw(false) +, _drawNode(nullptr) { } PhysicsWorld::~PhysicsWorld() { - CC_SAFE_DELETE(_worldInfo); + CC_SAFE_DELETE(_info); + CC_SAFE_RELEASE(_bodys); + CC_SAFE_RELEASE(_scene); } NS_CC_END + +#endif // CC_USE_PHYSICS diff --git a/cocos2dx/physics/CCPhysicsWorld.h b/cocos2dx/physics/CCPhysicsWorld.h index 5bf807c159..1cb2530351 100644 --- a/cocos2dx/physics/CCPhysicsWorld.h +++ b/cocos2dx/physics/CCPhysicsWorld.h @@ -36,10 +36,26 @@ NS_CC_BEGIN class PhysicsBody; class PhysicsJoint; class PhysicsWorldInfo; +class PhysicsShape; +class PhysicsContact; +class PhysicsContactPreSolve; +class PhysicsContactPostSolve; class PhysicsContactDelegate; +class Array; class Sprite; class Scene; +class DrawNode; + +namespace PhysicsInnerCallbackFunctions +{ +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) + int collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data); + int collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); + void collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); + void collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); +#endif +} class PhysicsWorld { @@ -53,21 +69,46 @@ public: Array* getBodysInRect(Rect rect) const; Array* getAllBody() const; - void registerContactDelegate(PhysicsContactDelegate* delegate); + inline void registerContactDelegate(PhysicsContactDelegate* delegate) { _delegate = delegate; } + inline void unregisterContactDelegate() { _delegate = nullptr; } inline Point getGravity() { return _gravity; } inline void setGravity(Point gravity) { _gravity = gravity; } + inline bool getDebugDraw() { return _debugDraw; } + inline void setDebugDraw(bool debugDraw) { _debugDraw = debugDraw; } + protected: static PhysicsWorld* create(); bool init(); + + void setScene(Scene* scene); + virtual void addChild(PhysicsBody* body); + virtual void addShape(PhysicsShape* shape); + virtual void update(float delta); + + virtual void debugDraw(); + virtual void drawWithShape(DrawNode* node, PhysicsShape* shape); + + + virtual int collisionBeginCallback(const PhysicsContact& contact); + virtual int collisionPreSolveCallback(const PhysicsContact& contact, const PhysicsContactPreSolve& solve); + virtual void collisionPostSolveCallback(const PhysicsContact& contact, const PhysicsContactPostSolve& solve); + virtual void collisionSeparateCallback(const PhysicsContact& contact); protected: Point _gravity; float _speed; + PhysicsWorldInfo* _info; + PhysicsContactDelegate* _delegate; - PhysicsWorldInfo* _worldInfo; + + Array* _bodys; + Scene* _scene; + + bool _debugDraw; + DrawNode* _drawNode; protected: PhysicsWorld(); @@ -75,6 +116,14 @@ protected: friend class Sprite; friend class Scene; + friend class PhysicsBody; + +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) + friend int PhysicsInnerCallbackFunctions::collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data); + friend int PhysicsInnerCallbackFunctions::collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); + friend void PhysicsInnerCallbackFunctions::collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); + friend void PhysicsInnerCallbackFunctions::collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); +#endif }; NS_CC_END diff --git a/cocos2dx/physics/chipmunk/CCPhysicsBodyInfo.cpp b/cocos2dx/physics/chipmunk/CCPhysicsBodyInfo.cpp new file mode 100644 index 0000000000..4627c02499 --- /dev/null +++ b/cocos2dx/physics/chipmunk/CCPhysicsBodyInfo.cpp @@ -0,0 +1,45 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "CCPhysicsBodyInfo.h" +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) +NS_CC_BEGIN + +PhysicsBodyInfo::PhysicsBodyInfo() +: body(nullptr) +{ +} + +PhysicsBodyInfo::~PhysicsBodyInfo() +{ + if (body) cpBodyFree(body); +} + +Clonable* PhysicsBodyInfo::clone() const +{ + +} + +NS_CC_END +#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK diff --git a/cocos2dx/physics/CCPhysicsContactDelegate.h b/cocos2dx/physics/chipmunk/CCPhysicsBodyInfo.h similarity index 69% rename from cocos2dx/physics/CCPhysicsContactDelegate.h rename to cocos2dx/physics/chipmunk/CCPhysicsBodyInfo.h index a371b6e044..796f384584 100644 --- a/cocos2dx/physics/CCPhysicsContactDelegate.h +++ b/cocos2dx/physics/chipmunk/CCPhysicsBodyInfo.h @@ -22,31 +22,31 @@ THE SOFTWARE. ****************************************************************************/ -#include "CCPhysicsSetting.h" -#ifdef CC_USE_PHYSICS - -#ifndef __CCPHYSICS_CONTACTDELEGATE_H__ -#define __CCPHYSICS_CONTACTDELEGATE_H__ +#include "../CCPhysicsSetting.h" +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) +#ifndef __CCPHYSICS_BODY_INFO_H__ +#define __CCPHYSICS_BODY_INFO_H__ +#include "platform/CCPlatformMacros.h" #include "cocoa/CCObject.h" -#include "cocoa/CCGeometry.h" NS_CC_BEGIN -class PhysicsBody; - -class PhysicsContactDelegate +class PhysicsBodyInfo : public Clonable { public: - PhysicsContactDelegate(); - virtual ~PhysicsContactDelegate(); + cpBody* body; -public: - virtual void onContactBegin(PhysicsBody* bodyA, PhysicsBody* bodyB, float collisionImpulse, Point contactPoint) = 0; - virtual void onContactEnd(PhysicsBody* bodyA, PhysicsBody* bodyB, float collisionImpulse, Point contactPoint) = 0; +private: + PhysicsBodyInfo(); + ~PhysicsBodyInfo(); + + Clonable* clone() const override; + + friend class PhysicsBody; }; NS_CC_END -#endif //__CCPHYSICS_CONTACTDELEGATE_H__ +#endif // __CCPHYSICS_BODY_INFO_H__ -#endif // CC_USE_PHYSICS +#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK diff --git a/cocos2dx/physics/chipmunk/CCPhysicsContactInfo.cpp b/cocos2dx/physics/chipmunk/CCPhysicsContactInfo.cpp new file mode 100644 index 0000000000..53b4f29b14 --- /dev/null +++ b/cocos2dx/physics/chipmunk/CCPhysicsContactInfo.cpp @@ -0,0 +1,39 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "CCPhysicsContactInfo.h" +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) +NS_CC_BEGIN + +PhysicsContactInfo::PhysicsContactInfo(PhysicsContact* contact) +: contact(contact) +{ +} + +PhysicsContactInfo::~PhysicsContactInfo() +{ +} + +NS_CC_END +#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK diff --git a/cocos2dx/physics/chipmunk/CCPhysicsContactInfo.h b/cocos2dx/physics/chipmunk/CCPhysicsContactInfo.h new file mode 100644 index 0000000000..b0d123ae21 --- /dev/null +++ b/cocos2dx/physics/chipmunk/CCPhysicsContactInfo.h @@ -0,0 +1,49 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "../CCPhysicsSetting.h" +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) + +#ifndef __CCPHYSICS_CONTACT_INFO_H__ +#define __CCPHYSICS_CONTACT_INFO_H__ +#include "platform/CCPlatformMacros.h" +NS_CC_BEGIN + +class PhysicsContact; +class PhysicsContactInfo +{ +public: + PhysicsContact* contact; + +private: + PhysicsContactInfo(PhysicsContact* contact); + ~PhysicsContactInfo(); + + friend class PhysicsContact; +}; + +NS_CC_END +#endif // __CCPHYSICS_WORLD_INFO_H__ + +#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK diff --git a/cocos2dx/physics/chipmunk/CCPhysicsHelper.h b/cocos2dx/physics/chipmunk/CCPhysicsHelper.h new file mode 100644 index 0000000000..07508e9b13 --- /dev/null +++ b/cocos2dx/physics/chipmunk/CCPhysicsHelper.h @@ -0,0 +1,68 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "../CCPhysicsSetting.h" +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) + +#ifndef __CCPHYSICS_HELPER_H__ +#define __CCPHYSICS_HELPER_H__ + +#include "platform/CCPlatformMacros.h" +#include "cocoa/CCGeometry.h" + +NS_CC_BEGIN + +class PhysicsHelper +{ +public: + static Point cpv2point(const cpVect& vec) { return Point(vec.x, vec.y); } + static cpVect point2cpv(const Point& point) { return cpv(point.x, point.y); } + static Size cpv2size(const cpVect& vec) { return Size(vec.x, vec.y); } + static cpVect size2cpv(const Size& size) { return cpv(size.width, size.height); } + static float cpfloat2float(cpFloat f) { return f; } + static cpFloat float2cpfloat(float f) { return f; } + + static void cpvs2points(const cpVect* cpvs, Point* points, int count) + { + for (int i = 0; i < count; ++i) + { + points[i] = cpv2point(cpvs[i]); + } + } + + static cpVect* points2cpvs(const Point* points, cpVect* cpvs, int count) + { + for (int i = 0; i < count; ++i) + { + cpvs[i] = point2cpv(points[i]); + } + + return cpvs; + } +}; + +NS_CC_END +#endif // __CCPHYSICS_HELPER_H__ + +#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK diff --git a/cocos2dx/physics/chipmunk/CCPhysicsJointInfo.cpp b/cocos2dx/physics/chipmunk/CCPhysicsJointInfo.cpp new file mode 100644 index 0000000000..aca28aa0fb --- /dev/null +++ b/cocos2dx/physics/chipmunk/CCPhysicsJointInfo.cpp @@ -0,0 +1,43 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "CCPhysicsJointInfo.h" +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) +NS_CC_BEGIN + +PhysicsJointInfo::PhysicsJointInfo() +: joint(nullptr) +{ +} + +PhysicsJointInfo::~PhysicsJointInfo() +{ + if (joint) + { + cpConstraintFree(joint); + } +} + +NS_CC_END +#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK diff --git a/cocos2dx/physics/chipmunk/CCPhysicsJointInfo.h b/cocos2dx/physics/chipmunk/CCPhysicsJointInfo.h new file mode 100644 index 0000000000..4174c866f6 --- /dev/null +++ b/cocos2dx/physics/chipmunk/CCPhysicsJointInfo.h @@ -0,0 +1,50 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "../CCPhysicsSetting.h" +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) + +#ifndef __CCPHYSICS_JOINT_INFO_H__ +#define __CCPHYSICS_JOINT_INFO_H__ + +#include "platform/CCPlatformMacros.h" + +NS_CC_BEGIN + +class PhysicsJointInfo +{ +public: + cpConstraint* joint; + +private: + PhysicsJointInfo(); + ~PhysicsJointInfo(); + + friend class PhysicsJoint; +}; + +NS_CC_END +#endif // __CCPHYSICS_SHAPE_INFO_H__ + +#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK diff --git a/cocos2dx/physics/chipmunk/CCPhysicsShapeInfo.cpp b/cocos2dx/physics/chipmunk/CCPhysicsShapeInfo.cpp new file mode 100644 index 0000000000..f22d7a3267 --- /dev/null +++ b/cocos2dx/physics/chipmunk/CCPhysicsShapeInfo.cpp @@ -0,0 +1,70 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "CCPhysicsShapeInfo.h" +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) +NS_CC_BEGIN + +std::map PhysicsShapeInfo::map; + +PhysicsShapeInfo::PhysicsShapeInfo(PhysicsShape* shape) +: shape(shape) +{ +} + +PhysicsShapeInfo::~PhysicsShapeInfo() +{ + for (auto it = shapes.begin(); it != shapes.end(); it++) + { + cpShapeFree(*it); + + auto mit = map.find(*it); + if (mit != map.end()) map.erase(*it); + } +} + +void PhysicsShapeInfo::add(cpShape* shape) +{ + if (shape == nullptr) return; + + shapes.push_back(shape); + map.insert(std::pair(shape, this)); +} + +void PhysicsShapeInfo::remove(cpShape* shape) +{ + if (shape == nullptr) return; + + auto it = find(shapes.begin(), shapes.end(), shape); + if (it != shapes.end()) + { + shapes.erase(it); + + auto mit = map.find(shape); + if (mit != map.end()) map.erase(mit); + } +} + +NS_CC_END +#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK diff --git a/cocos2dx/physics/chipmunk/CCPhysicsShapeInfo.h b/cocos2dx/physics/chipmunk/CCPhysicsShapeInfo.h new file mode 100644 index 0000000000..7114bd3961 --- /dev/null +++ b/cocos2dx/physics/chipmunk/CCPhysicsShapeInfo.h @@ -0,0 +1,61 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "../CCPhysicsSetting.h" +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) + +#ifndef __CCPHYSICS_SHAPE_INFO_H__ +#define __CCPHYSICS_SHAPE_INFO_H__ +#include + +#include "platform/CCPlatformMacros.h" +#include + +NS_CC_BEGIN + +class PhysicsShape; + +class PhysicsShapeInfo +{ +public: + void add(cpShape* shape); + void remove(cpShape* shape); + void removeall(); + +public: + std::vector shapes; + static std::map map; + PhysicsShape* shape; + +private: + PhysicsShapeInfo(PhysicsShape* shape); + ~PhysicsShapeInfo(); + + friend class PhysicsShape; +}; + +NS_CC_END +#endif // __CCPHYSICS_SHAPE_INFO_H__ + +#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK diff --git a/cocos2dx/physics/chipmunk/CCPhysicsWorldInfo.cpp b/cocos2dx/physics/chipmunk/CCPhysicsWorldInfo.cpp new file mode 100644 index 0000000000..57f601ead7 --- /dev/null +++ b/cocos2dx/physics/chipmunk/CCPhysicsWorldInfo.cpp @@ -0,0 +1,40 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "CCPhysicsWorldInfo.h" +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) +NS_CC_BEGIN + +PhysicsWorldInfo::PhysicsWorldInfo() +{ + space = cpSpaceNew(); +} + +PhysicsWorldInfo::~PhysicsWorldInfo() +{ + cpSpaceFree(space); +} + +NS_CC_END +#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK diff --git a/cocos2dx/physics/chipmunk/CCPhysicsWorldInfo.h b/cocos2dx/physics/chipmunk/CCPhysicsWorldInfo.h new file mode 100644 index 0000000000..a1f1e66342 --- /dev/null +++ b/cocos2dx/physics/chipmunk/CCPhysicsWorldInfo.h @@ -0,0 +1,48 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + 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 "../CCPhysicsSetting.h" +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) + +#ifndef __CCPHYSICS_WORLD_INFO_H__ +#define __CCPHYSICS_WORLD_INFO_H__ +#include "platform/CCPlatformMacros.h" +NS_CC_BEGIN + +class PhysicsWorldInfo +{ +public: + cpSpace* space; + +private: + PhysicsWorldInfo(); + ~PhysicsWorldInfo(); + + friend class PhysicsWorld; +}; + +NS_CC_END +#endif // __CCPHYSICS_WORLD_INFO_H__ + +#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK diff --git a/cocos2dx/physics/chipmunk/ChipmunkDebugDraw.c b/cocos2dx/physics/chipmunk/ChipmunkDebugDraw.c new file mode 100644 index 0000000000..38b06af1a0 --- /dev/null +++ b/cocos2dx/physics/chipmunk/ChipmunkDebugDraw.c @@ -0,0 +1,446 @@ +/* Copyright (c) 2007 Scott Lembcke + * + * 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 +#include + +#ifdef __APPLE__ + #include "OpenGL/gl.h" + #include "OpenGL/glu.h" + #include +#else + #ifdef WIN32 + #include + #endif + + #include + #include + #include +#endif + +#include "chipmunk_private.h" +#include "ChipmunkDebugDraw.h" + +/* + IMPORTANT - READ ME! + + This file sets up a simple interface that the individual demos can use to get + a Chipmunk space running and draw what's in it. In order to keep the Chipmunk + examples clean and simple, they contain no graphics code. All drawing is done + by accessing the Chipmunk structures at a very low level. It is NOT + recommended to write a game or application this way as it does not scale + beyond simple shape drawing and is very dependent on implementation details + about Chipmunk which may change with little to no warning. +*/ + +const Color LINE_COLOR = {200.0/255.0, 210.0/255.0, 230.0/255.0, 1.0}; +const Color CONSTRAINT_COLOR = {0.0, 0.75, 0.0, 1.0}; +const float SHAPE_ALPHA = 1.0; + +float ChipmunkDebugDrawPointLineScale = 1.0; + +static Color +ColorFromHash(cpHashValue hash, float alpha) +{ + unsigned long val = (unsigned long)hash; + + // scramble the bits up using Robert Jenkins' 32 bit integer hash function + val = (val+0x7ed55d16) + (val<<12); + val = (val^0xc761c23c) ^ (val>>19); + val = (val+0x165667b1) + (val<<5); + val = (val+0xd3a2646c) ^ (val<<9); + val = (val+0xfd7046c5) + (val<<3); + val = (val^0xb55a4f09) ^ (val>>16); + + GLfloat r = (val>>0) & 0xFF; + GLfloat g = (val>>8) & 0xFF; + GLfloat b = (val>>16) & 0xFF; + + GLfloat max = cpfmax(cpfmax(r, g), b); + GLfloat min = cpfmin(cpfmin(r, g), b); + GLfloat intensity = 0.75; + + // Saturate and scale the color + if(min == max){ + return RGBAColor(intensity, 0.0, 0.0, alpha); + } else { + GLfloat coef = alpha*intensity/(max - min); + return RGBAColor( + (r - min)*coef, + (g - min)*coef, + (b - min)*coef, + alpha + ); + } +} + +static inline void +glColor_from_color(Color color){ + glColor4fv((GLfloat *)&color); +} + +static Color +ColorForShape(cpShape *shape) +{ + if(cpShapeGetSensor(shape)){ + return LAColor(1, 0); + } else { + cpBody *body = shape->body; + + if(cpBodyIsSleeping(body)){ + return LAColor(0.2, 1); + } else if(body->node.idleTime > shape->space->sleepTimeThreshold) { + return LAColor(0.66, 1); + } else { + return ColorFromHash(shape->hashid, SHAPE_ALPHA); + } + } +} + +static const GLfloat circleVAR[] = { + 0.0000f, 1.0000f, + 0.2588f, 0.9659f, + 0.5000f, 0.8660f, + 0.7071f, 0.7071f, + 0.8660f, 0.5000f, + 0.9659f, 0.2588f, + 1.0000f, 0.0000f, + 0.9659f, -0.2588f, + 0.8660f, -0.5000f, + 0.7071f, -0.7071f, + 0.5000f, -0.8660f, + 0.2588f, -0.9659f, + 0.0000f, -1.0000f, + -0.2588f, -0.9659f, + -0.5000f, -0.8660f, + -0.7071f, -0.7071f, + -0.8660f, -0.5000f, + -0.9659f, -0.2588f, + -1.0000f, -0.0000f, + -0.9659f, 0.2588f, + -0.8660f, 0.5000f, + -0.7071f, 0.7071f, + -0.5000f, 0.8660f, + -0.2588f, 0.9659f, + 0.0000f, 1.0000f, + 0.0f, 0.0f, // For an extra line to see the rotation. +}; +static const int circleVAR_count = sizeof(circleVAR)/sizeof(GLfloat)/2; + +void ChipmunkDebugDrawCircle(cpVect center, cpFloat angle, cpFloat radius, Color lineColor, Color fillColor) +{ + glVertexPointer(2, GL_FLOAT, 0, circleVAR); + + glPushMatrix(); { + glTranslatef(center.x, center.y, 0.0f); + glRotatef(angle*180.0f/M_PI, 0.0f, 0.0f, 1.0f); + glScalef(radius, radius, 1.0f); + + if(fillColor.a > 0){ + glColor_from_color(fillColor); + glDrawArrays(GL_TRIANGLE_FAN, 0, circleVAR_count - 1); + } + + if(lineColor.a > 0){ + glColor_from_color(lineColor); + glDrawArrays(GL_LINE_STRIP, 0, circleVAR_count); + } + } glPopMatrix(); +} + +static const GLfloat pillVAR[] = { + 0.0000f, 1.0000f, 1.0f, + 0.2588f, 0.9659f, 1.0f, + 0.5000f, 0.8660f, 1.0f, + 0.7071f, 0.7071f, 1.0f, + 0.8660f, 0.5000f, 1.0f, + 0.9659f, 0.2588f, 1.0f, + 1.0000f, 0.0000f, 1.0f, + 0.9659f, -0.2588f, 1.0f, + 0.8660f, -0.5000f, 1.0f, + 0.7071f, -0.7071f, 1.0f, + 0.5000f, -0.8660f, 1.0f, + 0.2588f, -0.9659f, 1.0f, + 0.0000f, -1.0000f, 1.0f, + + 0.0000f, -1.0000f, 0.0f, + -0.2588f, -0.9659f, 0.0f, + -0.5000f, -0.8660f, 0.0f, + -0.7071f, -0.7071f, 0.0f, + -0.8660f, -0.5000f, 0.0f, + -0.9659f, -0.2588f, 0.0f, + -1.0000f, -0.0000f, 0.0f, + -0.9659f, 0.2588f, 0.0f, + -0.8660f, 0.5000f, 0.0f, + -0.7071f, 0.7071f, 0.0f, + -0.5000f, 0.8660f, 0.0f, + -0.2588f, 0.9659f, 0.0f, + 0.0000f, 1.0000f, 0.0f, +}; +static const int pillVAR_count = sizeof(pillVAR)/sizeof(GLfloat)/3; + +void ChipmunkDebugDrawSegment(cpVect a, cpVect b, Color color) +{ + GLfloat verts[] = { + a.x, a.y, + b.x, b.y, + }; + + glVertexPointer(2, GL_FLOAT, 0, verts); + glColor_from_color(color); + glDrawArrays(GL_LINES, 0, 2); +} + +void ChipmunkDebugDrawFatSegment(cpVect a, cpVect b, cpFloat radius, Color lineColor, Color fillColor) +{ + if(radius){ + glVertexPointer(3, GL_FLOAT, 0, pillVAR); + glPushMatrix(); { + cpVect d = cpvsub(b, a); + cpVect r = cpvmult(d, radius/cpvlength(d)); + + const GLfloat matrix[] = { + r.x, r.y, 0.0f, 0.0f, + -r.y, r.x, 0.0f, 0.0f, + d.x, d.y, 0.0f, 0.0f, + a.x, a.y, 0.0f, 1.0f, + }; + glMultMatrixf(matrix); + + if(fillColor.a > 0){ + glColor_from_color(fillColor); + glDrawArrays(GL_TRIANGLE_FAN, 0, pillVAR_count); + } + + if(lineColor.a > 0){ + glColor_from_color(lineColor); + glDrawArrays(GL_LINE_LOOP, 0, pillVAR_count); + } + } glPopMatrix(); + } else { + ChipmunkDebugDrawSegment(a, b, lineColor); + } +} + +void ChipmunkDebugDrawPolygon(int count, cpVect *verts, Color lineColor, Color fillColor) +{ +#if CP_USE_DOUBLES + glVertexPointer(2, GL_DOUBLE, 0, verts); +#else + glVertexPointer(2, GL_FLOAT, 0, verts); +#endif + + if(fillColor.a > 0){ + glColor_from_color(fillColor); + glDrawArrays(GL_TRIANGLE_FAN, 0, count); + } + + if(lineColor.a > 0){ + glColor_from_color(lineColor); + glDrawArrays(GL_LINE_LOOP, 0, count); + } +} + +void ChipmunkDebugDrawPoints(cpFloat size, int count, cpVect *verts, Color color) +{ +#if CP_USE_DOUBLES + glVertexPointer(2, GL_DOUBLE, 0, verts); +#else + glVertexPointer(2, GL_FLOAT, 0, verts); +#endif + + glPointSize(size*ChipmunkDebugDrawPointLineScale); + glColor_from_color(color); + glDrawArrays(GL_POINTS, 0, count); +} + +void ChipmunkDebugDrawBB(cpBB bb, Color color) +{ + cpVect verts[] = { + cpv(bb.l, bb.b), + cpv(bb.l, bb.t), + cpv(bb.r, bb.t), + cpv(bb.r, bb.b), + }; + ChipmunkDebugDrawPolygon(4, verts, color, LAColor(0, 0)); +} + +static void +drawShape(cpShape *shape, void *unused) +{ + cpBody *body = shape->body; + Color color = ColorForShape(shape); + + switch(shape->klass->type){ + case CP_CIRCLE_SHAPE: { + cpCircleShape *circle = (cpCircleShape *)shape; + ChipmunkDebugDrawCircle(circle->tc, body->a, circle->r, LINE_COLOR, color); + break; + } + case CP_SEGMENT_SHAPE: { + cpSegmentShape *seg = (cpSegmentShape *)shape; + ChipmunkDebugDrawFatSegment(seg->ta, seg->tb, seg->r, LINE_COLOR, color); + break; + } + case CP_POLY_SHAPE: { + cpPolyShape *poly = (cpPolyShape *)shape; + ChipmunkDebugDrawPolygon(poly->numVerts, poly->tVerts, LINE_COLOR, color); + break; + } + default: break; + } +} + +void ChipmunkDebugDrawShape(cpShape *shape) +{ + drawShape(shape, NULL); +} + +void ChipmunkDebugDrawShapes(cpSpace *space) +{ + cpSpaceEachShape(space, drawShape, NULL); +} + +static const GLfloat springVAR[] = { + 0.00f, 0.0f, + 0.20f, 0.0f, + 0.25f, 3.0f, + 0.30f,-6.0f, + 0.35f, 6.0f, + 0.40f,-6.0f, + 0.45f, 6.0f, + 0.50f,-6.0f, + 0.55f, 6.0f, + 0.60f,-6.0f, + 0.65f, 6.0f, + 0.70f,-3.0f, + 0.75f, 6.0f, + 0.80f, 0.0f, + 1.00f, 0.0f, +}; +static const int springVAR_count = sizeof(springVAR)/sizeof(GLfloat)/2; + +static void +drawSpring(cpDampedSpring *spring, cpBody *body_a, cpBody *body_b) +{ + cpVect a = cpvadd(body_a->p, cpvrotate(spring->anchr1, body_a->rot)); + cpVect b = cpvadd(body_b->p, cpvrotate(spring->anchr2, body_b->rot)); + + cpVect points[] = {a, b}; + ChipmunkDebugDrawPoints(5, 2, points, CONSTRAINT_COLOR); + + cpVect delta = cpvsub(b, a); + + glVertexPointer(2, GL_FLOAT, 0, springVAR); + glPushMatrix(); { + GLfloat x = a.x; + GLfloat y = a.y; + GLfloat cos = delta.x; + GLfloat sin = delta.y; + GLfloat s = 1.0f/cpvlength(delta); + + const GLfloat matrix[] = { + cos, sin, 0.0f, 0.0f, + -sin*s, cos*s, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + x, y, 0.0f, 1.0f, + }; + + glMultMatrixf(matrix); + glDrawArrays(GL_LINE_STRIP, 0, springVAR_count); + } glPopMatrix(); +} + +static void +drawConstraint(cpConstraint *constraint, void *unused) +{ + cpBody *body_a = constraint->a; + cpBody *body_b = constraint->b; + + const cpConstraintClass *klass = constraint->klass; + if(klass == cpPinJointGetClass()){ + cpPinJoint *joint = (cpPinJoint *)constraint; + + cpVect a = cpvadd(body_a->p, cpvrotate(joint->anchr1, body_a->rot)); + cpVect b = cpvadd(body_b->p, cpvrotate(joint->anchr2, body_b->rot)); + + cpVect points[] = {a, b}; + ChipmunkDebugDrawPoints(5, 2, points, CONSTRAINT_COLOR); + ChipmunkDebugDrawSegment(a, b, CONSTRAINT_COLOR); + } else if(klass == cpSlideJointGetClass()){ + cpSlideJoint *joint = (cpSlideJoint *)constraint; + + cpVect a = cpvadd(body_a->p, cpvrotate(joint->anchr1, body_a->rot)); + cpVect b = cpvadd(body_b->p, cpvrotate(joint->anchr2, body_b->rot)); + + cpVect points[] = {a, b}; + ChipmunkDebugDrawPoints(5, 2, points, CONSTRAINT_COLOR); + ChipmunkDebugDrawSegment(a, b, CONSTRAINT_COLOR); + } else if(klass == cpPivotJointGetClass()){ + cpPivotJoint *joint = (cpPivotJoint *)constraint; + + cpVect a = cpvadd(body_a->p, cpvrotate(joint->anchr1, body_a->rot)); + cpVect b = cpvadd(body_b->p, cpvrotate(joint->anchr2, body_b->rot)); + + cpVect points[] = {a, b}; + ChipmunkDebugDrawPoints(10, 2, points, CONSTRAINT_COLOR); + } else if(klass == cpGrooveJointGetClass()){ + cpGrooveJoint *joint = (cpGrooveJoint *)constraint; + + cpVect a = cpvadd(body_a->p, cpvrotate(joint->grv_a, body_a->rot)); + cpVect b = cpvadd(body_a->p, cpvrotate(joint->grv_b, body_a->rot)); + cpVect c = cpvadd(body_b->p, cpvrotate(joint->anchr2, body_b->rot)); + + ChipmunkDebugDrawPoints(5, 1, &c, CONSTRAINT_COLOR); + ChipmunkDebugDrawSegment(a, b, CONSTRAINT_COLOR); + } else if(klass == cpDampedSpringGetClass()){ + drawSpring((cpDampedSpring *)constraint, body_a, body_b); + } +} + +void ChipmunkDebugDrawConstraint(cpConstraint *constraint) +{ + drawConstraint(constraint, NULL); +} + +void ChipmunkDebugDrawConstraints(cpSpace *space) +{ + cpSpaceEachConstraint(space, drawConstraint, NULL); +} + +void ChipmunkDebugDrawCollisionPoints(cpSpace *space) +{ + cpArray *arbiters = space->arbiters; + + glColor3f(1.0f, 0.0f, 0.0f); + glPointSize(4.0f*ChipmunkDebugDrawPointLineScale); + + glBegin(GL_POINTS); { + for(int i=0; inum; i++){ + cpArbiter *arb = (cpArbiter*)arbiters->arr[i]; + + for(int j=0; jnumContacts; j++){ + cpVect v = arb->contacts[j].p; + glVertex2f(v.x, v.y); + } + } + } glEnd(); +} diff --git a/cocos2dx/physics/chipmunk/ChipmunkDebugDraw.h b/cocos2dx/physics/chipmunk/ChipmunkDebugDraw.h new file mode 100644 index 0000000000..9cc0a722fd --- /dev/null +++ b/cocos2dx/physics/chipmunk/ChipmunkDebugDraw.h @@ -0,0 +1,50 @@ +/* Copyright (c) 2007 Scott Lembcke + * + * 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. + */ + +typedef struct Color { + float r, g, b, a; +} Color; + +static inline Color RGBAColor(float r, float g, float b, float a){ + Color color = {r, g, b, a}; + return color; +} + +static inline Color LAColor(float l, float a){ + Color color = {l, l, l, a}; + return color; +} + +extern float ChipmunkDebugDrawPointLineScale; + +void ChipmunkDebugDrawCircle(cpVect center, cpFloat angle, cpFloat radius, Color lineColor, Color fillColor); +void ChipmunkDebugDrawSegment(cpVect a, cpVect b, Color color); +void ChipmunkDebugDrawFatSegment(cpVect a, cpVect b, cpFloat radius, Color lineColor, Color fillColor); +void ChipmunkDebugDrawPolygon(int count, cpVect *verts, Color lineColor, Color fillColor); +void ChipmunkDebugDrawPoints(cpFloat size, int count, cpVect *verts, Color color); +void ChipmunkDebugDrawBB(cpBB bb, Color color); + +void ChipmunkDebugDrawConstraint(cpConstraint *constraint); +void ChipmunkDebugDrawShape(cpShape *shape); + +void ChipmunkDebugDrawShapes(cpSpace *space); +void ChipmunkDebugDrawConstraints(cpSpace *space); +void ChipmunkDebugDrawCollisionPoints(cpSpace *space); diff --git a/cocos2dx/sprite_nodes/CCSprite.cpp b/cocos2dx/sprite_nodes/CCSprite.cpp index ed8f0460c3..80c6fd696c 100644 --- a/cocos2dx/sprite_nodes/CCSprite.cpp +++ b/cocos2dx/sprite_nodes/CCSprite.cpp @@ -296,13 +296,18 @@ Sprite* Sprite::initWithCGImage(CGImageRef pImage, const char *pszKey) Sprite::Sprite(void) : _shouldBeHidden(false) , _texture(nullptr) +#ifdef CC_USE_PHYSICS , _physicsBody(nullptr) +#endif { } Sprite::~Sprite(void) { CC_SAFE_RELEASE(_texture); +#ifdef CC_USE_PHYSICS + CC_SAFE_RELEASE(_physicsBody); +#endif } void Sprite::setTextureRect(const Rect& rect) @@ -447,16 +452,6 @@ void Sprite::setTextureCoords(Rect rect) } } -void Sprite::setPhysicsBody(PhysicsBody* body) -{ - _physicsBody = body; -} - -PhysicsBody* Sprite::getPhysicsBody() const -{ - return _physicsBody; -} - void Sprite::updateTransform(void) { CCASSERT(_batchNode, "updateTransform is only valid when Sprite is being rendered using an SpriteBatchNode"); @@ -795,12 +790,27 @@ void Sprite::setPosition(const Point& pos) { Node::setPosition(pos); SET_DIRTY_RECURSIVELY(); + +#ifdef CC_USE_PHYSICS + if (_physicsBody) + { + _physicsBody->setPosition(pos); + } +#endif } -void Sprite::setRotation(float fRotation) +void Sprite::setRotation(float rotation) { - Node::setRotation(fRotation); + Node::setRotation(rotation); + SET_DIRTY_RECURSIVELY(); + +#ifdef CC_USE_PHYSICS + if (_physicsBody) + { + _physicsBody->setRotation(rotation); + } +#endif } void Sprite::setRotationX(float fRotationX) @@ -897,6 +907,33 @@ bool Sprite::isFlipY(void) const return _flipY; } +#ifdef CC_USE_PHYSICS +void Sprite::setPhysicsBody(PhysicsBody* body) +{ + _physicsBody = body; + _physicsBody->retain(); + _physicsBody->setPosition(getPosition()); + _physicsBody->setRotation(getRotation()); +} + +PhysicsBody* Sprite::getPhysicsBody() const +{ + return _physicsBody; +} + +void Sprite::visit() +{ + if (_physicsBody) + { + Node::setPosition(_physicsBody->getPosition()); + Node::setRotation(_physicsBody->getRotation()); + SET_DIRTY_RECURSIVELY(); + } + + Node::visit(); +} +#endif //CC_USE_PHYSICS + // // RGBA protocol // diff --git a/cocos2dx/sprite_nodes/CCSprite.h b/cocos2dx/sprite_nodes/CCSprite.h index 4d4d0e7bdf..6f66e630f6 100644 --- a/cocos2dx/sprite_nodes/CCSprite.h +++ b/cocos2dx/sprite_nodes/CCSprite.h @@ -454,6 +454,8 @@ public: * get the PhysicsBody the sprite have */ PhysicsBody* getPhysicsBody() const; + + virtual void visit() override; #endif /// @} End of Sprite properties getter/setters diff --git a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp index a1d9180b72..a95e864344 100644 --- a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp +++ b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp @@ -1,25 +1,42 @@ #include "PhysicsTest.h" #include "../testResource.h" -#include "cocos-ext.h" -USING_NS_CC_EXT; +USING_NS_CC; PhysicsTestLayer::PhysicsTestLayer() : _spriteTexture(NULL) { #ifdef CC_USE_PHYSICS - //Set up sprite -#if 1 - // Use batch node. Faster - auto parent = SpriteBatchNode::create("Images/blocks.png", 100); + setTouchEnabled(true); + setAccelerometerEnabled(true); + + // title + auto label = LabelTTF::create("Multi touch the screen", "Marker Felt", 36); + label->setPosition(Point( VisibleRect::center().x, VisibleRect::top().y - 30)); + this->addChild(label, -1); + + // menu for debug layer + MenuItemFont::setFontSize(18); + auto item = MenuItemFont::create("Toggle debug", CC_CALLBACK_1(PhysicsTestLayer::toggleDebugCallback, this)); + + auto menu = Menu::create(item, NULL); + this->addChild(menu); + menu->setPosition(Point(VisibleRect::right().x-100, VisibleRect::top().y-60)); + + auto sp = Sprite::create(); + auto body = PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size); + sp->setPhysicsBody(body); + this->addChild(sp); + sp->setPosition(VisibleRect::center()); + + auto parent = SpriteBatchNode::create("Images/grossini_dance_atlas.png", 100); _spriteTexture = parent->getTexture(); -#else - // doesn't use batch node. Slower - _spriteTexture = TextureCache::getInstance()->addImage("Images/blocks.png"); - auto parent = Node::create(); -#endif + + addNewSpriteAtPosition(VisibleRect::center()); + + createResetButton(); #else - auto label = LabelTTF::create("Should define CC_ENABLE_BOX2D_INTEGRATION=1\n to run this test case", + auto label = LabelTTF::create("Should define CC_USE_BOX2D or CC_USE_CHIPMUNK\n to run this test case", "Arial", 18); auto size = Director::getInstance()->getWinSize(); @@ -29,11 +46,18 @@ PhysicsTestLayer::PhysicsTestLayer() #endif } -PhysicsTestLayer::~PhysicsTestLayer() +void PhysicsTestLayer::toggleDebugCallback(Object* sender) { +#ifdef CC_USE_PHYSICS + if (dynamic_cast(this->getParent()) != nullptr) + { + PhysicsWorld* world = dynamic_cast(this->getParent())->getPhysicsWorld(); + world->setDebugDraw(!world->getDebugDraw()); + } +#endif } -void PhysicsTestLayer::initPhysics() +PhysicsTestLayer::~PhysicsTestLayer() { } @@ -41,6 +65,7 @@ void PhysicsTestLayer::createResetButton() { auto reset = MenuItemImage::create("Images/r1.png", "Images/r2.png", [](Object *sender) { auto s = new PhysicsTestScene(); + s->initTest(); auto child = new PhysicsTestLayer(); s->addChild(child); child->release(); @@ -55,37 +80,13 @@ void PhysicsTestLayer::createResetButton() } -void PhysicsTestLayer::addNewSpriteAtPosition(Point p) -{ - CCLOG("Add sprite %0.2f x %02.f",p.x,p.y); - -#if CC_ENABLE_BOX2D_INTEGRATION - auto parent = this->getChildByTag(kTagParentNode); - - //We have a 64x64 sprite sheet with 4 different 32x32 images. The following code is - //just randomly picking one of the images - int idx = (CCRANDOM_0_1() > .5 ? 0:1); - int idy = (CCRANDOM_0_1() > .5 ? 0:1); - auto sprite = PhysicsSprite::createWithTexture(_spriteTexture,Rect(32 * idx,32 * idy,32,32)); - parent->addChild(sprite); - sprite->setB2Body(body); - sprite->setPTMRatio(PTM_RATIO); - sprite->setPosition( Point( p.x, p.y) ); -#endif -} - void PhysicsTestLayer::ccTouchesEnded(Set* touches, Event* event) { //Add a new body/atlas sprite at the touched location - SetIterator it; - Touch* touch; - for( it = touches->begin(); it != touches->end(); it++) + for( auto &item: *touches) { - touch = static_cast(*it); - - if(!touch) - break; + auto touch = static_cast(item); auto location = touch->getLocation(); @@ -93,9 +94,40 @@ void PhysicsTestLayer::ccTouchesEnded(Set* touches, Event* event) } } +void PhysicsTestLayer::addNewSpriteAtPosition(Point p) +{ +#ifdef CC_USE_PHYSICS + CCLOG("Add sprite %0.2f x %02.f",p.x,p.y); + + int posx, posy; + + posx = CCRANDOM_0_1() * 200.0f; + posy = CCRANDOM_0_1() * 200.0f; + + posx = (posx % 4) * 85; + posy = (posy % 3) * 121; + + auto sp = Sprite::createWithTexture(_spriteTexture, Rect(posx, posy, 85, 121)); + auto body = PhysicsBody::createBox(Size(48, 108)); + sp->setPhysicsBody(body); + sp->setPosition(p); + this->addChild(sp); +#endif +} + bool PhysicsTestScene::initTest() { - return TestScene::initWithPhysics(); +#ifdef CC_USE_PHYSICS + if (TestScene::initWithPhysics()) + { + this->getPhysicsWorld()->setDebugDraw(true); + return true; + } +#else + return TestScene::init(); +#endif + + return false; } void PhysicsTestScene::runThisTest() diff --git a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h index 8140015488..73cfb56935 100644 --- a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h +++ b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.h @@ -11,10 +11,10 @@ class PhysicsTestLayer : public Layer public: PhysicsTestLayer(); ~PhysicsTestLayer(); - - void initPhysics(); + void createResetButton(); + void toggleDebugCallback(Object* sender); void addNewSpriteAtPosition(Point p); virtual void ccTouchesEnded(Set* touches, Event* event); } ; From 8ec7996dcd99424e60459902fd1a1d559f2bf357 Mon Sep 17 00:00:00 2001 From: boyu0 Date: Mon, 16 Sep 2013 22:51:48 +0800 Subject: [PATCH 13/28] issue #2771: Edit delegate to listener. --- cocos2dx/physics/CCPhysicsContact.cpp | 15 ++++- cocos2dx/physics/CCPhysicsContact.h | 46 +++---------- cocos2dx/physics/CCPhysicsWorld.cpp | 97 +++++++++++++-------------- cocos2dx/physics/CCPhysicsWorld.h | 32 +++------ 4 files changed, 78 insertions(+), 112 deletions(-) diff --git a/cocos2dx/physics/CCPhysicsContact.cpp b/cocos2dx/physics/CCPhysicsContact.cpp index d76b93f9d8..e7c5a8555a 100644 --- a/cocos2dx/physics/CCPhysicsContact.cpp +++ b/cocos2dx/physics/CCPhysicsContact.cpp @@ -128,10 +128,19 @@ bool PhysicsContactPostSolve::init() return true; } +PhysicsContactListener::PhysicsContactListener() +: onContactBegin(nullptr) +, onContactPreSolve(nullptr) +, onContactPostSolve(nullptr) +, onContactEnd(nullptr) +{ + +} -#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) -#elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) -#endif +PhysicsContactListener::~PhysicsContactListener() +{ + +} NS_CC_END #endif // CC_USE_PHYSICS diff --git a/cocos2dx/physics/CCPhysicsContact.h b/cocos2dx/physics/CCPhysicsContact.h index 6db1fd47f7..4b40fadf4a 100644 --- a/cocos2dx/physics/CCPhysicsContact.h +++ b/cocos2dx/physics/CCPhysicsContact.h @@ -36,16 +36,6 @@ NS_CC_BEGIN class PhysicsShape; class PhysicsWorld; -namespace PhysicsInnerCallbackFunctions -{ -#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) - int collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data); - int collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); - void collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); - void collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); -#endif -} - class PhysicsContactInfo; class PhysicsContact @@ -70,13 +60,6 @@ private: void* _data; friend class PhysicsWorld; - -#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) - friend int PhysicsInnerCallbackFunctions::collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data); - friend int PhysicsInnerCallbackFunctions::collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); - friend void PhysicsInnerCallbackFunctions::collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); - friend void PhysicsInnerCallbackFunctions::collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); -#endif }; class PhysicsContactPreSolve @@ -89,13 +72,6 @@ private: bool init(); friend class PhysicsWorld; - -#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) - friend int PhysicsInnerCallbackFunctions::collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data); - friend int PhysicsInnerCallbackFunctions::collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); - friend void PhysicsInnerCallbackFunctions::collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); - friend void PhysicsInnerCallbackFunctions::collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); -#endif }; class PhysicsContactPostSolve @@ -108,26 +84,20 @@ private: bool init(); friend class PhysicsWorld; - -#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) - friend int PhysicsInnerCallbackFunctions::collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data); - friend int PhysicsInnerCallbackFunctions::collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); - friend void PhysicsInnerCallbackFunctions::collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); - friend void PhysicsInnerCallbackFunctions::collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); -#endif }; -class PhysicsContactDelegate +class PhysicsContactListener { public: - PhysicsContactDelegate(); - virtual ~PhysicsContactDelegate(); + PhysicsContactListener(); + virtual ~PhysicsContactListener(); public: - virtual bool onContactBegin(const PhysicsContact& contact) = 0; - virtual bool onContactPreSolve(const PhysicsContact& contact, const PhysicsContactPreSolve& solve) = 0; - virtual void onContactPostSove(const PhysicsContact& contact, const PhysicsContactPostSolve& solve)= 0; - virtual void onContactEnd(const PhysicsContact& contact) = 0; + std::function onContactBegin; + std::function onContactPreSolve; + + std::function onContactPostSolve; + std::function onContactEnd; }; NS_CC_END diff --git a/cocos2dx/physics/CCPhysicsWorld.cpp b/cocos2dx/physics/CCPhysicsWorld.cpp index 78a6f3b806..e880f72f65 100644 --- a/cocos2dx/physics/CCPhysicsWorld.cpp +++ b/cocos2dx/physics/CCPhysicsWorld.cpp @@ -48,47 +48,44 @@ NS_CC_BEGIN #if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) -namespace PhysicsInnerCallbackFunctions +int PhysicsWorld::collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data) { - int collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data) - { - PhysicsWorld* world = static_cast(data); - - CP_ARBITER_GET_SHAPES(arb, a, b); - - auto ita = PhysicsShapeInfo::map.find(a); - auto itb = PhysicsShapeInfo::map.find(b); - CC_ASSERT(ita != PhysicsShapeInfo::map.end() && itb != PhysicsShapeInfo::map.end()); - - PhysicsContact* contact = PhysicsContact::create(ita->second->shape, itb->second->shape); - arb->data = contact; - - return world->collisionBeginCallback(*static_cast(arb->data)); - } + PhysicsWorld* world = static_cast(data); - int collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data) - { - PhysicsWorld* world = static_cast(data); - return world->collisionPreSolveCallback(*static_cast(arb->data), - PhysicsContactPreSolve()); - } + CP_ARBITER_GET_SHAPES(arb, a, b); - void collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data) - { - PhysicsWorld* world = static_cast(data); - world->collisionPostSolveCallback(*static_cast(arb->data), - PhysicsContactPostSolve()); - } + auto ita = PhysicsShapeInfo::map.find(a); + auto itb = PhysicsShapeInfo::map.find(b); + CC_ASSERT(ita != PhysicsShapeInfo::map.end() && itb != PhysicsShapeInfo::map.end()); - void collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data) - { - PhysicsWorld* world = static_cast(data); - PhysicsContact* contact = static_cast(arb->data); - - world->collisionSeparateCallback(*contact); - - delete contact; - } + PhysicsContact* contact = PhysicsContact::create(ita->second->shape, itb->second->shape); + arb->data = contact; + + return world->collisionBeginCallback(*static_cast(arb->data)); +} + +int PhysicsWorld::collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data) +{ + PhysicsWorld* world = static_cast(data); + return world->collisionPreSolveCallback(*static_cast(arb->data), + PhysicsContactPreSolve()); +} + +void PhysicsWorld::collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data) +{ + PhysicsWorld* world = static_cast(data); + world->collisionPostSolveCallback(*static_cast(arb->data), + PhysicsContactPostSolve()); +} + +void PhysicsWorld::collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data) +{ + PhysicsWorld* world = static_cast(data); + PhysicsContact* contact = static_cast(arb->data); + + world->collisionSeparateCallback(*contact); + + delete contact; } bool PhysicsWorld::init() @@ -98,10 +95,10 @@ bool PhysicsWorld::init() cpSpaceSetGravity(_info->space, PhysicsHelper::point2cpv(_gravity)); cpSpaceSetDefaultCollisionHandler(_info->space, - PhysicsInnerCallbackFunctions::collisionBeginCallbackFunc, - PhysicsInnerCallbackFunctions::collisionPreSolveCallbackFunc, - PhysicsInnerCallbackFunctions::collisionPostSolveCallbackFunc, - PhysicsInnerCallbackFunctions::collisionSeparateCallbackFunc, + PhysicsWorld::collisionBeginCallbackFunc, + PhysicsWorld::collisionPreSolveCallbackFunc, + PhysicsWorld::collisionPostSolveCallbackFunc, + PhysicsWorld::collisionSeparateCallbackFunc, this); return true; @@ -246,9 +243,9 @@ void PhysicsWorld::drawWithShape(DrawNode* node, PhysicsShape* shape) int PhysicsWorld::collisionBeginCallback(const PhysicsContact& contact) { - if (_delegate) + if (_listener && _listener->onContactBegin) { - return _delegate->onContactBegin(contact); + return _listener->onContactBegin(contact); } return true; @@ -256,9 +253,9 @@ int PhysicsWorld::collisionBeginCallback(const PhysicsContact& contact) int PhysicsWorld::collisionPreSolveCallback(const PhysicsContact& contact, const PhysicsContactPreSolve& solve) { - if (_delegate) + if (_listener && _listener->onContactPreSolve) { - return _delegate->onContactPreSolve(contact, solve); + return _listener->onContactPreSolve(contact, solve); } return true; @@ -266,17 +263,17 @@ int PhysicsWorld::collisionPreSolveCallback(const PhysicsContact& contact, const void PhysicsWorld::collisionPostSolveCallback(const PhysicsContact& contact, const PhysicsContactPostSolve& solve) { - if (_delegate) + if (_listener && _listener->onContactPreSolve) { - _delegate->onContactPostSove(contact, solve); + _listener->onContactPostSolve(contact, solve); } } void PhysicsWorld::collisionSeparateCallback(const PhysicsContact& contact) { - if (_delegate) + if (_listener && _listener->onContactEnd) { - _delegate->onContactEnd(contact); + _listener->onContactEnd(contact); } } @@ -300,7 +297,7 @@ PhysicsWorld::PhysicsWorld() : _gravity(Point(0.0f, -98.0f)) , _speed(1.0f) , _info(nullptr) -, _delegate(nullptr) +, _listener(nullptr) , _bodys(nullptr) , _scene(nullptr) , _debugDraw(false) diff --git a/cocos2dx/physics/CCPhysicsWorld.h b/cocos2dx/physics/CCPhysicsWorld.h index 1cb2530351..625ba9c065 100644 --- a/cocos2dx/physics/CCPhysicsWorld.h +++ b/cocos2dx/physics/CCPhysicsWorld.h @@ -40,23 +40,13 @@ class PhysicsShape; class PhysicsContact; class PhysicsContactPreSolve; class PhysicsContactPostSolve; -class PhysicsContactDelegate; +class PhysicsContactListener; class Array; class Sprite; class Scene; class DrawNode; -namespace PhysicsInnerCallbackFunctions -{ -#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) - int collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data); - int collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); - void collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); - void collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); -#endif -} - class PhysicsWorld { public: @@ -69,8 +59,8 @@ public: Array* getBodysInRect(Rect rect) const; Array* getAllBody() const; - inline void registerContactDelegate(PhysicsContactDelegate* delegate) { _delegate = delegate; } - inline void unregisterContactDelegate() { _delegate = nullptr; } + inline void registerContactListener(PhysicsContactListener* delegate) { _listener = delegate; } + inline void unregisterContactListener() { _listener = nullptr; } inline Point getGravity() { return _gravity; } inline void setGravity(Point gravity) { _gravity = gravity; } @@ -97,11 +87,18 @@ protected: virtual void collisionPostSolveCallback(const PhysicsContact& contact, const PhysicsContactPostSolve& solve); virtual void collisionSeparateCallback(const PhysicsContact& contact); +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) + static int collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data); + static int collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); + static void collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); + static void collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); +#endif + protected: Point _gravity; float _speed; PhysicsWorldInfo* _info; - PhysicsContactDelegate* _delegate; + PhysicsContactListener* _listener; Array* _bodys; @@ -117,13 +114,6 @@ protected: friend class Sprite; friend class Scene; friend class PhysicsBody; - -#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) - friend int PhysicsInnerCallbackFunctions::collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data); - friend int PhysicsInnerCallbackFunctions::collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); - friend void PhysicsInnerCallbackFunctions::collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); - friend void PhysicsInnerCallbackFunctions::collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data); -#endif }; NS_CC_END From f96bc2355a043baa8f363a66a4a126779d2269d1 Mon Sep 17 00:00:00 2001 From: boyu0 Date: Mon, 16 Sep 2013 22:56:20 +0800 Subject: [PATCH 14/28] issue #2771: update android.mk --- cocos2dx/Android.mk | 16 ++++++++++++++++ samples/Cpp/TestCpp/Android.mk | 2 ++ 2 files changed, 18 insertions(+) diff --git a/cocos2dx/Android.mk b/cocos2dx/Android.mk index 0c54f17697..c03343e1b1 100644 --- a/cocos2dx/Android.mk +++ b/cocos2dx/Android.mk @@ -96,6 +96,22 @@ particle_nodes/CCParticleBatchNode.cpp \ particle_nodes/CCParticleExamples.cpp \ particle_nodes/CCParticleSystem.cpp \ particle_nodes/CCParticleSystemQuad.cpp \ +physics/CCPhysicsBody.cpp \ +physics/CCPhysicsContact.cpp \ +physics/CCPhysicsJoint.cpp \ +physics/CCPhysicsShape.cpp \ +physics/CCPhysicsWorld.cpp \ +physics/Box2D/CCPhysicsBodyInfo.cpp \ +physics/Box2D/CCPhysicsContactInfo.cpp \ +physics/Box2D/CCPhysicsJointInfo.cpp \ +physics/Box2D/CCPhysicsShapeInfo.cpp \ +physics/Box2D/CCPhysicsWorldInfo.cpp \ +physics/chipmunk/CCPhysicsBodyInfo.cpp \ +physics/chipmunk/CCPhysicsContactInfo.cpp \ +physics/chipmunk/CCPhysicsJointInfo.cpp \ +physics/chipmunk/CCPhysicsShapeInfo.cpp \ +physics/chipmunk/CCPhysicsWorldInfo.cpp \ +physics/chipmunk/ChipmunkDebugDraw.c \ platform/CCEGLViewProtocol.cpp \ platform/CCFileUtils.cpp \ platform/CCSAXParser.cpp \ diff --git a/samples/Cpp/TestCpp/Android.mk b/samples/Cpp/TestCpp/Android.mk index 339a50a3d8..f96922a9e7 100644 --- a/samples/Cpp/TestCpp/Android.mk +++ b/samples/Cpp/TestCpp/Android.mk @@ -45,6 +45,7 @@ Classes/DrawPrimitivesTest/DrawPrimitivesTest.cpp \ Classes/EffectsAdvancedTest/EffectsAdvancedTest.cpp \ Classes/EffectsTest/EffectsTest.cpp \ Classes/ExtensionsTest/ExtensionsTest.cpp \ +Classes/ExtensionsTest/ArmatureTest/ArmatureScene.cpp \ Classes/ExtensionsTest/CocosBuilderTest/CocosBuilderTest.cpp \ Classes/ExtensionsTest/CocosBuilderTest/AnimationsTest/AnimationsTestLayer.cpp \ Classes/ExtensionsTest/CocosBuilderTest/ButtonTest/ButtonTestLayer.cpp \ @@ -98,6 +99,7 @@ Classes/PerformanceTest/PerformanceSpriteTest.cpp \ Classes/PerformanceTest/PerformanceTest.cpp \ Classes/PerformanceTest/PerformanceTextureTest.cpp \ Classes/PerformanceTest/PerformanceTouchesTest.cpp \ +Classes/PhysicsTest/PhysicsTest.cpp \ Classes/RenderTextureTest/RenderTextureTest.cpp \ Classes/RotateWorldTest/RotateWorldTest.cpp \ Classes/SceneTest/SceneTest.cpp \ From 481ad7ef125ea5262af23b9850d08a0dac71bc83 Mon Sep 17 00:00:00 2001 From: boyu0 Date: Tue, 17 Sep 2013 10:41:08 +0800 Subject: [PATCH 15/28] issue #2771: update project setting --- .../proj.win32/CocosDenshion.vcxproj | 2 +- .../project.pbxproj.REMOVED.git-id | 2 +- .../physics/chipmunk/CCPhysicsBodyInfo.cpp | 2 +- cocos2dx/physics/chipmunk/ChipmunkDebugDraw.c | 446 ------------------ cocos2dx/physics/chipmunk/ChipmunkDebugDraw.h | 50 -- cocos2dx/proj.linux/cocos2dx.mk | 3 +- cocos2dx/proj.win32/cocos2d.vcxproj | 35 +- cocos2dx/proj.win32/cocos2d.vcxproj.filters | 108 +++++ extensions/Android.mk | 2 +- .../Cpp/HelloCpp/proj.android/jni/Android.mk | 3 +- .../Cpp/HelloCpp/proj.win32/HelloCpp.vcxproj | 2 +- .../SimpleGame/proj.android/jni/Android.mk | 3 +- samples/Cpp/TestCpp/proj.emscripten/Makefile | 1 + samples/Cpp/TestCpp/proj.linux/Makefile | 1 + .../Cpp/TestCpp/proj.win32/TestCpp.vcxproj | 2 + .../proj.win32/TestCpp.vcxproj.filters | 9 + .../Lua/HelloLua/proj.win32/HelloLua.vcxproj | 2 +- scripting/lua/proj.win32/liblua.vcxproj | 2 +- 18 files changed, 168 insertions(+), 507 deletions(-) delete mode 100644 cocos2dx/physics/chipmunk/ChipmunkDebugDraw.c delete mode 100644 cocos2dx/physics/chipmunk/ChipmunkDebugDraw.h diff --git a/CocosDenshion/proj.win32/CocosDenshion.vcxproj b/CocosDenshion/proj.win32/CocosDenshion.vcxproj index c5738cf5a2..7a91634027 100644 --- a/CocosDenshion/proj.win32/CocosDenshion.vcxproj +++ b/CocosDenshion/proj.win32/CocosDenshion.vcxproj @@ -65,7 +65,7 @@ Disabled - $(MSBuildProgramFiles32)\Microsoft SDKs\Windows\v7.1A\include;..\Include;"$(ProjectDir)..\..\cocos2dx";"$(ProjectDir)..\..\cocos2dx\include";"$(ProjectDir)..\..\cocos2dx\kazmath\include";"$(ProjectDir)..\..\cocos2dx\platform\win32";"$(ProjectDir)..\..\cocos2dx\platform\third_party\win32\OGLES";%(AdditionalIncludeDirectories) + $(MSBuildProgramFiles32)\Microsoft SDKs\Windows\v7.1A\include;..\Include;$(ProjectDir)..\..\cocos2dx;$(ProjectDir)..\..\cocos2dx\include;$(ProjectDir)..\..\cocos2dx\kazmath\include;$(ProjectDir)..\..\cocos2dx\platform\win32;$(ProjectDir)..\..\cocos2dx\platform\third_party\win32\OGLES;$(ProjectDir)..\..\external\chipmunk\include\chipmunk;%(AdditionalIncludeDirectories) WIN32;_DEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) false EnableFastChecks diff --git a/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id b/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id index 1436cd2cc4..1e9cc1e16a 100644 --- a/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -3f1cfaf06d95973aa8f2a629582705eb65bb24f6 \ No newline at end of file +29c3c2dfd8b683a800709d085839444c3a304a0d \ No newline at end of file diff --git a/cocos2dx/physics/chipmunk/CCPhysicsBodyInfo.cpp b/cocos2dx/physics/chipmunk/CCPhysicsBodyInfo.cpp index 4627c02499..5fd8ceb171 100644 --- a/cocos2dx/physics/chipmunk/CCPhysicsBodyInfo.cpp +++ b/cocos2dx/physics/chipmunk/CCPhysicsBodyInfo.cpp @@ -38,7 +38,7 @@ PhysicsBodyInfo::~PhysicsBodyInfo() Clonable* PhysicsBodyInfo::clone() const { - + return nullptr; } NS_CC_END diff --git a/cocos2dx/physics/chipmunk/ChipmunkDebugDraw.c b/cocos2dx/physics/chipmunk/ChipmunkDebugDraw.c deleted file mode 100644 index 38b06af1a0..0000000000 --- a/cocos2dx/physics/chipmunk/ChipmunkDebugDraw.c +++ /dev/null @@ -1,446 +0,0 @@ -/* Copyright (c) 2007 Scott Lembcke - * - * 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 -#include - -#ifdef __APPLE__ - #include "OpenGL/gl.h" - #include "OpenGL/glu.h" - #include -#else - #ifdef WIN32 - #include - #endif - - #include - #include - #include -#endif - -#include "chipmunk_private.h" -#include "ChipmunkDebugDraw.h" - -/* - IMPORTANT - READ ME! - - This file sets up a simple interface that the individual demos can use to get - a Chipmunk space running and draw what's in it. In order to keep the Chipmunk - examples clean and simple, they contain no graphics code. All drawing is done - by accessing the Chipmunk structures at a very low level. It is NOT - recommended to write a game or application this way as it does not scale - beyond simple shape drawing and is very dependent on implementation details - about Chipmunk which may change with little to no warning. -*/ - -const Color LINE_COLOR = {200.0/255.0, 210.0/255.0, 230.0/255.0, 1.0}; -const Color CONSTRAINT_COLOR = {0.0, 0.75, 0.0, 1.0}; -const float SHAPE_ALPHA = 1.0; - -float ChipmunkDebugDrawPointLineScale = 1.0; - -static Color -ColorFromHash(cpHashValue hash, float alpha) -{ - unsigned long val = (unsigned long)hash; - - // scramble the bits up using Robert Jenkins' 32 bit integer hash function - val = (val+0x7ed55d16) + (val<<12); - val = (val^0xc761c23c) ^ (val>>19); - val = (val+0x165667b1) + (val<<5); - val = (val+0xd3a2646c) ^ (val<<9); - val = (val+0xfd7046c5) + (val<<3); - val = (val^0xb55a4f09) ^ (val>>16); - - GLfloat r = (val>>0) & 0xFF; - GLfloat g = (val>>8) & 0xFF; - GLfloat b = (val>>16) & 0xFF; - - GLfloat max = cpfmax(cpfmax(r, g), b); - GLfloat min = cpfmin(cpfmin(r, g), b); - GLfloat intensity = 0.75; - - // Saturate and scale the color - if(min == max){ - return RGBAColor(intensity, 0.0, 0.0, alpha); - } else { - GLfloat coef = alpha*intensity/(max - min); - return RGBAColor( - (r - min)*coef, - (g - min)*coef, - (b - min)*coef, - alpha - ); - } -} - -static inline void -glColor_from_color(Color color){ - glColor4fv((GLfloat *)&color); -} - -static Color -ColorForShape(cpShape *shape) -{ - if(cpShapeGetSensor(shape)){ - return LAColor(1, 0); - } else { - cpBody *body = shape->body; - - if(cpBodyIsSleeping(body)){ - return LAColor(0.2, 1); - } else if(body->node.idleTime > shape->space->sleepTimeThreshold) { - return LAColor(0.66, 1); - } else { - return ColorFromHash(shape->hashid, SHAPE_ALPHA); - } - } -} - -static const GLfloat circleVAR[] = { - 0.0000f, 1.0000f, - 0.2588f, 0.9659f, - 0.5000f, 0.8660f, - 0.7071f, 0.7071f, - 0.8660f, 0.5000f, - 0.9659f, 0.2588f, - 1.0000f, 0.0000f, - 0.9659f, -0.2588f, - 0.8660f, -0.5000f, - 0.7071f, -0.7071f, - 0.5000f, -0.8660f, - 0.2588f, -0.9659f, - 0.0000f, -1.0000f, - -0.2588f, -0.9659f, - -0.5000f, -0.8660f, - -0.7071f, -0.7071f, - -0.8660f, -0.5000f, - -0.9659f, -0.2588f, - -1.0000f, -0.0000f, - -0.9659f, 0.2588f, - -0.8660f, 0.5000f, - -0.7071f, 0.7071f, - -0.5000f, 0.8660f, - -0.2588f, 0.9659f, - 0.0000f, 1.0000f, - 0.0f, 0.0f, // For an extra line to see the rotation. -}; -static const int circleVAR_count = sizeof(circleVAR)/sizeof(GLfloat)/2; - -void ChipmunkDebugDrawCircle(cpVect center, cpFloat angle, cpFloat radius, Color lineColor, Color fillColor) -{ - glVertexPointer(2, GL_FLOAT, 0, circleVAR); - - glPushMatrix(); { - glTranslatef(center.x, center.y, 0.0f); - glRotatef(angle*180.0f/M_PI, 0.0f, 0.0f, 1.0f); - glScalef(radius, radius, 1.0f); - - if(fillColor.a > 0){ - glColor_from_color(fillColor); - glDrawArrays(GL_TRIANGLE_FAN, 0, circleVAR_count - 1); - } - - if(lineColor.a > 0){ - glColor_from_color(lineColor); - glDrawArrays(GL_LINE_STRIP, 0, circleVAR_count); - } - } glPopMatrix(); -} - -static const GLfloat pillVAR[] = { - 0.0000f, 1.0000f, 1.0f, - 0.2588f, 0.9659f, 1.0f, - 0.5000f, 0.8660f, 1.0f, - 0.7071f, 0.7071f, 1.0f, - 0.8660f, 0.5000f, 1.0f, - 0.9659f, 0.2588f, 1.0f, - 1.0000f, 0.0000f, 1.0f, - 0.9659f, -0.2588f, 1.0f, - 0.8660f, -0.5000f, 1.0f, - 0.7071f, -0.7071f, 1.0f, - 0.5000f, -0.8660f, 1.0f, - 0.2588f, -0.9659f, 1.0f, - 0.0000f, -1.0000f, 1.0f, - - 0.0000f, -1.0000f, 0.0f, - -0.2588f, -0.9659f, 0.0f, - -0.5000f, -0.8660f, 0.0f, - -0.7071f, -0.7071f, 0.0f, - -0.8660f, -0.5000f, 0.0f, - -0.9659f, -0.2588f, 0.0f, - -1.0000f, -0.0000f, 0.0f, - -0.9659f, 0.2588f, 0.0f, - -0.8660f, 0.5000f, 0.0f, - -0.7071f, 0.7071f, 0.0f, - -0.5000f, 0.8660f, 0.0f, - -0.2588f, 0.9659f, 0.0f, - 0.0000f, 1.0000f, 0.0f, -}; -static const int pillVAR_count = sizeof(pillVAR)/sizeof(GLfloat)/3; - -void ChipmunkDebugDrawSegment(cpVect a, cpVect b, Color color) -{ - GLfloat verts[] = { - a.x, a.y, - b.x, b.y, - }; - - glVertexPointer(2, GL_FLOAT, 0, verts); - glColor_from_color(color); - glDrawArrays(GL_LINES, 0, 2); -} - -void ChipmunkDebugDrawFatSegment(cpVect a, cpVect b, cpFloat radius, Color lineColor, Color fillColor) -{ - if(radius){ - glVertexPointer(3, GL_FLOAT, 0, pillVAR); - glPushMatrix(); { - cpVect d = cpvsub(b, a); - cpVect r = cpvmult(d, radius/cpvlength(d)); - - const GLfloat matrix[] = { - r.x, r.y, 0.0f, 0.0f, - -r.y, r.x, 0.0f, 0.0f, - d.x, d.y, 0.0f, 0.0f, - a.x, a.y, 0.0f, 1.0f, - }; - glMultMatrixf(matrix); - - if(fillColor.a > 0){ - glColor_from_color(fillColor); - glDrawArrays(GL_TRIANGLE_FAN, 0, pillVAR_count); - } - - if(lineColor.a > 0){ - glColor_from_color(lineColor); - glDrawArrays(GL_LINE_LOOP, 0, pillVAR_count); - } - } glPopMatrix(); - } else { - ChipmunkDebugDrawSegment(a, b, lineColor); - } -} - -void ChipmunkDebugDrawPolygon(int count, cpVect *verts, Color lineColor, Color fillColor) -{ -#if CP_USE_DOUBLES - glVertexPointer(2, GL_DOUBLE, 0, verts); -#else - glVertexPointer(2, GL_FLOAT, 0, verts); -#endif - - if(fillColor.a > 0){ - glColor_from_color(fillColor); - glDrawArrays(GL_TRIANGLE_FAN, 0, count); - } - - if(lineColor.a > 0){ - glColor_from_color(lineColor); - glDrawArrays(GL_LINE_LOOP, 0, count); - } -} - -void ChipmunkDebugDrawPoints(cpFloat size, int count, cpVect *verts, Color color) -{ -#if CP_USE_DOUBLES - glVertexPointer(2, GL_DOUBLE, 0, verts); -#else - glVertexPointer(2, GL_FLOAT, 0, verts); -#endif - - glPointSize(size*ChipmunkDebugDrawPointLineScale); - glColor_from_color(color); - glDrawArrays(GL_POINTS, 0, count); -} - -void ChipmunkDebugDrawBB(cpBB bb, Color color) -{ - cpVect verts[] = { - cpv(bb.l, bb.b), - cpv(bb.l, bb.t), - cpv(bb.r, bb.t), - cpv(bb.r, bb.b), - }; - ChipmunkDebugDrawPolygon(4, verts, color, LAColor(0, 0)); -} - -static void -drawShape(cpShape *shape, void *unused) -{ - cpBody *body = shape->body; - Color color = ColorForShape(shape); - - switch(shape->klass->type){ - case CP_CIRCLE_SHAPE: { - cpCircleShape *circle = (cpCircleShape *)shape; - ChipmunkDebugDrawCircle(circle->tc, body->a, circle->r, LINE_COLOR, color); - break; - } - case CP_SEGMENT_SHAPE: { - cpSegmentShape *seg = (cpSegmentShape *)shape; - ChipmunkDebugDrawFatSegment(seg->ta, seg->tb, seg->r, LINE_COLOR, color); - break; - } - case CP_POLY_SHAPE: { - cpPolyShape *poly = (cpPolyShape *)shape; - ChipmunkDebugDrawPolygon(poly->numVerts, poly->tVerts, LINE_COLOR, color); - break; - } - default: break; - } -} - -void ChipmunkDebugDrawShape(cpShape *shape) -{ - drawShape(shape, NULL); -} - -void ChipmunkDebugDrawShapes(cpSpace *space) -{ - cpSpaceEachShape(space, drawShape, NULL); -} - -static const GLfloat springVAR[] = { - 0.00f, 0.0f, - 0.20f, 0.0f, - 0.25f, 3.0f, - 0.30f,-6.0f, - 0.35f, 6.0f, - 0.40f,-6.0f, - 0.45f, 6.0f, - 0.50f,-6.0f, - 0.55f, 6.0f, - 0.60f,-6.0f, - 0.65f, 6.0f, - 0.70f,-3.0f, - 0.75f, 6.0f, - 0.80f, 0.0f, - 1.00f, 0.0f, -}; -static const int springVAR_count = sizeof(springVAR)/sizeof(GLfloat)/2; - -static void -drawSpring(cpDampedSpring *spring, cpBody *body_a, cpBody *body_b) -{ - cpVect a = cpvadd(body_a->p, cpvrotate(spring->anchr1, body_a->rot)); - cpVect b = cpvadd(body_b->p, cpvrotate(spring->anchr2, body_b->rot)); - - cpVect points[] = {a, b}; - ChipmunkDebugDrawPoints(5, 2, points, CONSTRAINT_COLOR); - - cpVect delta = cpvsub(b, a); - - glVertexPointer(2, GL_FLOAT, 0, springVAR); - glPushMatrix(); { - GLfloat x = a.x; - GLfloat y = a.y; - GLfloat cos = delta.x; - GLfloat sin = delta.y; - GLfloat s = 1.0f/cpvlength(delta); - - const GLfloat matrix[] = { - cos, sin, 0.0f, 0.0f, - -sin*s, cos*s, 0.0f, 0.0f, - 0.0f, 0.0f, 1.0f, 0.0f, - x, y, 0.0f, 1.0f, - }; - - glMultMatrixf(matrix); - glDrawArrays(GL_LINE_STRIP, 0, springVAR_count); - } glPopMatrix(); -} - -static void -drawConstraint(cpConstraint *constraint, void *unused) -{ - cpBody *body_a = constraint->a; - cpBody *body_b = constraint->b; - - const cpConstraintClass *klass = constraint->klass; - if(klass == cpPinJointGetClass()){ - cpPinJoint *joint = (cpPinJoint *)constraint; - - cpVect a = cpvadd(body_a->p, cpvrotate(joint->anchr1, body_a->rot)); - cpVect b = cpvadd(body_b->p, cpvrotate(joint->anchr2, body_b->rot)); - - cpVect points[] = {a, b}; - ChipmunkDebugDrawPoints(5, 2, points, CONSTRAINT_COLOR); - ChipmunkDebugDrawSegment(a, b, CONSTRAINT_COLOR); - } else if(klass == cpSlideJointGetClass()){ - cpSlideJoint *joint = (cpSlideJoint *)constraint; - - cpVect a = cpvadd(body_a->p, cpvrotate(joint->anchr1, body_a->rot)); - cpVect b = cpvadd(body_b->p, cpvrotate(joint->anchr2, body_b->rot)); - - cpVect points[] = {a, b}; - ChipmunkDebugDrawPoints(5, 2, points, CONSTRAINT_COLOR); - ChipmunkDebugDrawSegment(a, b, CONSTRAINT_COLOR); - } else if(klass == cpPivotJointGetClass()){ - cpPivotJoint *joint = (cpPivotJoint *)constraint; - - cpVect a = cpvadd(body_a->p, cpvrotate(joint->anchr1, body_a->rot)); - cpVect b = cpvadd(body_b->p, cpvrotate(joint->anchr2, body_b->rot)); - - cpVect points[] = {a, b}; - ChipmunkDebugDrawPoints(10, 2, points, CONSTRAINT_COLOR); - } else if(klass == cpGrooveJointGetClass()){ - cpGrooveJoint *joint = (cpGrooveJoint *)constraint; - - cpVect a = cpvadd(body_a->p, cpvrotate(joint->grv_a, body_a->rot)); - cpVect b = cpvadd(body_a->p, cpvrotate(joint->grv_b, body_a->rot)); - cpVect c = cpvadd(body_b->p, cpvrotate(joint->anchr2, body_b->rot)); - - ChipmunkDebugDrawPoints(5, 1, &c, CONSTRAINT_COLOR); - ChipmunkDebugDrawSegment(a, b, CONSTRAINT_COLOR); - } else if(klass == cpDampedSpringGetClass()){ - drawSpring((cpDampedSpring *)constraint, body_a, body_b); - } -} - -void ChipmunkDebugDrawConstraint(cpConstraint *constraint) -{ - drawConstraint(constraint, NULL); -} - -void ChipmunkDebugDrawConstraints(cpSpace *space) -{ - cpSpaceEachConstraint(space, drawConstraint, NULL); -} - -void ChipmunkDebugDrawCollisionPoints(cpSpace *space) -{ - cpArray *arbiters = space->arbiters; - - glColor3f(1.0f, 0.0f, 0.0f); - glPointSize(4.0f*ChipmunkDebugDrawPointLineScale); - - glBegin(GL_POINTS); { - for(int i=0; inum; i++){ - cpArbiter *arb = (cpArbiter*)arbiters->arr[i]; - - for(int j=0; jnumContacts; j++){ - cpVect v = arb->contacts[j].p; - glVertex2f(v.x, v.y); - } - } - } glEnd(); -} diff --git a/cocos2dx/physics/chipmunk/ChipmunkDebugDraw.h b/cocos2dx/physics/chipmunk/ChipmunkDebugDraw.h deleted file mode 100644 index 9cc0a722fd..0000000000 --- a/cocos2dx/physics/chipmunk/ChipmunkDebugDraw.h +++ /dev/null @@ -1,50 +0,0 @@ -/* Copyright (c) 2007 Scott Lembcke - * - * 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. - */ - -typedef struct Color { - float r, g, b, a; -} Color; - -static inline Color RGBAColor(float r, float g, float b, float a){ - Color color = {r, g, b, a}; - return color; -} - -static inline Color LAColor(float l, float a){ - Color color = {l, l, l, a}; - return color; -} - -extern float ChipmunkDebugDrawPointLineScale; - -void ChipmunkDebugDrawCircle(cpVect center, cpFloat angle, cpFloat radius, Color lineColor, Color fillColor); -void ChipmunkDebugDrawSegment(cpVect a, cpVect b, Color color); -void ChipmunkDebugDrawFatSegment(cpVect a, cpVect b, cpFloat radius, Color lineColor, Color fillColor); -void ChipmunkDebugDrawPolygon(int count, cpVect *verts, Color lineColor, Color fillColor); -void ChipmunkDebugDrawPoints(cpFloat size, int count, cpVect *verts, Color color); -void ChipmunkDebugDrawBB(cpBB bb, Color color); - -void ChipmunkDebugDrawConstraint(cpConstraint *constraint); -void ChipmunkDebugDrawShape(cpShape *shape); - -void ChipmunkDebugDrawShapes(cpSpace *space); -void ChipmunkDebugDrawConstraints(cpSpace *space); -void ChipmunkDebugDrawCollisionPoints(cpSpace *space); diff --git a/cocos2dx/proj.linux/cocos2dx.mk b/cocos2dx/proj.linux/cocos2dx.mk index 3269129024..35955f2990 100644 --- a/cocos2dx/proj.linux/cocos2dx.mk +++ b/cocos2dx/proj.linux/cocos2dx.mk @@ -59,7 +59,8 @@ INCLUDES += \ -I$(COCOS_SRC)/platform/linux \ -I$(COCOS_SRC)/platform/third_party/linux/libjpeg \ -I$(COCOS_SRC)/platform/third_party/linux/libtiff \ - -I$(COCOS_SRC)/platform/third_party/linux/libwebp + -I$(COCOS_SRC)/platform/third_party/linux/libwebp \ + -I$(COCOS_ROOT/external/chipmunk/include/chipmunk LBITS := $(shell getconf LONG_BIT) ifeq ($(LBITS),64) diff --git a/cocos2dx/proj.win32/cocos2d.vcxproj b/cocos2dx/proj.win32/cocos2d.vcxproj index d6efeb7168..a37adf2788 100644 --- a/cocos2dx/proj.win32/cocos2d.vcxproj +++ b/cocos2dx/proj.win32/cocos2d.vcxproj @@ -69,7 +69,7 @@ Disabled - $(MSBuildProgramFiles32)\Microsoft SDKs\Windows\v7.1A\include;$(ProjectDir)..;$(ProjectDir)..\platform\win32;$(ProjectDir)..\platform\third_party\win32\iconv;$(ProjectDir)..\platform\third_party\win32\zlib;$(ProjectDir)..\platform\third_party\win32\libpng;$(ProjectDir)..\platform\third_party\win32\libjpeg;$(ProjectDir)..\platform\third_party\win32\libtiff;$(ProjectDir)..\platform\third_party\win32\libwebp;$(ProjectDir)..\platform\third_party\win32\libfreetype2;$(ProjectDir)..\platform\third_party\win32\OGLES;..\include;$(ProjectDir)..\kazmath\include;$(ProjectDir)..\platform\third_party\common\etc;%(AdditionalIncludeDirectories) + $(MSBuildProgramFiles32)\Microsoft SDKs\Windows\v7.1A\include;$(ProjectDir)..;$(ProjectDir)..\platform\win32;$(ProjectDir)..\platform\third_party\win32\iconv;$(ProjectDir)..\platform\third_party\win32\zlib;$(ProjectDir)..\platform\third_party\win32\libpng;$(ProjectDir)..\platform\third_party\win32\libjpeg;$(ProjectDir)..\platform\third_party\win32\libtiff;$(ProjectDir)..\platform\third_party\win32\libwebp;$(ProjectDir)..\platform\third_party\win32\libfreetype2;$(ProjectDir)..\platform\third_party\win32\OGLES;..\include;$(ProjectDir)..\kazmath\include;$(ProjectDir)..\platform\third_party\common\etc;$(ProjectDir)..\..\external\chipmunk\include\chipmunk;%(AdditionalIncludeDirectories) WIN32;_DEBUG;_WINDOWS;_LIB;COCOS2DXWIN32_EXPORTS;GL_GLEXT_PROTOTYPES;COCOS2D_DEBUG=1;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) false EnableFastChecks @@ -211,6 +211,21 @@ xcopy /Y /Q "$(ProjectDir)..\platform\third_party\win32\libraries\*.*" "$(OutDir + + + + + + + + + + + + + + + @@ -365,6 +380,24 @@ xcopy /Y /Q "$(ProjectDir)..\platform\third_party\win32\libraries\*.*" "$(OutDir + + + + + + + + + + + + + + + + + + diff --git a/cocos2dx/proj.win32/cocos2d.vcxproj.filters b/cocos2dx/proj.win32/cocos2d.vcxproj.filters index dcd9961e74..81e2e2a3d0 100644 --- a/cocos2dx/proj.win32/cocos2d.vcxproj.filters +++ b/cocos2dx/proj.win32/cocos2d.vcxproj.filters @@ -103,6 +103,15 @@ {3ff2746c-a91b-4b86-93b7-43a9ec14825b} + + {08593631-5bf5-46aa-9436-62595c4f7bf6} + + + {aeadfa95-9c89-4212-98ae-89ad57db596a} + + + {b9880458-36e5-4f28-a34b-d01d9512a395} + @@ -512,6 +521,51 @@ event_dispatcher + + physics + + + physics + + + physics + + + physics + + + physics + + + physics\chipmunk + + + physics\chipmunk + + + physics\chipmunk + + + physics\chipmunk + + + physics\chipmunk + + + physics\Box2D + + + physics\Box2D + + + physics\Box2D + + + physics\Box2D + + + physics\Box2D + @@ -1035,5 +1089,59 @@ event_dispatcher + + physics + + + physics + + + physics + + + physics + + + physics + + + physics + + + physics\chipmunk + + + physics\chipmunk + + + physics\chipmunk + + + physics\chipmunk + + + physics\chipmunk + + + physics\chipmunk + + + physics\Box2D + + + physics\Box2D + + + physics\Box2D + + + physics\Box2D + + + physics\Box2D + + + physics\Box2D + \ No newline at end of file diff --git a/extensions/Android.mk b/extensions/Android.mk index e5f7924e84..0a1877b23e 100644 --- a/extensions/Android.mk +++ b/extensions/Android.mk @@ -43,8 +43,8 @@ CocoStudio/Armature/display/CCDisplayFactory.cpp \ CocoStudio/Armature/display/CCDisplayManager.cpp \ CocoStudio/Armature/display/CCSkin.cpp \ CocoStudio/Armature/physics/CCColliderDetector.cpp \ -CocoStudio/Armature/utils/CCArmatureDefine.cpp \ CocoStudio/Armature/utils/CCArmatureDataManager.cpp \ +CocoStudio/Armature/utils/CCArmatureDefine.cpp \ CocoStudio/Armature/utils/CCDataReaderHelper.cpp \ CocoStudio/Armature/utils/CCSpriteFrameCacheHelper.cpp \ CocoStudio/Armature/utils/CCTransformHelp.cpp \ diff --git a/samples/Cpp/HelloCpp/proj.android/jni/Android.mk b/samples/Cpp/HelloCpp/proj.android/jni/Android.mk index 13989f3cf1..a59a21c7dd 100644 --- a/samples/Cpp/HelloCpp/proj.android/jni/Android.mk +++ b/samples/Cpp/HelloCpp/proj.android/jni/Android.mk @@ -12,10 +12,11 @@ LOCAL_SRC_FILES := hellocpp/main.cpp \ LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes -LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static cocos2dxandroid_static cocosdenshion_static +LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static cocos2dxandroid_static cocosdenshion_static chipmunk_static include $(BUILD_SHARED_LIBRARY) $(call import-module,cocos2dx) $(call import-module,CocosDenshion/android) $(call import-module,cocos2dx/platform/android) +$(call import-module,external/chipmunk) diff --git a/samples/Cpp/HelloCpp/proj.win32/HelloCpp.vcxproj b/samples/Cpp/HelloCpp/proj.win32/HelloCpp.vcxproj index 616d9f442d..cc87373ff7 100644 --- a/samples/Cpp/HelloCpp/proj.win32/HelloCpp.vcxproj +++ b/samples/Cpp/HelloCpp/proj.win32/HelloCpp.vcxproj @@ -67,7 +67,7 @@ Disabled - $(ProjectDir)..\..\..\..\cocos2dx;$(ProjectDir)..\..\..\..\cocos2dx\include;$(ProjectDir)..\..\..\..\cocos2dx\kazmath\include;$(ProjectDir)..\..\..\..\cocos2dx\platform\win32;$(ProjectDir)..\..\..\..\cocos2dx\platform\third_party\win32\OGLES;..\Classes;%(AdditionalIncludeDirectories) + $(ProjectDir)..\..\..\..\cocos2dx;$(ProjectDir)..\..\..\..\cocos2dx\include;$(ProjectDir)..\..\..\..\cocos2dx\kazmath\include;$(ProjectDir)..\..\..\..\cocos2dx\platform\win32;$(ProjectDir)..\..\..\..\cocos2dx\platform\third_party\win32\OGLES;..\Classes;$(ProjectDir)..\..\..\..\external\chipmunk\include\chipmunk;%(AdditionalIncludeDirectories) WIN32;_DEBUG;_WINDOWS;COCOS2D_DEBUG=1;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) true EnableFastChecks diff --git a/samples/Cpp/SimpleGame/proj.android/jni/Android.mk b/samples/Cpp/SimpleGame/proj.android/jni/Android.mk index e3f5b5c1ef..d50779e5c4 100644 --- a/samples/Cpp/SimpleGame/proj.android/jni/Android.mk +++ b/samples/Cpp/SimpleGame/proj.android/jni/Android.mk @@ -13,7 +13,7 @@ LOCAL_SRC_FILES := hellocpp/main.cpp \ LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes -LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static cocos2dxandroid_static cocosdenshion_static +LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static cocos2dxandroid_static cocosdenshion_static chipmunk_static include $(BUILD_SHARED_LIBRARY) @@ -21,3 +21,4 @@ $(call import-module,CocosDenshion/android) $(call import-module,cocos2dx) $(call import-module,extensions) $(call import-module,cocos2dx/platform/android) +$(call import-module,external/chipmunk) diff --git a/samples/Cpp/TestCpp/proj.emscripten/Makefile b/samples/Cpp/TestCpp/proj.emscripten/Makefile index 426cb385e8..e2088a2ab4 100644 --- a/samples/Cpp/TestCpp/proj.emscripten/Makefile +++ b/samples/Cpp/TestCpp/proj.emscripten/Makefile @@ -76,6 +76,7 @@ SOURCES = ../Classes/AccelerometerTest/AccelerometerTest.cpp \ ../Classes/PerformanceTest/PerformanceTest.cpp \ ../Classes/PerformanceTest/PerformanceTextureTest.cpp \ ../Classes/PerformanceTest/PerformanceTouchesTest.cpp \ + ../Classes/PhysicsTest/PhysicsTest.cpp \ ../Classes/RenderTextureTest/RenderTextureTest.cpp \ ../Classes/RotateWorldTest/RotateWorldTest.cpp \ ../Classes/SceneTest/SceneTest.cpp \ diff --git a/samples/Cpp/TestCpp/proj.linux/Makefile b/samples/Cpp/TestCpp/proj.linux/Makefile index aa64af3764..a10cab6883 100644 --- a/samples/Cpp/TestCpp/proj.linux/Makefile +++ b/samples/Cpp/TestCpp/proj.linux/Makefile @@ -82,6 +82,7 @@ SOURCES = ../Classes/AccelerometerTest/AccelerometerTest.cpp \ ../Classes/PerformanceTest/PerformanceTest.cpp \ ../Classes/PerformanceTest/PerformanceTextureTest.cpp \ ../Classes/PerformanceTest/PerformanceTouchesTest.cpp \ + ../Classes/PhysicsTest/PhysicsTest.cpp \ ../Classes/RenderTextureTest/RenderTextureTest.cpp \ ../Classes/RotateWorldTest/RotateWorldTest.cpp \ ../Classes/SceneTest/SceneTest.cpp \ diff --git a/samples/Cpp/TestCpp/proj.win32/TestCpp.vcxproj b/samples/Cpp/TestCpp/proj.win32/TestCpp.vcxproj index e38cc2fe68..af1e2fa4cc 100644 --- a/samples/Cpp/TestCpp/proj.win32/TestCpp.vcxproj +++ b/samples/Cpp/TestCpp/proj.win32/TestCpp.vcxproj @@ -160,6 +160,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\..\..\external\libwebsockets\win32\lib\*.*" "$(O + @@ -273,6 +274,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\..\..\external\libwebsockets\win32\lib\*.*" "$(O + diff --git a/samples/Cpp/TestCpp/proj.win32/TestCpp.vcxproj.filters b/samples/Cpp/TestCpp/proj.win32/TestCpp.vcxproj.filters index f6dac52c63..bf4d74d5c1 100644 --- a/samples/Cpp/TestCpp/proj.win32/TestCpp.vcxproj.filters +++ b/samples/Cpp/TestCpp/proj.win32/TestCpp.vcxproj.filters @@ -241,6 +241,9 @@ {0b414789-7971-4a4c-86e3-40cc00936684} + + {a83cb042-e3d6-4d3b-a4f6-30a4b3fcb3e9} + @@ -571,6 +574,9 @@ Classes\NewEventDispatcherTest + + Classes\PhysicsTest + @@ -1078,5 +1084,8 @@ Classes\NewEventDispatcherTest + + Classes\PhysicsTest + \ No newline at end of file diff --git a/samples/Lua/HelloLua/proj.win32/HelloLua.vcxproj b/samples/Lua/HelloLua/proj.win32/HelloLua.vcxproj index 7ab70c4ea1..6cc69ba5c8 100644 --- a/samples/Lua/HelloLua/proj.win32/HelloLua.vcxproj +++ b/samples/Lua/HelloLua/proj.win32/HelloLua.vcxproj @@ -77,7 +77,7 @@ Disabled - $(ProjectDir)..\Classes;$(ProjectDir)..\..\..\..\scripting\auto-generated\lua-bindings;$(ProjectDir)..\..\..\..\scripting\lua\cocos2dx_support;$(ProjectDir)..\..\..\..\scripting\lua\lua;$(ProjectDir)..\..\..\..\scripting\lua\tolua;$(ProjectDir)..\..\..\..\scripting\lua\src;$(ProjectDir)..\..\..\..\cocos2dx;$(ProjectDir)..\..\..\..\cocos2dx\include;$(ProjectDir)..\..\..\..\cocos2dx\kazmath\include;$(ProjectDir)..\..\..\..\cocos2dx\platform\win32;$(ProjectDir)..\..\..\..\cocos2dx\platform\third_party\win32\OGLES;$(ProjectDir)..\..\..\..\CocosDenshion\include;$(ProjectDir)..\..\..\..\extensions;%(AdditionalIncludeDirectories) + $(ProjectDir)..\Classes;$(ProjectDir)..\..\..\..\scripting\auto-generated\lua-bindings;$(ProjectDir)..\..\..\..\scripting\lua\cocos2dx_support;$(ProjectDir)..\..\..\..\scripting\lua\lua;$(ProjectDir)..\..\..\..\scripting\lua\tolua;$(ProjectDir)..\..\..\..\scripting\lua\src;$(ProjectDir)..\..\..\..\cocos2dx;$(ProjectDir)..\..\..\..\cocos2dx\include;$(ProjectDir)..\..\..\..\cocos2dx\kazmath\include;$(ProjectDir)..\..\..\..\cocos2dx\platform\win32;$(ProjectDir)..\..\..\..\cocos2dx\platform\third_party\win32\OGLES;$(ProjectDir)..\..\..\..\CocosDenshion\include;$(ProjectDir)..\..\..\..\extensions;$(ProjectDir)..\..\..\..\external\chipmunk\include\chipmunk;%(AdditionalIncludeDirectories) WIN32;_WINDOWS;STRICT;_DEBUG;COCOS2D_DEBUG=1;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) false EnableFastChecks diff --git a/scripting/lua/proj.win32/liblua.vcxproj b/scripting/lua/proj.win32/liblua.vcxproj index cc6539600d..a890c7e2dc 100644 --- a/scripting/lua/proj.win32/liblua.vcxproj +++ b/scripting/lua/proj.win32/liblua.vcxproj @@ -62,7 +62,7 @@ Disabled - $(ProjectDir)..\..\..\cocos2dx;$(ProjectDir)..\..\..\cocos2dx\include;$(ProjectDir)..\..\..\cocos2dx\kazmath\include;$(ProjectDir)..\..\..\cocos2dx\platform\win32;$(ProjectDir)..\..\..\cocos2dx\platform\third_party\win32\OGLES;$(ProjectDir)..\..\..\cocos2dx\platform\third_party\win32\pthread;$(ProjectDir)..\..\..\CocosDenshion\include;$(ProjectDir)..\..\..\extensions;$(ProjectDir)..\..\..\extensions\network;$(ProjectDir)..\..\..\external\libwebsockets\win32\include;$(ProjectDir)..\tolua;$(ProjectDir)..\luajit\include;$(ProjectDir)..\..\auto-generated\lua-bindings;$(ProjectDir)..\cocos2dx_support;%(AdditionalIncludeDirectories) + $(ProjectDir)..\..\..\cocos2dx;$(ProjectDir)..\..\..\cocos2dx\include;$(ProjectDir)..\..\..\cocos2dx\kazmath\include;$(ProjectDir)..\..\..\cocos2dx\platform\win32;$(ProjectDir)..\..\..\cocos2dx\platform\third_party\win32\OGLES;$(ProjectDir)..\..\..\cocos2dx\platform\third_party\win32\pthread;$(ProjectDir)..\..\..\CocosDenshion\include;$(ProjectDir)..\..\..\extensions;$(ProjectDir)..\..\..\extensions\network;$(ProjectDir)..\..\..\external\libwebsockets\win32\include;$(ProjectDir)..\tolua;$(ProjectDir)..\luajit\include;$(ProjectDir)..\..\auto-generated\lua-bindings;$(ProjectDir)..\cocos2dx_support;$(ProjectDir)..\..\external\chipmunk\include\chipmunk;%(AdditionalIncludeDirectories) WIN32;_WINDOWS;_DEBUG;COCOS2D_DEBUG=1;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) false EnableFastChecks From 54f7146d5f7401e39f704af73bada2e9812350b6 Mon Sep 17 00:00:00 2001 From: boyu0 Date: Tue, 17 Sep 2013 11:18:53 +0800 Subject: [PATCH 16/28] issue #2771: update vs project setting --- samples/Cpp/HelloCpp/proj.win32/HelloCpp.vcxproj | 2 +- samples/Lua/HelloLua/proj.win32/HelloLua.vcxproj | 2 +- scripting/lua/proj.win32/liblua.vcxproj | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/Cpp/HelloCpp/proj.win32/HelloCpp.vcxproj b/samples/Cpp/HelloCpp/proj.win32/HelloCpp.vcxproj index cc87373ff7..2e747d142d 100644 --- a/samples/Cpp/HelloCpp/proj.win32/HelloCpp.vcxproj +++ b/samples/Cpp/HelloCpp/proj.win32/HelloCpp.vcxproj @@ -84,7 +84,7 @@ true Windows MachineX86 - libcocos2d.lib;%(AdditionalDependencies) + libcocos2d.lib;libchipmunk.lib;%(AdditionalDependencies) diff --git a/samples/Lua/HelloLua/proj.win32/HelloLua.vcxproj b/samples/Lua/HelloLua/proj.win32/HelloLua.vcxproj index 6cc69ba5c8..b8fd26308f 100644 --- a/samples/Lua/HelloLua/proj.win32/HelloLua.vcxproj +++ b/samples/Lua/HelloLua/proj.win32/HelloLua.vcxproj @@ -95,7 +95,7 @@ $(MSBuildProgramFiles32)\Microsoft SDKs\Windows\v7.1A\include;$(IntDir);%(AdditionalIncludeDirectories) - libcocos2d.lib;libCocosDenshion.lib;liblua.lib;lua51.lib;libExtensions.lib;websockets.lib;%(AdditionalDependencies) + libcocos2d.lib;libCocosDenshion.lib;liblua.lib;lua51.lib;libExtensions.lib;websockets.lib;libchipmunk.lib;%(AdditionalDependencies) $(OutDir);%(AdditionalLibraryDirectories) true Windows diff --git a/scripting/lua/proj.win32/liblua.vcxproj b/scripting/lua/proj.win32/liblua.vcxproj index a890c7e2dc..1c646b774c 100644 --- a/scripting/lua/proj.win32/liblua.vcxproj +++ b/scripting/lua/proj.win32/liblua.vcxproj @@ -62,7 +62,7 @@ Disabled - $(ProjectDir)..\..\..\cocos2dx;$(ProjectDir)..\..\..\cocos2dx\include;$(ProjectDir)..\..\..\cocos2dx\kazmath\include;$(ProjectDir)..\..\..\cocos2dx\platform\win32;$(ProjectDir)..\..\..\cocos2dx\platform\third_party\win32\OGLES;$(ProjectDir)..\..\..\cocos2dx\platform\third_party\win32\pthread;$(ProjectDir)..\..\..\CocosDenshion\include;$(ProjectDir)..\..\..\extensions;$(ProjectDir)..\..\..\extensions\network;$(ProjectDir)..\..\..\external\libwebsockets\win32\include;$(ProjectDir)..\tolua;$(ProjectDir)..\luajit\include;$(ProjectDir)..\..\auto-generated\lua-bindings;$(ProjectDir)..\cocos2dx_support;$(ProjectDir)..\..\external\chipmunk\include\chipmunk;%(AdditionalIncludeDirectories) + $(ProjectDir)..\..\..\cocos2dx;$(ProjectDir)..\..\..\cocos2dx\include;$(ProjectDir)..\..\..\cocos2dx\kazmath\include;$(ProjectDir)..\..\..\cocos2dx\platform\win32;$(ProjectDir)..\..\..\cocos2dx\platform\third_party\win32\OGLES;$(ProjectDir)..\..\..\cocos2dx\platform\third_party\win32\pthread;$(ProjectDir)..\..\..\CocosDenshion\include;$(ProjectDir)..\..\..\extensions;$(ProjectDir)..\..\..\extensions\network;$(ProjectDir)..\..\..\external\libwebsockets\win32\include;$(ProjectDir)..\tolua;$(ProjectDir)..\luajit\include;$(ProjectDir)..\..\auto-generated\lua-bindings;$(ProjectDir)..\cocos2dx_support;$(ProjectDir)..\..\..\external\chipmunk\include\chipmunk;%(AdditionalIncludeDirectories) WIN32;_WINDOWS;_DEBUG;COCOS2D_DEBUG=1;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) false EnableFastChecks From 0a53a12f514575282d7392aa7ee22e66e550a2ab Mon Sep 17 00:00:00 2001 From: boyu0 Date: Tue, 17 Sep 2013 14:31:43 +0800 Subject: [PATCH 17/28] issue #2771: update project setting --- CocosDenshion/android/Android.mk | 3 +- cocos2dx/Android.mk | 7 +- cocos2dx/physics/CCPhysicsBody.cpp | 2 + cocos2dx/physics/CCPhysicsJoint.cpp | 30 + cocos2dx/physics/CCPhysicsWorld.cpp | 6 + cocos2dx/physics/CCPhysicsWorld.h | 2 +- .../physics/chipmunk/CCPhysicsShapeInfo.cpp | 3 +- samples/Cpp/TestCpp/Android.mk | 22 +- .../ArmatureTest/ArmatureScene.cpp | 873 ------------------ .../ArmatureTest/ArmatureScene.h | 217 ----- .../CocoStudioGUITest/CocosGUIScene.cpp | 2 +- 11 files changed, 68 insertions(+), 1099 deletions(-) delete mode 100644 samples/Cpp/TestCpp/Classes/ExtensionsTest/ArmatureTest/ArmatureScene.cpp delete mode 100644 samples/Cpp/TestCpp/Classes/ExtensionsTest/ArmatureTest/ArmatureScene.h diff --git a/CocosDenshion/android/Android.mk b/CocosDenshion/android/Android.mk index 2d59a7e79b..996a07f321 100644 --- a/CocosDenshion/android/Android.mk +++ b/CocosDenshion/android/Android.mk @@ -16,7 +16,8 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH)/../include \ $(LOCAL_PATH)/../../cocos2dx \ $(LOCAL_PATH)/../../cocos2dx/include \ $(LOCAL_PATH)/../../cocos2dx/kazmath/include \ - $(LOCAL_PATH)/../../cocos2dx/platform/android + $(LOCAL_PATH)/../../cocos2dx/platform/android \ + $(LOCAL_PATH)/../../external/chipmunk/include/chipmunk LOCAL_CFLAGS += -Wno-psabi LOCAL_EXPORT_CFLAGS += -Wno-psabi diff --git a/cocos2dx/Android.mk b/cocos2dx/Android.mk index c03343e1b1..9dddcde603 100644 --- a/cocos2dx/Android.mk +++ b/cocos2dx/Android.mk @@ -111,7 +111,6 @@ physics/chipmunk/CCPhysicsContactInfo.cpp \ physics/chipmunk/CCPhysicsJointInfo.cpp \ physics/chipmunk/CCPhysicsShapeInfo.cpp \ physics/chipmunk/CCPhysicsWorldInfo.cpp \ -physics/chipmunk/ChipmunkDebugDraw.c \ platform/CCEGLViewProtocol.cpp \ platform/CCFileUtils.cpp \ platform/CCSAXParser.cpp \ @@ -165,7 +164,8 @@ LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH) \ $(LOCAL_PATH)/platform/android \ $(LOCAL_PATH)/platform/third_party/common/etc \ $(LOCAL_PATH)/platform/third_party/common/s3tc \ - $(LOCAL_PATH)/platform/third_party/common/atitc + $(LOCAL_PATH)/platform/third_party/common/atitc \ + $(LOCAL_PATH)/../external/chipmunk/include/chipmunk LOCAL_C_INCLUDES := $(LOCAL_PATH) \ $(LOCAL_PATH)/include \ @@ -173,7 +173,8 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH) \ $(LOCAL_PATH)/platform/android \ $(LOCAL_PATH)/platform/third_party/common/etc \ $(LOCAL_PATH)/platform/third_party/common/s3tc \ - $(LOCAL_PATH)/platform/third_party/common/atitc + $(LOCAL_PATH)/platform/third_party/common/atitc \ + $(LOCAL_PATH)/../external/chipmunk/include/chipmunk LOCAL_LDLIBS := -lGLESv2 \ diff --git a/cocos2dx/physics/CCPhysicsBody.cpp b/cocos2dx/physics/CCPhysicsBody.cpp index 588586dded..655a57eb89 100644 --- a/cocos2dx/physics/CCPhysicsBody.cpp +++ b/cocos2dx/physics/CCPhysicsBody.cpp @@ -24,6 +24,8 @@ #include "CCPhysicsBody.h" #ifdef CC_USE_PHYSICS +#include + #include "CCPhysicsShape.h" #include "CCPhysicsJoint.h" #include "CCPhysicsWorld.h" diff --git a/cocos2dx/physics/CCPhysicsJoint.cpp b/cocos2dx/physics/CCPhysicsJoint.cpp index 572e682fa8..6b57ee2351 100644 --- a/cocos2dx/physics/CCPhysicsJoint.cpp +++ b/cocos2dx/physics/CCPhysicsJoint.cpp @@ -83,6 +83,36 @@ PhysicsJointPin::~PhysicsJointPin() } +PhysicsJointFixed::PhysicsJointFixed() +{ + +} + +PhysicsJointFixed::~PhysicsJointFixed() +{ + +} + +PhysicsJointSliding::PhysicsJointSliding() +{ + +} + +PhysicsJointSliding::~PhysicsJointSliding() +{ + +} + +PhysicsJointLimit::PhysicsJointLimit() +{ + +} + +PhysicsJointLimit::~PhysicsJointLimit() +{ + +} + #if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) PhysicsBodyInfo* PhysicsJoint::bodyInfo(PhysicsBody* body) const { diff --git a/cocos2dx/physics/CCPhysicsWorld.cpp b/cocos2dx/physics/CCPhysicsWorld.cpp index e880f72f65..d35260d4cb 100644 --- a/cocos2dx/physics/CCPhysicsWorld.cpp +++ b/cocos2dx/physics/CCPhysicsWorld.cpp @@ -277,6 +277,12 @@ void PhysicsWorld::collisionSeparateCallback(const PhysicsContact& contact) } } +void PhysicsWorld::setGravity(Point gravity) +{ + _gravity = gravity; + cpSpaceSetGravity(_info->space, PhysicsHelper::point2cpv(_gravity)); +} + #elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) #endif diff --git a/cocos2dx/physics/CCPhysicsWorld.h b/cocos2dx/physics/CCPhysicsWorld.h index 625ba9c065..6c4bed8b16 100644 --- a/cocos2dx/physics/CCPhysicsWorld.h +++ b/cocos2dx/physics/CCPhysicsWorld.h @@ -63,7 +63,7 @@ public: inline void unregisterContactListener() { _listener = nullptr; } inline Point getGravity() { return _gravity; } - inline void setGravity(Point gravity) { _gravity = gravity; } + void setGravity(Point gravity); inline bool getDebugDraw() { return _debugDraw; } inline void setDebugDraw(bool debugDraw) { _debugDraw = debugDraw; } diff --git a/cocos2dx/physics/chipmunk/CCPhysicsShapeInfo.cpp b/cocos2dx/physics/chipmunk/CCPhysicsShapeInfo.cpp index f22d7a3267..0734bf6e49 100644 --- a/cocos2dx/physics/chipmunk/CCPhysicsShapeInfo.cpp +++ b/cocos2dx/physics/chipmunk/CCPhysicsShapeInfo.cpp @@ -24,6 +24,7 @@ #include "CCPhysicsShapeInfo.h" #if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) +#include NS_CC_BEGIN std::map PhysicsShapeInfo::map; @@ -56,7 +57,7 @@ void PhysicsShapeInfo::remove(cpShape* shape) { if (shape == nullptr) return; - auto it = find(shapes.begin(), shapes.end(), shape); + auto it = std::find(shapes.begin(), shapes.end(), shape); if (it != shapes.end()) { shapes.erase(it); diff --git a/samples/Cpp/TestCpp/Android.mk b/samples/Cpp/TestCpp/Android.mk index b52c6db179..87d662be3a 100644 --- a/samples/Cpp/TestCpp/Android.mk +++ b/samples/Cpp/TestCpp/Android.mk @@ -45,7 +45,6 @@ Classes/DrawPrimitivesTest/DrawPrimitivesTest.cpp \ Classes/EffectsAdvancedTest/EffectsAdvancedTest.cpp \ Classes/EffectsTest/EffectsTest.cpp \ Classes/ExtensionsTest/ExtensionsTest.cpp \ -Classes/ExtensionsTest/ArmatureTest/ArmatureScene.cpp \ Classes/ExtensionsTest/CocosBuilderTest/CocosBuilderTest.cpp \ Classes/ExtensionsTest/CocosBuilderTest/AnimationsTest/AnimationsTestLayer.cpp \ Classes/ExtensionsTest/CocosBuilderTest/ButtonTest/ButtonTestLayer.cpp \ @@ -60,8 +59,27 @@ Classes/ExtensionsTest/CocoStudioComponentsTest/GameOverScene.cpp \ Classes/ExtensionsTest/CocoStudioComponentsTest/PlayerController.cpp \ Classes/ExtensionsTest/CocoStudioComponentsTest/ProjectileController.cpp \ Classes/ExtensionsTest/CocoStudioComponentsTest/SceneController.cpp \ -Classes/ExtensionsTest/CocoStudioSceneTest/SceneEditorTest.cpp \ Classes/ExtensionsTest/CocoStudioGUITest/CocosGUIScene.cpp \ +Classes/ExtensionsTest/CocoStudioGUITest/UIScene.cpp \ +Classes/ExtensionsTest/CocoStudioGUITest/UISceneManager.cpp \ +Classes/ExtensionsTest/CocoStudioGUITest/UIButtonTest/UIButtonTest.cpp \ +Classes/ExtensionsTest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest.cpp \ +Classes/ExtensionsTest/CocoStudioGUITest/UIDragPanelTest/UIDragPanelTest.cpp \ +Classes/ExtensionsTest/CocoStudioGUITest/UIImageViewTest/UIImageViewTest.cpp \ +Classes/ExtensionsTest/CocoStudioGUITest/UILabelAtlasTest/UILabelAtlasTest.cpp \ +Classes/ExtensionsTest/CocoStudioGUITest/UILabelBMFontTest/UILabelBMFontTest.cpp \ +Classes/ExtensionsTest/CocoStudioGUITest/UILabelTest/UILabelTest.cpp \ +Classes/ExtensionsTest/CocoStudioGUITest/UIListViewTest/UIListViewTest.cpp \ +Classes/ExtensionsTest/CocoStudioGUITest/UILoadingBarTest/UILoadingBarTest.cpp \ +Classes/ExtensionsTest/CocoStudioGUITest/UINodeContainerTest/UINodeContainerTest.cpp \ +Classes/ExtensionsTest/CocoStudioGUITest/UIPageViewTest/UIPageViewTest.cpp \ +Classes/ExtensionsTest/CocoStudioGUITest/UIPanelTest/UIPanelTest.cpp \ +Classes/ExtensionsTest/CocoStudioGUITest/UIScrollViewTest/UIScrollViewTest.cpp \ +Classes/ExtensionsTest/CocoStudioGUITest/UISliderTest/UISliderTest.cpp \ +Classes/ExtensionsTest/CocoStudioGUITest/UITextAreaTest/UITextAreaTest.cpp \ +Classes/ExtensionsTest/CocoStudioGUITest/UITextButtonTest/UITextButtonTest.cpp \ +Classes/ExtensionsTest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp \ +Classes/ExtensionsTest/CocoStudioSceneTest/SceneEditorTest.cpp \ Classes/ExtensionsTest/ControlExtensionTest/CCControlScene.cpp \ Classes/ExtensionsTest/ControlExtensionTest/CCControlSceneManager.cpp \ Classes/ExtensionsTest/ControlExtensionTest/CCControlButtonTest/CCControlButtonTest.cpp \ diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/ArmatureTest/ArmatureScene.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/ArmatureTest/ArmatureScene.cpp deleted file mode 100644 index 0e62769a95..0000000000 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/ArmatureTest/ArmatureScene.cpp +++ /dev/null @@ -1,873 +0,0 @@ -#include "ArmatureScene.h" -#include "../../testResource.h" - - -using namespace cocos2d; -using namespace cocos2d::extension; - -CCLayer* NextTest(); -CCLayer* BackTest(); -CCLayer* RestartTest(); - -static int s_nActionIdx = -1; - - -CCLayer *CreateLayer(int index) -{ - CCLayer *pLayer = NULL; - switch(index) - { - case TEST_DRAGON_BONES_2_0: - pLayer = new TestDragonBones20(); break; - case TEST_COCOSTUDIO_WITH_SKELETON: - pLayer = new TestCSWithSkeleton(); break; - case TEST_COCOSTUDIO_WITHOUT_SKELETON: - pLayer = new TestCSWithoutSkeleton(); break; - case TEST_PERFORMANCE: - pLayer = new TestPerformance(); break; - case TEST_CHANGE_ZORDER: - pLayer = new TestChangeZorder(); break; - case TEST_ANIMATION_EVENT: - pLayer = new TestAnimationEvent(); break; - case TEST_PARTICLE_DISPLAY: - pLayer = new TestParticleDisplay(); break; - case TEST_USE_DIFFERENT_PICTURE: - pLayer = new TestUseMutiplePicture(); break; - case TEST_BCOLLIDER_DETECTOR: - pLayer = new TestColliderDetector(); break; - case TEST_BOUDINGBOX: - pLayer = new TestBoundingBox(); break; - case TEST_ANCHORPOINT: - pLayer = new TestAnchorPoint(); break; - case TEST_ARMATURE_NESTING: - pLayer = new TestArmatureNesting(); break; - default: - break; - } - - return pLayer; -} - - -CCLayer* NextTest() -{ - ++s_nActionIdx; - s_nActionIdx = s_nActionIdx % TEST_LAYER_COUNT; - - CCLayer* pLayer = CreateLayer(s_nActionIdx); - pLayer->autorelease(); - - return pLayer; -} - -CCLayer* BackTest() -{ - --s_nActionIdx; - if( s_nActionIdx < 0 ) - s_nActionIdx += TEST_LAYER_COUNT; - - CCLayer* pLayer = CreateLayer(s_nActionIdx); - pLayer->autorelease(); - - return pLayer; -} - -CCLayer* RestartTest() -{ - CCLayer* pLayer = CreateLayer(s_nActionIdx); - pLayer->autorelease(); - - return pLayer; -} - - -ArmatureTestScene::ArmatureTestScene(bool bPortrait) -{ - TestScene::init(); - - CCSprite *bg = CCSprite::create("armature/bg.jpg"); - bg->setPosition(VisibleRect::center()); - - float scaleX = VisibleRect::getVisibleRect().size.width / bg->getContentSize().width; - float scaleY = VisibleRect::getVisibleRect().size.height / bg->getContentSize().height; - - bg->setScaleX(scaleX); - bg->setScaleY(scaleY); - - addChild(bg); -} -void ArmatureTestScene::runThisTest() -{ - CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("armature/TestBone0.png", "armature/TestBone0.plist", "armature/TestBone.json"); - CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("armature/Cowboy0.png", "armature/Cowboy0.plist", "armature/Cowboy.ExportJson"); - CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("armature/knight.png", "armature/knight.plist", "armature/knight.xml"); - CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("armature/weapon.png", "armature/weapon.plist", "armature/weapon.xml"); - CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("armature/robot.png", "armature/robot.plist", "armature/robot.xml"); - CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("armature/cyborg.png", "armature/cyborg.plist", "armature/cyborg.xml"); - CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("armature/Dragon.png", "armature/Dragon.plist", "armature/Dragon.xml"); - - s_nActionIdx = -1; - addChild(NextTest()); - - CCDirector::sharedDirector()->replaceScene(this); -} -void ArmatureTestScene::MainMenuCallback(CCObject* pSender) -{ - TestScene::MainMenuCallback(pSender); - - removeAllChildren(); - CCArmatureDataManager::sharedArmatureDataManager()->purgeArmatureSystem(); -} - - - -void ArmatureTestLayer::onEnter() -{ - - CCLayer::onEnter(); - - // add title and subtitle - std::string str = title(); - const char * pTitle = str.c_str(); - CCLabelTTF* label = CCLabelTTF::create(pTitle, "Arial", 18); - label->setColor(ccc3(0, 0, 0)); - addChild(label, 1, 10000); - label->setPosition( ccp(VisibleRect::center().x, VisibleRect::top().y - 30) ); - - std::string strSubtitle = subtitle(); - if( ! strSubtitle.empty() ) - { - CCLabelTTF* l = CCLabelTTF::create(strSubtitle.c_str(), "Arial", 18); - l->setColor(ccc3(0, 0, 0)); - addChild(l, 1, 10001); - l->setPosition( ccp(VisibleRect::center().x, VisibleRect::top().y - 60) ); - } - - // add menu - CCMenuItemImage *item1 = CCMenuItemImage::create(s_pPathB1, s_pPathB2, this, menu_selector(ArmatureTestLayer::backCallback) ); - CCMenuItemImage *item2 = CCMenuItemImage::create(s_pPathR1, s_pPathR2, this, menu_selector(ArmatureTestLayer::restartCallback) ); - CCMenuItemImage *item3 = CCMenuItemImage::create(s_pPathF1, s_pPathF2, this, menu_selector(ArmatureTestLayer::nextCallback) ); - - CCMenu *menu = CCMenu::create(item1, item2, item3, NULL); - - menu->setPosition(CCPointZero); - item1->setPosition(ccp(VisibleRect::center().x - item2->getContentSize().width*2, VisibleRect::bottom().y+item2->getContentSize().height/2)); - item2->setPosition(ccp(VisibleRect::center().x, VisibleRect::bottom().y+item2->getContentSize().height/2)); - item3->setPosition(ccp(VisibleRect::center().x + item2->getContentSize().width*2, VisibleRect::bottom().y+item2->getContentSize().height/2)); - - addChild(menu, 100); - - setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor)); - -} -void ArmatureTestLayer::onExit() -{ -} - -std::string ArmatureTestLayer::title() -{ - return "CSArmature Test Bed"; -} -std::string ArmatureTestLayer::subtitle() -{ - return ""; -} - -void ArmatureTestLayer::restartCallback(CCObject* pSender) -{ - CCScene* s = new ArmatureTestScene(); - s->addChild( RestartTest() ); - CCDirector::sharedDirector()->replaceScene(s); - s->release(); -} -void ArmatureTestLayer::nextCallback(CCObject* pSender) -{ - CCScene* s = new ArmatureTestScene(); - s->addChild( NextTest() ); - CCDirector::sharedDirector()->replaceScene(s); - s->release(); -} -void ArmatureTestLayer::backCallback(CCObject* pSender) -{ - CCScene* s = new ArmatureTestScene(); - s->addChild( BackTest() ); - CCDirector::sharedDirector()->replaceScene(s); - s->release(); -} -void ArmatureTestLayer::draw() -{ - CCLayer::draw(); -} - - - -void TestDragonBones20::onEnter() -{ - ArmatureTestLayer::onEnter(); - - cocos2d::extension::CCArmature *armature = NULL; - armature = cocos2d::extension::CCArmature::create("Dragon"); - armature->getAnimation()->playByIndex(1); - armature->getAnimation()->setAnimationScale(0.4f); - armature->setPosition(VisibleRect::center().x, VisibleRect::center().y * 0.3f); - armature->setScale(0.6f); - addChild(armature); -} - -std::string TestDragonBones20::title() -{ - return "Test Export From DragonBones version 2.0"; -} - - -void TestCSWithSkeleton::onEnter() -{ - ArmatureTestLayer::onEnter(); - cocos2d::extension::CCArmature *armature = NULL; - armature = cocos2d::extension::CCArmature::create("Cowboy"); - armature->getAnimation()->playByIndex(0); - armature->setScale(0.2f); - armature->setPosition(ccp(VisibleRect::center().x, VisibleRect::center().y/*-100*/)); - addChild(armature); -} - -std::string TestCSWithSkeleton::title() -{ - return "Test Export From CocoStudio With Skeleton Effect"; -} - - - -void TestCSWithoutSkeleton::onEnter() -{ - ArmatureTestLayer::onEnter(); - cocos2d::extension::CCArmature *armature = NULL; - armature = cocos2d::extension::CCArmature::create("TestBone"); - armature->getAnimation()->playByIndex(0); - armature->setAnchorPoint(ccp(0.5, -0.1)); - armature->setScale(0.2f); - armature->setPosition(ccp(VisibleRect::center().x, VisibleRect::center().y-100)); - addChild(armature); -} - -std::string TestCSWithoutSkeleton::title() -{ - return "Test Export From CocoStudio Without Skeleton Effect"; -} - - - - -TestPerformance::~TestPerformance() -{ -} -void TestPerformance::onEnter() -{ - ArmatureTestLayer::onEnter(); - - armatureCount = frames = times = lastTimes = 0; - generated = false; - - scheduleUpdate(); -} - -std::string TestPerformance::title() -{ - return "Test Performance"; -} -std::string TestPerformance::subtitle() -{ - return "Current CCArmature Count : "; -} -void TestPerformance::addArmature(cocos2d::extension::CCArmature *armature) -{ - armatureCount++; - addChild(armature, armatureCount); -} -void TestPerformance::update(float delta) -{ - frames ++; - times += delta; - - if (frames/times > 58) - { - cocos2d::extension::CCArmature *armature = NULL; - armature = new cocos2d::extension::CCArmature(); - armature->init("Knight_f/Knight"); - armature->getAnimation()->playByIndex(0); - armature->setPosition(50 + armatureCount * 2, 150); - armature->setScale(0.6f); - addArmature(armature); - armature->release(); - - char pszCount[255]; - sprintf(pszCount, "%s %i", subtitle().c_str(), armatureCount); - CCLabelTTF *label = (CCLabelTTF*)getChildByTag(10001); - label->setString(pszCount); - } -} - - - - - - -void TestChangeZorder::onEnter() -{ - ArmatureTestLayer::onEnter(); - - cocos2d::extension::CCArmature *armature = NULL; - currentTag = -1; - - armature = cocos2d::extension::CCArmature::create("Knight_f/Knight"); - armature->getAnimation()->playByIndex(0); - armature->setPosition(ccp(VisibleRect::center().x, VisibleRect::center().y-100)); - ++currentTag; - armature->setScale(0.6f); - addChild(armature, currentTag, currentTag); - - armature = cocos2d::extension::CCArmature::create("TestBone"); - armature->getAnimation()->playByIndex(0); - armature->setScale(0.24f); - armature->setPosition(ccp(VisibleRect::center().x, VisibleRect::center().y-100)); - ++currentTag; - addChild(armature, currentTag, currentTag); - - armature = cocos2d::extension::CCArmature::create("Dragon"); - armature->getAnimation()->playByIndex(0); - armature->setPosition(ccp(VisibleRect::center().x , VisibleRect::center().y-100)); - ++currentTag; - armature->setScale(0.6f); - addChild(armature, currentTag, currentTag); - - schedule( schedule_selector(TestChangeZorder::changeZorder), 1); - - currentTag = 0; -} -std::string TestChangeZorder::title() -{ - return "Test Change ZOrder Of Different CCArmature"; -} -void TestChangeZorder::changeZorder(float dt) -{ - - CCNode *node = getChildByTag(currentTag); - - node->setZOrder(CCRANDOM_0_1() * 3); - - currentTag ++; - currentTag = currentTag % 3; -} - - - - -void TestAnimationEvent::onEnter() -{ - ArmatureTestLayer::onEnter(); - armature = cocos2d::extension::CCArmature::create("Cowboy"); - armature->getAnimation()->play("Fire"); - armature->setScaleX(-0.24f); - armature->setScaleY(0.24f); - armature->setPosition(ccp(VisibleRect::left().x + 50, VisibleRect::left().y)); - armature->getAnimation()->MovementEventSignal.connect(this, &TestAnimationEvent::animationEvent); - addChild(armature); -} -std::string TestAnimationEvent::title() -{ - return "Test CCArmature Animation Event"; -} -void TestAnimationEvent::animationEvent(cocos2d::extension::CCArmature *armature, MovementEventType movementType, const char *movementID) -{ - std::string id = movementID; - - if (movementType == LOOP_COMPLETE) - { - if (id.compare("Fire") == 0) - { - CCActionInterval *actionToRight = CCMoveTo::create(2, ccp(VisibleRect::right().x - 50, VisibleRect::right().y)); - armature->stopAllActions(); - armature->runAction(CCSequence::create(actionToRight, CCCallFunc::create(this, callfunc_selector(TestAnimationEvent::callback1)), NULL)); - armature->getAnimation()->play("Walk"); - } - else if (id.compare("FireMax") == 0) - { - CCActionInterval *actionToLeft = CCMoveTo::create(2, ccp(VisibleRect::left().x + 50, VisibleRect::left().y)); - armature->stopAllActions(); - armature->runAction(CCSequence::create(actionToLeft, CCCallFunc::create(this, callfunc_selector(TestAnimationEvent::callback2)), NULL)); - armature->getAnimation()->play("Walk"); - } - } -} -void TestAnimationEvent::callback1() -{ - armature->runAction(CCScaleTo::create(0.3f, 0.3f, 0.3f)); - armature->getAnimation()->play("FireMax", 10); -} -void TestAnimationEvent::callback2() -{ - armature->runAction(CCScaleTo::create(0.3f, -0.3f, 0.3f)); - armature->getAnimation()->play("Fire", 10); -} - - - - -void TestParticleDisplay::onEnter() -{ - ArmatureTestLayer::onEnter(); - setTouchEnabled(true); - - animationID = 0; - - armature = cocos2d::extension::CCArmature::create("robot"); - armature->getAnimation()->playByIndex(0); - armature->setPosition(VisibleRect::center()); - armature->setScale(0.48f); - armature->getAnimation()->setAnimationScale(0.5f); - addChild(armature); - - - CCParticleSystem *p1 = CCParticleSystemQuad::create("Particles/SmallSun.plist"); - CCParticleSystem *p2 = CCParticleSystemQuad::create("Particles/SmallSun.plist"); - - cocos2d::extension::CCBone *bone = cocos2d::extension::CCBone::create("p1"); - bone->addDisplay(p1, 0); - bone->changeDisplayByIndex(0, true); - bone->setIgnoreMovementBoneData(true); - bone->setZOrder(100); - bone->setScale(1.2f); - armature->addBone(bone, "bady-a3"); - - bone = cocos2d::extension::CCBone::create("p2"); - bone->addDisplay(p2, 0); - bone->changeDisplayByIndex(0, true); - bone->setIgnoreMovementBoneData(true); - bone->setZOrder(100); - bone->setScale(1.2f); - armature->addBone(bone, "bady-a30"); -} -std::string TestParticleDisplay::title() -{ - return "Test Particle Display"; -} -std::string TestParticleDisplay::subtitle() -{ - return "Touch to change animation"; -} -bool TestParticleDisplay::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) -{ - ++animationID; - animationID = animationID % armature->getAnimation()->getMovementCount(); - armature->getAnimation()->playByIndex(animationID); - return false; -} - -void TestParticleDisplay::registerWithTouchDispatcher() -{ - CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, INT_MIN+1, true); -} - - - - -void TestUseMutiplePicture::onEnter() -{ - ArmatureTestLayer::onEnter(); - setTouchEnabled(true); - - displayIndex = 0; - - armature = cocos2d::extension::CCArmature::create("Knight_f/Knight"); - armature->getAnimation()->playByIndex(0); - armature->setPosition(ccp(VisibleRect::left().x+70, VisibleRect::left().y)); - armature->setScale(1.2f); - addChild(armature); - - std::string weapon[] = {"weapon_f-sword.png", "weapon_f-sword2.png", "weapon_f-sword3.png", "weapon_f-sword4.png", "weapon_f-sword5.png", "weapon_f-knife.png", "weapon_f-hammer.png"}; - - CCSpriteDisplayData displayData; - for (int i = 0; i < 7; i++) - { - displayData.setParam(weapon[i].c_str()); - armature->getBone("weapon")->addDisplay(&displayData, i); - } -} -std::string TestUseMutiplePicture::title() -{ - return "Test One CCArmature Use Different Picture"; -} -std::string TestUseMutiplePicture::subtitle() -{ - return "weapon and armature are in different picture"; -} -bool TestUseMutiplePicture::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) -{ - ++displayIndex; - displayIndex = (displayIndex) % 6; - armature->getBone("weapon")->changeDisplayByIndex(displayIndex, true); - return false; -} -void TestUseMutiplePicture::registerWithTouchDispatcher() -{ - CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, INT_MIN+1, true); -} - - - - - - -#if ENABLE_PHYSICS_BOX2D_DETECT - -class Contact -{ -public: - b2Fixture *fixtureA; - b2Fixture *fixtureB; -}; - -class ContactListener : public b2ContactListener -{ - //! Callbacks for derived classes. - virtual void BeginContact(b2Contact *contact) - { - if (contact) - { - Contact c; - c.fixtureA = contact->GetFixtureA(); - c.fixtureB = contact->GetFixtureB(); - - contact_list.push_back(c); - } - B2_NOT_USED(contact); - } - virtual void EndContact(b2Contact *contact) - { - contact_list.clear(); - B2_NOT_USED(contact); - } - virtual void PreSolve(b2Contact *contact, const b2Manifold *oldManifold) - { - B2_NOT_USED(contact); - B2_NOT_USED(oldManifold); - } - virtual void PostSolve(const b2Contact *contact, const b2ContactImpulse *impulse) - { - B2_NOT_USED(contact); - B2_NOT_USED(impulse); - } - -public: - std::list contact_list; -}; - -#elif ENABLE_PHYSICS_CHIPMUNK_DETECT - -enum ColliderType -{ - eBulletTag, - eEnemyTag -}; - - -int TestColliderDetector::beginHit(cpArbiter *arb, cpSpace *space, void *unused) -{ - CP_ARBITER_GET_SHAPES(arb, a, b); - - CCBone *bone = (CCBone*)a->data; - bone->getArmature()->setVisible(false); - - return 0; -} - -void TestColliderDetector::endHit(cpArbiter *arb, cpSpace *space, void *unused) -{ - CP_ARBITER_GET_SHAPES(arb, a, b); - - CCBone *bone = (CCBone*)a->data; - bone->getArmature()->setVisible(true); -} - -void TestColliderDetector::destroyCPBody(cpBody *body) -{ - cpShape *shape = body->shapeList_private; - while(shape) - { - cpShape *temp = shape->next_private; - - cpSpaceRemoveShape(space, shape); - cpShapeFree(shape); - - shape = temp; - } - - cpSpaceRemoveBody(space, body); - cpBodyFree(body); -} -#endif - -TestColliderDetector::~TestColliderDetector() -{ -#if ENABLE_PHYSICS_BOX2D_DETECT - CC_SAFE_DELETE(world); - CC_SAFE_DELETE(listener); - CC_SAFE_DELETE(debugDraw); -#elif ENABLE_PHYSICS_CHIPMUNK_DETECT - destroyCPBody(armature2->getCPBody()); - destroyCPBody(bullet->getCPBody()); - - cpSpaceFree(space); -#endif -} - -void TestColliderDetector::onEnter() -{ - ArmatureTestLayer::onEnter(); - - scheduleUpdate(); - - armature = cocos2d::extension::CCArmature::create("Cowboy"); - armature->getAnimation()->play("FireWithoutBullet"); - armature->getAnimation()->setAnimationScale(0.2f); - armature->setScaleX(-0.2f); - armature->setScaleY(0.2f); - armature->setPosition(ccp(VisibleRect::left().x + 70, VisibleRect::left().y)); - addChild(armature); - - armature2 = cocos2d::extension::CCArmature::create("Cowboy"); - armature2->getAnimation()->play("Walk"); - armature2->setScaleX(-0.2f); - armature2->setScaleY(0.2f); - armature2->setPosition(ccp(VisibleRect::right().x - 30, VisibleRect::left().y)); - addChild(armature2); - - bullet = CCPhysicsSprite::createWithSpriteFrameName("25.png"); - addChild(bullet); - - initWorld(); -} -std::string TestColliderDetector::title() -{ - return "Test Collider Detector"; -} -void TestColliderDetector::draw() -{ -#if ENABLE_PHYSICS_BOX2D_DETECT - ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position ); - kmGLPushMatrix(); - world->DrawDebugData(); - kmGLPopMatrix(); -#endif -} -void TestColliderDetector::update(float delta) -{ -#if ENABLE_PHYSICS_BOX2D_DETECT - armature2->setVisible(true); -#endif - - if (armature->getAnimation()->getCurrentFrameIndex() == 9) - { - CCPoint p = armature->getBone("Layer126")->getDisplayRenderNode()->convertToWorldSpaceAR(ccp(0, 0)); - bullet->setPosition(ccp(p.x + 60, p.y)); - - bullet->stopAllActions(); - bullet->runAction(CCMoveBy::create(1.5f, ccp(350, 0))); - } - -#if ENABLE_PHYSICS_BOX2D_DETECT - world->Step(delta, 0, 0); - - for (std::list::iterator it = listener->contact_list.begin(); it != listener->contact_list.end(); ++it) - { - Contact &contact = *it; - - b2Body *b2a = contact.fixtureA->GetBody(); - b2Body *b2b = contact.fixtureB->GetBody(); - - CCBone *ba = (CCBone *)b2a->GetUserData(); - CCBone *bb = (CCBone *)b2b->GetUserData(); - - bb->getArmature()->setVisible(false); - } -#elif ENABLE_PHYSICS_CHIPMUNK_DETECT - cpSpaceStep(space, delta); -#endif -} -void TestColliderDetector::initWorld() -{ -#if ENABLE_PHYSICS_BOX2D_DETECT - b2Vec2 noGravity(0, 0); - - world = new b2World(noGravity); - world->SetAllowSleeping(true); - - listener = new ContactListener(); - world->SetContactListener(listener); - - debugDraw = new GLESDebugDraw( PT_RATIO ); - world->SetDebugDraw(debugDraw); - - uint32 flags = 0; - flags += b2Draw::e_shapeBit; - // flags += b2Draw::e_jointBit; - // flags += b2Draw::e_aabbBit; - // flags += b2Draw::e_pairBit; - // flags += b2Draw::e_centerOfMassBit; - debugDraw->SetFlags(flags); - - - // Define the dynamic body. - //Set up a 1m squared box in the physics world - b2BodyDef bodyDef; - bodyDef.type = b2_dynamicBody; - - b2Body *body = world->CreateBody(&bodyDef); - - // Define another box shape for our dynamic body. - b2PolygonShape dynamicBox; - dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box - - // Define the dynamic body fixture. - b2FixtureDef fixtureDef; - fixtureDef.shape = &dynamicBox; - fixtureDef.isSensor = true; - body->CreateFixture(&fixtureDef); - - - bullet->setB2Body(body); - bullet->setPTMRatio(PT_RATIO); - bullet->setPosition( ccp( -100, -100) ); - - body = world->CreateBody(&bodyDef); - armature2->setB2Body(body); - -#elif ENABLE_PHYSICS_CHIPMUNK_DETECT - space = cpSpaceNew(); - space->gravity = cpv(0, 0); - - // Physics debug layer - CCPhysicsDebugNode *debugLayer = CCPhysicsDebugNode::create(space); - this->addChild(debugLayer, INT_MAX); - - CCSize size = bullet->getContentSize(); - - int num = 4; - cpVect verts[] = { - cpv(-size.width/2,-size.height/2), - cpv(-size.width/2,size.height/2), - cpv(size.width/2,size.height/2), - cpv(size.width/2,-size.height/2), - }; - - cpBody *body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, cpvzero)); - cpSpaceAddBody(space, body); - - cpShape* shape = cpPolyShapeNew(body, num, verts, cpvzero); - shape->collision_type = eBulletTag; - cpSpaceAddShape(space, shape); - - bullet->setCPBody(body); - - body = cpBodyNew(INFINITY, INFINITY); - cpSpaceAddBody(space, body); - armature2->setCPBody(body); - - shape = body->shapeList_private; - while(shape){ - cpShape *next = shape->next_private; - shape->collision_type = eEnemyTag; - shape = next; - } - - cpSpaceAddCollisionHandler(space, eEnemyTag, eBulletTag, beginHit, NULL, NULL, endHit, NULL); -#endif -} - - - - - -void TestBoundingBox::onEnter() -{ - ArmatureTestLayer::onEnter(); - - armature = cocos2d::extension::CCArmature::create("Cowboy"); - armature->getAnimation()->playByIndex(0); - armature->setPosition(VisibleRect::center()); - armature->setScale(0.2f); - addChild(armature); -} -std::string TestBoundingBox::title() -{ - return "Test BoundingBox"; -} -void TestBoundingBox::draw() -{ - CC_NODE_DRAW_SETUP(); - - rect = CCRectApplyAffineTransform(armature->boundingBox(), armature->nodeToParentTransform()); - - ccDrawColor4B(100, 100, 100, 255); - ccDrawRect(rect.origin, ccp(rect.getMaxX(), rect.getMaxY())); -} - - - -void TestAnchorPoint::onEnter() -{ - ArmatureTestLayer::onEnter(); - - for (int i = 0; i<5; i++) - { - cocos2d::extension::CCArmature *armature = cocos2d::extension::CCArmature::create("Cowboy"); - armature->getAnimation()->playByIndex(0); - armature->setPosition(VisibleRect::center()); - armature->setScale(0.2f); - addChild(armature, 0, i); - } - - getChildByTag(0)->setAnchorPoint(ccp(0,0)); - getChildByTag(1)->setAnchorPoint(ccp(0,1)); - getChildByTag(2)->setAnchorPoint(ccp(1,0)); - getChildByTag(3)->setAnchorPoint(ccp(1,1)); - getChildByTag(4)->setAnchorPoint(ccp(0.5,0.5)); - -} -std::string TestAnchorPoint::title() -{ - return "Test Set AnchorPoint"; -} - - -void TestArmatureNesting::onEnter() -{ - ArmatureTestLayer::onEnter(); - setTouchEnabled(true); - - armature = cocos2d::extension::CCArmature::create("cyborg"); - armature->getAnimation()->playByIndex(1); - armature->setPosition(VisibleRect::center()); - armature->setScale(1.2f); - armature->getAnimation()->setAnimationScale(0.4f); - addChild(armature); - - weaponIndex = 0; -} -std::string TestArmatureNesting::title() -{ - return "Test CCArmature Nesting"; -} -bool TestArmatureNesting::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) -{ - ++weaponIndex; - weaponIndex = weaponIndex % 4; - - armature->getBone("armInside")->getChildArmature()->getAnimation()->playByIndex(weaponIndex); - armature->getBone("armOutside")->getChildArmature()->getAnimation()->playByIndex(weaponIndex); - return false; -} -void TestArmatureNesting::registerWithTouchDispatcher() -{ - CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, INT_MIN+1, true); -} \ No newline at end of file diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/ArmatureTest/ArmatureScene.h b/samples/Cpp/TestCpp/Classes/ExtensionsTest/ArmatureTest/ArmatureScene.h deleted file mode 100644 index 67576a3746..0000000000 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/ArmatureTest/ArmatureScene.h +++ /dev/null @@ -1,217 +0,0 @@ -#ifndef __HELLOWORLD_SCENE_H__ -#define __HELLOWORLD_SCENE_H__ - -#include "cocos2d.h" -#include "cocos-ext.h" -#include "../../VisibleRect.h" -#include "../../testBasic.h" - -#if ENABLE_PHYSICS_BOX2D_DETECT -#include "../../Box2DTestBed/GLES-Render.h" -#include "Box2D/Box2D.h" -#elif ENABLE_PHYSICS_CHIPMUNK_DETECT -#include "chipmunk.h" -#endif - -class ArmatureTestScene : public TestScene -{ -public: - ArmatureTestScene(bool bPortrait = false); - - virtual void runThisTest(); - - // The CallBack for back to the main menu scene - virtual void MainMenuCallback(CCObject* pSender); -}; - -enum { - TEST_COCOSTUDIO_WITH_SKELETON = 0, - TEST_COCOSTUDIO_WITHOUT_SKELETON, - TEST_DRAGON_BONES_2_0, - TEST_PERFORMANCE, - TEST_CHANGE_ZORDER, - TEST_ANIMATION_EVENT, - TEST_PARTICLE_DISPLAY, - TEST_USE_DIFFERENT_PICTURE, - TEST_BCOLLIDER_DETECTOR, - TEST_BOUDINGBOX, - TEST_ANCHORPOINT, - TEST_ARMATURE_NESTING, - - TEST_LAYER_COUNT -}; - -class ArmatureTestLayer : public CCLayer -{ -public: - virtual void onEnter(); - virtual void onExit(); - - virtual std::string title(); - virtual std::string subtitle(); - - void restartCallback(CCObject* pSender); - void nextCallback(CCObject* pSender); - void backCallback(CCObject* pSender); - - virtual void draw(); -}; - - - -class TestDragonBones20 : public ArmatureTestLayer -{ -public: - virtual void onEnter(); - virtual std::string title(); -}; - - - -class TestCSWithSkeleton : public ArmatureTestLayer -{ - virtual void onEnter(); - virtual std::string title(); -}; - -class TestCSWithoutSkeleton : public ArmatureTestLayer -{ - virtual void onEnter(); - virtual std::string title(); -}; - - -class TestPerformance : public ArmatureTestLayer -{ -public: - ~TestPerformance(); - - virtual void onEnter(); - virtual std::string title(); - virtual std::string subtitle(); - virtual void addArmature(cocos2d::extension::CCArmature *armature); - void update(float delta); - - int armatureCount; - - int frames; - float times; - float lastTimes; - bool generated; -}; - - -class TestChangeZorder : public ArmatureTestLayer -{ - virtual void onEnter(); - virtual std::string title(); - void changeZorder(float dt); - - int currentTag; -}; - - -class TestAnimationEvent : public ArmatureTestLayer, public sigslot::has_slots<> -{ -public: - - virtual void onEnter(); - virtual std::string title(); - void animationEvent(cocos2d::extension::CCArmature *armature, cocos2d::extension::MovementEventType movementType, const char *movementID); - void callback1(); - void callback2(); - - cocos2d::extension::CCArmature *armature; -}; - -class TestUseMutiplePicture : public ArmatureTestLayer -{ - virtual void onEnter(); - virtual std::string title(); - virtual std::string subtitle(); - virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); - virtual void registerWithTouchDispatcher(); - - int displayIndex; - cocos2d::extension::CCArmature *armature; -}; - -class TestParticleDisplay : public ArmatureTestLayer -{ - virtual void onEnter(); - virtual std::string title(); - virtual std::string subtitle(); - virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); - virtual void registerWithTouchDispatcher(); - - int animationID; - cocos2d::extension::CCArmature *armature; -}; - -#if ENABLE_PHYSICS_BOX2D_DETECT -class ContactListener; -#endif - -class TestColliderDetector : public ArmatureTestLayer, public sigslot::has_slots<> -{ -public: - ~TestColliderDetector(); - - virtual void onEnter(); - virtual std::string title(); - virtual void draw(); - virtual void update(float delta); - - void initWorld(); - - cocos2d::extension::CCArmature *armature; - cocos2d::extension::CCArmature *armature2; - - cocos2d::extension::CCPhysicsSprite *bullet; - -#if ENABLE_PHYSICS_BOX2D_DETECT - b2World *world; - ContactListener *listener; - GLESDebugDraw *debugDraw; -#elif ENABLE_PHYSICS_CHIPMUNK_DETECT - cpSpace *space; - - static int beginHit(cpArbiter *arb, cpSpace *space, void *unused); - static void endHit(cpArbiter *arb, cpSpace *space, void *unused); - - void destroyCPBody(cpBody *body); -#endif -}; - - - -class TestBoundingBox : public ArmatureTestLayer -{ -public: - virtual void onEnter(); - virtual std::string title(); - virtual void draw(); - - cocos2d::extension::CCArmature *armature; - CCRect rect; -}; - -class TestAnchorPoint : public ArmatureTestLayer -{ -public: - virtual void onEnter(); - virtual std::string title(); -}; - -class TestArmatureNesting : public ArmatureTestLayer -{ -public: - virtual void onEnter(); - virtual std::string title(); - virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); - virtual void registerWithTouchDispatcher(); - - cocos2d::extension::CCArmature *armature; - int weaponIndex; -}; -#endif // __HELLOWORLD_SCENE_H__ \ No newline at end of file diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/CocosGUIScene.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/CocosGUIScene.cpp index 6514e7e2cf..4d17896184 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/CocosGUIScene.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/CocosGUIScene.cpp @@ -1,7 +1,7 @@ #include "CocosGUIScene.h" #include "UISceneManager.h" //#include "CocosGUIExamplesRegisterScene.h" -#include "ExtensionsTest.h" +#include "../ExtensionsTest.h" const char* gui_scene_names[2] = { From 5040ae33a17a7e5e88aed1f28a335e7d5b39487a Mon Sep 17 00:00:00 2001 From: boyu0 Date: Tue, 17 Sep 2013 16:27:43 +0800 Subject: [PATCH 18/28] issue #2771: add comments --- cocos2dx/physics/CCPhysicsBody.h | 112 +++++++++++++++++- cocos2dx/physics/CCPhysicsContact.h | 39 +++++- cocos2dx/physics/CCPhysicsJoint.h | 18 +++ cocos2dx/physics/CCPhysicsShape.h | 14 ++- cocos2dx/physics/CCPhysicsWorld.h | 14 ++- .../Classes/PhysicsTest/PhysicsTest.cpp | 2 +- 6 files changed, 193 insertions(+), 6 deletions(-) diff --git a/cocos2dx/physics/CCPhysicsBody.h b/cocos2dx/physics/CCPhysicsBody.h index 08fffb8722..515366e64e 100644 --- a/cocos2dx/physics/CCPhysicsBody.h +++ b/cocos2dx/physics/CCPhysicsBody.h @@ -46,42 +46,126 @@ class PhysicsShapeEdgePolygon; class PhysicsShapeEdgeChain; class PhysicsBodyInfo; - +/** + * A body affect by physics. + * it can attach one or more shapes. + */ class PhysicsBody : public Object//, public Clonable { public: + /** + * @brief Create a body contains a circle shape. + */ static PhysicsBody* createCircle(float radius); + /** + * @brief Create a body contains a box shape. + */ static PhysicsBody* createBox(Size size); + /** + * @brief Create a body contains a polygon shape. + * points is an array of Point structs defining a convex hull with a clockwise winding. + */ static PhysicsBody* createPolygon(Point* points, int count); + /** + * @brief Create a body contains a EdgeSegment shape. + */ static PhysicsBody* createEdgeSegment(Point a, Point b, float border = 1); + /** + * @brief Create a body contains a EdgeBox shape. + */ static PhysicsBody* createEdgeBox(Size size, float border = 1); + /** + * @brief Create a body contains a EdgePolygon shape. + */ static PhysicsBody* createEdgePolygon(Point* points, int count, float border = 1); + /** + * @brief Create a body contains a EdgeChain shape. + */ static PhysicsBody* createEdgeChain(Point* points, int count, float border = 1); + /** + * @brief Attach a circle shape with body + */ virtual PhysicsShapeCircle* addCircle(float radius, Point offset = Point(0, 0)); + /** + * @brief Attach a box shape with body + */ virtual PhysicsShapeBox* addBox(Size size, Point offset = Point(0, 0)); + /** + * @brief Attach a polygon shape with body + */ virtual PhysicsShapePolygon* addPolygon(Point* points, int count, Point offset = Point(0, 0)); + /** + * @brief Attach a edge segment shape with body + */ virtual PhysicsShapeEdgeSegment* addEdgeSegment(Point a, Point b, float border = 1); + /** + * @brief Attach a edge box shape with body + */ virtual PhysicsShapeEdgeBox* addEdgeBox(Size size, float border = 1, Point offset = Point(0, 0)); + /** + * @brief Attach a edge polygon shape with body + * points is an array of Point structs defining a convex hull with a clockwise winding. + */ virtual PhysicsShapeEdgePolygon* addEdgePolygon(Point* points, int count, float border = 1); + /** + * @brief Attach a edge chain shape with body + * points is an array of Point structs defining a convex hull with a clockwise winding. + */ virtual PhysicsShapeEdgeChain* addEdgeChain(Point* points, int count, float border = 1); + /** + * @brief Applies a immediate force to body. + */ virtual void applyForce(Point force); + /** + * @brief Applies a immediate force to body. + */ virtual void applyForce(Point force, Point offset); + /** + * @brief Applies a continuous force to body. + */ virtual void applyImpulse(Point impulse); + /** + * @brief Applies a continuous force to body. + */ virtual void applyImpulse(Point impulse, Point offset); + /** + * @brief Applies a torque force to body. + */ virtual void applyTorque(float torque); + /* + * @brief get the body shapes. + */ inline std::vector& getShapes() { return _shapes; } + /* + * @brief get the first body shapes. + */ inline PhysicsShape* getShape() { return _shapes.size() >= 1 ? _shapes.front() : nullptr; } + /* + * @brief remove a shape from body + */ void removeShape(PhysicsShape* shape); + /* + * @brief remove all shapes + */ void removeAllShapes(); + /* + * @brief get the world body added to. + */ inline PhysicsWorld* getWorld() const { return _world; } + /* + * @brief get all joints the body have + */ inline const std::vector* getJoints() const { return &_joints; } + /* + * @brief get the sprite the body set to. + */ inline Sprite* getOwner() const { return _owner; } void setCategoryBitmask(int bitmask); @@ -91,16 +175,42 @@ public: void setCollisionBitmask(int bitmask); inline int getCollisionBitmask() const { return _collisionBitmask; } + /* + * @brief get the body position. + */ Point getPosition() const; + /* + * @brief get the body rotation. + */ float getRotation() const; + /* + * @brief test the body is dynamic or not. + * a dynamic body will effect with gravity. + */ inline bool isDynamic() { return _dynamic; } + /* + * @brief set dynamic to body. + * a dynamic body will effect with gravity. + */ void setDynamic(bool dynamic); + /* + * @brief set the body mass. + */ void setMass(float mass); + /* + * @brief get the body mass. + */ inline float getMass() { return _mass; } + /* + * @brief set angular damping. + */ void setAngularDamping(float angularDamping); + /* + * @brief get angular damping. + */ inline float getAngularDamping() { return _angularDamping; } //virtual Clonable* clone() const override; diff --git a/cocos2dx/physics/CCPhysicsContact.h b/cocos2dx/physics/CCPhysicsContact.h index 4b40fadf4a..605a6dcf80 100644 --- a/cocos2dx/physics/CCPhysicsContact.h +++ b/cocos2dx/physics/CCPhysicsContact.h @@ -38,12 +38,28 @@ class PhysicsWorld; class PhysicsContactInfo; +/** + * @brief Contact infomation. it will created automatically when two shape contact with each other. and it will destoried automatically when two shape separated. + */ class PhysicsContact { public: + /* + * @brief get contact shape A. + */ inline PhysicsShape* getShapeA() { return _shapeA; } + /* + * @brief get contact shape B. + */ inline PhysicsShape* getShapeB() { return _shapeB; } + /* + * @brief get data. + */ inline void* getData() { return _data; } + /* + * @brief set data to contact. you must manage the memory yourself, Generally you can set data at contact begin, and distory it at contact end. + */ + inline void setData(void* data) { _data = data; } private: static PhysicsContact* create(PhysicsShape* a, PhysicsShape* b); @@ -62,6 +78,9 @@ private: friend class PhysicsWorld; }; +/* + * @brief presolve value generated when onContactPreSolve called. + */ class PhysicsContactPreSolve { private: @@ -74,6 +93,9 @@ private: friend class PhysicsWorld; }; +/* + * @brief postsolve value generated when onContactPostSolve called. + */ class PhysicsContactPostSolve { private: @@ -86,6 +108,9 @@ private: friend class PhysicsWorld; }; +/* + * @brief contact listener. + */ class PhysicsContactListener { public: @@ -93,10 +118,22 @@ public: virtual ~PhysicsContactListener(); public: + /* + * @brief it will called at two shapes start to contact, and only call it once. + */ std::function onContactBegin; + /* + * @brief Two shapes are touching during this step. Return false from the callback to make world ignore the collision this step or true to process it normally. Additionally, you may override collision values, elasticity, or surface velocity values. + */ std::function onContactPreSolve; - + /* + * @brief Two shapes are touching and their collision response has been processed. You can retrieve the collision impulse or kinetic energy at this time if you want to use it to calculate sound volumes or damage amounts. See cpArbiter for more info + */ std::function onContactPostSolve; + /* + * @brief it will called at two shapes separated, and only call it once. + * onContactBegin and onContactEnd will called in pairs. + */ std::function onContactEnd; }; diff --git a/cocos2dx/physics/CCPhysicsJoint.h b/cocos2dx/physics/CCPhysicsJoint.h index ab0315d690..854963049d 100644 --- a/cocos2dx/physics/CCPhysicsJoint.h +++ b/cocos2dx/physics/CCPhysicsJoint.h @@ -37,6 +37,9 @@ class PhysicsBody; class PhysicsJointInfo; class PhysicsBodyInfo; +/* + * @brief An PhysicsJoint object connects two physics bodies together. + */ class PhysicsJoint : public Object { protected: @@ -63,6 +66,9 @@ protected: friend class PhysicsBody; }; +/* + * @brief A fixed joint fuses the two bodies together at a reference point. Fixed joints are useful for creating complex shapes that can be broken apart later. + */ class PhysicsJointFixed : public PhysicsJoint { public: @@ -76,6 +82,9 @@ protected: virtual ~PhysicsJointFixed(); }; +/* + * @brief A sliding joint allows the two bodies to slide along a chosen axis. + */ class PhysicsJointSliding : public PhysicsJoint { public: @@ -89,6 +98,9 @@ protected: virtual ~PhysicsJointSliding(); }; +/* + * @brief A spring joint connects the two bodies with a spring whose length is the initial distance between the two bodies. + */ class PhysicsJointSpring : public PhysicsJoint { public: @@ -102,6 +114,9 @@ protected: virtual ~PhysicsJointSpring(); }; +/* + * @brief A limit joint imposes a maximum distance between the two bodies, as if they were connected by a rope. + */ class PhysicsJointLimit : public PhysicsJoint { public: @@ -115,6 +130,9 @@ protected: virtual ~PhysicsJointLimit(); }; +/* + * @brief A pin joint allows the two bodies to independently rotate around the anchor point as if pinned together. + */ class PhysicsJointPin : public PhysicsJoint { public: diff --git a/cocos2dx/physics/CCPhysicsShape.h b/cocos2dx/physics/CCPhysicsShape.h index 64049c2947..79cda68e6d 100644 --- a/cocos2dx/physics/CCPhysicsShape.h +++ b/cocos2dx/physics/CCPhysicsShape.h @@ -37,6 +37,9 @@ class PhysicsShapeInfo; class PhysicsBody; class PhysicsBodyInfo; +/** + * @brief A shape for body. You do not create PhysicsWorld objects directly, instead, you can view PhysicsBody to see how to create it. + */ class PhysicsShape : public Object { public: @@ -60,7 +63,7 @@ protected: bool init(PhysicsBody* body, Type type); /** - * PhysicsShape is PhysicsBody's friend class, but all the subclasses isn't. so this method is use for subclasses to catch the bodyInfo from PhysicsBody. + * @brief PhysicsShape is PhysicsBody's friend class, but all the subclasses isn't. so this method is use for subclasses to catch the bodyInfo from PhysicsBody. */ PhysicsBodyInfo* bodyInfo() const; @@ -68,7 +71,7 @@ protected: protected: PhysicsShape(); - virtual ~PhysicsShape(); + virtual ~PhysicsShape() = 0; protected: PhysicsBody* _body; @@ -79,6 +82,7 @@ protected: friend class PhysicsBody; }; +/** A circle shape */ class PhysicsShapeCircle : public PhysicsShape { protected: @@ -92,6 +96,7 @@ protected: friend class PhysicsBody; }; +/** A box shape */ class PhysicsShapeBox : public PhysicsShape { protected: @@ -105,6 +110,7 @@ protected: friend class PhysicsBody; }; +/** A polygon shape */ class PhysicsShapePolygon : public PhysicsShape { protected: @@ -118,6 +124,7 @@ protected: friend class PhysicsBody; }; +/** A segment shape */ class PhysicsShapeEdgeSegment : public PhysicsShape { protected: @@ -131,6 +138,7 @@ protected: friend class PhysicsBody; }; +/** An edge box shape */ class PhysicsShapeEdgeBox : public PhysicsShape { public: @@ -146,6 +154,7 @@ protected: friend class PhysicsBody; }; +/** An edge polygon shape */ class PhysicsShapeEdgePolygon : public PhysicsShape { public: @@ -161,6 +170,7 @@ protected: friend class PhysicsBody; }; +/** a chain shape */ class PhysicsShapeEdgeChain : public PhysicsShape { public: diff --git a/cocos2dx/physics/CCPhysicsWorld.h b/cocos2dx/physics/CCPhysicsWorld.h index 6c4bed8b16..425a831442 100644 --- a/cocos2dx/physics/CCPhysicsWorld.h +++ b/cocos2dx/physics/CCPhysicsWorld.h @@ -47,11 +47,17 @@ class Sprite; class Scene; class DrawNode; +/** + * @brief An PhysicsWorld object simulates collisions and other physical properties. You do not create PhysicsWorld objects directly; instead, you can get it from an Scene object. + */ class PhysicsWorld { public: + /** Adds a joint to the physics world.*/ void addJoint(PhysicsJoint* joint); + /** Removes a joint from the physics world.*/ void removeJoint(PhysicsJoint* joint); + /** Remove all joints from the physics world.*/ void removeAllJoints(); Array* getBodysAlongRay(Point start, Point end) const; @@ -59,13 +65,19 @@ public: Array* getBodysInRect(Rect rect) const; Array* getAllBody() const; + /** Register a listener to receive contact callbacks*/ inline void registerContactListener(PhysicsContactListener* delegate) { _listener = delegate; } + /** Unregister a listener. */ inline void unregisterContactListener() { _listener = nullptr; } + /** get the gravity value */ inline Point getGravity() { return _gravity; } + /** set the gravity value */ void setGravity(Point gravity); - inline bool getDebugDraw() { return _debugDraw; } + /** test the debug draw is enabled */ + inline bool isDebugDraw() { return _debugDraw; } + /** set the debug draw */ inline void setDebugDraw(bool debugDraw) { _debugDraw = debugDraw; } protected: diff --git a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp index e3b1b271a8..94f63e7f59 100644 --- a/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp +++ b/samples/Cpp/TestCpp/Classes/PhysicsTest/PhysicsTest.cpp @@ -52,7 +52,7 @@ void PhysicsTestLayer::toggleDebugCallback(Object* sender) #ifdef CC_USE_PHYSICS if (_scene != nullptr) { - _scene->getPhysicsWorld()->setDebugDraw(!_scene->getPhysicsWorld()->getDebugDraw()); + _scene->getPhysicsWorld()->setDebugDraw(!_scene->getPhysicsWorld()->isDebugDraw()); } #endif } From acbb50b5436c46b2461b4f24883e340be2f3137e Mon Sep 17 00:00:00 2001 From: boyu0 Date: Tue, 17 Sep 2013 16:28:32 +0800 Subject: [PATCH 19/28] issue #2771: edit Xcode project setting. --- samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id b/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id index 2fabb28142..a9525c1ccd 100644 --- a/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -60e85f9caa5b49a958f331723b961bdfcd0baed9 \ No newline at end of file +6d8e6b3004ee535bfb88fdd9148eb337bb67a3aa \ No newline at end of file From 89ec5edf78c9c49833290c5dfd4b962476c9aeb4 Mon Sep 17 00:00:00 2001 From: boyu0 Date: Tue, 17 Sep 2013 16:42:24 +0800 Subject: [PATCH 20/28] issue #2771: edit compile setting. --- tools/tojs/cocos2dx.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/tojs/cocos2dx.ini b/tools/tojs/cocos2dx.ini index 84d3d8ebc9..82440540cd 100644 --- a/tools/tojs/cocos2dx.ini +++ b/tools/tojs/cocos2dx.ini @@ -13,7 +13,7 @@ android_flags = -D_SIZE_T_DEFINED_ clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include clang_flags = -nostdinc -x c++ -std=c++11 -cocos_headers = -I%(cocosdir)s/cocos2dx/include -I%(cocosdir)s/cocos2dx/platform -I%(cocosdir)s/cocos2dx/platform/android -I%(cocosdir)s/cocos2dx -I%(cocosdir)s/cocos2dx/kazmath/include +cocos_headers = -I%(cocosdir)s/cocos2dx/include -I%(cocosdir)s/cocos2dx/platform -I%(cocosdir)s/cocos2dx/platform/android -I%(cocosdir)s/cocos2dx -I%(cocosdir)s/cocos2dx/kazmath/include -I%(cocosdir)s/external/chipmunk/include/chipmunk cocos_flags = -DANDROID -DCOCOS2D_JAVASCRIPT cxxgenerator_headers = -I%(cxxgeneratordir)s/targets/spidermonkey/common From b5b6406976d5ccaa76db574eb07ec7107667c7ad Mon Sep 17 00:00:00 2001 From: boyu0 Date: Tue, 17 Sep 2013 17:09:07 +0800 Subject: [PATCH 21/28] issue #2771: edit compile setting. --- tools/tojs/cocos2dx_extension.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/tojs/cocos2dx_extension.ini b/tools/tojs/cocos2dx_extension.ini index d0554f5314..9056fafb16 100644 --- a/tools/tojs/cocos2dx_extension.ini +++ b/tools/tojs/cocos2dx_extension.ini @@ -13,7 +13,7 @@ android_flags = -D_SIZE_T_DEFINED_ clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include clang_flags = -nostdinc -x c++ -std=c++11 -cocos_headers = -I%(cocosdir)s/cocos2dx/include -I%(cocosdir)s/cocos2dx/platform -I%(cocosdir)s/cocos2dx/platform/android -I%(cocosdir)s/cocos2dx -I%(cocosdir)s/cocos2dx/kazmath/include -I%(cocosdir)s/extensions +cocos_headers = -I%(cocosdir)s/cocos2dx/include -I%(cocosdir)s/cocos2dx/platform -I%(cocosdir)s/cocos2dx/platform/android -I%(cocosdir)s/cocos2dx -I%(cocosdir)s/cocos2dx/kazmath/include -I%(cocosdir)s/extensions -I%(cocosdir)s/external/chipmunk/include/chipmunk cocos_flags = -DANDROID -DCOCOS2D_JAVASCRIPT cxxgenerator_headers = -I%(cxxgeneratordir)s/targets/spidermonkey/common From a454e373c79f9e18030f1d2e20f934ec96ef42fa Mon Sep 17 00:00:00 2001 From: boyu0 Date: Tue, 17 Sep 2013 17:39:08 +0800 Subject: [PATCH 22/28] issue #2771: move include chipmunk and box2d to cpp --- cocos2dx/physics/CCPhysicsBody.cpp | 6 ++++++ cocos2dx/physics/CCPhysicsContact.cpp | 7 +++++++ cocos2dx/physics/CCPhysicsJoint.cpp | 7 +++++++ cocos2dx/physics/CCPhysicsSetting.h | 6 ------ cocos2dx/physics/CCPhysicsShape.cpp | 6 ++++++ cocos2dx/physics/CCPhysicsWorld.cpp | 6 ++++++ cocos2dx/physics/CCPhysicsWorld.h | 6 ++++++ cocos2dx/physics/chipmunk/CCPhysicsBodyInfo.h | 1 + cocos2dx/physics/chipmunk/CCPhysicsContactInfo.h | 1 + cocos2dx/physics/chipmunk/CCPhysicsHelper.h | 1 + cocos2dx/physics/chipmunk/CCPhysicsJointInfo.h | 3 +-- cocos2dx/physics/chipmunk/CCPhysicsShapeInfo.h | 5 +++-- cocos2dx/physics/chipmunk/CCPhysicsWorldInfo.h | 1 + tools/tojs/cocos2dx.ini | 2 +- tools/tojs/cocos2dx_extension.ini | 2 +- 15 files changed, 48 insertions(+), 12 deletions(-) diff --git a/cocos2dx/physics/CCPhysicsBody.cpp b/cocos2dx/physics/CCPhysicsBody.cpp index 655a57eb89..3fb5d485b1 100644 --- a/cocos2dx/physics/CCPhysicsBody.cpp +++ b/cocos2dx/physics/CCPhysicsBody.cpp @@ -26,6 +26,12 @@ #include +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) +#include "chipmunk.h" +#elif (CC_PHYSICS_ENGINE == CCPHYSICS_BOX2D) +#include "Box2D.h" +#endif + #include "CCPhysicsShape.h" #include "CCPhysicsJoint.h" #include "CCPhysicsWorld.h" diff --git a/cocos2dx/physics/CCPhysicsContact.cpp b/cocos2dx/physics/CCPhysicsContact.cpp index e7c5a8555a..928c8c9e59 100644 --- a/cocos2dx/physics/CCPhysicsContact.cpp +++ b/cocos2dx/physics/CCPhysicsContact.cpp @@ -23,6 +23,13 @@ ****************************************************************************/ #include "CCPhysicsContact.h" #ifdef CC_USE_PHYSICS + +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) +#include "chipmunk.h" +#elif (CC_PHYSICS_ENGINE == CCPHYSICS_BOX2D) +#include "Box2D.h" +#endif + #include "chipmunk/CCPhysicsContactInfo.h" #include "Box2D/CCPhysicsContactInfo.h" diff --git a/cocos2dx/physics/CCPhysicsJoint.cpp b/cocos2dx/physics/CCPhysicsJoint.cpp index 6b57ee2351..3e60d246ab 100644 --- a/cocos2dx/physics/CCPhysicsJoint.cpp +++ b/cocos2dx/physics/CCPhysicsJoint.cpp @@ -24,6 +24,13 @@ #include "CCPhysicsJoint.h" #ifdef CC_USE_PHYSICS + +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) +#include "chipmunk.h" +#elif (CC_PHYSICS_ENGINE == CCPHYSICS_BOX2D) +#include "Box2D.h" +#endif + #include "CCPhysicsBody.h" #include "chipmunk/CCPhysicsJointInfo.h" diff --git a/cocos2dx/physics/CCPhysicsSetting.h b/cocos2dx/physics/CCPhysicsSetting.h index 3d3aaaaf55..98b39f8f66 100644 --- a/cocos2dx/physics/CCPhysicsSetting.h +++ b/cocos2dx/physics/CCPhysicsSetting.h @@ -43,10 +43,4 @@ #define CC_USE_PHYSICS #endif -#if (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) -#include "Box2D.h" -#elif (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) -#include "chipmunk.h" -#endif - #endif // __CCPHYSICS_SETTING_H__ diff --git a/cocos2dx/physics/CCPhysicsShape.cpp b/cocos2dx/physics/CCPhysicsShape.cpp index 002c848eff..e98dff0b5c 100644 --- a/cocos2dx/physics/CCPhysicsShape.cpp +++ b/cocos2dx/physics/CCPhysicsShape.cpp @@ -25,6 +25,12 @@ #include "CCPhysicsShape.h" #ifdef CC_USE_PHYSICS +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) +#include "chipmunk.h" +#elif (CC_PHYSICS_ENGINE == CCPHYSICS_BOX2D) +#include "Box2D.h" +#endif + #include "CCPhysicsBody.h" #include "chipmunk/CCPhysicsBodyInfo.h" diff --git a/cocos2dx/physics/CCPhysicsWorld.cpp b/cocos2dx/physics/CCPhysicsWorld.cpp index d35260d4cb..08857de24b 100644 --- a/cocos2dx/physics/CCPhysicsWorld.cpp +++ b/cocos2dx/physics/CCPhysicsWorld.cpp @@ -25,6 +25,12 @@ #include "CCPhysicsWorld.h" #ifdef CC_USE_PHYSICS +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) +#include "chipmunk.h" +#elif (CC_PHYSICS_ENGINE == CCPHYSICS_BOX2D) +#include "Box2D.h" +#endif + #include "CCPhysicsBody.h" #include "CCPhysicsShape.h" #include "CCPhysicsContact.h" diff --git a/cocos2dx/physics/CCPhysicsWorld.h b/cocos2dx/physics/CCPhysicsWorld.h index 425a831442..bd06fdf661 100644 --- a/cocos2dx/physics/CCPhysicsWorld.h +++ b/cocos2dx/physics/CCPhysicsWorld.h @@ -31,6 +31,12 @@ #include "cocoa/CCObject.h" #include "cocoa/CCGeometry.h" + +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) +typedef struct cpArbiter cpArbiter; +typedef struct cpSpace cpSpace; +#endif + NS_CC_BEGIN class PhysicsBody; diff --git a/cocos2dx/physics/chipmunk/CCPhysicsBodyInfo.h b/cocos2dx/physics/chipmunk/CCPhysicsBodyInfo.h index 796f384584..0faef2eb49 100644 --- a/cocos2dx/physics/chipmunk/CCPhysicsBodyInfo.h +++ b/cocos2dx/physics/chipmunk/CCPhysicsBodyInfo.h @@ -27,6 +27,7 @@ #ifndef __CCPHYSICS_BODY_INFO_H__ #define __CCPHYSICS_BODY_INFO_H__ +#include "chipmunk.h" #include "platform/CCPlatformMacros.h" #include "cocoa/CCObject.h" diff --git a/cocos2dx/physics/chipmunk/CCPhysicsContactInfo.h b/cocos2dx/physics/chipmunk/CCPhysicsContactInfo.h index b0d123ae21..5033a2181d 100644 --- a/cocos2dx/physics/chipmunk/CCPhysicsContactInfo.h +++ b/cocos2dx/physics/chipmunk/CCPhysicsContactInfo.h @@ -27,6 +27,7 @@ #ifndef __CCPHYSICS_CONTACT_INFO_H__ #define __CCPHYSICS_CONTACT_INFO_H__ +#include "chipmunk.h" #include "platform/CCPlatformMacros.h" NS_CC_BEGIN diff --git a/cocos2dx/physics/chipmunk/CCPhysicsHelper.h b/cocos2dx/physics/chipmunk/CCPhysicsHelper.h index 07508e9b13..a026d632b7 100644 --- a/cocos2dx/physics/chipmunk/CCPhysicsHelper.h +++ b/cocos2dx/physics/chipmunk/CCPhysicsHelper.h @@ -28,6 +28,7 @@ #ifndef __CCPHYSICS_HELPER_H__ #define __CCPHYSICS_HELPER_H__ +#include "chipmunk.h" #include "platform/CCPlatformMacros.h" #include "cocoa/CCGeometry.h" diff --git a/cocos2dx/physics/chipmunk/CCPhysicsJointInfo.h b/cocos2dx/physics/chipmunk/CCPhysicsJointInfo.h index 4174c866f6..47b1fc6141 100644 --- a/cocos2dx/physics/chipmunk/CCPhysicsJointInfo.h +++ b/cocos2dx/physics/chipmunk/CCPhysicsJointInfo.h @@ -27,9 +27,8 @@ #ifndef __CCPHYSICS_JOINT_INFO_H__ #define __CCPHYSICS_JOINT_INFO_H__ - +#include "chipmunk.h" #include "platform/CCPlatformMacros.h" - NS_CC_BEGIN class PhysicsJointInfo diff --git a/cocos2dx/physics/chipmunk/CCPhysicsShapeInfo.h b/cocos2dx/physics/chipmunk/CCPhysicsShapeInfo.h index 7114bd3961..7e0be63747 100644 --- a/cocos2dx/physics/chipmunk/CCPhysicsShapeInfo.h +++ b/cocos2dx/physics/chipmunk/CCPhysicsShapeInfo.h @@ -27,10 +27,11 @@ #ifndef __CCPHYSICS_SHAPE_INFO_H__ #define __CCPHYSICS_SHAPE_INFO_H__ -#include -#include "platform/CCPlatformMacros.h" +#include #include +#include "chipmunk.h" +#include "platform/CCPlatformMacros.h" NS_CC_BEGIN diff --git a/cocos2dx/physics/chipmunk/CCPhysicsWorldInfo.h b/cocos2dx/physics/chipmunk/CCPhysicsWorldInfo.h index a1f1e66342..3bc0cf2bc4 100644 --- a/cocos2dx/physics/chipmunk/CCPhysicsWorldInfo.h +++ b/cocos2dx/physics/chipmunk/CCPhysicsWorldInfo.h @@ -27,6 +27,7 @@ #ifndef __CCPHYSICS_WORLD_INFO_H__ #define __CCPHYSICS_WORLD_INFO_H__ +#include "chipmunk.h" #include "platform/CCPlatformMacros.h" NS_CC_BEGIN diff --git a/tools/tojs/cocos2dx.ini b/tools/tojs/cocos2dx.ini index 82440540cd..87551f1c09 100644 --- a/tools/tojs/cocos2dx.ini +++ b/tools/tojs/cocos2dx.ini @@ -13,7 +13,7 @@ android_flags = -D_SIZE_T_DEFINED_ clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include clang_flags = -nostdinc -x c++ -std=c++11 -cocos_headers = -I%(cocosdir)s/cocos2dx/include -I%(cocosdir)s/cocos2dx/platform -I%(cocosdir)s/cocos2dx/platform/android -I%(cocosdir)s/cocos2dx -I%(cocosdir)s/cocos2dx/kazmath/include -I%(cocosdir)s/external/chipmunk/include/chipmunk +cocos_headers = -I%(cocosdir)s/cocos2dx/include -I%(cocosdir)s/cocos2dx/platform -I%(cocosdir)s/cocos2dx/platform/android -I%(cocosdir)s/cocos2dx -I%(cocosdir)s/cocos2dx/kazmath/include cocos_flags = -DANDROID -DCOCOS2D_JAVASCRIPT cxxgenerator_headers = -I%(cxxgeneratordir)s/targets/spidermonkey/common diff --git a/tools/tojs/cocos2dx_extension.ini b/tools/tojs/cocos2dx_extension.ini index 9056fafb16..41ecc9e5a6 100644 --- a/tools/tojs/cocos2dx_extension.ini +++ b/tools/tojs/cocos2dx_extension.ini @@ -13,7 +13,7 @@ android_flags = -D_SIZE_T_DEFINED_ clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include clang_flags = -nostdinc -x c++ -std=c++11 -cocos_headers = -I%(cocosdir)s/cocos2dx/include -I%(cocosdir)s/cocos2dx/platform -I%(cocosdir)s/cocos2dx/platform/android -I%(cocosdir)s/cocos2dx -I%(cocosdir)s/cocos2dx/kazmath/include -I%(cocosdir)s/extensions -I%(cocosdir)s/external/chipmunk/include/chipmunk +cocos_headers = -I%(cocosdir)s/cocos2dx/include -I%(cocosdir)s/cocos2dx/platform -I%(cocosdir)s/cocos2dx/platform/android -I%(cocosdir)s/cocos2dx -I%(cocosdir)s/cocos2dx/kazmath/include -I%(cocosdir)s/extensions cocos_flags = -DANDROID -DCOCOS2D_JAVASCRIPT cxxgenerator_headers = -I%(cxxgeneratordir)s/targets/spidermonkey/common From 9d6d9dcd1964b9eed6fdcccd5c917dac855682d0 Mon Sep 17 00:00:00 2001 From: CaiWenzhi Date: Tue, 17 Sep 2013 17:59:20 +0800 Subject: [PATCH 23/28] Add getDescription method --- .../GUI/BaseClasses/UIRootWidget.cpp | 5 ++ .../CocoStudio/GUI/BaseClasses/UIRootWidget.h | 5 ++ .../CocoStudio/GUI/BaseClasses/UIWidget.cpp | 5 ++ .../CocoStudio/GUI/BaseClasses/UIWidget.h | 5 ++ extensions/CocoStudio/GUI/Layouts/Layout.cpp | 5 ++ extensions/CocoStudio/GUI/Layouts/Layout.h | 4 ++ .../UIWidgets/ScrollWidget/UIDragPanel.cpp | 5 ++ .../GUI/UIWidgets/ScrollWidget/UIDragPanel.h | 5 ++ .../GUI/UIWidgets/ScrollWidget/UIListView.cpp | 6 +++ .../GUI/UIWidgets/ScrollWidget/UIListView.h | 4 ++ .../GUI/UIWidgets/ScrollWidget/UIPageView.cpp | 5 ++ .../GUI/UIWidgets/ScrollWidget/UIPageView.h | 4 ++ .../UIWidgets/ScrollWidget/UIScrollView.cpp | 5 ++ .../GUI/UIWidgets/ScrollWidget/UIScrollView.h | 5 ++ .../CocoStudio/GUI/UIWidgets/UIButton.cpp | 53 ++----------------- .../CocoStudio/GUI/UIWidgets/UIButton.h | 7 +-- .../CocoStudio/GUI/UIWidgets/UICheckBox.cpp | 5 ++ .../CocoStudio/GUI/UIWidgets/UICheckBox.h | 4 ++ .../CocoStudio/GUI/UIWidgets/UIImageView.cpp | 5 ++ .../CocoStudio/GUI/UIWidgets/UIImageView.h | 5 ++ .../CocoStudio/GUI/UIWidgets/UILabel.cpp | 6 ++- extensions/CocoStudio/GUI/UIWidgets/UILabel.h | 5 ++ .../CocoStudio/GUI/UIWidgets/UILabelAtlas.cpp | 5 ++ .../CocoStudio/GUI/UIWidgets/UILabelAtlas.h | 5 ++ .../GUI/UIWidgets/UILabelBMFont.cpp | 6 +++ .../CocoStudio/GUI/UIWidgets/UILabelBMFont.h | 5 ++ .../CocoStudio/GUI/UIWidgets/UILoadingBar.cpp | 5 ++ .../CocoStudio/GUI/UIWidgets/UILoadingBar.h | 4 ++ .../CocoStudio/GUI/UIWidgets/UISlider.cpp | 6 +++ .../CocoStudio/GUI/UIWidgets/UISlider.h | 5 ++ .../CocoStudio/GUI/UIWidgets/UITextField.cpp | 5 ++ .../CocoStudio/GUI/UIWidgets/UITextField.h | 7 ++- 32 files changed, 158 insertions(+), 53 deletions(-) diff --git a/extensions/CocoStudio/GUI/BaseClasses/UIRootWidget.cpp b/extensions/CocoStudio/GUI/BaseClasses/UIRootWidget.cpp index e2b7a69ceb..bc34480328 100644 --- a/extensions/CocoStudio/GUI/BaseClasses/UIRootWidget.cpp +++ b/extensions/CocoStudio/GUI/BaseClasses/UIRootWidget.cpp @@ -56,4 +56,9 @@ bool UIRootWidget::init() return false; } +const char* UIRootWidget::getDescription() const +{ + return "RootWidget"; +} + NS_CC_EXT_END \ No newline at end of file diff --git a/extensions/CocoStudio/GUI/BaseClasses/UIRootWidget.h b/extensions/CocoStudio/GUI/BaseClasses/UIRootWidget.h index 59ba41a498..c08a75f457 100644 --- a/extensions/CocoStudio/GUI/BaseClasses/UIRootWidget.h +++ b/extensions/CocoStudio/GUI/BaseClasses/UIRootWidget.h @@ -46,6 +46,11 @@ public: * Allocates and initializes a widget. */ static UIRootWidget* create(); + + /** + * Returns the "class name" of widget. + */ + virtual const char* getDescription() const; protected: //initializes state of widget. virtual bool init(); diff --git a/extensions/CocoStudio/GUI/BaseClasses/UIWidget.cpp b/extensions/CocoStudio/GUI/BaseClasses/UIWidget.cpp index 78584293a0..3a64c4667a 100644 --- a/extensions/CocoStudio/GUI/BaseClasses/UIWidget.cpp +++ b/extensions/CocoStudio/GUI/BaseClasses/UIWidget.cpp @@ -1158,6 +1158,11 @@ LayoutParameter* UIWidget::getLayoutParameter() return _layoutParameter; } +const char* UIWidget::getDescription() const +{ + return "Widget"; +} + /*temp action*/ void UIWidget::setActionTag(int tag) { diff --git a/extensions/CocoStudio/GUI/BaseClasses/UIWidget.h b/extensions/CocoStudio/GUI/BaseClasses/UIWidget.h index 4600eea4b7..ce3c9e59a8 100644 --- a/extensions/CocoStudio/GUI/BaseClasses/UIWidget.h +++ b/extensions/CocoStudio/GUI/BaseClasses/UIWidget.h @@ -875,6 +875,11 @@ public: virtual void onEnter(); virtual void onExit(); + /** + * Returns the "class name" of widget. + */ + virtual const char* getDescription() const; + /*temp action*/ void setActionTag(int tag); int getActionTag(); diff --git a/extensions/CocoStudio/GUI/Layouts/Layout.cpp b/extensions/CocoStudio/GUI/Layouts/Layout.cpp index 17e90a2108..a32aaf6cb6 100644 --- a/extensions/CocoStudio/GUI/Layouts/Layout.cpp +++ b/extensions/CocoStudio/GUI/Layouts/Layout.cpp @@ -425,6 +425,11 @@ const Size& Layout::getContentSize() const return _renderer->getContentSize(); } +const char* Layout::getDescription() const +{ + return "Layout"; +} + RectClippingNode::RectClippingNode(): m_pInnerStencil(NULL), _enabled(true), diff --git a/extensions/CocoStudio/GUI/Layouts/Layout.h b/extensions/CocoStudio/GUI/Layouts/Layout.h index 6b29af43a4..9afa9f539c 100644 --- a/extensions/CocoStudio/GUI/Layouts/Layout.h +++ b/extensions/CocoStudio/GUI/Layouts/Layout.h @@ -179,6 +179,10 @@ public: */ virtual const Size& getContentSize() const; + /** + * Returns the "class name" of widget. + */ + virtual const char* getDescription() const; protected: //override "init" method of widget. virtual bool init(); diff --git a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIDragPanel.cpp b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIDragPanel.cpp index 01e6403700..29e7957094 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIDragPanel.cpp +++ b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIDragPanel.cpp @@ -1383,4 +1383,9 @@ Layout* UIDragPanel::getInnerContainer() return m_pInnerContainer; } +const char* UIDragPanel::getDescription() const +{ + return "DragPanel"; +} + NS_CC_EXT_END \ No newline at end of file diff --git a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIDragPanel.h b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIDragPanel.h index a1ba2a34ae..f5b8ae1598 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIDragPanel.h +++ b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIDragPanel.h @@ -226,6 +226,11 @@ public: */ Layout* getInnerContainer(); + /** + * Returns the "class name" of widget. + */ + virtual const char* getDescription() const; + protected: virtual bool init(); virtual void initRenderer(); diff --git a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIListView.cpp b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIListView.cpp index ba292aed22..9a28f435f0 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIListView.cpp +++ b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIListView.cpp @@ -1457,4 +1457,10 @@ void UIListView::addUpdateChildEvent(Object *target, SEL_ListViewUpdateChildEven _updateChildSelector = selector; } +const char* UIListView::getDescription() const +{ + return "ListView"; +} + + NS_CC_EXT_END \ No newline at end of file diff --git a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIListView.h b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIListView.h index 24a4a9edc8..3a8eb327d6 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIListView.h +++ b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIListView.h @@ -139,6 +139,10 @@ public: /**/ virtual void update(float dt); + /** + * Returns the "class name" of widget. + */ + virtual const char* getDescription() const; protected: virtual bool init(); diff --git a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIPageView.cpp b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIPageView.cpp index e6a215d5d3..b3abef206b 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIPageView.cpp +++ b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIPageView.cpp @@ -565,4 +565,9 @@ int UIPageView::getCurPageIndex() const return _curPageIdx; } +const char* UIPageView::getDescription() const +{ + return "PageView"; +} + NS_CC_EXT_END \ No newline at end of file diff --git a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIPageView.h b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIPageView.h index 6db0a85930..eb6fc8b52d 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIPageView.h +++ b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIPageView.h @@ -134,6 +134,10 @@ public: //override "update" method of widget. virtual void update(float dt); + /** + * Returns the "class name" of widget. + */ + virtual const char* getDescription() const; protected: virtual bool addChild(UIWidget* widget); virtual bool init(); diff --git a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIScrollView.cpp b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIScrollView.cpp index 8a8e341ad6..0881f5f98a 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIScrollView.cpp +++ b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIScrollView.cpp @@ -698,4 +698,9 @@ LayoutExecutant* UIScrollView::getLayoutExecutant() const return _innerContainer->getLayoutExecutant(); } +const char* UIScrollView::getDescription() const +{ + return "ScrollView"; +} + NS_CC_EXT_END \ No newline at end of file diff --git a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIScrollView.h b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIScrollView.h index 42feb83edf..67d1995c08 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIScrollView.h +++ b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIScrollView.h @@ -183,6 +183,11 @@ public: virtual void onTouchLongClicked(const Point &touchPoint); virtual void update(float dt); + + /** + * Returns the "class name" of widget. + */ + virtual const char* getDescription() const; protected: virtual bool init(); virtual void initRenderer(); diff --git a/extensions/CocoStudio/GUI/UIWidgets/UIButton.cpp b/extensions/CocoStudio/GUI/UIWidgets/UIButton.cpp index b456613e9e..7273378c0f 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UIButton.cpp +++ b/extensions/CocoStudio/GUI/UIWidgets/UIButton.cpp @@ -431,54 +431,6 @@ void UIButton::setAnchorPoint(const Point &pt) _titleRenderer->setPosition(Point(_size.width*(0.5f-_anchorPoint.x), _size.height*(0.5f-_anchorPoint.y))); } -void UIButton::setNormalSpriteFrame(SpriteFrame *frame) -{ - if (!frame) - { - return; - } - if (_scale9Enabled) - { - dynamic_cast(_buttonNormalRenderer)->setSpriteFrame(frame); - } - else - { - dynamic_cast(_buttonNormalRenderer)->setDisplayFrame(frame); - } -} - -void UIButton::setPressedSpriteFrame(SpriteFrame *frame) -{ - if (!frame) - { - return; - } - if (_scale9Enabled) - { - dynamic_cast(_buttonClickedRenderer)->setSpriteFrame(frame); - } - else - { - dynamic_cast(_buttonClickedRenderer)->setDisplayFrame(frame); - } -} - -void UIButton::setDisabledSpriteFrame(SpriteFrame *frame) -{ - if (!frame) - { - return; - } - if (_scale9Enabled) - { - dynamic_cast(_buttonDisableRenderer)->setSpriteFrame(frame); - } - else - { - dynamic_cast(_buttonDisableRenderer)->setDisplayFrame(frame); - } -} - void UIButton::onSizeChanged() { normalTextureScaleChangedWithSize(); @@ -657,4 +609,9 @@ void UIButton::setColor(const Color3B &color) setTitleColor(_titleColor); } +const char* UIButton::getDescription() const +{ + return "Button"; +} + NS_CC_EXT_END \ No newline at end of file diff --git a/extensions/CocoStudio/GUI/UIWidgets/UIButton.h b/extensions/CocoStudio/GUI/UIWidgets/UIButton.h index ace2829c3a..f44e897a90 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UIButton.h +++ b/extensions/CocoStudio/GUI/UIWidgets/UIButton.h @@ -171,9 +171,10 @@ public: void setTitleFontName(const char* fontName); const char* getTitleFontName() const; - virtual void setNormalSpriteFrame(SpriteFrame* frame); - virtual void setPressedSpriteFrame(SpriteFrame* frame); - virtual void setDisabledSpriteFrame(SpriteFrame* frame); + /** + * Returns the "class name" of widget. + */ + virtual const char* getDescription() const; protected: virtual bool init(); virtual void initRenderer(); diff --git a/extensions/CocoStudio/GUI/UIWidgets/UICheckBox.cpp b/extensions/CocoStudio/GUI/UIWidgets/UICheckBox.cpp index ea8671f02b..12bf8a3a72 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UICheckBox.cpp +++ b/extensions/CocoStudio/GUI/UIWidgets/UICheckBox.cpp @@ -456,4 +456,9 @@ void UICheckBox::frontCrossDisabledTextureScaleChangedWithSize() } } +const char* UICheckBox::getDescription() const +{ + return "CheckBox"; +} + NS_CC_EXT_END \ No newline at end of file diff --git a/extensions/CocoStudio/GUI/UIWidgets/UICheckBox.h b/extensions/CocoStudio/GUI/UIWidgets/UICheckBox.h index 8b9658148d..b997f6f1e1 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UICheckBox.h +++ b/extensions/CocoStudio/GUI/UIWidgets/UICheckBox.h @@ -157,6 +157,10 @@ public: //override "getVirtualRenderer" method of widget. virtual Node* getVirtualRenderer(); + /** + * Returns the "class name" of widget. + */ + virtual const char* getDescription() const; protected: virtual bool init(); virtual void initRenderer(); diff --git a/extensions/CocoStudio/GUI/UIWidgets/UIImageView.cpp b/extensions/CocoStudio/GUI/UIWidgets/UIImageView.cpp index a8175661f1..23db5afef3 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UIImageView.cpp +++ b/extensions/CocoStudio/GUI/UIWidgets/UIImageView.cpp @@ -368,4 +368,9 @@ void UIImageView::imageTextureScaleChangedWithSize() } } +const char* UIImageView::getDescription() const +{ + return "ImageView"; +} + NS_CC_EXT_END \ No newline at end of file diff --git a/extensions/CocoStudio/GUI/UIWidgets/UIImageView.h b/extensions/CocoStudio/GUI/UIWidgets/UIImageView.h index 725e94f72f..89e73d3fc4 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UIImageView.h +++ b/extensions/CocoStudio/GUI/UIWidgets/UIImageView.h @@ -107,6 +107,11 @@ public: void checkDoubleClick(float dt); virtual const Size& getContentSize() const; virtual Node* getVirtualRenderer(); + + /** + * Returns the "class name" of widget. + */ + virtual const char* getDescription() const; protected: virtual void initRenderer(); virtual void onSizeChanged(); diff --git a/extensions/CocoStudio/GUI/UIWidgets/UILabel.cpp b/extensions/CocoStudio/GUI/UIWidgets/UILabel.cpp index 9c1104f3a1..0bc0da834d 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UILabel.cpp +++ b/extensions/CocoStudio/GUI/UIWidgets/UILabel.cpp @@ -221,7 +221,11 @@ void UILabel::labelScaleChangedWithSize() _labelRenderer->setScaleX(scaleX); _labelRenderer->setScaleY(scaleY); } - +} + +const char* UILabel::getDescription() const +{ + return "Label"; } NS_CC_EXT_END \ No newline at end of file diff --git a/extensions/CocoStudio/GUI/UIWidgets/UILabel.h b/extensions/CocoStudio/GUI/UIWidgets/UILabel.h index 4b6e4804c9..68b764595e 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UILabel.h +++ b/extensions/CocoStudio/GUI/UIWidgets/UILabel.h @@ -117,6 +117,11 @@ public: //override "getVirtualRenderer" method of widget. virtual Node* getVirtualRenderer(); + /** + * Returns the "class name" of widget. + */ + virtual const char* getDescription() const; + void setTextAreaSize(const Size &size); void setTextHorizontalAlignment(TextHAlignment alignment); void setTextVerticalAlignment(TextVAlignment alignment); diff --git a/extensions/CocoStudio/GUI/UIWidgets/UILabelAtlas.cpp b/extensions/CocoStudio/GUI/UIWidgets/UILabelAtlas.cpp index 779a8bc9ec..c4ba1922a7 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UILabelAtlas.cpp +++ b/extensions/CocoStudio/GUI/UIWidgets/UILabelAtlas.cpp @@ -167,4 +167,9 @@ void UILabelAtlas::labelAtlasScaleChangedWithSize() } } +const char* UILabelAtlas::getDescription() const +{ + return "LabelAtlase"; +} + NS_CC_EXT_END \ No newline at end of file diff --git a/extensions/CocoStudio/GUI/UIWidgets/UILabelAtlas.h b/extensions/CocoStudio/GUI/UIWidgets/UILabelAtlas.h index 7c1be4a670..6ff0b65a1e 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UILabelAtlas.h +++ b/extensions/CocoStudio/GUI/UIWidgets/UILabelAtlas.h @@ -87,6 +87,11 @@ public: //override "getVirtualRenderer" method of widget. virtual Node* getVirtualRenderer(); + + /** + * Returns the "class name" of widget. + */ + virtual const char* getDescription() const; protected: virtual void initRenderer(); virtual void onSizeChanged(); diff --git a/extensions/CocoStudio/GUI/UIWidgets/UILabelBMFont.cpp b/extensions/CocoStudio/GUI/UIWidgets/UILabelBMFont.cpp index 0484fa2432..c5a687015c 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UILabelBMFont.cpp +++ b/extensions/CocoStudio/GUI/UIWidgets/UILabelBMFont.cpp @@ -126,4 +126,10 @@ void UILabelBMFont::labelBMFontScaleChangedWithSize() } } +const char* UILabelBMFont::getDescription() const +{ + return "LabelBMFont"; +} + + NS_CC_EXT_END \ No newline at end of file diff --git a/extensions/CocoStudio/GUI/UIWidgets/UILabelBMFont.h b/extensions/CocoStudio/GUI/UIWidgets/UILabelBMFont.h index f85a3ee335..757a4c33b1 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UILabelBMFont.h +++ b/extensions/CocoStudio/GUI/UIWidgets/UILabelBMFont.h @@ -58,6 +58,11 @@ public: virtual void setAnchorPoint(const Point &pt); virtual const Size& getContentSize() const; virtual Node* getVirtualRenderer(); + + /** + * Returns the "class name" of widget. + */ + virtual const char* getDescription() const; protected: virtual void initRenderer(); virtual void onSizeChanged(); diff --git a/extensions/CocoStudio/GUI/UIWidgets/UILoadingBar.cpp b/extensions/CocoStudio/GUI/UIWidgets/UILoadingBar.cpp index 8873e87403..6f71980dc5 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UILoadingBar.cpp +++ b/extensions/CocoStudio/GUI/UIWidgets/UILoadingBar.cpp @@ -331,4 +331,9 @@ void UILoadingBar::setScale9Scale() dynamic_cast(_barRenderer)->setPreferredSize(Size(width, _barRendererTextureSize.height)); } +const char* UILoadingBar::getDescription() const +{ + return "LoadingBar"; +} + NS_CC_EXT_END \ No newline at end of file diff --git a/extensions/CocoStudio/GUI/UIWidgets/UILoadingBar.h b/extensions/CocoStudio/GUI/UIWidgets/UILoadingBar.h index f62dfba722..5f38f56eda 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UILoadingBar.h +++ b/extensions/CocoStudio/GUI/UIWidgets/UILoadingBar.h @@ -117,6 +117,10 @@ public: //override "getVirtualRenderer" method of widget. virtual Node* getVirtualRenderer(); + /** + * Returns the "class name" of widget. + */ + virtual const char* getDescription() const; protected: virtual void initRenderer(); virtual void onSizeChanged(); diff --git a/extensions/CocoStudio/GUI/UIWidgets/UISlider.cpp b/extensions/CocoStudio/GUI/UIWidgets/UISlider.cpp index cc70352f13..48d27f5d14 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UISlider.cpp +++ b/extensions/CocoStudio/GUI/UIWidgets/UISlider.cpp @@ -535,4 +535,10 @@ void UISlider::onPressStateChangedToDisabled() _slidBallPressedRenderer->setVisible(false); _slidBallDisabledRenderer->setVisible(true); } + +const char* UISlider::getDescription() const +{ + return "Slider"; +} + NS_CC_EXT_END \ No newline at end of file diff --git a/extensions/CocoStudio/GUI/UIWidgets/UISlider.h b/extensions/CocoStudio/GUI/UIWidgets/UISlider.h index db4749adf5..2ed5854280 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UISlider.h +++ b/extensions/CocoStudio/GUI/UIWidgets/UISlider.h @@ -180,6 +180,11 @@ public: //override "ignoreContentAdaptWithSize" method of widget. virtual void ignoreContentAdaptWithSize(bool ignore); + + /** + * Returns the "class name" of widget. + */ + virtual const char* getDescription() const; protected: virtual void initRenderer(); float getPercentWithBallPos(float location); diff --git a/extensions/CocoStudio/GUI/UIWidgets/UITextField.cpp b/extensions/CocoStudio/GUI/UIWidgets/UITextField.cpp index 1afc92be9b..170bfe96ac 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UITextField.cpp +++ b/extensions/CocoStudio/GUI/UIWidgets/UITextField.cpp @@ -584,4 +584,9 @@ Node* UITextField::getVirtualRenderer() return _textFieldRenderer; } +const char* UITextField::getDescription() const +{ + return "TextField"; +} + NS_CC_EXT_END \ No newline at end of file diff --git a/extensions/CocoStudio/GUI/UIWidgets/UITextField.h b/extensions/CocoStudio/GUI/UIWidgets/UITextField.h index 80ffa71e31..9820f05750 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UITextField.h +++ b/extensions/CocoStudio/GUI/UIWidgets/UITextField.h @@ -70,6 +70,8 @@ public: bool getInsertText(); void setDeleteBackward(bool deleteBackward); bool getDeleteBackward(); + + protected: bool m_bMaxLengthEnabled; int m_nMaxLength; @@ -131,7 +133,10 @@ public: virtual void setAnchorPoint(const Point &pt); virtual void setColor(const Color3B &color); virtual void setOpacity(int opacity); - + /** + * Returns the "class name" of widget. + */ + virtual const char* getDescription() const; /*compatibel*/ /** * These methods will be removed From ddaff9cce6cd63de3c9d5099ecc54f43cc3dd175 Mon Sep 17 00:00:00 2001 From: minggo Date: Tue, 17 Sep 2013 19:24:30 +0800 Subject: [PATCH 24/28] make cocos2dx depend chipmunk --- CocosDenshion/android/Android.mk | 3 +-- cocos2dx/Android.mk | 2 ++ samples/Cpp/AssetsManagerTest/proj.android/jni/Android.mk | 2 -- samples/Cpp/HelloCpp/proj.android/jni/Android.mk | 3 +-- samples/Cpp/SimpleGame/proj.android/jni/Android.mk | 3 +-- samples/Cpp/TestCpp/proj.android/jni/Android.mk | 2 -- samples/Javascript/CocosDragonJS/proj.android/jni/Android.mk | 2 -- samples/Javascript/CrystalCraze/proj.android/jni/Android.mk | 2 -- samples/Javascript/MoonWarriors/proj.android/jni/Android.mk | 2 -- samples/Javascript/TestJavascript/proj.android/jni/Android.mk | 2 -- .../Javascript/WatermelonWithMe/proj.android/jni/Android.mk | 2 -- samples/Lua/TestLua/proj.android/jni/Android.mk | 2 -- template/multi-platform-cpp/proj.android/jni/Android.mk | 2 -- template/multi-platform-js/proj.android/jni/Android.mk | 2 -- template/multi-platform-lua/proj.android/jni/Android.mk | 2 -- 15 files changed, 5 insertions(+), 28 deletions(-) diff --git a/CocosDenshion/android/Android.mk b/CocosDenshion/android/Android.mk index 996a07f321..2704964c93 100644 --- a/CocosDenshion/android/Android.mk +++ b/CocosDenshion/android/Android.mk @@ -16,8 +16,7 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH)/../include \ $(LOCAL_PATH)/../../cocos2dx \ $(LOCAL_PATH)/../../cocos2dx/include \ $(LOCAL_PATH)/../../cocos2dx/kazmath/include \ - $(LOCAL_PATH)/../../cocos2dx/platform/android \ - $(LOCAL_PATH)/../../external/chipmunk/include/chipmunk + $(LOCAL_PATH)/../../cocos2dx/platform/android LOCAL_CFLAGS += -Wno-psabi LOCAL_EXPORT_CFLAGS += -Wno-psabi diff --git a/cocos2dx/Android.mk b/cocos2dx/Android.mk index 9dddcde603..0033dd4975 100644 --- a/cocos2dx/Android.mk +++ b/cocos2dx/Android.mk @@ -193,6 +193,7 @@ LOCAL_WHOLE_STATIC_LIBRARIES += cocos_libxml2_static LOCAL_WHOLE_STATIC_LIBRARIES += cocos_libtiff_static LOCAL_WHOLE_STATIC_LIBRARIES += cocos_libwebp_static LOCAL_WHOLE_STATIC_LIBRARIES += cocos_freetype2_static +LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static # define the macro to compile through support/zip_support/ioapi.c LOCAL_CFLAGS := -Wno-psabi -DUSE_FILE32API @@ -205,3 +206,4 @@ $(call import-module,libpng) $(call import-module,libtiff) $(call import-module,libwebp) $(call import-module,libfreetype2) +$(call import-module,external/chipmunk) diff --git a/samples/Cpp/AssetsManagerTest/proj.android/jni/Android.mk b/samples/Cpp/AssetsManagerTest/proj.android/jni/Android.mk index 5ffbbfbf2f..a85ad02ade 100644 --- a/samples/Cpp/AssetsManagerTest/proj.android/jni/Android.mk +++ b/samples/Cpp/AssetsManagerTest/proj.android/jni/Android.mk @@ -13,7 +13,6 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static -LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static LOCAL_WHOLE_STATIC_LIBRARIES += spidermonkey_static LOCAL_WHOLE_STATIC_LIBRARIES += scriptingcore-spidermonkey LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dxandroid_static @@ -24,7 +23,6 @@ include $(BUILD_SHARED_LIBRARY) $(call import-module,cocos2dx) $(call import-module,CocosDenshion/android) -$(call import-module,external/chipmunk) $(call import-module,scripting/javascript/spidermonkey-android) $(call import-module,scripting/javascript/bindings) $(call import-module,cocos2dx/platform/android) diff --git a/samples/Cpp/HelloCpp/proj.android/jni/Android.mk b/samples/Cpp/HelloCpp/proj.android/jni/Android.mk index a59a21c7dd..13989f3cf1 100644 --- a/samples/Cpp/HelloCpp/proj.android/jni/Android.mk +++ b/samples/Cpp/HelloCpp/proj.android/jni/Android.mk @@ -12,11 +12,10 @@ LOCAL_SRC_FILES := hellocpp/main.cpp \ LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes -LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static cocos2dxandroid_static cocosdenshion_static chipmunk_static +LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static cocos2dxandroid_static cocosdenshion_static include $(BUILD_SHARED_LIBRARY) $(call import-module,cocos2dx) $(call import-module,CocosDenshion/android) $(call import-module,cocos2dx/platform/android) -$(call import-module,external/chipmunk) diff --git a/samples/Cpp/SimpleGame/proj.android/jni/Android.mk b/samples/Cpp/SimpleGame/proj.android/jni/Android.mk index d50779e5c4..e3f5b5c1ef 100644 --- a/samples/Cpp/SimpleGame/proj.android/jni/Android.mk +++ b/samples/Cpp/SimpleGame/proj.android/jni/Android.mk @@ -13,7 +13,7 @@ LOCAL_SRC_FILES := hellocpp/main.cpp \ LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes -LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static cocos2dxandroid_static cocosdenshion_static chipmunk_static +LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static cocos2dxandroid_static cocosdenshion_static include $(BUILD_SHARED_LIBRARY) @@ -21,4 +21,3 @@ $(call import-module,CocosDenshion/android) $(call import-module,cocos2dx) $(call import-module,extensions) $(call import-module,cocos2dx/platform/android) -$(call import-module,external/chipmunk) diff --git a/samples/Cpp/TestCpp/proj.android/jni/Android.mk b/samples/Cpp/TestCpp/proj.android/jni/Android.mk index 210fef8fa1..98de0c8956 100644 --- a/samples/Cpp/TestCpp/proj.android/jni/Android.mk +++ b/samples/Cpp/TestCpp/proj.android/jni/Android.mk @@ -14,7 +14,6 @@ LOCAL_WHOLE_STATIC_LIBRARIES := cocos_testcpp_common LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_static LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static -LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dxandroid_static @@ -26,5 +25,4 @@ $(call import-module,cocos2dx/platform/third_party/android/prebuilt/libcurl) $(call import-module,CocosDenshion/android) $(call import-module,extensions) $(call import-module,external/Box2D) -$(call import-module,external/chipmunk) $(call import-module,cocos2dx/platform/android) diff --git a/samples/Javascript/CocosDragonJS/proj.android/jni/Android.mk b/samples/Javascript/CocosDragonJS/proj.android/jni/Android.mk index d2615a756b..0ab251941c 100644 --- a/samples/Javascript/CocosDragonJS/proj.android/jni/Android.mk +++ b/samples/Javascript/CocosDragonJS/proj.android/jni/Android.mk @@ -13,7 +13,6 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static -LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static LOCAL_WHOLE_STATIC_LIBRARIES += spidermonkey_static LOCAL_WHOLE_STATIC_LIBRARIES += scriptingcore-spidermonkey LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dxandroid_static @@ -24,7 +23,6 @@ include $(BUILD_SHARED_LIBRARY) $(call import-module,cocos2dx) $(call import-module,CocosDenshion/android) -$(call import-module,external/chipmunk) $(call import-module,scripting/javascript/spidermonkey-android) $(call import-module,scripting/javascript/bindings) $(call import-module,cocos2dx/platform/android) diff --git a/samples/Javascript/CrystalCraze/proj.android/jni/Android.mk b/samples/Javascript/CrystalCraze/proj.android/jni/Android.mk index de9f96689d..12575cad7a 100644 --- a/samples/Javascript/CrystalCraze/proj.android/jni/Android.mk +++ b/samples/Javascript/CrystalCraze/proj.android/jni/Android.mk @@ -13,7 +13,6 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static -LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static LOCAL_WHOLE_STATIC_LIBRARIES += spidermonkey_static LOCAL_WHOLE_STATIC_LIBRARIES += scriptingcore-spidermonkey LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dxandroid_static @@ -24,7 +23,6 @@ include $(BUILD_SHARED_LIBRARY) $(call import-module,cocos2dx) $(call import-module,CocosDenshion/android) -$(call import-module,external/chipmunk) $(call import-module,scripting/javascript/spidermonkey-android) $(call import-module,scripting/javascript/bindings) $(call import-module,cocos2dx/platform/android) diff --git a/samples/Javascript/MoonWarriors/proj.android/jni/Android.mk b/samples/Javascript/MoonWarriors/proj.android/jni/Android.mk index 5567a3879c..1cdcb62cd6 100644 --- a/samples/Javascript/MoonWarriors/proj.android/jni/Android.mk +++ b/samples/Javascript/MoonWarriors/proj.android/jni/Android.mk @@ -13,7 +13,6 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static -LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static LOCAL_WHOLE_STATIC_LIBRARIES += spidermonkey_static LOCAL_WHOLE_STATIC_LIBRARIES += scriptingcore-spidermonkey LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dxandroid_static @@ -24,7 +23,6 @@ include $(BUILD_SHARED_LIBRARY) $(call import-module,cocos2dx) $(call import-module,CocosDenshion/android) -$(call import-module,external/chipmunk) $(call import-module,scripting/javascript/spidermonkey-android) $(call import-module,scripting/javascript/bindings) $(call import-module,cocos2dx/platform/android) diff --git a/samples/Javascript/TestJavascript/proj.android/jni/Android.mk b/samples/Javascript/TestJavascript/proj.android/jni/Android.mk index a5e5f60ec0..88ce0ac197 100644 --- a/samples/Javascript/TestJavascript/proj.android/jni/Android.mk +++ b/samples/Javascript/TestJavascript/proj.android/jni/Android.mk @@ -14,7 +14,6 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static -LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static LOCAL_WHOLE_STATIC_LIBRARIES += spidermonkey_static LOCAL_WHOLE_STATIC_LIBRARIES += scriptingcore-spidermonkey LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dxandroid_static @@ -25,7 +24,6 @@ include $(BUILD_SHARED_LIBRARY) $(call import-module,cocos2dx) $(call import-module,CocosDenshion/android) -$(call import-module,external/chipmunk) $(call import-module,extensions) $(call import-module,scripting/javascript/spidermonkey-android) $(call import-module,scripting/javascript/bindings) diff --git a/samples/Javascript/WatermelonWithMe/proj.android/jni/Android.mk b/samples/Javascript/WatermelonWithMe/proj.android/jni/Android.mk index 33fdf59cf3..76b8e7fd1e 100644 --- a/samples/Javascript/WatermelonWithMe/proj.android/jni/Android.mk +++ b/samples/Javascript/WatermelonWithMe/proj.android/jni/Android.mk @@ -13,7 +13,6 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static -LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static LOCAL_WHOLE_STATIC_LIBRARIES += spidermonkey_static LOCAL_WHOLE_STATIC_LIBRARIES += scriptingcore-spidermonkey LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dxandroid_static @@ -24,7 +23,6 @@ include $(BUILD_SHARED_LIBRARY) $(call import-module,cocos2dx) $(call import-module,CocosDenshion/android) -$(call import-module,external/chipmunk) $(call import-module,scripting/javascript/spidermonkey-android) $(call import-module,scripting/javascript/bindings) $(call import-module,cocos2dx/platform/android) diff --git a/samples/Lua/TestLua/proj.android/jni/Android.mk b/samples/Lua/TestLua/proj.android/jni/Android.mk index 306ca7e320..189dd81468 100644 --- a/samples/Lua/TestLua/proj.android/jni/Android.mk +++ b/samples/Lua/TestLua/proj.android/jni/Android.mk @@ -18,7 +18,6 @@ LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static LOCAL_WHOLE_STATIC_LIBRARIES += cocos_lua_static LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static -LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dxandroid_static @@ -30,5 +29,4 @@ $(call import-module,scripting/lua/proj.android) $(call import-module,cocos2dx/platform/third_party/android/prebuilt/libcurl) $(call import-module,extensions) $(call import-module,external/Box2D) -$(call import-module,external/chipmunk) $(call import-module,cocos2dx/platform/android) diff --git a/template/multi-platform-cpp/proj.android/jni/Android.mk b/template/multi-platform-cpp/proj.android/jni/Android.mk index 329ebc0e4f..5430a288ba 100644 --- a/template/multi-platform-cpp/proj.android/jni/Android.mk +++ b/template/multi-platform-cpp/proj.android/jni/Android.mk @@ -15,7 +15,6 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_static LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static -LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dxandroid_static @@ -26,5 +25,4 @@ $(call import-module,cocos2dx/platform/third_party/android/prebuilt/libcurl) $(call import-module,CocosDenshion/android) $(call import-module,extensions) $(call import-module,external/Box2D) -$(call import-module,external/chipmunk) $(call import-module,cocos2dx/platform/android) diff --git a/template/multi-platform-js/proj.android/jni/Android.mk b/template/multi-platform-js/proj.android/jni/Android.mk index 2b70298a7c..09cabaea59 100644 --- a/template/multi-platform-js/proj.android/jni/Android.mk +++ b/template/multi-platform-js/proj.android/jni/Android.mk @@ -13,7 +13,6 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static -LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static LOCAL_WHOLE_STATIC_LIBRARIES += spidermonkey_static LOCAL_WHOLE_STATIC_LIBRARIES += scriptingcore-spidermonkey LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dxandroid_static @@ -24,7 +23,6 @@ include $(BUILD_SHARED_LIBRARY) $(call import-module,cocos2dx) $(call import-module,CocosDenshion/android) -$(call import-module,external/chipmunk) $(call import-module,scripting/javascript/spidermonkey-android) $(call import-module,scripting/javascript/bindings) $(call import-module,cocos2dx/platform/android) diff --git a/template/multi-platform-lua/proj.android/jni/Android.mk b/template/multi-platform-lua/proj.android/jni/Android.mk index 3cab251681..05362699b5 100644 --- a/template/multi-platform-lua/proj.android/jni/Android.mk +++ b/template/multi-platform-lua/proj.android/jni/Android.mk @@ -19,7 +19,6 @@ LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static LOCAL_WHOLE_STATIC_LIBRARIES += cocos_lua_static LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static -LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dxandroid_static @@ -31,5 +30,4 @@ $(call import-module,scripting/lua/proj.android) $(call import-module,cocos2dx/platform/third_party/android/prebuilt/libcurl) $(call import-module,extensions) $(call import-module,external/Box2D) -$(call import-module,external/chipmunk) $(call import-module,cocos2dx/platform/android) From 9d07b2c1d24a954767de2c0325deed66eecd4607 Mon Sep 17 00:00:00 2001 From: CaiWenzhi Date: Tue, 17 Sep 2013 20:13:23 +0800 Subject: [PATCH 25/28] Temp commit --- .../UIWidgets/ScrollWidget/UIDragPanel.cpp | 575 +++++++----------- .../GUI/UIWidgets/ScrollWidget/UIDragPanel.h | 185 ++---- .../GUI/UIWidgets/ScrollWidget/UIListView.cpp | 26 +- .../GUI/UIWidgets/ScrollWidget/UIListView.h | 26 +- .../GUI/UIWidgets/ScrollWidget/UIPageView.cpp | 14 +- .../GUI/UIWidgets/ScrollWidget/UIPageView.h | 17 +- .../CocoStudio/GUI/UIWidgets/UICheckBox.cpp | 2 +- .../CocoStudio/GUI/UIWidgets/UICheckBox.h | 2 +- .../CocoStudio/GUI/UIWidgets/UISlider.cpp | 2 +- .../CocoStudio/GUI/UIWidgets/UISlider.h | 2 +- .../CocoStudio/GUI/UIWidgets/UITextField.cpp | 52 +- .../CocoStudio/GUI/UIWidgets/UITextField.h | 37 +- .../UICheckBoxTest/UICheckBoxTest.cpp | 2 +- .../UIDragPanelTest/UIDragPanelTest.cpp | 168 +++-- .../UIDragPanelTest/UIDragPanelTest.h | 18 +- .../UIListViewTest/UIListViewTest.cpp | 126 ++-- .../UIListViewTest/UIListViewTest.h | 8 +- .../UIPageViewTest/UIPageViewTest.cpp | 24 +- .../UIPageViewTest/UIPageViewTest.h | 2 +- .../UISliderTest/UISliderTest.cpp | 4 +- .../UITextFieldTest/UITextFieldTest.cpp | 194 +++--- .../UITextFieldTest/UITextFieldTest.h | 15 +- 22 files changed, 634 insertions(+), 867 deletions(-) diff --git a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIDragPanel.cpp b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIDragPanel.cpp index 29e7957094..d533fcd6c2 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIDragPanel.cpp +++ b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIDragPanel.cpp @@ -28,67 +28,35 @@ NS_CC_EXT_BEGIN UIDragPanel::UIDragPanel() -: m_pInnerContainer(NULL) -, m_bTouchPressed(false) -, m_bTouchMoved(false) -, m_bTouchReleased(false) -, m_bTouchCanceld(false) -, m_touchStartNodeSpace(Point::ZERO) -, m_touchStartWorldSpace(Point::ZERO) -, m_touchEndWorldSpace(Point::ZERO) -, m_fSlidTime(0.0f) -, m_eMoveType(DRAGPANEL_MOVE_TYPE_AUTOMOVE) -, m_fAutoMoveDuration(0.5f) -, m_fAutoMoveEaseRate(2.0f) -, m_eBerthDirection(DRAGPANEL_BERTH_DIR_NONE) -, m_pBerthToLeftListener(NULL) -, m_pfnBerthToLeftSelector(NULL) -, m_pBerthToRightListener(NULL) -, m_pfnBerthToRightSelector(NULL) -, m_pBerthToTopListener(NULL) -, m_pfnBerthToTopSelector(NULL) -, m_pBerthToBottomListener(NULL) -, m_pfnBerthToBottomSelector(NULL) -, m_pBerthToLeftBottomListener(NULL) -, m_pfnBerthToLeftBottomSelector(NULL) -, m_pBerthToLeftTopListener(NULL) -, m_pfnBerthToLeftTopSelector(NULL) -, m_pBerthToRightBottomListener(NULL) -, m_pfnBerthToRightBottomSelector(NULL) -, m_pBerthToRightTopListener(NULL) -, m_pfnBerthToRightTopSelector(NULL) -, m_bBounceEnable(false) -, m_eBounceDirection(DRAGPANEL_BOUNCE_DIR_NONE) -, m_fBounceDuration(0.5f) -, m_fBounceEaseRate(2.0f) -, m_pBounceOverListener(NULL) -, m_pfnBounceOverSelector(NULL) -, m_pBounceToLeftBottomListener(NULL) -, m_pfnBounceToLeftBottomSelector(NULL) -, m_pBounceToLeftTopListener(NULL) -, m_pfnBounceToLeftTopSelector(NULL) -, m_pBounceToRightBottomListener(NULL) -, m_pfnBounceToRightBottomSelector(NULL) -, m_pBounceToRightTopListener(NULL) -, m_pfnBounceToRightTopSelector(NULL) -, m_pBounceToLeftListener(NULL) -, m_pfnBounceToLeftSelector(NULL) -, m_pBounceToTopListener(NULL) -, m_pfnBounceToTopSelector(NULL) -, m_pBounceToRightListener(NULL) -, m_pfnBounceToRightSelector(NULL) -, m_pBounceToBottomListener(NULL) -, m_pfnBounceToBottomSelector(NULL) -, m_bRunningAction(false) -, m_nActionType(0) -, m_pActionWidget(NULL) -, m_fDuration(0.0f) -, m_elapsed(0.0f) -, m_bFirstTick(false) -, m_positionDelta(Point::ZERO) -, m_startPosition(Point::ZERO) -, m_previousPosition(Point::ZERO) -, m_endPosition(Point::ZERO) +: _innerContainer(NULL) +, _touchPressed(false) +, _touchMoved(false) +, _touchReleased(false) +, _touchCanceld(false) +, _touchStartNodeSpace(Point::ZERO) +, _touchStartWorldSpace(Point::ZERO) +, _touchEndWorldSpace(Point::ZERO) +, _slidTime(0.0f) +, _moveType(DRAGPANEL_MOVE_TYPE_AUTOMOVE) +, _autoMoveDuration(0.5f) +, _autoMoveEaseRate(2.0f) +, _eventLister(NULL) +, _eventSelector(NULL) +, _berthDirection(DRAGPANEL_BERTH_DIR_NONE) +, _bounceEnable(false) +, _bounceDirection(DRAGPANEL_BOUNCE_DIR_NONE) +, _bounceDuration(0.5f) +, _bounceEaseRate(2.0f) +, _runningAction(false) +, _actionType(0) +, _actionWidget(NULL) +, _duration(0.0f) +, _elapsed(0.0f) +, _firstTick(false) +, _positionDelta(Point::ZERO) +, _startPosition(Point::ZERO) +, _previousPosition(Point::ZERO) +, _endPosition(Point::ZERO) { } @@ -126,8 +94,8 @@ void UIDragPanel::initRenderer() { Layout::initRenderer(); - m_pInnerContainer = Layout::create(); - Layout::addChild(m_pInnerContainer); + _innerContainer = Layout::create(); + Layout::addChild(_innerContainer); } @@ -139,7 +107,7 @@ void UIDragPanel::releaseResoures() _renderer->removeFromParentAndCleanup(true); _renderer->release(); - Layout::removeChild(m_pInnerContainer); + Layout::removeChild(_innerContainer); _children->release(); } @@ -176,7 +144,7 @@ void UIDragPanel::onTouchLongClicked(const Point &touchPoint) void UIDragPanel::update(float dt) { // widget action - if (m_bRunningAction) + if (_runningAction) { if (actionIsDone()) { @@ -194,14 +162,14 @@ void UIDragPanel::update(float dt) bool UIDragPanel::addChild(UIWidget *widget) { - m_pInnerContainer->addChild(widget); + _innerContainer->addChild(widget); return true; } bool UIDragPanel::removeChild(UIWidget *child) { bool value = false; - if (m_pInnerContainer->removeChild(child)) + if (_innerContainer->removeChild(child)) { value = true; } @@ -211,28 +179,28 @@ bool UIDragPanel::removeChild(UIWidget *child) void UIDragPanel::removeAllChildren() { - m_pInnerContainer->removeAllChildren(); + _innerContainer->removeAllChildren(); } Array* UIDragPanel::getChildren() { - return m_pInnerContainer->getChildren(); + return _innerContainer->getChildren(); } void UIDragPanel::onSizeChanged() { Layout::onSizeChanged(); - Size innerSize = m_pInnerContainer->getSize(); + Size innerSize = _innerContainer->getSize(); float orginInnerSizeWidth = innerSize.width; float orginInnerSizeHeight = innerSize.height; float innerSizeWidth = MAX(orginInnerSizeWidth, _size.width); float innerSizeHeight = MAX(orginInnerSizeHeight, _size.height); - m_pInnerContainer->setSize(Size(innerSizeWidth, innerSizeHeight)); + _innerContainer->setSize(Size(innerSizeWidth, innerSizeHeight)); } const Size& UIDragPanel::getInnerContainerSize() const { - return m_pInnerContainer->getContentSize(); + return _innerContainer->getContentSize(); } void UIDragPanel::setInnerContainerSize(const cocos2d::Size &size) @@ -255,20 +223,20 @@ void UIDragPanel::setInnerContainerSize(const cocos2d::Size &size) { innerSizeHeight = size.height; } - m_pInnerContainer->setSize(Size(innerSizeWidth, innerSizeHeight)); - m_pInnerContainer->setPosition(Point(0, _size.height - m_pInnerContainer->getSize().height)); + _innerContainer->setSize(Size(innerSizeWidth, innerSizeHeight)); + _innerContainer->setPosition(Point(0, _size.height - _innerContainer->getSize().height)); } const Point& UIDragPanel::getInnerContainerPosition() const { - return m_pInnerContainer->getPosition(); + return _innerContainer->getPosition(); } void UIDragPanel::setInnerContainerPosition(const Point &point, bool animated) { - Point delta = point - m_pInnerContainer->getPosition(); + Point delta = point - _innerContainer->getPosition(); -// Point delta = ccpSub(point, m_pInnerContainer->getPosition()); +// Point delta = ccpSub(point, _innerContainer->getPosition()); setInnerContainerOffset(delta, animated); } @@ -282,8 +250,8 @@ void UIDragPanel::setInnerContainerOffset(const Point &offset, bool animated) { delta = calculateToBoundaryDeltaPosition(delta); } - actionStartWithWidget(m_pInnerContainer); - moveByWithDuration(m_fAutoMoveDuration, delta); + actionStartWithWidget(_innerContainer); + moveByWithDuration(_autoMoveDuration, delta); } else { @@ -312,18 +280,18 @@ void UIDragPanel::handlePressLogic(const Point &touchPoint) // check inner rect < drag panel rect if (checkContainInnerRect()) { - m_bTouchPressed = false; + _touchPressed = false; return; } - m_bTouchPressed = true; - m_bTouchMoved = false; - m_bTouchReleased = false; - m_bTouchCanceld = false; + _touchPressed = true; + _touchMoved = false; + _touchReleased = false; + _touchCanceld = false; - if (m_bRunningAction) + if (_runningAction) { - switch (m_eMoveType) + switch (_moveType) { case DRAGPANEL_MOVE_TYPE_AUTOMOVE: stopAutoMove(); @@ -331,7 +299,7 @@ void UIDragPanel::handlePressLogic(const Point &touchPoint) break; case DRAGPANEL_MOVE_TYPE_BOUNCE: - m_bTouchPressed = false; + _touchPressed = false; break; default: @@ -340,39 +308,39 @@ void UIDragPanel::handlePressLogic(const Point &touchPoint) } Point nsp = _renderer->convertToNodeSpace(touchPoint); - m_touchStartNodeSpace = nsp; + _touchStartNodeSpace = nsp; - m_touchStartWorldSpace = touchPoint; + _touchStartWorldSpace = touchPoint; } void UIDragPanel::handleMoveLogic(const Point &touchPoint) { - if (!m_bTouchPressed) + if (!_touchPressed) { return; } // check touch out of drag panel boundary - if (m_bTouchCanceld) + if (_touchCanceld) { return; } - m_bTouchMoved = true; + _touchMoved = true; Point nsp = _renderer->convertToNodeSpace(touchPoint); - Point delta = nsp - m_touchStartNodeSpace; -// Point delta = ccpSub(nsp, m_touchStartNodeSpace); - m_touchStartNodeSpace = nsp; + Point delta = nsp - _touchStartNodeSpace; +// Point delta = ccpSub(nsp, _touchStartNodeSpace); + _touchStartNodeSpace = nsp; // reset berth dir to none - if (!m_bBounceEnable) + if (!_bounceEnable) { - m_eBerthDirection = DRAGPANEL_BERTH_DIR_NONE; + _berthDirection = DRAGPANEL_BERTH_DIR_NONE; } // check will berth (bounce disable) - if (!m_bBounceEnable) + if (!_bounceEnable) { if (checkToBoundaryWithDeltaPosition(delta)) { @@ -382,16 +350,16 @@ void UIDragPanel::handleMoveLogic(const Point &touchPoint) // move moveWithDelta(delta); // check bounce or berth - if (m_bBounceEnable) + if (_bounceEnable) { // bounce if (!hitTest(touchPoint)) { - m_bTouchMoved = false; + _touchMoved = false; if (checkNeedBounce()) { - m_bTouchCanceld = true; + _touchCanceld = true; startBounce(); } } @@ -408,25 +376,25 @@ void UIDragPanel::handleMoveLogic(const Point &touchPoint) void UIDragPanel::handleReleaseLogic(const Point &touchPoint) { - if (!m_bTouchPressed) + if (!_touchPressed) { return; } - m_bTouchPressed = false; - m_bTouchMoved = false; - m_bTouchReleased = true; - m_bTouchCanceld = false; + _touchPressed = false; + _touchMoved = false; + _touchReleased = true; + _touchCanceld = false; // check touch out of drag panel boundary - if (m_bTouchCanceld) + if (_touchCanceld) { return; } if (hitTest(touchPoint)) { - m_touchEndWorldSpace = touchPoint; + _touchEndWorldSpace = touchPoint; startAutoMove(); } } @@ -467,9 +435,9 @@ void UIDragPanel::interceptTouchEvent(int handleState, UIWidget *sender, const P void UIDragPanel::recordSlidTime(float dt) { - if (m_bTouchPressed) + if (_touchPressed) { - m_fSlidTime += dt; + _slidTime += dt; } } @@ -478,8 +446,8 @@ bool UIDragPanel::checkContainInnerRect() { float width = _size.width; float height = _size.height; - float innerWidth = m_pInnerContainer->getSize().width; - float innerHeight = m_pInnerContainer->getSize().height; + float innerWidth = _innerContainer->getSize().width; + float innerHeight = _innerContainer->getSize().height; if (innerWidth <= width && innerHeight <= height) { @@ -492,15 +460,15 @@ bool UIDragPanel::checkContainInnerRect() // move void UIDragPanel::moveWithDelta(const Point &delta) { - Point newPos = m_pInnerContainer->getPosition() + delta; -// Point newPos = ccpAdd(m_pInnerContainer->getPosition(), delta); - m_pInnerContainer->setPosition(newPos); + Point newPos = _innerContainer->getPosition() + delta; +// Point newPos = ccpAdd(_innerContainer->getPosition(), delta); + _innerContainer->setPosition(newPos); } // auto move void UIDragPanel::autoMove() { - if (m_bBounceEnable) + if (_bounceEnable) { if (checkNeedBounce()) { @@ -517,47 +485,47 @@ void UIDragPanel::autoMoveOver() if (checkBerth()) { berthEvent(); - m_eBerthDirection = DRAGPANEL_BERTH_DIR_NONE; + _berthDirection = DRAGPANEL_BERTH_DIR_NONE; } } void UIDragPanel::startAutoMove() { - m_eMoveType = DRAGPANEL_MOVE_TYPE_AUTOMOVE; + _moveType = DRAGPANEL_MOVE_TYPE_AUTOMOVE; actionStop(); - Point delta = m_touchEndWorldSpace - m_touchStartWorldSpace; -// Point delta = ccpSub(m_touchEndWorldSpace, m_touchStartWorldSpace); - delta.x /= m_fSlidTime * 60; - delta.y /= m_fSlidTime * 60; - m_fSlidTime = 0.0; + Point delta = _touchEndWorldSpace - _touchStartWorldSpace; +// Point delta = ccpSub(m_touchEndWorldSpace, _touchStartWorldSpace); + delta.x /= _slidTime * 60; + delta.y /= _slidTime * 60; + _slidTime = 0.0; // bounceEnable is disable - if (!m_bBounceEnable) + if (!_bounceEnable) { if (checkToBoundaryWithDeltaPosition(delta)) { delta = calculateToBoundaryDeltaPosition(delta); } } - actionStartWithWidget(m_pInnerContainer); - moveByWithDuration(m_fAutoMoveDuration, delta); + actionStartWithWidget(_innerContainer); + moveByWithDuration(_autoMoveDuration, delta); } void UIDragPanel::stopAutoMove() { - m_eMoveType = DRAGPANEL_MOVE_TYPE_NONE; + _moveType = DRAGPANEL_MOVE_TYPE_NONE; } void UIDragPanel::setAutoMoveDuration(float duration) { - m_fAutoMoveDuration = duration; + _autoMoveDuration = duration; } void UIDragPanel::setAutoMoveEaseRate(float rate) { - m_fAutoMoveEaseRate = rate; + _autoMoveEaseRate = rate; } // berth @@ -566,10 +534,10 @@ void UIDragPanel::setAutoMoveEaseRate(float rate) bool UIDragPanel::checkToBoundaryWithDeltaPosition(const Point& delta) { - float innerLeft = m_pInnerContainer->getLeftInParent(); - float innerTop = m_pInnerContainer->getTopInParent(); - float innerRight = m_pInnerContainer->getRightInParent(); - float innerBottom = m_pInnerContainer->getBottomInParent(); + float innerLeft = _innerContainer->getLeftInParent(); + float innerTop = _innerContainer->getTopInParent(); + float innerRight = _innerContainer->getRightInParent(); + float innerBottom = _innerContainer->getBottomInParent(); float left = 0; float top = _size.height; @@ -629,10 +597,10 @@ bool UIDragPanel::checkToBoundaryWithDeltaPosition(const Point& delta) Point UIDragPanel::calculateToBoundaryDeltaPosition(const Point& paramDelta) { - float innerLeft = m_pInnerContainer->getLeftInParent(); - float innerTop = m_pInnerContainer->getTopInParent(); - float innerRight = m_pInnerContainer->getRightInParent(); - float innerBottom = m_pInnerContainer->getBottomInParent(); + float innerLeft = _innerContainer->getLeftInParent(); + float innerTop = _innerContainer->getTopInParent(); + float innerRight = _innerContainer->getRightInParent(); + float innerBottom = _innerContainer->getBottomInParent(); float left = 0; float top = _size.height; @@ -683,16 +651,16 @@ Point UIDragPanel::calculateToBoundaryDeltaPosition(const Point& paramDelta) bool UIDragPanel::isBerth() { - return m_eBerthDirection != DRAGPANEL_BERTH_DIR_NONE; + return _berthDirection != DRAGPANEL_BERTH_DIR_NONE; } // check berth bool UIDragPanel::checkBerth() { - float innerLeft = m_pInnerContainer->getLeftInParent(); - float innerTop = m_pInnerContainer->getTopInParent(); - float innerRight = m_pInnerContainer->getRightInParent(); - float innerBottom = m_pInnerContainer->getBottomInParent(); + float innerLeft = _innerContainer->getLeftInParent(); + float innerTop = _innerContainer->getTopInParent(); + float innerRight = _innerContainer->getRightInParent(); + float innerBottom = _innerContainer->getBottomInParent(); float left = 0; float top = _size.height; @@ -701,38 +669,38 @@ bool UIDragPanel::checkBerth() if (innerLeft == left && innerBottom == bottom) // left bottom { - m_eBerthDirection = DRAGPANEL_BERTH_DIR_LEFTBOTTOM; + _berthDirection = DRAGPANEL_BERTH_DIR_LEFTBOTTOM; } else if (innerLeft == left && innerTop == top) // left top { - m_eBerthDirection = DRAGPANEL_BERTH_DIR_LFETTOP; + _berthDirection = DRAGPANEL_BERTH_DIR_LFETTOP; } else if (innerRight == right && innerBottom == bottom) // right bottom { - m_eBerthDirection = DRAGPANEL_BERTH_DIR_RIGHTBOTTOM; + _berthDirection = DRAGPANEL_BERTH_DIR_RIGHTBOTTOM; } else if (innerRight == right && innerTop == top) // right top { - m_eBerthDirection = DRAGPANEL_BERTH_DIR_RIGHTTOP; + _berthDirection = DRAGPANEL_BERTH_DIR_RIGHTTOP; } else if (innerLeft == left) // left { - m_eBerthDirection = DRAGPANEL_BERTH_DIR_LEFT; + _berthDirection = DRAGPANEL_BERTH_DIR_LEFT; } else if (innerRight == right) // right { - m_eBerthDirection = DRAGPANEL_BERTH_DIR_RIGHT; + _berthDirection = DRAGPANEL_BERTH_DIR_RIGHT; } else if (innerTop == top) // top { - m_eBerthDirection = DRAGPANEL_BERTH_DIR_TOP; + _berthDirection = DRAGPANEL_BERTH_DIR_TOP; } else if (innerBottom == bottom) // bottom { - m_eBerthDirection = DRAGPANEL_BERTH_DIR_BOTTOM; + _berthDirection = DRAGPANEL_BERTH_DIR_BOTTOM; } - if (m_eBerthDirection != DRAGPANEL_BERTH_DIR_NONE) + if (_berthDirection != DRAGPANEL_BERTH_DIR_NONE) { return true; } @@ -742,7 +710,7 @@ bool UIDragPanel::checkBerth() void UIDragPanel::berthEvent() { - switch (m_eBerthDirection) + switch (_berthDirection) { case DRAGPANEL_BERTH_DIR_LEFTBOTTOM: berthToLeftBottomEvent(); @@ -783,134 +751,91 @@ void UIDragPanel::berthEvent() void UIDragPanel::berthToLeftBottomEvent() { - if (m_pBerthToLeftBottomListener && m_pfnBerthToLeftBottomSelector) + if (_eventLister && _eventSelector) { - (m_pBerthToLeftBottomListener->*m_pfnBerthToLeftBottomSelector)(this); + (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_LEFTBOTTOM); } } void UIDragPanel::berthToLeftTopEvent() { - if (m_pBerthToLeftTopListener && m_pfnBerthToLeftTopSelector) + if (_eventLister && _eventSelector) { - (m_pBerthToLeftTopListener->*m_pfnBerthToLeftTopSelector)(this); + (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_LFETTOP); } } void UIDragPanel::berthToRightBottomEvent() { - if (m_pBerthToRightBottomListener && m_pfnBerthToRightBottomSelector) + if (_eventLister && _eventSelector) { - (m_pBerthToRightBottomListener->*m_pfnBerthToRightBottomSelector)(this); + (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_RIGHTBOTTOM); } } void UIDragPanel::berthToRightTopEvent() { - if (m_pBerthToRightTopListener && m_pfnBerthToRightTopSelector) + if (_eventLister && _eventSelector) { - (m_pBerthToRightTopListener->*m_pfnBerthToRightTopSelector)(this); + (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_RIGHTTOP); } } void UIDragPanel::berthToLeftEvent() { - if (m_pBerthToLeftListener && m_pfnBerthToLeftSelector) + if (_eventLister && _eventSelector) { - (m_pBerthToLeftListener->*m_pfnBerthToLeftSelector)(this); + (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_LEFT); } } void UIDragPanel::berthToTopEvent() { - if (m_pBerthToTopListener && m_pfnBerthToTopSelector) + if (_eventLister && _eventSelector) { - (m_pBerthToTopListener->*m_pfnBerthToTopSelector)(this); + (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_TOP); } } void UIDragPanel::berthToRightEvent() { - if (m_pBerthToRightListener && m_pfnBerthToRightSelector) + if (_eventLister && _eventSelector) { - (m_pBerthToRightListener->*m_pfnBerthToRightSelector)(this); + (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_RIGHT); } } void UIDragPanel::berthToBottomEvent() { - if (m_pBerthToBottomListener && m_pfnBerthToBottomSelector) + if (_eventLister && _eventSelector) { - (m_pBerthToBottomListener->*m_pfnBerthToBottomSelector)(this); + (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BERTH_BOTTOM); } } -void UIDragPanel::addBerthToLeftBottomEvent(Object *target, SEL_DragPanelBerthToLeftBottomEvent selector) +void UIDragPanel::addEventListener(Object *target, SEL_DragPanelEvent selector) { - m_pBerthToLeftBottomListener = target; - m_pfnBerthToLeftBottomSelector = selector; + _eventLister = target; + _eventSelector = selector; } -void UIDragPanel::addBerthToLeftTopEvent(Object *target, SEL_DragPanelBerthToLeftTopEvent selector) -{ - m_pBerthToLeftTopListener = target; - m_pfnBerthToLeftTopSelector = selector; -} - -void UIDragPanel::addBerthToRightBottomEvent(Object *target, SEL_DragPanelBerthToRightBottomEvent selector) -{ - m_pBerthToRightBottomListener = target; - m_pfnBerthToRightBottomSelector = selector; -} - -void UIDragPanel::addBerthToRightTopEvent(Object *target, SEL_DragPanelBerthToRightTopEvent selector) -{ - m_pBerthToRightTopListener = target; - m_pfnBerthToRightTopSelector = selector; -} - -void UIDragPanel::addBerthToLeftEvent(Object *target, SEL_DragPanelBerthToLeftEvent selector) -{ - m_pBerthToLeftListener = target; - m_pfnBerthToLeftSelector = selector; -} - -void UIDragPanel::addBerthToTopEvent(Object *target, SEL_DragPanelBerthToTopEvent selector) -{ - m_pBerthToTopListener = target; - m_pfnBerthToTopSelector = selector; -} - -void UIDragPanel::addBerthToRightEvent(Object *target, SEL_DragPanelBerthToRightEvent selector) -{ - m_pBerthToRightListener = target; - m_pfnBerthToRightSelector = selector; -} - -void UIDragPanel::addBerthToBottomEvent(Object *target, SEL_DragPanelBerthToBottomEvent selector) -{ - m_pBerthToBottomListener = target; - m_pfnBerthToBottomSelector = selector; -} - - // bounce bool UIDragPanel::isBounceEnable() { - return m_bBounceEnable; + return _bounceEnable; } void UIDragPanel::setBounceEnable(bool bounce) { - m_bBounceEnable = bounce; + _bounceEnable = bounce; } bool UIDragPanel::checkNeedBounce() { - float innerLeft = m_pInnerContainer->getLeftInParent(); - float innerTop = m_pInnerContainer->getTopInParent(); - float innerRight = m_pInnerContainer->getRightInParent(); - float innerBottom = m_pInnerContainer->getBottomInParent(); + float innerLeft = _innerContainer->getLeftInParent(); + float innerTop = _innerContainer->getTopInParent(); + float innerRight = _innerContainer->getRightInParent(); + float innerBottom = _innerContainer->getBottomInParent(); float left = 0; float top = _size.height; @@ -930,27 +855,27 @@ bool UIDragPanel::checkNeedBounce() void UIDragPanel::startBounce() { - if (m_eMoveType == DRAGPANEL_MOVE_TYPE_BOUNCE) + if (_moveType == DRAGPANEL_MOVE_TYPE_BOUNCE) { return; } actionStop(); - m_eMoveType = DRAGPANEL_MOVE_TYPE_BOUNCE; + _moveType = DRAGPANEL_MOVE_TYPE_BOUNCE; bounceToCorner(); } void UIDragPanel::stopBounce() { - m_eMoveType = DRAGPANEL_MOVE_TYPE_NONE; + _moveType = DRAGPANEL_MOVE_TYPE_NONE; } void UIDragPanel::bounceToCorner() { - float innerLeft = m_pInnerContainer->getLeftInParent(); - float innerTop = m_pInnerContainer->getTopInParent(); - float innerRight = m_pInnerContainer->getRightInParent(); - float innerBottom = m_pInnerContainer->getBottomInParent(); + float innerLeft = _innerContainer->getLeftInParent(); + float innerTop = _innerContainer->getTopInParent(); + float innerRight = _innerContainer->getRightInParent(); + float innerBottom = _innerContainer->getBottomInParent(); float width = _size.width; float height = _size.height; @@ -972,7 +897,7 @@ void UIDragPanel::bounceToCorner() to_x = left; to_y = bottom; - m_eBounceDirection = DRAGPANEL_BOUNCE_DIR_LEFTBOTTOM; + _bounceDirection = DRAGPANEL_BOUNCE_DIR_LEFTBOTTOM; } else if (innerLeft > left && innerTop < top) // left top { @@ -981,7 +906,7 @@ void UIDragPanel::bounceToCorner() to_x = left; to_y = top; - m_eBounceDirection = DRAGPANEL_BOUNCE_DIR_LEFTTOP; + _bounceDirection = DRAGPANEL_BOUNCE_DIR_LEFTTOP; } else if (innerRight < right && innerBottom > bottom) // right bottom { @@ -990,7 +915,7 @@ void UIDragPanel::bounceToCorner() to_x = right; to_y = bottom; - m_eBounceDirection = DRAGPANEL_BOUNCE_DIR_RIGHTBOTTOM; + _bounceDirection = DRAGPANEL_BOUNCE_DIR_RIGHTBOTTOM; } else if (innerRight < right && innerTop < top) // right top { @@ -999,7 +924,7 @@ void UIDragPanel::bounceToCorner() to_x = right; to_y = top; - m_eBounceDirection = DRAGPANEL_BOUNCE_DIR_RIGHTTOP; + _bounceDirection = DRAGPANEL_BOUNCE_DIR_RIGHTTOP; } else if (innerLeft > left) // left { @@ -1008,7 +933,7 @@ void UIDragPanel::bounceToCorner() to_x = left; to_y = from_y; - m_eBounceDirection = DRAGPANEL_BOUNCE_DIR_LEFT; + _bounceDirection = DRAGPANEL_BOUNCE_DIR_LEFT; } else if (innerTop < top) // top { @@ -1017,7 +942,7 @@ void UIDragPanel::bounceToCorner() to_x = from_x; to_y = top; - m_eBounceDirection = DRAGPANEL_BOUNCE_DIR_TOP; + _bounceDirection = DRAGPANEL_BOUNCE_DIR_TOP; } else if (innerRight < right) // right { @@ -1026,7 +951,7 @@ void UIDragPanel::bounceToCorner() to_x = right; to_y = from_y; - m_eBounceDirection = DRAGPANEL_BOUNCE_DIR_RIGHT; + _bounceDirection = DRAGPANEL_BOUNCE_DIR_RIGHT; } else if (innerBottom > bottom) // bottom { @@ -1035,22 +960,20 @@ void UIDragPanel::bounceToCorner() to_x = from_x; to_y = bottom; - m_eBounceDirection = DRAGPANEL_BOUNCE_DIR_BOTTOM; + _bounceDirection = DRAGPANEL_BOUNCE_DIR_BOTTOM; } delta = Point(to_x, to_y) - Point(from_x, from_y); // delta = ccpSub(ccp(to_x, to_y), ccp(from_x, from_y)); - actionStartWithWidget(m_pInnerContainer); - moveByWithDuration(m_fBounceDuration, delta); + actionStartWithWidget(_innerContainer); + moveByWithDuration(_bounceDuration, delta); } void UIDragPanel::bounceOver() { stopBounce(); - bounceOverEvent(); - - switch (m_eBounceDirection) + switch (_bounceDirection) { case DRAGPANEL_BOUNCE_DIR_LEFTBOTTOM: bounceToLeftBottomEvent(); @@ -1088,176 +1011,114 @@ void UIDragPanel::bounceOver() break; } - m_eBounceDirection = DRAGPANEL_BOUNCE_DIR_NONE; -} - -void UIDragPanel::bounceOverEvent() -{ - if (m_pBounceOverListener && m_pfnBounceOverSelector) - { - (m_pBounceOverListener->*m_pfnBounceOverSelector)(this); - } + _bounceDirection = DRAGPANEL_BOUNCE_DIR_NONE; } void UIDragPanel::bounceToLeftBottomEvent() { - if (m_pBounceToLeftBottomListener && m_pfnBounceToLeftBottomSelector) + if (_eventLister && _eventSelector) { - (m_pBounceToLeftBottomListener->*m_pfnBounceToLeftBottomSelector)(this); + (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_LEFTBOTTOM); } } void UIDragPanel::bounceToLeftTopEvent() { - if (m_pBounceToLeftTopListener && m_pfnBounceToLeftTopSelector) + if (_eventLister && _eventSelector) { - (m_pBounceToLeftTopListener->*m_pfnBounceToLeftTopSelector)(this); + (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_LEFTTOP); } } void UIDragPanel::bounceToRightBottomEvent() { - if (m_pBounceToRightBottomListener && m_pfnBounceToRightBottomSelector) + if (_eventLister && _eventSelector) { - (m_pBounceToRightBottomListener->*m_pfnBounceToRightBottomSelector)(this); + (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_RIGHTBOTTOM); } } void UIDragPanel::bounceToRightTopEvent() { - if (m_pBounceToRightTopListener && m_pfnBounceToRightTopSelector) + if (_eventLister && _eventSelector) { - (m_pBounceToRightTopListener->*m_pfnBounceToRightTopSelector)(this); + (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_RIGHTTOP); } } void UIDragPanel::bounceToLeftEvent() { - if (m_pBounceToLeftListener && m_pfnBounceToLeftSelector) + if (_eventLister && _eventSelector) { - (m_pBounceToLeftListener->*m_pfnBounceToLeftSelector)(this); + (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_LEFT); } } void UIDragPanel::bounceToTopEvent() { - if (m_pBounceToTopListener && m_pfnBounceToTopSelector) + if (_eventLister && _eventSelector) { - (m_pBounceToTopListener->*m_pfnBounceToTopSelector)(this); + (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_TOP); } } void UIDragPanel::bounceToRightEvent() { - if (m_pBounceToRightListener && m_pfnBounceToRightSelector) + if (_eventLister && _eventSelector) { - (m_pBounceToRightListener->*m_pfnBounceToRightSelector)(this); + (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_RIGHT); } } void UIDragPanel::bounceToBottomEvent() { - if (m_pBounceToBottomListener && m_pfnBounceToBottomSelector) + if (_eventLister && _eventSelector) { - (m_pBounceToBottomListener->*m_pfnBounceToBottomSelector)(this); + (_eventLister->*_eventSelector)(this, DRAGPANEL_EVENT_BOUNCE_BOTTOM); } } -void UIDragPanel::addBounceOverEvent(Object *target, SEL_DragPanelBounceOverEvent selector) -{ - m_pBounceOverListener = target; - m_pfnBounceOverSelector = selector; -} - -void UIDragPanel::addBounceToLeftBottomEvent(Object *target, SEL_DragPanelBounceToLeftBottomEvent selector) -{ - m_pBounceToLeftBottomListener = target; - m_pfnBounceToLeftBottomSelector = selector; -} - -void UIDragPanel::addBounceToLeftTopEvent(Object *target, SEL_DragPanelBounceToLeftTopEvent selector) -{ - m_pBounceToLeftTopListener = target; - m_pfnBounceToLeftTopSelector = selector; -} - -void UIDragPanel::addBounceToRightBottomEvent(Object *target, SEL_DragPanelBounceToRightBottomEvent selector) -{ - m_pBounceToRightBottomListener = target; - m_pfnBounceToRightBottomSelector = selector; -} - -void UIDragPanel::addBounceToRightTopEvent(Object *target, SEL_DragPanelBounceToRightTopEvent selector) -{ - m_pBounceToRightTopListener = target; - m_pfnBounceToRightTopSelector = selector; -} - -void UIDragPanel::addBounceToLeftEvent(Object *target, SEL_DragPanelBounceToLeftEvent selector) -{ - m_pBounceToLeftListener = target; - m_pfnBounceToLeftSelector = selector; -} - -void UIDragPanel::addBounceToTopEvent(Object *target, SEL_DragPanelBounceToTopEvent selector) -{ - m_pBounceToTopListener = target; - m_pfnBounceToTopSelector = selector; -} - -void UIDragPanel::addBounceToRightEvent(Object *target, SEL_DragPanelBounceToRightEvent selector) -{ - m_pBounceToRightListener = target; - m_pfnBounceToRightSelector = selector; -} - -void UIDragPanel::addBounceToBottomEvent(Object *target, SEL_DragPanelBounceToBottomEvent selector) -{ - m_pBounceToBottomListener = target; - m_pfnBounceToBottomSelector = selector; -} - // widget action void UIDragPanel::actionWithDuration(float duration) { - m_fDuration = duration; + _duration = duration; - if (m_fDuration == 0) + if (_duration == 0) { - m_fDuration = FLT_EPSILON; + _duration = FLT_EPSILON; } - m_elapsed = 0; - m_bFirstTick = true; + _elapsed = 0; + _firstTick = true; } bool UIDragPanel::actionIsDone() { - bool value = (m_elapsed >= m_fDuration); + bool value = (_elapsed >= _duration); return value; } void UIDragPanel::actionStartWithWidget(UIWidget *widget) { - m_bRunningAction = true; - m_pActionWidget = widget; + _runningAction = true; + _actionWidget = widget; } void UIDragPanel::actionStep(float dt) { - if (m_bFirstTick) + if (_firstTick) { - m_bFirstTick = false; - m_elapsed = 0; + _firstTick = false; + _elapsed = 0; } else { - m_elapsed += dt; + _elapsed += dt; } actionUpdate(MAX (0, - MIN(1, m_elapsed / - MAX(m_fDuration, FLT_EPSILON) + MIN(1, _elapsed / + MAX(_duration, FLT_EPSILON) ) ) ); @@ -1265,7 +1126,7 @@ void UIDragPanel::actionStep(float dt) void UIDragPanel::actionUpdate(float dt) { - switch (m_nActionType) + switch (_actionType) { case 1: // move by moveByUpdate(dt); @@ -1282,12 +1143,12 @@ void UIDragPanel::actionUpdate(float dt) void UIDragPanel::actionStop() { - m_bRunningAction = false; + _runningAction = false; } void UIDragPanel::actionDone() { - switch (m_eMoveType) + switch (_moveType) { case DRAGPANEL_MOVE_TYPE_AUTOMOVE: autoMoveOver(); @@ -1306,27 +1167,27 @@ void UIDragPanel::actionDone() void UIDragPanel::moveByWithDuration(float duration, const Point& deltaPosition) { actionWithDuration(duration); - m_positionDelta = deltaPosition; + _positionDelta = deltaPosition; moveByInit(); - m_nActionType = 1; + _actionType = 1; } void UIDragPanel::moveByInit() { - m_previousPosition = m_startPosition = m_pActionWidget->getPosition(); + _previousPosition = _startPosition = _actionWidget->getPosition(); } void UIDragPanel::moveByUpdate(float t) { float easeRate = 0.0f; - switch (m_eMoveType) + switch (_moveType) { case DRAGPANEL_MOVE_TYPE_AUTOMOVE: - easeRate = m_fAutoMoveEaseRate; + easeRate = _autoMoveEaseRate; break; case DRAGPANEL_MOVE_TYPE_BOUNCE: - easeRate = m_fBounceEaseRate; + easeRate = _bounceEaseRate; break; default: @@ -1334,19 +1195,19 @@ void UIDragPanel::moveByUpdate(float t) } t = powf(t, 1 / easeRate); - Point currentPos = m_pActionWidget->getPosition(); - Point diff = currentPos - m_previousPosition; - m_startPosition = m_startPosition + diff; -// Point diff = ccpSub(currentPos, m_previousPosition); -// m_startPosition = ccpAdd( m_startPosition, diff); + Point currentPos = _actionWidget->getPosition(); + Point diff = currentPos - _previousPosition; + _startPosition = _startPosition + diff; +// Point diff = ccpSub(currentPos, _previousPosition); +// _startPosition = ccpAdd( _startPosition, diff); -// Point newPos = ccpAdd( m_startPosition, ccpMult(m_positionDelta, t) ); - Point newPos = m_startPosition + (m_positionDelta * t); +// Point newPos = ccpAdd( _startPosition, ccpMult(_positionDelta, t) ); + Point newPos = _startPosition + (_positionDelta * t); - m_pActionWidget->setPosition(newPos); - m_previousPosition = newPos; + _actionWidget->setPosition(newPos); + _previousPosition = newPos; - switch (m_eMoveType) + switch (_moveType) { case DRAGPANEL_MOVE_TYPE_AUTOMOVE: autoMove(); @@ -1361,16 +1222,16 @@ void UIDragPanel::moveByUpdate(float t) void UIDragPanel::moveToWithDuration(float duration, const Point& position) { actionWithDuration(duration); - m_endPosition = position; + _endPosition = position; moveToInit(); - m_nActionType = 2; + _actionType = 2; } void UIDragPanel::moveToInit() { moveByInit(); - m_positionDelta = m_endPosition - m_pActionWidget->getPosition(); -// m_positionDelta = ccpSub( m_endPosition, m_pActionWidget->getPosition() ); + _positionDelta = _endPosition - _actionWidget->getPosition(); +// _positionDelta = ccpSub( _endPosition, _actionWidget->getPosition() ); } void UIDragPanel::moveToUpdate(float t) @@ -1380,7 +1241,7 @@ void UIDragPanel::moveToUpdate(float t) Layout* UIDragPanel::getInnerContainer() { - return m_pInnerContainer; + return _innerContainer; } const char* UIDragPanel::getDescription() const diff --git a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIDragPanel.h b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIDragPanel.h index f5b8ae1598..1538b68d54 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIDragPanel.h +++ b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIDragPanel.h @@ -72,47 +72,31 @@ enum DRAGPANEL_BOUNCE_DIR DRAGPANEL_BOUNCE_DIR_BOTTOM, }; -/** - * dragpanel berth event - */ -typedef void (Object::*SEL_DragPanelBerthToLeftBottomEvent)(Object*); -#define coco_DragPane_BerthToLeftBottom_selector(_SELECTOR) (SEL_DragPanelBerthToLeftBottomEvent)(&_SELECTOR) -typedef void (Object::*SEL_DragPanelBerthToLeftTopEvent)(Object*); -#define coco_DragPanel_BerthToLeftTop_selector(_SELECTOR) (SEL_DragPanelBerthToLeftTopEvent)(&_SELECTOR) -typedef void (Object::*SEL_DragPanelBerthToRightBottomEvent)(Object*); -#define coco_DragPanel_BerthToRightBottom_selector(_SELECTOR) (SEL_DragPanelBerthToRightBottomEvent)(&_SELECTOR) -typedef void (Object::*SEL_DragPanelBerthToRightTopEvent)(Object*); -#define coco_DragPanel_BerthToRightTop_selector(_SELECTOR) (SEL_DragPanelBerthToRightTopEvent)(&_SELECTOR) -typedef void (Object::*SEL_DragPanelBerthToLeftEvent)(Object*); -#define coco_DragPanel_BerthToLeft_selector(_SELECTOR) (SEL_DragPanelBerthToLeftEvent)(&_SELECTOR) -typedef void (Object::*SEL_DragPanelBerthToRightEvent)(Object*); -#define coco_DragPanel_BerthToRight_selector(_SELECTOR) (SEL_DragPanelBerthToRightEvent)(&_SELECTOR) -typedef void (Object::*SEL_DragPanelBerthToTopEvent)(Object*); -#define coco_DragPanel_BerthToTop_selector(_SELECTOR) (SEL_DragPanelBerthToTopEvent)(&_SELECTOR) -typedef void (Object::*SEL_DragPanelBerthToBottomEvent)(Object*); -#define coco_DragPanel_BerthToBottom_selector(_SELECTOR) (SEL_DragPanelBerthToBottomEvent)(&_SELECTOR) +typedef enum +{ + DRAGPANEL_EVENT_BERTH_LEFTBOTTOM, + DRAGPANEL_EVENT_BERTH_LFETTOP, + DRAGPANEL_EVENT_BERTH_RIGHTBOTTOM, + DRAGPANEL_EVENT_BERTH_RIGHTTOP, + DRAGPANEL_EVENT_BERTH_LEFT, + DRAGPANEL_EVENT_BERTH_TOP, + DRAGPANEL_EVENT_BERTH_RIGHT, + DRAGPANEL_EVENT_BERTH_BOTTOM, + DRAGPANEL_EVENT_BOUNCE_LEFTBOTTOM, + DRAGPANEL_EVENT_BOUNCE_LEFTTOP, + DRAGPANEL_EVENT_BOUNCE_RIGHTBOTTOM, + DRAGPANEL_EVENT_BOUNCE_RIGHTTOP, + DRAGPANEL_EVENT_BOUNCE_LEFT, + DRAGPANEL_EVENT_BOUNCE_TOP, + DRAGPANEL_EVENT_BOUNCE_RIGHT, + DRAGPANEL_EVENT_BOUNCE_BOTTOM, +}DragPanelEventType; /** - * dragpanel bounce event + * dragpanel event */ -typedef void (Object::*SEL_DragPanelBounceOverEvent)(Object*); -#define coco_DragPanel_BounceOver_selector(_SELECTOR) (SEL_DragPanelBounceOverEvent)(&_SELECTOR) -typedef void (Object::*SEL_DragPanelBounceToLeftBottomEvent)(Object*); -#define coco_DragPanel_BounceToLeftBottom_selector(_SELECTOR) (SEL_DragPanelBounceToLeftBottomEvent)(&_SELECTOR) -typedef void (Object::*SEL_DragPanelBounceToLeftTopEvent)(Object*); -#define coco_DragPanel_BounceToLeftTop_selector(_SELECTOR) (SEL_DragPanelBounceToLeftTopEvent)(&_SELECTOR) -typedef void (Object::*SEL_DragPanelBounceToRightBottomEvent)(Object*); -#define coco_DragPanel_BounceToRightBottom_selector(_SELECTOR) (SEL_DragPanelBounceToRightBottomEvent)(&_SELECTOR) -typedef void (Object::*SEL_DragPanelBounceToRightTopEvent)(Object*); -#define coco_DragPanel_BounceToRightTop_selector(_SELECTOR) (SEL_DragPanelBounceToRightTopEvent)(&_SELECTOR) -typedef void (Object::*SEL_DragPanelBounceToLeftEvent)(Object*); -#define coco_DragPanel_BounceToLeft_selector(_SELECTOR) (SEL_DragPanelBounceToLeftEvent)(&_SELECTOR) -typedef void (Object::*SEL_DragPanelBounceToTopEvent)(Object*); -#define coco_DragPanel_BounceToTop_selector(_SELECTOR) (SEL_DragPanelBounceToTopEvent)(&_SELECTOR) -typedef void (Object::*SEL_DragPanelBounceToRightEvent)(Object*); -#define coco_DragPanel_BounceToRight_selector(_SELECTOR) (SEL_DragPanelBounceToRightEvent)(&_SELECTOR) -typedef void (Object::*SEL_DragPanelBounceToBottomEvent)(Object*); -#define coco_DragPanel_BounceToBottom_selector(_SELECTOR) (SEL_DragPanelBounceToBottomEvent)(&_SELECTOR) +typedef void (Object::*SEL_DragPanelEvent)(Object*, DragPanelEventType); +#define dragpaneleventselector(_SELECTOR)(SEL_DragPanelEvent)(&_SELECTOR) class UIDragPanel : public Layout, public UIScrollInterface { @@ -180,17 +164,10 @@ public: bool isBerth(); /** - * berth event by direction + * event */ - void addBerthToLeftBottomEvent(Object* target, SEL_DragPanelBerthToLeftBottomEvent selector); - void addBerthToLeftTopEvent(Object* target, SEL_DragPanelBerthToLeftTopEvent selector); - void addBerthToRightBottomEvent(Object* target, SEL_DragPanelBerthToRightBottomEvent selector); - void addBerthToRightTopEvent(Object* target, SEL_DragPanelBerthToRightTopEvent selector); - void addBerthToLeftEvent(Object* target, SEL_DragPanelBerthToLeftEvent selector); - void addBerthToTopEvent(Object* target, SEL_DragPanelBerthToTopEvent selector); - void addBerthToRightEvent(Object* target, SEL_DragPanelBerthToRightEvent selector); - void addBerthToBottomEvent(Object* target, SEL_DragPanelBerthToBottomEvent selector); - + void addEventListener(Object* target, SEL_DragPanelEvent selector); + /** * get and set bounce enable */ @@ -204,18 +181,6 @@ public: * set bounce ease rate */ void setBounceEaseRate(float rate); - /** - * bounce event by dircetion - */ - void addBounceOverEvent(Object* target, SEL_DragPanelBounceOverEvent selector); - void addBounceToLeftBottomEvent(Object* target, SEL_DragPanelBounceToLeftBottomEvent selector); - void addBounceToLeftTopEvent(Object* target, SEL_DragPanelBounceToLeftTopEvent selector); - void addBounceToRightBottomEvent(Object* target, SEL_DragPanelBounceToRightBottomEvent selector); - void addBounceToRightTopEvent(Object* target, SEL_DragPanelBounceToRightTopEvent selector); - void addBounceToLeftEvent(Object* target, SEL_DragPanelBounceToLeftEvent selector); - void addBounceToTopEvent(Object* target, SEL_DragPanelBounceToTopEvent selector); - void addBounceToRightEvent(Object* target, SEL_DragPanelBounceToRightEvent selector); - void addBounceToBottomEvent(Object* target, SEL_DragPanelBounceToBottomEvent selector); /** * Gets inner container of dragpanel. @@ -291,7 +256,6 @@ protected: void bounceToCorner(); void bounceOver(); // bounce event - void bounceOverEvent(); void bounceToLeftBottomEvent(); void bounceToRightBottomEvent(); void bounceToLeftTopEvent(); @@ -323,94 +287,59 @@ protected: /************/ virtual void setClippingEnabled(bool able){Layout::setClippingEnabled(able);}; protected: - Layout* m_pInnerContainer; + Layout* _innerContainer; /* DRAGPANEL_DIR m_eDirection; DRAGPANEL_MOVE_DIR m_eMoveDirection; */ - bool m_bTouchPressed; - bool m_bTouchMoved; - bool m_bTouchReleased; - bool m_bTouchCanceld; // check touch out of drag panel boundary + bool _touchPressed; + bool _touchMoved; + bool _touchReleased; + bool _touchCanceld; // check touch out of drag panel boundary - Point m_touchStartNodeSpace; - Point m_touchStartWorldSpace; - Point m_touchEndWorldSpace; + Point _touchStartNodeSpace; + Point _touchStartWorldSpace; + Point _touchEndWorldSpace; - float m_fSlidTime; + float _slidTime; // move type - DRAGPANEL_MOVE_TYPE m_eMoveType; + DRAGPANEL_MOVE_TYPE _moveType; // auto move - float m_fAutoMoveDuration; - float m_fAutoMoveEaseRate; + float _autoMoveDuration; + float _autoMoveEaseRate; + + // event + Object* _eventLister; + SEL_DragPanelEvent _eventSelector; // berth - DRAGPANEL_BERTH_DIR m_eBerthDirection; - - // berth event - Object* m_pBerthToLeftListener; - SEL_DragPanelBerthToLeftEvent m_pfnBerthToLeftSelector; - Object* m_pBerthToRightListener; - SEL_DragPanelBerthToRightEvent m_pfnBerthToRightSelector; - Object* m_pBerthToTopListener; - SEL_DragPanelBerthToTopEvent m_pfnBerthToTopSelector; - Object* m_pBerthToBottomListener; - SEL_DragPanelBerthToBottomEvent m_pfnBerthToBottomSelector; - Object* m_pBerthToLeftBottomListener; - SEL_DragPanelBerthToLeftBottomEvent m_pfnBerthToLeftBottomSelector; - Object* m_pBerthToLeftTopListener; - SEL_DragPanelBerthToLeftTopEvent m_pfnBerthToLeftTopSelector; - Object* m_pBerthToRightBottomListener; - SEL_DragPanelBerthToRightBottomEvent m_pfnBerthToRightBottomSelector; - Object* m_pBerthToRightTopListener; - SEL_DragPanelBerthToRightTopEvent m_pfnBerthToRightTopSelector; - + DRAGPANEL_BERTH_DIR _berthDirection; + // bounce - bool m_bBounceEnable; - DRAGPANEL_BOUNCE_DIR m_eBounceDirection; - float m_fBounceDuration; - float m_fBounceEaseRate; - - // bounce event - Object* m_pBounceOverListener; - SEL_DragPanelBounceOverEvent m_pfnBounceOverSelector; - Object* m_pBounceToLeftBottomListener; - SEL_DragPanelBounceToLeftBottomEvent m_pfnBounceToLeftBottomSelector; - Object* m_pBounceToLeftTopListener; - SEL_DragPanelBounceToLeftTopEvent m_pfnBounceToLeftTopSelector; - Object* m_pBounceToRightBottomListener; - SEL_DragPanelBounceToRightBottomEvent m_pfnBounceToRightBottomSelector; - Object* m_pBounceToRightTopListener; - SEL_DragPanelBounceToRightTopEvent m_pfnBounceToRightTopSelector; - Object* m_pBounceToLeftListener; - SEL_DragPanelBounceToLeftEvent m_pfnBounceToLeftSelector; - Object* m_pBounceToTopListener; - SEL_DragPanelBounceToTopEvent m_pfnBounceToTopSelector; - Object* m_pBounceToRightListener; - SEL_DragPanelBounceToRightEvent m_pfnBounceToRightSelector; - Object* m_pBounceToBottomListener; - SEL_DragPanelBounceToBottomEvent m_pfnBounceToBottomSelector; + bool _bounceEnable; + DRAGPANEL_BOUNCE_DIR _bounceDirection; + float _bounceDuration; + float _bounceEaseRate; + float _runningAction; + int _actionType; - float m_bRunningAction; - int m_nActionType; + UIWidget* _actionWidget; - UIWidget* m_pActionWidget; + float _duration; + float _elapsed; + bool _firstTick; - float m_fDuration; - float m_elapsed; - bool m_bFirstTick; + Point _positionDelta; + Point _startPosition; + Point _previousPosition; - Point m_positionDelta; - Point m_startPosition; - Point m_previousPosition; - - Point m_endPosition; + Point _endPosition; }; NS_CC_EXT_END diff --git a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIListView.cpp b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIListView.cpp index 9a28f435f0..27775474c5 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIListView.cpp +++ b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIListView.cpp @@ -42,10 +42,8 @@ UIListView::UIListView() , _bePressed(false) , _slidTime(0.0f) , _childFocusCancelOffset(5.0f) -, _initChildListener(NULL) -, _initChildSelector(NULL) -, _updateChildListener(NULL) -, _updateChildSelector(NULL) +, _eventListener(NULL) +, _eventSelector(NULL) , _childPool(NULL) , _updatePool(NULL) , _dataLength(0) @@ -1431,30 +1429,24 @@ void UIListView::updateChild() void UIListView::initChildEvent() { - if (_initChildListener && _initChildSelector) + if (_eventListener && _eventSelector) { - (_initChildListener->*_initChildSelector)(this); + (_eventListener->*_eventSelector)(this, LISTVIEW_EVENT_INIT_CHILD); } } void UIListView::updateChildEvent() { - if (_updateChildListener && _updateChildSelector) + if (_eventListener && _eventSelector) { - (_updateChildListener->*_updateChildSelector)(this); + (_eventListener->*_eventSelector)(this, LISTVIEW_EVENT_UPDATE_CHILD); } } -void UIListView::addInitChildEvent(Object *target, SEL_ListViewInitChildEvent seletor) +void UIListView::addEventListenter(Object *target, SEL_ListViewEvent selector) { - _initChildListener = target; - _initChildSelector = seletor; -} - -void UIListView::addUpdateChildEvent(Object *target, SEL_ListViewUpdateChildEvent selector) -{ - _updateChildListener = target; - _updateChildSelector = selector; + _eventListener = target; + _eventSelector = selector; } const char* UIListView::getDescription() const diff --git a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIListView.h b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIListView.h index 3a8eb327d6..658dd4482c 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIListView.h +++ b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIListView.h @@ -53,13 +53,17 @@ typedef enum LISTVIEW_MOVE_DIR LISTVIEW_MOVE_DIR_RIGHT, }ListViewMoveDirection; +typedef enum +{ + LISTVIEW_EVENT_INIT_CHILD, + LISTVIEW_EVENT_UPDATE_CHILD, +}ListViewEventType; + /** * list view event */ -typedef void (cocos2d::Object::*SEL_ListViewInitChildEvent)(cocos2d::Object*); -typedef void (cocos2d::Object::*SEL_ListViewUpdateChildEvent)(cocos2d::Object*); -#define coco_ListView_InitChild_selector(_SELECTOR) (SEL_ListViewInitChildEvent)(&_SELECTOR) -#define coco_ListView_UpdateChild_selector(_SELECTOR) (SEL_ListViewUpdateChildEvent)(&_SELECTOR) +typedef void (Object::*SEL_ListViewEvent)(Object*, ListViewEventType); +#define listvieweventselector(_SELECTOR)(SEL_ListViewEvent)(&_SELECTOR) class UIListView : public Layout { @@ -124,13 +128,9 @@ public: * add event call-back function */ /** - * add init child event + * add event */ - void addInitChildEvent(cocos2d::Object* target, SEL_ListViewInitChildEvent seletor); - /** - * add udpate child event - */ - void addUpdateChildEvent(cocos2d::Object* target, SEL_ListViewUpdateChildEvent selector); + void addEventListenter(cocos2d::Object* target, SEL_ListViewEvent selector); /* gui mark */ /** @@ -213,10 +213,8 @@ protected: Point _moveChildPoint; float _childFocusCancelOffset; - cocos2d::Object* _initChildListener; - SEL_ListViewInitChildEvent _initChildSelector; - cocos2d::Object* _updateChildListener; - SEL_ListViewUpdateChildEvent _updateChildSelector; + Object* _eventListener; + SEL_ListViewEvent _eventSelector; Array* _childPool; Array* _updatePool; diff --git a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIPageView.cpp b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIPageView.cpp index b3abef206b..28716b8db9 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIPageView.cpp +++ b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIPageView.cpp @@ -43,8 +43,8 @@ _autoScrollDistance(0.0f), _autoScrollSpeed(0.0f), _autoScrollDir(0), _childFocusCancelOffset(5.0f), -_pageTurningListener(NULL), -_pageTurningSelector(NULL) +_eventListener(NULL), +_eventSelector(NULL) { } @@ -548,16 +548,16 @@ void UIPageView::interceptTouchEvent(int handleState, UIWidget *sender, const Po void UIPageView::pageTurningEvent() { - if (_pageTurningListener && _pageTurningSelector) + if (_eventListener && _eventSelector) { - (_pageTurningListener->*_pageTurningSelector)(this); + (_eventListener->*_eventSelector)(this, PAGEVIEW_EVENT_TURNING); } } -void UIPageView::addPageTurningEvent(Object *target, SEL_PageViewPageTurningEvent selector) +void UIPageView::addEventListener(Object *target, SEL_PageViewEvent selector) { - _pageTurningListener = target; - _pageTurningSelector = selector; + _eventListener = target; + _eventSelector = selector; } int UIPageView::getCurPageIndex() const diff --git a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIPageView.h b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIPageView.h index eb6fc8b52d..974f35019b 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIPageView.h +++ b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIPageView.h @@ -30,8 +30,13 @@ NS_CC_EXT_BEGIN -typedef void (Object::*SEL_PageViewPageTurningEvent)(Object*); -#define coco_PageView_PageTurning_selector(_SELECTOR) (SEL_PageViewPageTurningEvent)(&_SELECTOR) +typedef enum +{ + PAGEVIEW_EVENT_TURNING, +}PageViewEventType; + +typedef void (Object::*SEL_PageViewEvent)(Object*, PageViewEventType); +#define pagevieweventselector(_SELECTOR)(SEL_PageViewEvent)(&_SELECTOR) typedef enum { PAGEVIEW_TOUCHLEFT, @@ -110,8 +115,8 @@ public: */ int getCurPageIndex() const; - //Add call back function called when page turning. - void addPageTurningEvent(Object *target, SEL_PageViewPageTurningEvent selector); + // event + void addEventListener(Object *target, SEL_PageViewEvent selector); //override "removeChild" method of widget. virtual bool removeChild(UIWidget* widget); @@ -174,8 +179,8 @@ protected: float _autoScrollSpeed; int _autoScrollDir; float _childFocusCancelOffset; - Object* _pageTurningListener; - SEL_PageViewPageTurningEvent _pageTurningSelector; + Object* _eventListener; + SEL_PageViewEvent _eventSelector; }; NS_CC_EXT_END diff --git a/extensions/CocoStudio/GUI/UIWidgets/UICheckBox.cpp b/extensions/CocoStudio/GUI/UIWidgets/UICheckBox.cpp index 12bf8a3a72..588ff138e1 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UICheckBox.cpp +++ b/extensions/CocoStudio/GUI/UIWidgets/UICheckBox.cpp @@ -287,7 +287,7 @@ void UICheckBox::unSelectedEvent() } } -void UICheckBox::addSelectedStateEvent(Object *target, SEL_SelectedStateEvent selector) +void UICheckBox::addEventListener(Object *target, SEL_SelectedStateEvent selector) { _selectedStateEventListener = target; _selectedStateEventSelector = selector; diff --git a/extensions/CocoStudio/GUI/UIWidgets/UICheckBox.h b/extensions/CocoStudio/GUI/UIWidgets/UICheckBox.h index b997f6f1e1..89f7473f05 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UICheckBox.h +++ b/extensions/CocoStudio/GUI/UIWidgets/UICheckBox.h @@ -134,7 +134,7 @@ public: virtual void setAnchorPoint(const Point &pt); //add a call back function would called when checkbox is selected or unselected. - void addSelectedStateEvent(Object* target,SEL_SelectedStateEvent selector); + void addEventListener(Object* target,SEL_SelectedStateEvent selector); //override "setFlipX" method of widget. virtual void setFlipX(bool flipX); diff --git a/extensions/CocoStudio/GUI/UIWidgets/UISlider.cpp b/extensions/CocoStudio/GUI/UIWidgets/UISlider.cpp index 48d27f5d14..022646033f 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UISlider.cpp +++ b/extensions/CocoStudio/GUI/UIWidgets/UISlider.cpp @@ -410,7 +410,7 @@ float UISlider::getPercentWithBallPos(float px) return (((px-(-_barLength/2.0f))/_barLength)*100.0f); } -void UISlider::addPercentEvent(Object *target, SEL_SlidPercentChangedEvent selector) +void UISlider::addEventListener(Object *target, SEL_SlidPercentChangedEvent selector) { _slidPercentListener = target; _slidPercentSelector = selector; diff --git a/extensions/CocoStudio/GUI/UIWidgets/UISlider.h b/extensions/CocoStudio/GUI/UIWidgets/UISlider.h index 2ed5854280..39b912fbe6 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UISlider.h +++ b/extensions/CocoStudio/GUI/UIWidgets/UISlider.h @@ -158,7 +158,7 @@ public: /** * Add call back function called when slider's percent has changed to slider. */ - void addPercentEvent(Object* target,SEL_SlidPercentChangedEvent selector); + void addEventListener(Object* target,SEL_SlidPercentChangedEvent selector); //override "onTouchBegan" method of widget. virtual bool onTouchBegan(const Point &touchPoint); diff --git a/extensions/CocoStudio/GUI/UIWidgets/UITextField.cpp b/extensions/CocoStudio/GUI/UIWidgets/UITextField.cpp index 170bfe96ac..2703a0557f 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UITextField.cpp +++ b/extensions/CocoStudio/GUI/UIWidgets/UITextField.cpp @@ -271,18 +271,12 @@ bool UICCTextField::getDeleteBackward() UITextField::UITextField(): +_textFieldRenderer(NULL), _touchWidth(0.0f), _touchHeight(0.0f), _useTouchArea(false), -_attachWithIMEListener(NULL), -_detachWithIMEListener(NULL), -_insertTextListener(NULL), -_deleteBackwardListener(NULL), -_attachWithIMESelector(NULL), -_detachWithIMESelector(NULL), -_insertTextSelector(NULL), -_deleteBackwardSelector(NULL), -_textFieldRenderer(NULL) +_eventListener(NULL), +_eventSelector(NULL) { } @@ -475,58 +469,40 @@ void UITextField::setDeleteBackward(bool deleteBackward) void UITextField::attachWithIMEEvent() { - if (_attachWithIMEListener && _attachWithIMESelector) + if (_eventListener && _eventSelector) { - (_attachWithIMEListener->*_attachWithIMESelector)(this); + (_eventListener->*_eventSelector)(this, TEXTFIELD_EVENT_ATTACH_WITH_IME); } } void UITextField::detachWithIMEEvent() { - if (_detachWithIMEListener && _detachWithIMESelector) + if (_eventListener && _eventSelector) { - (_detachWithIMEListener->*_detachWithIMESelector)(this); + (_eventListener->*_eventSelector)(this, TEXTFIELD_EVENT_DETACH_WITH_IME); } } void UITextField::insertTextEvent() { - if (_insertTextListener && _insertTextSelector) + if (_eventListener && _eventSelector) { - (_insertTextListener->*_insertTextSelector)(this); + (_eventListener->*_eventSelector)(this, TEXTFIELD_EVENT_INDERT_TEXT); } } void UITextField::deleteBackwardEvent() { - if (_deleteBackwardListener && _deleteBackwardSelector) + if (_eventListener && _eventSelector) { - (_deleteBackwardListener->*_deleteBackwardSelector)(this); + (_eventListener->*_eventSelector)(this, TEXTFIELD_EVENT_DELETE_BACKWARD); } } -void UITextField::addAttachWithIMEEvent(Object *target, SEL_TextFieldAttachWithIMEEvent selecor) +void UITextField::addEventListener(Object *target, SEL_TextFieldEvent selecor) { - _attachWithIMEListener = target; - _attachWithIMESelector = selecor; -} - -void UITextField::addDetachWithIMEEvent(Object *target, SEL_TextFieldDetachWithIMEEvent selecor) -{ - _detachWithIMEListener = target; - _detachWithIMESelector = selecor; -} - -void UITextField::addInsertTextEvent(Object *target, SEL_TextFieldInsertTextEvent selecor) -{ - _insertTextListener = target; - _insertTextSelector = selecor; -} - -void UITextField::addDeleteBackwardEvent(Object *target, SEL_TextFieldDeleteBackwardEvent selecor) -{ - _deleteBackwardListener = target; - _deleteBackwardSelector = selecor; + _eventListener = target; + _eventSelector = selecor; } void UITextField::setAnchorPoint(const Point &pt) diff --git a/extensions/CocoStudio/GUI/UIWidgets/UITextField.h b/extensions/CocoStudio/GUI/UIWidgets/UITextField.h index 9820f05750..5efc920b80 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/UITextField.h +++ b/extensions/CocoStudio/GUI/UIWidgets/UITextField.h @@ -84,14 +84,16 @@ protected: }; -typedef void (Object::*SEL_TextFieldAttachWithIMEEvent)(Object*); -#define coco_TextField_AttachWithIME_selector(_SELECTOR) (SEL_TextFieldAttachWithIMEEvent)(&_SELECTOR) -typedef void (Object::*SEL_TextFieldDetachWithIMEEvent)(Object*); -#define coco_TextField_DetachWithIME_selector(_SELECTOR) (SEL_TextFieldDetachWithIMEEvent)(&_SELECTOR) -typedef void (Object::*SEL_TextFieldInsertTextEvent)(Object*); -#define coco_TextField_InsertText_selector(_SELECTOR) (SEL_TextFieldInsertTextEvent)(&_SELECTOR) -typedef void (Object::*SEL_TextFieldDeleteBackwardEvent)(Object*); -#define coco_TextField_DeleteBackward_selector(_SELECTOR) (SEL_TextFieldDeleteBackwardEvent)(&_SELECTOR) +typedef enum +{ + TEXTFIELD_EVENT_ATTACH_WITH_IME, + TEXTFIELD_EVENT_DETACH_WITH_IME, + TEXTFIELD_EVENT_INDERT_TEXT, + TEXTFIELD_EVENT_DELETE_BACKWARD, +}TextFiledEventType; + +typedef void (Object::*SEL_TextFieldEvent)(Object*, TextFiledEventType); +#define textfieldeventselector(_SELECTOR) (SEL_TextFieldEvent)(&_SELECTOR) //class UITextField : public UIWidget class UITextField : public UIWidget @@ -126,10 +128,7 @@ public: void setInsertText(bool insertText); bool getDeleteBackward(); void setDeleteBackward(bool deleteBackward); - void addAttachWithIMEEvent(Object* target, SEL_TextFieldAttachWithIMEEvent selecor); - void addDetachWithIMEEvent(Object* target, SEL_TextFieldDetachWithIMEEvent selecor); - void addInsertTextEvent(Object* target, SEL_TextFieldInsertTextEvent selecor); - void addDeleteBackwardEvent(Object* target, SEL_TextFieldDeleteBackwardEvent selecor); + void addEventListener(Object* target, SEL_TextFieldEvent selecor); virtual void setAnchorPoint(const Point &pt); virtual void setColor(const Color3B &color); virtual void setOpacity(int opacity); @@ -155,21 +154,15 @@ protected: virtual void onSizeChanged(); void textfieldRendererScaleChangedWithSize(); protected: + UICCTextField* _textFieldRenderer; + float _touchWidth; float _touchHeight; bool _useTouchArea; - Object* _attachWithIMEListener; - Object* _detachWithIMEListener; - Object* _insertTextListener; - Object* _deleteBackwardListener; + Object* _eventListener; + SEL_TextFieldEvent _eventSelector; - SEL_TextFieldAttachWithIMEEvent _attachWithIMESelector; - SEL_TextFieldDetachWithIMEEvent _detachWithIMESelector; - SEL_TextFieldInsertTextEvent _insertTextSelector; - SEL_TextFieldDeleteBackwardEvent _deleteBackwardSelector; - - UICCTextField* _textFieldRenderer; }; NS_CC_EXT_END diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest.cpp index d8359a5684..3855d5afe3 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest.cpp @@ -54,7 +54,7 @@ bool UICheckBoxTest::init() "cocosgui/check_box_active_disable.png"); checkBox->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f)); - checkBox->addSelectedStateEvent(this, checkboxselectedeventselector(UICheckBoxTest::selectedEvent)); + checkBox->addEventListener(this, checkboxselectedeventselector(UICheckBoxTest::selectedEvent)); // checkBox->addSelectEvent(this, coco_selectselector(UICheckBoxTest::selectedEvent)); m_pUiLayer->addWidget(checkBox); diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIDragPanelTest/UIDragPanelTest.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIDragPanelTest/UIDragPanelTest.cpp index a9f3dc4c76..e80ff2fd01 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIDragPanelTest/UIDragPanelTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIDragPanelTest/UIDragPanelTest.cpp @@ -57,14 +57,7 @@ bool UIDragPanelTest::init() (backgroundSize.width - dragPanel->getSize().width) / 2, (widgetSize.height - backgroundSize.height) / 2 + (backgroundSize.height - dragPanel->getSize().height) / 2)); - dragPanel->addBerthToLeftBottomEvent(this, coco_DragPane_BerthToLeftBottom_selector(UIDragPanelTest::berthToLeftBottomEvent)); - dragPanel->addBerthToLeftTopEvent(this, coco_DragPanel_BerthToLeftTop_selector(UIDragPanelTest::berthToLeftTopEvent)); - dragPanel->addBerthToRightBottomEvent(this, coco_DragPanel_BerthToRightBottom_selector(UIDragPanelTest::berthToRightBottomEvent)); - dragPanel->addBerthToRightTopEvent(this, coco_DragPanel_BerthToRightTop_selector(UIDragPanelTest::berthToRightTopEvent)); - dragPanel->addBerthToLeftEvent(this, coco_DragPanel_BerthToLeft_selector(UIDragPanelTest::berthToLeftEvent)); - dragPanel->addBerthToTopEvent(this, coco_DragPanel_BerthToTop_selector(UIDragPanelTest::berthToTopEvent)); - dragPanel->addBerthToRightEvent(this, coco_DragPanel_BerthToRight_selector(UIDragPanelTest::berthToRightEvent)); - dragPanel->addBerthToBottomEvent(this, coco_DragPanel_BerthToBottom_selector(UIDragPanelTest::berthToBottomEvent)); + dragPanel->addEventListener(this, dragpaneleventselector(UIDragPanelTest::dragPanelEvent)); UIImageView* imageView = UIImageView::create(); imageView->setTouchEnabled(true); @@ -82,44 +75,45 @@ bool UIDragPanelTest::init() return false; } -void UIDragPanelTest::berthToLeftBottomEvent(Object *pSender) +void UIDragPanelTest::dragPanelEvent(Object *pSender, DragPanelEventType type) { - m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Left Bottom")->getCString()); -} - -void UIDragPanelTest::berthToLeftTopEvent(Object *pSender) -{ - m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Left Top")->getCString()); -} - -void UIDragPanelTest::berthToRightBottomEvent(Object *pSender) -{ - m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Right Bottom")->getCString()); -} - -void UIDragPanelTest::berthToRightTopEvent(Object *pSender) -{ - m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Right Top")->getCString()); -} - -void UIDragPanelTest::berthToLeftEvent(Object* pSender) -{ - m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Left")->getCString()); -} - -void UIDragPanelTest::berthToTopEvent(Object *pSender) -{ - m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Top")->getCString()); -} - -void UIDragPanelTest::berthToRightEvent(Object *pSender) -{ - m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Right")->getCString()); -} - -void UIDragPanelTest::berthToBottomEvent(Object *pSender) -{ - m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Bottom")->getCString()); + switch (type) + { + case DRAGPANEL_EVENT_BERTH_LEFTBOTTOM: + m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Left Bottom")->getCString()); + break; + + case DRAGPANEL_EVENT_BERTH_LFETTOP: + m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Left Top")->getCString()); + break; + + case DRAGPANEL_EVENT_BERTH_RIGHTBOTTOM: + m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Right Bottom")->getCString()); + break; + + case DRAGPANEL_EVENT_BERTH_RIGHTTOP: + m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Right Top")->getCString()); + break; + + case DRAGPANEL_EVENT_BERTH_LEFT: + m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Left")->getCString()); + break; + + case DRAGPANEL_EVENT_BERTH_TOP: + m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Top")->getCString()); + break; + + case DRAGPANEL_EVENT_BERTH_RIGHT: + m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Right")->getCString()); + break; + + case DRAGPANEL_EVENT_BERTH_BOTTOM: + m_pDisplayValueLabel->setText(CCString::createWithFormat("Berth To Bottom")->getCString()); + break; + + default: + break; + } } // UIDragPanelTest_Bounce @@ -171,14 +165,7 @@ bool UIDragPanelTest_Bounce::init() (backgroundSize.width - dragPanel->getSize().width) / 2, (widgetSize.height - backgroundSize.height) / 2 + (backgroundSize.height - dragPanel->getSize().height) / 2)); - dragPanel->addBounceToLeftBottomEvent(this, coco_DragPanel_BounceToLeftBottom_selector(UIDragPanelTest_Bounce::bounceToLeftBottomEvent)); - dragPanel->addBounceToLeftTopEvent(this, coco_DragPanel_BounceToLeftTop_selector(UIDragPanelTest_Bounce::bounceToLeftTopEvent)); - dragPanel->addBounceToRightBottomEvent(this, coco_DragPanel_BounceToRightBottom_selector(UIDragPanelTest_Bounce::bounceToRightBottomEvent)); - dragPanel->addBounceToRightTopEvent(this, coco_DragPanel_BounceToRightTop_selector(UIDragPanelTest_Bounce::bounceToRightTopEvent)); - dragPanel->addBounceToLeftEvent(this, coco_DragPanel_BounceToLeft_selector(UIDragPanelTest_Bounce::bounceToLeftEvent)); - dragPanel->addBounceToTopEvent(this, coco_DragPanel_BounceToTop_selector(UIDragPanelTest_Bounce::bounceToTopEvent)); - dragPanel->addBounceToRightEvent(this, coco_DragPanel_BounceToRight_selector(UIDragPanelTest_Bounce::bounceToRightEvent)); - dragPanel->addBounceToBottomEvent(this, coco_DragPanel_BounceToBottom_selector(UIDragPanelTest_Bounce::bounceToBottomEvent)); + dragPanel->addEventListener(this, dragpaneleventselector(UIDragPanelTest_Bounce::dragPanelEvent)); UIImageView* imageView = UIImageView::create(); imageView->setTouchEnabled(true); @@ -197,42 +184,43 @@ bool UIDragPanelTest_Bounce::init() return false; } -void UIDragPanelTest_Bounce::bounceToLeftBottomEvent(Object *pSender) +void UIDragPanelTest_Bounce::dragPanelEvent(Object *pSender, DragPanelEventType type) { - m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Left Bottom")->getCString()); -} - -void UIDragPanelTest_Bounce::bounceToLeftTopEvent(Object *pSender) -{ - m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Left Top")->getCString()); -} - -void UIDragPanelTest_Bounce::bounceToRightBottomEvent(Object *pSender) -{ - m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Right Bottom")->getCString()); -} - -void UIDragPanelTest_Bounce::bounceToRightTopEvent(Object *pSender) -{ - m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Right Top")->getCString()); -} - -void UIDragPanelTest_Bounce::bounceToLeftEvent(Object *pSender) -{ - m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Left")->getCString()); -} - -void UIDragPanelTest_Bounce::bounceToTopEvent(Object *pSender) -{ - m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Top")->getCString()); -} - -void UIDragPanelTest_Bounce::bounceToRightEvent(Object *pSender) -{ - m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Right")->getCString()); -} - -void UIDragPanelTest_Bounce::bounceToBottomEvent(Object *pSender) -{ - m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Bottom")->getCString()); + switch (type) + { + case DRAGPANEL_EVENT_BOUNCE_LEFTBOTTOM: + m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Left Bottom")->getCString()); + break; + + case DRAGPANEL_EVENT_BOUNCE_LEFTTOP: + m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Left Top")->getCString()); + break; + + case DRAGPANEL_EVENT_BOUNCE_RIGHTBOTTOM: + m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Right Bottom")->getCString()); + break; + + case DRAGPANEL_EVENT_BOUNCE_RIGHTTOP: + m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Right Top")->getCString()); + break; + + case DRAGPANEL_EVENT_BOUNCE_LEFT: + m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Left")->getCString()); + break; + + case DRAGPANEL_EVENT_BOUNCE_TOP: + m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Top")->getCString()); + break; + + case DRAGPANEL_EVENT_BOUNCE_RIGHT: + m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Right")->getCString()); + break; + + case DRAGPANEL_EVENT_BOUNCE_BOTTOM: + m_pDisplayValueLabel->setText(CCString::createWithFormat("Bounce To Bottom")->getCString()); + break; + + default: + break; + } } diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIDragPanelTest/UIDragPanelTest.h b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIDragPanelTest/UIDragPanelTest.h index 11793ada27..76f8469154 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIDragPanelTest/UIDragPanelTest.h +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIDragPanelTest/UIDragPanelTest.h @@ -33,14 +33,7 @@ public: UIDragPanelTest(); ~UIDragPanelTest(); bool init(); - void berthToLeftBottomEvent(Object* pSender); - void berthToLeftTopEvent(Object* pSender); - void berthToRightBottomEvent(Object* pSender); - void berthToRightTopEvent(Object* pSender); - void berthToLeftEvent(Object* pSender); - void berthToTopEvent(Object* pSender); - void berthToRightEvent(Object* pSender); - void berthToBottomEvent(Object* pSender); + void dragPanelEvent(Object* pSender, DragPanelEventType type); protected: UI_SCENE_CREATE_FUNC(UIDragPanelTest) @@ -53,14 +46,7 @@ public: UIDragPanelTest_Bounce(); ~UIDragPanelTest_Bounce(); bool init(); - void bounceToLeftBottomEvent(Object* pSender); - void bounceToRightBottomEvent(Object* pSender); - void bounceToLeftTopEvent(Object* pSender); - void bounceToRightTopEvent(Object* pSender); - void bounceToLeftEvent(Object* pSender); - void bounceToTopEvent(Object* pSender); - void bounceToRightEvent(Object* pSender); - void bounceToBottomEvent(Object* pSender); + void dragPanelEvent(Object* pSender, DragPanelEventType type); protected: UI_SCENE_CREATE_FUNC(UIDragPanelTest_Bounce) diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIListViewTest/UIListViewTest.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIListViewTest/UIListViewTest.cpp index efb20975e2..6681c744ee 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIListViewTest/UIListViewTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIListViewTest/UIListViewTest.cpp @@ -90,8 +90,7 @@ bool UIListViewTest_Vertical::init() listView->addChild(layout); } - listView->addInitChildEvent(this, coco_ListView_InitChild_selector(UIListViewTest_Vertical::initChildEvent)); - listView->addUpdateChildEvent(this, coco_ListView_UpdateChild_selector(UIListViewTest_Vertical::updateChildEvent)); + listView->addEventListenter(this, listvieweventselector(UIListViewTest_Vertical::listViewEvent)); listView->initChildWithDataLength(m_array->count()); m_pUiLayer->addWidget(listView); @@ -101,33 +100,44 @@ bool UIListViewTest_Vertical::init() return false; } -void UIListViewTest_Vertical::initChildEvent(Object *pSender) +void UIListViewTest_Vertical::listViewEvent(Object *pSender, ListViewEventType type) { - String* ccstr = static_cast(m_array->getObjectAtIndex(m_nCount)); - UIListView* list = dynamic_cast(pSender); - - Layout* layout = dynamic_cast(list->getUpdateChild()); - UIButton* textButton = dynamic_cast(layout->getChildByName("TextButton")); - textButton->setTitleText(ccstr->getCString()); - - m_nCount++; -} - -void UIListViewTest_Vertical::updateChildEvent(Object *pSender) -{ - UIListView* list = dynamic_cast(pSender); - int index = list->getUpdateDataIndex(); - - if (index < 0 || index >= list->getDataLength()) + switch (type) { - list->setUpdateSuccess(false); + case LISTVIEW_EVENT_INIT_CHILD: + { + String* ccstr = static_cast(m_array->getObjectAtIndex(m_nCount)); + UIListView* list = dynamic_cast(pSender); + + Layout* layout = dynamic_cast(list->getUpdateChild()); + UIButton* textButton = dynamic_cast(layout->getChildByName("TextButton")); + textButton->setTitleText(ccstr->getCString()); + + m_nCount++; + } + break; + + case LISTVIEW_EVENT_UPDATE_CHILD: + { + UIListView* list = dynamic_cast(pSender); + int index = list->getUpdateDataIndex(); + + if (index < 0 || index >= list->getDataLength()) + { + list->setUpdateSuccess(false); + } + + String* ccstr = static_cast(m_array->getObjectAtIndex(index)); + Layout* layout = dynamic_cast(list->getUpdateChild()); + UIButton* textButton = dynamic_cast(layout->getChildByName("TextButton")); + textButton->setTitleText(ccstr->getCString()); + list->setUpdateSuccess(true); + } + break; + + default: + break; } - - String* ccstr = static_cast(m_array->getObjectAtIndex(index)); - Layout* layout = dynamic_cast(list->getUpdateChild()); - UIButton* textButton = dynamic_cast(layout->getChildByName("TextButton")); - textButton->setTitleText(ccstr->getCString()); - list->setUpdateSuccess(true); } // UIListViewTest_Horizontal @@ -211,8 +221,7 @@ bool UIListViewTest_Horizontal::init() listView->addChild(layout); } - listView->addInitChildEvent(this, coco_ListView_InitChild_selector(UIListViewTest_Horizontal::initChildEvent)); - listView->addUpdateChildEvent(this, coco_ListView_UpdateChild_selector(UIListViewTest_Horizontal::updateChildEvent)); + listView->addEventListenter(this, listvieweventselector(UIListViewTest_Horizontal::listViewEvent)); listView->initChildWithDataLength(m_array->count()); m_pUiLayer->addWidget(listView); @@ -222,31 +231,42 @@ bool UIListViewTest_Horizontal::init() return false; } -void UIListViewTest_Horizontal::initChildEvent(Object *pSender) +void UIListViewTest_Horizontal::listViewEvent(Object *pSender, ListViewEventType type) { - String* ccstr = static_cast(m_array->getObjectAtIndex(m_nCount)); - UIListView* list = dynamic_cast(pSender); - - Layout* layout = dynamic_cast(list->getUpdateChild()); - UIButton* textButton = dynamic_cast(layout->getChildByName("TextButton")); - textButton->setTitleText(ccstr->getCString()); - - m_nCount++; -} - -void UIListViewTest_Horizontal::updateChildEvent(Object *pSender) -{ - UIListView* list = dynamic_cast(pSender); - int index = list->getUpdateDataIndex(); - - if (index < 0 || index >= list->getDataLength()) + switch (type) { - list->setUpdateSuccess(false); + case LISTVIEW_EVENT_INIT_CHILD: + { + String* ccstr = static_cast(m_array->getObjectAtIndex(m_nCount)); + UIListView* list = dynamic_cast(pSender); + + Layout* layout = dynamic_cast(list->getUpdateChild()); + UIButton* textButton = dynamic_cast(layout->getChildByName("TextButton")); + textButton->setTitleText(ccstr->getCString()); + + m_nCount++; + } + break; + + case LISTVIEW_EVENT_UPDATE_CHILD: + { + UIListView* list = dynamic_cast(pSender); + int index = list->getUpdateDataIndex(); + + if (index < 0 || index >= list->getDataLength()) + { + list->setUpdateSuccess(false); + } + + String* ccstr = static_cast(m_array->getObjectAtIndex(index)); + Layout* layout = dynamic_cast(list->getUpdateChild()); + UIButton* textButton = dynamic_cast(layout->getChildByName("TextButton")); + textButton->setTitleText(ccstr->getCString()); + list->setUpdateSuccess(true); + } + break; + + default: + break; } - - String* ccstr = static_cast(m_array->getObjectAtIndex(index)); - Layout* layout = dynamic_cast(list->getUpdateChild()); - UIButton* textButton = dynamic_cast(layout->getChildByName("TextButton")); - textButton->setTitleText(ccstr->getCString()); - list->setUpdateSuccess(true); -} \ No newline at end of file +} diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIListViewTest/UIListViewTest.h b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIListViewTest/UIListViewTest.h index bc170a6c34..b2725e33ee 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIListViewTest/UIListViewTest.h +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIListViewTest/UIListViewTest.h @@ -32,9 +32,8 @@ class UIListViewTest_Vertical : public UIScene public: UIListViewTest_Vertical(); ~UIListViewTest_Vertical(); - bool init(); - void initChildEvent(Object* pSender); - void updateChildEvent(Object* pSender); + bool init(); + void listViewEvent(Object* pSender, ListViewEventType type); protected: UI_SCENE_CREATE_FUNC(UIListViewTest_Vertical) @@ -50,8 +49,7 @@ public: UIListViewTest_Horizontal(); ~UIListViewTest_Horizontal(); bool init(); - void initChildEvent(Object* pSender); - void updateChildEvent(Object* pSender); + void listViewEvent(Object* pSender, ListViewEventType type); protected: UI_SCENE_CREATE_FUNC(UIListViewTest_Horizontal) diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIPageViewTest/UIPageViewTest.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIPageViewTest/UIPageViewTest.cpp index faaf4e7927..22b97ef9e6 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIPageViewTest/UIPageViewTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIPageViewTest/UIPageViewTest.cpp @@ -79,8 +79,7 @@ bool UIPageViewTest::init() pageView->addPage(layout); } - - pageView->addPageTurningEvent(this, coco_PageView_PageTurning_selector(UIPageViewTest::pageTurningEvent)); + pageView->addEventListener(this, pagevieweventselector(UIPageViewTest::pageViewEvent)); m_pUiLayer->addWidget(pageView); @@ -89,10 +88,19 @@ bool UIPageViewTest::init() return false; } -void UIPageViewTest::pageTurningEvent(Object *pSender) +void UIPageViewTest::pageViewEvent(Object *pSender, PageViewEventType type) { - UIPageView* pageView = dynamic_cast(pSender); - CCLOG("page = %d", pageView->getCurPageIndex()); - - m_pDisplayValueLabel->setText(CCString::createWithFormat("page = %d", pageView->getCurPageIndex() + 1)->getCString()); -} \ No newline at end of file + switch (type) + { + case PAGEVIEW_EVENT_TURNING: + { + UIPageView* pageView = dynamic_cast(pSender); + + m_pDisplayValueLabel->setText(CCString::createWithFormat("page = %d", pageView->getCurPageIndex() + 1)->getCString()); + } + break; + + default: + break; + } +} diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIPageViewTest/UIPageViewTest.h b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIPageViewTest/UIPageViewTest.h index 3ae0e4e96d..a24e099a4d 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIPageViewTest/UIPageViewTest.h +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIPageViewTest/UIPageViewTest.h @@ -34,7 +34,7 @@ public: ~UIPageViewTest(); bool init(); - void pageTurningEvent(Object* pSender); + void pageViewEvent(Object* pSender, PageViewEventType type); protected: UI_SCENE_CREATE_FUNC(UIPageViewTest) diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UISliderTest/UISliderTest.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UISliderTest/UISliderTest.cpp index 1e53c27f34..2749e061ab 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UISliderTest/UISliderTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UISliderTest/UISliderTest.cpp @@ -52,7 +52,7 @@ bool UISliderTest::init() slider->loadSlidBallTextures("cocosgui/sliderThumb.png", "cocosgui/sliderThumb.png", ""); slider->loadProgressBarTexture("cocosgui/sliderProgress.png"); slider->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f)); - slider->addPercentEvent(this, sliderpercentchangedselector(UISliderTest::percentChangedEvent)); + slider->addEventListener(this, sliderpercentchangedselector(UISliderTest::percentChangedEvent)); m_pUiLayer->addWidget(slider); return true; @@ -116,7 +116,7 @@ bool UISliderTest_Scale9::init() slider->setCapInsets(Rect(0, 0, 0, 0)); slider->setSize(Size(250, 10)); slider->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f)); - slider->addPercentEvent(this, sliderpercentchangedselector(UISliderTest_Scale9::percentChangedEvent)); + slider->addEventListener(this, sliderpercentchangedselector(UISliderTest_Scale9::percentChangedEvent)); m_pUiLayer->addWidget(slider); return true; diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp index 61279c8458..3e85c987b4 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp @@ -51,10 +51,7 @@ bool UITextFieldTest::init() textField->setFontSize(30); textField->setPlaceHolder("input words here"); textField->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f)); - textField->addAttachWithIMEEvent(this, coco_TextField_AttachWithIME_selector(UITextFieldTest::attachWithIMEEvent)); - textField->addDetachWithIMEEvent(this, coco_TextField_DetachWithIME_selector(UITextFieldTest::detachWithIMEEvent)); - textField->addInsertTextEvent(this, coco_TextField_InsertText_selector(UITextFieldTest::insertTextEvent)); - textField->addDeleteBackwardEvent(this, coco_TextField_DeleteBackward_selector(UITextFieldTest::deleteBackwardEvent)); + textField->addEventListener(this, textfieldeventselector(UITextFieldTest::textFieldEvent)); m_pUiLayer->addWidget(textField); return true; @@ -62,31 +59,40 @@ bool UITextFieldTest::init() return false; } -void UITextFieldTest::attachWithIMEEvent(Object *pSender) +void UITextFieldTest::textFieldEvent(Object *pSender, TextFiledEventType type) { - UITextField* textField = dynamic_cast(pSender); - Size screenSize = CCDirector::getInstance()->getWinSize(); - textField->runAction(CCMoveTo::create(0.225, - Point(screenSize.width / 2.0f, screenSize.height / 2.0f + textField->getContentSize().height / 2))); - m_pDisplayValueLabel->setText(CCString::createWithFormat("attach with IME")->getCString()); -} - -void UITextFieldTest::detachWithIMEEvent(Object* pSender) -{ - UITextField* textField = dynamic_cast(pSender); - Size screenSize = CCDirector::getInstance()->getWinSize(); - textField->runAction(CCMoveTo::create(0.175, Point(screenSize.width / 2.0f, screenSize.height / 2.0f))); - m_pDisplayValueLabel->setText(CCString::createWithFormat("detach with IME")->getCString()); -} - -void UITextFieldTest::insertTextEvent(Object *pSender) -{ - m_pDisplayValueLabel->setText(CCString::createWithFormat("insert words")->getCString()); -} - -void UITextFieldTest::deleteBackwardEvent(Object *pSender) -{ - m_pDisplayValueLabel->setText(CCString::createWithFormat("delete word")->getCString()); + switch (type) + { + case TEXTFIELD_EVENT_ATTACH_WITH_IME: + { + UITextField* textField = dynamic_cast(pSender); + Size screenSize = CCDirector::getInstance()->getWinSize(); + textField->runAction(CCMoveTo::create(0.225, + Point(screenSize.width / 2.0f, screenSize.height / 2.0f + textField->getContentSize().height / 2))); + m_pDisplayValueLabel->setText(CCString::createWithFormat("attach with IME")->getCString()); + } + break; + + case TEXTFIELD_EVENT_DETACH_WITH_IME: + { + UITextField* textField = dynamic_cast(pSender); + Size screenSize = CCDirector::getInstance()->getWinSize(); + textField->runAction(CCMoveTo::create(0.175, Point(screenSize.width / 2.0f, screenSize.height / 2.0f))); + m_pDisplayValueLabel->setText(CCString::createWithFormat("detach with IME")->getCString()); + } + break; + + case TEXTFIELD_EVENT_INDERT_TEXT: + m_pDisplayValueLabel->setText(CCString::createWithFormat("insert words")->getCString()); + break; + + case TEXTFIELD_EVENT_DELETE_BACKWARD: + m_pDisplayValueLabel->setText(CCString::createWithFormat("delete word")->getCString()); + break; + + default: + break; + } } // UITextFieldTest_MaxLength @@ -133,10 +139,7 @@ bool UITextFieldTest_MaxLength::init() textField->setFontSize(30); textField->setPlaceHolder("input words here"); textField->setPosition(Point(screenSize.width / 2.0f, screenSize.height / 2.0f)); - textField->addAttachWithIMEEvent(this, coco_TextField_AttachWithIME_selector(UITextFieldTest_MaxLength::attachWithIMEEvent)); - textField->addDetachWithIMEEvent(this, coco_TextField_DetachWithIME_selector(UITextFieldTest_MaxLength::detachWithIMEEvent)); - textField->addInsertTextEvent(this, coco_TextField_InsertText_selector(UITextFieldTest_MaxLength::insertTextEvent)); - textField->addDeleteBackwardEvent(this, coco_TextField_DeleteBackward_selector(UITextFieldTest_MaxLength::deleteBackwardEvent)); + textField->addEventListener(this, textfieldeventselector(UITextFieldTest_MaxLength::textFieldEvent)); m_pUiLayer->addWidget(textField); return true; @@ -144,33 +147,46 @@ bool UITextFieldTest_MaxLength::init() return false; } -void UITextFieldTest_MaxLength::attachWithIMEEvent(Object *pSender) +void UITextFieldTest_MaxLength::textFieldEvent(Object *pSender, TextFiledEventType type) { - UITextField* textField = dynamic_cast(pSender); - Size screenSize = CCDirector::getInstance()->getWinSize(); - textField->runAction(CCMoveTo::create(0.225, - Point(screenSize.width / 2.0f, screenSize.height / 2.0f + textField->getContentSize().height / 2))); - m_pDisplayValueLabel->setText(CCString::createWithFormat("attach with IME max length %d", textField->getMaxLength())->getCString()); -} - -void UITextFieldTest_MaxLength::detachWithIMEEvent(Object* pSender) -{ - UITextField* textField = dynamic_cast(pSender); - Size screenSize = CCDirector::getInstance()->getWinSize(); - textField->runAction(CCMoveTo::create(0.175, Point(screenSize.width / 2.0f, screenSize.height / 2.0f))); - m_pDisplayValueLabel->setText(CCString::createWithFormat("detach with IME max length %d", textField->getMaxLength())->getCString()); -} - -void UITextFieldTest_MaxLength::insertTextEvent(Object *pSender) -{ - UITextField* textField = dynamic_cast(pSender); - m_pDisplayValueLabel->setText(CCString::createWithFormat("insert words max length %d", textField->getMaxLength())->getCString()); -} - -void UITextFieldTest_MaxLength::deleteBackwardEvent(Object *pSender) -{ - UITextField* textField = dynamic_cast(pSender); - m_pDisplayValueLabel->setText(CCString::createWithFormat("delete word max length %d", textField->getMaxLength())->getCString()); + switch (type) + { + case TEXTFIELD_EVENT_ATTACH_WITH_IME: + { + UITextField* textField = dynamic_cast(pSender); + Size screenSize = CCDirector::getInstance()->getWinSize(); + textField->runAction(CCMoveTo::create(0.225, + Point(screenSize.width / 2.0f, screenSize.height / 2.0f + textField->getContentSize().height / 2))); + m_pDisplayValueLabel->setText(CCString::createWithFormat("attach with IME max length %d", textField->getMaxLength())->getCString()); + } + break; + + case TEXTFIELD_EVENT_DETACH_WITH_IME: + { + UITextField* textField = dynamic_cast(pSender); + Size screenSize = CCDirector::getInstance()->getWinSize(); + textField->runAction(CCMoveTo::create(0.175, Point(screenSize.width / 2.0f, screenSize.height / 2.0f))); + m_pDisplayValueLabel->setText(CCString::createWithFormat("detach with IME max length %d", textField->getMaxLength())->getCString()); + } + break; + + case TEXTFIELD_EVENT_INDERT_TEXT: + { + UITextField* textField = dynamic_cast(pSender); + m_pDisplayValueLabel->setText(CCString::createWithFormat("insert words max length %d", textField->getMaxLength())->getCString()); + } + break; + + case TEXTFIELD_EVENT_DELETE_BACKWARD: + { + UITextField* textField = dynamic_cast(pSender); + m_pDisplayValueLabel->setText(CCString::createWithFormat("delete word max length %d", textField->getMaxLength())->getCString()); + } + break; + + default: + break; + } } // UITextFieldTest_Password @@ -217,10 +233,7 @@ bool UITextFieldTest_Password::init() textField->setFontSize(30); textField->setPlaceHolder("input password here"); textField->setPosition(Point(screenSize.width / 2.0f, screenSize.height / 2.0f)); - textField->addAttachWithIMEEvent(this, coco_TextField_AttachWithIME_selector(UITextFieldTest_Password::attachWithIMEEvent)); - textField->addDetachWithIMEEvent(this, coco_TextField_DetachWithIME_selector(UITextFieldTest_Password::detachWithIMEEvent)); - textField->addInsertTextEvent(this, coco_TextField_InsertText_selector(UITextFieldTest_Password::insertTextEvent)); - textField->addDeleteBackwardEvent(this, coco_TextField_DeleteBackward_selector(UITextFieldTest_Password::deleteBackwardEvent)); + textField->addEventListener(this, textfieldeventselector(UITextFieldTest_Password::textFieldEvent)); m_pUiLayer->addWidget(textField); return true; @@ -228,29 +241,38 @@ bool UITextFieldTest_Password::init() return false; } -void UITextFieldTest_Password::attachWithIMEEvent(Object *pSender) +void UITextFieldTest_Password::textFieldEvent(Object *pSender, TextFiledEventType type) { - UITextField* textField = dynamic_cast(pSender); - Size screenSize = CCDirector::getInstance()->getWinSize(); - textField->runAction(CCMoveTo::create(0.225, - Point(screenSize.width / 2.0f, screenSize.height / 2.0f + textField->getContentSize().height / 2))); - m_pDisplayValueLabel->setText(CCString::createWithFormat("attach with IME password")->getCString()); -} - -void UITextFieldTest_Password::detachWithIMEEvent(Object* pSender) -{ - UITextField* textField = dynamic_cast(pSender); - Size screenSize = CCDirector::getInstance()->getWinSize(); - textField->runAction(CCMoveTo::create(0.175, Point(screenSize.width / 2.0f, screenSize.height / 2.0f))); - m_pDisplayValueLabel->setText(CCString::createWithFormat("detach with IME password")->getCString()); -} - -void UITextFieldTest_Password::insertTextEvent(Object *pSender) -{ - m_pDisplayValueLabel->setText(CCString::createWithFormat("insert words password")->getCString()); -} - -void UITextFieldTest_Password::deleteBackwardEvent(Object *pSender) -{ - m_pDisplayValueLabel->setText(CCString::createWithFormat("delete word password")->getCString()); + switch (type) + { + case TEXTFIELD_EVENT_ATTACH_WITH_IME: + { + UITextField* textField = dynamic_cast(pSender); + Size screenSize = CCDirector::getInstance()->getWinSize(); + textField->runAction(CCMoveTo::create(0.225, + Point(screenSize.width / 2.0f, screenSize.height / 2.0f + textField->getContentSize().height / 2))); + m_pDisplayValueLabel->setText(CCString::createWithFormat("attach with IME password")->getCString()); + } + break; + + case TEXTFIELD_EVENT_DETACH_WITH_IME: + { + UITextField* textField = dynamic_cast(pSender); + Size screenSize = CCDirector::getInstance()->getWinSize(); + textField->runAction(CCMoveTo::create(0.175, Point(screenSize.width / 2.0f, screenSize.height / 2.0f))); + m_pDisplayValueLabel->setText(CCString::createWithFormat("detach with IME password")->getCString()); + } + break; + + case TEXTFIELD_EVENT_INDERT_TEXT: + m_pDisplayValueLabel->setText(CCString::createWithFormat("insert words password")->getCString()); + break; + + case TEXTFIELD_EVENT_DELETE_BACKWARD: + m_pDisplayValueLabel->setText(CCString::createWithFormat("delete word password")->getCString()); + break; + + default: + break; + } } diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.h b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.h index 3ab1dedae1..10674258f1 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.h +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.h @@ -33,10 +33,7 @@ public: UITextFieldTest(); ~UITextFieldTest(); bool init(); - void attachWithIMEEvent(Object* pSender); - void detachWithIMEEvent(Object* pSender); - void insertTextEvent(Object* pSender); - void deleteBackwardEvent(Object* pSender); + void textFieldEvent(Object* pSender, TextFiledEventType type); protected: UI_SCENE_CREATE_FUNC(UITextFieldTest) @@ -49,10 +46,7 @@ public: UITextFieldTest_MaxLength(); ~UITextFieldTest_MaxLength(); bool init(); - void attachWithIMEEvent(Object* pSender); - void detachWithIMEEvent(Object* pSender); - void insertTextEvent(Object* pSender); - void deleteBackwardEvent(Object* pSender); + void textFieldEvent(Object* pSender, TextFiledEventType type); protected: UI_SCENE_CREATE_FUNC(UITextFieldTest_MaxLength) @@ -65,10 +59,7 @@ public: UITextFieldTest_Password(); ~UITextFieldTest_Password(); bool init(); - void attachWithIMEEvent(Object* pSender); - void detachWithIMEEvent(Object* pSender); - void insertTextEvent(Object* pSender); - void deleteBackwardEvent(Object* pSender); + void textFieldEvent(Object* pSender, TextFiledEventType type); protected: UI_SCENE_CREATE_FUNC(UITextFieldTest_Password) From 841408d1bc255444c372c924763bc27c34bb1de2 Mon Sep 17 00:00:00 2001 From: CaiWenzhi Date: Tue, 17 Sep 2013 20:34:26 +0800 Subject: [PATCH 26/28] Modify events methods --- .../UIWidgets/ScrollWidget/UIScrollView.cpp | 52 +++++-------------- .../GUI/UIWidgets/ScrollWidget/UIScrollView.h | 50 ++++++------------ 2 files changed, 30 insertions(+), 72 deletions(-) diff --git a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIScrollView.cpp b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIScrollView.cpp index 0881f5f98a..1a64d69778 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIScrollView.cpp +++ b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIScrollView.cpp @@ -28,6 +28,7 @@ NS_CC_EXT_BEGIN UIScrollView::UIScrollView(): +_innerContainer(NULL), _direction(SCROLLVIEW_DIR_VERTICAL), _moveDirection(SCROLLVIEW_MOVE_DIR_NONE), _touchStartLocation(0.0f), @@ -48,15 +49,8 @@ _bePressed(false), _slidTime(0.0f), _moveChildPoint(Point::ZERO), _childFocusCancelOffset(5.0f), -_scrollToTopListener(NULL), -_scrollToTopSelector(NULL), -_scrollToBottomListener(NULL), -_scrollToBottomSelector(NULL), -_scrollToLeftListener(NULL), -_scrollToLeftSelector(NULL), -_scrollToRightListener(NULL), -_scrollToRightSelector(NULL), -_innerContainer(NULL) +_eventListener(NULL), +_eventSelector(NULL) { } @@ -609,58 +603,40 @@ void UIScrollView::checkChildInfo(int handleState,UIWidget* sender,const Point & void UIScrollView::scrollToTopEvent() { - if (_scrollToTopListener && _scrollToTopSelector) + if (_eventListener && _eventSelector) { - (_scrollToTopListener->*_scrollToTopSelector)(this); + (_eventListener->*_eventSelector)(this, SCROLLVIEW_EVENT_SCROLL_TO_TOP); } } void UIScrollView::scrollToBottomEvent() { - if (_scrollToBottomListener && _scrollToBottomSelector) + if (_eventListener && _eventSelector) { - (_scrollToBottomListener->*_scrollToBottomSelector)(this); + (_eventListener->*_eventSelector)(this, SCROLLVIEW_EVENT_SCROLL_TO_BOTTOM); } } void UIScrollView::scrollToLeftEvent() { - if (_scrollToLeftListener && _scrollToLeftSelector) + if (_eventListener && _eventSelector) { - (_scrollToLeftListener->*_scrollToLeftSelector)(this); + (_eventListener->*_eventSelector)(this, SCROLLVIEW_EVENT_SCROLL_TO_LEFT); } } void UIScrollView::scrollToRightEvent() { - if (_scrollToRightListener && _scrollToRightSelector) + if (_eventListener && _eventSelector) { - (_scrollToRightListener->*_scrollToRightSelector)(this); + (_eventListener->*_eventSelector)(this, SCROLLVIEW_EVENT_SCROLL_TO_RIGHT); } } -void UIScrollView::addScrollToTopEvent(Object *target, SEL_ScrollToTopEvent selector) +void UIScrollView::addEventListener(Object *target, SEL_ScrollViewEvent selector) { - _scrollToTopListener = target; - _scrollToTopSelector = selector; -} - -void UIScrollView::addScrollToBottomEvent(Object *target, SEL_ScrollToBottomEvent selector) -{ - _scrollToBottomListener = target; - _scrollToBottomSelector = selector; -} - -void UIScrollView::addScrollToLeftEvent(Object *target, SEL_ScrollToLeftEvent selector) -{ - _scrollToLeftListener = target; - _scrollToLeftSelector = selector; -} - -void UIScrollView::addScrollToRightEvent(Object *target, SEL_ScrollToRightEvent selector) -{ - _scrollToRightListener = target; - _scrollToRightSelector = selector; + _eventListener = target; + _eventSelector = selector; } void UIScrollView::setDirection(SCROLLVIEW_DIR dir) diff --git a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIScrollView.h b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIScrollView.h index 67d1995c08..f2ef9157ee 100644 --- a/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIScrollView.h +++ b/extensions/CocoStudio/GUI/UIWidgets/ScrollWidget/UIScrollView.h @@ -46,14 +46,16 @@ enum SCROLLVIEW_MOVE_DIR SCROLLVIEW_MOVE_DIR_RIGHT, }; -typedef void (Object::*SEL_ScrollToTopEvent)(Object*); -typedef void (Object::*SEL_ScrollToBottomEvent)(Object*); -typedef void (Object::*SEL_ScrollToLeftEvent)(Object*); -typedef void (Object::*SEL_ScrollToRightEvent)(Object*); -#define coco_ScrollToTopSelector(_SELECTOR) (cocos2d::extension::SEL_ScrollToTopEvent)(&_SELECTOR) -#define coco_ScrollToBottomSelector(_SELECTOR) (cocos2d::extension::SEL_ScrollToBottomEvent)(&_SELECTOR) -#define coco_ScrollToLeftSelector(_SELECTOR) (cocos2d::extension::SEL_ScrollToLeftEvent)(&_SELECTOR) -#define coco_ScrollToRightSelector(_SELECTOR) (cocos2d::extension::SEL_ScrollToRightEvent)(&_SELECTOR) +typedef enum +{ + SCROLLVIEW_EVENT_SCROLL_TO_TOP, + SCROLLVIEW_EVENT_SCROLL_TO_BOTTOM, + SCROLLVIEW_EVENT_SCROLL_TO_LEFT, + SCROLLVIEW_EVENT_SCROLL_TO_RIGHT, +}ScrollviewEventType; + +typedef void (Object::*SEL_ScrollViewEvent)(Object*, ScrollviewEventType); +#define scrollvieweventselector(_SELECTOR) (SEL_ScrollViewEvent)(&_SELECTOR) class UIScrollView : public Layout , public UIScrollInterface @@ -130,24 +132,9 @@ public: const Size& getInnerContainerSize() const; /** - * Add call back function called when scrollview scrolled to top. + * Add call back function called scrollview event triggered */ - void addScrollToTopEvent(Object* target, SEL_ScrollToTopEvent selector); - - /** - * Add call back function called when scrollview scrolled to bottom. - */ - void addScrollToBottomEvent(Object* target, SEL_ScrollToBottomEvent selector); - - /** - * Add call back function called when scrollview scrolled to left. - */ - void addScrollToLeftEvent(Object* target, SEL_ScrollToLeftEvent selector); - - /** - * Add call back function called when scrollview scrolled to right. - */ - void addScrollToRightEvent(Object* target, SEL_ScrollToRightEvent selector); + void addEventListener(Object* target, SEL_ScrollViewEvent selector); //override "setLayoutExecutant" method of widget. virtual void setLayoutExecutant(LayoutExecutant* exe); @@ -217,6 +204,8 @@ protected: virtual void onSizeChanged(); virtual void setClippingEnabled(bool able){Layout::setClippingEnabled(able);}; protected: + Layout* _innerContainer; + SCROLLVIEW_DIR _direction; SCROLLVIEW_MOVE_DIR _moveDirection; float _touchStartLocation; @@ -242,16 +231,9 @@ protected: Point _moveChildPoint; float _childFocusCancelOffset; - Object* _scrollToTopListener; - SEL_ScrollToTopEvent _scrollToTopSelector; - Object* _scrollToBottomListener; - SEL_ScrollToBottomEvent _scrollToBottomSelector; - Object* _scrollToLeftListener; - SEL_ScrollToLeftEvent _scrollToLeftSelector; - Object* _scrollToRightListener; - SEL_ScrollToRightEvent _scrollToRightSelector; + Object* _eventListener; + SEL_ScrollViewEvent _eventSelector; - Layout* _innerContainer; }; NS_CC_EXT_END From 1f45bad5e72c93efffd296a3943e4f56f25692dd Mon Sep 17 00:00:00 2001 From: CaiWenzhi Date: Tue, 17 Sep 2013 21:19:04 +0800 Subject: [PATCH 27/28] Modify guy example --- .../CocoStudio/GUI/System/UIInputManager.cpp | 3 --- .../CocoStudioGUITest/UIScene.cpp | 26 ++++++++++--------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/extensions/CocoStudio/GUI/System/UIInputManager.cpp b/extensions/CocoStudio/GUI/System/UIInputManager.cpp index b1ed1668a7..21371d481a 100644 --- a/extensions/CocoStudio/GUI/System/UIInputManager.cpp +++ b/extensions/CocoStudio/GUI/System/UIInputManager.cpp @@ -45,7 +45,6 @@ _rootWidget(NULL) UIInputManager::~UIInputManager() { - CCLOG("~UIInputManager"); _manageredWidget->removeAllObjects(); CC_SAFE_RELEASE_NULL(_manageredWidget); _checkedDoubleClickWidget->removeAllObjects(); @@ -177,9 +176,7 @@ void UIInputManager::onTouchEnd(Touch* touch) { UIWidget* hitWidget = (UIWidget*)(selectedWidgetArray->arr[i]); hitWidget->onTouchEnded(_touchEndedPoint); - CCLOG("widget touch end"); } - CCLOG("_selectedWidgets remove widgets"); _selectedWidgets->removeAllObjects(); } diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIScene.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIScene.cpp index 920449e71a..f1ed704289 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIScene.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioGUITest/UIScene.cpp @@ -2,6 +2,7 @@ #include "cocos-ext.h" #include "UIScene.h" #include "UISceneManager.h" +#include "../ExtensionsTest.h" UIScene::UIScene() : m_pSceneTitle(NULL) @@ -40,13 +41,16 @@ bool UIScene::init() UIButton *right_button = dynamic_cast(m_pUiLayer->getWidgetByName("right_Button")); right_button->addTouchEventListener(this, toucheventselector(UIScene::nextCallback)); - // exit button - UIButton* exit_button = UIButton::create(); - exit_button->setTouchEnabled(true); - exit_button->loadTextures("CloseNormal.png", "CloseSelected.png", ""); - exit_button->setPosition(Point(m_pUiLayer->getContentSize().width - exit_button->getContentSize().width, exit_button->getContentSize().height)); - exit_button->addTouchEventListener(this, toucheventselector(UIScene::menuCloseCallback)); - m_pUiLayer->addWidget(exit_button); + + UILabel* mainMenuLabel = UILabel::create(); + mainMenuLabel->setText("MainMenu"); + mainMenuLabel->setFontSize(20); + mainMenuLabel->setTouchScaleChangeEnabled(true); + mainMenuLabel->setPosition(Point(430,30)); + mainMenuLabel->setTouchEnabled(true); + mainMenuLabel->addTouchEventListener(this, toucheventselector(UIScene::menuCloseCallback)); + m_pUiLayer->addWidget(mainMenuLabel); + return true; } @@ -57,12 +61,10 @@ void UIScene::menuCloseCallback(Object* pSender, TouchEventType type) { if (type == TOUCH_EVENT_ENDED) { - CCDirector::getInstance()->end(); + auto scene = new ExtensionsTestScene(); + scene->runThisTest(); + scene->release(); } - -#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) - exit(0); -#endif } void UIScene::previousCallback(Object* sender, TouchEventType type) From b1c88712083c2b64c67a7eb7674e3d1f0c541f7f Mon Sep 17 00:00:00 2001 From: minggo Date: Tue, 17 Sep 2013 22:36:15 +0800 Subject: [PATCH 28/28] linux build ok --- Makefile | 4 ++-- cocos2dx/proj.linux/Makefile | 38 ++++++++++++++++++++++----------- cocos2dx/proj.linux/cocos2dx.mk | 3 +-- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index fb820a5881..d82b65ec0f 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ box2d: box2d-clean: $(MAKE) -C external/Box2D/proj.$(PLATFORM) clean -libextensions: cocosdenshion chipmunk box2d +libextensions: chipmunk cocosdenshion box2d $(MAKE) -C extensions/proj.$(PLATFORM) libextensions-clean: $(MAKE) -C extensions/proj.$(PLATFORM) clean @@ -47,7 +47,7 @@ simplegame: libcocos2dx simplegame-clean: $(MAKE) -C samples/Cpp/SimpleGame/proj.$(PLATFORM) clean -all: box2d cocosdenshion libextensions libcocos2dx lua hellocpp testcpp simplegame +all: chipmunk cocosdenshion libextensions libcocos2dx lua hellocpp testcpp simplegame clean: libcocos2dx-clean box2d-clean chipmunk-clean cocosdenshion-clean libextensions-clean lua-clean hellocpp-clean testcpp-clean simplegame-clean # Haven't yet got the lua projects working with emscripten diff --git a/cocos2dx/proj.linux/Makefile b/cocos2dx/proj.linux/Makefile index a1234a9f90..c063e48a34 100644 --- a/cocos2dx/proj.linux/Makefile +++ b/cocos2dx/proj.linux/Makefile @@ -1,17 +1,17 @@ TARGET = libcocos2d.so INCLUDES += \ - -I../platform/third_party/linux/libfreetype2 \ - -I../platform/third_party/common/etc \ - -I../platform/third_party/common/s3tc \ - -I../platform/third_party/common/atitc \ - -I../../extensions \ - -I../../extensions/CCBReader \ - -I../../extensions/GUI/CCControlExtension \ - -I../../extensions/GUI/CCControlExtension \ - -I../../external/chipmunk/include/chipmunk \ - -I../../extensions/network \ - -I../../extensions/Components \ + -I../platform/third_party/linux/libfreetype2 \ + -I../platform/third_party/common/etc \ + -I../platform/third_party/common/s3tc \ + -I../platform/third_party/common/atitc \ + -I../../extensions \ + -I../../extensions/CCBReader \ + -I../../extensions/GUI/CCControlExtension \ + -I../../extensions/GUI/CCControlExtension \ + -I../../external/chipmunk/include/chipmunk \ + -I../../extensions/network \ + -I../../extensions/Components SOURCES = ../actions/CCAction.cpp \ ../actions/CCActionCamera.cpp \ @@ -81,9 +81,19 @@ SOURCES = ../actions/CCAction.cpp \ ../particle_nodes/CCParticleSystem.cpp \ ../particle_nodes/CCParticleSystemQuad.cpp \ ../particle_nodes/CCParticleBatchNode.cpp \ +../physics/Box2D/CCPhysicsContactInfo.cpp \ +../physics/Box2D/CCPhysicsJointInfo.cpp \ +../physics/Box2D/CCPhysicsShapeInfo.cpp \ +../physics/Box2D/CCPhysicsBodyInfo.cpp \ +../physics/Box2D/CCPhysicsWorldInfo.cpp \ +../physics/chipmunk/CCPhysicsContactInfo.cpp \ +../physics/chipmunk/CCPhysicsJointInfo.cpp \ +../physics/chipmunk/CCPhysicsShapeInfo.cpp \ +../physics/chipmunk/CCPhysicsBodyInfo.cpp \ +../physics/chipmunk/CCPhysicsWorldInfo.cpp \ ../physics/CCPhysicsBody.cpp \ -../physics/CCPhysicsContactDelegate.cpp \ -../physics/CCPhysicsFixture.cpp \ +../physics/CCPhysicsContact.cpp \ +../physics/CCPhysicsShape.cpp \ ../physics/CCPhysicsJoint.cpp \ ../physics/CCPhysicsWorld.cpp \ ../platform/CCSAXParser.cpp \ @@ -167,6 +177,8 @@ include cocos2dx.mk CXXFLAGS += -Wno-sequence-point CCFLAGS += -Wno-sequence-point +STATICLIBS += $(LIB_DIR)/libchipmunk.a + TARGET := $(LIB_DIR)/$(TARGET) all: $(TARGET) diff --git a/cocos2dx/proj.linux/cocos2dx.mk b/cocos2dx/proj.linux/cocos2dx.mk index 061b5b9a5d..29c1f8f3fd 100644 --- a/cocos2dx/proj.linux/cocos2dx.mk +++ b/cocos2dx/proj.linux/cocos2dx.mk @@ -59,8 +59,7 @@ INCLUDES += \ -I$(COCOS_SRC)/platform/linux \ -I$(COCOS_SRC)/platform/third_party/linux/libjpeg \ -I$(COCOS_SRC)/platform/third_party/linux/libtiff \ - -I$(COCOS_SRC)/platform/third_party/linux/libwebp \ - -I$(COCOS_ROOT/external/chipmunk/include/chipmunk + -I$(COCOS_SRC)/platform/third_party/linux/libwebp LBITS := $(shell getconf LONG_BIT) ifeq ($(LBITS),64)