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"
|
2013-10-15 18:00:03 +08:00
|
|
|
#include "CCMenu.h"
|
|
|
|
#include "CCTouch.h"
|
2013-07-26 14:37:26 +08:00
|
|
|
#include "CCInvocation.h"
|
2013-10-15 18:00:03 +08:00
|
|
|
#include "CCEventDispatcher.h"
|
|
|
|
#include "CCEventListenerTouch.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
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Control::Control()
|
2013-07-29 14:07:57 +08:00
|
|
|
: _enabled(false)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _selected(false)
|
|
|
|
, _highlighted(false)
|
2013-07-29 14:07:57 +08:00
|
|
|
, _hasVisibleParents(false)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _dispatchTable(NULL)
|
2013-07-29 14:07:57 +08:00
|
|
|
, _isOpacityModifyRGB(false)
|
|
|
|
, _state(State::NORMAL)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Control* Control::create()
|
2012-11-08 10:41:14 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Control* pRet = new Control();
|
2012-11-08 10:41:14 +08:00
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool Control::init()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
if (Layer::init())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-15 16:47:30 +08:00
|
|
|
//this->setTouchEnabled(true);
|
2013-06-15 14:03:30 +08:00
|
|
|
//_isTouchEnabled=true;
|
2012-04-19 14:35:52 +08:00
|
|
|
// Initialise instance variables
|
2013-07-26 14:37:26 +08:00
|
|
|
_state=Control::State::NORMAL;
|
2012-09-25 16:57:51 +08:00
|
|
|
setEnabled(true);
|
|
|
|
setSelected(false);
|
|
|
|
setHighlighted(false);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Initialise the tables
|
2013-08-22 11:12:09 +08:00
|
|
|
_dispatchTable = new Dictionary();
|
|
|
|
_dispatchTable->init();
|
2013-05-24 11:42:27 +08:00
|
|
|
|
2013-11-01 20:53:51 +08:00
|
|
|
auto dispatcher = Director::getInstance()->getEventDispatcher();
|
|
|
|
auto touchListener = EventListenerTouchOneByOne::create();
|
|
|
|
touchListener->onTouchBegan = CC_CALLBACK_2(Control::onTouchBegan, this);
|
|
|
|
touchListener->onTouchMoved = CC_CALLBACK_2(Control::onTouchMoved, this);
|
|
|
|
touchListener->onTouchEnded = CC_CALLBACK_2(Control::onTouchEnded, this);
|
|
|
|
touchListener->onTouchCancelled = CC_CALLBACK_2(Control::onTouchCancelled, this);
|
|
|
|
|
|
|
|
dispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Control::~Control()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_dispatchTable);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
void Control::sendActionsForControlEvents(EventType 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
|
2013-07-26 14:37:26 +08:00
|
|
|
if (((int)controlEvents & (1 << i)))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Call invocations
|
2013-06-20 14:15:53 +08:00
|
|
|
// <Invocation*>
|
2013-07-26 14:37:26 +08:00
|
|
|
Array* invocationList = this->dispatchListforControlEvent((Control::EventType)(1<<i));
|
2013-06-20 14:15:53 +08:00
|
|
|
Object* pObj = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
CCARRAY_FOREACH(invocationList, pObj)
|
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
Invocation* invocation = static_cast<Invocation*>(pObj);
|
2012-04-19 14:35:52 +08:00
|
|
|
invocation->invoke(this);
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2013-05-24 11:42:27 +08:00
|
|
|
//Call ScriptFunc
|
2013-07-16 09:55:06 +08:00
|
|
|
if (kScriptTypeLua == _scriptType)
|
2013-05-24 11:42:27 +08:00
|
|
|
{
|
2013-07-22 17:12:53 +08:00
|
|
|
cocos2d::BasicScriptData data(this,(void*)&controlEvents);
|
2013-07-16 09:55:06 +08:00
|
|
|
cocos2d::ScriptEvent event(cocos2d::kControlEvent,(void*)&data);
|
2013-07-22 17:24:54 +08:00
|
|
|
cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
|
2013-05-24 11:42:27 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-26 14:37:26 +08:00
|
|
|
void Control::addTargetWithActionForControlEvents(Object* target, Handler action, EventType 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
|
2013-07-26 14:37:26 +08:00
|
|
|
if (((int)controlEvents & (1 << i)))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 14:37:26 +08:00
|
|
|
this->addTargetWithActionForControlEvent(target, action, (EventType)(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.
|
|
|
|
*/
|
2013-07-26 14:37:26 +08:00
|
|
|
void Control::addTargetWithActionForControlEvent(Object* target, Handler action, EventType controlEvent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Create the invocation object
|
2013-06-20 14:15:53 +08:00
|
|
|
Invocation *invocation = Invocation::create(target, action, controlEvent);
|
2012-09-25 16:57:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Add the invocation into the dispatch list for the given control event
|
2013-06-20 14:15:53 +08:00
|
|
|
Array* eventInvocationList = this->dispatchListforControlEvent(controlEvent);
|
2012-04-19 14:35:52 +08:00
|
|
|
eventInvocationList->addObject(invocation);
|
|
|
|
}
|
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
void Control::removeTargetWithActionForControlEvents(Object* target, Handler action, EventType 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
|
2013-07-26 14:37:26 +08:00
|
|
|
if (((int)controlEvents & (1 << i)))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 14:37:26 +08:00
|
|
|
this->removeTargetWithActionForControlEvent(target, action, (EventType)(1 << i));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
void Control::removeTargetWithActionForControlEvent(Object* target, Handler action, EventType controlEvent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Retrieve all invocations for the given control event
|
2013-06-20 14:15:53 +08:00
|
|
|
//<Invocation*>
|
|
|
|
Array *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
|
2013-06-20 14:15:53 +08:00
|
|
|
Object* pObj = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
CCARRAY_FOREACH(eventInvocationList, pObj)
|
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
Invocation *invocation = static_cast<Invocation*>(pObj);
|
2012-04-19 14:35:52 +08:00
|
|
|
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
|
2013-06-20 14:15:53 +08:00
|
|
|
void Control::setOpacityModifyRGB(bool bOpacityModifyRGB)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_isOpacityModifyRGB=bOpacityModifyRGB;
|
2013-06-20 14:15:53 +08:00
|
|
|
Object* child;
|
|
|
|
Array* children=getChildren();
|
2012-04-19 14:35:52 +08:00
|
|
|
CCARRAY_FOREACH(children, child)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
RGBAProtocol* pNode = dynamic_cast<RGBAProtocol*>(child);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pNode)
|
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
pNode->setOpacityModifyRGB(bOpacityModifyRGB);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-05 15:06:38 +08:00
|
|
|
bool Control::isOpacityModifyRGB() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _isOpacityModifyRGB;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Point Control::getTouchLocation(Touch* touch)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Point touchLocation = touch->getLocation(); // Get the touch position
|
2012-09-25 16:57:51 +08:00
|
|
|
touchLocation = this->convertToNodeSpace(touchLocation); // Convert to the node space of this class
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return touchLocation;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool Control::isTouchInside(Touch* touch)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Point touchLocation = touch->getLocation(); // Get the touch position
|
2012-09-25 16:57:51 +08:00
|
|
|
touchLocation = this->getParent()->convertToNodeSpace(touchLocation);
|
2013-07-17 09:16:04 +08:00
|
|
|
Rect bBox = getBoundingBox();
|
2012-08-01 15:30:12 +08:00
|
|
|
return bBox.containsPoint(touchLocation);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 14:37:26 +08:00
|
|
|
Array* Control::dispatchListforControlEvent(EventType controlEvent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-08-09 10:56:39 +08:00
|
|
|
Array* invocationList = static_cast<Array*>(_dispatchTable->objectForKey((int)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)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
invocationList = Array::createWithCapacity(1);
|
2013-07-26 14:37:26 +08:00
|
|
|
_dispatchTable->setObject(invocationList, (int)controlEvent);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
return invocationList;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Control::needsLayout()
|
2012-09-25 17:26:09 +08:00
|
|
|
{
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Control::setEnabled(bool bEnabled)
|
2012-06-15 16:47:30 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_enabled = bEnabled;
|
|
|
|
if(_enabled) {
|
2013-07-26 14:37:26 +08:00
|
|
|
_state = Control::State::NORMAL;
|
2012-09-25 17:26:09 +08:00
|
|
|
} else {
|
2013-07-26 14:37:26 +08:00
|
|
|
_state = Control::State::DISABLED;
|
2012-09-25 17:26:09 +08:00
|
|
|
}
|
|
|
|
|
2012-09-25 16:57:51 +08:00
|
|
|
this->needsLayout();
|
2012-06-15 16:47:30 +08:00
|
|
|
}
|
|
|
|
|
2013-07-19 07:30:19 +08:00
|
|
|
bool Control::isEnabled() const
|
2012-06-15 16:47:30 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _enabled;
|
2012-06-15 16:47:30 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Control::setSelected(bool bSelected)
|
2012-06-15 16:47:30 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_selected = bSelected;
|
2012-09-25 16:57:51 +08:00
|
|
|
this->needsLayout();
|
2012-06-15 16:47:30 +08:00
|
|
|
}
|
|
|
|
|
2013-07-19 07:30:19 +08:00
|
|
|
bool Control::isSelected() const
|
2012-06-15 16:47:30 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _selected;
|
2012-06-15 16:47:30 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void Control::setHighlighted(bool bHighlighted)
|
2012-06-15 16:47:30 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_highlighted = bHighlighted;
|
2012-09-25 16:57:51 +08:00
|
|
|
this->needsLayout();
|
2012-06-15 16:47:30 +08:00
|
|
|
}
|
|
|
|
|
2013-07-19 07:30:19 +08:00
|
|
|
bool Control::isHighlighted() const
|
2012-06-15 16:47:30 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _highlighted;
|
2012-06-15 16:47:30 +08:00
|
|
|
}
|
|
|
|
|
2013-07-19 07:30:19 +08:00
|
|
|
bool Control::hasVisibleParents() const
|
2012-09-25 17:26:09 +08:00
|
|
|
{
|
2013-07-19 07:30:19 +08:00
|
|
|
auto parent = this->getParent();
|
|
|
|
for( auto c = parent; c != NULL; c = c->getParent() )
|
2012-09-25 17:26:09 +08:00
|
|
|
{
|
|
|
|
if( !c->isVisible() )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2012-09-25 16:57:51 +08:00
|
|
|
}
|
2013-11-06 10:55:24 +08:00
|
|
|
|
|
|
|
Control::EventType operator|(Control::EventType a, Control::EventType b) {
|
|
|
|
return static_cast<Control::EventType>(static_cast<int>(a) | static_cast<int>(b));
|
|
|
|
}
|
|
|
|
|
2012-04-27 18:47:49 +08:00
|
|
|
NS_CC_EXT_END
|