From 47e5688a9dcda6ee18b77503990bbc07706b4dba Mon Sep 17 00:00:00 2001 From: Wenhai Lin Date: Wed, 20 Apr 2016 17:36:59 +0800 Subject: [PATCH] Bug fix: 1.Fixed fail to add component for node 2.Fixed crash on exit 3.Fixed FileUtils::isDirectoryExist may return the error result in iOS/Mac --- native/cocos/2d/CCComponentContainer.cpp | 2 +- native/cocos/2d/CCNode.cpp | 2 + native/cocos/base/CCScriptSupport.cpp | 20 ++- native/cocos/platform/CCGLView.cpp | 8 +- .../cocos/platform/apple/CCFileUtils-apple.h | 5 +- .../cocos/platform/apple/CCFileUtils-apple.mm | 63 +++++----- native/cocos/ui/UILayout.cpp | 14 +-- native/cocos/ui/UILayoutComponent.cpp | 115 ++++++------------ native/cocos/ui/UILayoutComponent.h | 2 - 9 files changed, 97 insertions(+), 134 deletions(-) diff --git a/native/cocos/2d/CCComponentContainer.cpp b/native/cocos/2d/CCComponentContainer.cpp index 44703eb55e..ab6502f825 100644 --- a/native/cocos/2d/CCComponentContainer.cpp +++ b/native/cocos/2d/CCComponentContainer.cpp @@ -58,7 +58,7 @@ bool ComponentContainer::add(Component *com) CCASSERT(com->getOwner() == nullptr, "Component already added. It can't be added again"); do { - if (com == nullptr || com->getOwner() == nullptr) { + if (com == nullptr || com->getOwner() != nullptr) { break; } diff --git a/native/cocos/2d/CCNode.cpp b/native/cocos/2d/CCNode.cpp index 1d9750a863..18c93352e5 100644 --- a/native/cocos/2d/CCNode.cpp +++ b/native/cocos/2d/CCNode.cpp @@ -730,6 +730,8 @@ Node* Node::getChildByTag(int tag) const Node* Node::getChildByName(const std::string& name) const { CCASSERT(!name.empty(), "Invalid name"); + if (name.empty()) + return nullptr; std::hash h; size_t hash = h(name); diff --git a/native/cocos/base/CCScriptSupport.cpp b/native/cocos/base/CCScriptSupport.cpp index b1c98bc0ce..1b866ed0bf 100644 --- a/native/cocos/base/CCScriptSupport.cpp +++ b/native/cocos/base/CCScriptSupport.cpp @@ -167,7 +167,10 @@ void ScriptEngineManager::destroyInstance() bool ScriptEngineManager::sendActionEventToJS(Action* actionObject, int eventType, void* param) { - auto scriptEngine = getInstance()->getScriptEngine(); + if (ShareInstance == nullptr) + return false; + + auto scriptEngine = ShareInstance->getScriptEngine(); ActionObjectScriptData data(actionObject,(int*)&eventType, param); ScriptEvent scriptEvent(kScriptActionEvent,(void*)&data); @@ -179,7 +182,10 @@ bool ScriptEngineManager::sendActionEventToJS(Action* actionObject, int eventTyp bool ScriptEngineManager::sendNodeEventToJS(Node* node, int action) { - auto scriptEngine = getInstance()->getScriptEngine(); + if (ShareInstance == nullptr) + return false; + + auto scriptEngine = ShareInstance->getScriptEngine(); if (scriptEngine->isCalledFromScript()) { @@ -199,7 +205,10 @@ bool ScriptEngineManager::sendNodeEventToJS(Node* node, int action) bool ScriptEngineManager::sendNodeEventToJSExtended(Node* node, int action) { - auto scriptEngine = getInstance()->getScriptEngine(); + if (ShareInstance == nullptr) + return false; + + auto scriptEngine = ShareInstance->getScriptEngine(); if (!scriptEngine->isCalledFromScript()) { @@ -214,7 +223,10 @@ bool ScriptEngineManager::sendNodeEventToJSExtended(Node* node, int action) void ScriptEngineManager::sendNodeEventToLua(Node* node, int action) { - auto scriptEngine = getInstance()->getScriptEngine(); + if (ShareInstance == nullptr) + return; + + auto scriptEngine = ShareInstance->getScriptEngine(); BasicScriptData data(node,(void*)&action); ScriptEvent scriptEvent(kNodeEvent,(void*)&data); diff --git a/native/cocos/platform/CCGLView.cpp b/native/cocos/platform/CCGLView.cpp index 8b9a22ec92..33a84cbb55 100644 --- a/native/cocos/platform/CCGLView.cpp +++ b/native/cocos/platform/CCGLView.cpp @@ -221,10 +221,10 @@ Vec2 GLView::getVisibleOrigin() const void GLView::setViewPortInPoints(float x , float y , float w , float h) { - glViewport((float)(x * _scaleX + _viewPortRect.origin.x), - (float)(y * _scaleY + _viewPortRect.origin.y), - (float)(w * _scaleX), - (float)(h * _scaleY)); + glViewport((GLint)(x * _scaleX + _viewPortRect.origin.x), + (GLint)(y * _scaleY + _viewPortRect.origin.y), + (GLsizei)(w * _scaleX), + (GLsizei)(h * _scaleY)); } void GLView::setScissorInPoints(float x , float y , float w , float h) diff --git a/native/cocos/platform/apple/CCFileUtils-apple.h b/native/cocos/platform/apple/CCFileUtils-apple.h index 9d507fd48c..259ec6318f 100644 --- a/native/cocos/platform/apple/CCFileUtils-apple.h +++ b/native/cocos/platform/apple/CCFileUtils-apple.h @@ -57,9 +57,12 @@ public: virtual ValueVector getValueVectorFromFile(const std::string& filename) override; void setBundle(NSBundle* bundle); -private: + + virtual bool isDirectoryExistInternal(const std::string& dirPath) const override; virtual bool isFileExistInternal(const std::string& filePath) const override; virtual bool removeDirectory(const std::string& dirPath) override; +private: + bool isFileExistInternal(const std::string& filePath,bool& isDirectory) const; NSBundle* getBundle() const; NSBundle* _bundle; diff --git a/native/cocos/platform/apple/CCFileUtils-apple.mm b/native/cocos/platform/apple/CCFileUtils-apple.mm index ee4751bac7..cd73e20cbc 100644 --- a/native/cocos/platform/apple/CCFileUtils-apple.mm +++ b/native/cocos/platform/apple/CCFileUtils-apple.mm @@ -359,46 +359,43 @@ std::string FileUtilsApple::getWritablePath() const return strRet; } -bool FileUtilsApple::isFileExistInternal(const std::string& filePath) const +bool FileUtilsApple::isDirectoryExistInternal(const std::string &dirPath) const +{ + bool isDirectory = false; + auto isExist = isFileExistInternal(dirPath, isDirectory); + return isExist && isDirectory; +} + +bool FileUtilsApple::isFileExistInternal(const std::string &filePath) const +{ + bool isDirectory = false; + auto isExist = isFileExistInternal(filePath, isDirectory); + return isExist && !isDirectory; +} + +bool FileUtilsApple::isFileExistInternal(const std::string& filePath, bool& isDirectory) const { if (filePath.empty()) - { return false; - } - - bool ret = false; - + + isDirectory = false; + + NSString* path = nil; if (filePath[0] != '/') - { - std::string path; - std::string file; - size_t pos = filePath.find_last_of("/"); - if (pos != std::string::npos) - { - file = filePath.substr(pos+1); - path = filePath.substr(0, pos+1); - } - else - { - file = filePath; - } - - NSString* fullpath = [getBundle() pathForResource:[NSString stringWithUTF8String:file.c_str()] - ofType:nil - inDirectory:[NSString stringWithUTF8String:path.c_str()]]; - if (fullpath != nil) { - ret = true; - } - } + path = [[getBundle() resourcePath] stringByAppendingPathComponent: + [NSString stringWithUTF8String:filePath.c_str()]]; else - { - // Search path is an absolute path. - if ([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:filePath.c_str()]]) { - ret = true; - } + path = [NSString stringWithUTF8String:filePath.c_str()]; + + BOOL isDir = NO; + // Search path is an absolute path. + if ([s_fileManager fileExistsAtPath:path isDirectory:&isDir]) { + if (isDir) + isDirectory = true; + return true; } - return ret; + return false; } static int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) diff --git a/native/cocos/ui/UILayout.cpp b/native/cocos/ui/UILayout.cpp index 2168f7478a..2a39eee1b1 100644 --- a/native/cocos/ui/UILayout.cpp +++ b/native/cocos/ui/UILayout.cpp @@ -774,13 +774,10 @@ const Color3B& Layout::getBackGroundColor()const void Layout::setBackGroundColor(const Color3B &startColor, const Color3B &endColor) { _gStartColor = startColor; - if (_gradientRender) - { - _gradientRender->setStartColor(startColor); - } _gEndColor = endColor; if (_gradientRender) { + _gradientRender->setStartColor(startColor); _gradientRender->setEndColor(endColor); } } @@ -899,8 +896,6 @@ void Layout::setLayoutType(Type type) _doLayoutDirty = true; } - - Layout::Type Layout::getLayoutType() const { return _layoutType; @@ -950,7 +945,6 @@ LayoutManager* Layout::createLayoutManager() void Layout::doLayout() { - if (!_doLayoutDirty) { return; @@ -1158,7 +1152,7 @@ int Layout::findFirstFocusEnabledWidgetIndex() } index++; } - CCASSERT(0, "invalide operation"); + CCASSERT(0, "invalid operation"); return 0; } @@ -1257,8 +1251,6 @@ int Layout::findFarthestChildWidgetIndex(FocusDirection direction, cocos2d::ui:: return 0; } - - Widget* Layout::findFocusEnabledChildWidgetByIndex(ssize_t index) { @@ -1472,7 +1464,7 @@ Widget* Layout::getPreviousFocusedWidget(FocusDirection direction, Widget *curre } else { - //handling the disabled widget, there is no actual focus lose or get, so we don't need any envet + //handling the disabled widget, there is no actual focus lose or get, so we don't need any event return this->getPreviousFocusedWidget(direction, nextWidget); } } diff --git a/native/cocos/ui/UILayoutComponent.cpp b/native/cocos/ui/UILayoutComponent.cpp index e633e912d5..343e0685f4 100644 --- a/native/cocos/ui/UILayoutComponent.cpp +++ b/native/cocos/ui/UILayoutComponent.cpp @@ -31,7 +31,7 @@ NS_CC_BEGIN namespace ui { LayoutComponent::LayoutComponent() - :_horizontalEdge(HorizontalEdge::None) + : _horizontalEdge(HorizontalEdge::None) , _verticalEdge(VerticalEdge::None) , _leftMargin(0) , _rightMargin(0) @@ -75,23 +75,6 @@ namespace ui { return nullptr; } - bool LayoutComponent::init() - { - bool ret = true; - do - { - if (!Component::init()) - { - ret = false; - break; - } - - //put layout component initalized code here - - } while (0); - return ret; - } - Node* LayoutComponent::getOwnerParent() { if (_owner) { @@ -188,7 +171,7 @@ namespace ui { else { _positionPercentX = 0; - if (_usingPositionPercentX) + if (_usingPositionPercentX || _horizontalEdge == HorizontalEdge::Center) ownerPoint.x = 0; } @@ -197,7 +180,7 @@ namespace ui { else { _positionPercentY = 0; - if (_usingPositionPercentY) + if (_usingPositionPercentY || _verticalEdge == VerticalEdge::Center) ownerPoint.y = 0; } @@ -231,11 +214,14 @@ namespace ui { { _positionPercentX = percentMargin; - Node* parent = this->getOwnerParent(); - if (parent != nullptr) + if (_usingPositionPercentX || _horizontalEdge == HorizontalEdge::Center) { - _owner->setPositionX(parent->getContentSize().width * _positionPercentX); - this->refreshHorizontalMargin(); + Node* parent = this->getOwnerParent(); + if (parent != nullptr) + { + _owner->setPositionX(parent->getContentSize().width * _positionPercentX); + this->refreshHorizontalMargin(); + } } } @@ -260,11 +246,14 @@ namespace ui { { _positionPercentY = percentMargin; - Node* parent = this->getOwnerParent(); - if (parent != nullptr) + if (_usingPositionPercentY || _verticalEdge == VerticalEdge::Center) { - _owner->setPositionY(parent->getContentSize().height * _positionPercentY); - this->refreshVerticalMargin(); + Node* parent = this->getOwnerParent(); + if (parent != nullptr) + { + _owner->setPositionY(parent->getContentSize().height * _positionPercentY); + this->refreshVerticalMargin(); + } } } @@ -279,24 +268,6 @@ namespace ui { { _usingPositionPercentX = false; } - - Node* parent = this->getOwnerParent(); - if (parent != nullptr) - { - Point ownerPoint = _owner->getPosition(); - const Size& parentSize = parent->getContentSize(); - if (parentSize.width != 0) - _positionPercentX = ownerPoint.x / parentSize.width; - else - { - _positionPercentX = 0; - ownerPoint.x = 0; - if (_usingPositionPercentX) - _owner->setPosition(ownerPoint); - } - - this->refreshHorizontalMargin(); - } } LayoutComponent::VerticalEdge LayoutComponent::getVerticalEdge()const @@ -310,24 +281,6 @@ namespace ui { { _usingPositionPercentY = false; } - - Node* parent = this->getOwnerParent(); - if (parent != nullptr) - { - Point ownerPoint = _owner->getPosition(); - const Size& parentSize = parent->getContentSize(); - if (parentSize.height != 0) - _positionPercentY = ownerPoint.y / parentSize.height; - else - { - _positionPercentY = 0; - ownerPoint.y = 0; - if (_usingPositionPercentY) - _owner->setPosition(ownerPoint); - } - - this->refreshVerticalMargin(); - } } float LayoutComponent::getLeftMargin()const @@ -384,7 +337,7 @@ namespace ui { else { _percentWidth = 0; - if (_usingPercentWidth) + if (_usingPercentWidth || (this->_horizontalEdge != HorizontalEdge::Center && this->_usingStretchWidth)) ownerSize.width = 0; } @@ -393,7 +346,7 @@ namespace ui { else { _percentHeight = 0; - if (_usingPercentHeight) + if (_usingPercentHeight || (this->_verticalEdge != VerticalEdge::Center && this->_usingStretchHeight)) ownerSize.height = 0; } @@ -455,14 +408,17 @@ namespace ui { { _percentWidth = percentWidth; - Node* parent = this->getOwnerParent(); - if (parent != nullptr) + if (_usingPercentWidth) { - Size ownerSize = _owner->getContentSize(); - ownerSize.width = parent->getContentSize().width * _percentWidth; - _owner->setContentSize(ownerSize); + Node* parent = this->getOwnerParent(); + if (parent != nullptr) + { + Size ownerSize = _owner->getContentSize(); + ownerSize.width = parent->getContentSize().width * _percentWidth; + _owner->setContentSize(ownerSize); - this->refreshHorizontalMargin(); + this->refreshHorizontalMargin(); + } } } @@ -515,14 +471,17 @@ namespace ui { { _percentHeight = percentHeight; - Node* parent = this->getOwnerParent(); - if (parent != nullptr) + if (_usingPercentHeight) { - Size ownerSize = _owner->getContentSize(); - ownerSize.height = parent->getContentSize().height * _percentHeight; - _owner->setContentSize(ownerSize); + Node* parent = this->getOwnerParent(); + if (parent != nullptr) + { + Size ownerSize = _owner->getContentSize(); + ownerSize.height = parent->getContentSize().height * _percentHeight; + _owner->setContentSize(ownerSize); - this->refreshVerticalMargin(); + this->refreshVerticalMargin(); + } } } diff --git a/native/cocos/ui/UILayoutComponent.h b/native/cocos/ui/UILayoutComponent.h index 36dae41e36..aab19fd995 100644 --- a/native/cocos/ui/UILayoutComponent.h +++ b/native/cocos/ui/UILayoutComponent.h @@ -55,8 +55,6 @@ namespace ui { */ ~LayoutComponent(); - virtual bool init()override; - /** * Create a LayoutComponent instance with default settings. */