Implementation of cocos2d-x Slider Control

Source: Internet
Author: User

TTSliderControl

The implementation code is as follows:

# Include <iostream> # include "cocos2d. h "# include" cocos-ext.h "USING_NS_CC; USING_NS_CC_EXT; # include" ClientDefine. h "# include" vector "# include" string "using namespace std; typedef struct _ SLIDER_INIT_STRUCT {CCSize m_sliderSize; // control size vector <CCNode *> m_nodes; // rolling node int m_curCenterIndex; // The subscript of the current most intermediate node, starting from 0} SLIDER_INIT_STRUCT; class TTSliderControl: public CCLayer {public: TTSliderControl ();~ TTSliderControl (); // implements cropping void visit (void); CREATE_FUNC (TTSliderControl) virtual bool init (); virtual void onEnter (); virtual void onExit (); virtual bool compute (CCTouch * pTouch, CCEvent * pEvent); virtual void ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent); virtual void ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent ); virtual void ccTouchCancelled (CCTouch * pTouch, CCEvent * pEvent); // determines whether the click is in the valid region bool isTouchInvalidated (CCTouch * touch); void initWithStruct (CCObject * pObj ); // move the node of an index to the middle void moveNodeToCenter (int index); // fine-tune the node to move the offset void moveNodeOffset (float offset ); // determine the int getCurCenterIndexInUI (); public: SLIDER_INIT_STRUCT m_initS; // The index in the UI is the final data int m_curIndex; // This index is the current data public: CC_SYNTHESIZE (SEL_CallFuncO, m_changeIndexSelector, ChangeIndexSelector); CC_SYNTHESIZE (CCNode *, m_target, Target );};

# Include "TTSliderControl. h" TTSliderControl: TTSliderControl () {m_target = NULL; m_changeIndexSelector = NULL; m_curIndex =-1;} TTSliderControl ::~ TTSliderControl () {}// implement cropping void TTSliderControl: visit (void) {glable (GL_SCISSOR_TEST); CCPoint selfPos = this-> getPosition (); CCSize selfContentSize = this-> getContentSize (); CCLOG ("selfPos = % d, % d, selfContentSize = % d, % d", selfPos. x, selfPos. y, selfContentSize. width, selfContentSize. height); float scaleX = CCEGLView: Export dopenglview ()-> getScaleX (); float scaleY = CCEGLView: Export dopenglview () -> GetScaleY (); CCRect viewPortRect = CCEGLView: Export dopenglview ()-> getViewPortRect (); glScissor (selfPos. x * scaleX + viewPortRect. origin. x, selfPos. y * scaleY + viewPortRect. origin. y, selfContentSize. width * scaleX, selfContentSize. height * scaleY); CCNode: visit (); // display the content of the parent class glDisable (GL_SCISSOR_TEST);} bool TTSliderControl: init () {// respond to the touch event CCDirector: sharedDirector ()-> getTouchDispatcher ()-> DdTargetedDelegate (this,-128-1, true); return true;} void TTSliderControl: onEnter () {CCLayer: onEnter ();} void TTSliderControl: onExit () {CCLayer: onExit (); CCDirector: shareddire()-> getTouchDispatcher ()-> removeDelegate (this);} bool TTSliderControl: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent) {if (isTouchInvalidated (pTouch) {return true;} else {return false ;}} void TTSliderControl:: CcTouchMoved (CCTouch * pTouch, CCEvent * pEvent) {CCPoint prePos = CCDirector ctor: shareddire()-> convertToGL (pTouch-> convert (); prePos = convertToNodeSpace (prePos ); CCPoint curPos = CCDirector: sharedDirector ()-> convertToGL (pTouch-> getLocationInView (); curPos = convertToNodeSpace (curPos); CCPoint offsetPos = ccpSub (curPos, prePos ); if (offsetPos. x) {moveNodeOffset (offsetPo S. x) ;}} void TTSliderControl: ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent) {// CCPoint endPos = convertTouchToNodeSpace (pTouch); // determines which node is in the middle, then slide to the node center int curIndexUI = getCurCenterIndexInUI (); if (curIndexUI! =-1) {moveNodeToCenter (curIndexUI) ;}} void TTSliderControl: ccTouchCancelled (CCTouch * pTouch, CCEvent * pEvent) {}// determine whether the click is in the valid region bool TTSliderControl :: isTouchInvalidated (CCTouch * touch) {CCPoint touchLocation = touch-> getLocation (); CCPoint localLocation = this-> convertToNodeSpace (touchLocation); CCRect rc = CCRectMake (0, 0, this-> getContentSize (). width, this-> getContentSize (). height); if (rc. c OntainsPoint (localLocation) {return true;} else {return false ;}} void TTSliderControl: initWithStruct (CCObject * pObj) {if (pObj = NULL) {return ;} m_initS = * (SLIDER_INIT_STRUCT *) pObj; // determines the number of nodes. if it is null, if (m_initS.m_nodes.size () = 0) {return;} is returned directly ;} // make centerIndx within a reasonable range if (m_initS.m_curCenterIndex <0) {m_initS.m_curCenterIndex = 0;} if (m_initS.m_curCenterIndex> = m_initS.m_no Des. size () {m_initS.m_curCenterIndex = m_initS.m_nodes.size ()-1;} // set the widget size this-> setContentSize (m_initS.m_sliderSize ); // Add nodes to this for (int I = 0; I <m_initS.m_nodes.size (); I ++) {CCNode * tempNode = m_initS.m_nodes.at (I); if (tempNode) {this-> addChild (tempNode, 2) ;}// set the location of each child node based on centerIndex moveNodeToCenter (m_initS.m_curCenterIndex);} // move the node of an index to the middle void TTS LiderControl: moveNodeToCenter (int index) {// make centerIndex within a reasonable range if (index <0) {return;} if (index> = m_initS.m_nodes.size ()) {return;} m_initS.m_curCenterIndex = index; // traverse the entire m_nodes CCNode * centerSliderUnit = NULL; for (int I = 0; I <m_initS.m_nodes.size (); I ++) {CCNode * sliderUnit = (CCNode *) m_initS.m_nodes.at (I); // The default node's anchpoint is (0, 0) CCPoint newPoint; CCLOG ("node size = % f, % F ", sliderUnit-> getContentSize (). width, sliderUnit-> getContentSize (). height); newPoint. x = (I-m_initS.m_curCenterIndex) * sliderUnit-> getContentSize (). width; newPoint. y = (this-> getContentSize (). height-sliderUnit-> getContentSize (). height) * 0.5f; CCMoveTo * moveto = CCMoveTo: create (0.25f, newPoint); sliderUnit-> runAction (moveto);} // if the index is changed if (m_curIndex! = Cursor) {if (m_target & m_changeIndexSelector) {(m_target-> * m_changeIndexSelector) (this) ;}} m_curIndex = m_initS.m_curCenterIndex;} // fine-tuned to move node offsetvoid TTSliderControl :: moveNodeOffset (float offset) {// if no node exists, if (m_initS.m_nodes.size () = 0) {return;} // if it has reached the leftmost, you can no longer move it to the left and return CCNode * rightMostUnit = (CCNode *) m_initS.m_nodes.at (m_initS.m_nodes.size ()-1); if (rightMostUnit-> getPosition (). x <= 0 & offset <0) {return;} // if it has reached the rightmost and cannot be moved to the right, the CCNode * leftMostUnit = (CCNode *) is returned directly *) m_initS.m_nodes.at (0); if (leftMostUnit-> getPosition (). x> = 0 & offset> 0) {return;} // traverse. All nodes move int counter = m_initS.m_nodes.size (); for (int I = 0; I <counter; I ++) {CCNode * tempNode = (CCNode *) m_initS.m_nodes.at (I); CCPoint oldPoint = tempNode-> getPosition (); CCPoint newPoint = oldPoint; newPoint. x = oldPoint. x + offset; tempNode-> setPosition (newPoint) ;}// determines the int TTSliderControl: getCurCenterIndexInUI () in the middle of the UI. {// if no node exists, then return-1 if (m_initS.m_nodes.size () = 0) {return-1;} // traverse int counter = m_initS.m_nodes.size (); for (int I = 0; I <counter; I ++) {CCNode * tempNode = m_initS.m_nodes.at (I); CCPoint pos = tempNode-> getPosition (); CCSize size = tempNode-> getContentSize (); if (pos. x-size. width * 0.5f) <= 0 & (pos. x + size. width * 0.5f)> = 0) {return I;} return-1 ;}



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.