Cocos2d-x3.3Final(7)Focus常用成员函数(C++)

主要函数有 _horizontalLayout->setFocused(true);//设置焦点聚焦在该布局上

_horizontalLayout->setLoopFocus(true);//在该布局内循环设置焦点

_horizontalLayout->setFocusEnabled();//设置该控件上聚焦

_horizontalLayout->isFocusEnabled();//是否可以聚焦

这里说明几点特殊情况 VBox创建的布局,如果isLoopFocus(true);即可以在该布局例循环获取布局里控件的焦点,那么只能在垂直方向上循环获取焦点,在水平方向上不行。

同理如果是HBox创建的布局,且isLoopFocus(true);则只能在水平方向上获取焦点,垂直方向不可以。

如果isLoopFocus(false);则无论水平还是垂直,都不可以,循环获取焦点。

setScale设置的缩放是相对于自己的父类容器的,所以孙容器相对于爷爷容器类的缩放是子容器的缩放系数,乘以孙容器的缩放系数。

如果孙容器没缩放,而子容器缩放了0.5,则相当于孙容器缩放了1,最终相对于爷爷容器的缩放系数是0.5*1还是0.5.

Menu _dpadMenu = Menu::create(); //创建一个菜单

auto winSize = Director::getInstance()->getVisibleSize();
//获取窗口大小

auto leftItem = MenuItemFont::create("Left", CC_CALLBACK_0(UIFocusTestBase::onLeftKeyPressed, this));
//创建菜单项,设置菜单项获得焦点回调函数

_dpadMenu->addChild(leftItem);//加入菜单项到菜单中

//call this method to enable Dpad focus navigation

Widget::enableDpadNavigation(true);

cocos2d::EventListenerFocus _eventListener = EventListenerFocus::create();//新建事件监听器焦点

_eventListener->onFocusChanged = CC_CALLBACK_2(UIFocusTestBase::onFocusChanged, this);//给此焦点设置回调函数

_eventDispatcher->addEventListenerWithFixedPriority(_eventListener, 1);//为特定的事件添加特定优先级的事件监听

//菜单项回调函数

void UIFocusTestBase::onLeftKeyPressed()

{

cocos2d::EventKeyboard::KeyCode cocos2dKey = EventKeyboard::KeyCode::KEY_DPAD_LEFT;//键盘事件值

cocos2d::EventKeyboard event(cocos2dKey, false);//根据此键盘事件值构造键盘事件

cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);//事件调度器调度此事件

}

//焦点改变的回调函数

void UIFocusTestBase::onFocusChanged(cocos2d::ui::Widget * widgetLostFocus, cocos2d::ui::Widget * widgetGetFocus)

{

if (widgetGetFocus && widgetGetFocus->isFocusEnabled()){

widgetGetFocus->setColor(Color3B::RED);
//得到焦点的设置为红色

}

if (widgetLostFocus && widgetLostFocus->isFocusEnabled())

{

widgetLostFocus->setCOlor(Color3B::WHITE);//失去焦点的设为白色

}

if (widgetLostFocus && widgetGetFocus)

{

CCLOG();

}

}

//水平布局测试

Layout *_horizontalLayout = HBox::create();

_horizontalLayout->setFocused(true);

_horizontalLayout->setLoopFocused(true);

int count = 3;

for (in i =0; i < count; ++i)

{

ImageView * w = ImageView::create("cocosui/scrollviewbg.png");

w->setTouchEnabled(true);

w->setTag(i);

w->addTouchEventListener(CC_CALLBACK_2(UIFocusTestHorizontal::onImageViewClicked, this));//添加图片事件回调处理

_horizontalLayout->addChild(w);

}

auto btn = Button::create("cocosui/switch-mask.png");

btn->addTouchEventListener(CC_CALLBACK_2(UIFocusTestHorizontal::toggleFocusLoop, this));//设置是否可以循环

//图片事件回调处理函数

void UIFocusTestBase::onImageViewClicked(cocos2d::Ref * ref, Widget::TouchEventType touchType)

{

if (touchType == Widget::TouchEventType::ENDED){

Widget * w = (Widget *)ref;

if (w->isFocusEnabled())//如果可以被聚焦,点击一下,设置为黄色,并且改为不能被聚焦

{

w->setFocusEnabled(false);

w->setColor(Color3B::YELLOW);

}else{
//如果不能被聚焦,点击一下,设置为白色,并且改为可以被聚焦

w->setFocusEnabled(true);

w->setColor(Color3B::WHITE);

}

}

void UIFocusTestHorizontal::toggleFocusLoop(cocos2d::Ref * pObjc, Widget::TouchEventType type)

{

if (type == WIdget::TouchEventType::ENDED)

{

_horizontalLayout->setLoopFocus(!_horizontalLayout->isLoopFocus());
//点击一下按钮,如果 isLoopFocus为true,则说明焦点只在layout内部循环,否则会跳出layout,如果 为false说明要跳出layout了,就不能再在layout内部循环了。

if (_horizontalLayout->isLoopFocus()){

_loopText->setString("loop enabled");

}else{

_loopText->setString("loop disabled");

}

}

}

//垂直布局测试

cocos2d::ui::Layout _verticalLayout = VBox::create();

int count = 3;

for (int i = 0; i < count; ++i)

{

ImageView * w = ImageView::create("cocosui/scrollviewbg.png");

w->setTouchEnabled(true);

w->addTouchEventListener(CC_CALLBACK_2(UIFocosTestVertical::onImageViewClicked, this));//设置图片事件回调函数

_verticalLayout->addChild(w);

if(i == 2){

w->requestFocus();//请求焦点

}

}

auto btn = Button::create("cocosui/switch-mask.png");btn->addTouchEventListener(CC_CALLBACK_2(UIFocosTestVertical::toggleFocusLoop, this));//这里是要处理整个布局的LoopFocus 即焦点是否仅仅在布局内部移动

这张Layout布局

主题是一个VBox,scale设为0.5,Vbox里有一张图片,锚点为(0,0),图片的scaleX设为2.5,

里面添加了一个HBox,,scale设为0.8,加入到Vbox中,并且添加两张图片,锚点为(1,0),scaleY设为2.0

HBox里再添加VBox,VBox中添加2张图片。

这张图片的布局,第一层是HBox,scale设为0.6,然后加入两张图片,图片锚点为(0, 1),scaleY设为2.4

加入VBox,VBox scale设为0.8,VBox添加两张图片,图片锚点为(0, 1),scaleX设为2.0,

VBox加入新建HBox,HBox里加入两张图片

LinearLayoutParameter *bottomParams = LinearLayoutParameter::create();

params->setMargin(Margin(0, 0, 50, 0)); //参数 左,上,右,下 的顺序

firstVbox->setLayoutParameter(params); //控件设置边界参数,就是该控件与他相邻的控件边界相隔多远,layout之间似乎没有固定的大小,如果设置的边界距离值太小,有可能会重叠在一起。

时间: 2024-08-07 16:32:01

Cocos2d-x3.3Final(7)Focus常用成员函数(C++)的相关文章

string 类操作的重载实现及其提供的其他常用成员函数

目录 1,string 类操作的重载实现 2,String类提供的其他常用成员函数 @ 1,string 类操作的重载实现 /* string 类操作的重载实现 */ class CMyString { public: CMyString(char *ptr = NULL) { if (ptr == NULL) { mpStr = new char[1]; *mpStr = '\0'; } else { mpStr = new char[strlen(ptr) + 1]; strcpy(mpSt

Cocos2d-x3.3Final(1) ListView常用成员函数(C++)

ListView listView = ListView::create(); --创建列表 listView->setDirection(ui::ScrollView::Direction::VERTICAL); --设置列表方向 listView->setBounceEnabled(true); --设置弹力 listView->setBackGroundImage("cocosui/green_edit.png"); --设置背景图片 listView->

Cocos2d-x3.3Final(3) Button常用成员函数(C++)

Button * button = Button::create("cocosui/animationbuttonnormal.png"); //新建按钮 Button* button = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png"); //新建按钮 button->addTouchEventListener(CC_CALLBACK_2(UIBut

Cocos2d-x3.3Final(5)LoadingBar常用成员函数(C++)

loadingbar就是进度条,如果要实现进度条的滑动,我们必须要实现update函数例如: void HelloWorld::update(float delta) { _count++; if (_count > 100) _count = 0; LoadingBar * loadingBar = static_cast<LoadingBar *>(getChildByTag(0)); loadingBar->setPercent(_count); //设置进度条的位置 } 除

Cocos2d-x3.3Final(6)Slider常用成员函数(C++)

滑动栏: Slider * slider = Slider::create(); slider->loadBarTexture("sliderTrack.png");//设置滑动栏的背景图片 slider->loadSlidBallTextures("sliderThumb.png", "sliderThumb.png", ""); //滑动栏的滑动按钮图片 slider->loadProgressBarTe

Cocos2d-x3.3Final(12)TextField常用成员函数(C++)

TextField * textField = TextField::create("input words here", "Arial", 30); textField->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f)); textField->addEventListener(CC_CALLBACK_2(HelloWorld::textFieldEvent, th

Cocos2d-x3.3Final(4) ImageView常用成员函数(C++)

说明这个系列只是提供函数的熟悉和方便查找,实在没什么技术含量- - ImageView * imageView = ImageView::create("cocosui/ccicon.png");//新建图片 imageView->setScale9Enabled(true); //是否启用九宫格显示模式 imageViewChild->setSizeType(Widget::SizeType::PERCENT);//设置大小类型 imageViewChild->se

Cocos2d-x3.3Final(11)TextBMFont常用成员函数(C++)

TextBMFont:来自http://www.cocos2d-x.org/wiki/Widget的释义,In addition to UILabelAtlas, you can also display a label by UILabelBMFont. 显然这两个是都是可以显示一个标签的另外一个控件,和TextAtlas一样,但显然这两个有不同. TextBMFont * textBMFont = TextBMFont::create("BMFont",  "cocosu

Cocos2d-x3.3Final(10)TextAtlas常用成员函数(C++)

TextAtlas  //来自于官网 http://www.cocos2d-x.org/wiki/Widget的释义, UILabelAtlas can display number label that jointed by images: atlas的英文含义是地图集,所以释义可以解释为可以显示由图片组合而成的数字标签的标签地图集. TextAtlas * textAtlas = TextAtlas::create("12780", "cocosui/labelatlas