issue #1486: Fix bugs for CCEditBox iOS port.

This commit is contained in:
James Chen 2012-10-15 13:01:36 +08:00
parent 39a4af57cc
commit a65f13defe
3 changed files with 53 additions and 44 deletions

View File

@ -791,6 +791,32 @@ static EAGLView *view = 0;
break;
}
float scaleX = cocos2d::CCEGLView::sharedOpenGLView()->getScaleX();
float scaleY = cocos2d::CCEGLView::sharedOpenGLView()->getScaleY();
if (self.contentScaleFactor == 2.0f)
{
// Convert to pixel coordinate
begin = CGRectApplyAffineTransform(begin, CGAffineTransformScale(CGAffineTransformIdentity, 2.0f, 2.0f));
end = CGRectApplyAffineTransform(end, CGAffineTransformScale(CGAffineTransformIdentity, 2.0f, 2.0f));
}
float offestY = cocos2d::CCEGLView::sharedOpenGLView()->getViewPortRect().origin.y;
CCLOG("offestY = %f", offestY);
if (offestY < 0.0f)
{
begin.origin.y += offestY;
begin.size.height -= offestY;
end.size.height -= offestY;
}
// Convert to desigin coordinate
begin = CGRectApplyAffineTransform(begin, CGAffineTransformScale(CGAffineTransformIdentity, 1.0f/scaleX, 1.0f/scaleY));
end = CGRectApplyAffineTransform(end, CGAffineTransformScale(CGAffineTransformIdentity, 1.0f/scaleX, 1.0f/scaleY));
cocos2d::CCIMEKeyboardNotificationInfo notiInfo;
notiInfo.begin = cocos2d::CCRect(begin.origin.x,
begin.origin.y,
@ -802,28 +828,6 @@ static EAGLView *view = 0;
end.size.height);
notiInfo.duration = (float)aniDuration;
float offestY = cocos2d::CCEGLView::sharedOpenGLView()->getViewPortRect().origin.y;
if (offestY > 0.0f)
{
notiInfo.begin.origin.y += offestY;
notiInfo.begin.size.height -= offestY;
notiInfo.end.size.height -= offestY;
}
float scaleX = cocos2d::CCEGLView::sharedOpenGLView()->getScaleX();
float scaleY = cocos2d::CCEGLView::sharedOpenGLView()->getScaleY();
notiInfo.begin.origin.x /= scaleX;
notiInfo.begin.origin.y /= scaleY;
notiInfo.begin.size.width /= scaleX;
notiInfo.begin.size.height /= scaleY;
notiInfo.end.origin.x /= scaleX;
notiInfo.end.origin.y /= scaleY;
notiInfo.end.size.width /= scaleX;
notiInfo.end.size.height /= scaleY;
cocos2d::CCIMEDispatcher* dispatcher = cocos2d::CCIMEDispatcher::sharedDispatcher();
if (UIKeyboardWillShowNotification == type)
{
@ -858,12 +862,17 @@ static EAGLView *view = 0;
[UIView setAnimationDuration:duration];
[UIView setAnimationBeginsFromCurrentState:YES];
// NSLog(@"[animation] dis = %f\n", dis);
//NSLog(@"[animation] dis = %f, scale = %f \n", dis, cocos2d::CCEGLView::sharedOpenGLView()->getScaleY());
if (dis < 0.0f) dis = 0.0f;
dis *= cocos2d::CCEGLView::sharedOpenGLView()->getScaleY();
if (self.contentScaleFactor == 2.0f)
{
dis /= 2.0f;
}
switch ([[UIApplication sharedApplication] statusBarOrientation])
{
case UIInterfaceOrientationPortrait:

View File

@ -64,6 +64,7 @@ private:
CCSize m_tContentSize;
void* m_pSysEdit;
int m_nMaxTextLength;
bool m_bInRetinaMode;
};

View File

@ -42,7 +42,7 @@ CCEditBoxImplIOS::CCEditBoxImplIOS(CCEditBox* pEditText)
, m_pSysEdit(NULL)
, m_nMaxTextLength(-1)
{
m_bInRetinaMode = [[EAGLView sharedEGLView] contentScaleFactor] == 2.0f ? true : false;
}
CCEditBoxImplIOS::~CCEditBoxImplIOS()
@ -64,15 +64,14 @@ bool CCEditBoxImplIOS::initWithSize(const CCSize& size)
{
CCEGLViewProtocol* eglView = CCEGLView::sharedOpenGLView();
CGRect rect;
if (eglView->isRetinaEnabled())
CGRect rect = CGRectMake(0, 0, size.width * eglView->getScaleX(),size.height * eglView->getScaleY());
if (m_bInRetinaMode)
{
rect = CGRectMake(0, 0, size.width,size.height);
}
else
{
rect = CGRectMake(0, 0, size.width * eglView->getScaleX(),size.height * eglView->getScaleY());
rect.size.width /= 2.0f;
rect.size.height /= 2.0f;
}
m_pSysEdit = [[EditBoxImplIOS alloc] initWithFrame:rect editBox:this];
if (!m_pSysEdit) break;
@ -195,29 +194,29 @@ void CCEditBoxImplIOS::setPlaceHolder(const char* pText)
GET_IMPL.textField.placeholder = [NSString stringWithUTF8String:pText];
}
static CGPoint convertDesignCoordToScreenCoord(const CCPoint& designCoord)
static CGPoint convertDesignCoordToScreenCoord(const CCPoint& designCoord, bool bInRetinaMode)
{
float viewH = (float)[[EAGLView sharedEGLView] getHeight];
CCEGLViewProtocol* eglView = CCEGLView::sharedOpenGLView();
CCPoint visiblePos;
if (eglView->isRetinaEnabled())
{
visiblePos = ccp(designCoord.x, designCoord.y);
}
else
{
visiblePos = ccp(designCoord.x * eglView->getScaleX(), designCoord.y * eglView->getScaleY());
}
float viewH = (float)[[EAGLView sharedEGLView] getHeight];
CCPoint visiblePos = ccp(designCoord.x * eglView->getScaleX(), designCoord.y * eglView->getScaleY());
CCPoint screenGLPos = ccpAdd(visiblePos, eglView->getViewPortRect().origin);
CGPoint screenPos = CGPointMake(screenGLPos.x, viewH - screenGLPos.y);
if (bInRetinaMode)
{
screenPos.x = screenPos.x / 2.0f;
screenPos.y = screenPos.y / 2.0f;
}
CCLOG("[EditBox] pos x = %f, y = %f", screenGLPos.x, screenGLPos.y);
return screenPos;
}
void CCEditBoxImplIOS::setPosition(const CCPoint& pos)
{
//TODO should consider anchor point, the default value is (0.5, 0,5)
[GET_IMPL setPosition:convertDesignCoordToScreenCoord(ccp(pos.x-m_tContentSize.width/2, pos.y+m_tContentSize.height/2))];
[GET_IMPL setPosition:convertDesignCoordToScreenCoord(ccp(pos.x-m_tContentSize.width/2, pos.y+m_tContentSize.height/2), m_bInRetinaMode)];
}
void CCEditBoxImplIOS::setContentSize(const CCSize& size)