2014-03-11 17:13:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
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 "ui/UILayout.h"
|
|
|
|
#include "ui/UIHelper.h"
|
|
|
|
#include "extensions/GUI/CCControlExtension/CCScale9Sprite.h"
|
2014-05-09 09:01:48 +08:00
|
|
|
#include "renderer/CCGLProgram.h"
|
2014-05-10 09:39:25 +08:00
|
|
|
#include "renderer/CCGLProgramCache.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "2d/CCDrawingPrimitives.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "renderer/CCRenderer.h"
|
2014-05-28 10:17:34 +08:00
|
|
|
#include "ui/UILayoutManager.h"
|
2014-06-04 14:26:21 +08:00
|
|
|
#include "2d/CCDrawNode.h"
|
|
|
|
#include "2d/CCLayer.h"
|
|
|
|
#include "CCGLView.h"
|
|
|
|
#include "2d/CCSprite.h"
|
|
|
|
#include "base/CCEventFocus.h"
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
|
|
|
static const int BACKGROUNDIMAGE_Z = (-1);
|
|
|
|
static const int BCAKGROUNDCOLORRENDERER_Z = (-2);
|
|
|
|
|
|
|
|
static GLint g_sStencilBits = -1;
|
|
|
|
static GLint s_layer = -1;
|
|
|
|
|
|
|
|
IMPLEMENT_CLASS_GUI_INFO(Layout)
|
|
|
|
|
|
|
|
Layout::Layout():
|
|
|
|
_clippingEnabled(false),
|
|
|
|
_backGroundScale9Enabled(false),
|
|
|
|
_backGroundImage(nullptr),
|
|
|
|
_backGroundImageFileName(""),
|
|
|
|
_backGroundImageCapInsets(Rect::ZERO),
|
2014-05-09 17:05:36 +08:00
|
|
|
_colorType(BackGroundColorType::NONE),
|
2014-05-09 14:56:05 +08:00
|
|
|
_bgImageTexType(TextureResType::LOCAL),
|
2014-03-11 17:13:54 +08:00
|
|
|
_colorRender(nullptr),
|
|
|
|
_gradientRender(nullptr),
|
|
|
|
_cColor(Color3B::WHITE),
|
|
|
|
_gStartColor(Color3B::WHITE),
|
|
|
|
_gEndColor(Color3B::WHITE),
|
2014-05-15 01:07:09 +08:00
|
|
|
_alongVector(Vec2(0.0f, -1.0f)),
|
2014-03-11 17:13:54 +08:00
|
|
|
_cOpacity(255),
|
|
|
|
_backGroundImageTextureSize(Size::ZERO),
|
2014-05-12 18:00:54 +08:00
|
|
|
_layoutType(Type::ABSOLUTE),
|
2014-05-09 16:31:22 +08:00
|
|
|
_clippingType(ClippingType::STENCIL),
|
2014-03-11 17:13:54 +08:00
|
|
|
_clippingStencil(nullptr),
|
|
|
|
_scissorRectDirty(false),
|
|
|
|
_clippingRect(Rect::ZERO),
|
|
|
|
_clippingParent(nullptr),
|
|
|
|
_doLayoutDirty(true),
|
|
|
|
_clippingRectDirty(true),
|
|
|
|
_currentStencilEnabled(GL_FALSE),
|
|
|
|
_currentStencilWriteMask(~0),
|
|
|
|
_currentStencilFunc(GL_ALWAYS),
|
|
|
|
_currentStencilRef(0),
|
|
|
|
_currentStencilValueMask(~0),
|
|
|
|
_currentStencilFail(GL_KEEP),
|
|
|
|
_currentStencilPassDepthFail(GL_KEEP),
|
|
|
|
_currentStencilPassDepthPass(GL_KEEP),
|
|
|
|
_currentDepthWriteMask(GL_TRUE),
|
|
|
|
_currentAlphaTestEnabled(GL_FALSE),
|
|
|
|
_currentAlphaTestFunc(GL_ALWAYS),
|
|
|
|
_currentAlphaTestRef(1),
|
|
|
|
_backGroundImageColor(Color3B::WHITE),
|
2014-03-25 17:15:54 +08:00
|
|
|
_backGroundImageOpacity(255),
|
2014-05-04 14:50:12 +08:00
|
|
|
_passFocusToChild(true),
|
2014-05-26 22:57:50 +08:00
|
|
|
_loopFocus(false),
|
|
|
|
_isFocusPassing(false)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-06 16:48:49 +08:00
|
|
|
//no-op
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Layout::~Layout()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(_clippingStencil);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::onEnter()
|
|
|
|
{
|
2014-06-13 17:59:23 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
|
|
|
if (_scriptType == kScriptTypeJavascript)
|
|
|
|
{
|
2014-06-18 11:51:52 +08:00
|
|
|
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
|
2014-06-13 17:59:23 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
Widget::onEnter();
|
|
|
|
if (_clippingStencil)
|
|
|
|
{
|
|
|
|
_clippingStencil->onEnter();
|
|
|
|
}
|
|
|
|
_doLayoutDirty = true;
|
|
|
|
_clippingRectDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::onExit()
|
|
|
|
{
|
|
|
|
Widget::onExit();
|
|
|
|
if (_clippingStencil)
|
|
|
|
{
|
|
|
|
_clippingStencil->onExit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Layout* Layout::create()
|
|
|
|
{
|
|
|
|
Layout* layout = new Layout();
|
|
|
|
if (layout && layout->init())
|
|
|
|
{
|
|
|
|
layout->autorelease();
|
|
|
|
return layout;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(layout);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Layout::init()
|
|
|
|
{
|
2014-05-23 10:59:18 +08:00
|
|
|
if (Widget::init())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
ignoreContentAdaptWithSize(false);
|
2014-06-20 14:03:33 +08:00
|
|
|
setContentSize(Size::ZERO);
|
2014-05-15 01:07:09 +08:00
|
|
|
setAnchorPoint(Vec2::ZERO);
|
2014-06-06 16:48:49 +08:00
|
|
|
onPassFocusToChild = CC_CALLBACK_2(Layout::findNearestChildWidgetIndex, this);
|
2014-03-11 17:13:54 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::addChild(Node *child)
|
|
|
|
{
|
2014-05-21 15:16:41 +08:00
|
|
|
Layout::addChild(child, child->getZOrder(), child->getTag());
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::addChild(Node * child, int zOrder)
|
|
|
|
{
|
2014-05-22 18:11:27 +08:00
|
|
|
Layout::addChild(child, zOrder, child->getTag());
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::addChild(Node *child, int zOrder, int tag)
|
|
|
|
{
|
2014-05-22 17:49:19 +08:00
|
|
|
if (dynamic_cast<Widget*>(child)) {
|
|
|
|
supplyTheLayoutParameterLackToChild(static_cast<Widget*>(child));
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
Widget::addChild(child, zOrder, tag);
|
|
|
|
_doLayoutDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::removeChild(Node *child, bool cleanup)
|
|
|
|
{
|
|
|
|
Widget::removeChild(child, cleanup);
|
|
|
|
_doLayoutDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::removeAllChildren()
|
|
|
|
{
|
|
|
|
Widget::removeAllChildren();
|
2014-05-21 15:16:41 +08:00
|
|
|
_doLayoutDirty = true;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::removeAllChildrenWithCleanup(bool cleanup)
|
|
|
|
{
|
|
|
|
Widget::removeAllChildrenWithCleanup(cleanup);
|
|
|
|
_doLayoutDirty = true;
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:36:33 +08:00
|
|
|
bool Layout::isClippingEnabled()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _clippingEnabled;
|
|
|
|
}
|
2014-04-17 14:08:25 +08:00
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void Layout::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-21 15:21:55 +08:00
|
|
|
if (!_visible)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-06-03 18:31:14 +08:00
|
|
|
|
2014-04-17 14:08:25 +08:00
|
|
|
adaptRenderers();
|
2014-06-06 16:48:49 +08:00
|
|
|
doLayout();
|
2014-06-03 18:31:14 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_clippingEnabled)
|
|
|
|
{
|
|
|
|
switch (_clippingType)
|
|
|
|
{
|
2014-05-09 16:31:22 +08:00
|
|
|
case ClippingType::STENCIL:
|
2014-05-31 07:42:05 +08:00
|
|
|
stencilClippingVisit(renderer, parentTransform, parentFlags);
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
2014-05-09 16:31:22 +08:00
|
|
|
case ClippingType::SCISSOR:
|
2014-05-31 07:42:05 +08:00
|
|
|
scissorClippingVisit(renderer, parentTransform, parentFlags);
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-06 16:48:49 +08:00
|
|
|
Widget::visit(renderer, parentTransform, parentFlags);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 00:19:22 +08:00
|
|
|
void Layout::stencilClippingVisit(Renderer *renderer, const Mat4& parentTransform, uint32_t parentFlags)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
if(!_visible)
|
|
|
|
return;
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
uint32_t flags = processParentFlags(parentTransform, parentFlags);
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
// IMPORTANT:
|
2014-05-15 01:07:09 +08:00
|
|
|
// To ease the migration to v3.0, we still support the Mat4 stack,
|
2014-03-11 17:13:54 +08:00
|
|
|
// but it is deprecated and your code should not rely on it
|
2014-04-03 14:55:55 +08:00
|
|
|
Director* director = Director::getInstance();
|
|
|
|
CCASSERT(nullptr != director, "Director is null when seting matrix stack");
|
|
|
|
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
|
2014-03-11 17:13:54 +08:00
|
|
|
//Add group command
|
|
|
|
|
|
|
|
_groupCommand.init(_globalZOrder);
|
|
|
|
renderer->addCommand(&_groupCommand);
|
|
|
|
|
|
|
|
renderer->pushGroup(_groupCommand.getRenderQueueID());
|
|
|
|
|
|
|
|
_beforeVisitCmdStencil.init(_globalZOrder);
|
|
|
|
_beforeVisitCmdStencil.func = CC_CALLBACK_0(Layout::onBeforeVisitStencil, this);
|
|
|
|
renderer->addCommand(&_beforeVisitCmdStencil);
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
_clippingStencil->visit(renderer, _modelViewTransform, flags);
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
_afterDrawStencilCmd.init(_globalZOrder);
|
|
|
|
_afterDrawStencilCmd.func = CC_CALLBACK_0(Layout::onAfterDrawStencil, this);
|
|
|
|
renderer->addCommand(&_afterDrawStencilCmd);
|
|
|
|
|
2014-03-24 15:25:44 +08:00
|
|
|
int i = 0; // used by _children
|
|
|
|
int j = 0; // used by _protectedChildren
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2014-03-24 15:25:44 +08:00
|
|
|
sortAllChildren();
|
|
|
|
sortAllProtectedChildren();
|
|
|
|
|
|
|
|
//
|
|
|
|
// draw children and protectedChildren zOrder < 0
|
|
|
|
//
|
|
|
|
for( ; i < _children.size(); i++ )
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-03-24 15:25:44 +08:00
|
|
|
auto node = _children.at(i);
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2014-04-11 18:06:53 +08:00
|
|
|
if ( node && node->getLocalZOrder() < 0 )
|
2014-05-31 07:42:05 +08:00
|
|
|
node->visit(renderer, _modelViewTransform, flags);
|
2014-03-24 15:25:44 +08:00
|
|
|
else
|
|
|
|
break;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-03-24 15:25:44 +08:00
|
|
|
|
|
|
|
for( ; j < _protectedChildren.size(); j++ )
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-03-24 15:25:44 +08:00
|
|
|
auto node = _protectedChildren.at(j);
|
|
|
|
|
2014-04-11 18:06:53 +08:00
|
|
|
if ( node && node->getLocalZOrder() < 0 )
|
2014-05-31 07:42:05 +08:00
|
|
|
node->visit(renderer, _modelViewTransform, flags);
|
2014-03-24 15:25:44 +08:00
|
|
|
else
|
|
|
|
break;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-03-24 15:25:44 +08:00
|
|
|
//
|
|
|
|
// draw self
|
|
|
|
//
|
2014-05-31 07:42:05 +08:00
|
|
|
this->draw(renderer, _modelViewTransform, flags);
|
2014-03-24 15:25:44 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// draw children and protectedChildren zOrder >= 0
|
|
|
|
//
|
|
|
|
for(auto it=_protectedChildren.cbegin()+j; it != _protectedChildren.cend(); ++it)
|
2014-05-31 07:42:05 +08:00
|
|
|
(*it)->visit(renderer, _modelViewTransform, flags);
|
2014-03-24 15:25:44 +08:00
|
|
|
|
|
|
|
for(auto it=_children.cbegin()+i; it != _children.cend(); ++it)
|
2014-05-31 07:42:05 +08:00
|
|
|
(*it)->visit(renderer, _modelViewTransform, flags);
|
2014-03-24 15:25:44 +08:00
|
|
|
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
_afterVisitCmdStencil.init(_globalZOrder);
|
|
|
|
_afterVisitCmdStencil.func = CC_CALLBACK_0(Layout::onAfterVisitStencil, this);
|
|
|
|
renderer->addCommand(&_afterVisitCmdStencil);
|
|
|
|
|
|
|
|
renderer->popGroup();
|
|
|
|
|
2014-04-03 14:55:55 +08:00
|
|
|
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::onBeforeVisitStencil()
|
|
|
|
{
|
|
|
|
s_layer++;
|
|
|
|
GLint mask_layer = 0x1 << s_layer;
|
|
|
|
GLint mask_layer_l = mask_layer - 1;
|
|
|
|
_mask_layer_le = mask_layer | mask_layer_l;
|
|
|
|
_currentStencilEnabled = glIsEnabled(GL_STENCIL_TEST);
|
|
|
|
glGetIntegerv(GL_STENCIL_WRITEMASK, (GLint *)&_currentStencilWriteMask);
|
|
|
|
glGetIntegerv(GL_STENCIL_FUNC, (GLint *)&_currentStencilFunc);
|
|
|
|
glGetIntegerv(GL_STENCIL_REF, &_currentStencilRef);
|
|
|
|
glGetIntegerv(GL_STENCIL_VALUE_MASK, (GLint *)&_currentStencilValueMask);
|
|
|
|
glGetIntegerv(GL_STENCIL_FAIL, (GLint *)&_currentStencilFail);
|
|
|
|
glGetIntegerv(GL_STENCIL_PASS_DEPTH_FAIL, (GLint *)&_currentStencilPassDepthFail);
|
|
|
|
glGetIntegerv(GL_STENCIL_PASS_DEPTH_PASS, (GLint *)&_currentStencilPassDepthPass);
|
|
|
|
|
|
|
|
glEnable(GL_STENCIL_TEST);
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
glStencilMask(mask_layer);
|
|
|
|
glGetBooleanv(GL_DEPTH_WRITEMASK, &_currentDepthWriteMask);
|
|
|
|
glDepthMask(GL_FALSE);
|
|
|
|
glStencilFunc(GL_NEVER, mask_layer, mask_layer);
|
|
|
|
glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
|
2014-04-03 14:55:55 +08:00
|
|
|
|
2014-06-03 18:31:14 +08:00
|
|
|
this->drawFullScreenQuadClearStencil();
|
|
|
|
|
|
|
|
glStencilFunc(GL_NEVER, mask_layer, mask_layer);
|
|
|
|
glStencilOp(GL_REPLACE, GL_KEEP, GL_KEEP);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::drawFullScreenQuadClearStencil()
|
|
|
|
{
|
2014-04-03 14:55:55 +08:00
|
|
|
Director* director = Director::getInstance();
|
|
|
|
CCASSERT(nullptr != director, "Director is null when seting matrix stack");
|
2014-06-03 18:31:14 +08:00
|
|
|
|
2014-04-03 14:55:55 +08:00
|
|
|
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
|
|
|
director->loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
2014-06-03 18:31:14 +08:00
|
|
|
|
2014-04-03 14:55:55 +08:00
|
|
|
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
director->loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
DrawPrimitives::drawSolidRect(Vec2(-1,-1), Vec2(1,1), Color4F(1, 1, 1, 1));
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2014-04-03 14:55:55 +08:00
|
|
|
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
|
|
|
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::onAfterDrawStencil()
|
|
|
|
{
|
|
|
|
glDepthMask(_currentDepthWriteMask);
|
|
|
|
glStencilFunc(GL_EQUAL, _mask_layer_le, _mask_layer_le);
|
|
|
|
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Layout::onAfterVisitStencil()
|
|
|
|
{
|
|
|
|
glStencilFunc(_currentStencilFunc, _currentStencilRef, _currentStencilValueMask);
|
|
|
|
glStencilOp(_currentStencilFail, _currentStencilPassDepthFail, _currentStencilPassDepthPass);
|
|
|
|
glStencilMask(_currentStencilWriteMask);
|
|
|
|
if (!_currentStencilEnabled)
|
|
|
|
{
|
|
|
|
glDisable(GL_STENCIL_TEST);
|
|
|
|
}
|
|
|
|
s_layer--;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::onBeforeVisitScissor()
|
|
|
|
{
|
|
|
|
Rect clippingRect = getClippingRect();
|
|
|
|
glEnable(GL_SCISSOR_TEST);
|
|
|
|
auto glview = Director::getInstance()->getOpenGLView();
|
|
|
|
glview->setScissorInPoints(clippingRect.origin.x, clippingRect.origin.y, clippingRect.size.width, clippingRect.size.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::onAfterVisitScissor()
|
|
|
|
{
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void Layout::scissorClippingVisit(Renderer *renderer, const Mat4& parentTransform, uint32_t parentFlags)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
_beforeVisitCmdScissor.init(_globalZOrder);
|
|
|
|
_beforeVisitCmdScissor.func = CC_CALLBACK_0(Layout::onBeforeVisitScissor, this);
|
|
|
|
renderer->addCommand(&_beforeVisitCmdScissor);
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
ProtectedNode::visit(renderer, parentTransform, parentFlags);
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
_afterVisitCmdScissor.init(_globalZOrder);
|
|
|
|
_afterVisitCmdScissor.func = CC_CALLBACK_0(Layout::onAfterVisitScissor, this);
|
|
|
|
renderer->addCommand(&_afterVisitCmdScissor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::setClippingEnabled(bool able)
|
|
|
|
{
|
|
|
|
if (able == _clippingEnabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_clippingEnabled = able;
|
|
|
|
switch (_clippingType)
|
|
|
|
{
|
2014-05-09 16:31:22 +08:00
|
|
|
case ClippingType::STENCIL:
|
2014-03-11 17:13:54 +08:00
|
|
|
if (able)
|
|
|
|
{
|
|
|
|
static bool once = true;
|
|
|
|
if (once)
|
|
|
|
{
|
|
|
|
glGetIntegerv(GL_STENCIL_BITS, &g_sStencilBits);
|
|
|
|
if (g_sStencilBits <= 0)
|
|
|
|
{
|
|
|
|
CCLOG("Stencil buffer is not enabled.");
|
|
|
|
}
|
|
|
|
once = false;
|
|
|
|
}
|
|
|
|
_clippingStencil = DrawNode::create();
|
|
|
|
if (_running)
|
|
|
|
{
|
|
|
|
_clippingStencil->onEnter();
|
|
|
|
}
|
|
|
|
_clippingStencil->retain();
|
2014-06-20 10:40:16 +08:00
|
|
|
setStencilClippingSize(_contentSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_running)
|
|
|
|
{
|
|
|
|
_clippingStencil->onExit();
|
|
|
|
}
|
|
|
|
_clippingStencil->release();
|
|
|
|
_clippingStencil = nullptr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-09 16:31:22 +08:00
|
|
|
void Layout::setClippingType(ClippingType type)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
if (type == _clippingType)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bool clippingEnabled = isClippingEnabled();
|
|
|
|
setClippingEnabled(false);
|
|
|
|
_clippingType = type;
|
|
|
|
setClippingEnabled(clippingEnabled);
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:36:33 +08:00
|
|
|
Layout::ClippingType Layout::getClippingType()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _clippingType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::setStencilClippingSize(const Size &size)
|
|
|
|
{
|
2014-05-09 16:31:22 +08:00
|
|
|
if (_clippingEnabled && _clippingType == ClippingType::STENCIL)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 rect[4];
|
|
|
|
rect[0] = Vec2::ZERO;
|
2014-06-20 10:40:16 +08:00
|
|
|
rect[1] = Vec2(_contentSize.width, 0);
|
|
|
|
rect[2] = Vec2(_contentSize.width, _contentSize.height);
|
|
|
|
rect[3] = Vec2(0, _contentSize.height);
|
2014-03-11 17:13:54 +08:00
|
|
|
Color4F green(0, 1, 0, 1);
|
|
|
|
_clippingStencil->clear();
|
|
|
|
_clippingStencil->drawPolygon(rect, 4, green, 0, green);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:19:05 +08:00
|
|
|
const Rect& Layout::getClippingRect()
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
if (_clippingRectDirty)
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 worldPos = convertToWorldSpace(Vec2::ZERO);
|
2014-03-11 17:13:54 +08:00
|
|
|
AffineTransform t = getNodeToWorldAffineTransform();
|
2014-06-20 10:40:16 +08:00
|
|
|
float scissorWidth = _contentSize.width*t.a;
|
|
|
|
float scissorHeight = _contentSize.height*t.d;
|
2014-03-11 17:13:54 +08:00
|
|
|
Rect parentClippingRect;
|
|
|
|
Layout* parent = this;
|
2014-04-09 20:41:09 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
while (parent)
|
|
|
|
{
|
|
|
|
parent = dynamic_cast<Layout*>(parent->getParent());
|
|
|
|
if(parent)
|
|
|
|
{
|
|
|
|
if (parent->isClippingEnabled())
|
|
|
|
{
|
2014-04-09 20:41:09 +08:00
|
|
|
_clippingParent = parent;
|
|
|
|
break;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_clippingParent)
|
|
|
|
{
|
|
|
|
parentClippingRect = _clippingParent->getClippingRect();
|
|
|
|
float finalX = worldPos.x - (scissorWidth * _anchorPoint.x);
|
|
|
|
float finalY = worldPos.y - (scissorHeight * _anchorPoint.y);
|
|
|
|
float finalWidth = scissorWidth;
|
|
|
|
float finalHeight = scissorHeight;
|
|
|
|
|
|
|
|
float leftOffset = worldPos.x - parentClippingRect.origin.x;
|
|
|
|
if (leftOffset < 0.0f)
|
|
|
|
{
|
|
|
|
finalX = parentClippingRect.origin.x;
|
|
|
|
finalWidth += leftOffset;
|
|
|
|
}
|
|
|
|
float rightOffset = (worldPos.x + scissorWidth) - (parentClippingRect.origin.x + parentClippingRect.size.width);
|
|
|
|
if (rightOffset > 0.0f)
|
|
|
|
{
|
|
|
|
finalWidth -= rightOffset;
|
|
|
|
}
|
|
|
|
float topOffset = (worldPos.y + scissorHeight) - (parentClippingRect.origin.y + parentClippingRect.size.height);
|
|
|
|
if (topOffset > 0.0f)
|
|
|
|
{
|
|
|
|
finalHeight -= topOffset;
|
|
|
|
}
|
|
|
|
float bottomOffset = worldPos.y - parentClippingRect.origin.y;
|
|
|
|
if (bottomOffset < 0.0f)
|
|
|
|
{
|
|
|
|
finalY = parentClippingRect.origin.x;
|
|
|
|
finalHeight += bottomOffset;
|
|
|
|
}
|
|
|
|
if (finalWidth < 0.0f)
|
|
|
|
{
|
|
|
|
finalWidth = 0.0f;
|
|
|
|
}
|
|
|
|
if (finalHeight < 0.0f)
|
|
|
|
{
|
|
|
|
finalHeight = 0.0f;
|
|
|
|
}
|
|
|
|
_clippingRect.origin.x = finalX;
|
|
|
|
_clippingRect.origin.y = finalY;
|
|
|
|
_clippingRect.size.width = finalWidth;
|
|
|
|
_clippingRect.size.height = finalHeight;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_clippingRect.origin.x = worldPos.x - (scissorWidth * _anchorPoint.x);
|
|
|
|
_clippingRect.origin.y = worldPos.y - (scissorHeight * _anchorPoint.y);
|
|
|
|
_clippingRect.size.width = scissorWidth;
|
|
|
|
_clippingRect.size.height = scissorHeight;
|
|
|
|
}
|
|
|
|
_clippingRectDirty = false;
|
|
|
|
}
|
|
|
|
return _clippingRect;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::onSizeChanged()
|
|
|
|
{
|
|
|
|
Widget::onSizeChanged();
|
2014-06-20 10:40:16 +08:00
|
|
|
setStencilClippingSize(_contentSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
_doLayoutDirty = true;
|
|
|
|
_clippingRectDirty = true;
|
|
|
|
if (_backGroundImage)
|
|
|
|
{
|
2014-06-20 10:40:16 +08:00
|
|
|
_backGroundImage->setPosition(Vec2(_contentSize.width/2.0f, _contentSize.height/2.0f));
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_backGroundScale9Enabled && _backGroundImage)
|
|
|
|
{
|
2014-06-20 10:40:16 +08:00
|
|
|
static_cast<extension::Scale9Sprite*>(_backGroundImage)->setPreferredSize(_contentSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_colorRender)
|
|
|
|
{
|
2014-06-20 10:40:16 +08:00
|
|
|
_colorRender->setContentSize(_contentSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
if (_gradientRender)
|
|
|
|
{
|
2014-06-20 10:40:16 +08:00
|
|
|
_gradientRender->setContentSize(_contentSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::setBackGroundImageScale9Enabled(bool able)
|
|
|
|
{
|
|
|
|
if (_backGroundScale9Enabled == able)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-03-24 15:25:44 +08:00
|
|
|
removeProtectedChild(_backGroundImage);
|
2014-03-11 17:13:54 +08:00
|
|
|
_backGroundImage = nullptr;
|
|
|
|
_backGroundScale9Enabled = able;
|
2014-03-24 15:25:44 +08:00
|
|
|
addBackGroundImage();
|
2014-04-03 11:14:35 +08:00
|
|
|
setBackGroundImage(_backGroundImageFileName,_bgImageTexType);
|
2014-03-11 17:13:54 +08:00
|
|
|
setBackGroundImageCapInsets(_backGroundImageCapInsets);
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:36:33 +08:00
|
|
|
bool Layout::isBackGroundImageScale9Enabled()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _backGroundScale9Enabled;
|
|
|
|
}
|
|
|
|
|
2014-04-03 11:14:35 +08:00
|
|
|
void Layout::setBackGroundImage(const std::string& fileName,TextureResType texType)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-04-03 11:14:35 +08:00
|
|
|
if (fileName.empty())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_backGroundImage == nullptr)
|
|
|
|
{
|
|
|
|
addBackGroundImage();
|
|
|
|
}
|
|
|
|
_backGroundImageFileName = fileName;
|
|
|
|
_bgImageTexType = texType;
|
|
|
|
if (_backGroundScale9Enabled)
|
|
|
|
{
|
|
|
|
extension::Scale9Sprite* bgiScale9 = static_cast<extension::Scale9Sprite*>(_backGroundImage);
|
|
|
|
switch (_bgImageTexType)
|
|
|
|
{
|
2014-05-09 14:56:05 +08:00
|
|
|
case TextureResType::LOCAL:
|
2014-03-11 17:13:54 +08:00
|
|
|
bgiScale9->initWithFile(fileName);
|
|
|
|
break;
|
2014-05-09 14:56:05 +08:00
|
|
|
case TextureResType::PLIST:
|
2014-03-11 17:13:54 +08:00
|
|
|
bgiScale9->initWithSpriteFrameName(fileName);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-06-20 10:40:16 +08:00
|
|
|
bgiScale9->setPreferredSize(_contentSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (_bgImageTexType)
|
|
|
|
{
|
2014-05-09 14:56:05 +08:00
|
|
|
case TextureResType::LOCAL:
|
2014-03-11 17:13:54 +08:00
|
|
|
static_cast<Sprite*>(_backGroundImage)->setTexture(fileName);
|
|
|
|
break;
|
2014-05-09 14:56:05 +08:00
|
|
|
case TextureResType::PLIST:
|
2014-03-11 17:13:54 +08:00
|
|
|
static_cast<Sprite*>(_backGroundImage)->setSpriteFrame(fileName);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_backGroundImageTextureSize = _backGroundImage->getContentSize();
|
2014-06-20 10:40:16 +08:00
|
|
|
_backGroundImage->setPosition(Vec2(_contentSize.width/2.0f, _contentSize.height/2.0f));
|
2014-03-11 17:13:54 +08:00
|
|
|
updateBackGroundImageRGBA();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::setBackGroundImageCapInsets(const Rect &capInsets)
|
|
|
|
{
|
|
|
|
_backGroundImageCapInsets = capInsets;
|
|
|
|
if (_backGroundScale9Enabled && _backGroundImage)
|
|
|
|
{
|
|
|
|
static_cast<extension::Scale9Sprite*>(_backGroundImage)->setCapInsets(capInsets);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:36:33 +08:00
|
|
|
const Rect& Layout::getBackGroundImageCapInsets()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _backGroundImageCapInsets;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::supplyTheLayoutParameterLackToChild(Widget *child)
|
|
|
|
{
|
|
|
|
if (!child)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (_layoutType)
|
|
|
|
{
|
2014-05-12 18:00:54 +08:00
|
|
|
case Type::ABSOLUTE:
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
2014-05-12 18:00:54 +08:00
|
|
|
case Type::HORIZONTAL:
|
|
|
|
case Type::VERTICAL:
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-16 10:33:28 +08:00
|
|
|
LinearLayoutParameter* layoutParameter = dynamic_cast<LinearLayoutParameter*>(child->getLayoutParameter());
|
2014-03-11 17:13:54 +08:00
|
|
|
if (!layoutParameter)
|
|
|
|
{
|
|
|
|
child->setLayoutParameter(LinearLayoutParameter::create());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-05-12 18:00:54 +08:00
|
|
|
case Type::RELATIVE:
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-16 10:33:28 +08:00
|
|
|
RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(child->getLayoutParameter());
|
2014-03-11 17:13:54 +08:00
|
|
|
if (!layoutParameter)
|
|
|
|
{
|
|
|
|
child->setLayoutParameter(RelativeLayoutParameter::create());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::addBackGroundImage()
|
|
|
|
{
|
|
|
|
if (_backGroundScale9Enabled)
|
|
|
|
{
|
|
|
|
_backGroundImage = extension::Scale9Sprite::create();
|
2014-03-24 15:25:44 +08:00
|
|
|
addProtectedChild(_backGroundImage, BACKGROUNDIMAGE_Z, -1);
|
2014-06-20 10:40:16 +08:00
|
|
|
static_cast<extension::Scale9Sprite*>(_backGroundImage)->setPreferredSize(_contentSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_backGroundImage = Sprite::create();
|
2014-03-24 15:25:44 +08:00
|
|
|
addProtectedChild(_backGroundImage, BACKGROUNDIMAGE_Z, -1);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-06-20 10:40:16 +08:00
|
|
|
_backGroundImage->setPosition(Vec2(_contentSize.width/2.0f, _contentSize.height/2.0f));
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::removeBackGroundImage()
|
|
|
|
{
|
|
|
|
if (!_backGroundImage)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-03-24 15:25:44 +08:00
|
|
|
removeProtectedChild(_backGroundImage);
|
2014-03-11 17:13:54 +08:00
|
|
|
_backGroundImage = nullptr;
|
|
|
|
_backGroundImageFileName = "";
|
|
|
|
_backGroundImageTextureSize = Size::ZERO;
|
|
|
|
}
|
|
|
|
|
2014-05-09 17:05:36 +08:00
|
|
|
void Layout::setBackGroundColorType(BackGroundColorType type)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
if (_colorType == type)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (_colorType)
|
|
|
|
{
|
2014-05-09 17:05:36 +08:00
|
|
|
case BackGroundColorType::NONE:
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_colorRender)
|
|
|
|
{
|
2014-03-24 15:25:44 +08:00
|
|
|
removeProtectedChild(_colorRender);
|
2014-03-11 17:13:54 +08:00
|
|
|
_colorRender = nullptr;
|
|
|
|
}
|
|
|
|
if (_gradientRender)
|
|
|
|
{
|
2014-03-24 15:25:44 +08:00
|
|
|
removeProtectedChild(_gradientRender);
|
2014-03-11 17:13:54 +08:00
|
|
|
_gradientRender = nullptr;
|
|
|
|
}
|
|
|
|
break;
|
2014-05-09 17:05:36 +08:00
|
|
|
case BackGroundColorType::SOLID:
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_colorRender)
|
|
|
|
{
|
2014-03-24 15:25:44 +08:00
|
|
|
removeProtectedChild(_colorRender);
|
2014-03-11 17:13:54 +08:00
|
|
|
_colorRender = nullptr;
|
|
|
|
}
|
|
|
|
break;
|
2014-05-09 17:05:36 +08:00
|
|
|
case BackGroundColorType::GRADIENT:
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_gradientRender)
|
|
|
|
{
|
2014-03-24 15:25:44 +08:00
|
|
|
removeProtectedChild(_gradientRender);
|
2014-03-11 17:13:54 +08:00
|
|
|
_gradientRender = nullptr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_colorType = type;
|
|
|
|
switch (_colorType)
|
|
|
|
{
|
2014-05-09 17:05:36 +08:00
|
|
|
case BackGroundColorType::NONE:
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
2014-05-09 17:05:36 +08:00
|
|
|
case BackGroundColorType::SOLID:
|
2014-03-11 17:13:54 +08:00
|
|
|
_colorRender = LayerColor::create();
|
2014-06-20 10:40:16 +08:00
|
|
|
_colorRender->setContentSize(_contentSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
_colorRender->setOpacity(_cOpacity);
|
|
|
|
_colorRender->setColor(_cColor);
|
2014-03-24 15:25:44 +08:00
|
|
|
addProtectedChild(_colorRender, BCAKGROUNDCOLORRENDERER_Z, -1);
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
2014-05-09 17:05:36 +08:00
|
|
|
case BackGroundColorType::GRADIENT:
|
2014-03-11 17:13:54 +08:00
|
|
|
_gradientRender = LayerGradient::create();
|
2014-06-20 10:40:16 +08:00
|
|
|
_gradientRender->setContentSize(_contentSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
_gradientRender->setOpacity(_cOpacity);
|
|
|
|
_gradientRender->setStartColor(_gStartColor);
|
|
|
|
_gradientRender->setEndColor(_gEndColor);
|
|
|
|
_gradientRender->setVector(_alongVector);
|
2014-03-24 15:25:44 +08:00
|
|
|
addProtectedChild(_gradientRender, BCAKGROUNDCOLORRENDERER_Z, -1);
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:36:33 +08:00
|
|
|
Layout::BackGroundColorType Layout::getBackGroundColorType()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _colorType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::setBackGroundColor(const Color3B &color)
|
|
|
|
{
|
|
|
|
_cColor = color;
|
|
|
|
if (_colorRender)
|
|
|
|
{
|
|
|
|
_colorRender->setColor(color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:36:33 +08:00
|
|
|
const Color3B& Layout::getBackGroundColor()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _cColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::setBackGroundColor(const Color3B &startColor, const Color3B &endColor)
|
|
|
|
{
|
|
|
|
_gStartColor = startColor;
|
|
|
|
if (_gradientRender)
|
|
|
|
{
|
|
|
|
_gradientRender->setStartColor(startColor);
|
|
|
|
}
|
|
|
|
_gEndColor = endColor;
|
|
|
|
if (_gradientRender)
|
|
|
|
{
|
|
|
|
_gradientRender->setEndColor(endColor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:36:33 +08:00
|
|
|
const Color3B& Layout::getBackGroundStartColor()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _gStartColor;
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:36:33 +08:00
|
|
|
const Color3B& Layout::getBackGroundEndColor()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _gEndColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::setBackGroundColorOpacity(GLubyte opacity)
|
|
|
|
{
|
|
|
|
_cOpacity = opacity;
|
|
|
|
switch (_colorType)
|
|
|
|
{
|
2014-05-09 17:05:36 +08:00
|
|
|
case BackGroundColorType::NONE:
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
2014-05-09 17:05:36 +08:00
|
|
|
case BackGroundColorType::SOLID:
|
2014-03-11 17:13:54 +08:00
|
|
|
_colorRender->setOpacity(opacity);
|
|
|
|
break;
|
2014-05-09 17:05:36 +08:00
|
|
|
case BackGroundColorType::GRADIENT:
|
2014-03-11 17:13:54 +08:00
|
|
|
_gradientRender->setOpacity(opacity);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:36:33 +08:00
|
|
|
GLubyte Layout::getBackGroundColorOpacity()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _cOpacity;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Layout::setBackGroundColorVector(const Vec2 &vector)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
_alongVector = vector;
|
|
|
|
if (_gradientRender)
|
|
|
|
{
|
|
|
|
_gradientRender->setVector(vector);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:36:33 +08:00
|
|
|
const Vec2& Layout::getBackGroundColorVector()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _alongVector;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::setBackGroundImageColor(const Color3B &color)
|
|
|
|
{
|
|
|
|
_backGroundImageColor = color;
|
|
|
|
updateBackGroundImageColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::setBackGroundImageOpacity(GLubyte opacity)
|
|
|
|
{
|
|
|
|
_backGroundImageOpacity = opacity;
|
|
|
|
updateBackGroundImageOpacity();
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:36:33 +08:00
|
|
|
const Color3B& Layout::getBackGroundImageColor()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _backGroundImageColor;
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:36:33 +08:00
|
|
|
GLubyte Layout::getBackGroundImageOpacity()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _backGroundImageOpacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::updateBackGroundImageColor()
|
|
|
|
{
|
|
|
|
if (_backGroundImage)
|
|
|
|
{
|
|
|
|
_backGroundImage->setColor(_backGroundImageColor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::updateBackGroundImageOpacity()
|
|
|
|
{
|
|
|
|
if (_backGroundImage)
|
|
|
|
{
|
|
|
|
_backGroundImage->setOpacity(_backGroundImageOpacity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::updateBackGroundImageRGBA()
|
|
|
|
{
|
|
|
|
if (_backGroundImage)
|
|
|
|
{
|
|
|
|
_backGroundImage->setColor(_backGroundImageColor);
|
|
|
|
_backGroundImage->setOpacity(_backGroundImageOpacity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Size& Layout::getBackGroundImageTextureSize() const
|
|
|
|
{
|
|
|
|
return _backGroundImageTextureSize;
|
|
|
|
}
|
|
|
|
|
2014-05-12 18:00:54 +08:00
|
|
|
void Layout::setLayoutType(Type type)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
_layoutType = type;
|
2014-05-14 18:24:23 +08:00
|
|
|
|
2014-03-24 15:25:44 +08:00
|
|
|
for (auto& child : _children)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-03-24 15:25:44 +08:00
|
|
|
Widget* widgetChild = dynamic_cast<Widget*>(child);
|
|
|
|
if (widgetChild)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
supplyTheLayoutParameterLackToChild(static_cast<Widget*>(child));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_doLayoutDirty = true;
|
|
|
|
}
|
2014-03-25 17:15:54 +08:00
|
|
|
|
2014-05-14 18:24:23 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2014-05-12 18:00:54 +08:00
|
|
|
Layout::Type Layout::getLayoutType() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _layoutType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::requestDoLayout()
|
|
|
|
{
|
|
|
|
_doLayoutDirty = true;
|
|
|
|
}
|
2014-05-15 10:07:42 +08:00
|
|
|
|
2014-05-27 13:49:00 +08:00
|
|
|
Size Layout::getLayoutContentSize()const
|
2014-05-15 10:07:42 +08:00
|
|
|
{
|
2014-06-20 11:18:53 +08:00
|
|
|
return this->getContentSize();
|
2014-05-15 10:07:42 +08:00
|
|
|
}
|
|
|
|
|
2014-05-27 10:19:05 +08:00
|
|
|
const Vector<Node*>& Layout::getLayoutElements()const
|
2014-05-15 10:07:42 +08:00
|
|
|
{
|
|
|
|
return this->getChildren();
|
|
|
|
}
|
2014-05-15 10:23:25 +08:00
|
|
|
|
2014-05-28 10:11:49 +08:00
|
|
|
LayoutManager* Layout::createLayoutManager()
|
2014-05-15 10:23:25 +08:00
|
|
|
{
|
2014-05-28 10:11:49 +08:00
|
|
|
LayoutManager* exe = nullptr;
|
2014-05-15 10:23:25 +08:00
|
|
|
switch (_layoutType)
|
|
|
|
{
|
2014-05-15 10:48:33 +08:00
|
|
|
case Type::VERTICAL:
|
2014-05-28 10:11:49 +08:00
|
|
|
exe = LinearVerticalLayoutManager::create();
|
2014-05-15 10:23:25 +08:00
|
|
|
break;
|
2014-05-15 10:48:33 +08:00
|
|
|
case Type::HORIZONTAL:
|
2014-05-28 10:11:49 +08:00
|
|
|
exe = LinearHorizontalLayoutManager::create();
|
2014-05-15 10:23:25 +08:00
|
|
|
break;
|
2014-05-15 10:48:33 +08:00
|
|
|
case Type::RELATIVE:
|
2014-05-28 10:11:49 +08:00
|
|
|
exe = RelativeLayoutManager::create();
|
2014-05-15 10:23:25 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return exe;
|
|
|
|
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
void Layout::doLayout()
|
|
|
|
{
|
|
|
|
if (!_doLayoutDirty)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-05-28 10:11:49 +08:00
|
|
|
LayoutManager* executant = this->createLayoutManager();
|
2014-05-15 10:07:42 +08:00
|
|
|
|
2014-05-14 18:24:23 +08:00
|
|
|
if (executant)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-05-15 10:07:42 +08:00
|
|
|
executant->doLayout(this);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-05-14 18:24:23 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
_doLayoutDirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Layout::getDescription() const
|
|
|
|
{
|
|
|
|
return "Layout";
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* Layout::createCloneInstance()
|
|
|
|
{
|
|
|
|
return Layout::create();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::copyClonedWidgetChildren(Widget* model)
|
|
|
|
{
|
|
|
|
Widget::copyClonedWidgetChildren(model);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::copySpecialProperties(Widget *widget)
|
|
|
|
{
|
|
|
|
Layout* layout = dynamic_cast<Layout*>(widget);
|
|
|
|
if (layout)
|
|
|
|
{
|
|
|
|
setBackGroundImageScale9Enabled(layout->_backGroundScale9Enabled);
|
2014-04-03 11:14:35 +08:00
|
|
|
setBackGroundImage(layout->_backGroundImageFileName,layout->_bgImageTexType);
|
2014-03-11 17:13:54 +08:00
|
|
|
setBackGroundImageCapInsets(layout->_backGroundImageCapInsets);
|
|
|
|
setBackGroundColorType(layout->_colorType);
|
|
|
|
setBackGroundColor(layout->_cColor);
|
|
|
|
setBackGroundColor(layout->_gStartColor, layout->_gEndColor);
|
|
|
|
setBackGroundColorOpacity(layout->_cOpacity);
|
|
|
|
setBackGroundColorVector(layout->_alongVector);
|
|
|
|
setLayoutType(layout->_layoutType);
|
|
|
|
setClippingEnabled(layout->_clippingEnabled);
|
|
|
|
setClippingType(layout->_clippingType);
|
2014-05-23 15:03:46 +08:00
|
|
|
_loopFocus = layout->_loopFocus;
|
|
|
|
_passFocusToChild = layout->_passFocusToChild;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
2014-05-04 14:50:12 +08:00
|
|
|
|
|
|
|
void Layout::setLoopFocus(bool loop)
|
|
|
|
{
|
|
|
|
_loopFocus = loop;
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:36:33 +08:00
|
|
|
bool Layout::isLoopFocus()const
|
2014-05-04 14:50:12 +08:00
|
|
|
{
|
|
|
|
return _loopFocus;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Layout::setPassFocusToChild(bool pass)
|
|
|
|
{
|
|
|
|
_passFocusToChild = pass;
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:36:33 +08:00
|
|
|
bool Layout::isPassFocusToChild()const
|
2014-05-04 14:50:12 +08:00
|
|
|
{
|
|
|
|
return _passFocusToChild;
|
|
|
|
}
|
|
|
|
|
2014-05-20 10:16:19 +08:00
|
|
|
Size Layout::getLayoutAccumulatedSize()const
|
2014-05-04 14:50:12 +08:00
|
|
|
{
|
2014-05-07 11:55:00 +08:00
|
|
|
const auto& children = this->getChildren();
|
2014-05-06 11:46:28 +08:00
|
|
|
Size layoutSize = Size::ZERO;
|
|
|
|
int widgetCount =0;
|
|
|
|
for(const auto& widget : children)
|
2014-05-04 14:50:12 +08:00
|
|
|
{
|
|
|
|
Layout *layout = dynamic_cast<Layout*>(widget);
|
2014-05-06 12:04:52 +08:00
|
|
|
if (nullptr != layout)
|
|
|
|
{
|
2014-05-20 10:16:19 +08:00
|
|
|
layoutSize = layoutSize + layout->getLayoutAccumulatedSize();
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
2014-05-06 12:04:52 +08:00
|
|
|
else
|
|
|
|
{
|
2014-05-06 11:46:28 +08:00
|
|
|
Widget *w = dynamic_cast<Widget*>(widget);
|
2014-05-06 12:04:52 +08:00
|
|
|
if (w)
|
|
|
|
{
|
2014-05-06 11:46:28 +08:00
|
|
|
widgetCount++;
|
2014-05-16 10:33:28 +08:00
|
|
|
Margin m = w->getLayoutParameter()->getMargin();
|
2014-06-20 11:18:53 +08:00
|
|
|
layoutSize = layoutSize + w->getContentSize() + Size(m.right + m.left, m.top + m.bottom) * 0.5;
|
2014-05-06 11:46:28 +08:00
|
|
|
}
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-06 11:46:28 +08:00
|
|
|
//substract extra size
|
2014-05-12 18:00:54 +08:00
|
|
|
Type type = this->getLayoutType();
|
|
|
|
if (type == Type::HORIZONTAL)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-06 11:46:28 +08:00
|
|
|
layoutSize = layoutSize - Size(0, layoutSize.height/widgetCount * (widgetCount-1));
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
2014-05-12 18:00:54 +08:00
|
|
|
if (type == Type::VERTICAL)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-06 11:46:28 +08:00
|
|
|
layoutSize = layoutSize - Size(layoutSize.width/widgetCount * (widgetCount-1), 0);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
return layoutSize;
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:36:33 +08:00
|
|
|
Vec2 Layout::getWorldCenterPoint(Widget* widget)const
|
2014-05-04 14:50:12 +08:00
|
|
|
{
|
|
|
|
Layout *layout = dynamic_cast<Layout*>(widget);
|
2014-05-07 18:27:05 +08:00
|
|
|
//FIXEDME: we don't need to calculate the content size of layout anymore
|
2014-06-20 11:18:53 +08:00
|
|
|
Size widgetSize = layout ? layout->getLayoutAccumulatedSize() : widget->getContentSize();
|
2014-05-07 18:27:05 +08:00
|
|
|
// CCLOG("contnet size : width = %f, height = %f", widgetSize.width, widgetSize.height);
|
2014-05-15 01:07:09 +08:00
|
|
|
return widget->convertToWorldSpace(Vec2(widgetSize.width/2, widgetSize.height/2));
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
|
2014-05-08 10:46:05 +08:00
|
|
|
float Layout::caculateNearestDistance(Widget* baseWidget)
|
2014-05-07 18:27:05 +08:00
|
|
|
{
|
|
|
|
float distance = FLT_MAX;
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 widgetPosition = this->getWorldCenterPoint(baseWidget);
|
2014-05-07 18:27:05 +08:00
|
|
|
|
|
|
|
for (Node* node : _children) {
|
|
|
|
Layout *layout = dynamic_cast<Layout*>(node);
|
|
|
|
int length;
|
|
|
|
if (layout) {
|
2014-05-08 10:46:05 +08:00
|
|
|
length = layout->caculateNearestDistance(baseWidget);
|
2014-05-07 18:27:05 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Widget* w = dynamic_cast<Widget*>(node);
|
2014-05-08 10:46:05 +08:00
|
|
|
if (w && w->isFocusEnabled()) {
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 wPosition = this->getWorldCenterPoint(w);
|
2014-05-07 18:27:05 +08:00
|
|
|
length = (wPosition - widgetPosition).length();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length < distance) {
|
|
|
|
distance = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
return distance;
|
|
|
|
}
|
2014-05-08 10:46:05 +08:00
|
|
|
|
|
|
|
float Layout::caculateFarestDistance(cocos2d::ui::Widget *baseWidget)
|
|
|
|
{
|
|
|
|
float distance = -FLT_MAX;
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 widgetPosition = this->getWorldCenterPoint(baseWidget);
|
2014-05-08 10:46:05 +08:00
|
|
|
|
|
|
|
for (Node* node : _children) {
|
|
|
|
Layout *layout = dynamic_cast<Layout*>(node);
|
|
|
|
int length;
|
|
|
|
if (layout) {
|
|
|
|
length = layout->caculateFarestDistance(baseWidget);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Widget* w = dynamic_cast<Widget*>(node);
|
|
|
|
if (w && w->isFocusEnabled()) {
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 wPosition = this->getWorldCenterPoint(w);
|
2014-05-08 10:46:05 +08:00
|
|
|
length = (wPosition - widgetPosition).length();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length > distance) {
|
|
|
|
distance = length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return distance;
|
|
|
|
}
|
2014-05-04 14:50:12 +08:00
|
|
|
|
2014-05-08 11:24:09 +08:00
|
|
|
int Layout::findFirstFocusEnabledWidgetIndex()
|
|
|
|
{
|
|
|
|
ssize_t index = 0;
|
|
|
|
ssize_t count = this->getChildren().size();
|
|
|
|
while (index < count) {
|
|
|
|
Widget* w = dynamic_cast<Widget*>(_children.at(index));
|
|
|
|
if (w && w->isFocusEnabled()) {
|
|
|
|
return (int)index;
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
CCASSERT(0, "invalide operation");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:27:05 +08:00
|
|
|
int Layout::findNearestChildWidgetIndex(FocusDirection direction, Widget* baseWidget)
|
2014-05-04 14:50:12 +08:00
|
|
|
{
|
2014-05-07 18:27:05 +08:00
|
|
|
if (baseWidget == nullptr || baseWidget == this)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-08 11:24:09 +08:00
|
|
|
return this->findFirstFocusEnabledWidgetIndex();
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
int index = 0;
|
|
|
|
ssize_t count = this->getChildren().size();
|
|
|
|
|
|
|
|
float distance = FLT_MAX;
|
|
|
|
int found = 0;
|
2014-05-08 18:10:21 +08:00
|
|
|
if (direction == FocusDirection::LEFT || direction == FocusDirection::RIGHT)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 widgetPosition = this->getWorldCenterPoint(baseWidget);
|
2014-05-06 12:04:52 +08:00
|
|
|
while (index < count)
|
|
|
|
{
|
2014-05-04 14:50:12 +08:00
|
|
|
Widget *w = dynamic_cast<Widget*>(this->getChildren().at(index));
|
2014-05-06 12:04:52 +08:00
|
|
|
if (w && w->isFocusEnabled())
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 wPosition = this->getWorldCenterPoint(w);
|
2014-05-07 18:27:05 +08:00
|
|
|
float length;
|
|
|
|
Layout *layout = dynamic_cast<Layout*>(w);
|
|
|
|
if (layout)
|
|
|
|
{
|
2014-05-08 10:46:05 +08:00
|
|
|
length = layout->caculateNearestDistance(baseWidget);
|
2014-05-07 18:27:05 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
length = (wPosition - widgetPosition).getLength();
|
|
|
|
}
|
|
|
|
|
2014-05-06 12:04:52 +08:00
|
|
|
if (length < distance)
|
|
|
|
{
|
2014-05-07 18:27:05 +08:00
|
|
|
found = index;
|
|
|
|
distance = length;
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = 0;
|
|
|
|
found = 0;
|
|
|
|
distance = FLT_MAX;
|
2014-05-08 18:10:21 +08:00
|
|
|
if (direction == FocusDirection::DOWN || direction == FocusDirection::UP) {
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 widgetPosition = this->getWorldCenterPoint(baseWidget);
|
2014-05-06 12:04:52 +08:00
|
|
|
while (index < count)
|
|
|
|
{
|
2014-05-04 14:50:12 +08:00
|
|
|
Widget *w = dynamic_cast<Widget*>(this->getChildren().at(index));
|
2014-05-06 12:04:52 +08:00
|
|
|
if (w && w->isFocusEnabled())
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 wPosition = this->getWorldCenterPoint(w);
|
2014-05-07 18:27:05 +08:00
|
|
|
float length;
|
|
|
|
Layout *layout = dynamic_cast<Layout*>(w);
|
|
|
|
if (layout)
|
|
|
|
{
|
2014-05-08 10:46:05 +08:00
|
|
|
length = layout->caculateNearestDistance(baseWidget);
|
2014-05-07 18:27:05 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
length = (wPosition - widgetPosition).getLength();
|
|
|
|
}
|
|
|
|
|
2014-05-06 12:04:52 +08:00
|
|
|
if (length < distance)
|
|
|
|
{
|
2014-05-04 14:50:12 +08:00
|
|
|
found = index;
|
|
|
|
distance = length;
|
|
|
|
}
|
2014-05-07 18:27:05 +08:00
|
|
|
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
CCASSERT(0, "invalid focus direction!!!");
|
|
|
|
return 0;
|
|
|
|
}
|
2014-05-08 10:46:05 +08:00
|
|
|
|
2014-05-08 18:10:21 +08:00
|
|
|
int Layout::findFarestChildWidgetIndex(FocusDirection direction, cocos2d::ui::Widget *baseWidget)
|
2014-05-08 10:46:05 +08:00
|
|
|
{
|
|
|
|
if (baseWidget == nullptr || baseWidget == this)
|
|
|
|
{
|
2014-05-08 11:24:09 +08:00
|
|
|
return this->findFirstFocusEnabledWidgetIndex();
|
2014-05-08 10:46:05 +08:00
|
|
|
}
|
|
|
|
int index = 0;
|
|
|
|
ssize_t count = this->getChildren().size();
|
|
|
|
|
|
|
|
float distance = -FLT_MAX;
|
|
|
|
int found = 0;
|
2014-05-08 18:10:21 +08:00
|
|
|
if (direction == FocusDirection::LEFT || direction == FocusDirection::RIGHT)
|
2014-05-08 10:46:05 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 widgetPosition = this->getWorldCenterPoint(baseWidget);
|
2014-05-08 10:46:05 +08:00
|
|
|
while (index < count)
|
|
|
|
{
|
|
|
|
Widget *w = dynamic_cast<Widget*>(this->getChildren().at(index));
|
|
|
|
if (w && w->isFocusEnabled())
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 wPosition = this->getWorldCenterPoint(w);
|
2014-05-08 10:46:05 +08:00
|
|
|
float length;
|
|
|
|
Layout *layout = dynamic_cast<Layout*>(w);
|
|
|
|
if (layout)
|
|
|
|
{
|
|
|
|
length = layout->caculateFarestDistance(baseWidget);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
length = (wPosition - widgetPosition).getLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length > distance)
|
|
|
|
{
|
|
|
|
found = index;
|
|
|
|
distance = length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = 0;
|
|
|
|
found = 0;
|
|
|
|
distance = -FLT_MAX;
|
2014-05-08 18:10:21 +08:00
|
|
|
if (direction == FocusDirection::DOWN || direction == FocusDirection::UP) {
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 widgetPosition = this->getWorldCenterPoint(baseWidget);
|
2014-05-08 10:46:05 +08:00
|
|
|
while (index < count)
|
|
|
|
{
|
|
|
|
Widget *w = dynamic_cast<Widget*>(this->getChildren().at(index));
|
|
|
|
if (w && w->isFocusEnabled())
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 wPosition = this->getWorldCenterPoint(w);
|
2014-05-08 10:46:05 +08:00
|
|
|
float length;
|
|
|
|
Layout *layout = dynamic_cast<Layout*>(w);
|
|
|
|
if (layout)
|
|
|
|
{
|
|
|
|
length = layout->caculateFarestDistance(baseWidget);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
length = (wPosition - widgetPosition).getLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length > distance)
|
|
|
|
{
|
|
|
|
found = index;
|
|
|
|
distance = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
CCASSERT(0, "invalid focus direction!!!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-04 14:50:12 +08:00
|
|
|
|
2014-05-06 17:28:12 +08:00
|
|
|
Widget* Layout::findFocusEnabledChildWidgetByIndex(ssize_t index)
|
2014-05-04 14:50:12 +08:00
|
|
|
{
|
2014-05-04 18:42:22 +08:00
|
|
|
|
2014-05-07 18:27:05 +08:00
|
|
|
Widget *widget = this->getChildWidgetByIndex(index);
|
2014-05-04 14:50:12 +08:00
|
|
|
|
2014-05-07 14:06:44 +08:00
|
|
|
if (widget)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-07 14:06:44 +08:00
|
|
|
if (widget->isFocusEnabled())
|
2014-05-06 17:28:12 +08:00
|
|
|
{
|
2014-05-07 14:06:44 +08:00
|
|
|
return widget;
|
2014-05-06 17:28:12 +08:00
|
|
|
}
|
|
|
|
index = index + 1;
|
|
|
|
return this->findFocusEnabledChildWidgetByIndex(index);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
2014-05-04 18:42:22 +08:00
|
|
|
return nullptr;
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
2014-05-08 10:46:05 +08:00
|
|
|
|
|
|
|
Widget *Layout::findFirstNonLayoutWidget()
|
|
|
|
{
|
|
|
|
Widget* widget = nullptr;
|
|
|
|
for(Node *node : _children)
|
|
|
|
{
|
|
|
|
Layout* layout = dynamic_cast<Layout*>(node);
|
|
|
|
if (layout) {
|
|
|
|
widget = layout->findFirstNonLayoutWidget();
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
Widget *w = dynamic_cast<Widget*>(node);
|
|
|
|
if (w) {
|
|
|
|
widget = w;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layout::findProperSearchingFunctor(FocusDirection dir, Widget* baseWidget)
|
|
|
|
{
|
|
|
|
if (baseWidget == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 previousWidgetPosition = this->getWorldCenterPoint(baseWidget);
|
2014-05-08 10:46:05 +08:00
|
|
|
|
2014-05-26 22:57:50 +08:00
|
|
|
Vec2 widgetPosition = this->getWorldCenterPoint(this->findFirstNonLayoutWidget());
|
2014-05-08 10:46:05 +08:00
|
|
|
|
2014-05-08 18:10:21 +08:00
|
|
|
if (dir == FocusDirection::LEFT) {
|
2014-05-26 22:57:50 +08:00
|
|
|
if (previousWidgetPosition.x > widgetPosition.x) {
|
2014-05-08 10:46:05 +08:00
|
|
|
onPassFocusToChild = CC_CALLBACK_2(Layout::findNearestChildWidgetIndex, this);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
onPassFocusToChild = CC_CALLBACK_2(Layout::findFarestChildWidgetIndex, this);
|
|
|
|
}
|
2014-05-08 18:10:21 +08:00
|
|
|
}else if(dir == FocusDirection::RIGHT){
|
2014-05-26 22:57:50 +08:00
|
|
|
if (previousWidgetPosition.x > widgetPosition.x) {
|
2014-05-08 10:46:05 +08:00
|
|
|
onPassFocusToChild = CC_CALLBACK_2(Layout::findFarestChildWidgetIndex, this);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
onPassFocusToChild = CC_CALLBACK_2(Layout::findNearestChildWidgetIndex, this);
|
|
|
|
}
|
2014-05-08 18:10:21 +08:00
|
|
|
}else if(dir == FocusDirection::DOWN){
|
2014-05-26 22:57:50 +08:00
|
|
|
if (previousWidgetPosition.y > widgetPosition.y) {
|
2014-05-08 10:46:05 +08:00
|
|
|
onPassFocusToChild = CC_CALLBACK_2(Layout::findNearestChildWidgetIndex, this);
|
|
|
|
}else{
|
|
|
|
onPassFocusToChild = CC_CALLBACK_2(Layout::findFarestChildWidgetIndex, this);
|
|
|
|
}
|
2014-05-08 18:10:21 +08:00
|
|
|
}else if(dir == FocusDirection::UP){
|
2014-05-26 22:57:50 +08:00
|
|
|
if (previousWidgetPosition.y < widgetPosition.y) {
|
2014-05-08 10:46:05 +08:00
|
|
|
onPassFocusToChild = CC_CALLBACK_2(Layout::findNearestChildWidgetIndex, this);
|
|
|
|
}else{
|
|
|
|
onPassFocusToChild = CC_CALLBACK_2(Layout::findFarestChildWidgetIndex, this);
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
CCASSERT(0, "invalid direction!");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-04 14:50:12 +08:00
|
|
|
|
2014-05-08 18:10:21 +08:00
|
|
|
Widget* Layout::passFocusToChild(FocusDirection dir, cocos2d::ui::Widget *current)
|
2014-05-04 14:50:12 +08:00
|
|
|
{
|
2014-05-07 10:05:20 +08:00
|
|
|
if (checkFocusEnabledChild())
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-26 22:57:50 +08:00
|
|
|
Widget* previousWidget = this->getCurrentFocusedWidget();
|
2014-05-08 10:46:05 +08:00
|
|
|
|
|
|
|
this->findProperSearchingFunctor(dir, previousWidget);
|
|
|
|
|
2014-05-07 18:27:05 +08:00
|
|
|
int index = onPassFocusToChild(dir, previousWidget);
|
|
|
|
|
|
|
|
Widget *widget = this->getChildWidgetByIndex(index);
|
2014-05-07 10:05:20 +08:00
|
|
|
Layout *layout = dynamic_cast<Layout*>(widget);
|
|
|
|
if (layout)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-26 22:57:50 +08:00
|
|
|
layout->_isFocusPassing = true;
|
2014-05-07 14:06:44 +08:00
|
|
|
return layout->findNextFocusedWidget(dir, layout);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
2014-05-06 17:28:12 +08:00
|
|
|
else
|
|
|
|
{
|
2014-05-26 22:57:50 +08:00
|
|
|
this->dispatchFocusEvent(current, widget);
|
2014-05-07 10:05:20 +08:00
|
|
|
return widget;
|
2014-05-06 17:28:12 +08:00
|
|
|
}
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
2014-05-07 10:05:20 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
|
2014-05-27 10:36:33 +08:00
|
|
|
bool Layout::checkFocusEnabledChild()const
|
2014-05-04 14:50:12 +08:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
for(Node* node : _children)
|
|
|
|
{
|
|
|
|
Widget* widget = dynamic_cast<Widget*>(node);
|
2014-05-06 12:04:52 +08:00
|
|
|
if (widget && widget->isFocusEnabled())
|
|
|
|
{
|
2014-05-04 14:50:12 +08:00
|
|
|
ret = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:36:33 +08:00
|
|
|
Widget* Layout::getChildWidgetByIndex(ssize_t index)const
|
2014-05-04 18:42:22 +08:00
|
|
|
{
|
|
|
|
ssize_t size = _children.size();
|
|
|
|
int count = 0;
|
2014-05-06 12:04:52 +08:00
|
|
|
ssize_t oldIndex = index;
|
2014-05-07 14:06:44 +08:00
|
|
|
Widget *widget = nullptr;
|
2014-05-06 12:04:52 +08:00
|
|
|
while (index < size)
|
|
|
|
{
|
2014-05-04 18:42:22 +08:00
|
|
|
Widget* firstChild = dynamic_cast<Widget*>(_children.at(index));
|
2014-05-06 12:04:52 +08:00
|
|
|
if (firstChild)
|
|
|
|
{
|
2014-05-07 14:06:44 +08:00
|
|
|
widget = firstChild;
|
2014-05-04 18:42:22 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
2014-05-07 14:06:44 +08:00
|
|
|
if (nullptr == widget)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-04 18:42:22 +08:00
|
|
|
int begin = 0;
|
2014-05-06 12:04:52 +08:00
|
|
|
while (begin < oldIndex)
|
|
|
|
{
|
2014-05-04 18:42:22 +08:00
|
|
|
Widget* firstChild = dynamic_cast<Widget*>(_children.at(begin));
|
2014-05-06 12:04:52 +08:00
|
|
|
if (firstChild)
|
|
|
|
{
|
2014-05-07 14:06:44 +08:00
|
|
|
widget = firstChild;
|
2014-05-04 18:42:22 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
begin++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-07 14:06:44 +08:00
|
|
|
return widget;
|
2014-05-04 18:42:22 +08:00
|
|
|
}
|
|
|
|
|
2014-05-07 14:26:41 +08:00
|
|
|
Widget* Layout::getPreviousFocusedWidget(FocusDirection direction, Widget *current)
|
2014-05-04 14:50:12 +08:00
|
|
|
{
|
|
|
|
Widget *nextWidget = nullptr;
|
|
|
|
ssize_t previousWidgetPos = _children.getIndex(current);
|
|
|
|
previousWidgetPos = previousWidgetPos - 1;
|
2014-05-06 12:04:52 +08:00
|
|
|
if (previousWidgetPos >= 0)
|
|
|
|
{
|
2014-05-07 18:27:05 +08:00
|
|
|
nextWidget = this->getChildWidgetByIndex(previousWidgetPos);
|
2014-05-06 12:04:52 +08:00
|
|
|
if (nextWidget->isFocusEnabled())
|
|
|
|
{
|
2014-05-04 14:50:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
Layout* layout = dynamic_cast<Layout*>(nextWidget);
|
2014-05-06 12:04:52 +08:00
|
|
|
if (layout)
|
|
|
|
{
|
2014-05-26 22:57:50 +08:00
|
|
|
layout->_isFocusPassing = true;
|
2014-05-07 14:26:41 +08:00
|
|
|
return layout->findNextFocusedWidget(direction, layout);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
2014-05-26 22:57:50 +08:00
|
|
|
this->dispatchFocusEvent(current, nextWidget);
|
2014-05-04 14:50:12 +08:00
|
|
|
return nextWidget;
|
|
|
|
}
|
2014-05-06 12:04:52 +08:00
|
|
|
else
|
|
|
|
{
|
2014-05-04 14:50:12 +08:00
|
|
|
//handling the disabled widget, there is no actual focus lose or get, so we don't need any envet
|
2014-05-07 14:26:41 +08:00
|
|
|
return this->getPreviousFocusedWidget(direction, nextWidget);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
2014-05-06 12:04:52 +08:00
|
|
|
}else
|
|
|
|
{
|
|
|
|
if (_loopFocus)
|
|
|
|
{
|
|
|
|
if (checkFocusEnabledChild())
|
|
|
|
{
|
2014-05-04 14:50:12 +08:00
|
|
|
previousWidgetPos = _children.size()-1;
|
2014-05-07 18:27:05 +08:00
|
|
|
nextWidget = this->getChildWidgetByIndex(previousWidgetPos);
|
2014-05-06 12:04:52 +08:00
|
|
|
if (nextWidget->isFocusEnabled())
|
|
|
|
{
|
2014-05-04 14:50:12 +08:00
|
|
|
Layout* layout = dynamic_cast<Layout*>(nextWidget);
|
2014-05-06 12:04:52 +08:00
|
|
|
if (layout)
|
|
|
|
{
|
2014-05-26 22:57:50 +08:00
|
|
|
layout->_isFocusPassing = true;
|
2014-05-07 14:26:41 +08:00
|
|
|
return layout->findNextFocusedWidget(direction, layout);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
2014-05-06 12:04:52 +08:00
|
|
|
else
|
|
|
|
{
|
2014-05-26 22:57:50 +08:00
|
|
|
this->dispatchFocusEvent(current, nextWidget);
|
2014-05-04 14:50:12 +08:00
|
|
|
return nextWidget;
|
|
|
|
}
|
|
|
|
}
|
2014-05-06 12:04:52 +08:00
|
|
|
else
|
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return this->getPreviousFocusedWidget(direction, nextWidget);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
}
|
2014-05-06 12:04:52 +08:00
|
|
|
else
|
|
|
|
{
|
2014-05-07 10:05:20 +08:00
|
|
|
if (dynamic_cast<Layout*>(current)) {
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return _focusedWidget;
|
|
|
|
}
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
}
|
2014-05-06 12:04:52 +08:00
|
|
|
else
|
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
if (isLastWidgetInContainer(current, direction))
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
if (isWidgetAncestorSupportLoopFocus(this, direction))
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return Widget::findNextFocusedWidget(direction, this);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
2014-05-07 10:05:20 +08:00
|
|
|
if (dynamic_cast<Layout*>(current)) {
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return _focusedWidget;
|
|
|
|
}
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
2014-05-06 12:04:52 +08:00
|
|
|
else
|
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return Widget::findNextFocusedWidget(direction, this);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-07 14:26:41 +08:00
|
|
|
Widget* Layout::getNextFocusedWidget(FocusDirection direction, Widget *current)
|
2014-05-04 14:50:12 +08:00
|
|
|
{
|
|
|
|
Widget *nextWidget = nullptr;
|
|
|
|
ssize_t previousWidgetPos = _children.getIndex(current);
|
|
|
|
previousWidgetPos = previousWidgetPos + 1;
|
2014-05-06 12:04:52 +08:00
|
|
|
if (previousWidgetPos < _children.size())
|
|
|
|
{
|
2014-05-07 18:27:05 +08:00
|
|
|
nextWidget = this->getChildWidgetByIndex(previousWidgetPos);
|
2014-05-04 14:50:12 +08:00
|
|
|
//handle widget
|
2014-05-06 12:04:52 +08:00
|
|
|
if (nextWidget)
|
|
|
|
{
|
|
|
|
if (nextWidget->isFocusEnabled())
|
|
|
|
{
|
2014-05-04 14:50:12 +08:00
|
|
|
|
|
|
|
Layout* layout = dynamic_cast<Layout*>(nextWidget);
|
2014-05-06 12:04:52 +08:00
|
|
|
if (layout)
|
|
|
|
{
|
2014-05-26 22:57:50 +08:00
|
|
|
layout->_isFocusPassing = true;
|
2014-05-07 14:26:41 +08:00
|
|
|
return layout->findNextFocusedWidget(direction, layout);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
2014-05-06 12:04:52 +08:00
|
|
|
else
|
|
|
|
{
|
2014-05-26 22:57:50 +08:00
|
|
|
this->dispatchFocusEvent(current, nextWidget);
|
2014-05-04 14:50:12 +08:00
|
|
|
return nextWidget;
|
|
|
|
}
|
|
|
|
}
|
2014-05-06 12:04:52 +08:00
|
|
|
else
|
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return this->getNextFocusedWidget(direction, nextWidget);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
}
|
2014-05-06 12:04:52 +08:00
|
|
|
else
|
|
|
|
{
|
2014-05-04 14:50:12 +08:00
|
|
|
return current;
|
|
|
|
}
|
2014-05-06 12:04:52 +08:00
|
|
|
}else
|
|
|
|
{
|
|
|
|
if (_loopFocus)
|
|
|
|
{
|
|
|
|
if (checkFocusEnabledChild())
|
|
|
|
{
|
2014-05-04 14:50:12 +08:00
|
|
|
previousWidgetPos = 0;
|
2014-05-07 18:27:05 +08:00
|
|
|
nextWidget = this->getChildWidgetByIndex(previousWidgetPos);
|
2014-05-06 12:04:52 +08:00
|
|
|
if (nextWidget->isFocusEnabled())
|
|
|
|
{
|
2014-05-04 14:50:12 +08:00
|
|
|
|
|
|
|
Layout* layout = dynamic_cast<Layout*>(nextWidget);
|
2014-05-06 12:04:52 +08:00
|
|
|
if (layout)
|
|
|
|
{
|
2014-05-26 22:57:50 +08:00
|
|
|
layout->_isFocusPassing = true;
|
2014-05-07 14:26:41 +08:00
|
|
|
return layout->findNextFocusedWidget(direction, layout);
|
2014-05-26 22:57:50 +08:00
|
|
|
}
|
|
|
|
else
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-26 22:57:50 +08:00
|
|
|
this->dispatchFocusEvent(current, nextWidget);
|
2014-05-04 14:50:12 +08:00
|
|
|
return nextWidget;
|
|
|
|
}
|
|
|
|
}
|
2014-05-06 12:04:52 +08:00
|
|
|
else
|
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return this->getNextFocusedWidget(direction, nextWidget);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
}
|
2014-05-06 12:04:52 +08:00
|
|
|
else
|
|
|
|
{
|
2014-05-07 10:05:20 +08:00
|
|
|
if (dynamic_cast<Layout*>(current)) {
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return _focusedWidget;
|
|
|
|
}
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
2014-05-07 14:26:41 +08:00
|
|
|
if (isLastWidgetInContainer(current, direction))
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
if (isWidgetAncestorSupportLoopFocus(this, direction))
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return Widget::findNextFocusedWidget(direction, this);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
2014-05-07 10:05:20 +08:00
|
|
|
if (dynamic_cast<Layout*>(current)) {
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return _focusedWidget;
|
|
|
|
}
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
2014-05-06 12:04:52 +08:00
|
|
|
else
|
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return Widget::findNextFocusedWidget(direction, this);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:36:33 +08:00
|
|
|
bool Layout::isLastWidgetInContainer(Widget* widget, FocusDirection direction)const
|
2014-05-04 14:50:12 +08:00
|
|
|
{
|
|
|
|
Layout* parent = dynamic_cast<Layout*>(widget->getParent());
|
2014-05-06 12:04:52 +08:00
|
|
|
if (parent == nullptr)
|
|
|
|
{
|
2014-05-04 14:50:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto container = parent->getChildren();
|
|
|
|
ssize_t index = container.getIndex(widget);
|
2014-05-12 18:00:54 +08:00
|
|
|
if (parent->getLayoutType() == Type::HORIZONTAL)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-08 18:10:21 +08:00
|
|
|
if (direction == FocusDirection::LEFT) {
|
2014-05-06 12:04:52 +08:00
|
|
|
if (index == 0)
|
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return true * isLastWidgetInContainer(parent, direction);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-05-08 18:10:21 +08:00
|
|
|
if (direction == FocusDirection::RIGHT) {
|
2014-05-06 12:04:52 +08:00
|
|
|
if (index == container.size()-1)
|
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return true * isLastWidgetInContainer(parent, direction);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-05-08 18:10:21 +08:00
|
|
|
if (direction == FocusDirection::DOWN)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return isLastWidgetInContainer(parent, direction);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
|
2014-05-08 18:10:21 +08:00
|
|
|
if (direction == FocusDirection::UP)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return isLastWidgetInContainer(parent, direction);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
}
|
2014-05-12 18:00:54 +08:00
|
|
|
else if(parent->getLayoutType() == Type::VERTICAL)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-08 18:10:21 +08:00
|
|
|
if (direction == FocusDirection::UP)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
|
|
|
if (index == 0)
|
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return true * isLastWidgetInContainer(parent, direction);
|
2014-05-04 14:50:12 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-05-08 18:10:21 +08:00
|
|
|
if (direction == FocusDirection::DOWN)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
|
|
|
if (index == container.size() - 1)
|
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return true * isLastWidgetInContainer(parent, direction);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-05-08 18:10:21 +08:00
|
|
|
if (direction == FocusDirection::LEFT)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return isLastWidgetInContainer(parent, direction);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
|
2014-05-08 18:10:21 +08:00
|
|
|
if (direction == FocusDirection::RIGHT)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return isLastWidgetInContainer(parent, direction);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCASSERT(0, "invalid layout Type");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:36:33 +08:00
|
|
|
bool Layout::isWidgetAncestorSupportLoopFocus(Widget* widget, FocusDirection direction)const
|
2014-05-04 14:50:12 +08:00
|
|
|
{
|
|
|
|
Layout* parent = dynamic_cast<Layout*>(widget->getParent());
|
2014-05-06 12:04:52 +08:00
|
|
|
if (parent == nullptr)
|
|
|
|
{
|
2014-05-04 14:50:12 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-05-07 14:06:44 +08:00
|
|
|
if (parent->isLoopFocus())
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-04 14:50:12 +08:00
|
|
|
auto layoutType = parent->getLayoutType();
|
2014-05-12 18:00:54 +08:00
|
|
|
if (layoutType == Type::HORIZONTAL)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-08 18:10:21 +08:00
|
|
|
if (direction == FocusDirection::LEFT || direction == FocusDirection::RIGHT)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-04 14:50:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-05-06 12:04:52 +08:00
|
|
|
else
|
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return isWidgetAncestorSupportLoopFocus(parent, direction);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
}
|
2014-05-12 18:00:54 +08:00
|
|
|
if (layoutType == Type::VERTICAL)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-08 18:10:21 +08:00
|
|
|
if (direction == FocusDirection::DOWN || direction == FocusDirection::UP)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-04 14:50:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return isWidgetAncestorSupportLoopFocus(parent, direction);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCASSERT(0, "invalid layout type");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-05-06 12:04:52 +08:00
|
|
|
else
|
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return isWidgetAncestorSupportLoopFocus(parent, direction);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-07 14:26:41 +08:00
|
|
|
Widget* Layout::findNextFocusedWidget(FocusDirection direction, Widget* current)
|
2014-05-04 14:50:12 +08:00
|
|
|
{
|
2014-05-26 22:57:50 +08:00
|
|
|
if (_isFocusPassing || this->isFocused())
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-08 11:24:09 +08:00
|
|
|
Layout* parent = dynamic_cast<Layout*>(this->getParent());
|
2014-05-26 22:57:50 +08:00
|
|
|
_isFocusPassing = false;
|
|
|
|
|
2014-05-07 10:05:20 +08:00
|
|
|
if (_passFocusToChild)
|
|
|
|
{
|
2014-05-08 11:24:09 +08:00
|
|
|
Widget * w = this->passFocusToChild(direction, current);
|
|
|
|
if (dynamic_cast<Layout*>(w)) {
|
|
|
|
if (parent) {
|
2014-05-26 22:57:50 +08:00
|
|
|
parent->_isFocusPassing = true;
|
2014-05-08 11:24:09 +08:00
|
|
|
return parent->findNextFocusedWidget(direction, this);
|
|
|
|
}
|
2014-05-07 10:05:20 +08:00
|
|
|
}
|
2014-05-08 11:24:09 +08:00
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nullptr == parent) {
|
|
|
|
return this;
|
2014-05-07 10:05:20 +08:00
|
|
|
}
|
2014-05-26 22:57:50 +08:00
|
|
|
parent->_isFocusPassing = true;
|
2014-05-08 11:24:09 +08:00
|
|
|
return parent->findNextFocusedWidget(direction, this);
|
|
|
|
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
2014-05-26 22:57:50 +08:00
|
|
|
else if(current->isFocused() || dynamic_cast<Layout*>(current))
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-12 18:00:54 +08:00
|
|
|
if (_layoutType == Type::HORIZONTAL)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
switch (direction)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-08 18:10:21 +08:00
|
|
|
case FocusDirection::LEFT:
|
2014-05-04 14:50:12 +08:00
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return this->getPreviousFocusedWidget(direction, current);
|
2014-05-04 14:50:12 +08:00
|
|
|
}break;
|
2014-05-08 18:10:21 +08:00
|
|
|
case FocusDirection::RIGHT:
|
2014-05-04 14:50:12 +08:00
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return this->getNextFocusedWidget(direction, current);
|
2014-05-04 14:50:12 +08:00
|
|
|
}break;
|
2014-05-08 18:10:21 +08:00
|
|
|
case FocusDirection::DOWN:
|
|
|
|
case FocusDirection::UP:
|
2014-05-04 14:50:12 +08:00
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
if (isLastWidgetInContainer(this, direction))
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
if (isWidgetAncestorSupportLoopFocus(current, direction))
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return Widget::findNextFocusedWidget(direction, this);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
else{
|
2014-05-07 14:26:41 +08:00
|
|
|
return Widget::findNextFocusedWidget(direction, this);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
}break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
CCASSERT(0, "Invalid Focus Direction");
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-05-12 18:00:54 +08:00
|
|
|
else if (_layoutType == Type::VERTICAL)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
switch (direction)
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-08 18:10:21 +08:00
|
|
|
case FocusDirection::LEFT:
|
|
|
|
case FocusDirection::RIGHT:
|
2014-05-04 14:50:12 +08:00
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
if (isLastWidgetInContainer(this, direction))
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
if (isWidgetAncestorSupportLoopFocus(current, direction))
|
2014-05-06 12:04:52 +08:00
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return Widget::findNextFocusedWidget(direction, this);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
return current;
|
|
|
|
}
|
2014-05-06 12:04:52 +08:00
|
|
|
else
|
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return Widget::findNextFocusedWidget(direction, this);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
} break;
|
2014-05-08 18:10:21 +08:00
|
|
|
case FocusDirection::DOWN:
|
2014-05-04 14:50:12 +08:00
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return getNextFocusedWidget(direction, current);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
break;
|
2014-05-08 18:10:21 +08:00
|
|
|
case FocusDirection::UP:
|
2014-05-04 14:50:12 +08:00
|
|
|
{
|
2014-05-07 14:26:41 +08:00
|
|
|
return getPreviousFocusedWidget(direction, current);
|
2014-05-04 14:50:12 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
CCASSERT(0, "Invalid Focus Direction");
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-05-06 12:04:52 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
CCASSERT(0, "Un Supported Layout type, please use VBox and HBox instead!!!");
|
2014-05-04 14:50:12 +08:00
|
|
|
return current;
|
|
|
|
}
|
|
|
|
}
|
2014-05-06 12:04:52 +08:00
|
|
|
else
|
|
|
|
{
|
2014-05-04 14:50:12 +08:00
|
|
|
return current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
NS_CC_END
|