void HServerTimer::update(float dt) { int second = max(0, HGameTime::getTimeInterval(m_toTime)); int day = second / 86400; int hour = (second - 86400*day) / 3600; int minut = (second - 86400*day - 3600*hour) / 60; int sec = (second - 86400*day - 3600*hour - minut * 60); CCLabelProtocol* label = dynamic_cast<CCLabelProtocol*>(getTarget()); const char* tm = ""; switch (m_type) { case ET_Day: { static const char* dayformat = ccLocalizedString("HCOM_TIMER_DAY"); tm = _T(dayformat, day, hour, minut, sec); } break; case ET_Hour: { tm = _T("%02d:%02d:%02d", day*24+hour, minut, sec); } break; default: break; } label->setString(_T(m_format.c_str(), tm)); if (second == 0 && !m_done) { m_done = true; if (m_callback) { m_callback->execute(); CC_SAFE_RELEASE_NULL(m_callback); } } }
bool CCControlButton::initWithLabelAndBackgroundSprite(CCNode* node, CCScale9Sprite* backgroundSprite) { if (CCControl::init()) { CCAssert(node != NULL, "Label must not be nil."); CCLabelProtocol* label = dynamic_cast<CCLabelProtocol*>(node); CCRGBAProtocol* rgbaLabel = dynamic_cast<CCRGBAProtocol*>(node); CCAssert(backgroundSprite != NULL, "Background sprite must not be nil."); CCAssert(label != NULL || rgbaLabel!=NULL || backgroundSprite != NULL, ""); m_bParentInited = true; // Initialize the button state tables this->setTitleDispatchTable(CCDictionary::create()); this->setTitleColorDispatchTable(CCDictionary::create()); this->setTitleLabelDispatchTable(CCDictionary::create()); this->setBackgroundSpriteDispatchTable(CCDictionary::create()); setTouchEnabled(true); m_isPushed = false; m_zoomOnTouchDown = true; m_currentTitle=NULL; // Adjust the background image by default setAdjustBackgroundImage(true); setPreferredSize(CCSizeZero); // Zooming button by default m_zoomOnTouchDown = true; // Set the default anchor point ignoreAnchorPointForPosition(false); setAnchorPoint(ccp(0.5f, 0.5f)); // Set the nodes setTitleLabel(node); setBackgroundSprite(backgroundSprite); // Set the default color and opacity setColor(ccc3(255.0f, 255.0f, 255.0f)); setOpacity(255.0f); setOpacityModifyRGB(true); // Initialize the dispatch table CCString* tempString = CCString::create(label->getString()); //tempString->autorelease(); setTitleForState(tempString, CCControlStateNormal); setTitleColorForState(rgbaLabel->getColor(), CCControlStateNormal); setTitleLabelForState(node, CCControlStateNormal); setBackgroundSpriteForState(backgroundSprite, CCControlStateNormal); setLabelAnchorPoint(ccp(0.5f, 0.5f)); // Layout update needsLayout(); return true; } //couldn't init the CCControl else { return false; } }
void CCControlButton::needsLayout() { if (!m_bParentInited) { return; } // Hide the background and the label if (m_titleLabel != NULL) { m_titleLabel->setIsVisible(false); } if (m_backgroundSprite) { m_backgroundSprite->setIsVisible(false); } // Update anchor of all labels this->setLabelAnchorPoint(this->m_labelAnchorPoint); // Update the label to match with the current state CC_SAFE_RELEASE(m_currentTitle); m_currentTitle = getTitleForState(m_eState); CC_SAFE_RETAIN(m_currentTitle); m_currentTitleColor=getTitleColorForState(m_eState); this->setTitleLabel(getTitleLabelForState(m_eState)); CCLabelProtocol* label = dynamic_cast<CCLabelProtocol*>(m_titleLabel); if (label && m_currentTitle) { label->setString(m_currentTitle->toStdString().c_str()); } CCRGBAProtocol* rgbaLabel = dynamic_cast<CCRGBAProtocol*>(m_titleLabel); if (rgbaLabel) { rgbaLabel->setColor(m_currentTitleColor); } if (m_titleLabel != NULL) { m_titleLabel->setPosition(ccp (getContentSize().width / 2, getContentSize().height / 2)); } // Update the background sprite this->setBackgroundSprite(this->getBackgroundSpriteForState(m_eState)); if (m_backgroundSprite != NULL) { m_backgroundSprite->setPosition(ccp (getContentSize().width / 2, getContentSize().height / 2)); } // Get the title label size CCSize titleLabelSize; if (m_titleLabel != NULL) { titleLabelSize = m_titleLabel->boundingBox().size; } // Adjust the background image if necessary if (m_doesAdjustBackgroundImage) { // Add the margins if (m_backgroundSprite != NULL) { m_backgroundSprite->setContentSize(CCSizeMake(titleLabelSize.width + m_marginH * 2, titleLabelSize.height + m_marginV * 2)); } } else { //TODO: should this also have margins if one of the preferred sizes is relaxed? if (m_backgroundSprite != NULL) { CCSize preferredSize = m_backgroundSprite->getPreferredSize(); if (preferredSize.width <= 0) { preferredSize.width = titleLabelSize.width; } if (preferredSize.height <= 0) { preferredSize.height = titleLabelSize.height; } m_backgroundSprite->setContentSize(preferredSize); } } // Set the content size CCRect rectTitle; if (m_titleLabel != NULL) { rectTitle = m_titleLabel->boundingBox(); } CCRect rectBackground; if (m_backgroundSprite != NULL) { rectBackground = m_backgroundSprite->boundingBox(); } CCRect maxRect = CCControlUtils::CCRectUnion(rectTitle, rectBackground); setContentSize(CCSizeMake(maxRect.size.width, maxRect.size.height)); if (m_titleLabel != NULL) { m_titleLabel->setPosition(ccp(getContentSize().width/2, getContentSize().height/2)); // Make visible the background and the label m_titleLabel->setIsVisible(true); } if (m_backgroundSprite != NULL) { m_backgroundSprite->setPosition(ccp(getContentSize().width/2, getContentSize().height/2)); m_backgroundSprite->setIsVisible(true); } }