[Cocos2dx] Enable UIButton to support disable status

Source: Internet
Author: User

Add a UIButton component in cocostudio. We can see that the following three buttons are usually used: normal, pressed, and disable. however, after we set the disable status, after running the button-> setEnable (false) on a button in our game project, the button is completely lost ?! Solution: Modify the Widget: visit () method and change it to: void Widget: visit () {CCNodeRGBA: visit () ;}, that is, delete the if judgment. modify the Widget: setEnable () method to void Widget: setEnabled (bool enabled) {_ enabled = enabled; if (_ widgetChildren & _ widgetChildren-> count ()> 0) {CCObject * child; CCARRAY_FOREACH (_ widgetChildren, child) {(Widget *) child)-> setEnabled (enabled) ;}} setBright (enabled ); // Add this line}, that is, add setBright (enabled); line. if you want to know why It is an explanation. explain the implementation of the strange rendering Widget: visit (): void Widget: visit () {if (_ enabled) {CCNodeRGBA: visit ();}} if you have no time to carefully read the cocos2dx source code, here is a simple explanation: the visit () method was originally inherited from CCNode. that is to say, any Display object has this method. CCNode: visit () is the rendering function of CCNode. It is the most important function in view rendering. It is similar to the UIView: draw () function in IOS development. the Framework calls this method at each frame, and this method is used to draw the openGL of the current component. let's take a look at the implementation of CCNode: visit () for verification: void CCNode: visit (){... this-> draw ();...} // forget it. This code is a bit long. Some people may not be interested in it for a long time. I put it in the *** appendix. (* ^__ ^ *) when you see this, you must have discovered the problem: Why is rendering only when _ enabled is true? Isn't it a pitfall? Will disable status be ignored? Maybe cocos2dx has another plan. I don't understand it clearly. But since this function does not meet my requirements, I will remove the if. (who makes us a programmer ?). However, the problem is not completely over. Or is the enable of the disable status. Widget not displayed? The UIButton: setEnable () method is relatively short. paste it and see it. void Widget: setEnabled (bool enabled) {_ enabled = enabled; if (_ widgetChildren & _ widgetChildren-> count ()> 0) {CCObject * child; CCARRAY_FOREACH (_ widgetChildren, child) {(Widget *) child)-> setEnabled (enabled );}}} it is found that this Code does nothing except setting the _ enable and all children's _ enable status to false. by viewing the widget code, it is easy to see that the three-state widget is implemented through the following three functions: void Widget: onPressStateChangedToNormal () {} void Widget:: OnPressStateChangedToPressed () {} void Widget: onPressStateChangedToDisabled () {} Yes, they are all empty functions. sub-classes implement these three functions as needed. the following is the implementation of setBright: void Widget: setBright (bool bright) {_ bright = bright; if (_ bright) {_ brightStyle = BRIGHT_NONE; setBrightStyle (BRIGHT_NORMAL );} else {onPressStateChangedToDisabled () ;}} can be seen from the code above. we call setBright (false) to render different images based on the current status of the widget. appendix code snippet 1 CCNode: visit () void CCNode: visit (){/ /Quick return if not visible. children won't be drawn. if (! M_bVisible) {return;} kmGLPushMatrix (); if (m_pGrid & m_pGrid-> isActive () {m_pGrid-> beforeDraw ();} this-> transform (); CCNode * pNode = NULL; unsigned int I = 0; if (m_pChildren & m_pChildren-> count ()> 0) {sortAllChildren (); // draw children zOrder <0 ccArray * arrayData = m_pChildren-> data; for (; I <arrayData-> num; I ++) {pNode = (CCNode *) arrayData-> arr [I]; if (pNode & pNode-> m_nZOrder <0) {pNode-> visit ();} else {break ;}} // self draw this-> draw (); for (; I <arrayData-> num; I ++) {pNode = (CCNode *) arrayData-> arr [I]; if (pNode) {pNode-> visit () ;}} else {this-> draw () ;}// reset for next frame m_uOrderOfArrival = 0; if (m_pGrid & m_pGrid-> isActive () {m_pGrid-> afterDraw (this) ;} kmGLPopMatrix ();}

Related Article

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.