axmol/core/ui/UIEditBox/UIEditBoxImpl-ios.mm

249 lines
7.5 KiB
Plaintext
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2012 James Chen
Copyright (c) 2013-2015 zilongshanren
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
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:
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.
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.
****************************************************************************/
#include "ui/UIEditBox/UIEditBoxImpl-ios.h"
2022-07-16 10:43:05 +08:00
#if (AX_TARGET_PLATFORM == AX_PLATFORM_IOS)
2019-11-23 20:27:39 +08:00
# define kLabelZOrder 9999
2019-11-23 20:27:39 +08:00
# include "ui/UIEditBox/UIEditBox.h"
# include "base/CCDirector.h"
# include "2d/CCLabel.h"
# import "platform/ios/CCEAGLView-ios.h"
2019-11-23 20:27:39 +08:00
# import <Foundation/Foundation.h>
# import <UIKit/UIKit.h>
2019-11-23 20:27:39 +08:00
# import "ui/UIEditBox/iOS/CCUIEditBoxIOS.h"
2019-11-23 20:27:39 +08:00
2022-08-08 18:02:17 +08:00
# define getEditBoxImplIOS() ((ax::ui::EditBoxImplIOS*)_editBox)
2019-11-23 20:27:39 +08:00
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
namespace ui
{
2019-11-23 20:27:39 +08:00
EditBoxImpl* __createSystemEditBox(EditBox* pEditBox)
{
return new EditBoxImplIOS(pEditBox);
}
EditBoxImplIOS::EditBoxImplIOS(EditBox* pEditText) : EditBoxImplCommon(pEditText), _systemControl(nullptr) {}
2019-11-23 20:27:39 +08:00
EditBoxImplIOS::~EditBoxImplIOS()
{
[_systemControl release];
_systemControl = nil;
}
2019-11-23 20:27:39 +08:00
void EditBoxImplIOS::createNativeControl(const Rect& frame)
{
auto glView = ax::Director::getInstance()->getOpenGLView();
2019-11-23 20:27:39 +08:00
Rect rect(0, 0, frame.size.width * glView->getScaleX(), frame.size.height * glView->getScaleY());
2019-11-23 20:27:39 +08:00
2022-08-08 18:02:17 +08:00
float factor = ax::Director::getInstance()->getContentScaleFactor();
2019-11-23 20:27:39 +08:00
rect.size.width /= factor;
rect.size.height /= factor;
_systemControl = [[UIEditBoxImplIOS_objc alloc]
initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)
editBox:this];
2019-11-23 20:27:39 +08:00
}
bool EditBoxImplIOS::isEditing()
{
return [_systemControl isEditState] ? true : false;
}
void EditBoxImplIOS::doAnimationWhenKeyboardMove(float duration, float distance)
{
if ([_systemControl isEditState] || distance < 0.0f)
{
2019-11-23 20:27:39 +08:00
[_systemControl doAnimationWhenKeyboardMoveWithDuration:duration distance:distance];
}
}
2019-11-23 20:27:39 +08:00
void EditBoxImplIOS::setNativeFont(const char* pFontName, int fontSize)
{
UIFont* textFont = constructFont(pFontName, fontSize);
if (textFont != nil)
{
2019-11-23 20:27:39 +08:00
[_systemControl setFont:textFont];
}
}
2019-11-23 20:27:39 +08:00
void EditBoxImplIOS::setNativeFontColor(const Color4B& color)
{
_systemControl.textColor = [UIColor colorWithRed:color.r / 255.0f
green:color.g / 255.0f
blue:color.b / 255.0f
alpha:color.a / 255.f];
2019-11-23 20:27:39 +08:00
}
void EditBoxImplIOS::setNativePlaceholderFont(const char* pFontName, int fontSize)
{
UIFont* textFont = constructFont(pFontName, fontSize);
if (textFont != nil)
{
2019-11-23 20:27:39 +08:00
[_systemControl setPlaceholderFont:textFont];
}
}
void EditBoxImplIOS::setNativePlaceholderFontColor(const Color4B& color)
{
[_systemControl setPlaceholderTextColor:[UIColor colorWithRed:color.r / 255.0f
green:color.g / 255.0f
blue:color.b / 255.0f
alpha:color.a / 255.f]];
2019-11-23 20:27:39 +08:00
}
void EditBoxImplIOS::setNativeInputMode(EditBox::InputMode inputMode)
{
[_systemControl setInputMode:inputMode];
auto oldPos = _editBox->getPosition();
_editBox->setPosition(oldPos + Vec2(10, 10));
2019-11-23 20:27:39 +08:00
_editBox->setPosition(oldPos);
}
void EditBoxImplIOS::setNativeInputFlag(EditBox::InputFlag inputFlag)
{
[_systemControl setInputFlag:inputFlag];
}
2019-11-23 20:27:39 +08:00
NSString* removeSiriString(NSString* str)
{
NSString* siriString = @"\xef\xbf\xbc";
return [str stringByReplacingOccurrencesOfString:siriString withString:@""];
}
const char* EditBoxImplIOS::getText()
{
return [removeSiriString(_systemControl.text) UTF8String];
}
void EditBoxImplIOS::setNativeReturnType(EditBox::KeyboardReturnType returnType)
{
[_systemControl setReturnType:returnType];
}
2022-08-08 18:02:17 +08:00
void EditBoxImplIOS::setNativeTextHorizontalAlignment(ax::TextHAlignment alignment)
2019-11-23 20:27:39 +08:00
{
[_systemControl setTextHorizontalAlignment:alignment];
}
void EditBoxImplIOS::setNativeText(const char* pText)
{
NSString* nsText = [NSString stringWithUTF8String:pText];
if ([nsText compare:_systemControl.text] != NSOrderedSame)
{
2019-11-23 20:27:39 +08:00
_systemControl.text = nsText;
}
}
void EditBoxImplIOS::setNativePlaceHolder(const char* pText)
{
[_systemControl setPlaceHolder:[NSString stringWithUTF8String:pText]];
}
void EditBoxImplIOS::setNativeVisible(bool visible)
{
[_systemControl setVisible:visible];
}
void EditBoxImplIOS::updateNativeFrame(const Rect& rect)
{
auto glView = ax::Director::getInstance()->getOpenGLView();
CCEAGLView* eaglView = (CCEAGLView*)glView->getEAGLView();
2019-11-23 20:27:39 +08:00
float factor = eaglView.contentScaleFactor;
2021-12-31 11:00:35 +08:00
[_systemControl updateFrame:CGRectMake(rect.origin.x / factor, rect.origin.y / factor, rect.size.width / factor,
rect.size.height / factor)];
2019-11-23 20:27:39 +08:00
}
const char* EditBoxImplIOS::getNativeDefaultFontName()
{
const char* pDefaultFontName = [[_systemControl getDefaultFontName] UTF8String];
return pDefaultFontName;
}
void EditBoxImplIOS::nativeOpenKeyboard()
{
[_systemControl setVisible:YES];
[_systemControl openKeyboard];
}
void EditBoxImplIOS::nativeCloseKeyboard()
{
[_systemControl closeKeyboard];
}
UIFont* EditBoxImplIOS::constructFont(const char* fontName, int fontSize)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(fontName != nullptr, "fontName can't be nullptr");
CCEAGLView* eaglView = static_cast<CCEAGLView*>(ax::Director::getInstance()->getOpenGLView()->getEAGLView());
float retinaFactor = eaglView.contentScaleFactor;
NSString* fntName = [NSString stringWithUTF8String:fontName];
2019-11-23 20:27:39 +08:00
fntName = [[fntName lastPathComponent] stringByDeletingPathExtension];
auto glView = ax::Director::getInstance()->getOpenGLView();
float scaleFactor = glView->getScaleX();
2019-11-23 20:27:39 +08:00
if (fontSize == -1)
{
fontSize = _systemControl.frameRect.size.height * 2 / 3;
2019-11-23 20:27:39 +08:00
}
else
{
fontSize = fontSize * scaleFactor / retinaFactor;
}
UIFont* textFont = nil;
2019-11-23 20:27:39 +08:00
if (strlen(fontName) > 0)
{
textFont = [UIFont fontWithName:fntName size:fontSize];
if (textFont == nil)
{
2019-11-23 20:27:39 +08:00
textFont = [UIFont systemFontOfSize:fontSize];
}
}
else
{
textFont = [UIFont systemFontOfSize:fontSize];
}
return textFont;
}
}
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_IOS) */