2014-01-07 11:25:07 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2016-08-05 09:42:15 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
#include "platform/CCGLView.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCTouch.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCEventDispatcher.h"
|
2015-06-01 14:26:08 +08:00
|
|
|
#include "2d/CCCamera.h"
|
2016-06-16 02:33:25 +08:00
|
|
|
#include "2d/CCScene.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "vr/CCVRProtocol.h"
|
|
|
|
#include "vr/CCVRGenericRenderer.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
|
2014-03-26 15:04:11 +08:00
|
|
|
static std::map<intptr_t, 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
|
|
|
|
2014-06-26 18:40:33 +08:00
|
|
|
static std::vector<Touch*> getAllTouchesVector()
|
|
|
|
{
|
|
|
|
std::vector<Touch*> ret;
|
|
|
|
int i;
|
|
|
|
int temp = g_indexBitsUsed;
|
|
|
|
|
|
|
|
for (i = 0; i < EventTouch::MAX_TOUCHES; i++) {
|
|
|
|
if ( temp & 0x00000001) {
|
|
|
|
ret.push_back(g_touches[i]);
|
|
|
|
}
|
|
|
|
temp >>= 1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-09-22 16:08:23 +08:00
|
|
|
//default context attributions are set as follows
|
2014-08-22 16:22:16 +08:00
|
|
|
GLContextAttrs GLView::_glContextAttrs = {5, 6, 5, 0, 16, 0};
|
2014-08-21 09:35:32 +08:00
|
|
|
|
2014-08-22 16:22:16 +08:00
|
|
|
void GLView::setGLContextAttrs(GLContextAttrs& glContextAttrs)
|
2014-08-21 09:35:32 +08:00
|
|
|
{
|
2014-08-22 16:22:16 +08:00
|
|
|
_glContextAttrs = glContextAttrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLContextAttrs GLView::getGLContextAttrs()
|
|
|
|
{
|
|
|
|
return _glContextAttrs;
|
2014-08-21 09:35:32 +08:00
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
GLView::GLView()
|
2014-02-02 20:44:57 +08:00
|
|
|
: _scaleX(1.0f)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _scaleY(1.0f)
|
2013-07-26 18:18:58 +08:00
|
|
|
, _resolutionPolicy(ResolutionPolicy::UNKNOWN)
|
2016-06-16 02:33:25 +08:00
|
|
|
, _vrImpl(nullptr)
|
2016-09-07 11:11:52 +08:00
|
|
|
, _designResolutionSize(0,0)
|
|
|
|
, _screenSize(0,0)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
GLView::~GLView()
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
void GLView::pollInputEvents()
|
2013-07-08 10:26:53 +08:00
|
|
|
{
|
2014-07-31 00:53:04 +08:00
|
|
|
pollEvents();
|
2013-07-08 10:26:53 +08:00
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
void GLView::pollEvents()
|
|
|
|
{
|
|
|
|
}
|
2014-03-07 13:50:50 +08:00
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
void GLView::updateDesignResolutionSize()
|
2014-03-07 13:50:50 +08:00
|
|
|
{
|
|
|
|
if (_screenSize.width > 0 && _screenSize.height > 0
|
|
|
|
&& _designResolutionSize.width > 0 && _designResolutionSize.height > 0)
|
|
|
|
{
|
|
|
|
_scaleX = (float)_screenSize.width / _designResolutionSize.width;
|
|
|
|
_scaleY = (float)_screenSize.height / _designResolutionSize.height;
|
|
|
|
|
|
|
|
if (_resolutionPolicy == ResolutionPolicy::NO_BORDER)
|
|
|
|
{
|
|
|
|
_scaleX = _scaleY = MAX(_scaleX, _scaleY);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (_resolutionPolicy == ResolutionPolicy::SHOW_ALL)
|
|
|
|
{
|
|
|
|
_scaleX = _scaleY = MIN(_scaleX, _scaleY);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ( _resolutionPolicy == ResolutionPolicy::FIXED_HEIGHT) {
|
|
|
|
_scaleX = _scaleY;
|
|
|
|
_designResolutionSize.width = ceilf(_screenSize.width/_scaleX);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ( _resolutionPolicy == ResolutionPolicy::FIXED_WIDTH) {
|
|
|
|
_scaleY = _scaleX;
|
|
|
|
_designResolutionSize.height = ceilf(_screenSize.height/_scaleY);
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate the rect of viewport
|
|
|
|
float viewPortW = _designResolutionSize.width * _scaleX;
|
|
|
|
float viewPortH = _designResolutionSize.height * _scaleY;
|
|
|
|
|
|
|
|
_viewPortRect.setRect((_screenSize.width - viewPortW) / 2, (_screenSize.height - viewPortH) / 2, viewPortW, viewPortH);
|
2016-07-22 06:12:57 +08:00
|
|
|
|
|
|
|
|
2014-03-07 13:50:50 +08:00
|
|
|
// reset director's member variables to fit visible rect
|
|
|
|
auto director = Director::getInstance();
|
|
|
|
director->_winSizeInPoints = getDesignResolutionSize();
|
2015-04-02 23:35:56 +08:00
|
|
|
director->_isStatusLabelUpdated = true;
|
2016-06-02 16:18:31 +08:00
|
|
|
director->setProjection(director->getProjection());
|
2016-07-22 06:12:57 +08:00
|
|
|
|
|
|
|
// Github issue #16139
|
|
|
|
// A default viewport is needed in order to display the FPS,
|
|
|
|
// since the FPS are rendered in the Director, and there is no viewport there.
|
|
|
|
// Everything, including the FPS should renderer in the Scene.
|
|
|
|
glViewport(0, 0, _screenSize.width, _screenSize.height);
|
2014-03-07 13:50:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
void GLView::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);
|
|
|
|
_resolutionPolicy = resolutionPolicy;
|
2012-08-07 14:29:46 +08:00
|
|
|
|
2014-03-07 13:50:50 +08:00
|
|
|
updateDesignResolutionSize();
|
|
|
|
}
|
2012-08-07 14:29:46 +08:00
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
const Size& GLView::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
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
const Size& GLView::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
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
void GLView::setFrameSize(float width, float height)
|
2012-08-07 14:29:46 +08:00
|
|
|
{
|
2016-07-04 20:48:24 +08:00
|
|
|
_screenSize = Size(width, height);
|
2016-09-07 11:11:52 +08:00
|
|
|
|
|
|
|
// Github issue #16003 and #16485
|
|
|
|
// only update the designResolution if it wasn't previously set
|
|
|
|
if (_designResolutionSize.equals(Size::ZERO))
|
|
|
|
_designResolutionSize = _screenSize;
|
2012-08-07 14:29:46 +08:00
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
Rect GLView::getVisibleRect() const
|
2014-02-09 04:46:44 +08:00
|
|
|
{
|
|
|
|
Rect ret;
|
|
|
|
ret.size = getVisibleSize();
|
|
|
|
ret.origin = getVisibleOrigin();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
Size GLView::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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
Vec2 GLView::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
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
return Vec2((_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
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
return Vec2::ZERO;
|
2012-08-07 14:29:46 +08:00
|
|
|
}
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
void GLView::setViewPortInPoints(float x , float y , float w , float h)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2015-08-05 10:57:27 +08:00
|
|
|
experimental::Viewport vp((float)(x * _scaleX + _viewPortRect.origin.x),
|
2015-06-01 14:26:08 +08:00
|
|
|
(float)(y * _scaleY + _viewPortRect.origin.y),
|
|
|
|
(float)(w * _scaleX),
|
2015-08-05 10:57:27 +08:00
|
|
|
(float)(h * _scaleY));
|
2015-06-01 14:26:08 +08:00
|
|
|
Camera::setDefaultViewport(vp);
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
void GLView::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
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
bool GLView::isScissorEnabled()
|
2013-03-29 01:28:26 +08:00
|
|
|
{
|
2015-08-14 21:17:51 +08:00
|
|
|
return (GL_FALSE == glIsEnabled(GL_SCISSOR_TEST)) ? false : true;
|
2013-03-29 01:28:26 +08:00
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
Rect GLView::getScissorRect() const
|
2013-03-29 01:28:26 +08:00
|
|
|
{
|
2015-08-14 21:17:51 +08:00
|
|
|
GLfloat params[4];
|
|
|
|
glGetFloatv(GL_SCISSOR_BOX, params);
|
|
|
|
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;
|
|
|
|
return Rect(x, y, w, h);
|
2013-03-29 01:28:26 +08:00
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
void GLView::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
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
const std::string& GLView::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
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
void GLView::handleTouchesBegin(int num, intptr_t ids[], float xs[], float ys[])
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2014-03-26 15:04:11 +08:00
|
|
|
intptr_t 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);
|
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
|
|
|
}
|
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
Touch* touch = g_touches[unusedIndex] = new (std::nothrow) Touch();
|
2015-08-14 21:17:51 +08:00
|
|
|
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
|
|
|
|
2014-06-20 00:59:08 +08:00
|
|
|
CCLOGINFO("x = %f y = %f", touch->getLocationInView().x, touch->getLocationInView().y);
|
2012-08-01 16:56:12 +08:00
|
|
|
|
2016-11-08 11:50:00 +08:00
|
|
|
g_touchIdReorderMap.emplace(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
|
|
|
}
|
|
|
|
|
2015-12-02 11:02:27 +08:00
|
|
|
void GLView::handleTouchesMove(int num, intptr_t ids[], float xs[], float ys[])
|
|
|
|
{
|
|
|
|
handleTouchesMove(num, ids, xs, ys, nullptr, nullptr);
|
|
|
|
}
|
|
|
|
|
2015-12-01 13:02:53 +08:00
|
|
|
void GLView::handleTouchesMove(int num, intptr_t ids[], float xs[], float ys[], float fs[], float ms[])
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2014-03-26 15:04:11 +08:00
|
|
|
intptr_t id = 0;
|
2013-09-12 14:26:39 +08:00
|
|
|
float x = 0.0f;
|
|
|
|
float y = 0.0f;
|
2015-12-01 13:02:53 +08:00
|
|
|
float force = 0.0f;
|
|
|
|
float maxForce = 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];
|
2015-12-01 13:02:53 +08:00
|
|
|
force = fs ? fs[i] : 0.0f;
|
|
|
|
maxForce = ms ? ms[i] : 0.0f;
|
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
|
|
|
}
|
|
|
|
|
2015-12-01 13:02:53 +08:00
|
|
|
CCLOGINFO("Moving touches with id: %d, x=%f, y=%f, force=%f, maxFource=%f", id, x, y, force, maxForce);
|
2013-09-12 14:26:39 +08:00
|
|
|
Touch* touch = g_touches[iter->second];
|
|
|
|
if (touch)
|
2012-04-20 15:23:00 +08:00
|
|
|
{
|
2015-08-14 21:17:51 +08:00
|
|
|
touch->setTouchInfo(iter->second, (x - _viewPortRect.origin.x) / _scaleX,
|
2015-12-01 13:02:53 +08:00
|
|
|
(y - _viewPortRect.origin.y) / _scaleY, force, maxForce);
|
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.
|
2014-07-11 17:39:56 +08:00
|
|
|
CCLOG("Moving touches with id: %ld error", (long int)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
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
void GLView::handleTouchesOfEndOrCancel(EventTouch::EventCode eventCode, int num, intptr_t ids[], float xs[], float ys[])
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2014-03-26 15:04:11 +08:00
|
|
|
intptr_t 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);
|
2015-08-14 21:17:51 +08:00
|
|
|
touch->setTouchInfo(iter->second, (x - _viewPortRect.origin.x) / _scaleX,
|
|
|
|
(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
|
|
|
|
{
|
2014-04-10 10:51:05 +08:00
|
|
|
CCLOG("Ending touches with id: %ld error", static_cast<long>(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
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
void GLView::handleTouchesEnd(int num, intptr_t 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
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
void GLView::handleTouchesCancel(int num, intptr_t 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
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
const Rect& GLView::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
|
|
|
}
|
|
|
|
|
2014-06-26 18:40:33 +08:00
|
|
|
std::vector<Touch*> GLView::getAllTouches() const
|
|
|
|
{
|
|
|
|
return getAllTouchesVector();
|
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
float GLView::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
|
|
|
}
|
|
|
|
|
2014-07-31 00:53:04 +08:00
|
|
|
float GLView::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
|
|
|
}
|
|
|
|
|
2016-06-16 02:33:25 +08:00
|
|
|
void GLView::renderScene(Scene* scene, Renderer* renderer)
|
|
|
|
{
|
|
|
|
CCASSERT(scene, "Invalid Scene");
|
|
|
|
CCASSERT(renderer, "Invalid Renderer");
|
|
|
|
|
|
|
|
if (_vrImpl)
|
|
|
|
{
|
|
|
|
_vrImpl->render(scene, renderer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
scene->render(renderer, Mat4::IDENTITY, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VRIRenderer* GLView::getVR() const
|
|
|
|
{
|
|
|
|
return _vrImpl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLView::setVR(VRIRenderer* vrRenderer)
|
|
|
|
{
|
|
|
|
if (_vrImpl != vrRenderer)
|
|
|
|
{
|
2016-06-24 10:03:38 +08:00
|
|
|
if (_vrImpl) {
|
|
|
|
_vrImpl->cleanup();
|
2016-06-16 02:33:25 +08:00
|
|
|
delete _vrImpl;
|
2016-06-24 10:03:38 +08:00
|
|
|
}
|
2016-06-16 02:33:25 +08:00
|
|
|
|
|
|
|
if (vrRenderer)
|
|
|
|
vrRenderer->setup(this);
|
|
|
|
|
|
|
|
_vrImpl = vrRenderer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-23 14:30:38 +08:00
|
|
|
NS_CC_END
|