2013-09-13 22:20:20 +08:00
|
|
|
/****************************************************************************
|
|
|
|
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 "UILayer.h"
|
|
|
|
#include "UIHelper.h"
|
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
|
|
|
UILayer::UILayer():
|
2013-09-16 20:54:13 +08:00
|
|
|
_rootWidget(NULL),
|
|
|
|
_inputManager(NULL)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
UILayer::~UILayer()
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_rootWidget->release();
|
|
|
|
CC_SAFE_DELETE(_inputManager);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UILayer::init()
|
|
|
|
{
|
|
|
|
if (CCLayer::init())
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_rootWidget = UIRootWidget::create();
|
|
|
|
_rootWidget->retain();
|
|
|
|
_rootWidget->onEnter();
|
|
|
|
addChild(_rootWidget->getRenderer());
|
|
|
|
_inputManager = new UIInputManager();
|
|
|
|
_inputManager->setRootWidget(_rootWidget);
|
2013-09-13 22:20:20 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
UILayer* UILayer::create(void)
|
|
|
|
{
|
|
|
|
UILayer *pRet = new UILayer();
|
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UILayer::onEnter()
|
|
|
|
{
|
|
|
|
setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
|
|
|
|
setTouchEnabled(true);
|
|
|
|
CCLayer::onEnter();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void UILayer::onExit()
|
|
|
|
{
|
|
|
|
setTouchEnabled(false);
|
|
|
|
CCLayer::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UILayer::onEnterTransitionDidFinish()
|
|
|
|
{
|
|
|
|
CCLayer::onEnterTransitionDidFinish();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UILayer::addWidget(UIWidget* widget)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_rootWidget->addChild(widget);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UILayer::removeWidget(UIWidget* widget)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_rootWidget->removeChild(widget);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UILayer::setVisible(bool visible)
|
|
|
|
{
|
|
|
|
CCLayer::setVisible(visible);
|
2013-09-16 20:54:13 +08:00
|
|
|
_rootWidget->setVisible(visible);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
UIWidget* UILayer::getWidgetByTag(int tag)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (!_rootWidget)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
return CCUIHELPER->seekWidgetByTag(_rootWidget, tag);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
UIWidget* UILayer::getWidgetByName(const char* name)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (!_rootWidget)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
return CCUIHELPER->seekWidgetByName(_rootWidget, name);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
UIRootWidget* UILayer::getRootWidget()
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _rootWidget;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
UIInputManager* UILayer::getInputManager()
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _inputManager;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UILayer::clear()
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_rootWidget->removeAllChildren();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-09-16 16:19:41 +08:00
|
|
|
bool UILayer::onTouchBegan(Touch *pTouch, Event *pEvent)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (_inputManager && _inputManager->onTouchBegan(pTouch))
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-16 16:19:41 +08:00
|
|
|
void UILayer::onTouchMoved(Touch *pTouch, Event *pEvent)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_inputManager->onTouchMoved(pTouch);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-09-16 16:19:41 +08:00
|
|
|
void UILayer::onTouchEnded(Touch *pTouch, Event *pEvent)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_inputManager->onTouchEnd(pTouch);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-09-16 16:19:41 +08:00
|
|
|
void UILayer::onTouchCancelled(Touch *pTouch, Event *pEvent)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_inputManager->onTouchCancelled(pTouch);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_EXT_END
|