mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
Withespaces does matter.
This commit is contained in:
@@ -4,17 +4,17 @@
|
||||
*
|
||||
* Copyright 2011 Yannick Loriot.
|
||||
* http://yannickloriot.com
|
||||
*
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
@@ -80,7 +80,7 @@ bool ControlButton::initWithLabelAndBackgroundSprite(Node* node, ui::Scale9Sprit
|
||||
if (backgroundSprite == nullptr || label == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
_parentInited = true;
|
||||
|
||||
_isPushed = false;
|
||||
@@ -88,15 +88,15 @@ bool ControlButton::initWithLabelAndBackgroundSprite(Node* node, ui::Scale9Sprit
|
||||
setPreferredSize(Size::ZERO);
|
||||
// Adjust the background image by adjustBackGroundSize
|
||||
setAdjustBackgroundImage(adjustBackGroundSize);
|
||||
|
||||
|
||||
// Zooming button by default
|
||||
_zoomOnTouchDown = true;
|
||||
_scaleRatio = 1.1f;
|
||||
|
||||
|
||||
// Set the default anchor point
|
||||
ignoreAnchorPointForPosition(false);
|
||||
setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
|
||||
|
||||
// Set the nodes
|
||||
setTitleLabel(node);
|
||||
setBackgroundSprite(backgroundSprite);
|
||||
@@ -105,14 +105,14 @@ bool ControlButton::initWithLabelAndBackgroundSprite(Node* node, ui::Scale9Sprit
|
||||
setColor(Color3B::WHITE);
|
||||
setOpacity(255.0f);
|
||||
setOpacityModifyRGB(true);
|
||||
|
||||
|
||||
// Initialize the dispatch table
|
||||
|
||||
|
||||
setTitleForState(label->getString(), Control::State::NORMAL);
|
||||
setTitleColorForState(node->getColor(), Control::State::NORMAL);
|
||||
setTitleLabelForState(node, Control::State::NORMAL);
|
||||
setBackgroundSpriteForState(backgroundSprite, Control::State::NORMAL);
|
||||
|
||||
|
||||
setLabelAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
||||
|
||||
// Layout update
|
||||
@@ -200,13 +200,13 @@ void ControlButton::setHighlighted(bool enabled)
|
||||
{
|
||||
_state = Control::State::NORMAL;
|
||||
}
|
||||
|
||||
|
||||
Control::setHighlighted(enabled);
|
||||
|
||||
Action *action = getActionByTag(kZoomActionTag);
|
||||
if (action)
|
||||
{
|
||||
stopAction(action);
|
||||
stopAction(action);
|
||||
}
|
||||
needsLayout();
|
||||
if( _zoomOnTouchDown )
|
||||
@@ -237,7 +237,7 @@ void ControlButton::setPreferredSize(const Size& size)
|
||||
else
|
||||
{
|
||||
_doesAdjustBackgroundImage = false;
|
||||
|
||||
|
||||
for (auto iter = _backgroundSpriteDispatchTable.begin(); iter != _backgroundSpriteDispatchTable.end(); ++iter)
|
||||
{
|
||||
iter->second->setPreferredSize(size);
|
||||
@@ -285,9 +285,9 @@ std::string ControlButton::getTitleForState(State state)
|
||||
{
|
||||
return iter->second;
|
||||
}
|
||||
|
||||
|
||||
iter = _titleDispatchTable.find((int)Control::State::NORMAL);
|
||||
|
||||
|
||||
return iter != _titleDispatchTable.end() ? iter->second : "";
|
||||
}
|
||||
|
||||
@@ -299,7 +299,7 @@ void ControlButton::setTitleForState(const std::string& title, State state)
|
||||
{
|
||||
_titleDispatchTable[(int)state] = title;
|
||||
}
|
||||
|
||||
|
||||
// If the current state if equal to the given state we update the layout
|
||||
if (getState() == state)
|
||||
{
|
||||
@@ -333,7 +333,7 @@ void ControlButton::setTitleColorForState(const Color3B& color, State state)
|
||||
{
|
||||
_titleColorDispatchTable.erase((int)state);
|
||||
_titleColorDispatchTable[(int)state] = color;
|
||||
|
||||
|
||||
// If the current state if equal to the given state we update the layout
|
||||
if (getState() == state)
|
||||
{
|
||||
@@ -471,7 +471,7 @@ void ControlButton::setBackgroundSpriteForState(ui::Scale9Sprite* sprite, State
|
||||
// Force update of preferred size
|
||||
sprite->setPreferredSize(Size(oldPreferredSize.width+1, oldPreferredSize.height+1));
|
||||
}
|
||||
|
||||
|
||||
sprite->setPreferredSize(this->_preferredSize);
|
||||
}
|
||||
|
||||
@@ -503,7 +503,7 @@ void ControlButton::needsLayout()
|
||||
}
|
||||
// Update anchor of all labels
|
||||
this->setLabelAnchorPoint(this->_labelAnchorPoint);
|
||||
|
||||
|
||||
// Update the label to match with the current state
|
||||
_currentTitle = getTitleForState(_state);
|
||||
|
||||
@@ -525,21 +525,21 @@ void ControlButton::needsLayout()
|
||||
{
|
||||
_titleLabel->setPosition(getContentSize().width / 2, getContentSize().height / 2);
|
||||
}
|
||||
|
||||
|
||||
// Update the background sprite
|
||||
this->setBackgroundSprite(this->getBackgroundSpriteForState(_state));
|
||||
if (_backgroundSprite != nullptr)
|
||||
{
|
||||
_backgroundSprite->setPosition(getContentSize().width / 2, getContentSize().height / 2);
|
||||
}
|
||||
|
||||
|
||||
// Get the title label size
|
||||
Size titleLabelSize;
|
||||
if (_titleLabel != nullptr)
|
||||
{
|
||||
titleLabelSize = _titleLabel->getBoundingBox().size;
|
||||
}
|
||||
|
||||
|
||||
// Adjust the background image if necessary
|
||||
if (_doesAdjustBackgroundImage)
|
||||
{
|
||||
@@ -548,9 +548,9 @@ void ControlButton::needsLayout()
|
||||
{
|
||||
_backgroundSprite->setContentSize(Size(titleLabelSize.width + _marginH * 2, titleLabelSize.height + _marginV * 2));
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
{
|
||||
//TODO: should this also have margins if one of the preferred sizes is relaxed?
|
||||
if (_backgroundSprite != nullptr)
|
||||
{
|
||||
@@ -567,7 +567,7 @@ void ControlButton::needsLayout()
|
||||
_backgroundSprite->setContentSize(preferredSize);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Set the content size
|
||||
Rect rectTitle;
|
||||
if (_titleLabel != nullptr)
|
||||
@@ -581,20 +581,20 @@ void ControlButton::needsLayout()
|
||||
}
|
||||
|
||||
Rect maxRect = ControlUtils::RectUnion(rectTitle, rectBackground);
|
||||
setContentSize(Size(maxRect.size.width, maxRect.size.height));
|
||||
|
||||
setContentSize(Size(maxRect.size.width, maxRect.size.height));
|
||||
|
||||
if (_titleLabel != nullptr)
|
||||
{
|
||||
_titleLabel->setPosition(getContentSize().width/2, getContentSize().height/2);
|
||||
// Make visible the background and the label
|
||||
_titleLabel->setVisible(true);
|
||||
}
|
||||
|
||||
|
||||
if (_backgroundSprite != nullptr)
|
||||
{
|
||||
_backgroundSprite->setPosition(getContentSize().width/2, getContentSize().height/2);
|
||||
_backgroundSprite->setVisible(true);
|
||||
}
|
||||
_backgroundSprite->setVisible(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -605,7 +605,7 @@ bool ControlButton::onTouchBegan(Touch *pTouch, Event *pEvent)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
for (Node *c = this->_parent; c != nullptr; c = c->getParent())
|
||||
{
|
||||
if (c->isVisible() == false)
|
||||
@@ -613,7 +613,7 @@ bool ControlButton::onTouchBegan(Touch *pTouch, Event *pEvent)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_isPushed = true;
|
||||
this->setHighlighted(true);
|
||||
sendActionsForControlEvents(Control::EventType::TOUCH_DOWN);
|
||||
@@ -621,7 +621,7 @@ bool ControlButton::onTouchBegan(Touch *pTouch, Event *pEvent)
|
||||
}
|
||||
|
||||
void ControlButton::onTouchMoved(Touch *pTouch, Event *pEvent)
|
||||
{
|
||||
{
|
||||
if (!isEnabled() || !isPushed() || isSelected())
|
||||
{
|
||||
if (isHighlighted())
|
||||
@@ -630,7 +630,7 @@ void ControlButton::onTouchMoved(Touch *pTouch, Event *pEvent)
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
bool isTouchMoveInside = isTouchInside(pTouch);
|
||||
if (isTouchMoveInside && !isHighlighted())
|
||||
{
|
||||
@@ -644,34 +644,34 @@ void ControlButton::onTouchMoved(Touch *pTouch, Event *pEvent)
|
||||
else if (!isTouchMoveInside && isHighlighted())
|
||||
{
|
||||
setHighlighted(false);
|
||||
|
||||
sendActionsForControlEvents(Control::EventType::DRAG_EXIT);
|
||||
|
||||
sendActionsForControlEvents(Control::EventType::DRAG_EXIT);
|
||||
}
|
||||
else if (!isTouchMoveInside && !isHighlighted())
|
||||
{
|
||||
sendActionsForControlEvents(Control::EventType::DRAG_OUTSIDE);
|
||||
sendActionsForControlEvents(Control::EventType::DRAG_OUTSIDE);
|
||||
}
|
||||
}
|
||||
void ControlButton::onTouchEnded(Touch *pTouch, Event *pEvent)
|
||||
{
|
||||
_isPushed = false;
|
||||
setHighlighted(false);
|
||||
|
||||
|
||||
|
||||
|
||||
if (isTouchInside(pTouch))
|
||||
{
|
||||
sendActionsForControlEvents(Control::EventType::TOUCH_UP_INSIDE);
|
||||
sendActionsForControlEvents(Control::EventType::TOUCH_UP_INSIDE);
|
||||
}
|
||||
else
|
||||
{
|
||||
sendActionsForControlEvents(Control::EventType::TOUCH_UP_OUTSIDE);
|
||||
sendActionsForControlEvents(Control::EventType::TOUCH_UP_OUTSIDE);
|
||||
}
|
||||
}
|
||||
|
||||
void ControlButton::setOpacity(GLubyte opacity)
|
||||
{
|
||||
Control::setOpacity(opacity);
|
||||
|
||||
|
||||
for (auto iter = _backgroundSpriteDispatchTable.begin(); iter != _backgroundSpriteDispatchTable.end(); ++iter)
|
||||
{
|
||||
iter->second->setOpacity(opacity);
|
||||
@@ -701,7 +701,7 @@ void ControlButton::updateDisplayedOpacity(GLubyte parentOpacity)
|
||||
void ControlButton::setColor(const Color3B & color)
|
||||
{
|
||||
Control::setColor(color);
|
||||
|
||||
|
||||
for (auto iter = _backgroundSpriteDispatchTable.begin(); iter != _backgroundSpriteDispatchTable.end(); ++iter)
|
||||
{
|
||||
iter->second->setColor(color);
|
||||
@@ -748,3 +748,4 @@ ControlButton* ControlButton::create()
|
||||
}
|
||||
|
||||
NS_CC_EXT_END
|
||||
|
||||
|
||||
Reference in New Issue
Block a user