axmol/extensions/CocoStudio/GUI/System/UIInputManager.cpp

208 lines
5.9 KiB
C++
Raw Normal View History

2013-09-13 22:20:20 +08:00
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
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 "UIInputManager.h"
#include "UIHelper.h"
NS_CC_EXT_BEGIN
UIInputManager::UIInputManager():
2013-09-16 20:54:13 +08:00
_manageredWidget(NULL),
_touchDown(false),
_longClickTime(0.0),
_longClickRecordTime(0.0),
_checkedDoubleClickWidget(NULL),
_rootWidget(NULL)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_manageredWidget = CCArray::create();
_manageredWidget->retain();
_checkedDoubleClickWidget = CCArray::create();
_checkedDoubleClickWidget->retain();
_selectedWidgets = CCArray::create();
_selectedWidgets->retain();
2013-09-13 22:20:20 +08:00
}
UIInputManager::~UIInputManager()
{
2013-09-16 20:54:13 +08:00
_manageredWidget->removeAllObjects();
CC_SAFE_RELEASE_NULL(_manageredWidget);
_checkedDoubleClickWidget->removeAllObjects();
CC_SAFE_RELEASE_NULL(_checkedDoubleClickWidget);
_selectedWidgets->removeAllObjects();
CC_SAFE_RELEASE_NULL(_selectedWidgets);
2013-09-13 22:20:20 +08:00
}
void UIInputManager::registWidget(UIWidget* widget)
{
if (!widget)
{
return;
}
2013-09-16 20:54:13 +08:00
if (_manageredWidget->containsObject(widget))
2013-09-13 22:20:20 +08:00
{
return;
}
2013-09-16 20:54:13 +08:00
_manageredWidget->addObject(widget);
2013-09-13 22:20:20 +08:00
}
bool UIInputManager::checkTouchEvent(UIWidget *root, const Point &touchPoint)
{
ccArray* arrayRootChildren = root->getChildren()->data;
int length = arrayRootChildren->num;
for (int i=length-1; i >= 0; i--)
{
UIWidget* widget = (UIWidget*)(arrayRootChildren->arr[i]);
if (checkTouchEvent(widget, touchPoint))
{
return true;
}
}
if (root->isEnabled() && root->isTouchEnabled() && root->hitTest(touchPoint) && root->clippingParentAreaContainPoint(touchPoint))
{
2013-09-16 20:54:13 +08:00
_selectedWidgets->addObject(root);
2013-09-13 22:20:20 +08:00
root->onTouchBegan(touchPoint);
return true;
}
return false;
}
void UIInputManager::removeManageredWidget(UIWidget* widget)
{
if (!widget)
{
return;
}
2013-09-16 20:54:13 +08:00
if (!_manageredWidget->containsObject(widget))
2013-09-13 22:20:20 +08:00
{
return;
}
2013-09-16 20:54:13 +08:00
_manageredWidget->removeObject(widget);
2013-09-13 22:20:20 +08:00
}
bool UIInputManager::checkEventWidget(const Point &touchPoint)
{
2013-09-16 20:54:13 +08:00
checkTouchEvent(_rootWidget,touchPoint);
return (_selectedWidgets->count() > 0);
2013-09-13 22:20:20 +08:00
}
void UIInputManager::addCheckedDoubleClickWidget(UIWidget* widget)
{
2013-09-16 20:54:13 +08:00
if (_checkedDoubleClickWidget->containsObject(widget))
2013-09-13 22:20:20 +08:00
{
return;
}
2013-09-16 20:54:13 +08:00
_checkedDoubleClickWidget->addObject(widget);
2013-09-13 22:20:20 +08:00
}
void UIInputManager::update(float dt)
{
2013-09-16 20:54:13 +08:00
if (_touchDown)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_longClickRecordTime += dt;
if (_longClickRecordTime >= _longClickTime)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_longClickRecordTime = 0;
_touchDown = false;
2013-09-13 22:20:20 +08:00
// m_pCurSelectedWidget->onTouchLongClicked(touchBeganedPoint);
}
}
2013-09-16 20:54:13 +08:00
ccArray* arrayWidget = _checkedDoubleClickWidget->data;
2013-09-13 22:20:20 +08:00
int widgetCount = arrayWidget->num;
for (int i=0;i<widgetCount;i++)
{
UIWidget* widget = (UIWidget*)(arrayWidget->arr[i]);
if (!widget->isVisible())
{
continue;
}
}
}
bool UIInputManager::onTouchBegan(Touch* touch)
{
2013-09-16 20:54:13 +08:00
_touchBeganedPoint.x = touch->getLocation().x;
_touchBeganedPoint.y = touch->getLocation().y;
_touchDown = true;
return checkEventWidget(_touchBeganedPoint);
2013-09-13 22:20:20 +08:00
}
void UIInputManager::onTouchMoved(Touch* touch)
{
2013-09-16 20:54:13 +08:00
_touchMovedPoint.x = touch->getLocation().x;
_touchMovedPoint.y = touch->getLocation().y;
ccArray* selectedWidgetArray = _selectedWidgets->data;
2013-09-13 22:20:20 +08:00
int length = selectedWidgetArray->num;
for (int i=0; i<length; ++i)
{
UIWidget* hitWidget = (UIWidget*)(selectedWidgetArray->arr[i]);
2013-09-16 20:54:13 +08:00
hitWidget->onTouchMoved(_touchMovedPoint);
2013-09-13 22:20:20 +08:00
}
2013-09-16 20:54:13 +08:00
if (_touchDown)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_longClickRecordTime = 0;
_touchDown = false;
2013-09-13 22:20:20 +08:00
}
}
void UIInputManager::onTouchEnd(Touch* touch)
{
2013-09-16 20:54:13 +08:00
_touchDown = false;
_touchEndedPoint.x = touch->getLocation().x;
_touchEndedPoint.y = touch->getLocation().y;
ccArray* selectedWidgetArray = _selectedWidgets->data;
2013-09-13 22:20:20 +08:00
int length = selectedWidgetArray->num;
for (int i=0; i<length; ++i)
{
UIWidget* hitWidget = (UIWidget*)(selectedWidgetArray->arr[i]);
2013-09-16 20:54:13 +08:00
hitWidget->onTouchEnded(_touchEndedPoint);
2013-09-13 22:20:20 +08:00
}
2013-09-16 20:54:13 +08:00
_selectedWidgets->removeAllObjects();
2013-09-13 22:20:20 +08:00
}
void UIInputManager::onTouchCancelled(Touch* touch)
{
2013-09-16 20:54:13 +08:00
_touchDown = false;
_touchEndedPoint.x = touch->getLocation().x;
_touchEndedPoint.y = touch->getLocation().y;
ccArray* selectedWidgetArray = _selectedWidgets->data;
2013-09-13 22:20:20 +08:00
int length = selectedWidgetArray->num;
for (int i=0; i<length; ++i)
{
UIWidget* hitWidget = (UIWidget*)(selectedWidgetArray->arr[i]);
2013-09-16 20:54:13 +08:00
hitWidget->onTouchCancelled(_touchEndedPoint);
2013-09-13 22:20:20 +08:00
}
2013-09-16 20:54:13 +08:00
_selectedWidgets->removeAllObjects();
2013-09-13 22:20:20 +08:00
}
void UIInputManager::setRootWidget(UIWidget *root)
{
2013-09-16 20:54:13 +08:00
_rootWidget = root;
2013-09-13 22:20:20 +08:00
}
UIWidget* UIInputManager::getRootWidget()
{
2013-09-16 20:54:13 +08:00
return _rootWidget;
2013-09-13 22:20:20 +08:00
}
NS_CC_EXT_END