2014-08-19 10:28:24 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2012 Jozef Pridavok
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-03-01 05:53:00 +08:00
|
|
|
#include "platform/CCPlatformConfig.h"
|
2014-08-19 10:28:24 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
|
|
|
|
2016-03-21 11:02:00 +08:00
|
|
|
#include "ui/UIEditBox/UIEditBoxImpl-mac.h"
|
2014-09-10 07:50:02 +08:00
|
|
|
#include "base/CCDirector.h"
|
2016-03-21 11:02:00 +08:00
|
|
|
#include "ui/UIEditBox/UIEditBox.h"
|
2014-09-24 03:18:24 +08:00
|
|
|
#include "deprecated/CCString.h"
|
2016-03-21 11:02:00 +08:00
|
|
|
#include "ui/UIEditBox/Mac/CCUIEditBoxMac.h"
|
2015-07-17 22:12:09 +08:00
|
|
|
|
2014-08-19 10:28:24 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
|
|
|
EditBoxImpl* __createSystemEditBox(EditBox* pEditBox)
|
|
|
|
{
|
|
|
|
return new EditBoxImplMac(pEditBox);
|
|
|
|
}
|
|
|
|
|
|
|
|
EditBoxImplMac::EditBoxImplMac(EditBox* pEditText)
|
2016-02-04 14:21:07 +08:00
|
|
|
: EditBoxImplCommon(pEditText)
|
2014-08-19 10:28:24 +08:00
|
|
|
, _sysEdit(nullptr)
|
|
|
|
{
|
|
|
|
//! TODO: Retina on Mac
|
|
|
|
//! _inRetinaMode = [[CCEAGLView sharedEGLView] contentScaleFactor] == 2.0f ? true : false;
|
|
|
|
_inRetinaMode = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
EditBoxImplMac::~EditBoxImplMac()
|
|
|
|
{
|
|
|
|
[_sysEdit release];
|
|
|
|
}
|
|
|
|
|
2016-02-04 14:21:07 +08:00
|
|
|
void EditBoxImplMac::createNativeControl(const cocos2d::Rect &frame)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2016-02-04 14:21:07 +08:00
|
|
|
auto glview = cocos2d::Director::getInstance()->getOpenGLView();
|
|
|
|
Size size = frame.size;
|
|
|
|
NSRect rect = NSMakeRect(0, 0,
|
|
|
|
size.width * glview->getScaleX(),
|
|
|
|
size.height * glview->getScaleY());
|
2014-08-19 10:28:24 +08:00
|
|
|
|
2016-02-04 14:21:07 +08:00
|
|
|
float factor = cocos2d::Director::getInstance()->getContentScaleFactor();
|
2014-08-19 10:28:24 +08:00
|
|
|
|
2016-02-04 14:21:07 +08:00
|
|
|
rect.size.width /= factor;
|
|
|
|
rect.size.height /= factor;
|
2014-08-19 10:28:24 +08:00
|
|
|
|
2016-02-04 14:21:07 +08:00
|
|
|
_sysEdit = [[UIEditBoxImplMac alloc] initWithFrame:rect editBox:this];
|
|
|
|
this->setNativeVisible(false);
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
2015-08-07 17:27:15 +08:00
|
|
|
|
|
|
|
NSFont* EditBoxImplMac::constructFont(const char *fontName, int fontSize)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2015-08-07 17:27:15 +08:00
|
|
|
NSString * fntName = [NSString stringWithUTF8String:fontName];
|
2015-12-14 23:55:21 +08:00
|
|
|
fntName = [[fntName lastPathComponent] stringByDeletingPathExtension];
|
2014-08-19 10:28:24 +08:00
|
|
|
float retinaFactor = _inRetinaMode ? 2.0f : 1.0f;
|
|
|
|
auto glview = cocos2d::Director::getInstance()->getOpenGLView();
|
|
|
|
float scaleFactor = glview->getScaleX();
|
2015-08-07 17:27:15 +08:00
|
|
|
|
|
|
|
if (fontSize == -1)
|
|
|
|
{
|
2016-02-04 14:21:07 +08:00
|
|
|
NSRect frameRect = [_sysEdit.textInput frame];
|
2015-08-07 17:27:15 +08:00
|
|
|
fontSize = frameRect.size.height*2/3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fontSize = fontSize * scaleFactor / retinaFactor;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSFont *textFont = nil;
|
|
|
|
if (strlen(fontName) == 0)
|
|
|
|
{
|
|
|
|
textFont = [NSFont systemFontOfSize:fontSize];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
textFont = [NSFont fontWithName:fntName size:fontSize];
|
|
|
|
}
|
|
|
|
|
|
|
|
return textFont;
|
|
|
|
}
|
|
|
|
|
2016-02-04 14:21:07 +08:00
|
|
|
void EditBoxImplMac::setNativeFont(const char *pFontName, int fontSize)
|
2015-08-07 17:27:15 +08:00
|
|
|
{
|
|
|
|
NSFont* textFont = constructFont(pFontName, fontSize);
|
2016-02-04 14:21:07 +08:00
|
|
|
[_sysEdit setFont:textFont];
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2016-02-04 14:21:07 +08:00
|
|
|
void EditBoxImplMac::setNativePlaceholderFont(const char *pFontName, int fontSize)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2015-08-07 17:27:15 +08:00
|
|
|
NSFont *textFont = constructFont(pFontName, fontSize);
|
2014-08-19 10:28:24 +08:00
|
|
|
|
2015-08-07 17:27:15 +08:00
|
|
|
if (!textFont) {
|
2014-08-19 10:28:24 +08:00
|
|
|
CCLOGWARN("Font not found: %s", pFontName);
|
|
|
|
return;
|
|
|
|
}
|
2016-02-04 14:21:07 +08:00
|
|
|
[_sysEdit setPlaceholderFont:textFont];
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2016-02-04 14:21:07 +08:00
|
|
|
void EditBoxImplMac::setNativeFontColor(const cocos2d::Color4B &color)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2016-02-04 14:21:07 +08:00
|
|
|
NSColor *newColor = [NSColor colorWithCalibratedRed:color.r / 255.0f
|
|
|
|
green:color.g / 255.0f
|
|
|
|
blue:color.b / 255.0f
|
|
|
|
alpha:color.a / 255.f];
|
|
|
|
|
|
|
|
[_sysEdit setTextColor:newColor];
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
2015-03-02 19:31:59 +08:00
|
|
|
|
2016-02-04 14:21:07 +08:00
|
|
|
void EditBoxImplMac::setNativePlaceholderFontColor(const cocos2d::Color4B &color)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2016-02-04 14:21:07 +08:00
|
|
|
NSColor *newColor = [NSColor colorWithCalibratedRed:color.r/255.f
|
|
|
|
green:color.g / 255.f
|
|
|
|
blue:color.b / 255.f
|
|
|
|
alpha:color.a / 255.f];
|
|
|
|
[_sysEdit setPlaceholderFontColor:newColor];
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2016-02-04 14:21:07 +08:00
|
|
|
void EditBoxImplMac::setNativeInputMode(EditBox::InputMode inputMode)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2016-02-04 14:21:07 +08:00
|
|
|
[_sysEdit setInputMode:inputMode];
|
|
|
|
auto oldPosition = _editBox->getPosition();
|
|
|
|
_editBox->setPosition(_editBox->getPosition() + Vec2(10,10));
|
|
|
|
_editBox->setPosition(oldPosition);
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2016-02-04 14:21:07 +08:00
|
|
|
void EditBoxImplMac::setNativeMaxLength(int maxLength)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2016-02-04 14:21:07 +08:00
|
|
|
[_sysEdit setMaxLength:maxLength];
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-04 14:21:07 +08:00
|
|
|
void EditBoxImplMac::setNativeInputFlag(EditBox::InputFlag inputFlag)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2016-02-04 14:21:07 +08:00
|
|
|
[_sysEdit setInputFlag:inputFlag];
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2016-02-04 14:21:07 +08:00
|
|
|
void EditBoxImplMac::setNativeReturnType(EditBox::KeyboardReturnType returnType)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2016-02-04 14:21:07 +08:00
|
|
|
[_sysEdit setReturnType:returnType];
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EditBoxImplMac::isEditing()
|
|
|
|
{
|
|
|
|
return [_sysEdit isEditState] ? true : false;
|
|
|
|
}
|
|
|
|
|
2016-02-04 14:21:07 +08:00
|
|
|
void EditBoxImplMac::setNativeText(const char *pText)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2016-02-04 14:21:07 +08:00
|
|
|
NSString *text = [NSString stringWithUTF8String:pText];
|
|
|
|
[_sysEdit setText:text];
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2016-02-04 14:21:07 +08:00
|
|
|
void EditBoxImplMac::setNativePlaceHolder(const char* pText)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2016-02-04 14:21:07 +08:00
|
|
|
[_sysEdit setPlaceHolder:pText];
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2016-02-04 14:21:07 +08:00
|
|
|
void EditBoxImplMac::setNativeVisible(bool visible)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2016-02-04 14:21:07 +08:00
|
|
|
[_sysEdit setVisible:visible];
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2016-02-04 14:21:07 +08:00
|
|
|
void EditBoxImplMac::updateNativeFrame(const cocos2d::Rect &rect)
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2016-02-04 14:21:07 +08:00
|
|
|
GLView* eglView = Director::getInstance()->getOpenGLView();
|
|
|
|
auto viewPortRect = eglView->getViewPortRect();
|
|
|
|
// Coordinate System on OSX has its origin at the lower left corner.
|
|
|
|
// https://developer.apple.com/library/ios/documentation/General/Conceptual/Devpedia-CocoaApp/CoordinateSystem.html
|
|
|
|
auto screenPosY = viewPortRect.size.height - rect.origin.y - rect.size.height;
|
|
|
|
[_sysEdit updateFrame:CGRectMake(rect.origin.x,
|
|
|
|
screenPosY,
|
|
|
|
rect.size.width, rect.size.height)];
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
2015-07-17 23:05:42 +08:00
|
|
|
|
2016-02-04 14:21:07 +08:00
|
|
|
const char* EditBoxImplMac::getNativeDefaultFontName()
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2016-02-04 14:21:07 +08:00
|
|
|
return [[_sysEdit getDefaultFontName] UTF8String];
|
2014-08-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-04 14:21:07 +08:00
|
|
|
void EditBoxImplMac::nativeOpenKeyboard()
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
2016-02-04 14:21:07 +08:00
|
|
|
[_sysEdit setVisible:YES];
|
2014-08-19 10:28:24 +08:00
|
|
|
[_sysEdit openKeyboard];
|
|
|
|
}
|
|
|
|
|
2016-02-04 14:21:07 +08:00
|
|
|
void EditBoxImplMac::nativeCloseKeyboard()
|
2014-08-19 10:28:24 +08:00
|
|
|
{
|
|
|
|
[_sysEdit closeKeyboard];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif // #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
|
|
|
|
|
|
|
|