Cocos2d-x3.3final (7) Focus common member function (c + +)

Source: Internet
Author: User
Tags addchild event listener

The main function has _horizontallayout->setfocused (true);//Set focus to focus on the layout
_horizontallayout->setloopfocus (TRUE);//loop set focus within the layout
_horizontallayout->setfocusenabled ();//Set the focus on the control
_horizontallayout->isfocusenabled ();//Whether it is possible to focus


Here are a few special cases where VBox creates the layout, if Isloopfocus (true), that is, the focus of the control in the layout can be looped in the layout example, then the focus can only be looped in the vertical direction, not horizontally.
Similarly, if the layout created by Hbox is Isloopfocus (true), the focus can only be obtained horizontally, not vertically.
If Isloopfocus (false), no matter whether horizontal or vertical, it is not possible to loop to get the focus.
The scaling of Setscale is relative to the container of its own parent class, so the scale of the sun container relative to Grandpa's container class is the scaling factor of the child container, multiplied by the zoom factor of the sun container.
If the sun container is not scaled, and the child container is scaled by 0.5, it is equivalent to the Sun container scaling by 1, and ultimately relative to the grandfather container's zoom factor is 0.5*1 or 0.5.


Menu _dpadmenu = Menu::create (); Create a Menu
Auto winsize = Director::getinstance ()->getvisiblesize ();Get window Size
Auto Leftitem = Menuitemfont::create ("Left", Cc_callback_0 (uifocustestbase::onleftkeypressed, this));Create a menu item, set menu item to get focus callback function
_dpadmenu->addchild (Leftitem);//Add menu item to menu
Call this method to enable Dpad focus navigation
Widget::enabledpadnavigation (TRUE);
Cocos2d::eventlistenerfocus _eventlistener = Eventlistenerfocus::create ();//new Event listener focus
_eventlistener->onfocuschanged = Cc_callback_2 (uifocustestbase::onfocuschanged, this);//Set the callback function for this focus
_eventdispatcher->addeventlistenerwithfixedpriority (_eventlistener, 1);//Event listener with specific priority added for specific events
menu item Callback function
void Uifocustestbase::onleftkeypressed ()
{
Cocos2d::eventkeyboard::keycode Cocos2dkey = eventkeyboard::keycode::key_dpad_left;//Keyboard Event Value
Cocos2d::eventkeyboard event (Cocos2dkey, FALSE);//construct keyboard events based on this keyboard event value
Cocos2d::D irector::getinstance ()->geteventdispatcher ()->dispatchevent (&event);//Event Scheduler Dispatches this event
}
callback function with focus change
void Uifocustestbase::onfocuschanged (Cocos2d::ui::widget * widgetlostfocus, Cocos2d::ui::widget * widgetGetFocus)
{
if (Widgetgetfocus && widgetgetfocus->isfocusenabled ()) {
Widgetgetfocus->setcolor (color3b::red);Gets the focus set to red
}
if (Widgetlostfocus && widgetlostfocus->isfocusenabled ())
{
Widgetlostfocus->setcolor (color3b::white);//set to white without focus
}
if (Widgetlostfocus && widgetgetfocus)
{
Cclog ();
}
}
Horizontal layout test
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));//Add picture Event callback processing
_horizontallayout->addchild (w);
}
Auto BTN = button::create ("Cocosui/switch-mask.png");
Btn->addtoucheventlistener (Cc_callback_2 (Uifocustesthorizontal::togglefocusloop, this));//Set whether you can cycle
Picture Event Callback Handler function
void uifocustestbase::onimageviewclicked (Cocos2d::ref * Ref, Widget::toucheventtype touchtype)
{
if (Touchtype = = widget::toucheventtype::ended) {
Widget * w = (widget *) ref;
if (w->isfocusenabled ())//if can be focused, click, set to Yellow, and instead of being focused
{
W->setfocusenabled (FALSE);
W->setcolor (Color3b::yellow);
}else{ If it cannot be focused, click, set to White, and instead can be focused
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 ());Click the button, if Isloopfocus is true, then the focus is only in the layout of the internal loop, otherwise you will jump out of layout, if False to show that you want to jump out of layout, you can no longer in the layout of the loop inside.
if (_horizontallayout->isloopfocus ()) {
_looptext->setstring ("loop Enabled");
}else{
_looptext->setstring ("loop disabled");
}
}
}




Vertical Layout Test
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));//Set Picture event callback function
_verticallayout->addchild (w);
if (i = = 2) {
W->requestfocus ();//Request Focus
}
}
Auto BTN = button::create ("Cocosui/switch-mask.png"); Btn->addtoucheventlistener (Cc_callback_2 ( Uifocostestvertical::togglefocusloop, this);//This is the Loopfocus to handle the entire layout, that is, whether the focus is only moving inside the layout


This layout
The theme is a vbox,scale set to 0.5,vbox there is a picture, the anchor point for (0,0), the picture of the ScaleX set to 2.5,
A hbox,,scale is added to 0.8, added to VBox, and two images, anchor points (1,0), ScaleY set to 2.0
Add 2 more pictures in Hbox add Vbox,vbox.



The layout of this picture, the first layer is Hbox,scale set to 0.6, then add two pictures, the image anchor points for (0, 1), ScaleY set to 2.4

Added Vbox,vbox scale to 0.8,vbox add two images, image anchor points (0, 1), ScaleX set to 2.0,

VBox add two images to the new Hbox,hbox


Linearlayoutparameter *bottomparams = Linearlayoutparameter::create ();

Params->setmargin (Margin (0, 0, 50, 0)); //Parameters left, top, right, down order

Firstvbox->setlayoutparameter (params); The//control sets the bounding parameter, which is how far apart the control is from the border of his adjacent control, and there seems to be no fixed size between layout, and if the boundary distance values set are too small, they may overlap.


Cocos2d-x3.3final (7) Focus common member function (c + +)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.