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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-10-16 16:48:39 +08:00
|
|
|
#include "gui/UILayer.h"
|
|
|
|
#include "gui/UIHelper.h"
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
using namespace cocos2d;
|
|
|
|
|
|
|
|
namespace gui {
|
2013-09-13 22:20:20 +08:00
|
|
|
|
|
|
|
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();
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
CCLayer::onEnter();
|
|
|
|
|
2013-10-12 14:02:01 +08:00
|
|
|
auto listener = EventListenerTouchOneByOne::create();
|
|
|
|
listener->setSwallowTouches(true);
|
|
|
|
|
|
|
|
listener->onTouchBegan = CC_CALLBACK_2(UILayer::onTouchBegan, this);
|
|
|
|
listener->onTouchMoved = CC_CALLBACK_2(UILayer::onTouchMoved, this);
|
|
|
|
listener->onTouchEnded = CC_CALLBACK_2(UILayer::onTouchEnded, this);
|
|
|
|
listener->onTouchCancelled = CC_CALLBACK_2(UILayer::onTouchCancelled, this);
|
|
|
|
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2013-11-13 20:01:57 +08:00
|
|
|
_rootWidget->onEnter();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UILayer::onExit()
|
|
|
|
{
|
|
|
|
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-11-06 16:04:06 +08:00
|
|
|
return UIHelper::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-11-06 16:04:06 +08:00
|
|
|
return UIHelper::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
|
|
|
}
|
|
|
|
|
2013-10-12 14:02:01 +08:00
|
|
|
}
|