2014-01-07 11:25:07 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-04-23 14:30:38 +08:00
|
|
|
#include "CCEGLViewProtocol.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCTouch.h"
|
2012-04-23 14:30:38 +08:00
|
|
|
#include "CCDirector.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCSet.h"
|
|
|
|
#include "CCEventDispatcher.h"
|
2013-09-12 14:26:39 +08:00
|
|
|
|
2012-04-23 14:30:38 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
namespace {
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
static Touch* g_touches[EventTouch::MAX_TOUCHES] = { nullptr };
|
2013-09-12 14:26:39 +08:00
|
|
|
static unsigned int g_indexBitsUsed = 0;
|
2013-09-03 18:22:03 +08:00
|
|
|
// System touch pointer ID (It may not be ascending order number) <-> Ascending order number from 0
|
2013-12-05 17:19:01 +08:00
|
|
|
static std::map<int, int> g_touchIdReorderMap;
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
static int getUnUsedIndex()
|
|
|
|
{
|
|
|
|
int i;
|
2013-09-12 14:26:39 +08:00
|
|
|
int temp = g_indexBitsUsed;
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2013-09-20 19:19:31 +08:00
|
|
|
for (i = 0; i < EventTouch::MAX_TOUCHES; i++) {
|
2013-09-03 18:22:03 +08:00
|
|
|
if (! (temp & 0x00000001)) {
|
2013-09-12 14:26:39 +08:00
|
|
|
g_indexBitsUsed |= (1 << i);
|
2013-09-03 18:22:03 +08:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
temp >>= 1;
|
2012-04-20 15:23:00 +08:00
|
|
|
}
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
// all bits are used
|
|
|
|
return -1;
|
2012-04-20 15:23:00 +08:00
|
|
|
}
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
static void removeUsedIndexBit(int index)
|
2012-04-20 15:23:00 +08:00
|
|
|
{
|
2013-09-20 19:19:31 +08:00
|
|
|
if (index < 0 || index >= EventTouch::MAX_TOUCHES)
|
2013-09-03 18:22:03 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int temp = 1 << index;
|
|
|
|
temp = ~temp;
|
2013-09-12 14:26:39 +08:00
|
|
|
g_indexBitsUsed &= temp;
|
2012-04-20 15:23:00 +08:00
|
|
|
}
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
EGLViewProtocol::EGLViewProtocol()
|
2013-12-18 17:47:20 +08:00
|
|
|
: _delegate(nullptr)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _scaleX(1.0f)
|
|
|
|
, _scaleY(1.0f)
|
2013-07-26 18:18:58 +08:00
|
|
|
, _resolutionPolicy(ResolutionPolicy::UNKNOWN)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
EGLViewProtocol::~EGLViewProtocol()
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-08 10:26:53 +08:00
|
|
|
void EGLViewProtocol::pollInputEvents()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void EGLViewProtocol::setDesignResolutionSize(float width, float height, ResolutionPolicy resolutionPolicy)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2013-07-26 18:18:58 +08:00
|
|
|
CCASSERT(resolutionPolicy != ResolutionPolicy::UNKNOWN, "should set resolutionPolicy");
|
2012-08-01 16:56:12 +08:00
|
|
|
|
2012-04-23 14:39:27 +08:00
|
|
|
if (width == 0.0f || height == 0.0f)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_designResolutionSize.setSize(width, height);
|
2012-08-01 16:56:12 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_scaleX = (float)_screenSize.width / _designResolutionSize.width;
|
|
|
|
_scaleY = (float)_screenSize.height / _designResolutionSize.height;
|
2012-08-01 16:56:12 +08:00
|
|
|
|
2013-07-26 18:18:58 +08:00
|
|
|
if (resolutionPolicy == ResolutionPolicy::NO_BORDER)
|
2012-08-01 16:56:12 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_scaleX = _scaleY = MAX(_scaleX, _scaleY);
|
2012-08-01 16:56:12 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 18:18:58 +08:00
|
|
|
if (resolutionPolicy == ResolutionPolicy::SHOW_ALL)
|
2012-08-01 16:56:12 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_scaleX = _scaleY = MIN(_scaleX, _scaleY);
|
2012-08-01 16:56:12 +08:00
|
|
|
}
|
2012-05-03 15:05:19 +08:00
|
|
|
|
2013-07-26 18:18:58 +08:00
|
|
|
if ( resolutionPolicy == ResolutionPolicy::FIXED_HEIGHT) {
|
2013-06-15 14:03:30 +08:00
|
|
|
_scaleX = _scaleY;
|
|
|
|
_designResolutionSize.width = ceilf(_screenSize.width/_scaleX);
|
2013-03-19 03:06:01 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 18:18:58 +08:00
|
|
|
if ( resolutionPolicy == ResolutionPolicy::FIXED_WIDTH) {
|
2013-06-15 14:03:30 +08:00
|
|
|
_scaleY = _scaleX;
|
|
|
|
_designResolutionSize.height = ceilf(_screenSize.height/_scaleY);
|
2013-03-19 03:06:01 +08:00
|
|
|
}
|
|
|
|
|
2012-08-01 16:56:12 +08:00
|
|
|
// calculate the rect of viewport
|
2013-06-15 14:03:30 +08:00
|
|
|
float viewPortW = _designResolutionSize.width * _scaleX;
|
|
|
|
float viewPortH = _designResolutionSize.height * _scaleY;
|
2012-04-23 14:39:27 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_viewPortRect.setRect((_screenSize.width - viewPortW) / 2, (_screenSize.height - viewPortH) / 2, viewPortW, viewPortH);
|
2012-08-07 14:29:46 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_resolutionPolicy = resolutionPolicy;
|
2012-08-07 14:29:46 +08:00
|
|
|
|
2012-10-18 15:53:08 +08:00
|
|
|
// reset director's member variables to fit visible rect
|
2014-01-24 07:36:55 +08:00
|
|
|
auto director = Director::getInstance();
|
|
|
|
director->_winSizeInPoints = getDesignResolutionSize();
|
|
|
|
director->createStatsLabel();
|
|
|
|
director->setGLDefaultValues();
|
2012-08-07 14:29:46 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
const Size& EGLViewProtocol::getDesignResolutionSize() const
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _designResolutionSize;
|
2012-08-01 16:56:12 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
const Size& EGLViewProtocol::getFrameSize() const
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _screenSize;
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void EGLViewProtocol::setFrameSize(float width, float height)
|
2012-08-07 14:29:46 +08:00
|
|
|
{
|
2013-07-12 14:30:26 +08:00
|
|
|
_designResolutionSize = _screenSize = Size(width, height);
|
2012-08-07 14:29:46 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Size EGLViewProtocol::getVisibleSize() const
|
2012-08-01 16:56:12 +08:00
|
|
|
{
|
2013-07-26 18:18:58 +08:00
|
|
|
if (_resolutionPolicy == ResolutionPolicy::NO_BORDER)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2013-07-12 14:30:26 +08:00
|
|
|
return Size(_screenSize.width/_scaleX, _screenSize.height/_scaleY);
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
2012-08-07 14:29:46 +08:00
|
|
|
else
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _designResolutionSize;
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point EGLViewProtocol::getVisibleOrigin() const
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2013-07-26 18:18:58 +08:00
|
|
|
if (_resolutionPolicy == ResolutionPolicy::NO_BORDER)
|
2012-08-07 14:29:46 +08:00
|
|
|
{
|
2013-07-12 14:30:26 +08:00
|
|
|
return Point((_designResolutionSize.width - _screenSize.width/_scaleX)/2,
|
2013-06-15 14:03:30 +08:00
|
|
|
(_designResolutionSize.height - _screenSize.height/_scaleY)/2);
|
2012-08-07 14:29:46 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-12 14:47:36 +08:00
|
|
|
return Point::ZERO;
|
2012-08-07 14:29:46 +08:00
|
|
|
}
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
void EGLViewProtocol::setTouchDelegate(EGLTouchDelegate * delegate)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
_delegate = delegate;
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void EGLViewProtocol::setViewPortInPoints(float x , float y , float w , float h)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
glViewport((GLint)(x * _scaleX + _viewPortRect.origin.x),
|
|
|
|
(GLint)(y * _scaleY + _viewPortRect.origin.y),
|
|
|
|
(GLsizei)(w * _scaleX),
|
|
|
|
(GLsizei)(h * _scaleY));
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void EGLViewProtocol::setScissorInPoints(float x , float y , float w , float h)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
glScissor((GLint)(x * _scaleX + _viewPortRect.origin.x),
|
|
|
|
(GLint)(y * _scaleY + _viewPortRect.origin.y),
|
|
|
|
(GLsizei)(w * _scaleX),
|
|
|
|
(GLsizei)(h * _scaleY));
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool EGLViewProtocol::isScissorEnabled()
|
2013-03-29 01:28:26 +08:00
|
|
|
{
|
2013-06-07 12:50:02 +08:00
|
|
|
return (GL_FALSE == glIsEnabled(GL_SCISSOR_TEST)) ? false : true;
|
2013-03-29 01:28:26 +08:00
|
|
|
}
|
|
|
|
|
2013-11-23 08:09:05 +08:00
|
|
|
Rect EGLViewProtocol::getScissorRect() const
|
2013-03-29 01:28:26 +08:00
|
|
|
{
|
|
|
|
GLfloat params[4];
|
|
|
|
glGetFloatv(GL_SCISSOR_BOX, params);
|
2013-06-15 14:03:30 +08:00
|
|
|
float x = (params[0] - _viewPortRect.origin.x) / _scaleX;
|
|
|
|
float y = (params[1] - _viewPortRect.origin.y) / _scaleY;
|
|
|
|
float w = params[2] / _scaleX;
|
|
|
|
float h = params[3] / _scaleY;
|
2013-07-12 14:30:26 +08:00
|
|
|
return Rect(x, y, w, h);
|
2013-03-29 01:28:26 +08:00
|
|
|
}
|
|
|
|
|
2013-11-23 08:09:05 +08:00
|
|
|
void EGLViewProtocol::setViewName(const std::string& viewname )
|
2013-03-29 15:32:30 +08:00
|
|
|
{
|
2013-11-23 08:09:05 +08:00
|
|
|
_viewName = viewname;
|
2013-03-29 15:32:30 +08:00
|
|
|
}
|
|
|
|
|
2013-11-23 08:09:05 +08:00
|
|
|
const std::string& EGLViewProtocol::getViewName() const
|
2013-03-29 15:32:30 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _viewName;
|
2012-09-20 01:56:58 +08:00
|
|
|
}
|
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
void EGLViewProtocol::handleTouchesBegin(int num, int ids[], float xs[], float ys[])
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2013-12-05 17:19:01 +08:00
|
|
|
int id = 0;
|
2013-09-12 14:26:39 +08:00
|
|
|
float x = 0.0f;
|
|
|
|
float y = 0.0f;
|
2013-12-18 17:47:20 +08:00
|
|
|
int unusedIndex = 0;
|
2013-09-20 19:19:31 +08:00
|
|
|
EventTouch touchEvent;
|
2013-09-12 14:26:39 +08:00
|
|
|
|
2012-04-23 14:30:38 +08:00
|
|
|
for (int i = 0; i < num; ++i)
|
|
|
|
{
|
2013-09-12 14:26:39 +08:00
|
|
|
id = ids[i];
|
|
|
|
x = xs[i];
|
|
|
|
y = ys[i];
|
2012-04-23 14:30:38 +08:00
|
|
|
|
2013-09-12 14:26:39 +08:00
|
|
|
auto iter = g_touchIdReorderMap.find(id);
|
2013-12-18 17:47:20 +08:00
|
|
|
unusedIndex = 0;
|
2012-04-23 14:30:38 +08:00
|
|
|
|
|
|
|
// it is a new touch
|
2013-09-12 14:26:39 +08:00
|
|
|
if (iter == g_touchIdReorderMap.end())
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
unusedIndex = getUnUsedIndex();
|
2012-04-23 14:30:38 +08:00
|
|
|
|
2012-04-20 15:23:00 +08:00
|
|
|
// The touches is more than MAX_TOUCHES ?
|
2013-12-18 17:47:20 +08:00
|
|
|
if (unusedIndex == -1) {
|
|
|
|
CCLOG("The touches is more than MAX_TOUCHES, unusedIndex = %d", unusedIndex);
|
2012-08-20 10:38:21 +08:00
|
|
|
continue;
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
Touch* touch = g_touches[unusedIndex] = new Touch();
|
|
|
|
touch->setTouchInfo(unusedIndex, (x - _viewPortRect.origin.x) / _scaleX,
|
2013-06-15 14:03:30 +08:00
|
|
|
(y - _viewPortRect.origin.y) / _scaleY);
|
2012-08-01 18:44:10 +08:00
|
|
|
|
2013-09-12 14:26:39 +08:00
|
|
|
CCLOGINFO("x = %f y = %f", pTouch->getLocationInView().x, pTouch->getLocationInView().y);
|
2012-08-01 16:56:12 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
g_touchIdReorderMap.insert(std::make_pair(id, unusedIndex));
|
2013-09-12 20:43:09 +08:00
|
|
|
touchEvent._touches.push_back(touch);
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-12 20:43:09 +08:00
|
|
|
if (touchEvent._touches.size() == 0)
|
2012-04-20 15:23:00 +08:00
|
|
|
{
|
2013-09-12 14:26:39 +08:00
|
|
|
CCLOG("touchesBegan: size = 0");
|
2012-04-23 14:30:38 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2013-09-20 19:19:31 +08:00
|
|
|
touchEvent._eventCode = EventTouch::EventCode::BEGAN;
|
2013-10-26 15:04:01 +08:00
|
|
|
auto dispatcher = Director::getInstance()->getEventDispatcher();
|
|
|
|
dispatcher->dispatchEvent(&touchEvent);
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
void EGLViewProtocol::handleTouchesMove(int num, int ids[], float xs[], float ys[])
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2013-12-05 17:19:01 +08:00
|
|
|
int id = 0;
|
2013-09-12 14:26:39 +08:00
|
|
|
float x = 0.0f;
|
|
|
|
float y = 0.0f;
|
2013-09-20 19:19:31 +08:00
|
|
|
EventTouch touchEvent;
|
2013-09-12 14:26:39 +08:00
|
|
|
|
2012-04-23 14:30:38 +08:00
|
|
|
for (int i = 0; i < num; ++i)
|
|
|
|
{
|
2013-09-12 14:26:39 +08:00
|
|
|
id = ids[i];
|
|
|
|
x = xs[i];
|
|
|
|
y = ys[i];
|
2012-04-20 15:23:00 +08:00
|
|
|
|
2013-09-12 14:26:39 +08:00
|
|
|
auto iter = g_touchIdReorderMap.find(id);
|
|
|
|
if (iter == g_touchIdReorderMap.end())
|
2013-09-03 18:22:03 +08:00
|
|
|
{
|
2012-04-20 15:23:00 +08:00
|
|
|
CCLOG("if the index doesn't exist, it is an error");
|
2012-08-20 10:38:21 +08:00
|
|
|
continue;
|
2012-04-20 15:23:00 +08:00
|
|
|
}
|
|
|
|
|
2012-04-26 11:39:49 +08:00
|
|
|
CCLOGINFO("Moving touches with id: %d, x=%f, y=%f", id, x, y);
|
2013-09-12 14:26:39 +08:00
|
|
|
Touch* touch = g_touches[iter->second];
|
|
|
|
if (touch)
|
2012-04-20 15:23:00 +08:00
|
|
|
{
|
2013-09-12 14:26:39 +08:00
|
|
|
touch->setTouchInfo(iter->second, (x - _viewPortRect.origin.x) / _scaleX,
|
2013-06-15 14:03:30 +08:00
|
|
|
(y - _viewPortRect.origin.y) / _scaleY);
|
2012-08-01 16:56:12 +08:00
|
|
|
|
2013-09-12 20:43:09 +08:00
|
|
|
touchEvent._touches.push_back(touch);
|
2012-04-20 15:23:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// It is error, should return.
|
2013-12-06 16:32:06 +08:00
|
|
|
CCLOG("Moving touches with id: %d error", id);
|
2012-04-20 15:23:00 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
2013-09-12 20:43:09 +08:00
|
|
|
if (touchEvent._touches.size() == 0)
|
2012-04-20 15:23:00 +08:00
|
|
|
{
|
2013-09-12 14:26:39 +08:00
|
|
|
CCLOG("touchesMoved: size = 0");
|
2012-04-23 14:30:38 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2013-09-20 19:19:31 +08:00
|
|
|
touchEvent._eventCode = EventTouch::EventCode::MOVED;
|
2013-10-26 15:04:01 +08:00
|
|
|
auto dispatcher = Director::getInstance()->getEventDispatcher();
|
|
|
|
dispatcher->dispatchEvent(&touchEvent);
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
void EGLViewProtocol::handleTouchesOfEndOrCancel(EventTouch::EventCode eventCode, int num, int ids[], float xs[], float ys[])
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2013-12-05 17:19:01 +08:00
|
|
|
int id = 0;
|
2013-09-12 14:26:39 +08:00
|
|
|
float x = 0.0f;
|
|
|
|
float y = 0.0f;
|
2013-09-20 19:19:31 +08:00
|
|
|
EventTouch touchEvent;
|
2013-09-12 14:26:39 +08:00
|
|
|
|
2012-04-23 14:30:38 +08:00
|
|
|
for (int i = 0; i < num; ++i)
|
|
|
|
{
|
2013-09-12 14:26:39 +08:00
|
|
|
id = ids[i];
|
|
|
|
x = xs[i];
|
|
|
|
y = ys[i];
|
2012-04-23 14:30:38 +08:00
|
|
|
|
2013-09-12 14:26:39 +08:00
|
|
|
auto iter = g_touchIdReorderMap.find(id);
|
|
|
|
if (iter == g_touchIdReorderMap.end())
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
|
|
|
CCLOG("if the index doesn't exist, it is an error");
|
2012-08-20 10:38:21 +08:00
|
|
|
continue;
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
2013-09-12 14:26:39 +08:00
|
|
|
|
2012-04-20 15:23:00 +08:00
|
|
|
/* Add to the set to send to the director */
|
2013-09-12 14:26:39 +08:00
|
|
|
Touch* touch = g_touches[iter->second];
|
|
|
|
if (touch)
|
2012-04-20 15:23:00 +08:00
|
|
|
{
|
2012-04-26 11:39:49 +08:00
|
|
|
CCLOGINFO("Ending touches with id: %d, x=%f, y=%f", id, x, y);
|
2013-09-12 14:26:39 +08:00
|
|
|
touch->setTouchInfo(iter->second, (x - _viewPortRect.origin.x) / _scaleX,
|
2013-06-15 14:03:30 +08:00
|
|
|
(y - _viewPortRect.origin.y) / _scaleY);
|
2012-08-01 18:44:10 +08:00
|
|
|
|
2013-09-12 20:43:09 +08:00
|
|
|
touchEvent._touches.push_back(touch);
|
2013-09-12 14:26:39 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
g_touches[iter->second] = nullptr;
|
2013-09-03 18:22:03 +08:00
|
|
|
removeUsedIndexBit(iter->second);
|
2012-04-20 15:23:00 +08:00
|
|
|
|
2013-09-12 14:26:39 +08:00
|
|
|
g_touchIdReorderMap.erase(id);
|
2012-04-20 15:23:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-06 16:32:06 +08:00
|
|
|
CCLOG("Ending touches with id: %d error", id);
|
2012-04-20 15:23:00 +08:00
|
|
|
return;
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-09-12 20:43:09 +08:00
|
|
|
if (touchEvent._touches.size() == 0)
|
2012-04-20 15:23:00 +08:00
|
|
|
{
|
2013-09-12 14:26:39 +08:00
|
|
|
CCLOG("touchesEnded or touchesCancel: size = 0");
|
2012-04-23 14:30:38 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2013-09-12 20:43:09 +08:00
|
|
|
touchEvent._eventCode = eventCode;
|
2013-10-26 15:04:01 +08:00
|
|
|
auto dispatcher = Director::getInstance()->getEventDispatcher();
|
|
|
|
dispatcher->dispatchEvent(&touchEvent);
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2013-09-12 20:43:09 +08:00
|
|
|
for (auto& touch : touchEvent._touches)
|
2013-09-03 18:22:03 +08:00
|
|
|
{
|
2013-11-12 19:24:09 +08:00
|
|
|
// release the touch object.
|
|
|
|
touch->release();
|
2013-09-03 18:22:03 +08:00
|
|
|
}
|
2013-09-12 14:26:39 +08:00
|
|
|
}
|
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
void EGLViewProtocol::handleTouchesEnd(int num, int ids[], float xs[], float ys[])
|
2013-09-12 14:26:39 +08:00
|
|
|
{
|
2013-09-20 19:19:31 +08:00
|
|
|
handleTouchesOfEndOrCancel(EventTouch::EventCode::ENDED, num, ids, xs, ys);
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
void EGLViewProtocol::handleTouchesCancel(int num, int ids[], float xs[], float ys[])
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2013-09-20 19:19:31 +08:00
|
|
|
handleTouchesOfEndOrCancel(EventTouch::EventCode::CANCELLED, num, ids, xs, ys);
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
const Rect& EGLViewProtocol::getViewPortRect() const
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _viewPortRect;
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
float EGLViewProtocol::getScaleX() const
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _scaleX;
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
float EGLViewProtocol::getScaleY() const
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _scaleY;
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2012-04-23 14:30:38 +08:00
|
|
|
NS_CC_END
|