2012-04-23 14:30:38 +08:00
|
|
|
#include "CCEGLViewProtocol.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "touch_dispatcher/CCTouchDispatcher.h"
|
|
|
|
#include "touch_dispatcher/CCTouch.h"
|
2012-04-23 14:30:38 +08:00
|
|
|
#include "CCDirector.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "cocoa/CCSet.h"
|
|
|
|
#include "cocoa/CCDictionary.h"
|
|
|
|
#include "cocoa/CCInteger.h"
|
2012-04-23 14:30:38 +08:00
|
|
|
|
|
|
|
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()
|
2012-08-01 16:56:12 +08:00
|
|
|
: m_pDelegate(NULL)
|
2012-08-15 14:33:56 +08:00
|
|
|
, m_fScaleY(1.0f)
|
|
|
|
, m_fScaleX(1.0f)
|
2012-08-01 18:44:10 +08:00
|
|
|
, m_bIsRetinaEnabled(false)
|
2012-08-07 14:29:46 +08:00
|
|
|
, m_eResolutionPolicy(kResolutionUnKnown)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CCEGLViewProtocol::~CCEGLViewProtocol()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-08-01 16:56:12 +08:00
|
|
|
void CCEGLViewProtocol::setDesignResolutionSize(float width, float height, ResolutionPolicy resolutionPolicy)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2012-08-07 14:29:46 +08:00
|
|
|
CCAssert(m_bIsRetinaEnabled == false, "can not enable retina while set design resolution size!");
|
|
|
|
CCAssert(resolutionPolicy != kResolutionUnKnown, "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;
|
|
|
|
}
|
|
|
|
|
2012-08-01 16:56:12 +08:00
|
|
|
m_obDesignResolutionSize.setSize(width, height);
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
m_fScaleX = (float)m_obScreenSize.width / m_obDesignResolutionSize.width;
|
|
|
|
m_fScaleY = (float)m_obScreenSize.height / m_obDesignResolutionSize.height;
|
2012-08-01 16:56:12 +08:00
|
|
|
|
2012-08-20 16:32:22 +08:00
|
|
|
if (resolutionPolicy == kResolutionNoBorder)
|
2012-08-01 16:56:12 +08:00
|
|
|
{
|
2012-08-15 14:33:56 +08:00
|
|
|
m_fScaleX = m_fScaleY = MAX(m_fScaleX, m_fScaleY);
|
2012-08-01 16:56:12 +08:00
|
|
|
}
|
|
|
|
|
2012-08-20 16:32:22 +08:00
|
|
|
if (resolutionPolicy == kResolutionShowAll)
|
2012-08-01 16:56:12 +08:00
|
|
|
{
|
2012-08-15 14:33:56 +08:00
|
|
|
m_fScaleX = m_fScaleY = MIN(m_fScaleX, m_fScaleY);
|
2012-08-01 16:56:12 +08:00
|
|
|
}
|
2012-05-03 15:05:19 +08:00
|
|
|
|
2012-08-01 16:56:12 +08:00
|
|
|
// calculate the rect of viewport
|
2012-08-15 14:33:56 +08:00
|
|
|
float viewPortW = m_obDesignResolutionSize.width * m_fScaleX;
|
|
|
|
float viewPortH = m_obDesignResolutionSize.height * m_fScaleY;
|
2012-04-23 14:39:27 +08:00
|
|
|
|
2012-08-01 16:56:12 +08:00
|
|
|
m_obViewPortRect.setRect((m_obScreenSize.width - viewPortW) / 2, (m_obScreenSize.height - viewPortH) / 2, viewPortW, viewPortH);
|
2012-08-07 14:29:46 +08:00
|
|
|
|
|
|
|
m_eResolutionPolicy = resolutionPolicy;
|
|
|
|
|
2012-08-07 18:24:45 +08:00
|
|
|
//setViewPortInPoints(0, 0,m_obScreenSize.width, m_obScreenSize.height);
|
2012-08-07 14:29:46 +08:00
|
|
|
|
|
|
|
// reset director's member vaviables to fit visible rect
|
|
|
|
CCDirector::sharedDirector()->createStatsLabel();
|
|
|
|
CCDirector::sharedDirector()->m_obWinSizeInPoints = CCDirector::sharedDirector()->m_obWinSizeInPixels = getSize();
|
2012-08-07 18:24:45 +08:00
|
|
|
CCDirector::sharedDirector()->setGLDefaultValues();
|
2012-08-07 14:29:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCEGLViewProtocol::enableRetina()
|
|
|
|
{
|
|
|
|
return false;
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
const CCSize& CCEGLViewProtocol::getSize() const
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2012-08-01 16:56:12 +08:00
|
|
|
return m_obDesignResolutionSize;
|
|
|
|
}
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
const CCSize& CCEGLViewProtocol::getFrameSize() const
|
|
|
|
{
|
|
|
|
return m_obScreenSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEGLViewProtocol::setFrameSize(float width, float height)
|
2012-08-07 14:29:46 +08:00
|
|
|
{
|
|
|
|
m_obDesignResolutionSize = m_obScreenSize = CCSizeMake(width, height);
|
|
|
|
}
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
CCSize CCEGLViewProtocol::getVisibleSize() const
|
2012-08-01 16:56:12 +08:00
|
|
|
{
|
2012-08-20 16:32:22 +08:00
|
|
|
if (m_eResolutionPolicy == kResolutionNoBorder)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2012-08-15 14:33:56 +08:00
|
|
|
return CCSizeMake(m_obScreenSize.width/m_fScaleX, m_obScreenSize.height/m_fScaleY);
|
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
|
|
|
{
|
2012-08-07 14:29:46 +08:00
|
|
|
return m_obDesignResolutionSize;
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
CCPoint CCEGLViewProtocol::getVisibleOrigin() const
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2012-08-20 16:32:22 +08:00
|
|
|
if (m_eResolutionPolicy == kResolutionNoBorder)
|
2012-08-07 14:29:46 +08:00
|
|
|
{
|
2012-08-15 14:33:56 +08:00
|
|
|
return CCPointMake((m_obDesignResolutionSize.width - m_obScreenSize.width/m_fScaleX)/2,
|
|
|
|
(m_obDesignResolutionSize.height - m_obScreenSize.height/m_fScaleY)/2);
|
2012-08-07 14:29:46 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return CCPointZero;
|
|
|
|
}
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCEGLViewProtocol::setTouchDelegate(EGLTouchDelegate * pDelegate)
|
|
|
|
{
|
|
|
|
m_pDelegate = pDelegate;
|
|
|
|
}
|
|
|
|
|
2012-08-07 14:29:46 +08:00
|
|
|
bool CCEGLViewProtocol::setContentScaleFactor(float contentScaleFactor)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2012-08-24 18:01:16 +08:00
|
|
|
m_fScaleX = m_fScaleY = contentScaleFactor;
|
2012-04-23 14:30:38 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEGLViewProtocol::setViewPortInPoints(float x , float y , float w , float h)
|
|
|
|
{
|
2012-08-15 14:33:56 +08:00
|
|
|
glViewport((GLint)(x * m_fScaleX + m_obViewPortRect.origin.x),
|
|
|
|
(GLint)(y * m_fScaleY + m_obViewPortRect.origin.y),
|
|
|
|
(GLsizei)(w * m_fScaleX),
|
|
|
|
(GLsizei)(h * m_fScaleY));
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCEGLViewProtocol::setScissorInPoints(float x , float y , float w , float h)
|
|
|
|
{
|
2012-08-15 14:33:56 +08:00
|
|
|
glScissor((GLint)(x * m_fScaleX + m_obViewPortRect.origin.x),
|
|
|
|
(GLint)(y * m_fScaleY + m_obViewPortRect.origin.y),
|
|
|
|
(GLsizei)(w * m_fScaleX),
|
|
|
|
(GLsizei)(h * m_fScaleY));
|
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);
|
2012-08-20 10:38:21 +08:00
|
|
|
continue;
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCTouch* pTouch = s_pTouches[nUnusedIndex] = new CCTouch();
|
2012-08-01 18:44:10 +08:00
|
|
|
if (m_bIsRetinaEnabled)
|
|
|
|
{
|
|
|
|
// on iOS, though retina is enabled, the value got from os is also
|
|
|
|
// relative to its original size
|
|
|
|
pTouch->setTouchInfo(nUnusedIndex, (x - m_obViewPortRect.origin.x),
|
|
|
|
(y - m_obViewPortRect.origin.y));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-15 14:33:56 +08:00
|
|
|
pTouch->setTouchInfo(nUnusedIndex, (x - m_obViewPortRect.origin.x) / m_fScaleX,
|
|
|
|
(y - m_obViewPortRect.origin.y) / m_fScaleY);
|
2012-08-01 18:44:10 +08:00
|
|
|
}
|
|
|
|
|
2012-08-07 18:24:45 +08:00
|
|
|
CCLOG("x = %f y = %f", pTouch->getLocationInView().x, pTouch->getLocationInView().y);
|
2012-08-01 16:56:12 +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");
|
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);
|
2012-04-20 15:23:00 +08:00
|
|
|
CCTouch* pTouch = s_pTouches[pIndex->getValue()];
|
|
|
|
if (pTouch)
|
|
|
|
{
|
2012-08-01 18:44:10 +08:00
|
|
|
if (m_bIsRetinaEnabled)
|
|
|
|
{
|
|
|
|
pTouch->setTouchInfo(pIndex->getValue(), (x - m_obViewPortRect.origin.x),
|
|
|
|
(y - m_obViewPortRect.origin.y));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-15 14:33:56 +08:00
|
|
|
pTouch->setTouchInfo(pIndex->getValue(), (x - m_obViewPortRect.origin.x) / m_fScaleX,
|
|
|
|
(y - m_obViewPortRect.origin.y) / m_fScaleY);
|
2012-08-01 18:44:10 +08:00
|
|
|
}
|
2012-08-01 16:56:12 +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");
|
2012-08-20 10:38:21 +08:00
|
|
|
continue;
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
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-23 15:40:46 +08:00
|
|
|
|
2012-08-01 18:44:10 +08:00
|
|
|
if (m_bIsRetinaEnabled)
|
|
|
|
{
|
|
|
|
pTouch->setTouchInfo(pIndex->getValue(), (x - m_obViewPortRect.origin.x),
|
|
|
|
(y - m_obViewPortRect.origin.y));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-15 14:33:56 +08:00
|
|
|
pTouch->setTouchInfo(pIndex->getValue(), (x - m_obViewPortRect.origin.x) / m_fScaleX,
|
|
|
|
(y - m_obViewPortRect.origin.y) / m_fScaleY);
|
2012-08-01 18:44:10 +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);
|
|
|
|
}
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
const CCRect& CCEGLViewProtocol::getViewPortRect() const
|
|
|
|
{
|
|
|
|
return m_obViewPortRect;
|
|
|
|
}
|
|
|
|
|
|
|
|
float CCEGLViewProtocol::getScaleX() const
|
|
|
|
{
|
|
|
|
return m_fScaleX;
|
|
|
|
}
|
|
|
|
|
|
|
|
float CCEGLViewProtocol::getScaleY() const
|
|
|
|
{
|
|
|
|
return m_fScaleY;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCEGLViewProtocol::isRetinaEnabled() const
|
|
|
|
{
|
|
|
|
return m_bIsRetinaEnabled;
|
|
|
|
}
|
|
|
|
|
2012-04-23 14:30:38 +08:00
|
|
|
NS_CC_END
|