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