2012-04-19 14:35:52 +08:00
|
|
|
/*
|
2012-09-25 17:26:09 +08:00
|
|
|
* Copyright (c) 2012 cocos2d-x.org
|
|
|
|
* http://www.cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
*
|
|
|
|
* Copyright 2011 Yannick Loriot.
|
|
|
|
* http://yannickloriot.com
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* converted to c++ / cocos2d-x by Angus C
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "CCControl.h"
|
|
|
|
#include "CCDirector.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "touch_dispatcher/CCTouchDispatcher.h"
|
|
|
|
#include "menu_nodes/CCMenu.h"
|
|
|
|
#include "touch_dispatcher/CCTouch.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-04-27 18:47:49 +08:00
|
|
|
NS_CC_EXT_BEGIN
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CCControl::CCControl()
|
2012-09-25 16:57:51 +08:00
|
|
|
: 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)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-11-08 10:41:14 +08:00
|
|
|
CCControl* CCControl::create()
|
|
|
|
{
|
|
|
|
CCControl* pRet = new CCControl();
|
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
bool CCControl::init()
|
|
|
|
{
|
|
|
|
if (CCLayer::init())
|
|
|
|
{
|
2012-06-15 16:47:30 +08:00
|
|
|
//this->setTouchEnabled(true);
|
2012-04-19 14:35:52 +08:00
|
|
|
//m_bIsTouchEnabled=true;
|
|
|
|
// Initialise instance variables
|
2012-09-25 16:57:51 +08:00
|
|
|
m_eState=CCControlStateNormal;
|
|
|
|
setEnabled(true);
|
|
|
|
setSelected(false);
|
|
|
|
setHighlighted(false);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Set the touch dispatcher priority by default to 1
|
2012-09-25 16:57:51 +08:00
|
|
|
setDefaultTouchPriority(1);
|
2012-04-19 14:35:52 +08:00
|
|
|
this->setDefaultTouchPriority(m_nDefaultTouchPriority);
|
|
|
|
// Initialise the tables
|
2012-09-25 16:57:51 +08:00
|
|
|
m_pDispatchTable = new CCDictionary();
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return false;
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCControl::~CCControl()
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
CC_SAFE_RELEASE(m_pDispatchTable);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//Menu - Events
|
|
|
|
void CCControl::registerWithTouchDispatcher()
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, m_nDefaultTouchPriority, true);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCControl::onEnter()
|
|
|
|
{
|
|
|
|
CCLayer::onEnter();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControl::onExit()
|
|
|
|
{
|
|
|
|
CCLayer::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControl::sendActionsForControlEvents(CCControlEvent controlEvents)
|
|
|
|
{
|
|
|
|
// For each control events
|
2012-09-25 16:57:51 +08:00
|
|
|
for (int i = 0; i < kControlEventTotalNumber; i++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// If the given controlEvents bitmask contains the curent event
|
|
|
|
if ((controlEvents & (1 << i)))
|
|
|
|
{
|
|
|
|
// Call invocations
|
|
|
|
// <CCInvocation*>
|
2012-09-25 16:57:51 +08:00
|
|
|
CCArray* invocationList = this->dispatchListforControlEvent(1<<i);
|
2012-04-19 14:35:52 +08:00
|
|
|
CCObject* pObj = NULL;
|
|
|
|
CCARRAY_FOREACH(invocationList, pObj)
|
|
|
|
{
|
|
|
|
CCInvocation* invocation = (CCInvocation*)pObj;
|
|
|
|
invocation->invoke(this);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-07 05:31:14 +08:00
|
|
|
void CCControl::addTargetWithActionForControlEvents(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvents)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// For each control events
|
2012-09-25 16:57:51 +08:00
|
|
|
for (int i = 0; i < kControlEventTotalNumber; i++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// If the given controlEvents bitmask contains the curent event
|
|
|
|
if ((controlEvents & (1 << i)))
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
this->addTargetWithActionForControlEvent(target, action, 1<<i);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2012-09-25 16:57:51 +08:00
|
|
|
* @param target The target object that is, the object to which the action
|
2012-04-19 14:35:52 +08:00
|
|
|
* 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.
|
|
|
|
*/
|
2012-06-07 05:31:14 +08:00
|
|
|
void CCControl::addTargetWithActionForControlEvent(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Create the invocation object
|
2012-09-25 16:57:51 +08:00
|
|
|
CCInvocation *invocation = CCInvocation::create(target, action, controlEvent);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Add the invocation into the dispatch list for the given control event
|
2012-09-25 16:57:51 +08:00
|
|
|
CCArray* eventInvocationList = this->dispatchListforControlEvent(controlEvent);
|
2012-04-19 14:35:52 +08:00
|
|
|
eventInvocationList->addObject(invocation);
|
|
|
|
}
|
|
|
|
|
2012-06-07 05:31:14 +08:00
|
|
|
void CCControl::removeTargetWithActionForControlEvents(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvents)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// For each control events
|
2012-09-25 16:57:51 +08:00
|
|
|
for (int i = 0; i < kControlEventTotalNumber; i++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// If the given controlEvents bitmask contains the curent event
|
|
|
|
if ((controlEvents & (1 << i)))
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
this->removeTargetWithActionForControlEvent(target, action, 1 << i);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-07 05:31:14 +08:00
|
|
|
void CCControl::removeTargetWithActionForControlEvent(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Retrieve all invocations for the given control event
|
|
|
|
//<CCInvocation*>
|
2012-09-25 16:57:51 +08:00
|
|
|
CCArray *eventInvocationList = this->dispatchListforControlEvent(controlEvent);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
//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?
|
|
|
|
bool bDeleteObjects=true;
|
2012-05-05 10:44:42 +08:00
|
|
|
if (!target && !action)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
//remove objects
|
|
|
|
eventInvocationList->removeAllObjects();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//normally we would use a predicate, but this won't work here. Have to do it manually
|
|
|
|
CCObject* pObj = NULL;
|
|
|
|
CCARRAY_FOREACH(eventInvocationList, pObj)
|
|
|
|
{
|
|
|
|
CCInvocation *invocation = (CCInvocation*)pObj;
|
|
|
|
bool shouldBeRemoved=true;
|
2012-09-25 16:57:51 +08:00
|
|
|
if (target)
|
|
|
|
{
|
|
|
|
shouldBeRemoved=(target==invocation->getTarget());
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
if (action)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
shouldBeRemoved=(shouldBeRemoved && (action==invocation->getAction()));
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
// Remove the corresponding invocation object
|
|
|
|
if (shouldBeRemoved)
|
2012-09-25 16:57:51 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
eventInvocationList->removeObject(invocation, bDeleteObjects);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//CRGBA protocol
|
|
|
|
void CCControl::setColor(const ccColor3B& color)
|
|
|
|
{
|
|
|
|
m_tColor=color;
|
|
|
|
CCObject* child;
|
|
|
|
CCArray* children=getChildren();
|
|
|
|
CCARRAY_FOREACH(children, child)
|
|
|
|
{
|
|
|
|
CCRGBAProtocol* pNode = dynamic_cast<CCRGBAProtocol*>(child);
|
|
|
|
if (pNode)
|
|
|
|
{
|
|
|
|
pNode->setColor(m_tColor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ccColor3B& CCControl::getColor(void)
|
|
|
|
{
|
|
|
|
return m_tColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CCControl::setOpacity(GLubyte opacity)
|
|
|
|
{
|
|
|
|
m_cOpacity = opacity;
|
|
|
|
|
|
|
|
CCObject* child;
|
|
|
|
CCArray* children=getChildren();
|
|
|
|
CCARRAY_FOREACH(children, child)
|
|
|
|
{
|
|
|
|
CCRGBAProtocol* pNode = dynamic_cast<CCRGBAProtocol*>(child);
|
|
|
|
if (pNode)
|
|
|
|
{
|
|
|
|
pNode->setOpacity(opacity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
GLubyte CCControl::getOpacity()
|
|
|
|
{
|
|
|
|
return m_cOpacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
void CCControl::setOpacityModifyRGB(bool bOpacityModifyRGB)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
m_bIsOpacityModifyRGB=bOpacityModifyRGB;
|
|
|
|
CCObject* child;
|
2012-04-19 14:35:52 +08:00
|
|
|
CCArray* children=getChildren();
|
|
|
|
CCARRAY_FOREACH(children, child)
|
|
|
|
{
|
|
|
|
CCRGBAProtocol* pNode = dynamic_cast<CCRGBAProtocol*>(child);
|
|
|
|
if (pNode)
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
pNode->setOpacityModifyRGB(bOpacityModifyRGB);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
bool CCControl::isOpacityModifyRGB()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return m_bIsOpacityModifyRGB;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CCPoint CCControl::getTouchLocation(CCTouch* touch)
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
CCPoint touchLocation = touch->getLocation(); // Get the touch position
|
|
|
|
touchLocation = this->convertToNodeSpace(touchLocation); // Convert to the node space of this class
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return touchLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCControl::isTouchInside(CCTouch* touch)
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
CCPoint touchLocation = touch->getLocation(); // Get the touch position
|
|
|
|
touchLocation = this->getParent()->convertToNodeSpace(touchLocation);
|
2012-04-19 14:35:52 +08:00
|
|
|
CCRect bBox=boundingBox();
|
2012-08-01 15:30:12 +08:00
|
|
|
return bBox.containsPoint(touchLocation);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCArray* CCControl::dispatchListforControlEvent(CCControlEvent controlEvent)
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
CCArray* invocationList = (CCArray*)m_pDispatchTable->objectForKey(controlEvent);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// If the invocation list does not exist for the dispatch table, we create it
|
|
|
|
if (invocationList == NULL)
|
|
|
|
{
|
2012-07-23 22:49:11 +08:00
|
|
|
invocationList = CCArray::createWithCapacity(1);
|
2012-09-25 16:57:51 +08:00
|
|
|
m_pDispatchTable->setObject(invocationList, controlEvent);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
return invocationList;
|
|
|
|
}
|
|
|
|
|
2012-09-25 17:26:09 +08:00
|
|
|
void CCControl::needsLayout()
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2012-06-15 16:47:30 +08:00
|
|
|
void CCControl::setEnabled(bool bEnabled)
|
|
|
|
{
|
|
|
|
m_bEnabled = bEnabled;
|
2012-09-25 17:26:09 +08:00
|
|
|
if(m_bEnabled) {
|
|
|
|
m_eState = CCControlStateNormal;
|
|
|
|
} else {
|
|
|
|
m_eState = CCControlStateDisabled;
|
|
|
|
}
|
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
this->needsLayout();
|
2012-06-15 16:47:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCControl::isEnabled()
|
|
|
|
{
|
|
|
|
return m_bEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControl::setSelected(bool bSelected)
|
|
|
|
{
|
|
|
|
m_bSelected = bSelected;
|
2012-09-25 16:57:51 +08:00
|
|
|
this->needsLayout();
|
2012-06-15 16:47:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCControl::isSelected()
|
|
|
|
{
|
|
|
|
return m_bSelected;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCControl::setHighlighted(bool bHighlighted)
|
|
|
|
{
|
|
|
|
m_bHighlighted = bHighlighted;
|
2012-09-25 16:57:51 +08:00
|
|
|
this->needsLayout();
|
2012-06-15 16:47:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCControl::isHighlighted()
|
|
|
|
{
|
|
|
|
return m_bHighlighted;
|
|
|
|
}
|
|
|
|
|
2012-09-25 17:26:09 +08:00
|
|
|
bool CCControl::hasVisibleParents()
|
|
|
|
{
|
|
|
|
CCNode* pParent = this->getParent();
|
|
|
|
for( CCNode *c = pParent; c != NULL; c = c->getParent() )
|
|
|
|
{
|
|
|
|
if( !c->isVisible() )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2012-04-27 18:47:49 +08:00
|
|
|
NS_CC_EXT_END
|