2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2012 Jozef Pridavok
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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:
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "platform/PlatformConfig.h"
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_MAC)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
# include "ui/UIEditBox/UIEditBoxImpl-mac.h"
|
2023-06-11 13:08:08 +08:00
|
|
|
# include "base/Director.h"
|
|
|
|
# include "base/UTF8.h"
|
2021-12-31 12:12:40 +08:00
|
|
|
# include "ui/UIEditBox/UIEditBox.h"
|
2023-06-11 13:08:08 +08:00
|
|
|
# include "ui/UIEditBox/Mac/UIEditBoxMac.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
namespace ui
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
EditBoxImpl* __createSystemEditBox(EditBox* pEditBox)
|
|
|
|
{
|
|
|
|
return new EditBoxImplMac(pEditBox);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
EditBoxImplMac::EditBoxImplMac(EditBox* pEditText) : EditBoxImplCommon(pEditText), _sysEdit(nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
//! TODO: Retina on Mac
|
2023-08-31 18:14:26 +08:00
|
|
|
//! _inRetinaMode = [[CCEAGLView sharedEGLView] contentScaleFactor] == 2.0f ? true : false;
|
2019-11-23 20:27:39 +08:00
|
|
|
_inRetinaMode = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
EditBoxImplMac::~EditBoxImplMac()
|
|
|
|
{
|
|
|
|
[_sysEdit release];
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
void EditBoxImplMac::createNativeControl(const ax::Rect& frame)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-09-05 14:13:52 +08:00
|
|
|
auto glView = ax::Director::getInstance()->getOpenGLView();
|
2021-12-31 12:12:40 +08:00
|
|
|
Size size = frame.size;
|
2022-09-05 14:13:52 +08:00
|
|
|
NSRect rect = NSMakeRect(0, 0, size.width * glView->getScaleX(), size.height * glView->getScaleY());
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
float factor = ax::Director::getInstance()->getContentScaleFactor();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
rect.size.width /= factor;
|
|
|
|
rect.size.height /= factor;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_sysEdit = [[UIEditBoxImplMac alloc] initWithFrame:rect editBox:this];
|
|
|
|
this->setNativeVisible(false);
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
NSFont* EditBoxImplMac::constructFont(const char* fontName, int fontSize)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
NSString* fntName = [NSString stringWithUTF8String:fontName];
|
|
|
|
fntName = [[fntName lastPathComponent] stringByDeletingPathExtension];
|
2019-11-23 20:27:39 +08:00
|
|
|
float retinaFactor = _inRetinaMode ? 2.0f : 1.0f;
|
2022-09-05 14:13:52 +08:00
|
|
|
auto glView = ax::Director::getInstance()->getOpenGLView();
|
|
|
|
float scaleFactor = glView->getScaleX();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (fontSize == -1)
|
|
|
|
{
|
|
|
|
NSRect frameRect = [_sysEdit.textInput frame];
|
2021-12-31 12:12:40 +08:00
|
|
|
fontSize = frameRect.size.height * 2 / 3;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fontSize = fontSize * scaleFactor / retinaFactor;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
NSFont* textFont = nil;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (strlen(fontName) == 0)
|
|
|
|
{
|
|
|
|
textFont = [NSFont systemFontOfSize:fontSize];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
textFont = [NSFont fontWithName:fntName size:fontSize];
|
2021-12-31 12:12:40 +08:00
|
|
|
if (textFont == nil)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
textFont = [NSFont systemFontOfSize:fontSize];
|
|
|
|
}
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return textFont;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void EditBoxImplMac::setNativeFont(const char* pFontName, int fontSize)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
NSFont* textFont = constructFont(pFontName, fontSize);
|
|
|
|
[_sysEdit setFont:textFont];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void EditBoxImplMac::setNativePlaceholderFont(const char* pFontName, int fontSize)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
NSFont* textFont = constructFont(pFontName, fontSize);
|
|
|
|
|
|
|
|
if (!textFont)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGWARN("Font not found: %s", pFontName);
|
2019-11-23 20:27:39 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
[_sysEdit setPlaceholderFont:textFont];
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
void EditBoxImplMac::setNativeFontColor(const ax::Color4B& color)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
NSColor* newColor = [NSColor colorWithCalibratedRed:color.r / 255.0f
|
2019-11-23 20:27:39 +08:00
|
|
|
green:color.g / 255.0f
|
|
|
|
blue:color.b / 255.0f
|
|
|
|
alpha:color.a / 255.f];
|
|
|
|
|
|
|
|
[_sysEdit setTextColor:newColor];
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
void EditBoxImplMac::setNativePlaceholderFontColor(const ax::Color4B& color)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +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];
|
2019-11-23 20:27:39 +08:00
|
|
|
[_sysEdit setPlaceholderFontColor:newColor];
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplMac::setNativeInputMode(EditBox::InputMode inputMode)
|
|
|
|
{
|
|
|
|
[_sysEdit setInputMode:inputMode];
|
|
|
|
auto oldPosition = _editBox->getPosition();
|
2021-12-31 12:12:40 +08:00
|
|
|
_editBox->setPosition(_editBox->getPosition() + Vec2(10, 10));
|
2019-11-23 20:27:39 +08:00
|
|
|
_editBox->setPosition(oldPosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplMac::setNativeMaxLength(int maxLength)
|
|
|
|
{
|
|
|
|
[_sysEdit setMaxLength:maxLength];
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplMac::setNativeInputFlag(EditBox::InputFlag inputFlag)
|
|
|
|
{
|
|
|
|
[_sysEdit setInputFlag:inputFlag];
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplMac::setNativeReturnType(EditBox::KeyboardReturnType returnType)
|
|
|
|
{
|
|
|
|
[_sysEdit setReturnType:returnType];
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
void EditBoxImplMac::setNativeTextHorizontalAlignment(ax::TextHAlignment alignment)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
[_sysEdit setTextHorizontalAlignment:alignment];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditBoxImplMac::isEditing()
|
|
|
|
{
|
|
|
|
return [_sysEdit isEditState] ? true : false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void EditBoxImplMac::setNativeText(const char* pText)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
NSString* text = [NSString stringWithUTF8String:pText];
|
2019-11-23 20:27:39 +08:00
|
|
|
[_sysEdit setText:text];
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
void EditBoxImplMac::setNativePlaceHolder(const char* pText)
|
|
|
|
{
|
|
|
|
[_sysEdit setPlaceHolder:pText];
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplMac::setNativeVisible(bool visible)
|
|
|
|
{
|
|
|
|
[_sysEdit setVisible:visible];
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
void EditBoxImplMac::updateNativeFrame(const ax::Rect& rect)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
GLView* eglView = Director::getInstance()->getOpenGLView();
|
2021-12-31 12:12:40 +08:00
|
|
|
auto frameSize = eglView->getFrameSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
// Coordinate System on OSX has its origin at the lower left corner.
|
2021-12-31 12:12:40 +08:00
|
|
|
// https://developer.apple.com/library/ios/documentation/General/Conceptual/Devpedia-CocoaApp/CoordinateSystem.html
|
2019-11-24 00:27:19 +08:00
|
|
|
auto screenPosY = frameSize.height - rect.origin.y - rect.size.height;
|
2021-12-31 12:12:40 +08:00
|
|
|
[_sysEdit updateFrame:CGRectMake(rect.origin.x, screenPosY, rect.size.width, rect.size.height)];
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
const char* EditBoxImplMac::getNativeDefaultFontName()
|
|
|
|
{
|
|
|
|
return [[_sysEdit getDefaultFontName] UTF8String];
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplMac::nativeOpenKeyboard()
|
|
|
|
{
|
|
|
|
[_sysEdit setVisible:YES];
|
|
|
|
[_sysEdit openKeyboard];
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplMac::nativeCloseKeyboard()
|
|
|
|
{
|
|
|
|
[_sysEdit closeKeyboard];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#endif // #if (AX_TARGET_PLATFORM == AX_PLATFORM_MAC)
|