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"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCMenu.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCTouch.h"
|
2013-07-26 14:37:26 +08:00
|
|
|
#include "CCInvocation.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCEventDispatcher.h"
|
|
|
|
#include "base/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)
|
|
|
|
, _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
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
Control* pRet = new (std::nothrow) Control();
|
2012-11-08 10:41:14 +08:00
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
2014-07-10 00:45:27 +08:00
|
|
|
return nullptr;
|
2012-11-08 10:41:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
// 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
|
|
|
|
2013-11-01 20:53:51 +08:00
|
|
|
auto dispatcher = Director::getInstance()->getEventDispatcher();
|
|
|
|
auto touchListener = EventListenerTouchOneByOne::create();
|
2014-04-02 17:01:11 +08:00
|
|
|
touchListener->setSwallowTouches(true);
|
2013-11-01 20:53:51 +08:00
|
|
|
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-12-07 14:36:07 +08:00
|
|
|
for (auto iter = _dispatchTable.begin(); iter != _dispatchTable.end(); ++iter)
|
|
|
|
{
|
|
|
|
delete iter->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
_dispatchTable.clear();
|
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
|
|
|
{
|
2014-03-13 10:14:24 +08:00
|
|
|
retain();
|
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
|
|
|
{
|
2015-12-11 23:37:58 +08:00
|
|
|
// If the given controlEvents bitmask contains the current 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-12-07 14:36:07 +08:00
|
|
|
const auto& invocationList = this->dispatchListforControlEvent((Control::EventType)(1<<i));
|
2013-12-20 05:34:41 +08:00
|
|
|
|
|
|
|
for(const auto &invocation : invocationList) {
|
2012-04-19 14:35:52 +08:00
|
|
|
invocation->invoke(this);
|
2013-12-20 05:34:41 +08:00
|
|
|
}
|
2013-12-07 14:36:07 +08:00
|
|
|
|
2014-02-20 16:40:46 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
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
|
|
|
}
|
2014-02-20 16:40:46 +08:00
|
|
|
#endif
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2014-03-13 10:14:24 +08:00
|
|
|
release();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2014-02-20 10:53:49 +08:00
|
|
|
void Control::addTargetWithActionForControlEvents(Ref* 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
|
|
|
{
|
2015-12-11 23:37:58 +08:00
|
|
|
// If the given controlEvents bitmask contains the current 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.
|
2015-12-11 23:37:58 +08:00
|
|
|
* The action message may optionally include the sender and the event as
|
2012-04-19 14:35:52 +08:00
|
|
|
* 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.
|
2014-07-10 00:45:27 +08:00
|
|
|
* @param action A selector identifying an action message. It cannot be nullptr.
|
2012-04-19 14:35:52 +08:00
|
|
|
* @param controlEvent A control event for which the action message is sent.
|
|
|
|
* See "CCControlEvent" for constants.
|
|
|
|
*/
|
2014-02-20 10:53:49 +08:00
|
|
|
void Control::addTargetWithActionForControlEvent(Ref* 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-12-07 14:36:07 +08:00
|
|
|
auto& eventInvocationList = this->dispatchListforControlEvent(controlEvent);
|
|
|
|
eventInvocationList.pushBack(invocation);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void Control::removeTargetWithActionForControlEvents(Ref* 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
|
|
|
{
|
2015-12-11 23:37:58 +08:00
|
|
|
// If the given controlEvents bitmask contains the current 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void Control::removeTargetWithActionForControlEvent(Ref* target, Handler action, EventType controlEvent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Retrieve all invocations for the given control event
|
2013-12-07 14:36:07 +08:00
|
|
|
auto& 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?
|
2014-02-21 11:24:37 +08:00
|
|
|
|
2012-05-05 10:44:42 +08:00
|
|
|
if (!target && !action)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
//remove objects
|
2013-12-07 14:36:07 +08:00
|
|
|
eventInvocationList.clear();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-17 17:45:29 +08:00
|
|
|
std::vector<Invocation*> tobeRemovedInvocations;
|
|
|
|
|
|
|
|
//normally we would use a predicate, but this won't work here. Have to do it manually
|
2013-12-20 05:34:41 +08:00
|
|
|
for(const auto &invocation : eventInvocationList) {
|
2013-12-07 14:36:07 +08:00
|
|
|
bool shouldBeRemoved=true;
|
|
|
|
if (target)
|
|
|
|
{
|
|
|
|
shouldBeRemoved=(target==invocation->getTarget());
|
|
|
|
}
|
|
|
|
if (action)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-07 14:36:07 +08:00
|
|
|
shouldBeRemoved=(shouldBeRemoved && (action==invocation->getAction()));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-12-07 14:36:07 +08:00
|
|
|
// Remove the corresponding invocation object
|
|
|
|
if (shouldBeRemoved)
|
|
|
|
{
|
2013-12-17 17:45:29 +08:00
|
|
|
tobeRemovedInvocations.push_back(invocation);
|
2013-12-07 14:36:07 +08:00
|
|
|
}
|
2013-12-20 05:34:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for(const auto &invocation : tobeRemovedInvocations) {
|
2014-02-21 11:24:37 +08:00
|
|
|
eventInvocationList.eraseObject(invocation);
|
2013-12-20 05:34:41 +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-11-28 18:23:06 +08:00
|
|
|
|
2013-12-19 17:05:59 +08:00
|
|
|
for(auto child : _children){
|
|
|
|
child->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
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 Control::getTouchLocation(Touch* touch)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 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
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 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-12-07 14:36:07 +08:00
|
|
|
Vector<Invocation*>& Control::dispatchListforControlEvent(EventType controlEvent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-07 14:36:07 +08:00
|
|
|
Vector<Invocation*>* invocationList = nullptr;
|
|
|
|
auto iter = _dispatchTable.find((int)controlEvent);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// If the invocation list does not exist for the dispatch table, we create it
|
2013-12-07 14:36:07 +08:00
|
|
|
if (iter == _dispatchTable.end())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
invocationList = new (std::nothrow) Vector<Invocation*>();
|
2013-12-07 14:36:07 +08:00
|
|
|
_dispatchTable[(int)controlEvent] = invocationList;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
invocationList = iter->second;
|
|
|
|
}
|
|
|
|
return *invocationList;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
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();
|
2014-07-10 00:45:27 +08:00
|
|
|
for( auto c = parent; c != nullptr; 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
|