2012-04-23 14:30:38 +08:00
|
|
|
#include "CCEGLViewProtocol.h"
|
|
|
|
#include "CCTouchDispatcher.h"
|
|
|
|
#include "CCDirector.h"
|
|
|
|
#include "CCSet.h"
|
|
|
|
#include "CCTouch.h"
|
|
|
|
#include "CCDictionary.h"
|
|
|
|
#include "CCInteger.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
static CCTouch* s_pTouches[CC_MAX_TOUCHES] = { NULL };
|
|
|
|
static unsigned int s_indexBitsUsed = 0;
|
|
|
|
static CCDictionary s_TouchesIntergerDict;
|
2012-04-20 15:23:00 +08:00
|
|
|
|
|
|
|
static int getUnUsedIndex()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int temp = s_indexBitsUsed;
|
|
|
|
|
2012-04-23 14:30:38 +08:00
|
|
|
for (i = 0; i < CC_MAX_TOUCHES; i++) {
|
2012-04-20 15:23:00 +08:00
|
|
|
if (! (temp & 0x00000001)) {
|
|
|
|
s_indexBitsUsed |= (1 << i);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
temp >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// all bits are used
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void removeUsedIndexBit(int index)
|
|
|
|
{
|
2012-04-23 14:30:38 +08:00
|
|
|
if (index < 0 || index >= CC_MAX_TOUCHES)
|
2012-04-20 15:23:00 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int temp = 1 << index;
|
|
|
|
temp = ~temp;
|
|
|
|
s_indexBitsUsed &= temp;
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCEGLViewProtocol::CCEGLViewProtocol()
|
|
|
|
: m_bNeedScale(false)
|
|
|
|
, m_pDelegate(NULL)
|
|
|
|
, m_fScreenScaleFactor(1.0f)
|
|
|
|
{
|
2012-05-02 17:50:26 +08:00
|
|
|
strncpy(m_szViewName, "Cocos2d-x Game", sizeof(m_szViewName));
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCEGLViewProtocol::~CCEGLViewProtocol()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCEGLViewProtocol::isIpad()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEGLViewProtocol::setFrameSize(float width, float height)
|
|
|
|
{
|
|
|
|
m_sSizeInPixel.setSize(width, height);
|
|
|
|
m_rcViewPort.size.setSize(width, height);
|
|
|
|
}
|
|
|
|
|
2012-05-23 11:57:43 +08:00
|
|
|
CCSize CCEGLViewProtocol::getFrameSize()
|
|
|
|
{
|
|
|
|
return m_sSizeInPixel;
|
|
|
|
}
|
|
|
|
|
2012-04-23 14:30:38 +08:00
|
|
|
void CCEGLViewProtocol::setDesignResolutionSize(float width, float height)
|
|
|
|
{
|
2012-04-23 14:39:27 +08:00
|
|
|
if (width == 0.0f || height == 0.0f)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_sSizeInPoint.setSize(width, height);
|
|
|
|
|
|
|
|
// calculate the factor and the rect of viewport
|
|
|
|
m_fScreenScaleFactor = MIN((float)m_sSizeInPixel.width / m_sSizeInPoint.width,
|
|
|
|
(float)m_sSizeInPixel.height / m_sSizeInPoint.height);
|
|
|
|
int viewPortW = (int)(m_sSizeInPoint.width * m_fScreenScaleFactor);
|
|
|
|
int viewPortH = (int)(m_sSizeInPoint.height * m_fScreenScaleFactor);
|
2012-05-03 15:05:19 +08:00
|
|
|
|
|
|
|
m_rcViewPort.setRect((m_sSizeInPixel.width - viewPortW) / 2, (m_sSizeInPixel.height - viewPortH) / 2, viewPortW, viewPortH);
|
2012-04-23 14:39:27 +08:00
|
|
|
|
|
|
|
CCLOG("m_fScreenScaleFactor = %f", m_fScreenScaleFactor);
|
2012-04-23 14:30:38 +08:00
|
|
|
m_bNeedScale = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCSize CCEGLViewProtocol::getSize()
|
|
|
|
{
|
|
|
|
CCSize size;
|
|
|
|
if (m_bNeedScale)
|
|
|
|
{
|
|
|
|
size.setSize(m_sSizeInPoint.width, m_sSizeInPoint.height);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size.setSize(m_sSizeInPixel.width, m_sSizeInPixel.height);
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCRect CCEGLViewProtocol::getViewPort()
|
|
|
|
{
|
|
|
|
return m_rcViewPort;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEGLViewProtocol::setTouchDelegate(EGLTouchDelegate * pDelegate)
|
|
|
|
{
|
|
|
|
m_pDelegate = pDelegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
float CCEGLViewProtocol::getScreenScaleFactor()
|
|
|
|
{
|
|
|
|
return m_fScreenScaleFactor;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCEGLViewProtocol::canSetContentScaleFactor()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEGLViewProtocol::setContentScaleFactor(float contentScaleFactor)
|
|
|
|
{
|
|
|
|
m_fScreenScaleFactor = contentScaleFactor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEGLViewProtocol::setViewPortInPoints(float x , float y , float w , float h)
|
|
|
|
{
|
|
|
|
if (m_bNeedScale)
|
|
|
|
{
|
|
|
|
float factor = m_fScreenScaleFactor / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
glViewport((GLint)(x * factor) + m_rcViewPort.origin.x,
|
|
|
|
(GLint)(y * factor) + m_rcViewPort.origin.y,
|
|
|
|
(GLsizei)(w * factor),
|
|
|
|
(GLsizei)(h * factor));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-23 14:39:27 +08:00
|
|
|
glViewport((GLint)x,
|
|
|
|
(GLint)y,
|
|
|
|
(GLsizei)w,
|
2012-04-23 14:30:38 +08:00
|
|
|
(GLsizei)h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEGLViewProtocol::setScissorInPoints(float x , float y , float w , float h)
|
|
|
|
{
|
|
|
|
if (m_bNeedScale)
|
|
|
|
{
|
|
|
|
float factor = m_fScreenScaleFactor / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
glScissor((GLint)(x * factor) + m_rcViewPort.origin.x,
|
|
|
|
(GLint)(y * factor) + m_rcViewPort.origin.y,
|
|
|
|
(GLsizei)(w * factor),
|
|
|
|
(GLsizei)(h * factor));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-23 14:39:27 +08:00
|
|
|
glScissor((GLint)x,
|
|
|
|
(GLint)y,
|
|
|
|
(GLsizei)w,
|
2012-04-23 14:30:38 +08:00
|
|
|
(GLsizei)h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float CCEGLViewProtocol::getMainScreenScale()
|
|
|
|
{
|
|
|
|
return -1.0f;
|
|
|
|
}
|
|
|
|
|
2012-05-02 17:50:26 +08:00
|
|
|
void CCEGLViewProtocol::setViewName(const char* pszViewName)
|
|
|
|
{
|
|
|
|
if (pszViewName != NULL && strlen(pszViewName) > 0)
|
|
|
|
{
|
|
|
|
strncpy(m_szViewName, pszViewName, sizeof(m_szViewName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* CCEGLViewProtocol::getViewName()
|
|
|
|
{
|
|
|
|
return m_szViewName;
|
|
|
|
}
|
|
|
|
|
2012-04-23 14:30:38 +08:00
|
|
|
void CCEGLViewProtocol::handleTouchesBegin(int num, int ids[], float xs[], float ys[])
|
|
|
|
{
|
|
|
|
CCSet set;
|
|
|
|
for (int i = 0; i < num; ++i)
|
|
|
|
{
|
|
|
|
int id = ids[i];
|
|
|
|
float x = xs[i];
|
|
|
|
float y = ys[i];
|
|
|
|
|
|
|
|
CCInteger* pIndex = (CCInteger*)s_TouchesIntergerDict.objectForKey(id);
|
|
|
|
int nUnusedIndex = 0;
|
|
|
|
|
|
|
|
// it is a new touch
|
|
|
|
if (pIndex == NULL)
|
|
|
|
{
|
|
|
|
nUnusedIndex = getUnUsedIndex();
|
|
|
|
|
2012-04-20 15:23:00 +08:00
|
|
|
// The touches is more than MAX_TOUCHES ?
|
|
|
|
if (nUnusedIndex == -1) {
|
|
|
|
CCLOG("The touches is more than MAX_TOUCHES, nUnusedIndex = %d", nUnusedIndex);
|
|
|
|
return;
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCTouch* pTouch = s_pTouches[nUnusedIndex] = new CCTouch();
|
2012-04-23 15:40:46 +08:00
|
|
|
if (m_bNeedScale)
|
|
|
|
{
|
2012-05-24 17:47:46 +08:00
|
|
|
pTouch->setTouchInfo(nUnusedIndex, (x - m_rcViewPort.origin.x) / m_fScreenScaleFactor,
|
2012-04-23 15:40:46 +08:00
|
|
|
(y - m_rcViewPort.origin.y) / m_fScreenScaleFactor);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-05-24 17:47:46 +08:00
|
|
|
pTouch->setTouchInfo(nUnusedIndex, x, y);
|
2012-04-23 15:40:46 +08:00
|
|
|
}
|
2012-04-20 17:33:28 +08:00
|
|
|
CCInteger* pInterObj = new CCInteger(nUnusedIndex);
|
|
|
|
s_TouchesIntergerDict.setObject(pInterObj, id);
|
2012-04-23 14:30:38 +08:00
|
|
|
set.addObject(pTouch);
|
|
|
|
pInterObj->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-20 15:23:00 +08:00
|
|
|
if (set.count() == 0)
|
|
|
|
{
|
|
|
|
CCLOG("touchesBegan: count = 0");
|
2012-04-23 14:30:38 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pDelegate->touchesBegan(&set, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEGLViewProtocol::handleTouchesMove(int num, int ids[], float xs[], float ys[])
|
|
|
|
{
|
|
|
|
CCSet set;
|
|
|
|
for (int i = 0; i < num; ++i)
|
|
|
|
{
|
|
|
|
int id = ids[i];
|
|
|
|
float x = xs[i];
|
2012-04-20 15:23:00 +08:00
|
|
|
float y = ys[i];
|
|
|
|
|
|
|
|
CCInteger* pIndex = (CCInteger*)s_TouchesIntergerDict.objectForKey(id);
|
|
|
|
if (pIndex == NULL) {
|
|
|
|
CCLOG("if the index doesn't exist, it is an error");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-26 11:39:49 +08:00
|
|
|
CCLOGINFO("Moving touches with id: %d, x=%f, y=%f", id, x, y);
|
2012-04-20 15:23:00 +08:00
|
|
|
CCTouch* pTouch = s_pTouches[pIndex->getValue()];
|
|
|
|
if (pTouch)
|
|
|
|
{
|
2012-04-23 15:40:46 +08:00
|
|
|
if (m_bNeedScale)
|
|
|
|
{
|
2012-05-24 17:47:46 +08:00
|
|
|
pTouch->setTouchInfo(pIndex->getValue(), (x - m_rcViewPort.origin.x) / m_fScreenScaleFactor,
|
2012-04-23 15:40:46 +08:00
|
|
|
(y - m_rcViewPort.origin.y) / m_fScreenScaleFactor);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-05-24 17:47:46 +08:00
|
|
|
pTouch->setTouchInfo(pIndex->getValue(), x, y);
|
2012-04-23 15:40:46 +08:00
|
|
|
}
|
2012-04-20 15:23:00 +08:00
|
|
|
set.addObject(pTouch);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// It is error, should return.
|
|
|
|
CCLOG("Moving touches with id: %d error", id);
|
|
|
|
return;
|
|
|
|
}
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
2012-04-20 15:23:00 +08:00
|
|
|
if (set.count() == 0)
|
|
|
|
{
|
|
|
|
CCLOG("touchesMoved: count = 0");
|
2012-04-23 14:30:38 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pDelegate->touchesMoved(&set, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEGLViewProtocol::getSetOfTouchesEndOrCancel(CCSet& set, int num, int ids[], float xs[], float ys[])
|
|
|
|
{
|
|
|
|
for (int i = 0; i < num; ++i)
|
|
|
|
{
|
|
|
|
int id = ids[i];
|
|
|
|
float x = xs[i];
|
|
|
|
float y = ys[i];
|
|
|
|
|
|
|
|
CCInteger* pIndex = (CCInteger*)s_TouchesIntergerDict.objectForKey(id);
|
|
|
|
if (pIndex == NULL)
|
|
|
|
{
|
|
|
|
CCLOG("if the index doesn't exist, it is an error");
|
|
|
|
return;
|
|
|
|
}
|
2012-04-20 15:23:00 +08:00
|
|
|
/* Add to the set to send to the director */
|
|
|
|
CCTouch* pTouch = s_pTouches[pIndex->getValue()];
|
|
|
|
if (pTouch)
|
|
|
|
{
|
2012-04-26 11:39:49 +08:00
|
|
|
CCLOGINFO("Ending touches with id: %d, x=%f, y=%f", id, x, y);
|
2012-04-20 15:23:00 +08:00
|
|
|
|
2012-04-23 15:40:46 +08:00
|
|
|
if (m_bNeedScale)
|
|
|
|
{
|
2012-05-24 17:47:46 +08:00
|
|
|
pTouch->setTouchInfo(pIndex->getValue(), (x - m_rcViewPort.origin.x) / m_fScreenScaleFactor,
|
2012-04-23 15:40:46 +08:00
|
|
|
(y - m_rcViewPort.origin.y) / m_fScreenScaleFactor);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-05-24 17:47:46 +08:00
|
|
|
pTouch->setTouchInfo(pIndex->getValue(), x, y);
|
2012-04-23 15:40:46 +08:00
|
|
|
}
|
|
|
|
|
2012-04-20 15:23:00 +08:00
|
|
|
set.addObject(pTouch);
|
|
|
|
|
|
|
|
// release the object
|
|
|
|
pTouch->release();
|
|
|
|
s_pTouches[pIndex->getValue()] = NULL;
|
|
|
|
removeUsedIndexBit(pIndex->getValue());
|
|
|
|
|
|
|
|
s_TouchesIntergerDict.removeObjectForKey(id);
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("Ending touches with id: %d error", id);
|
|
|
|
return;
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-04-20 15:23:00 +08:00
|
|
|
if (set.count() == 0)
|
|
|
|
{
|
|
|
|
CCLOG("touchesEnded or touchesCancel: count = 0");
|
2012-04-23 14:30:38 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEGLViewProtocol::handleTouchesEnd(int num, int ids[], float xs[], float ys[])
|
|
|
|
{
|
|
|
|
CCSet set;
|
|
|
|
getSetOfTouchesEndOrCancel(set, num, ids, xs, ys);
|
|
|
|
m_pDelegate->touchesEnded(&set, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEGLViewProtocol::handleTouchesCancel(int num, int ids[], float xs[], float ys[])
|
|
|
|
{
|
|
|
|
CCSet set;
|
|
|
|
getSetOfTouchesEndOrCancel(set, num, ids, xs, ys);
|
|
|
|
m_pDelegate->touchesCancelled(&set, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|