issue #1483: Synchronizing CCControlExtension to latest version.

This commit is contained in:
James Chen 2012-09-25 16:57:51 +08:00
parent 8937a3f9cc
commit f7229f74d8
41 changed files with 2299 additions and 1252 deletions

View File

@ -144,13 +144,13 @@ OBJECTS = ../actions/CCAction.o \
../../extensions/CCBReader/CCLayerGradientLoader.o \ ../../extensions/CCBReader/CCLayerGradientLoader.o \
../../extensions/CCBReader/CCSpriteLoader.o \ ../../extensions/CCBReader/CCSpriteLoader.o \
../../extensions/CCBReader/CCLayerLoader.o \ ../../extensions/CCBReader/CCLayerLoader.o \
../../extensions/CCBReader/CCBAnimationManager.o \ ../../extensions/CCBReader/CCBAnimationManager.o \
../../extensions/CCBReader/CCBKeyframe.o \ ../../extensions/CCBReader/CCBKeyframe.o \
../../extensions/CCBReader/CCBSequence.o \ ../../extensions/CCBReader/CCBSequence.o \
../../extensions/CCBReader/CCBSequenceProperty.o \ ../../extensions/CCBReader/CCBSequenceProperty.o \
../../extensions/CCBReader/CCBValue.o \ ../../extensions/CCBReader/CCBValue.o \
../../extensions/CCBReader/CCData.o \ ../../extensions/CCBReader/CCData.o \
../../extensions/CCBReader/CCNode+CCBRelativePositioning.o \ ../../extensions/CCBReader/CCNode+CCBRelativePositioning.o \
../../extensions/GUI/CCScrollView/CCScrollView.o \ ../../extensions/GUI/CCScrollView/CCScrollView.o \
../../extensions/GUI/CCScrollView/CCSorting.o \ ../../extensions/GUI/CCScrollView/CCSorting.o \
../../extensions/GUI/CCScrollView/CCTableView.o \ ../../extensions/GUI/CCScrollView/CCTableView.o \
@ -164,9 +164,9 @@ OBJECTS = ../actions/CCAction.o \
../../extensions/GUI/CCControlExtension/CCControlSwitch.o \ ../../extensions/GUI/CCControlExtension/CCControlSwitch.o \
../../extensions/GUI/CCControlExtension/CCControlUtils.o \ ../../extensions/GUI/CCControlExtension/CCControlUtils.o \
../../extensions/GUI/CCControlExtension/CCInvocation.o \ ../../extensions/GUI/CCControlExtension/CCInvocation.o \
../../extensions/GUI/CCControlExtension/CCMenuPassive.o \
../../extensions/GUI/CCControlExtension/CCScale9Sprite.o \ ../../extensions/GUI/CCControlExtension/CCScale9Sprite.o \
../../extensions/GUI/CCControlExtension/CCSpacer.o \ ../../extensions/GUI/CCControlExtension/CCControlPotentiometer.cpp \
../../extensions/GUI/CCControlExtension/CCControlStepper.cpp \
../../extensions/network/HttpClient.o \ ../../extensions/network/HttpClient.o \
../kazmath/src/aabb.o \ ../kazmath/src/aabb.o \
../kazmath/src/plane.o \ ../kazmath/src/plane.o \

View File

@ -38,9 +38,9 @@ GUI/CCControlExtension/CCControlSlider.cpp \
GUI/CCControlExtension/CCControlSwitch.cpp \ GUI/CCControlExtension/CCControlSwitch.cpp \
GUI/CCControlExtension/CCControlUtils.cpp \ GUI/CCControlExtension/CCControlUtils.cpp \
GUI/CCControlExtension/CCInvocation.cpp \ GUI/CCControlExtension/CCInvocation.cpp \
GUI/CCControlExtension/CCMenuPassive.cpp \
GUI/CCControlExtension/CCScale9Sprite.cpp \ GUI/CCControlExtension/CCScale9Sprite.cpp \
GUI/CCControlExtension/CCSpacer.cpp \ GUI/CCControlExtension/CCControlPotentiometer.cpp \
GUI/CCControlExtension/CCControlStepper.cpp \
GUI/CCScrollView/CCScrollView.cpp \ GUI/CCScrollView/CCScrollView.cpp \
GUI/CCScrollView/CCTableView.cpp \ GUI/CCScrollView/CCTableView.cpp \
GUI/CCScrollView/CCTableViewCell.cpp \ GUI/CCScrollView/CCTableViewCell.cpp \

View File

@ -35,6 +35,16 @@
NS_CC_EXT_BEGIN NS_CC_EXT_BEGIN
CCControl::CCControl() CCControl::CCControl()
: m_cOpacity(0)
, m_tColor(ccBLACK)
, m_bIsOpacityModifyRGB(false)
, m_nDefaultTouchPriority(0)
, m_eState(CCControlStateNormal)
, m_hasVisibleParents(false)
, m_bEnabled(false)
, m_bSelected(false)
, m_bHighlighted(false)
, m_pDispatchTable(NULL)
{ {
} }
@ -46,76 +56,75 @@ bool CCControl::init()
//this->setTouchEnabled(true); //this->setTouchEnabled(true);
//m_bIsTouchEnabled=true; //m_bIsTouchEnabled=true;
// Initialise instance variables // Initialise instance variables
m_nState=CCControlStateNormal; m_eState=CCControlStateNormal;
m_bEnabled=true; setEnabled(true);
m_bSelected=false; setSelected(false);
m_bHighlighted=false; setHighlighted(false);
// Set the touch dispatcher priority by default to 1 // Set the touch dispatcher priority by default to 1
m_nDefaultTouchPriority = 1; setDefaultTouchPriority(1);
this->setDefaultTouchPriority(m_nDefaultTouchPriority); this->setDefaultTouchPriority(m_nDefaultTouchPriority);
// Initialise the tables // Initialise the tables
dispatchTable=new CCDictionary(); m_pDispatchTable = new CCDictionary();
//dispatchTable->autorelease();
// dispatchTable_ = [[NSMutableDictionary alloc] initWithCapacity:1];
return true; return true;
} }
else else
{
return false; return false;
}
} }
CCControl::~CCControl() CCControl::~CCControl()
{ {
CC_SAFE_RELEASE(dispatchTable); CC_SAFE_RELEASE(m_pDispatchTable);
} }
//Menu - Events //Menu - Events
void CCControl::registerWithTouchDispatcher() void CCControl::registerWithTouchDispatcher()
{ {
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority, true); CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, m_nDefaultTouchPriority, true);
} }
void CCControl::onEnter() void CCControl::onEnter()
{ {
//CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, m_nDefaultTouchPriority, true);
CCLayer::onEnter(); CCLayer::onEnter();
} }
void CCControl::onExit() void CCControl::onExit()
{ {
//CCTouchDispatcher::sharedDispatcher()->removeDelegate(this);
CCLayer::onExit(); CCLayer::onExit();
} }
void CCControl::sendActionsForControlEvents(CCControlEvent controlEvents) void CCControl::sendActionsForControlEvents(CCControlEvent controlEvents)
{ {
// For each control events // For each control events
for (int i = 0; i < CONTROL_EVENT_TOTAL_NUMBER; i++) for (int i = 0; i < kControlEventTotalNumber; i++)
{ {
// If the given controlEvents bitmask contains the curent event // If the given controlEvents bitmask contains the curent event
if ((controlEvents & (1 << i))) if ((controlEvents & (1 << i)))
{ {
// Call invocations // Call invocations
// <CCInvocation*> // <CCInvocation*>
CCArray* invocationList=CCControl::dispatchListforControlEvent(1<<i); CCArray* invocationList = this->dispatchListforControlEvent(1<<i);
CCObject* pObj = NULL; CCObject* pObj = NULL;
CCARRAY_FOREACH(invocationList, pObj) CCARRAY_FOREACH(invocationList, pObj)
{ {
CCInvocation* invocation = (CCInvocation*)pObj; CCInvocation* invocation = (CCInvocation*)pObj;
invocation->invoke(this); invocation->invoke(this);
} }
} }
} }
} }
void CCControl::addTargetWithActionForControlEvents(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvents) void CCControl::addTargetWithActionForControlEvents(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvents)
{ {
// For each control events // For each control events
for (int i = 0; i < CONTROL_EVENT_TOTAL_NUMBER; i++) for (int i = 0; i < kControlEventTotalNumber; i++)
{ {
// If the given controlEvents bitmask contains the curent event // If the given controlEvents bitmask contains the curent event
if ((controlEvents & (1 << i))) if ((controlEvents & (1 << i)))
{ {
CCControl::addTargetWithActionForControlEvent(target, action, 1<<i); this->addTargetWithActionForControlEvent(target, action, 1<<i);
} }
} }
} }
@ -129,7 +138,7 @@ void CCControl::addTargetWithActionForControlEvents(CCObject* target, SEL_CCCont
* parameters, in that order. * parameters, in that order.
* When you call this method, target is not retained. * When you call this method, target is not retained.
* *
* @param target The target objectthat is, the object to which the action * @param target The target object that is, the object to which the action
* message is sent. It cannot be nil. The target is not retained. * message is sent. It cannot be nil. The target is not retained.
* @param action A selector identifying an action message. It cannot be NULL. * @param action A selector identifying an action message. It cannot be NULL.
* @param controlEvent A control event for which the action message is sent. * @param controlEvent A control event for which the action message is sent.
@ -138,43 +147,31 @@ void CCControl::addTargetWithActionForControlEvents(CCObject* target, SEL_CCCont
void CCControl::addTargetWithActionForControlEvent(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvent) void CCControl::addTargetWithActionForControlEvent(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvent)
{ {
// Create the invocation object // Create the invocation object
CCInvocation *invocation=new CCInvocation(target, action, controlEvent); CCInvocation *invocation = CCInvocation::create(target, action, controlEvent);
invocation->autorelease();
// Add the invocation into the dispatch list for the given control event // Add the invocation into the dispatch list for the given control event
CCArray* eventInvocationList = dispatchListforControlEvent(controlEvent); CCArray* eventInvocationList = this->dispatchListforControlEvent(controlEvent);
eventInvocationList->addObject(invocation); eventInvocationList->addObject(invocation);
} }
void CCControl::removeTargetWithActionForControlEvents(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvents) void CCControl::removeTargetWithActionForControlEvents(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvents)
{ {
// For each control events // For each control events
for (int i = 0; i < CONTROL_EVENT_TOTAL_NUMBER; i++) for (int i = 0; i < kControlEventTotalNumber; i++)
{ {
// If the given controlEvents bitmask contains the curent event // If the given controlEvents bitmask contains the curent event
if ((controlEvents & (1 << i))) if ((controlEvents & (1 << i)))
{ {
removeTargetWithActionForControlEvent(target, action, 1 << i); this->removeTargetWithActionForControlEvent(target, action, 1 << i);
} }
} }
} }
/**
* Removes a target and action for a particular event from an internal dispatch
* table.
*
* @param target The target objectthat is, the object to which the action
* message is sent. Pass nil to remove all targets paired with action and the
* specified control events.
* @param action A selector identifying an action message. Pass NULL to remove
* all action messages paired with target.
* @param controlEvent A control event for which the action message is sent.
* See "CCControlEvent" for constants.
*/
void CCControl::removeTargetWithActionForControlEvent(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvent) void CCControl::removeTargetWithActionForControlEvent(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvent)
{ {
// Retrieve all invocations for the given control event // Retrieve all invocations for the given control event
//<CCInvocation*> //<CCInvocation*>
CCArray *eventInvocationList=dispatchListforControlEvent(controlEvent); CCArray *eventInvocationList = this->dispatchListforControlEvent(controlEvent);
//remove all invocations if the target and action are null //remove all invocations if the target and action are null
//TODO: should the invocations be deleted, or just removed from the array? Won't that cause issues if you add a single invocation for multiple events? //TODO: should the invocations be deleted, or just removed from the array? Won't that cause issues if you add a single invocation for multiple events?
@ -192,13 +189,19 @@ void CCControl::removeTargetWithActionForControlEvent(CCObject* target, SEL_CCCo
{ {
CCInvocation *invocation = (CCInvocation*)pObj; CCInvocation *invocation = (CCInvocation*)pObj;
bool shouldBeRemoved=true; bool shouldBeRemoved=true;
if (target) if (target)
shouldBeRemoved=(target==invocation->getTarget()); {
shouldBeRemoved=(target==invocation->getTarget());
}
if (action) if (action)
{
shouldBeRemoved=(shouldBeRemoved && (action==invocation->getAction())); shouldBeRemoved=(shouldBeRemoved && (action==invocation->getAction()));
}
// Remove the corresponding invocation object // Remove the corresponding invocation object
if (shouldBeRemoved) if (shouldBeRemoved)
{
eventInvocationList->removeObject(invocation, bDeleteObjects); eventInvocationList->removeObject(invocation, bDeleteObjects);
}
} }
} }
} }
@ -249,17 +252,17 @@ GLubyte CCControl::getOpacity()
} }
void CCControl::setOpacityModifyRGB(bool opacityModifyRGB) void CCControl::setOpacityModifyRGB(bool bOpacityModifyRGB)
{ {
m_bIsOpacityModifyRGB=opacityModifyRGB; m_bIsOpacityModifyRGB=bOpacityModifyRGB;
CCObject* child; CCObject* child;
CCArray* children=getChildren(); CCArray* children=getChildren();
CCARRAY_FOREACH(children, child) CCARRAY_FOREACH(children, child)
{ {
CCRGBAProtocol* pNode = dynamic_cast<CCRGBAProtocol*>(child); CCRGBAProtocol* pNode = dynamic_cast<CCRGBAProtocol*>(child);
if (pNode) if (pNode)
{ {
pNode->setOpacityModifyRGB(opacityModifyRGB); pNode->setOpacityModifyRGB(bOpacityModifyRGB);
} }
} }
} }
@ -272,35 +275,47 @@ bool CCControl::isOpacityModifyRGB()
CCPoint CCControl::getTouchLocation(CCTouch* touch) CCPoint CCControl::getTouchLocation(CCTouch* touch)
{ {
CCPoint touchLocation = touch->getLocation();; // Get the touch position CCPoint touchLocation = touch->getLocation(); // Get the touch position
touchLocation = this->getParent()->convertToNodeSpace(touchLocation); // Convert to the node space of this class touchLocation = this->convertToNodeSpace(touchLocation); // Convert to the node space of this class
return touchLocation; return touchLocation;
} }
bool CCControl::isTouchInside(CCTouch* touch) bool CCControl::isTouchInside(CCTouch* touch)
{ {
CCPoint touchLocation=getTouchLocation(touch); CCPoint touchLocation = touch->getLocation(); // Get the touch position
touchLocation = this->getParent()->convertToNodeSpace(touchLocation);
CCRect bBox=boundingBox(); CCRect bBox=boundingBox();
return bBox.containsPoint(touchLocation); return bBox.containsPoint(touchLocation);
} }
CCArray* CCControl::dispatchListforControlEvent(CCControlEvent controlEvent) CCArray* CCControl::dispatchListforControlEvent(CCControlEvent controlEvent)
{ {
CCArray* invocationList = (CCArray*)dispatchTable->objectForKey(controlEvent); CCArray* invocationList = (CCArray*)m_pDispatchTable->objectForKey(controlEvent);
// If the invocation list does not exist for the dispatch table, we create it // If the invocation list does not exist for the dispatch table, we create it
if (invocationList == NULL) if (invocationList == NULL)
{ {
invocationList = CCArray::createWithCapacity(1); invocationList = CCArray::createWithCapacity(1);
dispatchTable->setObject(invocationList, controlEvent); m_pDispatchTable->setObject(invocationList, controlEvent);
} }
return invocationList; return invocationList;
} }
void CCControl::needsLayout()
{
}
void CCControl::setEnabled(bool bEnabled) void CCControl::setEnabled(bool bEnabled)
{ {
m_bEnabled = bEnabled; m_bEnabled = bEnabled;
if(m_bEnabled) {
m_eState = CCControlStateNormal;
} else {
m_eState = CCControlStateDisabled;
}
this->needsLayout();
} }
bool CCControl::isEnabled() bool CCControl::isEnabled()
@ -311,6 +326,7 @@ bool CCControl::isEnabled()
void CCControl::setSelected(bool bSelected) void CCControl::setSelected(bool bSelected)
{ {
m_bSelected = bSelected; m_bSelected = bSelected;
this->needsLayout();
} }
bool CCControl::isSelected() bool CCControl::isSelected()
@ -321,6 +337,7 @@ bool CCControl::isSelected()
void CCControl::setHighlighted(bool bHighlighted) void CCControl::setHighlighted(bool bHighlighted)
{ {
m_bHighlighted = bHighlighted; m_bHighlighted = bHighlighted;
this->needsLayout();
} }
bool CCControl::isHighlighted() bool CCControl::isHighlighted()
@ -328,4 +345,17 @@ bool CCControl::isHighlighted()
return m_bHighlighted; return m_bHighlighted;
} }
bool CCControl::hasVisibleParents()
{
CCNode* pParent = this->getParent();
for( CCNode *c = pParent; c != NULL; c = c->getParent() )
{
if( !c->isVisible() )
{
return false;
}
}
return true;
}
NS_CC_EXT_END NS_CC_EXT_END

View File

@ -31,7 +31,7 @@
#include "CCInvocation.h" #include "CCInvocation.h"
#include "CCControlUtils.h" #include "CCControlUtils.h"
#include "layers_scenes_transitions_nodes/CCLayer.h" #include "cocos2d.h"
NS_CC_EXT_BEGIN NS_CC_EXT_BEGIN
@ -45,7 +45,7 @@ class CCInvocation;
*/ */
/** Number of kinds of control event. */ /** Number of kinds of control event. */
#define CONTROL_EVENT_TOTAL_NUMBER 9 #define kControlEventTotalNumber 9
/** Kinds of possible events for the control objects. */ /** Kinds of possible events for the control objects. */
enum enum
@ -65,25 +65,24 @@ typedef unsigned int CCControlEvent;
/** The possible state for a control. */ /** The possible state for a control. */
enum enum
{ {
CCControlStateNormal = 1 << 0, // The normal, or default state of a controlthat is, enabled but neither selected nor highlighted. CCControlStateNormal = 1 << 0, // The normal, or default state of a control¡ªthat is, enabled but neither selected nor highlighted.
CCControlStateHighlighted = 1 << 1, // Highlighted state of a control. A control enters this state when a touch down, drag inside or drag enter is performed. You can retrieve and set this value through the highlighted property. CCControlStateHighlighted = 1 << 1, // Highlighted state of a control. A control enters this state when a touch down, drag inside or drag enter is performed. You can retrieve and set this value through the highlighted property.
CCControlStateDisabled = 1 << 2, // Disabled state of a control. This state indicates that the control is currently disabled. You can retrieve and set this value through the enabled property. CCControlStateDisabled = 1 << 2, // Disabled state of a control. This state indicates that the control is currently disabled. You can retrieve and set this value through the enabled property.
CCControlStateSelected = 1 << 3, // Selected state of a control. This state indicates that the control is currently selected. You can retrieve and set this value through the selected property. CCControlStateSelected = 1 << 3 // Selected state of a control. This state indicates that the control is currently selected. You can retrieve and set this value through the selected property.
CCControlStateInitial = 1 << 3
}; };
typedef unsigned int CCControlState; typedef unsigned int CCControlState;
/* /*
* @class * @class
* CCControl is inspired by the UIControl API class from the UIKit library of * CCControl is inspired by the UIControl API class from the UIKit library of
* CocoaTouch. It provides a base class for control CCSprites such as CCButton * CocoaTouch. It provides a base class for control CCSprites such as CCButton
* or CCSlider that convey user intent to the application. * or CCSlider that convey user intent to the application.
* *
* The goal of CCControl is to define an interface and base implementation for * The goal of CCControl is to define an interface and base implementation for
* preparing action messages and initially dispatching them to their targets when * preparing action messages and initially dispatching them to their targets when
* certain events occur. * certain events occur.
* *
* To use the CCControl you have to subclass it. * To use the CCControl you have to subclass it.
*/ */
class CCControl : public CCLayer, public CCRGBAProtocol class CCControl : public CCLayer, public CCRGBAProtocol
{ {
@ -93,12 +92,16 @@ class CCControl : public CCLayer, public CCRGBAProtocol
CC_PROPERTY_PASS_BY_REF(ccColor3B, m_tColor, Color); CC_PROPERTY_PASS_BY_REF(ccColor3B, m_tColor, Color);
bool m_bIsOpacityModifyRGB; bool m_bIsOpacityModifyRGB;
bool isOpacityModifyRGB(); bool isOpacityModifyRGB();
void setOpacityModifyRGB(bool isOpacityModifyRGB); void setOpacityModifyRGB(bool bOpacityModifyRGB);
/** Changes the priority of the button. The lower the number, the higher the priority. */ /** Changes the priority of the button. The lower the number, the higher the priority. */
CC_SYNTHESIZE(int, m_nDefaultTouchPriority, DefaultTouchPriority); CC_SYNTHESIZE(int, m_nDefaultTouchPriority, DefaultTouchPriority);
/** The current control state constant. */ /** The current control state constant. */
CC_SYNTHESIZE_READONLY(CCControlState, m_nState, State); CC_SYNTHESIZE_READONLY(CCControlState, m_eState, State);
/** True if all of the controls parents are visible */
protected:
bool m_hasVisibleParents;
public: public:
/** Tells whether the control is enabled. */ /** Tells whether the control is enabled. */
@ -110,16 +113,23 @@ public:
/** A Boolean value that determines whether the control is highlighted. */ /** A Boolean value that determines whether the control is highlighted. */
virtual void setHighlighted(bool bHighlighted); virtual void setHighlighted(bool bHighlighted);
virtual bool isHighlighted(); virtual bool isHighlighted();
bool hasVisibleParents();
/**
* Updates the control layout using its current internal state.
*/
virtual void needsLayout();
protected: protected:
bool m_bEnabled; bool m_bEnabled;
bool m_bSelected; bool m_bSelected;
bool m_bHighlighted; bool m_bHighlighted;
// CCControlState, CCArray<CCInvocation*> /**
CCDictionary* dispatchTable; * Table of connection between the CCControlEvents and their associated
* target-actions pairs. For each CCButtonEvents a list of NSInvocation
* (which contains the target-action pair) is linked.
*/
CCDictionary* m_pDispatchTable; //cjh need to be retained
public: public:
CCControl(); CCControl();
@ -146,7 +156,7 @@ public:
* parameters, in that order. * parameters, in that order.
* When you call this method, target is not retained. * When you call this method, target is not retained.
* *
* @param target The target objectthat is, the object to which the action * @param target The target object that is, the object to which the action
* message is sent. It cannot be nil. The target is not retained. * message is sent. It cannot be nil. The target is not retained.
* @param action A selector identifying an action message. It cannot be NULL. * @param action A selector identifying an action message. It cannot be NULL.
* @param controlEvents A bitmask specifying the control events for which the * @param controlEvents A bitmask specifying the control events for which the
@ -175,32 +185,32 @@ public:
*/ */
virtual CCPoint getTouchLocation(CCTouch* touch); virtual CCPoint getTouchLocation(CCTouch* touch);
/** /**
* Returns a boolean value that indicates whether a touch is inside the bounds * Returns a boolean value that indicates whether a touch is inside the bounds
* of the receiver. The given touch must be relative to the world. * of the receiver. The given touch must be relative to the world.
* *
* @param touch A CCTouch object that represents a touch. * @param touch A CCTouch object that represents a touch.
* *
* @return YES whether a touch is inside the receivers rect. * @return YES whether a touch is inside the receiver¡¯s rect.
*/ */
virtual bool isTouchInside(CCTouch * touch); virtual bool isTouchInside(CCTouch * touch);
protected: protected:
/** /**
* Returns an CCInvocation object able to construct messages using a given * Returns an CCInvocation object able to construct messages using a given
* target-action pair. (The invocation may optionnaly include the sender and * target-action pair. (The invocation may optionnaly include the sender and
* the event as parameters, in that order) * the event as parameters, in that order)
* *
* @param target The target object. * @param target The target object.
* @param action A selector identifying an action message. * @param action A selector identifying an action message.
* @param controlEvent A control events for which the action message is sent. * @param controlEvent A control events for which the action message is sent.
* See "CCControlEvent" for constants. * See "CCControlEvent" for constants.
* *
* @return an CCInvocation object able to construct messages using a given * @return an CCInvocation object able to construct messages using a given
* target-action pair. * target-action pair.
*/ */
CCInvocation* invocationWithTargetAndActionForControlEvent(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvent); CCInvocation* invocationWithTargetAndActionForControlEvent(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvent);
@ -216,8 +226,33 @@ protected:
*/ */
//<CCInvocation*> //<CCInvocation*>
CCArray* dispatchListforControlEvent(CCControlEvent controlEvent); CCArray* dispatchListforControlEvent(CCControlEvent controlEvent);
public: /**
* Adds a target and action for a particular event to an internal dispatch
* table.
* The action message may optionnaly include the sender and the event as
* parameters, in that order.
* When you call this method, target is not retained.
*
* @param target The target object¡ªthat is, the object to which the action
* message is sent. It cannot be nil. The target is not retained.
* @param action A selector identifying an action message. It cannot be NULL.
* @param controlEvent A control event for which the action message is sent.
* See "CCControlEvent" for constants.
*/
void addTargetWithActionForControlEvent(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvent); void addTargetWithActionForControlEvent(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvent);
/**
* Removes a target and action for a particular event from an internal dispatch
* table.
*
* @param target The target object¡ªthat is, the object to which the action
* message is sent. Pass nil to remove all targets paired with action and the
* specified control events.
* @param action A selector identifying an action message. Pass NULL to remove
* all action messages paired with target.
* @param controlEvent A control event for which the action message is sent.
* See "CCControlEvent" for constants.
*/
void removeTargetWithActionForControlEvent(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvent); void removeTargetWithActionForControlEvent(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvent);
CREATE_FUNC(CCControl); CREATE_FUNC(CCControl);

View File

@ -41,12 +41,32 @@ enum
kZoomActionTag = 0xCCCB0001, kZoomActionTag = 0xCCCB0001,
}; };
CCControlButton::CCControlButton()
: m_currentTitle(NULL)
, m_currentTitleColor(ccWHITE)
, m_doesAdjustBackgroundImage(false)
, m_titleLabel(NULL)
, m_backgroundSprite(NULL)
, m_zoomOnTouchDown(false)
, m_isPushed(false)
, m_bParentInited(false)
, m_titleDispatchTable(NULL)
, m_titleColorDispatchTable(NULL)
, m_titleLabelDispatchTable(NULL)
, m_backgroundSpriteDispatchTable(NULL)
, m_marginH(CCControlButtonMarginLR)
, m_marginV(CCControlButtonMarginTB)
{
}
CCControlButton::~CCControlButton() CCControlButton::~CCControlButton()
{ {
CC_SAFE_RELEASE(m_backgroundSpriteDispatchTable); CC_SAFE_RELEASE(m_backgroundSpriteDispatchTable);
CC_SAFE_RELEASE(m_titleLabelDispatchTable); CC_SAFE_RELEASE(m_titleLabelDispatchTable);
CC_SAFE_RELEASE(m_titleColorDispatchTable); CC_SAFE_RELEASE(m_titleColorDispatchTable);
CC_SAFE_RELEASE(m_titleDispatchTable); CC_SAFE_RELEASE(m_titleDispatchTable);
CC_SAFE_RELEASE(m_backgroundSprite);
} }
//initialisers //initialisers
@ -56,50 +76,47 @@ bool CCControlButton::init()
return this->initWithLabelAndBackgroundSprite(CCLabelTTF::create("", "Helvetica", 12), CCScale9Sprite::create()); return this->initWithLabelAndBackgroundSprite(CCLabelTTF::create("", "Helvetica", 12), CCScale9Sprite::create());
} }
bool CCControlButton::initWithLabelAndBackgroundSprite(CCNode* node, CCScale9Sprite* backgroundSprite) bool CCControlButton::initWithLabelAndBackgroundSprite(CCNode* node, CCScale9Sprite* backgroundSprite)
{ {
if (CCControl::init()) if (CCControl::init())
{ {
assert(node != NULL); CCAssert(node != NULL, "Label must not be nil.");
CCLabelProtocol* label = dynamic_cast<CCLabelProtocol*>(node); CCLabelProtocol* label = dynamic_cast<CCLabelProtocol*>(node);
CCRGBAProtocol* rgbaLabel = dynamic_cast<CCRGBAProtocol*>(node); CCRGBAProtocol* rgbaLabel = dynamic_cast<CCRGBAProtocol*>(node);
assert(label != NULL || rgbaLabel!=NULL || backgroundSprite != NULL); 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); setTouchEnabled(true);
pushed=false; m_isPushed = false;
m_zoomOnTouchDown = true; m_zoomOnTouchDown = true;
m_nState=CCControlStateInitial;
m_currentTitle=NULL; m_currentTitle=NULL;
// Adjust the background image by default // Adjust the background image by default
m_adjustBackgroundImage=true; setAdjustBackgroundImage(true);
setPreferredSize(CCSizeZero);
// Zooming button by default // Zooming button by default
m_zoomOnTouchDown = true; m_zoomOnTouchDown = true;
// Set the default anchor point // Set the default anchor point
ignoreAnchorPointForPosition(false); ignoreAnchorPointForPosition(false);
setAnchorPoint(ccp(0.5f, 0.5f)); setAnchorPoint(ccp(0.5f, 0.5f));
// Set the nodes // Set the nodes
m_titleLabel = node; setTitleLabel(node);
m_backgroundSprite = backgroundSprite; setBackgroundSprite(backgroundSprite);
// Initialize the button state tables
m_titleDispatchTable=new CCDictionary();
//m_titleDispatchTable->autorelease();
m_titleColorDispatchTable=new CCDictionary();
//m_titleColorDispatchTable->autorelease();
m_titleLabelDispatchTable=new CCDictionary();
//m_titleLabelDispatchTable->autorelease();
m_backgroundSpriteDispatchTable=new CCDictionary();
//m_backgroundSpriteDispatchTable->autorelease();
// Set the default color and opacity // Set the default color and opacity
setColor(ccc3(255, 255, 255)); setColor(ccc3(255.0f, 255.0f, 255.0f));
setOpacity(255); setOpacity(255.0f);
setOpacityModifyRGB(true); setOpacityModifyRGB(true);
// Initialize the dispatch table // Initialize the dispatch table
@ -111,13 +128,7 @@ bool CCControlButton::initWithLabelAndBackgroundSprite(CCNode* node, CCScale9Spr
setTitleLabelForState(node, CCControlStateNormal); setTitleLabelForState(node, CCControlStateNormal);
setBackgroundSpriteForState(backgroundSprite, CCControlStateNormal); setBackgroundSpriteForState(backgroundSprite, CCControlStateNormal);
m_nState=CCControlStateNormal; setLabelAnchorPoint(ccp(0.5f, 0.5f));
//default margins
m_marginH=24;
m_marginV=12;
this->m_labelAnchorPoint = CCPoint(0.5f, 0.5f);
// Layout update // Layout update
needsLayout(); needsLayout();
@ -126,7 +137,9 @@ bool CCControlButton::initWithLabelAndBackgroundSprite(CCNode* node, CCScale9Spr
} }
//couldn't init the CCControl //couldn't init the CCControl
else else
{
return false; return false;
}
} }
CCControlButton* CCControlButton::buttonWithLabelAndBackgroundSprite(CCNode* label, CCScale9Sprite* backgroundSprite) CCControlButton* CCControlButton::buttonWithLabelAndBackgroundSprite(CCNode* label, CCScale9Sprite* backgroundSprite)
@ -233,20 +246,20 @@ void CCControlButton::setPreferredSize(CCSize size)
{ {
if(size.width == 0 && size.height == 0) if(size.width == 0 && size.height == 0)
{ {
m_adjustBackgroundImage = true; m_doesAdjustBackgroundImage = true;
} }
else else
{ {
m_adjustBackgroundImage = false; m_doesAdjustBackgroundImage = false;
CCDictElement * item = NULL; CCDictElement * item = NULL;
CCDICT_FOREACH(m_backgroundSpriteDispatchTable, item) CCDICT_FOREACH(m_backgroundSpriteDispatchTable, item)
{ {
CCScale9Sprite* sprite = (CCScale9Sprite*)item->getObject(); CCScale9Sprite* sprite = (CCScale9Sprite*)item->getObject();
sprite->setPreferredSize(size); sprite->setPreferredSize(size);
} }
m_preferredSize = size;
} }
m_preferredSize = size;
needsLayout(); needsLayout();
} }
@ -257,13 +270,13 @@ CCSize CCControlButton::getPreferredSize()
void CCControlButton::setAdjustBackgroundImage(bool adjustBackgroundImage) void CCControlButton::setAdjustBackgroundImage(bool adjustBackgroundImage)
{ {
m_adjustBackgroundImage=adjustBackgroundImage; m_doesAdjustBackgroundImage=adjustBackgroundImage;
needsLayout(); needsLayout();
} }
bool CCControlButton::getAdjustBackgroundImage() bool CCControlButton::doesAdjustBackgroundImage()
{ {
return m_adjustBackgroundImage; return m_doesAdjustBackgroundImage;
} }
CCPoint CCControlButton::getLabelAnchorPoint() CCPoint CCControlButton::getLabelAnchorPoint()
@ -274,18 +287,24 @@ CCPoint CCControlButton::getLabelAnchorPoint()
void CCControlButton::setLabelAnchorPoint(CCPoint labelAnchorPoint) void CCControlButton::setLabelAnchorPoint(CCPoint labelAnchorPoint)
{ {
this->m_labelAnchorPoint = labelAnchorPoint; this->m_labelAnchorPoint = labelAnchorPoint;
if (m_titleLabel != NULL)
this->m_titleLabel->setAnchorPoint(labelAnchorPoint); {
this->m_titleLabel->setAnchorPoint(labelAnchorPoint);
}
} }
CCString* CCControlButton::getTitleForState(CCControlState state) CCString* CCControlButton::getTitleForState(CCControlState state)
{ {
CCString* title=(CCString*)m_titleDispatchTable->objectForKey(state); if (m_titleDispatchTable != NULL)
if (title)
{ {
return title; CCString* title=(CCString*)m_titleDispatchTable->objectForKey(state);
if (title)
{
return title;
}
return (CCString*)m_titleDispatchTable->objectForKey(CCControlStateNormal);
} }
return (CCString*)m_titleDispatchTable->objectForKey(CCControlStateNormal); return CCString::create("");
} }
void CCControlButton::setTitleForState(CCString* title, CCControlState state) void CCControlButton::setTitleForState(CCString* title, CCControlState state)
@ -307,15 +326,24 @@ void CCControlButton::setTitleForState(CCString* title, CCControlState state)
const ccColor3B CCControlButton::getTitleColorForState(CCControlState state) const ccColor3B CCControlButton::getTitleColorForState(CCControlState state)
{ {
ccColor3B returnColor; ccColor3B returnColor = ccWHITE;
CCColor3bObject* colorObject=(CCColor3bObject*)m_titleColorDispatchTable->objectForKey(state); do
if (colorObject)
{ {
returnColor= colorObject->value; CC_BREAK_IF(NULL == m_titleColorDispatchTable);
return returnColor; CCColor3bObject* colorObject=(CCColor3bObject*)m_titleColorDispatchTable->objectForKey(state);
} if (colorObject)
colorObject=(CCColor3bObject*)m_titleColorDispatchTable->objectForKey(CCControlStateNormal); {
returnColor=colorObject->value; returnColor= colorObject->value;
break;
}
colorObject=(CCColor3bObject*)m_titleColorDispatchTable->objectForKey(CCControlStateNormal);
if (colorObject)
{
returnColor=colorObject->value;
}
} while (0);
return returnColor; return returnColor;
} }
@ -368,7 +396,10 @@ void CCControlButton::setTitleLabelForState(CCNode* titleLabel, CCControlState s
void CCControlButton::setTitleTTFForState(const char * fntFile, CCControlState state) void CCControlButton::setTitleTTFForState(const char * fntFile, CCControlState state)
{ {
CCString * title = this->getTitleForState(state); CCString * title = this->getTitleForState(state);
if (!title) title = new CCString(""); if (!title)
{
title = CCString::create("");
}
this->setTitleLabelForState(CCLabelTTF::create(title->getCString(), fntFile, 12), state); this->setTitleLabelForState(CCLabelTTF::create(title->getCString(), fntFile, 12), state);
} }
@ -416,7 +447,10 @@ float CCControlButton::getTitleTTFSizeForState(CCControlState state)
void CCControlButton::setTitleBMFontForState(const char * fntFile, CCControlState state) void CCControlButton::setTitleBMFontForState(const char * fntFile, CCControlState state)
{ {
CCString * title = this->getTitleForState(state); CCString * title = this->getTitleForState(state);
if (!title) title = new CCString(""); if (!title)
{
title = CCString::create("");
}
this->setTitleLabelForState(CCLabelBMFont::create(title->getCString(), fntFile), state); this->setTitleLabelForState(CCLabelBMFont::create(title->getCString(), fntFile), state);
} }
@ -448,10 +482,12 @@ CCScale9Sprite* CCControlButton::getBackgroundSpriteForState(CCControlState stat
void CCControlButton::setBackgroundSpriteForState(CCScale9Sprite* sprite, CCControlState state) void CCControlButton::setBackgroundSpriteForState(CCScale9Sprite* sprite, CCControlState state)
{ {
CCScale9Sprite* previousSprite = (CCScale9Sprite*)m_backgroundSpriteDispatchTable->objectForKey(state); CCSize oldPreferredSize = m_preferredSize;
if (previousSprite)
CCScale9Sprite* previousBackgroundSprite = (CCScale9Sprite*)m_backgroundSpriteDispatchTable->objectForKey(state);
if (previousBackgroundSprite)
{ {
removeChild(previousSprite, true); removeChild(previousBackgroundSprite, true);
m_backgroundSpriteDispatchTable->removeObjectForKey(state); m_backgroundSpriteDispatchTable->removeObjectForKey(state);
} }
@ -462,6 +498,12 @@ void CCControlButton::setBackgroundSpriteForState(CCScale9Sprite* sprite, CCCont
if (this->m_preferredSize.width != 0 || this->m_preferredSize.height != 0) if (this->m_preferredSize.width != 0 || this->m_preferredSize.height != 0)
{ {
if (oldPreferredSize.equals(m_preferredSize))
{
// Force update of preferred size
sprite->setPreferredSize(CCSizeMake(oldPreferredSize.width+1, oldPreferredSize.height+1));
}
sprite->setPreferredSize(this->m_preferredSize); sprite->setPreferredSize(this->m_preferredSize);
} }
@ -481,70 +523,113 @@ void CCControlButton::setBackgroundSpriteFrameForState(CCSpriteFrame * spriteFra
void CCControlButton::needsLayout() void CCControlButton::needsLayout()
{ {
if (!m_bParentInited) {
return;
}
// Hide the background and the label // Hide the background and the label
m_titleLabel->setVisible(false); if (m_titleLabel != NULL) {
m_backgroundSprite->setVisible(false); m_titleLabel->setVisible(false);
}
if (m_backgroundSprite) {
m_backgroundSprite->setVisible(false);
}
// Update anchor of all labels // Update anchor of all labels
this->setLabelAnchorPoint(this->m_labelAnchorPoint); this->setLabelAnchorPoint(this->m_labelAnchorPoint);
// Update the label to match with the current state // Update the label to match with the current state
//CC_SAFE_RELEASE(m_currentTitle) CC_SAFE_RELEASE(m_currentTitle);
m_currentTitle = getTitleForState(m_eState);
m_currentTitle=getTitleForState(m_nState); CC_SAFE_RETAIN(m_currentTitle);
m_currentTitleColor=getTitleColorForState(m_nState);
m_currentTitleColor=getTitleColorForState(m_eState);
this->setTitleLabel(getTitleLabelForState(m_eState));
m_titleLabel=getTitleLabelForState(m_nState);
CCLabelProtocol* label = dynamic_cast<CCLabelProtocol*>(m_titleLabel); CCLabelProtocol* label = dynamic_cast<CCLabelProtocol*>(m_titleLabel);
if (label) if (label && m_currentTitle)
{
label->setString(m_currentTitle->getCString()); label->setString(m_currentTitle->getCString());
}
CCRGBAProtocol* rgbaLabel = dynamic_cast<CCRGBAProtocol*>(m_titleLabel); CCRGBAProtocol* rgbaLabel = dynamic_cast<CCRGBAProtocol*>(m_titleLabel);
if (rgbaLabel) if (rgbaLabel)
{
rgbaLabel->setColor(m_currentTitleColor); rgbaLabel->setColor(m_currentTitleColor);
m_titleLabel->setPosition(ccp (getContentSize().width / 2, getContentSize().height / 2)); }
if (m_titleLabel != NULL)
{
m_titleLabel->setPosition(ccp (getContentSize().width / 2, getContentSize().height / 2));
}
// Update the background sprite // Update the background sprite
m_backgroundSprite=getBackgroundSpriteForState(m_nState); this->setBackgroundSprite(this->getBackgroundSpriteForState(m_eState));
m_backgroundSprite->setPosition(ccp (getContentSize().width / 2, getContentSize().height / 2)); if (m_backgroundSprite != NULL)
{
m_backgroundSprite->setPosition(ccp (getContentSize().width / 2, getContentSize().height / 2));
}
// Get the title label size // Get the title label size
CCSize titleLabelSize =m_titleLabel->boundingBox().size; CCSize titleLabelSize;
if (m_titleLabel != NULL)
{
titleLabelSize = m_titleLabel->boundingBox().size;
}
// Adjust the background image if necessary // Adjust the background image if necessary
if (m_adjustBackgroundImage) if (m_doesAdjustBackgroundImage)
{ {
// Add the margins // Add the margins
m_backgroundSprite->setContentSize(CCSizeMake(titleLabelSize.width + m_marginH * 2, titleLabelSize.height + m_marginV * 2)); if (m_backgroundSprite != NULL)
{
m_backgroundSprite->setContentSize(CCSizeMake(titleLabelSize.width + m_marginH * 2, titleLabelSize.height + m_marginV * 2));
}
} }
else else
{ {
//TODO: should this also have margins if one of the preferred sizes is relaxed? //TODO: should this also have margins if one of the preferred sizes is relaxed?
CCSize preferredSize = m_backgroundSprite->getPreferredSize(); if (m_backgroundSprite != NULL)
if (preferredSize.width <= 0)
{ {
preferredSize.width = titleLabelSize.width; 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);
} }
if (preferredSize.height <= 0)
{
preferredSize.height = titleLabelSize.height;
}
m_backgroundSprite->setContentSize(preferredSize);
} }
// Set the content size // Set the content size
CCRect rectTitle;
CCRect maxRect = CCControlUtils::CCRectUnion(m_titleLabel->boundingBox(), m_backgroundSprite->boundingBox()); 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)); setContentSize(CCSizeMake(maxRect.size.width, maxRect.size.height));
m_titleLabel->setPosition(ccp(getContentSize().width/2, getContentSize().height/2)); if (m_titleLabel != NULL)
m_backgroundSprite->setPosition(ccp(getContentSize().width/2, getContentSize().height/2)); {
m_titleLabel->setPosition(ccp(getContentSize().width/2, getContentSize().height/2));
// Make visible the background and the label // Make visible the background and the label
m_titleLabel->setVisible(true); m_titleLabel->setVisible(true);
m_backgroundSprite->setVisible(true); }
if (m_backgroundSprite != NULL)
{
m_backgroundSprite->setPosition(ccp(getContentSize().width/2, getContentSize().height/2));
m_backgroundSprite->setVisible(true);
}
} }
@ -556,8 +641,8 @@ bool CCControlButton::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
return false; return false;
} }
m_nState=CCControlStateHighlighted; m_eState=CCControlStateHighlighted;
pushed=true; m_isPushed=true;
this->setHighlighted(true); this->setHighlighted(true);
sendActionsForControlEvents(CCControlEventTouchDown); sendActionsForControlEvents(CCControlEventTouchDown);
return true; return true;
@ -565,9 +650,9 @@ bool CCControlButton::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
void CCControlButton::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent) void CCControlButton::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{ {
if (!m_bEnabled || !pushed || m_bSelected) if (!isEnabled() || !isPushed() || isSelected())
{ {
if (m_bHighlighted) if (isHighlighted())
{ {
setHighlighted(false); setHighlighted(false);
} }
@ -575,32 +660,32 @@ void CCControlButton::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
} }
bool isTouchMoveInside = isTouchInside(pTouch); bool isTouchMoveInside = isTouchInside(pTouch);
if (isTouchMoveInside && !m_bHighlighted) if (isTouchMoveInside && !isHighlighted())
{ {
m_nState = CCControlStateHighlighted; m_eState = CCControlStateHighlighted;
setHighlighted(true); setHighlighted(true);
sendActionsForControlEvents(CCControlEventTouchDragEnter); sendActionsForControlEvents(CCControlEventTouchDragEnter);
} }
else if (isTouchMoveInside && m_bHighlighted) else if (isTouchMoveInside && isHighlighted())
{ {
sendActionsForControlEvents(CCControlEventTouchDragInside); sendActionsForControlEvents(CCControlEventTouchDragInside);
} }
else if (!isTouchMoveInside && m_bHighlighted) else if (!isTouchMoveInside && isHighlighted())
{ {
m_nState = CCControlStateNormal; m_eState = CCControlStateNormal;
setHighlighted(false); setHighlighted(false);
sendActionsForControlEvents(CCControlEventTouchDragExit); sendActionsForControlEvents(CCControlEventTouchDragExit);
} }
else if (!isTouchMoveInside && !m_bHighlighted) else if (!isTouchMoveInside && !isHighlighted())
{ {
sendActionsForControlEvents(CCControlEventTouchDragOutside); sendActionsForControlEvents(CCControlEventTouchDragOutside);
} }
} }
void CCControlButton::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) void CCControlButton::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{ {
m_nState = CCControlStateNormal; m_eState = CCControlStateNormal;
pushed = false; m_isPushed = false;
setHighlighted(false); setHighlighted(false);
@ -643,8 +728,8 @@ GLubyte CCControlButton::getOpacity()
void CCControlButton::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent) void CCControlButton::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent)
{ {
m_nState = CCControlStateNormal; m_eState = CCControlStateNormal;
pushed = false; m_isPushed = false;
setHighlighted(false); setHighlighted(false);
sendActionsForControlEvents(CCControlEventTouchCancel); sendActionsForControlEvents(CCControlEventTouchCancel);
} }

View File

@ -35,6 +35,12 @@
NS_CC_EXT_BEGIN NS_CC_EXT_BEGIN
/* Define the button margin for Left/Right edge */
#define CCControlButtonMarginLR 8 // px
/* Define the button margin for Top/Bottom edge */
#define CCControlButtonMarginTB 2 // px
/** /**
* @addtogroup GUI * @addtogroup GUI
* @{ * @{
@ -46,6 +52,7 @@ NS_CC_EXT_BEGIN
class CCControlButton : public CCControl class CCControlButton : public CCControl
{ {
public: public:
CCControlButton();
virtual ~CCControlButton(); virtual ~CCControlButton();
virtual void needsLayout(void); virtual void needsLayout(void);
@ -56,42 +63,51 @@ protected:
// CCRGBAProtocol // CCRGBAProtocol
//bool m_bIsOpacityModifyRGB; //bool m_bIsOpacityModifyRGB;
/** The current title that is displayed on the button. */
CC_SYNTHESIZE_READONLY(CCString*, m_currentTitle, CurrentTitle);
/** The current color used to display the title. */
CC_SYNTHESIZE_READONLY_PASS_BY_REF(ccColor3B, m_currentTitleColor, CurrentTitleColor);
/** Adjust the background image. YES by default. If the property is set to NO, the /** Adjust the background image. YES by default. If the property is set to NO, the
background will use the prefered size of the background image. */ background will use the prefered size of the background image. */
CC_PROPERTY(bool, m_adjustBackgroundImage, AdjustBackgroundImage); bool doesAdjustBackgroundImage();
void setAdjustBackgroundImage(bool adjustBackgroundImage);
bool m_doesAdjustBackgroundImage;
/** Adjust the button zooming on touchdown. Default value is YES. */ /** The current title label. */
CC_PROPERTY(bool, m_zoomOnTouchDown, ZoomOnTouchDown); CC_SYNTHESIZE_RETAIN(CCNode*, m_titleLabel, TitleLabel);
/** The current background sprite. */
CC_SYNTHESIZE_RETAIN(CCScale9Sprite*, m_backgroundSprite, BackgroundSprite);
/** The prefered size of the button, if label is larger it will be expanded. */ /** The prefered size of the button, if label is larger it will be expanded. */
CC_PROPERTY(CCSize, m_preferredSize, PreferredSize); CC_PROPERTY(CCSize, m_preferredSize, PreferredSize);
/** Adjust the button zooming on touchdown. Default value is YES. */
CC_PROPERTY(bool, m_zoomOnTouchDown, ZoomOnTouchDown);
CC_PROPERTY(CCPoint, m_labelAnchorPoint, LabelAnchorPoint); CC_PROPERTY(CCPoint, m_labelAnchorPoint, LabelAnchorPoint);
/** The current title that is displayed on the button. */
CC_SYNTHESIZE_READONLY(CCString*, m_currentTitle, CurrentTitle);
/** The current color used to display the title. */
CC_SYNTHESIZE_READONLY_PASS_BY_REF(ccColor3B, m_currentTitleColor, CurrentTitleColor);
/** The current title label. */
//CC_PROPERTY(CCNode*, m_titleLabel, TitleLabel);
CCNode* m_titleLabel;
/** The current background sprite. */
//CC_PROPERTY(CCScale9Sprite*, m_backgroundSprite, BackgroundSprite);
CCScale9Sprite* m_backgroundSprite;
/* Override setter to affect a background sprite too */ /* Override setter to affect a background sprite too */
CC_PROPERTY(GLubyte, m_cOpacity, Opacity); virtual GLubyte getOpacity(void);
virtual void setOpacity(GLubyte var);
/** Flag to know if the button is currently pushed. */ /** Flag to know if the button is currently pushed. */
CC_SYNTHESIZE_READONLY(bool, pushed, IsPushed); protected:
bool m_isPushed;
bool m_bParentInited;
public:
bool isPushed() { return m_isPushed; }
// <CCControlState, CCString*> // <CCControlState, CCString*>
CCDictionary* m_titleDispatchTable; CC_SYNTHESIZE_RETAIN(CCDictionary*, m_titleDispatchTable, TitleDispatchTable);
// <CCControlState, CCColor3bObject*> // <CCControlState, CCColor3bObject*>
CCDictionary* m_titleColorDispatchTable; CC_SYNTHESIZE_RETAIN(CCDictionary*, m_titleColorDispatchTable, TitleColorDispatchTable);
// <CCControlState, CCNode*> // <CCControlState, CCNode*>
CCDictionary* m_titleLabelDispatchTable; CC_SYNTHESIZE_RETAIN(CCDictionary*, m_titleLabelDispatchTable, TitleLabelDispatchTable);
// <CCControlState, CCScale9Sprite*> // <CCControlState, CCScale9Sprite*>
CCDictionary* m_backgroundSpriteDispatchTable; CC_SYNTHESIZE_RETAIN(CCDictionary*, m_backgroundSpriteDispatchTable, BackgroundSpriteDispatchTable);
/* Define the button margin for Top/Bottom edge */ /* Define the button margin for Top/Bottom edge */
CC_SYNTHESIZE_READONLY(int, m_marginV, VerticalMargin); CC_SYNTHESIZE_READONLY(int, m_marginV, VerticalMargin);

View File

@ -35,6 +35,36 @@
NS_CC_EXT_BEGIN NS_CC_EXT_BEGIN
CCControlColourPicker::CCControlColourPicker()
: m_colourPicker(NULL)
, m_huePicker(NULL)
, m_background(NULL)
{
}
CCControlColourPicker::~CCControlColourPicker()
{
if (m_background)
{
m_background->removeFromParentAndCleanup(true);
}
if (m_huePicker)
{
m_huePicker->removeFromParentAndCleanup(true);
}
if (m_colourPicker)
{
m_colourPicker->removeFromParentAndCleanup(true);
}
m_background = NULL;
m_huePicker = NULL;
m_colourPicker = NULL;
}
bool CCControlColourPicker::init() bool CCControlColourPicker::init()
{ {
if (CCControl::init()) if (CCControl::init())
@ -48,10 +78,10 @@ bool CCControlColourPicker::init()
addChild(spriteSheet); addChild(spriteSheet);
// MIPMAP // MIPMAP
ccTexParams params = {GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT}; // ccTexParams params = {GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
spriteSheet->getTexture()->setAliasTexParameters(); spriteSheet->getTexture()->setAliasTexParameters();
spriteSheet->getTexture()->setTexParameters(&params); // spriteSheet->getTexture()->setTexParameters(&params);
spriteSheet->getTexture()->generateMipmap(); // spriteSheet->getTexture()->generateMipmap();
// Init default color // Init default color
m_hsv.h = 0; m_hsv.h = 0;
@ -63,12 +93,14 @@ bool CCControlColourPicker::init()
CCPoint backgroundPointZero = ccpSub(m_background->getPosition(), ccp (m_background->getContentSize().width / 2, m_background->getContentSize().height / 2)); CCPoint backgroundPointZero = ccpSub(m_background->getPosition(), ccp (m_background->getContentSize().width / 2, m_background->getContentSize().height / 2));
// Setup panels . currently hard-coded... // Setup panels
float hueShift = 8; float hueShift = 8;
float colourShift = 28; float colourShift = 28;
m_huePicker=CCControlHuePicker::create(spriteSheet, ccp(backgroundPointZero.x + hueShift, backgroundPointZero.y + hueShift)); m_huePicker = new CCControlHuePicker();
m_colourPicker=CCControlSaturationBrightnessPicker::create(spriteSheet, ccp(backgroundPointZero.x + colourShift, backgroundPointZero.y + colourShift)); m_huePicker->initWithTargetAndPos(spriteSheet, ccp(backgroundPointZero.x + hueShift, backgroundPointZero.y + hueShift));
m_colourPicker = new CCControlSaturationBrightnessPicker();
m_colourPicker->initWithTargetAndPos(spriteSheet, ccp(backgroundPointZero.x + colourShift, backgroundPointZero.y + colourShift));
// Setup events // Setup events
m_huePicker->addTargetWithActionForControlEvents(this, cccontrol_selector(CCControlColourPicker::hueSliderValueChanged), CCControlEventValueChanged); m_huePicker->addTargetWithActionForControlEvents(this, cccontrol_selector(CCControlColourPicker::hueSliderValueChanged), CCControlEventValueChanged);
@ -101,20 +133,34 @@ CCControlColourPicker* CCControlColourPicker::create()
} }
void CCControlColourPicker::setColorValue(const ccColor3B& colorValue) void CCControlColourPicker::setColor(const ccColor3B& color)
{ {
m_colorValue = colorValue; m_tColor = color;
RGBA rgba; RGBA rgba;
rgba.r = colorValue.r / 255.0f; rgba.r = color.r / 255.0f;
rgba.g = colorValue.g / 255.0f; rgba.g = color.g / 255.0f;
rgba.b = colorValue.b / 255.0f; rgba.b = color.b / 255.0f;
rgba.a = 1.0f; rgba.a = 1.0f;
m_hsv=CCControlUtils::HSVfromRGB(rgba); m_hsv=CCControlUtils::HSVfromRGB(rgba);
updateHueAndControlPicker(); updateHueAndControlPicker();
} }
void CCControlColourPicker::setEnabled(bool enabled)
{
CCControl::setEnabled(enabled);
if (m_huePicker != NULL)
{
m_huePicker->setEnabled(enabled);
}
if (m_colourPicker)
{
m_colourPicker->setEnabled(enabled);
}
}
//need two events to prevent an infinite loop! (can't update huePicker when the huePicker triggers the callback due to CCControlEventValueChanged) //need two events to prevent an infinite loop! (can't update huePicker when the huePicker triggers the callback due to CCControlEventValueChanged)
void CCControlColourPicker::updateControlPicker() void CCControlColourPicker::updateControlPicker()
{ {
@ -136,7 +182,7 @@ void CCControlColourPicker::hueSliderValueChanged(CCObject * sender, CCControlEv
// Update the value // Update the value
RGBA rgb = CCControlUtils::RGBfromHSV(m_hsv); RGBA rgb = CCControlUtils::RGBfromHSV(m_hsv);
m_colorValue= ccc3((GLubyte)(rgb.r * 255.0f), (GLubyte)(rgb.g * 255.0f), (GLubyte)(rgb.b * 255.0f)); m_tColor= ccc3((GLubyte)(rgb.r * 255.0f), (GLubyte)(rgb.g * 255.0f), (GLubyte)(rgb.b * 255.0f));
// Send CCControl callback // Send CCControl callback
sendActionsForControlEvents(CCControlEventValueChanged); sendActionsForControlEvents(CCControlEventValueChanged);
@ -151,7 +197,7 @@ void CCControlColourPicker::colourSliderValueChanged(CCObject * sender, CCContro
// Update the value // Update the value
RGBA rgb = CCControlUtils::RGBfromHSV(m_hsv); RGBA rgb = CCControlUtils::RGBfromHSV(m_hsv);
m_colorValue=ccc3((GLubyte)(rgb.r * 255.0f), (GLubyte)(rgb.g * 255.0f), (GLubyte)(rgb.b * 255.0f)); m_tColor=ccc3((GLubyte)(rgb.r * 255.0f), (GLubyte)(rgb.g * 255.0f), (GLubyte)(rgb.b * 255.0f));
// Send CCControl callback // Send CCControl callback
sendActionsForControlEvents(CCControlEventValueChanged); sendActionsForControlEvents(CCControlEventValueChanged);

View File

@ -48,15 +48,16 @@ NS_CC_EXT_BEGIN
class CCControlColourPicker: public CCControl class CCControlColourPicker: public CCControl
{ {
CC_SYNTHESIZE_READONLY_PASS_BY_REF(ccColor3B, m_colorValue, ColorValue); public:
virtual void setColorValue(const ccColor3B& colorValue); CCControlColourPicker();
virtual ~CCControlColourPicker();
virtual void setColor(const ccColor3B& colorValue);
virtual void setEnabled(bool bEnabled);
protected: protected:
HSV m_hsv; HSV m_hsv;
CCControlSaturationBrightnessPicker* m_colourPicker; CC_SYNTHESIZE_RETAIN(CCControlSaturationBrightnessPicker*, m_colourPicker, colourPicker)
CCControlHuePicker* m_huePicker; CC_SYNTHESIZE_RETAIN(CCControlHuePicker*, m_huePicker, HuePicker)
CC_SYNTHESIZE_RETAIN(CCSprite*, m_background, Background)
CC_SYNTHESIZE_READONLY(CCSprite*, m_background, Background);
public: public:
//@deprecated: This interface will be deprecated sooner or later. //@deprecated: This interface will be deprecated sooner or later.

View File

@ -1,14 +1,13 @@
#ifndef __CCCONTROL_EXTENSIONS_H__ #ifndef __CCCONTROL_EXTENSIONS_H__
#define __CCCONTROL_EXTENSIONS_H__ #define __CCCONTROL_EXTENSIONS_H__
#include "CCControl.h" #include "CCScale9Sprite.h"
#include "CCControlButton.h" #include "CCControl.h"
#include "CCControlColourPicker.h" #include "CCControlButton.h"
#include "CCControlHuePicker.h" #include "CCControlColourPicker.h"
#include "CCControlSaturationBrightnessPicker.h" #include "CCControlPotentiometer.h"
#include "CCControlSlider.h" #include "CCControlSlider.h"
#include "CCControlStepper.h"
#include "CCControlSwitch.h" #include "CCControlSwitch.h"
#include "CCMenuPassive.h"
#include "CCSpacer.h"
#endif #endif

View File

@ -33,11 +33,22 @@
NS_CC_EXT_BEGIN NS_CC_EXT_BEGIN
CCControlHuePicker::~CCControlHuePicker() CCControlHuePicker::CCControlHuePicker()
: m_hue(0.0f)
, m_huePercentage(0.0f)
, m_background(NULL)
, m_slider(NULL)
{ {
} }
CCControlHuePicker::~CCControlHuePicker()
{
removeAllChildrenWithCleanup(true);
CC_SAFE_RELEASE(m_background);
CC_SAFE_RELEASE(m_slider);
}
CCControlHuePicker* CCControlHuePicker::pickerWithTargetAndPos(CCNode* target, CCPoint pos) CCControlHuePicker* CCControlHuePicker::pickerWithTargetAndPos(CCNode* target, CCPoint pos)
{ {
return CCControlHuePicker::create(target, pos); return CCControlHuePicker::create(target, pos);
@ -58,8 +69,8 @@ bool CCControlHuePicker::initWithTargetAndPos(CCNode* target, CCPoint pos)
{ {
setTouchEnabled(true); setTouchEnabled(true);
// Add background and slider sprites // Add background and slider sprites
m_background=CCControlUtils::addSpriteToTargetWithPosAndAnchor("huePickerBackground.png", target, pos, ccp(0.0f, 0.0f)); this->setBackground(CCControlUtils::addSpriteToTargetWithPosAndAnchor("huePickerBackground.png", target, pos, ccp(0.0f, 0.0f)));
m_slider=CCControlUtils::addSpriteToTargetWithPosAndAnchor("colourPicker.png", target, pos, ccp(0.5f, 0.5f)); this->setSlider(CCControlUtils::addSpriteToTargetWithPosAndAnchor("colourPicker.png", target, pos, ccp(0.5f, 0.5f)));
m_slider->setPosition(ccp(pos.x, pos.y + m_background->boundingBox().size.height * 0.5f)); m_slider->setPosition(ccp(pos.x, pos.y + m_background->boundingBox().size.height * 0.5f));
m_startPos=pos; m_startPos=pos;
@ -70,13 +81,18 @@ bool CCControlHuePicker::initWithTargetAndPos(CCNode* target, CCPoint pos)
return true; return true;
} }
else else
{
return false; return false;
}
} }
void CCControlHuePicker::setHue(float hueValue) void CCControlHuePicker::setHue(float hueValue)
{ {
m_hue=hueValue; m_hue=hueValue;
setHuePercentage(m_hue/360.0f); // Set the position of the slider to the correct hue
// We need to divide it by 360 as its taken as an angle in degrees
float huePercentage = hueValue / 360.0f;
setHuePercentage(huePercentage);
} }
void CCControlHuePicker::setHuePercentage(float hueValueInPercent) void CCControlHuePicker::setHuePercentage(float hueValueInPercent)
@ -105,6 +121,15 @@ void CCControlHuePicker::setHuePercentage(float hueValueInPercent)
} }
void CCControlHuePicker::setEnabled(bool enabled)
{
CCControl::setEnabled(enabled);
if (m_slider != NULL)
{
m_slider->setOpacity(enabled ? 255 : 128);
}
}
void CCControlHuePicker::updateSliderPosition(CCPoint location) void CCControlHuePicker::updateSliderPosition(CCPoint location)
{ {
@ -132,17 +157,25 @@ void CCControlHuePicker::updateSliderPosition(CCPoint location)
bool CCControlHuePicker::checkSliderPosition(CCPoint location) bool CCControlHuePicker::checkSliderPosition(CCPoint location)
{ {
// check that the touch location is within the bounding rectangle before sending updates // compute the distance between the current location and the center
if (m_background->boundingBox().containsPoint(location)) double distance = sqrt(pow (location.x + 10, 2) + pow(location.y, 2));
{
updateSliderPosition(location); // check that the touch location is within the circle
return true; if (160 > distance && distance > 118)
} {
updateSliderPosition(location);
return true;
}
return false; return false;
} }
bool CCControlHuePicker::ccTouchBegan(CCTouch* touch, CCEvent* event) bool CCControlHuePicker::ccTouchBegan(CCTouch* touch, CCEvent* event)
{ {
if (!isEnabled())
{
return false;
}
// Get the touch location // Get the touch location
CCPoint touchLocation=getTouchLocation(touch); CCPoint touchLocation=getTouchLocation(touch);
@ -157,10 +190,10 @@ void CCControlHuePicker::ccTouchMoved(CCTouch* touch, CCEvent* event)
CCPoint touchLocation=getTouchLocation(touch); CCPoint touchLocation=getTouchLocation(touch);
//small modification: this allows changing of the colour, even if the touch leaves the bounding area //small modification: this allows changing of the colour, even if the touch leaves the bounding area
updateSliderPosition(touchLocation); // updateSliderPosition(touchLocation);
sendActionsForControlEvents(CCControlEventValueChanged); // sendActionsForControlEvents(CCControlEventValueChanged);
// Check the touch position on the slider // Check the touch position on the slider
//checkSliderPosition(touchLocation); checkSliderPosition(touchLocation);
} }
NS_CC_EXT_END NS_CC_EXT_END

View File

@ -54,16 +54,18 @@ class CCControlHuePicker : public CCControl
//not sure if these need to be there actually. I suppose someone might want to access the sprite? //not sure if these need to be there actually. I suppose someone might want to access the sprite?
CC_SYNTHESIZE_READONLY(CCSprite*, m_background, Background); CC_SYNTHESIZE_RETAIN(CCSprite*, m_background, Background);
CC_SYNTHESIZE_READONLY(CCSprite*, m_slider, Slider); CC_SYNTHESIZE_RETAIN(CCSprite*, m_slider, Slider);
CC_SYNTHESIZE_READONLY(CCPoint, m_startPos, StartPos); CC_SYNTHESIZE_READONLY(CCPoint, m_startPos, StartPos);
public: public:
CCControlHuePicker();
virtual ~CCControlHuePicker(); virtual ~CCControlHuePicker();
virtual bool initWithTargetAndPos(CCNode* target, CCPoint pos); virtual bool initWithTargetAndPos(CCNode* target, CCPoint pos);
//@deprecated: This interface will be deprecated sooner or later. //@deprecated: This interface will be deprecated sooner or later.
CC_DEPRECATED_ATTRIBUTE static CCControlHuePicker* pickerWithTargetAndPos(CCNode* target, CCPoint pos); CC_DEPRECATED_ATTRIBUTE static CCControlHuePicker* pickerWithTargetAndPos(CCNode* target, CCPoint pos);
static CCControlHuePicker* create(CCNode* target, CCPoint pos); static CCControlHuePicker* create(CCNode* target, CCPoint pos);
virtual void setEnabled(bool enabled);
protected: protected:
void updateSliderPosition(CCPoint location); void updateSliderPosition(CCPoint location);
bool checkSliderPosition(CCPoint location); bool checkSliderPosition(CCPoint location);

View File

@ -0,0 +1,266 @@
/*
* CCControlPotentiometer.m
*
* Copyright 2012 Yannick Loriot. All rights reserved.
* 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
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include "CCControlPotentiometer.h"
#include "cocos2d.h"
NS_CC_EXT_BEGIN
CCControlPotentiometer::CCControlPotentiometer()
: m_fValue(0.0f)
, m_fMinimumValue(0.0f)
, m_fMaximumValue(0.0f)
, m_pThumbSprite(NULL)
, m_pProgressTimer(NULL)
{
}
CCControlPotentiometer::~CCControlPotentiometer()
{
CC_SAFE_RELEASE(m_pThumbSprite);
CC_SAFE_RELEASE(m_pProgressTimer);
}
CCControlPotentiometer* CCControlPotentiometer::create(const char* backgroundFile, const char* progressFile, const char* thumbFile)
{
CCControlPotentiometer* pRet = new CCControlPotentiometer();
if (pRet != NULL)
{
// Prepare track for potentiometer
CCSprite *backgroundSprite = CCSprite::create(backgroundFile);
// Prepare thumb for potentiometer
CCSprite *thumbSprite = CCSprite::create(thumbFile);
// Prepare progress for potentiometer
CCProgressTimer *progressTimer = CCProgressTimer::create(CCSprite::create(progressFile));
//progressTimer.type = kCCProgressTimerTypeRadialCW;
if (pRet->initWithTrackSprite_ProgressTimer_ThumbSprite(backgroundSprite, progressTimer, thumbSprite))
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
}
}
return pRet;
}
bool CCControlPotentiometer::initWithTrackSprite_ProgressTimer_ThumbSprite(CCSprite* trackSprite, CCProgressTimer* progressTimer, CCSprite* thumbSprite)
{
if (CCControl::init())
{
setTouchEnabled(true);
setProgressTimer(progressTimer);
setThumbSprite(thumbSprite);
thumbSprite->setPosition(progressTimer->getPosition());
addChild(thumbSprite, 2);
addChild(progressTimer, 1);
addChild(trackSprite);
setContentSize(trackSprite->getContentSize());
// Init default values
m_fMinimumValue = 0.0f;
m_fMaximumValue = 1.0f;
setValue(m_fMinimumValue);
return true;
}
return false;
}
void CCControlPotentiometer::setEnabled(bool enabled)
{
CCControl::setEnabled(enabled);
if (m_pThumbSprite != NULL)
{
m_pThumbSprite->setOpacity((enabled) ? 255 : 128);
}
}
void CCControlPotentiometer::setValue(float value)
{
// set new value with sentinel
if (value < m_fMinimumValue)
{
value = m_fMinimumValue;
}
if (value > m_fMaximumValue)
{
value = m_fMaximumValue;
}
m_fValue = value;
// Update thumb and progress position for new value
float percent = (value - m_fMinimumValue) / (m_fMaximumValue - m_fMinimumValue);
m_pProgressTimer->setPercentage(percent * 100.0f);
m_pThumbSprite->setRotation(percent * 360.0f);
sendActionsForControlEvents(CCControlEventValueChanged);
}
float CCControlPotentiometer::getValue()
{
return m_fValue;
}
void CCControlPotentiometer::setMinimumValue(float minimumValue)
{
m_fMinimumValue = minimumValue;
if (m_fMinimumValue >= m_fMaximumValue)
{
m_fMaximumValue = m_fMinimumValue + 1.0f;
}
setValue(m_fMaximumValue);
}
float CCControlPotentiometer::getMinimumValue()
{
return m_fMinimumValue;
}
void CCControlPotentiometer::setMaximumValue(float maximumValue)
{
m_fMaximumValue = maximumValue;
if (m_fMaximumValue <= m_fMinimumValue)
{
m_fMinimumValue = m_fMaximumValue - 1.0f;
}
setValue(m_fMinimumValue);
}
float CCControlPotentiometer::getMaximumValue()
{
return m_fMaximumValue;
}
bool CCControlPotentiometer::isTouchInside(CCTouch * touch)
{
CCPoint touchLocation = this->getTouchLocation(touch);
float distance = this->distanceBetweenPointAndPoint(m_pProgressTimer->getPosition(), touchLocation);
return distance < MIN(getContentSize().width / 2, getContentSize().height / 2);
}
bool CCControlPotentiometer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
if (!this->isTouchInside(pTouch)
|| !this->isEnabled())
{
return false;
}
m_tPreviousLocation = this->getTouchLocation(pTouch);
this->potentiometerBegan(m_tPreviousLocation);
return true;
}
void CCControlPotentiometer::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
CCPoint location = this->getTouchLocation(pTouch);
this->potentiometerMoved(location);
}
void CCControlPotentiometer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
this->potentiometerEnded(CCPointZero);
}
float CCControlPotentiometer::distanceBetweenPointAndPoint(CCPoint point1, CCPoint point2)
{
float dx = point1.x - point2.x;
float dy = point1.y - point2.y;
return sqrt(dx*dx + dy*dy);
}
float CCControlPotentiometer::angleInDegreesBetweenLineFromPoint_toPoint_toLineFromPoint_toPoint(
CCPoint beginLineA,
CCPoint endLineA,
CCPoint beginLineB,
CCPoint endLineB)
{
float a = endLineA.x - beginLineA.x;
float b = endLineA.y - beginLineA.y;
float c = endLineB.x - beginLineB.x;
float d = endLineB.y - beginLineB.y;
float atanA = atan2(a, b);
float atanB = atan2(c, d);
// convert radiants to degrees
return (atanA - atanB) * 180 / M_PI;
}
void CCControlPotentiometer::potentiometerBegan(CCPoint location)
{
setSelected(true);
getThumbSprite()->setColor(ccGRAY);
}
void CCControlPotentiometer::potentiometerMoved(CCPoint location)
{
float angle = this->angleInDegreesBetweenLineFromPoint_toPoint_toLineFromPoint_toPoint(
m_pProgressTimer->getPosition(),
location,
m_pProgressTimer->getPosition(),
m_tPreviousLocation);
// fix value, if the 12 o'clock position is between location and previousLocation
if (angle > 180)
{
angle -= 360;
}
else if (angle < -180)
{
angle += 360;
}
setValue(m_fValue + angle / 360.0f * (m_fMaximumValue - m_fMinimumValue));
m_tPreviousLocation = location;
}
void CCControlPotentiometer::potentiometerEnded(CCPoint location)
{
getThumbSprite()->setColor(ccWHITE);
setSelected(false);
}
NS_CC_EXT_END

View File

@ -0,0 +1,109 @@
/*
* CCControlPotentiometer.h
*
* Copyright 2012 Yannick Loriot. All rights reserved.
* 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
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#ifndef __CCCONTROLPOTENTIOMETER_H__
#define __CCCONTROLPOTENTIOMETER_H__
#include "CCControl.h"
NS_CC_EXT_BEGIN
/**
* @addtogroup GUI
* @{
* @addtogroup control_extension
* @{
*/
/** @class CCControlPotentiometer Potentiometer control for Cocos2D. */
class CCControlPotentiometer : public CCControl
{
public:
CCControlPotentiometer();
virtual ~CCControlPotentiometer();
/**
* Creates potentiometer with a track filename and a progress filename.
*/
static CCControlPotentiometer* create(const char* backgroundFile, const char* progressFile, const char* thumbFile);
/**
* Initializes a potentiometer with a track sprite and a progress bar.
*
* @param trackSprite CCSprite, that is used as a background.
* @param progressSprite CCProgressTimer, that is used as a progress bar.
*/
bool initWithTrackSprite_ProgressTimer_ThumbSprite(CCSprite* trackSprite, CCProgressTimer* progressTimer, CCSprite* thumbSprite);
void setValue(float value);
float getValue();
void setMinimumValue(float minimumValue);
float getMinimumValue();
void setMaximumValue(float maximumValue);
float getMaximumValue();
void setEnabled(bool enabled);
virtual bool isTouchInside(CCTouch * touch);
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
protected:
CC_SYNTHESIZE_RETAIN(CCSprite*, m_pThumbSprite, ThumbSprite)
CC_SYNTHESIZE_RETAIN(CCProgressTimer*, m_pProgressTimer, ProgressTimer)
CC_SYNTHESIZE(CCPoint, m_tPreviousLocation, PreviousLocation)
/** Contains the receivers current value. */
float m_fValue;
/** Contains the minimum value of the receiver.
* The default value of this property is 0.0. */
float m_fMinimumValue;
/** Contains the maximum value of the receiver.
* The default value of this property is 1.0. */
float m_fMaximumValue;
/** Factorize the event dispath into these methods. */
void potentiometerBegan(CCPoint location);
void potentiometerMoved(CCPoint location);
void potentiometerEnded(CCPoint location);
/** Returns the distance between the point1 and point2. */
float distanceBetweenPointAndPoint(CCPoint point1, CCPoint point2);
/** Returns the angle in degree between line1 and line2. */
float angleInDegreesBetweenLineFromPoint_toPoint_toLineFromPoint_toPoint(
CCPoint beginLineA,
CCPoint endLineA,
CCPoint beginLineB,
CCPoint endLineB);
};
// end of GUI group
/// @}
/// @}
NS_CC_EXT_END
#endif /* __CCCONTROLPOTENTIOMETER_H__ */

View File

@ -33,10 +33,28 @@
NS_CC_EXT_BEGIN NS_CC_EXT_BEGIN
CCControlSaturationBrightnessPicker::~CCControlSaturationBrightnessPicker() CCControlSaturationBrightnessPicker::CCControlSaturationBrightnessPicker()
: m_saturation(0.0f)
, m_brightness(0.0f)
, m_background(NULL)
, m_overlay(NULL)
, m_shadow(NULL)
, m_slider(NULL)
, boxPos(0)
, boxSize(0)
{ {
} }
CCControlSaturationBrightnessPicker::~CCControlSaturationBrightnessPicker()
{
removeAllChildrenWithCleanup(true);
m_background = NULL;
m_overlay = NULL;
m_shadow = NULL;
m_slider = NULL;
}
bool CCControlSaturationBrightnessPicker::initWithTargetAndPos(CCNode* target, CCPoint pos) bool CCControlSaturationBrightnessPicker::initWithTargetAndPos(CCNode* target, CCPoint pos)
{ {
@ -51,11 +69,13 @@ bool CCControlSaturationBrightnessPicker::initWithTargetAndPos(CCNode* target, C
m_startPos=pos; // starting position of the colour picker m_startPos=pos; // starting position of the colour picker
boxPos = 35; // starting position of the virtual box area for picking a colour boxPos = 35; // starting position of the virtual box area for picking a colour
boxSize = 150; // the size (width and height) of the virtual box for picking a colour from boxSize = m_background->getContentSize().width / 2;; // the size (width and height) of the virtual box for picking a colour from
return true; return true;
} }
else else
{
return false; return false;
}
} }
CCControlSaturationBrightnessPicker* CCControlSaturationBrightnessPicker::pickerWithTargetAndPos(CCNode* target, CCPoint pos) CCControlSaturationBrightnessPicker* CCControlSaturationBrightnessPicker::pickerWithTargetAndPos(CCNode* target, CCPoint pos)
@ -71,7 +91,14 @@ CCControlSaturationBrightnessPicker* CCControlSaturationBrightnessPicker::create
return pRet; return pRet;
} }
void CCControlSaturationBrightnessPicker::setEnabled(bool enabled)
{
CCControl::setEnabled(enabled);
if (m_slider != NULL)
{
m_slider->setOpacity(enabled ? 255 : 128);
}
}
void CCControlSaturationBrightnessPicker::updateWithHSV(HSV hsv) void CCControlSaturationBrightnessPicker::updateWithHSV(HSV hsv)
{ {
@ -149,7 +176,7 @@ bool CCControlSaturationBrightnessPicker::checkSliderPosition(CCPoint location)
float dist = sqrtf(dx*dx+dy*dy); float dist = sqrtf(dx*dx+dy*dy);
// check that the touch location is within the bounding rectangle before sending updates // check that the touch location is within the bounding rectangle before sending updates
if (dist <= m_background->boundingBox().size.width*.5) if (dist <= m_background->boundingBox().size.width*0.5f)
{ {
updateSliderPosition(location); updateSliderPosition(location);
sendActionsForControlEvents(CCControlEventValueChanged); sendActionsForControlEvents(CCControlEventValueChanged);
@ -161,6 +188,11 @@ bool CCControlSaturationBrightnessPicker::checkSliderPosition(CCPoint location)
bool CCControlSaturationBrightnessPicker::ccTouchBegan(CCTouch* touch, CCEvent* event) bool CCControlSaturationBrightnessPicker::ccTouchBegan(CCTouch* touch, CCEvent* event)
{ {
if (!isEnabled())
{
return false;
}
// Get the touch location // Get the touch location
CCPoint touchLocation=getTouchLocation(touch); CCPoint touchLocation=getTouchLocation(touch);
@ -175,10 +207,10 @@ void CCControlSaturationBrightnessPicker::ccTouchMoved(CCTouch* touch, CCEvent*
CCPoint touchLocation=getTouchLocation(touch); CCPoint touchLocation=getTouchLocation(touch);
//small modification: this allows changing of the colour, even if the touch leaves the bounding area //small modification: this allows changing of the colour, even if the touch leaves the bounding area
updateSliderPosition(touchLocation); // updateSliderPosition(touchLocation);
sendActionsForControlEvents(CCControlEventValueChanged); // sendActionsForControlEvents(CCControlEventValueChanged);
// Check the touch position on the slider // Check the touch position on the slider
//checkSliderPosition(touchLocation); checkSliderPosition(touchLocation);
} }
NS_CC_EXT_END NS_CC_EXT_END

View File

@ -46,7 +46,9 @@ NS_CC_EXT_BEGIN
class CCControlSaturationBrightnessPicker : public CCControl class CCControlSaturationBrightnessPicker : public CCControl
{ {
/** Contains the receiver¡¯s current saturation value. */
CC_SYNTHESIZE_READONLY(float, m_saturation, Saturation); CC_SYNTHESIZE_READONLY(float, m_saturation, Saturation);
/** Contains the receiver¡¯s current brightness value. */
CC_SYNTHESIZE_READONLY(float, m_brightness, Brightness); CC_SYNTHESIZE_READONLY(float, m_brightness, Brightness);
//not sure if these need to be there actually. I suppose someone might want to access the sprite? //not sure if these need to be there actually. I suppose someone might want to access the sprite?
@ -61,12 +63,15 @@ protected:
int boxSize; int boxSize;
public: public:
CCControlSaturationBrightnessPicker();
virtual ~CCControlSaturationBrightnessPicker(); virtual ~CCControlSaturationBrightnessPicker();
virtual bool initWithTargetAndPos(CCNode* target, CCPoint pos); virtual bool initWithTargetAndPos(CCNode* target, CCPoint pos);
//@deprecated: This interface will be deprecated sooner or later. //@deprecated: This interface will be deprecated sooner or later.
CC_DEPRECATED_ATTRIBUTE static CCControlSaturationBrightnessPicker* pickerWithTargetAndPos(CCNode* target, CCPoint pos); CC_DEPRECATED_ATTRIBUTE static CCControlSaturationBrightnessPicker* pickerWithTargetAndPos(CCNode* target, CCPoint pos);
static CCControlSaturationBrightnessPicker* create(CCNode* target, CCPoint pos); static CCControlSaturationBrightnessPicker* create(CCNode* target, CCPoint pos);
virtual void setEnabled(bool enabled);
virtual void updateWithHSV(HSV hsv); virtual void updateWithHSV(HSV hsv);
virtual void updateDraggerWithHSV(HSV hsv); virtual void updateDraggerWithHSV(HSV hsv);

View File

@ -33,11 +33,26 @@
NS_CC_EXT_BEGIN NS_CC_EXT_BEGIN
CCControlSlider::~CCControlSlider() CCControlSlider::CCControlSlider()
: m_value(0.0f)
, m_minimumValue(0.0f)
, m_maximumValue(0.0f)
, m_minimumAllowedValue(0.0f)
, m_maximumAllowedValue(0.0f)
, m_thumbSprite(NULL)
, m_progressSprite(NULL)
, m_backgroundSprite(NULL)
{ {
} }
CCControlSlider::~CCControlSlider()
{
CC_SAFE_RELEASE(m_thumbSprite);
CC_SAFE_RELEASE(m_progressSprite);
CC_SAFE_RELEASE(m_backgroundSprite);
}
CCControlSlider* CCControlSlider::sliderWithFiles(const char* bgFile, const char* progressFile, const char* thumbFile) CCControlSlider* CCControlSlider::sliderWithFiles(const char* bgFile, const char* progressFile, const char* thumbFile)
{ {
return CCControlSlider::create(bgFile, progressFile, thumbFile); return CCControlSlider::create(bgFile, progressFile, thumbFile);
@ -52,62 +67,62 @@ CCControlSlider* CCControlSlider::create(const char* bgFile, const char* progres
CCSprite *progressSprite = CCSprite::create(progressFile); CCSprite *progressSprite = CCSprite::create(progressFile);
// Prepare thumb (menuItem) for slider // Prepare thumb (menuItem) for slider
CCSprite *thumbNormal = CCSprite::create(thumbFile); CCSprite *thumbSprite = CCSprite::create(thumbFile);
CCSprite *thumbSelected = CCSprite::create(thumbFile);
thumbSelected->setColor(ccGRAY);
CCMenuItemSprite* thumbMenuItem =CCMenuItemSprite::create(thumbNormal, thumbSelected); return CCControlSlider::create(backgroundSprite, progressSprite, thumbSprite);
return CCControlSlider::create(backgroundSprite, progressSprite, thumbMenuItem);
} }
CCControlSlider* CCControlSlider::sliderWithSprites(CCSprite * backgroundSprite, CCSprite* pogressSprite, CCMenuItem* thumbItem) CCControlSlider* CCControlSlider::sliderWithSprites(CCSprite * backgroundSprite, CCSprite* pogressSprite, CCSprite* thumbSprite)
{ {
return CCControlSlider::create(backgroundSprite, pogressSprite, thumbItem); return CCControlSlider::create(backgroundSprite, pogressSprite, thumbSprite);
} }
CCControlSlider* CCControlSlider::create(CCSprite * backgroundSprite, CCSprite* pogressSprite, CCMenuItem* thumbItem) CCControlSlider* CCControlSlider::create(CCSprite * backgroundSprite, CCSprite* pogressSprite, CCSprite* thumbSprite)
{ {
CCControlSlider *pRet = new CCControlSlider(); CCControlSlider *pRet = new CCControlSlider();
pRet->initWithSprites(backgroundSprite, pogressSprite, thumbItem); pRet->initWithSprites(backgroundSprite, pogressSprite, thumbSprite);
pRet->autorelease(); pRet->autorelease();
return pRet; return pRet;
} }
bool CCControlSlider::initWithSprites(CCSprite * backgroundSprite, CCSprite* progessSprite, CCMenuItem* thumbItem) bool CCControlSlider::initWithSprites(CCSprite * backgroundSprite, CCSprite* progressSprite, CCSprite* thumbSprite)
{ {
if (CCControl::init()) if (CCControl::init())
{ {
CCAssert(backgroundSprite, "Background sprite must be not nil");
CCAssert(progressSprite, "Progress sprite must be not nil");
CCAssert(thumbSprite, "Thumb sprite must be not nil");
ignoreAnchorPointForPosition(false); ignoreAnchorPointForPosition(false);
setTouchEnabled(true); setTouchEnabled(true);
m_backgroundSprite=backgroundSprite; this->setBackgroundSprite(backgroundSprite);
m_progressSprite=progessSprite; this->setProgressSprite(progressSprite);
m_thumbItem=thumbItem; this->setThumbSprite(thumbSprite);
// Defines the content size // Defines the content size
CCRect maxRect = CCControlUtils::CCRectUnion(backgroundSprite->boundingBox(), thumbItem->boundingBox()); CCRect maxRect = CCControlUtils::CCRectUnion(backgroundSprite->boundingBox(), thumbSprite->boundingBox());
CCSize size=CCSizeMake(maxRect.size.width+2*SLIDER_MARGIN_H, maxRect.size.height+2*SLIDER_MARGIN_V);
setContentSize(size); setContentSize(CCSizeMake(maxRect.size.width, maxRect.size.height));
//setContentSize(CCSizeMake(backgroundSprite->getContentSize().width, thumbItem->getContentSize().height));
// Add the slider background // Add the slider background
m_backgroundSprite->setAnchorPoint(ccp(0.5f, 0.5f)); m_backgroundSprite->setAnchorPoint(ccp(0.5f, 0.5f));
m_backgroundSprite->setPosition(ccp(size.width / 2, size.height / 2)); m_backgroundSprite->setPosition(ccp(this->getContentSize().width / 2, this->getContentSize().height / 2));
addChild(m_backgroundSprite); addChild(m_backgroundSprite);
// Add the progress bar // Add the progress bar
m_progressSprite->setAnchorPoint(ccp(0.0f, 0.5f)); m_progressSprite->setAnchorPoint(ccp(0.0f, 0.5f));
m_progressSprite->setPosition(ccp(0.0f+SLIDER_MARGIN_H, size.height / 2)); m_progressSprite->setPosition(ccp(0.0f, this->getContentSize().height / 2));
addChild(m_progressSprite); addChild(m_progressSprite);
// Add the slider thumb // Add the slider thumb
m_thumbItem->setPosition(ccp(0+SLIDER_MARGIN_H, size.height / 2)); m_thumbSprite->setPosition(ccp(0.0f, this->getContentSize().height / 2));
addChild(m_thumbItem); addChild(m_thumbSprite);
// Init default values // Init default values
m_minimumValue = 0.0f; m_minimumValue = 0.0f;
m_maximumValue = 1.0f; m_maximumValue = 1.0f;
m_snappingInterval=-1.0f;
setValue(m_minimumValue); setValue(m_minimumValue);
return true; return true;
} }
@ -118,32 +133,33 @@ CCControlSlider* CCControlSlider::create(CCSprite * backgroundSprite, CCSprite*
} }
void CCControlSlider::setEnabled(bool enabled)
{
CCControl::setEnabled(enabled);
if (m_thumbSprite != NULL)
{
m_thumbSprite->setOpacity((enabled) ? 255 : 128);
}
}
void CCControlSlider::setValue(float value) void CCControlSlider::setValue(float value)
{ {
//clamp between the two bounds // set new value with sentinel
value=MAX(value, m_minimumValue); if (value < m_minimumValue)
value=MIN(value, m_maximumValue); {
value = m_minimumValue;
//if we're snapping }
if (m_snappingInterval>=0)
{ if (value > m_maximumValue)
//int nTotal=(int)(ceil(m_maximumValue-m_minimumValue)/m_snappingInterval); {
//floor (n + 0.5f) == round(n) value = m_maximumValue;
value=floor(0.5f + value/m_snappingInterval)*m_snappingInterval; }
}
m_value=value; m_value = value;
// Update thumb position for new value this->needsLayout();
float percent = (m_value - m_minimumValue) / (m_maximumValue - m_minimumValue);
CCPoint pos= m_thumbItem->getPosition(); this->sendActionsForControlEvents(CCControlEventValueChanged);
pos.x = percent * m_backgroundSprite->getContentSize().width+SLIDER_MARGIN_H;
m_thumbItem->setPosition(pos);
// Stretches content proportional to newLevel
CCRect textureRect = m_progressSprite->getTextureRect();
textureRect = CCRectMake(textureRect.origin.x, textureRect.origin.y, percent * m_backgroundSprite->getContentSize().width, textureRect.size.height);
m_progressSprite->setTextureRect(textureRect, m_progressSprite->isTextureRectRotated(), textureRect.size);
sendActionsForControlEvents(CCControlEventValueChanged);
} }
void CCControlSlider::setMinimumValue(float minimumValue) void CCControlSlider::setMinimumValue(float minimumValue)
@ -151,61 +167,96 @@ CCControlSlider* CCControlSlider::create(CCSprite * backgroundSprite, CCSprite*
m_minimumValue=minimumValue; m_minimumValue=minimumValue;
m_minimumAllowedValue = minimumValue; m_minimumAllowedValue = minimumValue;
if (m_minimumValue >= m_maximumValue) if (m_minimumValue >= m_maximumValue)
{
m_maximumValue = m_minimumValue + 1.0f; m_maximumValue = m_minimumValue + 1.0f;
}
setValue(m_value); setValue(m_value);
} }
void CCControlSlider::setMaximumValue(float maximumValue) void CCControlSlider::setMaximumValue(float maximumValue)
{ {
m_maximumValue=maximumValue; m_maximumValue=maximumValue;
m_maximumAllowedValue = maximumValue; m_maximumAllowedValue = maximumValue;
if (m_maximumValue <= m_minimumValue) if (m_maximumValue <= m_minimumValue)
{
m_minimumValue = m_maximumValue - 1.0f; m_minimumValue = m_maximumValue - 1.0f;
}
setValue(m_value); setValue(m_value);
} }
bool CCControlSlider::isTouchInside(CCTouch * touch)
{
CCPoint touchLocation = touch->getLocation();
touchLocation = this->getParent()->convertToNodeSpace(touchLocation);
CCRect rect = this->boundingBox();
rect.size.width += m_thumbSprite->getContentSize().width;
rect.origin.x -= m_thumbSprite->getContentSize().width / 2;
return rect.containsPoint(touchLocation);
}
CCPoint CCControlSlider::locationFromTouch(CCTouch* touch)
{
CCPoint touchLocation = touch->getLocation(); // Get the touch position
touchLocation = this->convertToNodeSpace(touchLocation); // Convert to the node space of this class
if (touchLocation.x < 0)
{
touchLocation.x = 0;
} else if (touchLocation.x > m_backgroundSprite->getContentSize().width)
{
touchLocation.x = m_backgroundSprite->getContentSize().width;
}
return touchLocation;
}
//this is the same as CCControl::getTouchLocation, but it returns the position relative to the position of this control
CCPoint CCControlSlider::getTouchLocationInControl(CCTouch* touch) bool CCControlSlider::ccTouchBegan(CCTouch* touch, CCEvent* pEvent)
{ {
CCPoint touchLocation = touch->getLocation();; // Get the touch position if (!isTouchInside(touch) || !isEnabled())
touchLocation = convertToNodeSpace(touchLocation); // Convert to the node space of this class
if (touchLocation.x < 0)
{
touchLocation.x = 0;
}
else if (touchLocation.x > m_backgroundSprite->getContentSize().width+SLIDER_MARGIN_H)
{
touchLocation.x = m_backgroundSprite->getContentSize().width+SLIDER_MARGIN_H;
}
return touchLocation;
}
bool CCControlSlider::ccTouchBegan(CCTouch* touch, CCEvent* pEvent)
{
if (!isTouchInside(touch))
return false; return false;
CCPoint location = getTouchLocationInControl(touch); CCPoint location = locationFromTouch(touch);
sliderBegan(location); sliderBegan(location);
return true; return true;
} }
void CCControlSlider::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent) void CCControlSlider::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{ {
CCPoint location = getTouchLocationInControl(pTouch); CCPoint location = locationFromTouch(pTouch);
sliderMoved(location); sliderMoved(location);
} }
void CCControlSlider::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) void CCControlSlider::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{ {
CCPoint location = getTouchLocationInControl(pTouch);
sliderEnded(CCPointZero); sliderEnded(CCPointZero);
} }
void CCControlSlider::needsLayout()
{
if (NULL == m_thumbSprite || NULL == m_backgroundSprite || NULL == m_progressSprite)
{
return;
}
// Update thumb position for new value
float percent = (m_value - m_minimumValue) / (m_maximumValue - m_minimumValue);
CCPoint pos = m_thumbSprite->getPosition();
pos.x = percent * m_backgroundSprite->getContentSize().width;
m_thumbSprite->setPosition(pos);
// Stretches content proportional to newLevel
CCRect textureRect = m_progressSprite->getTextureRect();
textureRect = CCRectMake(textureRect.origin.x, textureRect.origin.y, pos.x, textureRect.size.height);
m_progressSprite->setTextureRect(textureRect, m_progressSprite->isTextureRectRotated(), textureRect.size);
}
void CCControlSlider::sliderBegan(CCPoint location) void CCControlSlider::sliderBegan(CCPoint location)
{ {
m_thumbItem->selected(); this->setSelected(true);
this->getThumbSprite()->setColor(ccGRAY);
setValue(valueForLocation(location)); setValue(valueForLocation(location));
} }
@ -216,16 +267,17 @@ void CCControlSlider::sliderMoved(CCPoint location)
void CCControlSlider::sliderEnded(CCPoint location) void CCControlSlider::sliderEnded(CCPoint location)
{ {
if (m_thumbItem->isSelected()) if (this->isSelected())
{ {
m_thumbItem->unselected(); setValue(valueForLocation(m_thumbSprite->getPosition()));
setValue(valueForLocation(m_thumbItem->getPosition()));
} }
this->getThumbSprite()->setColor(ccWHITE);
this->setSelected(false);
} }
float CCControlSlider::valueForLocation(CCPoint location) float CCControlSlider::valueForLocation(CCPoint location)
{ {
float percent = (location.x-SLIDER_MARGIN_H)/ m_backgroundSprite->getContentSize().width; float percent = location.x/ m_backgroundSprite->getContentSize().width;
return MAX(MIN(m_minimumValue + percent * (m_maximumValue - m_minimumValue), m_maximumAllowedValue), m_minimumAllowedValue); return MAX(MIN(m_minimumValue + percent * (m_maximumValue - m_minimumValue), m_maximumAllowedValue), m_minimumAllowedValue);
} }

View File

@ -31,11 +31,6 @@
#include "CCControl.h" #include "CCControl.h"
#include "CCInvocation.h" #include "CCInvocation.h"
#include "sprite_nodes/CCSprite.h"
#include "menu_nodes/CCMenuItem.h"
#define SLIDER_MARGIN_H 24
#define SLIDER_MARGIN_V 8
NS_CC_EXT_BEGIN NS_CC_EXT_BEGIN
@ -49,27 +44,31 @@ NS_CC_EXT_BEGIN
class CCControlSlider: public CCControl class CCControlSlider: public CCControl
{ {
//maunally put in the setters //maunally put in the setters
/** Contains the receiver¡¯s current value. */
CC_SYNTHESIZE_READONLY(float, m_value, Value); CC_SYNTHESIZE_READONLY(float, m_value, Value);
virtual void setValue(float val); virtual void setValue(float val);
/** Contains the minimum value of the receiver.
* The default value of this property is 0.0. */
CC_SYNTHESIZE_READONLY(float, m_minimumValue, MinimumValue); CC_SYNTHESIZE_READONLY(float, m_minimumValue, MinimumValue);
virtual void setMinimumValue(float val); virtual void setMinimumValue(float val);
/** Contains the maximum value of the receiver.
* The default value of this property is 1.0. */
CC_SYNTHESIZE_READONLY(float, m_maximumValue, MaximumValue); CC_SYNTHESIZE_READONLY(float, m_maximumValue, MaximumValue);
virtual void setMaximumValue(float val); virtual void setMaximumValue(float val);
virtual void setEnabled(bool enabled);
virtual bool isTouchInside(CCTouch * touch);
CCPoint locationFromTouch(CCTouch* touch);
CC_SYNTHESIZE(float, m_minimumAllowedValue, MinimumAllowedValue); CC_SYNTHESIZE(float, m_minimumAllowedValue, MinimumAllowedValue);
CC_SYNTHESIZE(float, m_maximumAllowedValue, MaximumAllowedValue); CC_SYNTHESIZE(float, m_maximumAllowedValue, MaximumAllowedValue);
//interval to snap to
CC_SYNTHESIZE(float, m_snappingInterval, SnappingInterval);
// maybe this should be read-only // maybe this should be read-only
CC_SYNTHESIZE_READONLY(CCMenuItem*, m_thumbItem, ThumbItem); CC_SYNTHESIZE_RETAIN(CCSprite*, m_thumbSprite, ThumbSprite);
CC_SYNTHESIZE_READONLY(CCSprite*, m_progressSprite, ProgressSprite); CC_SYNTHESIZE_RETAIN(CCSprite*, m_progressSprite, ProgressSprite);
CC_SYNTHESIZE_READONLY(CCSprite*, m_backgroundSprite, BackgroundSprite); CC_SYNTHESIZE_RETAIN(CCSprite*, m_backgroundSprite, BackgroundSprite);
public: public:
CCControlSlider();
virtual ~CCControlSlider(); virtual ~CCControlSlider();
/** /**
@ -78,9 +77,9 @@ public:
* *
* @param backgroundSprite CCSprite, that is used as a background. * @param backgroundSprite CCSprite, that is used as a background.
* @param progressSprite CCSprite, that is used as a progress bar. * @param progressSprite CCSprite, that is used as a progress bar.
* @param thumbItem CCMenuItem, that is used as a thumb. * @param thumbItem CCSprite, that is used as a thumb.
*/ */
virtual bool initWithSprites(CCSprite * backgroundSprite, CCSprite* progessSprite, CCMenuItem* thumbItem); virtual bool initWithSprites(CCSprite * backgroundSprite, CCSprite* progressSprite, CCSprite* thumbSprite);
/** /**
@ -96,7 +95,7 @@ public:
*@deprecated: This interface will be deprecated sooner or later. *@deprecated: This interface will be deprecated sooner or later.
* @see initWithBackgroundSprite:progressSprite:thumbMenuItem: * @see initWithBackgroundSprite:progressSprite:thumbMenuItem:
*/ */
CC_DEPRECATED_ATTRIBUTE static CCControlSlider* sliderWithSprites(CCSprite * backgroundSprite, CCSprite* pogressSprite, CCMenuItem* thumbItem); CC_DEPRECATED_ATTRIBUTE static CCControlSlider* sliderWithSprites(CCSprite * backgroundSprite, CCSprite* pogressSprite, CCSprite* thumbSprite);
/** /**
@ -111,22 +110,18 @@ public:
* *
* @see initWithBackgroundSprite:progressSprite:thumbMenuItem: * @see initWithBackgroundSprite:progressSprite:thumbMenuItem:
*/ */
static CCControlSlider* create(CCSprite * backgroundSprite, CCSprite* pogressSprite, CCMenuItem* thumbItem); static CCControlSlider* create(CCSprite * backgroundSprite, CCSprite* pogressSprite, CCSprite* thumbSprite);
virtual void needsLayout();
protected: protected:
void sliderBegan(CCPoint location); void sliderBegan(CCPoint location);
void sliderMoved(CCPoint location); void sliderMoved(CCPoint location);
void sliderEnded(CCPoint location); void sliderEnded(CCPoint location);
virtual CCPoint getTouchLocationInControl(CCTouch* touch);
virtual bool ccTouchBegan(CCTouch* touch, CCEvent* pEvent); virtual bool ccTouchBegan(CCTouch* touch, CCEvent* pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent); virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
/** Returns the value for the given location. */ /** Returns the value for the given location. */
float valueForLocation(CCPoint location); float valueForLocation(CCPoint location);
}; };

View File

@ -0,0 +1,347 @@
/*
* CCControlStepper.m
*
* Copyright 2012 Yannick Loriot. All rights reserved.
* 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 falseT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND falseNINFRINGEMENT. IN false EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include "CCControlStepper.h"
NS_CC_EXT_BEGIN
#define CCControlStepperLabelColorEnabled ccc3(55, 55, 55)
#define CCControlStepperLabelColorDisabled ccc3(147, 147, 147)
#define CCControlStepperLabelFont "CourierNewPSMT"
#define kAutorepeatDeltaTime 0.15f
#define kAutorepeatIncreaseTimeIncrement 12
CCControlStepper::CCControlStepper()
: m_dValue(0.0)
, m_bContinuous(false)
, m_bAutorepeat(false)
, m_bWraps(false)
, m_dMinimumValue(0.0)
, m_dMaximumValue(0.0)
, m_dStepValue(0.0)
, m_pMinusSprite(NULL)
, m_pPlusSprite(NULL)
, m_pMinusLabel(NULL)
, m_pPlusLabel(NULL)
, m_bTouchInsideFlag(false)
, m_eTouchedPart(kCCControlStepperPartNone)
, m_nAutorepeatCount(0)
{
}
CCControlStepper::~CCControlStepper()
{
unscheduleAllSelectors();
CC_SAFE_RELEASE(m_pMinusSprite);
CC_SAFE_RELEASE(m_pPlusSprite);
CC_SAFE_RELEASE(m_pMinusLabel);
CC_SAFE_RELEASE(m_pPlusLabel);
}
bool CCControlStepper::initWithMinusSpriteAndPlusSprite(CCSprite *minusSprite, CCSprite *plusSprite)
{
if (CCControl::init())
{
CCAssert(minusSprite, "Minus sprite must be not nil");
CCAssert(plusSprite, "Plus sprite must be not nil");
setTouchEnabled(true);
// Set the default values
m_bAutorepeat = true;
m_bContinuous = true;
m_dMinimumValue = 0;
m_dMaximumValue = 100;
m_dValue = 0;
m_dStepValue = 1;
m_bWraps = false;
this->ignoreAnchorPointForPosition( false );
// Add the minus components
this->setMinusSprite(minusSprite);
m_pMinusSprite->setPosition( ccp(minusSprite->getContentSize().width / 2, minusSprite->getContentSize().height / 2) );
this->addChild(m_pMinusSprite);
this->setMinusLabel( CCLabelTTF::create("-", CCControlStepperLabelFont, 40));
m_pMinusLabel->setColor(CCControlStepperLabelColorDisabled);
m_pMinusLabel->setPosition(CCPointMake(m_pMinusSprite->getContentSize().width / 2, m_pMinusSprite->getContentSize().height / 2) );
m_pMinusSprite->addChild(m_pMinusLabel);
// Add the plus components
this->setPlusSprite( plusSprite );
m_pPlusSprite->setPosition( ccp(minusSprite->getContentSize().width + plusSprite->getContentSize().width / 2,
minusSprite->getContentSize().height / 2) );
this->addChild(m_pPlusSprite);
this->setPlusLabel( CCLabelTTF::create("+", CCControlStepperLabelFont, 40 ));
m_pPlusLabel->setColor( CCControlStepperLabelColorEnabled );
m_pPlusLabel->setPosition( CCPointMake(m_pPlusSprite->getContentSize().width / 2, m_pPlusSprite->getContentSize().height / 2) );
m_pPlusSprite->addChild(m_pPlusLabel);
// Defines the content size
CCRect maxRect = CCControlUtils::CCRectUnion(m_pMinusSprite->boundingBox(), m_pPlusSprite->boundingBox());
this->setContentSize( CCSizeMake(m_pMinusSprite->getContentSize().width + m_pPlusSprite->getContentSize().height, maxRect.size.height) );
return true;
}
return false;
}
CCControlStepper* CCControlStepper::create(CCSprite *minusSprite, CCSprite *plusSprite)
{
CCControlStepper* pRet = new CCControlStepper();
if (pRet != NULL && pRet->initWithMinusSpriteAndPlusSprite(minusSprite, plusSprite))
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
}
return pRet;
}
//#pragma mark Properties
void CCControlStepper::setWraps(bool wraps)
{
m_bWraps = wraps;
if (m_bWraps)
{
m_pMinusLabel->setColor( CCControlStepperLabelColorEnabled );
m_pPlusLabel->setColor(CCControlStepperLabelColorEnabled );
}
this->setValue( m_dValue );
}
void CCControlStepper::setMinimumValue(double minimumValue)
{
if (minimumValue >= m_dMaximumValue)
{
CCAssert(0, "Must be numerically less than maximumValue.");
}
m_dMinimumValue = minimumValue;
this->setValue( m_dValue );
}
void CCControlStepper::setMaximumValue(double maximumValue)
{
if (maximumValue <= m_dMinimumValue)
{
CCAssert(0, "Must be numerically greater than minimumValue.");
}
m_dMaximumValue = maximumValue;
this->setValue(m_dValue);
}
void CCControlStepper::setValue(double value)
{
this->setValueWithSendingEvent(value, true);
}
double CCControlStepper::getValue()
{
return m_dValue;
}
void CCControlStepper::setStepValue(double stepValue)
{
if (stepValue <= 0)
{
CCAssert(0,"Must be numerically greater than 0.");
}
m_dStepValue = stepValue;
}
bool CCControlStepper::isContinuous()
{
return m_bContinuous;
}
//#pragma mark -
//#pragma mark CCControlStepper Public Methods
void CCControlStepper::setValueWithSendingEvent(double value, bool send)
{
if (value < m_dMinimumValue)
{
value = m_bWraps ? m_dMaximumValue : m_dMinimumValue;
} else if (value > m_dMaximumValue)
{
value = m_bWraps ? m_dMinimumValue : m_dMaximumValue;
}
m_dValue = value;
if (!m_bWraps)
{
m_pMinusLabel->setColor((value == m_dMinimumValue) ? CCControlStepperLabelColorDisabled : CCControlStepperLabelColorEnabled);
m_pPlusLabel->setColor((value == m_dMaximumValue) ? CCControlStepperLabelColorDisabled : CCControlStepperLabelColorEnabled);
}
if (send)
{
this->sendActionsForControlEvents(CCControlEventValueChanged);
}
}
void CCControlStepper::startAutorepeat()
{
m_nAutorepeatCount = -1;
this->schedule(schedule_selector(CCControlStepper::update), kAutorepeatDeltaTime, kCCRepeatForever, kAutorepeatDeltaTime * 3);
}
/** Stop the autorepeat. */
void CCControlStepper::stopAutorepeat()
{
this->unschedule(schedule_selector(CCControlStepper::update));
}
void CCControlStepper::update(float dt)
{
m_nAutorepeatCount++;
if ((m_nAutorepeatCount < kAutorepeatIncreaseTimeIncrement) && (m_nAutorepeatCount % 3) != 0)
return;
if (m_eTouchedPart == kCCControlStepperPartMinus)
{
this->setValueWithSendingEvent(m_dValue - m_dStepValue, m_bContinuous);
} else if (m_eTouchedPart == kCCControlStepperPartPlus)
{
this->setValueWithSendingEvent(m_dValue + m_dStepValue, m_bContinuous);
}
}
//#pragma mark CCControlStepper Private Methods
void CCControlStepper::updateLayoutUsingTouchLocation(CCPoint location)
{
if (location.x < m_pMinusSprite->getContentSize().width
&& m_dValue > m_dMinimumValue)
{
m_eTouchedPart = kCCControlStepperPartMinus;
m_pMinusSprite->setColor(ccGRAY);
m_pPlusSprite->setColor(ccWHITE);
} else if (location.x >= m_pMinusSprite->getContentSize().width
&& m_dValue < m_dMaximumValue)
{
m_eTouchedPart = kCCControlStepperPartPlus;
m_pMinusSprite->setColor(ccWHITE);
m_pPlusSprite->setColor(ccGRAY);
} else
{
m_eTouchedPart = kCCControlStepperPartNone;
m_pMinusSprite->setColor(ccWHITE);
m_pPlusSprite->setColor(ccWHITE);
}
}
bool CCControlStepper::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
if (!this->isTouchInside(pTouch)
|| !this->isEnabled())
{
return false;
}
CCPoint location = this->getTouchLocation(pTouch);
this->updateLayoutUsingTouchLocation(location);
m_bTouchInsideFlag = true;
if (m_bAutorepeat)
{
this->startAutorepeat();
}
return true;
}
void CCControlStepper::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
if (this->isTouchInside(pTouch))
{
CCPoint location = this->getTouchLocation(pTouch);
this->updateLayoutUsingTouchLocation(location);
if (!m_bTouchInsideFlag)
{
m_bTouchInsideFlag = true;
if (m_bAutorepeat)
{
this->startAutorepeat();
}
}
} else
{
m_bTouchInsideFlag = false;
m_eTouchedPart = kCCControlStepperPartNone;
m_pMinusSprite->setColor(ccWHITE);
m_pPlusSprite->setColor(ccWHITE);
if (m_bAutorepeat)
{
this->stopAutorepeat();
}
}
}
void CCControlStepper::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
m_pMinusSprite->setColor(ccWHITE);
m_pPlusSprite->setColor(ccWHITE);
if (m_bAutorepeat)
{
this->stopAutorepeat();
}
if (this->isTouchInside(pTouch))
{
CCPoint location = this->getTouchLocation(pTouch);
this->setValue(m_dValue + ((location.x < m_pMinusSprite->getContentSize().width) ? (0.0-m_dStepValue) : m_dStepValue));
}
}
NS_CC_EXT_END

View File

@ -0,0 +1,115 @@
/*
* CCControlStepper.h
*
* Copyright 2012 Yannick Loriot. All rights reserved.
* 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
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#ifndef __CCCONTROLSTEPPER_H__
#define __CCCONTROLSTEPPER_H__
#include "CCControl.h"
NS_CC_EXT_BEGIN
/**
* @addtogroup GUI
* @{
* @addtogroup control_extension
* @{
*/
typedef enum
{
kCCControlStepperPartMinus,
kCCControlStepperPartPlus,
kCCControlStepperPartNone,
} CCControlStepperPart;
class CCControlStepper : public CCControl
{
public:
CCControlStepper();
virtual ~CCControlStepper();
bool initWithMinusSpriteAndPlusSprite(CCSprite *minusSprite, CCSprite *plusSprite);
static CCControlStepper* create(CCSprite *minusSprite, CCSprite *plusSprite);
virtual void setWraps(bool wraps);
virtual void setMinimumValue(double minimumValue);
virtual void setMaximumValue(double maximumValue);
virtual void setValue(double value);
virtual double getValue();
virtual void setStepValue(double stepValue);
virtual void setValueWithSendingEvent(double value, bool send);
virtual bool isContinuous();
void update(float dt);
//events
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
protected:
// Weak links to children
CC_SYNTHESIZE_RETAIN(CCSprite*, m_pMinusSprite, MinusSprite)
CC_SYNTHESIZE_RETAIN(CCSprite*, m_pPlusSprite, PlusSprite)
CC_SYNTHESIZE_RETAIN(CCLabelTTF*, m_pMinusLabel, MinusLabel)
CC_SYNTHESIZE_RETAIN(CCLabelTTF*, m_pPlusLabel, PlusLabel)
/** Update the layout of the stepper with the given touch location. */
void updateLayoutUsingTouchLocation(CCPoint location);
/** Set the numeric value of the stepper. If send is true, the CCControlEventValueChanged is sent. */
void setValue(double value, bool send);
/** Start the autorepeat increment/decrement. */
void startAutorepeat();
/** Stop the autorepeat. */
void stopAutorepeat();
/** The numeric value of the stepper. */
double m_dValue;
/** The continuous vs. noncontinuous state of the stepper. */
bool m_bContinuous;
/** The automatic vs. nonautomatic repeat state of the stepper. */
bool m_bAutorepeat;
/** The wrap vs. no-wrap state of the stepper. */
bool m_bWraps;
/** The lowest possible numeric value for the stepper. */
double m_dMinimumValue;
/** The highest possible numeric value for the stepper. */
double m_dMaximumValue;
/** The step, or increment, value for the stepper. */
double m_dStepValue;
bool m_bTouchInsideFlag;
CCControlStepperPart m_eTouchedPart;
int m_nAutorepeatCount;
};
// end of GUI group
/// @}
/// @}
NS_CC_EXT_END
#endif /* __CCCONTROLSTEPPER_H__ */

View File

@ -259,6 +259,10 @@ float CCControlSwitchSprite::offSideWidth()
// CCControlSwitch // CCControlSwitch
CCControlSwitch::CCControlSwitch() CCControlSwitch::CCControlSwitch()
: m_pSwitchSprite(NULL)
, m_fInitialTouchXPosition(0.0f)
, m_bMoved(false)
, m_bOn(false)
{ {
} }
@ -367,8 +371,10 @@ void CCControlSwitch::setOn(bool isOn, bool animated)
void CCControlSwitch::setEnabled(bool enabled) void CCControlSwitch::setEnabled(bool enabled)
{ {
m_bEnabled = enabled; m_bEnabled = enabled;
if (m_pSwitchSprite != NULL)
m_pSwitchSprite->setOpacity((enabled) ? 255 : 128); {
m_pSwitchSprite->setOpacity((enabled) ? 255 : 128);
}
} }
CCPoint CCControlSwitch::locationFromTouch(CCTouch* pTouch) CCPoint CCControlSwitch::locationFromTouch(CCTouch* pTouch)

View File

@ -2,6 +2,16 @@
NS_CC_EXT_BEGIN NS_CC_EXT_BEGIN
CCInvocation* CCInvocation::create(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvent)
{
CCInvocation* pRet = new CCInvocation(target, action, controlEvent);
if (pRet != NULL)
{
pRet->autorelease();
}
return pRet;
}
CCInvocation::CCInvocation(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvent) CCInvocation::CCInvocation(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvent)
{ {
m_target=target; m_target=target;

View File

@ -30,6 +30,7 @@ class CCInvocation : public CCObject
CC_SYNTHESIZE_READONLY(CCControlEvent, m_controlEvent, ControlEvent); CC_SYNTHESIZE_READONLY(CCControlEvent, m_controlEvent, ControlEvent);
public: public:
static CCInvocation* create(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvent);
CCInvocation(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvent); CCInvocation(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvent);
void invoke(CCObject* sender); void invoke(CCObject* sender);

View File

@ -1,465 +0,0 @@
#include "CCMenuPassive.h"
#include "CCDirector.h"
#include "support/CCPointExtension.h"
#include "menu_nodes/CCMenuItem.h"
#include <vector>
using namespace std;
NS_CC_EXT_BEGIN
enum
{
kDefaultPadding = 5,
};
//
//CCMenu
//
CCMenuPassive* CCMenuPassive::node()
{
return CCMenuPassive::create();
}
CCMenuPassive* CCMenuPassive::create()
{
return create(NULL, NULL);
}
CCMenuPassive * CCMenuPassive::menuWithItems(CCNode* item, ...)
{
va_list args;
va_start(args,item);
CCMenuPassive *pRet = new CCMenuPassive();
if (pRet && pRet->initWithItems(item, args))
{
pRet->autorelease();
va_end(args);
return pRet;
}
va_end(args);
CC_SAFE_DELETE(pRet);
return NULL;
}
CCMenuPassive * CCMenuPassive::create(CCNode* item, ...)
{
va_list args;
va_start(args,item);
CCMenuPassive *pRet = new CCMenuPassive();
if (pRet && pRet->initWithItems(item, args))
{
pRet->autorelease();
va_end(args);
return pRet;
}
va_end(args);
CC_SAFE_DELETE(pRet);
return NULL;
}
CCMenuPassive* CCMenuPassive::menuWithItem(CCNode* item)
{
return CCMenuPassive::createWithItem(item);
}
CCMenuPassive* CCMenuPassive::createWithItem(CCNode* item)
{
return create(item, NULL);
}
bool CCMenuPassive::initWithItems(CCNode* item, va_list args)
{
if (CCLayer::init())
{
//this->m_bIsTouchEnabled = false;
// menu in the center of the screen
CCSize s = CCDirector::sharedDirector()->getWinSize();
// Set the default anchor point
ignoreAnchorPointForPosition(true);
setAnchorPoint(ccp(0.5f, 0.5f));
this->setContentSize(s);
setPosition(ccp(s.width/2, s.height/2));
int z=0;
if (item)
{
this->addChild(item, z);
CCMenuItem *i = va_arg(args, CCMenuItem*);
while (i)
{
z++;
this->addChild(i, z);
i = va_arg(args, CCMenuItem*);
}
}
return true;
}
return false;
}
//Menu - Alignment
void CCMenuPassive::alignItemsVertically()
{
this->alignItemsVerticallyWithPadding(kDefaultPadding);
}
void CCMenuPassive::alignItemsVerticallyWithPadding(float padding)
{
float height = -padding;
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
if (pChild)
{
height += pChild->getContentSize().height * pChild->getScaleY() + padding;
}
}
}
float width=0;
float y = height / 2.0f;
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
if (pChild)
{
width=max(width, pChild->getContentSize().width);
pChild->setPosition(ccp(0, y - pChild->getContentSize().height * pChild->getScaleY() / 2.0f));
y -= pChild->getContentSize().height * pChild->getScaleY() + padding;
}
}
}
setContentSize(CCSizeMake(width, height));
}
void CCMenuPassive::alignItemsHorizontally(void)
{
this->alignItemsHorizontallyWithPadding(kDefaultPadding);
}
void CCMenuPassive::alignItemsHorizontallyWithPadding(float padding)
{
float width = -padding;
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
if (pChild)
{
width += pChild->getContentSize().width * pChild->getScaleX() + padding;
}
}
}
float height=0;
float x = -width / 2.0f;
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
if (pChild)
{
height=max(height, pChild->getContentSize().height);
pChild->setPosition(ccp(x + pChild->getContentSize().width * pChild->getScaleX() / 2.0f, 0));
x += pChild->getContentSize().width * pChild->getScaleX() + padding;
}
}
}
setContentSize(CCSizeMake(width, height));
}
void CCMenuPassive::alignItemsInColumns(unsigned int columns, ...)
{
va_list args;
va_start(args, columns);
this->alignItemsInColumns(columns, args);
va_end(args);
}
void CCMenuPassive::alignItemsInColumns(unsigned int columns, va_list args)
{
vector<unsigned int> rows;
while (columns)
{
rows.push_back(columns);
columns = va_arg(args, unsigned int);
}
int height = -5;
unsigned int row = 0;
unsigned int rowHeight = 0;
unsigned int columnsOccupied = 0;
unsigned int rowColumns;
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
if (pChild)
{
CCAssert(row < rows.size(), "");
rowColumns = rows[row];
// can not have zero columns on a row
CCAssert(rowColumns, "");
float tmp = pChild->getContentSize().height;
rowHeight = (unsigned int)((rowHeight >= tmp || isnan(tmp)) ? rowHeight : tmp);
++columnsOccupied;
if (columnsOccupied >= rowColumns)
{
height += rowHeight + 5;
columnsOccupied = 0;
rowHeight = 0;
++row;
}
}
}
}
// check if too many rows/columns for available menu items
CCAssert(! columnsOccupied, "");
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
row = 0;
rowHeight = 0;
rowColumns = 0;
float w = 0.0;
float x = 0.0;
float y = (float)(height / 2);
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
if (pChild)
{
if (rowColumns == 0)
{
rowColumns = rows[row];
w = winSize.width / (1 + rowColumns);
x = w;
}
float tmp = pChild->getContentSize().height;
rowHeight = (unsigned int)((rowHeight >= tmp || isnan(tmp)) ? rowHeight : tmp);
pChild->setPosition(ccp(x - winSize.width / 2,
y - pChild->getContentSize().height / 2));
x += w;
++columnsOccupied;
if (columnsOccupied >= rowColumns)
{
y -= rowHeight + 5;
columnsOccupied = 0;
rowColumns = 0;
rowHeight = 0;
++row;
}
}
}
}
}
void CCMenuPassive::alignItemsInRows(unsigned int rows, ...)
{
va_list args;
va_start(args, rows);
this->alignItemsInRows(rows, args);
va_end(args);
}
void CCMenuPassive::alignItemsInRows(unsigned int rows, va_list args)
{
vector<unsigned int> columns;
while (rows)
{
columns.push_back(rows);
rows = va_arg(args, unsigned int);
}
vector<unsigned int> columnWidths;
vector<unsigned int> columnHeights;
int width = -10;
int columnHeight = -5;
unsigned int column = 0;
unsigned int columnWidth = 0;
unsigned int rowsOccupied = 0;
unsigned int columnRows;
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
if (pChild)
{
// check if too many menu items for the amount of rows/columns
CCAssert(column < columns.size(), "");
columnRows = columns[column];
// can't have zero rows on a column
CCAssert(columnRows, "");
// columnWidth = fmaxf(columnWidth, [item contentSize].width);
float tmp = pChild->getContentSize().width;
columnWidth = (unsigned int)((columnWidth >= tmp || isnan(tmp)) ? columnWidth : tmp);
columnHeight += (int)(pChild->getContentSize().height + 5);
++rowsOccupied;
if (rowsOccupied >= columnRows)
{
columnWidths.push_back(columnWidth);
columnHeights.push_back(columnHeight);
width += columnWidth + 10;
rowsOccupied = 0;
columnWidth = 0;
columnHeight = -5;
++column;
}
}
}
}
// check if too many rows/columns for available menu items.
CCAssert(! rowsOccupied, "");
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
column = 0;
columnWidth = 0;
columnRows = 0;
float x = (float)(-width / 2);
float y = 0.0;
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
if (pChild)
{
if (columnRows == 0)
{
columnRows = columns[column];
y = (float) columnHeights[column];
}
// columnWidth = fmaxf(columnWidth, [item contentSize].width);
float tmp = pChild->getContentSize().width;
columnWidth = (unsigned int)((columnWidth >= tmp || isnan(tmp)) ? columnWidth : tmp);
pChild->setPosition(ccp(x + columnWidths[column] / 2,
y - winSize.height / 2));
y -= pChild->getContentSize().height + 10;
++rowsOccupied;
if (rowsOccupied >= columnRows)
{
x += columnWidth + 5;
rowsOccupied = 0;
columnRows = 0;
columnWidth = 0;
++column;
}
}
}
}
}
// Opacity Protocol
/** Override synthesized setOpacity to recurse items */
void CCMenuPassive::setOpacity(GLubyte var)
{
m_cOpacity = var;
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
if (pChild)
{
CCRGBAProtocol *pRGBAProtocol = dynamic_cast<CCRGBAProtocol*>(pChild);
if (pRGBAProtocol)
{
pRGBAProtocol->setOpacity(m_cOpacity);
}
}
}
}
}
GLubyte CCMenuPassive::getOpacity(void)
{
return m_cOpacity;
}
void CCMenuPassive::setColor(const ccColor3B& var)
{
m_tColor = var;
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
if (pChild)
{
CCRGBAProtocol *pRGBAProtocol = dynamic_cast<CCRGBAProtocol*>(pChild);
if (pRGBAProtocol)
{
pRGBAProtocol->setColor(m_tColor);
}
}
}
}
}
const ccColor3B& CCMenuPassive::getColor(void)
{
return m_tColor;
}
NS_CC_EXT_END

View File

@ -1,92 +0,0 @@
/*
*
* A menu that does not send any events, it's simply a passive container (lets the contents do their own thing) of CCNodes
*/
#ifndef __CCMENU_PASSIVE_H__
#define __CCMENU_PASSIVE_H__
#include "CCControl.h"
NS_CC_EXT_BEGIN
/**
* @addtogroup GUI
* @{
* @addtogroup control_extension
* @{
*/
class CCMenuPassive : public CCLayer, public CCRGBAProtocol
{
/** Color: conforms with CCRGBAProtocol protocol */
CC_PROPERTY_PASS_BY_REF(ccColor3B, m_tColor, Color);
/** Opacity: conforms with CCRGBAProtocol protocol */
CC_PROPERTY(GLubyte, m_cOpacity, Opacity);
public:
/** creates an empty CCMenu
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenuPassive* node();
/** creates a CCMenu with it's items
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenuPassive* menuWithItems(CCNode* item, ...);
/** creates a CCMenu with it's item, then use addChild() to add
* other items. It is used for script, it can't init with undetermined
* number of variables.
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenuPassive* menuWithItem(CCNode* item);
/** creates an empty CCMenu */
static CCMenuPassive* create();
/** creates a CCMenu with it's items */
static CCMenuPassive* create(CCNode* item, ...);
/** creates a CCMenu with it's item, then use addChild() to add
* other items. It is used for script, it can't init with undetermined
* number of variables.
*/
static CCMenuPassive* createWithItem(CCNode* item);
/** initializes a CCMenu with it's items */
bool initWithItems(CCNode* item, va_list args);
/** align items vertically */
void alignItemsVertically();
/** align items vertically with padding
@since v0.7.2
*/
void alignItemsVerticallyWithPadding(float padding);
/** align items horizontally */
void alignItemsHorizontally();
/** align items horizontally with padding
@since v0.7.2
*/
void alignItemsHorizontallyWithPadding(float padding);
/** align items in rows of columns */
void alignItemsInColumns(unsigned int columns, ...);
void alignItemsInColumns(unsigned int columns, va_list args);
/** align items in columns of rows */
void alignItemsInRows(unsigned int rows, ...);
void alignItemsInRows(unsigned int rows, va_list args);
//RGBA protocol
virtual void setOpacityModifyRGB(bool bValue) {CC_UNUSED_PARAM(bValue);}
virtual bool isOpacityModifyRGB(void) { return false;}
};
// end of GUI group
/// @}
/// @}
NS_CC_EXT_END
#endif

View File

@ -7,26 +7,58 @@
// //
#include "CCScale9Sprite.h" #include "CCScale9Sprite.h"
#include "sprite_nodes/CCSpriteBatchNode.h"
#include "sprite_nodes/CCSpriteFrame.h"
#include "sprite_nodes/CCSpriteFrameCache.h"
#include "sprite_nodes/CCSprite.h"
#include "support/CCPointExtension.h"
NS_CC_EXT_BEGIN NS_CC_EXT_BEGIN
enum positions
{
pCentre = 0,
pTop,
pLeft,
pRight,
pBottom,
pTopRight,
pTopLeft,
pBottomRight,
pBottomLeft
};
CCScale9Sprite::CCScale9Sprite() CCScale9Sprite::CCScale9Sprite()
: m_insetLeft(0) : m_insetLeft(0)
, m_insetTop(0) , m_insetTop(0)
, m_insetRight(0) , m_insetRight(0)
, m_insetBottom(0) , m_insetBottom(0)
, m_cOpacity(0)
, m_bSpritesGenerated(false)
, m_bSpriteFrameRotated(false)
, m_positionsAreDirty(false)
, scale9Image(NULL)
, topLeft(NULL)
, top(NULL)
, topRight(NULL)
, left(NULL)
, centre(NULL)
, right(NULL)
, bottomLeft(NULL)
, bottom(NULL)
, bottomRight(NULL)
, m_bIsOpacityModifyRGB(false)
{ {
} }
CCScale9Sprite::~CCScale9Sprite() CCScale9Sprite::~CCScale9Sprite()
{ {
CC_SAFE_RELEASE(topLeft);
CC_SAFE_RELEASE(top);
CC_SAFE_RELEASE(topRight);
CC_SAFE_RELEASE(left);
CC_SAFE_RELEASE(centre);
CC_SAFE_RELEASE(right);
CC_SAFE_RELEASE(bottomLeft);
CC_SAFE_RELEASE(bottom);
CC_SAFE_RELEASE(bottomRight);
CC_SAFE_RELEASE(scale9Image);
} }
bool CCScale9Sprite::init() bool CCScale9Sprite::init()
@ -35,10 +67,15 @@ bool CCScale9Sprite::init()
} }
bool CCScale9Sprite::initWithBatchNode(CCSpriteBatchNode* batchnode, CCRect rect, CCRect capInsets) bool CCScale9Sprite::initWithBatchNode(CCSpriteBatchNode* batchnode, CCRect rect, CCRect capInsets)
{
return this->initWithBatchNode(batchnode, rect, false, capInsets);
}
bool CCScale9Sprite::initWithBatchNode(CCSpriteBatchNode* batchnode, CCRect rect, bool rotated, CCRect capInsets)
{ {
if(batchnode) if(batchnode)
{ {
this->updateWithBatchNode(batchnode, rect, capInsets); this->updateWithBatchNode(batchnode, rect, rotated, capInsets);
this->setAnchorPoint(ccp(0.5f, 0.5f)); this->setAnchorPoint(ccp(0.5f, 0.5f));
} }
this->m_positionsAreDirty = true; this->m_positionsAreDirty = true;
@ -46,37 +83,30 @@ bool CCScale9Sprite::initWithBatchNode(CCSpriteBatchNode* batchnode, CCRect rect
return true; return true;
} }
bool CCScale9Sprite::updateWithBatchNode(CCSpriteBatchNode* batchnode, CCRect rect, CCRect capInsets) bool CCScale9Sprite::updateWithBatchNode(CCSpriteBatchNode* batchnode, CCRect rect, bool rotated, CCRect capInsets)
{ {
GLubyte opacity = m_cOpacity;
ccColor3B color = m_tColor;
// Release old sprites // Release old sprites
this->removeAllChildrenWithCleanup(true); this->removeAllChildrenWithCleanup(true);
// TODO Is this needed? CC_SAFE_RELEASE(this->centre);
/* CC_SAFE_RELEASE(this->top);
if(this->topLeft != NULL) CC_SAFE_RELEASE(this->topLeft);
{ CC_SAFE_RELEASE(this->topRight);
CC_SAFE_RELEASE(this->topLeft); CC_SAFE_RELEASE(this->left);
CC_SAFE_RELEASE(this->top); CC_SAFE_RELEASE(this->right);
CC_SAFE_RELEASE(this->topRight); CC_SAFE_RELEASE(this->bottomLeft);
CC_SAFE_RELEASE(this->left); CC_SAFE_RELEASE(this->bottom);
CC_SAFE_RELEASE(this->centre); CC_SAFE_RELEASE(this->bottomRight);
CC_SAFE_RELEASE(this->right);
CC_SAFE_RELEASE(this->bottomLeft);
CC_SAFE_RELEASE(this->bottom);
CC_SAFE_RELEASE(this->bottomRight);
}
*/
if(this->scale9Image != batchnode) if(this->scale9Image != batchnode)
{ {
// TODO Is this needed? CC_SAFE_RELEASE(this->scale9Image);
/* scale9Image = batchnode;
if(this->scale9Image != NULL) CC_SAFE_RETAIN(scale9Image);
{
CC_SAFE_RELEASE(this->scale9Image);
}
*/
scale9Image = batchnode; // TODO No retain on purpose?
} }
scale9Image->removeAllChildrenWithCleanup(true); scale9Image->removeAllChildrenWithCleanup(true);
@ -98,99 +128,219 @@ bool CCScale9Sprite::updateWithBatchNode(CCSpriteBatchNode* batchnode, CCRect re
m_preferredSize = m_originalSize; m_preferredSize = m_originalSize;
m_capInsetsInternal = capInsets; m_capInsetsInternal = capInsets;
// Get the image edges
float l = rect.origin.x;
float t = rect.origin.y;
float h = rect.size.height;
float w = rect.size.width;
// If there is no specified center region // If there is no specified center region
if ( m_capInsetsInternal.equals(CCRectZero) ) if ( m_capInsetsInternal.equals(CCRectZero) )
{ {
// Apply the 3x3 grid format // Apply the 3x3 grid format
m_capInsetsInternal = CCRectMake( if (rotated)
rect.origin.x + m_originalSize.width / 3, {
rect.origin.y + m_originalSize.height / 3, m_capInsetsInternal = CCRectMake(l+h/3, t+w/3, w/3, h/3);
m_originalSize.width / 3, }
m_originalSize.height / 3); else
{
m_capInsetsInternal = CCRectMake(l+w/3, t+h/3, w/3, h/3);
}
} }
// Get the image edges
float l = rect.origin.x;
float t = rect.origin.y;
float h = rect.size.height;
float w = rect.size.width;
// //
// Set up the image // Set up the image
// //
if (rotated)
// Centre {
centre = CCSprite::createWithTexture(scale9Image->getTexture(), m_capInsetsInternal); // Sprite frame is rotated
scale9Image->addChild(centre, 0, pCentre);
// Centre
// Top centre = new CCSprite();
top = CCSprite::createWithTexture(scale9Image->getTexture(), CCRectMake(m_capInsetsInternal.origin.x, centre->initWithTexture(scale9Image->getTexture(), m_capInsetsInternal, true);
t, scale9Image->addChild(centre, 0, pCentre);
m_capInsetsInternal.size.width,
m_capInsetsInternal.origin.y - t)); // Bottom
scale9Image->addChild(top, 1, pTop); bottom = new CCSprite();
bottom->initWithTexture(scale9Image->getTexture(), CCRectMake(l,
// Bottom m_capInsetsInternal.origin.y,
bottom = CCSprite::createWithTexture(scale9Image->getTexture(), CCRectMake( m_capInsetsInternal.origin.x, m_capInsetsInternal.size.width,
m_capInsetsInternal.origin.y + m_capInsetsInternal.size.height, m_capInsetsInternal.origin.x - l),
m_capInsetsInternal.size.width, rotated
h - (m_capInsetsInternal.origin.y - t + m_capInsetsInternal.size.height) )); );
scale9Image->addChild(bottom, 1, pBottom); scale9Image->addChild(bottom, 1, pBottom);
// Left // Top
left = CCSprite::createWithTexture(scale9Image->getTexture(), CCRectMake( top = new CCSprite();
l, top->initWithTexture(scale9Image->getTexture(),CCRectMake(m_capInsetsInternal.origin.x + m_capInsetsInternal.size.height,
m_capInsetsInternal.origin.y, m_capInsetsInternal.origin.y,
m_capInsetsInternal.origin.x - l, m_capInsetsInternal.size.width,
m_capInsetsInternal.size.height) ); h - m_capInsetsInternal.size.height - (m_capInsetsInternal.origin.x - l)),
scale9Image->addChild(left, 1, pLeft); rotated
);
// Right scale9Image->addChild(top, 1, pTop);
right = CCSprite::createWithTexture(scale9Image->getTexture(), CCRectMake(
m_capInsetsInternal.origin.x + m_capInsetsInternal.size.width, // Right
m_capInsetsInternal.origin.y, right = new CCSprite();
w - (m_capInsetsInternal.origin.x - l + m_capInsetsInternal.size.width), right->initWithTexture(scale9Image->getTexture(), CCRectMake(m_capInsetsInternal.origin.x,
m_capInsetsInternal.size.height)); m_capInsetsInternal.origin.y+m_capInsetsInternal.size.width,
scale9Image->addChild(right, 1, pRight); w - (m_capInsetsInternal.origin.y-t)-m_capInsetsInternal.size.width,
m_capInsetsInternal.size.height),
// Top left rotated
topLeft = CCSprite::createWithTexture(scale9Image->getTexture(), CCRectMake( );
l, scale9Image->addChild(right, 1, pRight);
// Left
left = new CCSprite();
left->initWithTexture(scale9Image->getTexture(), CCRectMake(m_capInsetsInternal.origin.x,
t,
m_capInsetsInternal.origin.y - t,
m_capInsetsInternal.size.height),
rotated
);
scale9Image->addChild(left ,1, pLeft);
// Top right
topRight = new CCSprite();
topRight->initWithTexture(scale9Image->getTexture(), CCRectMake(m_capInsetsInternal.origin.x + m_capInsetsInternal.size.height,
m_capInsetsInternal.origin.y + m_capInsetsInternal.size.width,
w - (m_capInsetsInternal.origin.y-t)-m_capInsetsInternal.size.width,
h - m_capInsetsInternal.size.height - (m_capInsetsInternal.origin.x - l)),
rotated
);
scale9Image->addChild(topRight ,2 ,pTopRight);
// Top left
topLeft = new CCSprite();
topLeft->initWithTexture(scale9Image->getTexture(), CCRectMake(m_capInsetsInternal.origin.x + m_capInsetsInternal.size.height,
t,
m_capInsetsInternal.origin.y - t,
h - m_capInsetsInternal.size.height - (m_capInsetsInternal.origin.x - l)),
rotated
);
scale9Image->addChild(topLeft, 2, pTopLeft);
// Bottom right
bottomRight = new CCSprite();
bottomRight->initWithTexture(scale9Image->getTexture(),CCRectMake(l,
m_capInsetsInternal.origin.y + m_capInsetsInternal.size.width,
w - (m_capInsetsInternal.origin.y-t)-m_capInsetsInternal.size.width,
m_capInsetsInternal.origin.x - l),
rotated
);
scale9Image->addChild(bottomRight, 2, pBottomRight);
// Bottom left
bottomLeft = new CCSprite();
bottomLeft->initWithTexture(scale9Image->getTexture(), CCRectMake(l,
t,
m_capInsetsInternal.origin.y - t,
m_capInsetsInternal.origin.x - l),
rotated
);
scale9Image->addChild(bottomLeft, 2, pBottomLeft);
}
else
{
// Sprite frame is not rotated
// Centre
centre = new CCSprite();
centre->initWithTexture(scale9Image->getTexture(), m_capInsetsInternal, rotated);
scale9Image->addChild(centre, 0, pCentre);
// Top
top = new CCSprite();
top->initWithTexture(scale9Image->getTexture(), CCRectMake(m_capInsetsInternal.origin.x,
t, t,
m_capInsetsInternal.origin.x - l, m_capInsetsInternal.size.width,
m_capInsetsInternal.origin.y - t)); m_capInsetsInternal.origin.y - t),
rotated
scale9Image->addChild(topLeft, 2, pTopLeft); );
scale9Image->addChild(top, 1, pTop);
// Top right
topRight = CCSprite::createWithTexture(scale9Image->getTexture(), CCRectMake( // Bottom
m_capInsetsInternal.origin.x + m_capInsetsInternal.size.width, bottom = new CCSprite();
t, bottom->initWithTexture(scale9Image->getTexture(), CCRectMake( m_capInsetsInternal.origin.x,
w - (m_capInsetsInternal.origin.x - l + m_capInsetsInternal.size.width),
m_capInsetsInternal.origin.y - t));
scale9Image->addChild(topRight, 2, pTopRight);
// Bottom left
bottomLeft = CCSprite::createWithTexture(scale9Image->getTexture(), CCRectMake(
l,
m_capInsetsInternal.origin.y + m_capInsetsInternal.size.height, m_capInsetsInternal.origin.y + m_capInsetsInternal.size.height,
m_capInsetsInternal.origin.x - l, m_capInsetsInternal.size.width,
h - (m_capInsetsInternal.origin.y - t + m_capInsetsInternal.size.height)) ); h - (m_capInsetsInternal.origin.y - t + m_capInsetsInternal.size.height)),
scale9Image->addChild(bottomLeft, 2, pBottomLeft); rotated);
scale9Image->addChild(bottom, 1, pBottom);
// Bottom right
bottomRight = CCSprite::createWithTexture(scale9Image->getTexture(), CCRectMake( // Left
m_capInsetsInternal.origin.x + m_capInsetsInternal.size.width, left = new CCSprite();
m_capInsetsInternal.origin.y + m_capInsetsInternal.size.height, left->initWithTexture(scale9Image->getTexture(), CCRectMake(
w - (m_capInsetsInternal.origin.x - l + m_capInsetsInternal.size.width), l,
h - (m_capInsetsInternal.origin.y - t + m_capInsetsInternal.size.height)) ); m_capInsetsInternal.origin.y,
scale9Image->addChild(bottomRight, 2, pBottomRight); m_capInsetsInternal.origin.x - l,
m_capInsetsInternal.size.height),
rotated);
scale9Image->addChild(left, 1, pLeft);
// Right
right = new CCSprite();
right->initWithTexture(scale9Image->getTexture(), CCRectMake(
m_capInsetsInternal.origin.x + m_capInsetsInternal.size.width,
m_capInsetsInternal.origin.y,
w - (m_capInsetsInternal.origin.x - l + m_capInsetsInternal.size.width),
m_capInsetsInternal.size.height),
rotated);
scale9Image->addChild(right, 1, pRight);
// Top left
topLeft = new CCSprite();
topLeft->initWithTexture(scale9Image->getTexture(), CCRectMake(
l,
t,
m_capInsetsInternal.origin.x - l,
m_capInsetsInternal.origin.y - t),
rotated);
scale9Image->addChild(topLeft, 2, pTopLeft);
// Top right
topRight = new CCSprite();
topRight->initWithTexture(scale9Image->getTexture(), CCRectMake(
m_capInsetsInternal.origin.x + m_capInsetsInternal.size.width,
t,
w - (m_capInsetsInternal.origin.x - l + m_capInsetsInternal.size.width),
m_capInsetsInternal.origin.y - t),
rotated);
scale9Image->addChild(topRight, 2, pTopRight);
// Bottom left
bottomLeft = new CCSprite();
bottomLeft->initWithTexture(scale9Image->getTexture(), CCRectMake(
l,
m_capInsetsInternal.origin.y + m_capInsetsInternal.size.height,
m_capInsetsInternal.origin.x - l,
h - (m_capInsetsInternal.origin.y - t + m_capInsetsInternal.size.height)),
rotated);
scale9Image->addChild(bottomLeft, 2, pBottomLeft);
// Bottom right
bottomRight = new CCSprite();
bottomRight->initWithTexture(scale9Image->getTexture(), CCRectMake(
m_capInsetsInternal.origin.x + m_capInsetsInternal.size.width,
m_capInsetsInternal.origin.y + m_capInsetsInternal.size.height,
w - (m_capInsetsInternal.origin.x - l + m_capInsetsInternal.size.width),
h - (m_capInsetsInternal.origin.y - t + m_capInsetsInternal.size.height)),
rotated);
scale9Image->addChild(bottomRight, 2, pBottomRight);
}
this->setContentSize(rect.size); this->setContentSize(rect.size);
this->addChild(scale9Image); this->addChild(scale9Image);
if (m_bSpritesGenerated)
{
// Restore color and opacity
this->setOpacity(opacity);
this->setColor(color);
}
m_bSpritesGenerated = true;
return true; return true;
} }
@ -206,10 +356,13 @@ void CCScale9Sprite::updatePositions()
float sizableWidth = size.width - topLeft->getContentSize().width - topRight->getContentSize().width; float sizableWidth = size.width - topLeft->getContentSize().width - topRight->getContentSize().width;
float sizableHeight = size.height - topLeft->getContentSize().height - bottomRight->getContentSize().height; float sizableHeight = size.height - topLeft->getContentSize().height - bottomRight->getContentSize().height;
float horizontalScale = sizableWidth/centre->getContentSize().width; float horizontalScale = sizableWidth/centre->getContentSize().width;
float verticalScale = sizableHeight/centre->getContentSize().height; float verticalScale = sizableHeight/centre->getContentSize().height;
centre->setScaleX(horizontalScale); centre->setScaleX(horizontalScale);
centre->setScaleY(verticalScale); centre->setScaleY(verticalScale);
float rescaledWidth = centre->getContentSize().width * horizontalScale; float rescaledWidth = centre->getContentSize().width * horizontalScale;
float rescaledHeight = centre->getContentSize().height * verticalScale; float rescaledHeight = centre->getContentSize().height * verticalScale;
@ -274,6 +427,7 @@ CCScale9Sprite* CCScale9Sprite::create(const char* file, CCRect rect, CCRect ca
bool CCScale9Sprite::initWithFile(const char* file, CCRect rect) bool CCScale9Sprite::initWithFile(const char* file, CCRect rect)
{ {
CCAssert(file != NULL, "Invalid file for sprite");
bool pReturn = this->initWithFile(file, rect, CCRectZero); bool pReturn = this->initWithFile(file, rect, CCRectZero);
return pReturn; return pReturn;
} }
@ -348,7 +502,7 @@ bool CCScale9Sprite::initWithSpriteFrame(CCSpriteFrame* spriteFrame, CCRect capI
CCAssert(spriteFrame != NULL, "Sprite frame must be not nil"); CCAssert(spriteFrame != NULL, "Sprite frame must be not nil");
CCSpriteBatchNode *batchnode = CCSpriteBatchNode::createWithTexture(spriteFrame->getTexture(), 9); CCSpriteBatchNode *batchnode = CCSpriteBatchNode::createWithTexture(spriteFrame->getTexture(), 9);
bool pReturn = this->initWithBatchNode(batchnode, spriteFrame->getRect(), capInsets); bool pReturn = this->initWithBatchNode(batchnode, spriteFrame->getRect(), spriteFrame->isRotated(), capInsets);
return pReturn; return pReturn;
} }
@ -370,6 +524,7 @@ CCScale9Sprite* CCScale9Sprite::createWithSpriteFrame(CCSpriteFrame* spriteFrame
} }
bool CCScale9Sprite::initWithSpriteFrame(CCSpriteFrame* spriteFrame) bool CCScale9Sprite::initWithSpriteFrame(CCSpriteFrame* spriteFrame)
{ {
CCAssert(spriteFrame != NULL, "Invalid spriteFrame for sprite");
bool pReturn = this->initWithSpriteFrame(spriteFrame, CCRectZero); bool pReturn = this->initWithSpriteFrame(spriteFrame, CCRectZero);
return pReturn; return pReturn;
} }
@ -508,15 +663,6 @@ void CCScale9Sprite::setOpacity(GLubyte var)
{ {
pNode->setOpacity(m_cOpacity); pNode->setOpacity(m_cOpacity);
} }
//CCNode* pNode = (CCNode*) child;
//if (pNode)
//{
// CCRGBAProtocol *pRGBAProtocol = (CCRGBAProtocol *)pNode;
// if (pRGBAProtocol)
// {
// pRGBAProtocol->setOpacity(m_cOpacity);
// }
//}
} }
} }
} }
@ -544,7 +690,7 @@ CCSize CCScale9Sprite::getPreferredSize()
void CCScale9Sprite::setCapInsets(CCRect capInsets) void CCScale9Sprite::setCapInsets(CCRect capInsets)
{ {
CCSize contentSize = this->m_tContentSize; CCSize contentSize = this->m_tContentSize;
this->updateWithBatchNode(this->scale9Image, this->m_spriteRect, capInsets); this->updateWithBatchNode(this->scale9Image, this->m_spriteRect, m_bSpriteFrameRotated, capInsets);
this->setContentSize(contentSize); this->setContentSize(contentSize);
} }
@ -562,10 +708,20 @@ void CCScale9Sprite::updateCapInset()
} }
else else
{ {
insets = CCRectMake(this->m_insetLeft, if (m_bSpriteFrameRotated)
this->m_insetTop, {
this->m_spriteRect.size.width-this->m_insetLeft-this->m_insetRight, insets = CCRectMake(m_spriteRect.origin.x + m_insetBottom,
this->m_spriteRect.size.height-this->m_insetTop-this->m_insetBottom); m_spriteRect.origin.y + m_insetLeft,
m_spriteRect.size.width-m_insetRight-m_insetLeft,
m_spriteRect.size.height-m_insetTop-m_insetBottom);
}
else
{
insets = CCRectMake(m_spriteRect.origin.x + m_insetLeft,
m_spriteRect.origin.y + m_insetTop,
m_spriteRect.size.width-m_insetLeft-m_insetRight,
m_spriteRect.size.height-m_insetTop-m_insetBottom);
}
} }
this->setCapInsets(insets); this->setCapInsets(insets);
} }
@ -583,15 +739,6 @@ void CCScale9Sprite::setOpacityModifyRGB(bool var)
{ {
pNode->setOpacityModifyRGB(m_bIsOpacityModifyRGB); pNode->setOpacityModifyRGB(m_bIsOpacityModifyRGB);
} }
//CCNode* pNode = (CCNode*) child;
//if (pNode)
//{
// CCRGBAProtocol *pRGBAProtocol = (CCRGBAProtocol *)pNode;
// if (pRGBAProtocol)
// {
// pRGBAProtocol->setOpacityModifyRGB(m_bIsOpacityModifyRGB);
// }
//}
} }
} }
} }
@ -603,7 +750,7 @@ bool CCScale9Sprite::isOpacityModifyRGB()
void CCScale9Sprite::setSpriteFrame(CCSpriteFrame * spriteFrame) void CCScale9Sprite::setSpriteFrame(CCSpriteFrame * spriteFrame)
{ {
CCSpriteBatchNode * batchnode = CCSpriteBatchNode::createWithTexture(spriteFrame->getTexture(), 9); CCSpriteBatchNode * batchnode = CCSpriteBatchNode::createWithTexture(spriteFrame->getTexture(), 9);
this->updateWithBatchNode(batchnode, spriteFrame->getRect(), CCRectZero); this->updateWithBatchNode(batchnode, spriteFrame->getRect(), spriteFrame->isRotated(), CCRectZero);
// Reset insets // Reset insets
this->m_insetLeft = 0; this->m_insetLeft = 0;

View File

@ -9,30 +9,11 @@
#ifndef __CCScale9Sprite_H__ #ifndef __CCScale9Sprite_H__
#define __CCScale9Sprite_H__ #define __CCScale9Sprite_H__
#include "base_nodes/CCNode.h" #include "cocos2d.h"
#include "CCProtocols.h"
#include "ExtensionMacros.h" #include "ExtensionMacros.h"
namespace cocos2d { class CCSprite; }
namespace cocos2d { class CCSpriteBatchNode; }
namespace cocos2d { class CCSpriteFrame; }
NS_CC_EXT_BEGIN NS_CC_EXT_BEGIN
enum positions
{
pCentre = 0,
pTop,
pLeft,
pRight,
pBottom,
pTopRight,
pTopLeft,
pBottomRight,
pBottomLeft
};
/** /**
* @addtogroup GUI * @addtogroup GUI
* @{ * @{
@ -48,7 +29,7 @@ public:
public: public:
/** Original sprite's size. */ /** Original sprite's size. */
CC_SYNTHESIZE(CCSize, m_originalSize, OriginalSize); CC_SYNTHESIZE_READONLY(CCSize, m_originalSize, OriginalSize);
/** Prefered sprite's size. By default the prefered size is the original size. */ /** Prefered sprite's size. By default the prefered size is the original size. */
//if the preferredSize component is given as -1, it is ignored //if the preferredSize component is given as -1, it is ignored
@ -58,19 +39,25 @@ public:
* On a non-resizeable sprite, this property is set to CGRectZero; the sprite * On a non-resizeable sprite, this property is set to CGRectZero; the sprite
* does not use end caps and the entire sprite is subject to stretching. * does not use end caps and the entire sprite is subject to stretching.
*/ */
CC_PROPERTY(CCRect, m_capInsets, CapInsets);
/** Sets the left side inset */
CC_PROPERTY(float, m_insetLeft, InsetLeft);
/** Sets the top side inset */
CC_PROPERTY(float, m_insetTop, InsetTop);
/** Sets the right side inset */
CC_PROPERTY(float, m_insetRight, InsetRight);
/** Sets the bottom side inset */
CC_PROPERTY(float, m_insetBottom, InsetBottom);
/** Opacity: conforms to CCRGBAProtocol protocol */ /** Opacity: conforms to CCRGBAProtocol protocol */
CC_PROPERTY(GLubyte, m_cOpacity, Opacity) CC_PROPERTY(GLubyte, m_cOpacity, Opacity)
/** Color: conforms to CCRGBAProtocol protocol */ /** Color: conforms to CCRGBAProtocol protocol */
CC_PROPERTY_PASS_BY_REF(ccColor3B, m_tColor, Color) CC_PROPERTY_PASS_BY_REF(ccColor3B, m_tColor, Color)
CC_PROPERTY(CCRect, m_capInsets, CapInsets);
CC_PROPERTY(float, m_insetLeft, InsetLeft);
CC_PROPERTY(float, m_insetTop, InsetTop);
CC_PROPERTY(float, m_insetRight, InsetRight);
CC_PROPERTY(float, m_insetBottom, InsetBottom);
protected: protected:
bool m_bSpritesGenerated;
CCRect m_spriteRect; CCRect m_spriteRect;
bool m_bSpriteFrameRotated;
CCRect m_capInsetsInternal; CCRect m_capInsetsInternal;
bool m_positionsAreDirty; bool m_positionsAreDirty;
@ -99,6 +86,7 @@ public:
virtual bool init(); virtual bool init();
virtual bool initWithBatchNode(CCSpriteBatchNode* batchnode, CCRect rect, bool rotated, CCRect capInsets);
virtual bool initWithBatchNode(CCSpriteBatchNode* batchnode, CCRect rect, CCRect capInsets); virtual bool initWithBatchNode(CCSpriteBatchNode* batchnode, CCRect rect, CCRect capInsets);
/** /**
* Initializes a 9-slice sprite with a texture file, a delimitation zone and * Initializes a 9-slice sprite with a texture file, a delimitation zone and
@ -153,7 +141,7 @@ public:
* @see initWithFile:rect: * @see initWithFile:rect:
@deprecated: This interface will be deprecated sooner or later. @deprecated: This interface will be deprecated sooner or later.
*/ */
static CCScale9Sprite* spriteWithFile(const char* file, CCRect rect); CC_DEPRECATED_ATTRIBUTE static CCScale9Sprite* spriteWithFile(const char* file, CCRect rect);
/** /**
* Creates a 9-slice sprite with a texture file and a delimitation zone. The * Creates a 9-slice sprite with a texture file and a delimitation zone. The
@ -378,7 +366,7 @@ public:
*/ */
virtual bool isOpacityModifyRGB(void); virtual bool isOpacityModifyRGB(void);
virtual bool updateWithBatchNode(CCSpriteBatchNode* batchnode, CCRect rect, CCRect capInsets); virtual bool updateWithBatchNode(CCSpriteBatchNode* batchnode, CCRect rect, bool rotated, CCRect capInsets);
virtual void setSpriteFrame(CCSpriteFrame * spriteFrame); virtual void setSpriteFrame(CCSpriteFrame * spriteFrame);
}; };

View File

@ -1,23 +0,0 @@
#include "CCSpacer.h"
NS_CC_EXT_BEGIN
CCSpacer* CCSpacer::verticalSpacer(float space)
{
CCSpacer *pRet = new CCSpacer();
pRet->init();
pRet->setContentSize(CCSizeMake(0, space));
pRet->autorelease();
return pRet;
}
CCSpacer* CCSpacer::horizontalSpacer(float space)
{
CCSpacer *pRet = new CCSpacer();
pRet->init();
pRet->setContentSize(CCSizeMake(space, 0));
pRet->autorelease();
return pRet;
}
NS_CC_EXT_END

View File

@ -1,29 +0,0 @@
#ifndef __CCSPACER_H__
#define __CCSPACER_H__
#include "layers_scenes_transitions_nodes/CCLayer.h"
#include "ExtensionMacros.h"
NS_CC_EXT_BEGIN
/**
* @addtogroup GUI
* @{
* @addtogroup control_extension
* @{
*/
class CCSpacer: public CCLayer
{
public:
static CCSpacer* verticalSpacer(float space);
static CCSpacer* horizontalSpacer(float space);
};
// end of GUI group
/// @}
/// @}
NS_CC_EXT_END
#endif

View File

@ -401,6 +401,14 @@
RelativePath="..\GUI\CCControlExtension\CCControlHuePicker.h" RelativePath="..\GUI\CCControlExtension\CCControlHuePicker.h"
> >
</File> </File>
<File
RelativePath="..\GUI\CCControlExtension\CCControlPotentiometer.cpp"
>
</File>
<File
RelativePath="..\GUI\CCControlExtension\CCControlPotentiometer.h"
>
</File>
<File <File
RelativePath="..\GUI\CCControlExtension\CCControlSaturationBrightnessPicker.cpp" RelativePath="..\GUI\CCControlExtension\CCControlSaturationBrightnessPicker.cpp"
> >
@ -417,6 +425,14 @@
RelativePath="..\GUI\CCControlExtension\CCControlSlider.h" RelativePath="..\GUI\CCControlExtension\CCControlSlider.h"
> >
</File> </File>
<File
RelativePath="..\GUI\CCControlExtension\CCControlStepper.cpp"
>
</File>
<File
RelativePath="..\GUI\CCControlExtension\CCControlStepper.h"
>
</File>
<File <File
RelativePath="..\GUI\CCControlExtension\CCControlSwitch.cpp" RelativePath="..\GUI\CCControlExtension\CCControlSwitch.cpp"
> >
@ -441,14 +457,6 @@
RelativePath="..\GUI\CCControlExtension\CCInvocation.h" RelativePath="..\GUI\CCControlExtension\CCInvocation.h"
> >
</File> </File>
<File
RelativePath="..\GUI\CCControlExtension\CCMenuPassive.cpp"
>
</File>
<File
RelativePath="..\GUI\CCControlExtension\CCMenuPassive.h"
>
</File>
<File <File
RelativePath="..\GUI\CCControlExtension\CCScale9Sprite.cpp" RelativePath="..\GUI\CCControlExtension\CCScale9Sprite.cpp"
> >
@ -457,14 +465,6 @@
RelativePath="..\GUI\CCControlExtension\CCScale9Sprite.h" RelativePath="..\GUI\CCControlExtension\CCScale9Sprite.h"
> >
</File> </File>
<File
RelativePath="..\GUI\CCControlExtension\CCSpacer.cpp"
>
</File>
<File
RelativePath="..\GUI\CCControlExtension\CCSpacer.h"
>
</File>
</Filter> </Filter>
<Filter <Filter
Name="CCScrollView" Name="CCScrollView"

View File

@ -107,14 +107,14 @@
<ClCompile Include="..\GUI\CCControlExtension\CCControlButton.cpp" /> <ClCompile Include="..\GUI\CCControlExtension\CCControlButton.cpp" />
<ClCompile Include="..\GUI\CCControlExtension\CCControlColourPicker.cpp" /> <ClCompile Include="..\GUI\CCControlExtension\CCControlColourPicker.cpp" />
<ClCompile Include="..\GUI\CCControlExtension\CCControlHuePicker.cpp" /> <ClCompile Include="..\GUI\CCControlExtension\CCControlHuePicker.cpp" />
<ClCompile Include="..\GUI\CCControlExtension\CCControlPotentiometer.cpp" />
<ClCompile Include="..\GUI\CCControlExtension\CCControlSaturationBrightnessPicker.cpp" /> <ClCompile Include="..\GUI\CCControlExtension\CCControlSaturationBrightnessPicker.cpp" />
<ClCompile Include="..\GUI\CCControlExtension\CCControlSlider.cpp" /> <ClCompile Include="..\GUI\CCControlExtension\CCControlSlider.cpp" />
<ClCompile Include="..\GUI\CCControlExtension\CCControlStepper.cpp" />
<ClCompile Include="..\GUI\CCControlExtension\CCControlSwitch.cpp" /> <ClCompile Include="..\GUI\CCControlExtension\CCControlSwitch.cpp" />
<ClCompile Include="..\GUI\CCControlExtension\CCControlUtils.cpp" /> <ClCompile Include="..\GUI\CCControlExtension\CCControlUtils.cpp" />
<ClCompile Include="..\GUI\CCControlExtension\CCInvocation.cpp" /> <ClCompile Include="..\GUI\CCControlExtension\CCInvocation.cpp" />
<ClCompile Include="..\GUI\CCControlExtension\CCMenuPassive.cpp" />
<ClCompile Include="..\GUI\CCControlExtension\CCScale9Sprite.cpp" /> <ClCompile Include="..\GUI\CCControlExtension\CCScale9Sprite.cpp" />
<ClCompile Include="..\GUI\CCControlExtension\CCSpacer.cpp" />
<ClCompile Include="..\GUI\CCScrollView\CCScrollView.cpp" /> <ClCompile Include="..\GUI\CCScrollView\CCScrollView.cpp" />
<ClCompile Include="..\GUI\CCScrollView\CCSorting.cpp" /> <ClCompile Include="..\GUI\CCScrollView\CCSorting.cpp" />
<ClCompile Include="..\GUI\CCScrollView\CCTableView.cpp" /> <ClCompile Include="..\GUI\CCScrollView\CCTableView.cpp" />
@ -155,14 +155,14 @@
<ClInclude Include="..\GUI\CCControlExtension\CCControlColourPicker.h" /> <ClInclude Include="..\GUI\CCControlExtension\CCControlColourPicker.h" />
<ClInclude Include="..\GUI\CCControlExtension\CCControlExtensions.h" /> <ClInclude Include="..\GUI\CCControlExtension\CCControlExtensions.h" />
<ClInclude Include="..\GUI\CCControlExtension\CCControlHuePicker.h" /> <ClInclude Include="..\GUI\CCControlExtension\CCControlHuePicker.h" />
<ClInclude Include="..\GUI\CCControlExtension\CCControlPotentiometer.h" />
<ClInclude Include="..\GUI\CCControlExtension\CCControlSaturationBrightnessPicker.h" /> <ClInclude Include="..\GUI\CCControlExtension\CCControlSaturationBrightnessPicker.h" />
<ClInclude Include="..\GUI\CCControlExtension\CCControlSlider.h" /> <ClInclude Include="..\GUI\CCControlExtension\CCControlSlider.h" />
<ClInclude Include="..\GUI\CCControlExtension\CCControlStepper.h" />
<ClInclude Include="..\GUI\CCControlExtension\CCControlSwitch.h" /> <ClInclude Include="..\GUI\CCControlExtension\CCControlSwitch.h" />
<ClInclude Include="..\GUI\CCControlExtension\CCControlUtils.h" /> <ClInclude Include="..\GUI\CCControlExtension\CCControlUtils.h" />
<ClInclude Include="..\GUI\CCControlExtension\CCInvocation.h" /> <ClInclude Include="..\GUI\CCControlExtension\CCInvocation.h" />
<ClInclude Include="..\GUI\CCControlExtension\CCMenuPassive.h" />
<ClInclude Include="..\GUI\CCControlExtension\CCScale9Sprite.h" /> <ClInclude Include="..\GUI\CCControlExtension\CCScale9Sprite.h" />
<ClInclude Include="..\GUI\CCControlExtension\CCSpacer.h" />
<ClInclude Include="..\GUI\CCScrollView\CCScrollView.h" /> <ClInclude Include="..\GUI\CCScrollView\CCScrollView.h" />
<ClInclude Include="..\cocos-ext.h" /> <ClInclude Include="..\cocos-ext.h" />
<ClInclude Include="..\ExtensionMacros.h" /> <ClInclude Include="..\ExtensionMacros.h" />

View File

@ -18,42 +18,6 @@
</Filter> </Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\GUI\CCControlExtension\CCControl.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCControlButton.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCControlColourPicker.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCControlHuePicker.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCControlSaturationBrightnessPicker.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCControlSlider.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCControlSwitch.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCControlUtils.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCInvocation.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCMenuPassive.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCScale9Sprite.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCSpacer.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCScrollView\CCScrollView.cpp"> <ClCompile Include="..\GUI\CCScrollView\CCScrollView.cpp">
<Filter>GUI\CCScrollView</Filter> <Filter>GUI\CCScrollView</Filter>
</ClCompile> </ClCompile>
@ -141,47 +105,44 @@
<ClCompile Include="..\CCBReader\CCSpriteLoader.cpp"> <ClCompile Include="..\CCBReader\CCSpriteLoader.cpp">
<Filter>CCBReader</Filter> <Filter>CCBReader</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCControlColourPicker.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCControlHuePicker.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCControlPotentiometer.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCControlSaturationBrightnessPicker.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCControlSlider.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCControlStepper.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCControlSwitch.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCControlUtils.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCInvocation.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCScale9Sprite.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCControl.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCControlExtension\CCControlButton.cpp">
<Filter>GUI\CCControlExtension</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\GUI\CCControlExtension\CCControl.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCControlButton.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCControlColourPicker.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCControlExtensions.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCControlHuePicker.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCControlSaturationBrightnessPicker.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCControlSlider.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCControlSwitch.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCControlUtils.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCInvocation.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCMenuPassive.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCScale9Sprite.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCSpacer.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCScrollView\CCScrollView.h"> <ClInclude Include="..\GUI\CCScrollView\CCScrollView.h">
<Filter>GUI\CCScrollView</Filter> <Filter>GUI\CCScrollView</Filter>
</ClInclude> </ClInclude>
@ -289,5 +250,44 @@
<ClInclude Include="..\CCBReader\CCSpriteLoader.h"> <ClInclude Include="..\CCBReader\CCSpriteLoader.h">
<Filter>CCBReader</Filter> <Filter>CCBReader</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCControlButton.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCControlColourPicker.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCControlExtensions.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCControlHuePicker.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCControlPotentiometer.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCControlSaturationBrightnessPicker.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCControlSlider.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCControlStepper.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCControlSwitch.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCControlUtils.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCInvocation.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCScale9Sprite.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCControlExtension\CCControl.h">
<Filter>GUI\CCControlExtension</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -136,14 +136,14 @@ bool CCControlButtonTest_Event::init()
addChild(background); addChild(background);
// Sets up event handlers // Sets up event handlers
controlButton->addTargetWithActionForControlEvent(this, cccontrol_selector(CCControlButtonTest_Event::touchDownAction), CCControlEventTouchDown); controlButton->addTargetWithActionForControlEvents(this, cccontrol_selector(CCControlButtonTest_Event::touchDownAction), CCControlEventTouchDown);
controlButton->addTargetWithActionForControlEvent(this, cccontrol_selector(CCControlButtonTest_Event::touchDragInsideAction), CCControlEventTouchDragInside); controlButton->addTargetWithActionForControlEvents(this, cccontrol_selector(CCControlButtonTest_Event::touchDragInsideAction), CCControlEventTouchDragInside);
controlButton->addTargetWithActionForControlEvent(this, cccontrol_selector(CCControlButtonTest_Event::touchDragOutsideAction), CCControlEventTouchDragOutside); controlButton->addTargetWithActionForControlEvents(this, cccontrol_selector(CCControlButtonTest_Event::touchDragOutsideAction), CCControlEventTouchDragOutside);
controlButton->addTargetWithActionForControlEvent(this, cccontrol_selector(CCControlButtonTest_Event::touchDragEnterAction), CCControlEventTouchDragEnter); controlButton->addTargetWithActionForControlEvents(this, cccontrol_selector(CCControlButtonTest_Event::touchDragEnterAction), CCControlEventTouchDragEnter);
controlButton->addTargetWithActionForControlEvent(this, cccontrol_selector(CCControlButtonTest_Event::touchDragExitAction), CCControlEventTouchDragExit); controlButton->addTargetWithActionForControlEvents(this, cccontrol_selector(CCControlButtonTest_Event::touchDragExitAction), CCControlEventTouchDragExit);
controlButton->addTargetWithActionForControlEvent(this, cccontrol_selector(CCControlButtonTest_Event::touchUpInsideAction), CCControlEventTouchUpInside); controlButton->addTargetWithActionForControlEvents(this, cccontrol_selector(CCControlButtonTest_Event::touchUpInsideAction), CCControlEventTouchUpInside);
controlButton->addTargetWithActionForControlEvent(this, cccontrol_selector(CCControlButtonTest_Event::touchUpOutsideAction), CCControlEventTouchUpOutside); controlButton->addTargetWithActionForControlEvents(this, cccontrol_selector(CCControlButtonTest_Event::touchUpOutsideAction), CCControlEventTouchUpOutside);
controlButton->addTargetWithActionForControlEvent(this, cccontrol_selector(CCControlButtonTest_Event::touchCancelAction), CCControlEventTouchCancel); controlButton->addTargetWithActionForControlEvents(this, cccontrol_selector(CCControlButtonTest_Event::touchCancelAction), CCControlEventTouchCancel);
return true; return true;
} }
return false; return false;

View File

@ -92,7 +92,7 @@ CCControlColourPickerTest::~CCControlColourPickerTest()
void CCControlColourPickerTest::colourValueChanged(CCObject *sender, CCControlEvent controlEvent) void CCControlColourPickerTest::colourValueChanged(CCObject *sender, CCControlEvent controlEvent)
{ {
CCControlColourPicker* pPicker = (CCControlColourPicker*)sender; CCControlColourPicker* pPicker = (CCControlColourPicker*)sender;
m_pColorLabel->setString(CCString::createWithFormat("#%02X%02X%02X",pPicker->getColorValue().r, pPicker->getColorValue().g, pPicker->getColorValue().b)->getCString()); m_pColorLabel->setString(CCString::createWithFormat("#%02X%02X%02X",pPicker->getColor().r, pPicker->getColor().g, pPicker->getColor().b)->getCString());
} }

View File

@ -0,0 +1,93 @@
/*
* Copyright (c) 2012 cocos2d-x.org
* Copyright (c) 2012 Yannick Loriot
*
* 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
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include "CCControlPotentiometerTest.h"
CCControlPotentiometerTest::CCControlPotentiometerTest()
: m_pDisplayValueLabel(NULL)
{
}
CCControlPotentiometerTest::~CCControlPotentiometerTest()
{
CC_SAFE_RELEASE(m_pDisplayValueLabel);
}
bool CCControlPotentiometerTest::init()
{
if (CCControlScene::init())
{
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
CCNode *layer = CCNode::create();
layer->setPosition(ccp (screenSize.width / 2, screenSize.height / 2));
this->addChild(layer, 1);
double layer_width = 0;
// Add the black background for the text
CCScale9Sprite *background = CCScale9Sprite::create("extensions/buttonBackground.png");
background->setContentSize(CCSizeMake(80, 50));
background->setPosition(ccp(layer_width + background->getContentSize().width / 2.0f, 0));
layer->addChild(background);
layer_width += background->getContentSize().width;
this->setDisplayValueLabel(CCLabelTTF::create("", "HelveticaNeue-Bold", 30));
m_pDisplayValueLabel->setPosition(background->getPosition());
layer->addChild(m_pDisplayValueLabel);
// Add the slider
CCControlPotentiometer *potentiometer = CCControlPotentiometer::create("extensions/potentiometerTrack.png"
,"extensions/potentiometerProgress.png"
,"extensions/potentiometerButton.png");
potentiometer->setPosition(ccp (layer_width + 10 + potentiometer->getContentSize().width / 2, 0));
// When the value of the slider will change, the given selector will be call
potentiometer->addTargetWithActionForControlEvents(this, cccontrol_selector(CCControlPotentiometerTest::valueChanged), CCControlEventValueChanged);
layer->addChild(potentiometer);
layer_width += potentiometer->getContentSize().width;
// Set the layer size
layer->setContentSize(CCSizeMake(layer_width, 0));
layer->setAnchorPoint(ccp (0.5f, 0.5f));
// Update the value label
this->valueChanged(potentiometer, CCControlEventValueChanged);
return true;
}
return false;
}
void CCControlPotentiometerTest::valueChanged(CCObject *sender, CCControlEvent controlEvent)
{
CCControlPotentiometer* pControl = (CCControlPotentiometer*)sender;
// Change value of label.
m_pDisplayValueLabel->setString(CCString::createWithFormat("%.02f", pControl->getValue())->getCString());
}

View File

@ -0,0 +1,45 @@
/*
* Copyright (c) 2012 cocos2d-x.org
* Copyright (c) 2012 Yannick Loriot
*
* 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
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#ifndef __CCCONTROLPOTENTIOMETERTEST_H__
#define __CCCONTROLPOTENTIOMETERTEST_H__
#include "../CCControlScene.h"
class CCControlPotentiometerTest : public CCControlScene
{
public:
CCControlPotentiometerTest();
virtual ~CCControlPotentiometerTest();
bool init();
CC_SYNTHESIZE_RETAIN(CCLabelTTF*, m_pDisplayValueLabel, DisplayValueLabel)
void valueChanged(CCObject *sender, CCControlEvent controlEvent);
CONTROL_SCENE_CREATE_FUNC(CCControlPotentiometerTest)
};
#endif /* __CCCONTROLPOTENTIOMETERTEST_H__ */

View File

@ -29,6 +29,8 @@
#include "CCControlColourPicker/CCControlColourPickerTest.h" #include "CCControlColourPicker/CCControlColourPickerTest.h"
#include "CCControlSliderTest/CCControlSliderTest.h" #include "CCControlSliderTest/CCControlSliderTest.h"
#include "CCControlSwitchTest/CCControlSwitchTest.h" #include "CCControlSwitchTest/CCControlSwitchTest.h"
#include "CCControlPotentiometerTest/CCControlPotentiometerTest.h"
#include "CCControlStepperTest/CCControlStepperTest.h"
USING_NS_CC; USING_NS_CC;
@ -40,6 +42,8 @@ enum
kCCControlButtonTest_HelloVariableSize, kCCControlButtonTest_HelloVariableSize,
kCCControlButtonTest_Event, kCCControlButtonTest_Event,
kCCControlButtonTest_Styling, kCCControlButtonTest_Styling,
kCCControlPotentiometerTest,
kCCControlStepperTest,
kCCControlTestMax kCCControlTestMax
}; };
@ -49,7 +53,9 @@ static const char* s_testArray[] = {
"ControlSwitchTest", "ControlSwitchTest",
"ControlButtonTest_HelloVariableSize", "ControlButtonTest_HelloVariableSize",
"ControlButtonTest_Event", "ControlButtonTest_Event",
"ControlButtonTest_Styling" "ControlButtonTest_Styling",
"ControlPotentiometerTest",
"CCControlStepperTest"
}; };
static CCControlSceneManager *sharedInstance = NULL; static CCControlSceneManager *sharedInstance = NULL;
@ -102,6 +108,8 @@ CCScene *CCControlSceneManager::currentControlScene()
case kCCControlButtonTest_HelloVariableSize:return CCControlButtonTest_HelloVariableSize::sceneWithTitle(s_testArray[m_nCurrentControlSceneId]); case kCCControlButtonTest_HelloVariableSize:return CCControlButtonTest_HelloVariableSize::sceneWithTitle(s_testArray[m_nCurrentControlSceneId]);
case kCCControlButtonTest_Event:return CCControlButtonTest_Event::sceneWithTitle(s_testArray[m_nCurrentControlSceneId]); case kCCControlButtonTest_Event:return CCControlButtonTest_Event::sceneWithTitle(s_testArray[m_nCurrentControlSceneId]);
case kCCControlButtonTest_Styling:return CCControlButtonTest_Styling::sceneWithTitle(s_testArray[m_nCurrentControlSceneId]); case kCCControlButtonTest_Styling:return CCControlButtonTest_Styling::sceneWithTitle(s_testArray[m_nCurrentControlSceneId]);
case kCCControlPotentiometerTest:return CCControlPotentiometerTest::sceneWithTitle(s_testArray[m_nCurrentControlSceneId]);
case kCCControlStepperTest:return CCControlStepperTest::sceneWithTitle(s_testArray[m_nCurrentControlSceneId]);
} }
return NULL; return NULL;
} }

View File

@ -0,0 +1,96 @@
/*
* Copyright (c) 2012 cocos2d-x.org
* Copyright (c) 2012 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
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include "CCControlStepperTest.h"
CCControlStepperTest::CCControlStepperTest()
: m_pDisplayValueLabel(NULL)
{
}
CCControlStepperTest::~CCControlStepperTest()
{
CC_SAFE_RELEASE(m_pDisplayValueLabel);
}
bool CCControlStepperTest::init()
{
if (CCControlScene::init())
{
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
CCNode *layer = CCNode::create();
layer->setPosition(ccp (screenSize.width / 2, screenSize.height / 2));
this->addChild(layer, 1);
double layer_width = 0;
// Add the black background for the text
CCScale9Sprite *background = CCScale9Sprite::create("extensions/buttonBackground.png");
background->setContentSize(CCSizeMake(100, 50));
background->setPosition(ccp(layer_width + background->getContentSize().width / 2.0f, 0));
layer->addChild(background);
this->setDisplayValueLabel(CCLabelTTF::create("0", "HelveticaNeue-Bold", 30));
m_pDisplayValueLabel->setPosition(background->getPosition());
layer->addChild(m_pDisplayValueLabel);
layer_width += background->getContentSize().width;
CCControlStepper *stepper = this->makeControlStepper();
stepper->setPosition(ccp (layer_width + 10 + stepper->getContentSize().width / 2, 0));
stepper->addTargetWithActionForControlEvents(this, cccontrol_selector(CCControlStepperTest::valueChanged), CCControlEventValueChanged);
layer->addChild(stepper);
layer_width += stepper->getContentSize().width;
// Set the layer size
layer->setContentSize(CCSizeMake(layer_width, 0));
layer->setAnchorPoint(ccp (0.5f, 0.5f));
// Update the value label
this->valueChanged(stepper, CCControlEventValueChanged);
return true;
}
return false;
}
CCControlStepper *CCControlStepperTest::makeControlStepper()
{
CCSprite *minusSprite = CCSprite::create("extensions/stepper-minus.png");
CCSprite *plusSprite = CCSprite::create("extensions/stepper-plus.png");
return CCControlStepper::create(minusSprite, plusSprite);
}
void CCControlStepperTest::valueChanged(CCObject *sender, CCControlEvent controlEvent)
{
CCControlStepper* pControl = (CCControlStepper*)sender;
// Change value of label.
m_pDisplayValueLabel->setString(CCString::createWithFormat("%0.02f", (float)pControl->getValue())->getCString());
}

View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2012 cocos2d-x.org
* Copyright (c) 2012 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
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#ifndef __CCCONTROLSTEPPERTEST_H__
#define __CCCONTROLSTEPPERTEST_H__
#include "../CCControlScene.h"
class CCControlStepperTest : public CCControlScene
{
public:
CCControlStepperTest();
virtual ~CCControlStepperTest();
bool init();
/** Creates and returns a new ControlStepper. */
CCControlStepper* makeControlStepper();
/** Callback for the change value. */
void valueChanged(CCObject *sender, CCControlEvent controlEvent);
protected:
CC_SYNTHESIZE_RETAIN(CCLabelTTF*, m_pDisplayValueLabel, DisplayValueLabel)
CONTROL_SCENE_CREATE_FUNC(CCControlStepperTest)
};
#endif /* __CCCONTROLSTEPPERTEST_H__ */

View File

@ -874,6 +874,30 @@
> >
</File> </File>
</Filter> </Filter>
<Filter
Name="CCControlPotentiometerTest"
>
<File
RelativePath="..\Classes\ExtensionsTest\ControlExtensionTest\CCControlPotentiometerTest\CCControlPotentiometerTest.cpp"
>
</File>
<File
RelativePath="..\Classes\ExtensionsTest\ControlExtensionTest\CCControlPotentiometerTest\CCControlPotentiometerTest.h"
>
</File>
</Filter>
<Filter
Name="CCControlStepperTest"
>
<File
RelativePath="..\Classes\ExtensionsTest\ControlExtensionTest\CCControlStepperTest\CCControlStepperTest.cpp"
>
</File>
<File
RelativePath="..\Classes\ExtensionsTest\ControlExtensionTest\CCControlStepperTest\CCControlStepperTest.h"
>
</File>
</Filter>
</Filter> </Filter>
<Filter <Filter
Name="NotificationCenterTest" Name="NotificationCenterTest"

View File

@ -108,6 +108,8 @@
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\Classes\ExtensionsTest\CocosBuilderTest\AnimationsTest\AnimationsTestLayer.cpp" /> <ClCompile Include="..\Classes\ExtensionsTest\CocosBuilderTest\AnimationsTest\AnimationsTestLayer.cpp" />
<ClCompile Include="..\Classes\ExtensionsTest\ControlExtensionTest\CCControlPotentiometerTest\CCControlPotentiometerTest.cpp" />
<ClCompile Include="..\Classes\ExtensionsTest\ControlExtensionTest\CCControlStepperTest\CCControlStepperTest.cpp" />
<ClCompile Include="..\Classes\ExtensionsTest\NetworkTest\HttpClientTest.cpp" /> <ClCompile Include="..\Classes\ExtensionsTest\NetworkTest\HttpClientTest.cpp" />
<ClCompile Include="..\Classes\ExtensionsTest\TableViewTest\CustomTableViewCell.cpp" /> <ClCompile Include="..\Classes\ExtensionsTest\TableViewTest\CustomTableViewCell.cpp" />
<ClCompile Include="..\Classes\ExtensionsTest\TableViewTest\TableViewTestScene.cpp" /> <ClCompile Include="..\Classes\ExtensionsTest\TableViewTest\TableViewTestScene.cpp" />
@ -194,6 +196,8 @@
<ItemGroup> <ItemGroup>
<ClInclude Include="..\Classes\ExtensionsTest\CocosBuilderTest\AnimationsTest\AnimationsLayerLoader.h" /> <ClInclude Include="..\Classes\ExtensionsTest\CocosBuilderTest\AnimationsTest\AnimationsLayerLoader.h" />
<ClInclude Include="..\Classes\ExtensionsTest\CocosBuilderTest\AnimationsTest\AnimationsTestLayer.h" /> <ClInclude Include="..\Classes\ExtensionsTest\CocosBuilderTest\AnimationsTest\AnimationsTestLayer.h" />
<ClInclude Include="..\Classes\ExtensionsTest\ControlExtensionTest\CCControlPotentiometerTest\CCControlPotentiometerTest.h" />
<ClInclude Include="..\Classes\ExtensionsTest\ControlExtensionTest\CCControlStepperTest\CCControlStepperTest.h" />
<ClInclude Include="..\Classes\ExtensionsTest\NetworkTest\HttpClientTest.h" /> <ClInclude Include="..\Classes\ExtensionsTest\NetworkTest\HttpClientTest.h" />
<ClInclude Include="..\Classes\ExtensionsTest\TableViewTest\CustomTableViewCell.h" /> <ClInclude Include="..\Classes\ExtensionsTest\TableViewTest\CustomTableViewCell.h" />
<ClInclude Include="..\Classes\ExtensionsTest\TableViewTest\TableViewTestScene.h" /> <ClInclude Include="..\Classes\ExtensionsTest\TableViewTest\TableViewTestScene.h" />

View File

@ -196,6 +196,12 @@
<Filter Include="Classes\ExtensionsTest\AnimationsTest"> <Filter Include="Classes\ExtensionsTest\AnimationsTest">
<UniqueIdentifier>{e8752620-8414-4f43-a572-5fa82650f223}</UniqueIdentifier> <UniqueIdentifier>{e8752620-8414-4f43-a572-5fa82650f223}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="Classes\ExtensionsTest\ControlExtensionTest\CCControlPotentiometerTest">
<UniqueIdentifier>{08850f9d-e7e5-4bca-ae90-cf8df391479f}</UniqueIdentifier>
</Filter>
<Filter Include="Classes\ExtensionsTest\ControlExtensionTest\CCControlStepperTest">
<UniqueIdentifier>{7e809230-9bb5-45bb-a26c-b5cdd1390914}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="main.cpp"> <ClCompile Include="main.cpp">
@ -447,6 +453,12 @@
<ClCompile Include="..\Classes\ExtensionsTest\CocosBuilderTest\AnimationsTest\AnimationsTestLayer.cpp"> <ClCompile Include="..\Classes\ExtensionsTest\CocosBuilderTest\AnimationsTest\AnimationsTestLayer.cpp">
<Filter>Classes\ExtensionsTest\AnimationsTest</Filter> <Filter>Classes\ExtensionsTest\AnimationsTest</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\Classes\ExtensionsTest\ControlExtensionTest\CCControlPotentiometerTest\CCControlPotentiometerTest.cpp">
<Filter>Classes\ExtensionsTest\ControlExtensionTest\CCControlPotentiometerTest</Filter>
</ClCompile>
<ClCompile Include="..\Classes\ExtensionsTest\ControlExtensionTest\CCControlStepperTest\CCControlStepperTest.cpp">
<Filter>Classes\ExtensionsTest\ControlExtensionTest\CCControlStepperTest</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="main.h"> <ClInclude Include="main.h">
@ -875,5 +887,11 @@
<ClInclude Include="..\Classes\ExtensionsTest\CocosBuilderTest\AnimationsTest\AnimationsTestLayer.h"> <ClInclude Include="..\Classes\ExtensionsTest\CocosBuilderTest\AnimationsTest\AnimationsTestLayer.h">
<Filter>Classes\ExtensionsTest\AnimationsTest</Filter> <Filter>Classes\ExtensionsTest\AnimationsTest</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\Classes\ExtensionsTest\ControlExtensionTest\CCControlPotentiometerTest\CCControlPotentiometerTest.h">
<Filter>Classes\ExtensionsTest\ControlExtensionTest\CCControlPotentiometerTest</Filter>
</ClInclude>
<ClInclude Include="..\Classes\ExtensionsTest\ControlExtensionTest\CCControlStepperTest\CCControlStepperTest.h">
<Filter>Classes\ExtensionsTest\ControlExtensionTest\CCControlStepperTest</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>