From 14d51f9f35508dae8c12cbc5b2695982503c768e Mon Sep 17 00:00:00 2001 From: chengstory Date: Thu, 14 Nov 2013 13:28:42 +0800 Subject: [PATCH] fixed #3185 --- .../cocostudio/CCComAttribute.cpp | 129 +----------------- .../cocostudio/CCComAttribute.h | 17 +-- .../cocostudio/CCComController.h | 10 +- .../ComponentsTestScene.h | 2 +- .../EnemyController.h | 8 +- .../PlayerController.h | 10 +- .../ProjectileController.h | 8 +- .../SceneController.cpp | 6 +- .../SceneController.h | 8 +- 9 files changed, 28 insertions(+), 170 deletions(-) diff --git a/cocos/editor-support/cocostudio/CCComAttribute.cpp b/cocos/editor-support/cocostudio/CCComAttribute.cpp index f3e57eddbc..16cd6d5a6c 100644 --- a/cocos/editor-support/cocostudio/CCComAttribute.cpp +++ b/cocos/editor-support/cocostudio/CCComAttribute.cpp @@ -28,8 +28,7 @@ using namespace cocos2d; namespace cocostudio { ComAttribute::ComAttribute(void) -: _attributes(NULL) -, _jsonDict(NULL) +: _jsonDict(NULL) { _name = "ComAttribute"; } @@ -37,14 +36,10 @@ ComAttribute::ComAttribute(void) ComAttribute::~ComAttribute(void) { CC_SAFE_DELETE(_jsonDict); - CC_SAFE_RELEASE(_attributes); } bool ComAttribute::init() { - _attributes = Dictionary::create(); - _attributes->retain(); - _jsonDict = new JsonDictionary(); return true; } @@ -63,128 +58,6 @@ ComAttribute* ComAttribute::create(void) return pRet; } -void ComAttribute::setInt(const char *key, int value) -{ - CCASSERT(key != NULL, "Argument must be non-nil"); - _attributes->setObject(Integer::create(value), key); -} - -void ComAttribute::setDouble(const char *key, double value) -{ - CCASSERT(key != NULL, "Argument must be non-nil"); - _attributes->setObject(Double::create(value), key); -} - -void ComAttribute::setFloat(const char *key, float value) -{ - CCASSERT(key != NULL, "Argument must be non-nil"); - _attributes->setObject(Float::create(value), key); -} - -void ComAttribute::setBool(const char *key, bool value) -{ - CCASSERT(key != NULL, "Argument must be non-nil"); - _attributes->setObject(Bool::create(value), key); -} - -void ComAttribute::setCString(const char *key, const char *value) -{ - CCASSERT(key != NULL, "Argument must be non-nil"); - _attributes->setObject(String::create(value), key); -} - -void ComAttribute::setObject(const char *key, Object *value) -{ - CCASSERT(key != NULL, "Argument must be non-nil"); - _attributes->setObject(value, key); -} - -int ComAttribute::getInt(const char *key) const -{ - Object *ret = _attributes->objectForKey(key); - if( ret ) - { - if( Integer *obj=dynamic_cast(ret) ) - return obj->getValue(); - - CCASSERT(false, "Key found, type is not integer"); - } - - // XXX: Should it throw an exception ? - CCLOG("Key not found: '%s'", key ); - return 0; -} - -double ComAttribute::getDouble(const char *key) const -{ - Object *ret = _attributes->objectForKey(key); - if( ret ) - { - if( Double *obj=dynamic_cast(ret) ) - return obj->getValue(); - - CCASSERT(false, "Key found, type is not double"); - } - - // XXX: Should it throw an exception ? - CCLOG("Key not found: '%s'", key ); - return 0.0; -} - -float ComAttribute::getFloat(const char *key) const -{ - Object *ret = _attributes->objectForKey(key); - if( ret ) - { - if( Float *obj=dynamic_cast(ret) ) - return obj->getValue(); - - CCASSERT(false, "Key found, type is not float"); - } - - // XXX: Should it throw an exception ? - CCLOG("Key not found: '%s'", key ); - return 0.0; -} - -bool ComAttribute::getBool(const char *key) const -{ - Object *ret = _attributes->objectForKey(key); - if( ret ) - { - if( Bool *boolobj=dynamic_cast(ret) ) - return boolobj->getValue(); - if( String *strobj=dynamic_cast(ret) ) - return strobj->boolValue(); - CCASSERT(false, "Key found, type is not Bool"); - } - - // XXX: Should it throw an exception ? - CCLOG("Key not found: '%s'", key ); - return false; -} - -const char* ComAttribute::getCString(const char *key) const -{ - Object *ret = _attributes->objectForKey(key); - if( ret ) - { - if( String *str=dynamic_cast(ret) ) - return str->getCString(); - - CCASSERT(false, "Key found, type is not CString"); - } - - // XXX: Should it throw an exception ? - CCLOG("Key not found: '%s'", key ); - return NULL; -} - -Object* ComAttribute::getObject(const char *key) const -{ - return _attributes->objectForKey(key); -} - JsonDictionary* ComAttribute::getDict() const { return _jsonDict; diff --git a/cocos/editor-support/cocostudio/CCComAttribute.h b/cocos/editor-support/cocostudio/CCComAttribute.h index 53c7c2c885..dcb1b177e3 100644 --- a/cocos/editor-support/cocostudio/CCComAttribute.h +++ b/cocos/editor-support/cocostudio/CCComAttribute.h @@ -47,24 +47,9 @@ protected: public: virtual bool init(); static ComAttribute* create(void); - - void setInt(const char *key, int value); - void setDouble(const char *key, double value); - void setFloat(const char *key, float value); - void setBool(const char *key, bool value); - void setCString(const char *key, const char *value); - void setObject(const char *key, Object *value); - - int getInt(const char *key) const; - double getDouble(const char *key) const; - float getFloat(const char *key) const; - bool getBool(const char *key) const; - const char* getCString(const char *key) const; - cocos2d::Object* getObject(const char *key) const; - JsonDictionary* getDict() const; + private: - cocos2d::Dictionary *_attributes; JsonDictionary *_jsonDict; }; diff --git a/cocos/editor-support/cocostudio/CCComController.h b/cocos/editor-support/cocostudio/CCComController.h index 00da3fa74f..a6a877f77e 100644 --- a/cocos/editor-support/cocostudio/CCComController.h +++ b/cocos/editor-support/cocostudio/CCComController.h @@ -32,7 +32,7 @@ namespace cocostudio { class ComController : public cocos2d::Component, public InputDelegate { -protected: +public: ComController(void); public: @@ -51,10 +51,10 @@ public: * @js NA * @lua NA */ - virtual void onExit(); - virtual void update(float delta); - virtual bool isEnabled() const; - virtual void setEnabled(bool b); + virtual void onExit() override; + virtual void update(float delta) override; + virtual bool isEnabled() const override; + virtual void setEnabled(bool b) override; static ComController* create(void); }; diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/ComponentsTestScene.h b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/ComponentsTestScene.h index 152764c4bc..23c17932d8 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/ComponentsTestScene.h +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/ComponentsTestScene.h @@ -14,7 +14,7 @@ public: // Here's a difference. Method 'init' in cocos2d-x returns bool, // instead of returning 'id' in cocos2d-iphone - virtual bool init(); + virtual bool init() override; // there's no 'id' in cpp, so we recommand to return the exactly class pointer static cocos2d::Scene* scene(); diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/EnemyController.h b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/EnemyController.h index 0025fd6309..790f9da45d 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/EnemyController.h +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/EnemyController.h @@ -13,10 +13,10 @@ protected: virtual ~EnemyController(void); public: - virtual bool init(); - virtual void onEnter(); - virtual void onExit(); - virtual void update(float delta); + virtual bool init() override; + virtual void onEnter() override; + virtual void onExit() override; + virtual void update(float delta) override; static EnemyController* create(void); public: diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/PlayerController.h b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/PlayerController.h index cc5233b539..7848ff1e08 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/PlayerController.h +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/PlayerController.h @@ -13,13 +13,13 @@ protected: virtual ~PlayerController(void); public: - void onTouchesEnded(const std::vector& touches, cocos2d::Event *event) override; + virtual void onTouchesEnded(const std::vector& touches, cocos2d::Event *event) override; public: - virtual bool init(); - virtual void onEnter(); - virtual void onExit(); - virtual void update(float delta); + virtual bool init() override; + virtual void onEnter() override; + virtual void onExit() override; + virtual void update(float delta) override; static PlayerController* create(void); }; diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/ProjectileController.h b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/ProjectileController.h index 7ae6b71ab1..55cd5cba78 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/ProjectileController.h +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/ProjectileController.h @@ -12,10 +12,10 @@ protected: virtual ~ProjectileController(void); public: - virtual bool init(); - virtual void onEnter(); - virtual void onExit(); - virtual void update(float delta); + virtual bool init() override; + virtual void onEnter() override; + virtual void onExit() override; + virtual void update(float delta) override; static ProjectileController* create(void); public: diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/SceneController.cpp b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/SceneController.cpp index 92ee81267f..f432a09664 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/SceneController.cpp +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/SceneController.cpp @@ -39,7 +39,7 @@ void SceneController::onEnter() _projectiles->retain(); ((ComAudio*)(_owner->getComponent("Audio")))->playBackgroundMusic("background-music-aac.wav", true); - ((ComAttribute*)(_owner->getComponent("ComAttribute")))->setInt("KillCount", 0); + ((ComAttribute*)(_owner->getComponent("ComAttribute")))->getDict()->insertItem("KillCount", 0); } void SceneController::onExit() @@ -102,10 +102,10 @@ void SceneController::spriteMoveFinished(Node* sender) void SceneController::increaseKillCount() { - int nProjectilesDestroyed = ((ComAttribute*)(_owner->getComponent("ComAttribute")))->getInt("KillCount"); + int nProjectilesDestroyed = ((ComAttribute*)(_owner->getComponent("ComAttribute")))->getDict()->getItemIntValue("KillCount", -1); ComAttribute *p = (ComAttribute*)(_owner->getComponent("ComAttribute")); - p->setInt("KillCount", ++nProjectilesDestroyed); + p->getDict()->insertItem("KillCount", ++nProjectilesDestroyed); if (nProjectilesDestroyed >= 5) { diff --git a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/SceneController.h b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/SceneController.h index 4d6efe0c89..b2d2769d2f 100644 --- a/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/SceneController.h +++ b/samples/Cpp/TestCpp/Classes/ExtensionsTest/CocoStudioComponentsTest/SceneController.h @@ -12,10 +12,10 @@ protected: virtual ~SceneController(void); public: - virtual bool init(); - virtual void onEnter(); - virtual void onExit(); - virtual void update(float delta); + virtual bool init() override; + virtual void onEnter() override; + virtual void onExit() override; + virtual void update(float delta) override; static SceneController* create(void);