axmol/cocos/ui/UIEditBox/Mac/CCUIEditBoxMac.mm

369 lines
11 KiB
Plaintext
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 zilongshanren
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-31 11:00:35 +08:00
2019-11-23 20:27:39 +08:00
http://www.cocos2d-x.org
2021-12-31 11:00:35 +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 11:00:35 +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 11:00:35 +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.
****************************************************************************/
#import "ui/UIEditBox/Mac/CCUIEditBoxMac.h"
#include "base/CCDirector.h"
#include "ui/UIEditBox/Mac/CCUISingleLineTextField.h"
#include "ui/UIEditBox/Mac/CCUIPasswordTextField.h"
#include "ui/UIEditBox/Mac/CCUIMultilineTextField.h"
2021-12-31 11:00:35 +08:00
#define getEditBoxImplMac() ((cocos2d::ui::EditBoxImplMac *)_editBox)
2019-11-23 20:27:39 +08:00
@implementation UIEditBoxImplMac
2021-12-31 11:00:35 +08:00
- (instancetype)initWithFrame:(NSRect)frameRect editBox:(void *)editBox
2019-11-23 20:27:39 +08:00
{
self = [super init];
2021-12-31 11:00:35 +08:00
if (self) {
_editState = NO;
2019-11-23 20:27:39 +08:00
self.frameRect = frameRect;
2021-12-31 11:00:35 +08:00
self.editBox = editBox;
self.dataInputMode = cocos2d::ui::EditBox::InputFlag::LOWERCASE_ALL_CHARACTERS;
2019-11-23 20:27:39 +08:00
self.keyboardReturnType = cocos2d::ui::EditBox::KeyboardReturnType::DEFAULT;
2021-12-31 11:00:35 +08:00
2019-11-23 20:27:39 +08:00
[self createMultiLineTextField];
}
2021-12-31 11:00:35 +08:00
2019-11-23 20:27:39 +08:00
return self;
}
- (void)createSingleLineTextField
{
2021-12-31 11:00:35 +08:00
CCUISingleLineTextField *textField = [[[CCUISingleLineTextField alloc] initWithFrame:self.frameRect] autorelease];
2019-11-23 20:27:39 +08:00
self.textInput = textField;
}
- (void)createMultiLineTextField
{
2021-12-31 11:00:35 +08:00
CCUIMultilineTextField *textView = [[[CCUIMultilineTextField alloc] initWithFrame:self.frameRect] autorelease];
2019-11-23 20:27:39 +08:00
[textView setVerticallyResizable:NO];
self.textInput = textView;
}
- (void)createPasswordTextField
{
2021-12-31 11:00:35 +08:00
CCUIPasswordTextField *textField = [[[CCUIPasswordTextField alloc] initWithFrame:self.frameRect] autorelease];
2019-11-23 20:27:39 +08:00
self.textInput = textField;
}
2021-12-31 11:00:35 +08:00
- (void)setTextInput:(NSView<CCUITextInput> *)textInput
2019-11-23 20:27:39 +08:00
{
2021-12-31 11:00:35 +08:00
if (_textInput == textInput) {
2019-11-23 20:27:39 +08:00
return;
}
// Migrate properties
2021-12-31 11:00:35 +08:00
textInput.ccui_textColor = _textInput.ccui_textColor ?: [NSColor whiteColor];
textInput.ccui_text = _textInput.ccui_text ?: @"";
2019-11-23 20:27:39 +08:00
textInput.ccui_placeholder = _textInput.ccui_placeholder ?: @"";
2021-12-31 11:00:35 +08:00
textInput.ccui_font = _textInput.ccui_font ?: [NSFont systemFontOfSize:self.frameRect.size.height*3/2];
textInput.ccui_maxLength = getEditBoxImplMac()->getMaxLength();
textInput.ccui_alignment = _textInput.ccui_alignment;
2019-11-23 20:27:39 +08:00
[_textInput removeFromSuperview];
[_textInput release];
2021-12-31 11:00:35 +08:00
2019-11-23 20:27:39 +08:00
_textInput = [textInput retain];
2021-12-31 11:00:35 +08:00
2019-11-23 20:27:39 +08:00
[_textInput performSelector:@selector(setTextColor:) withObject:_textInput.ccui_textColor];
[_textInput performSelector:@selector(setBackgroundColor:) withObject:[NSColor clearColor]];
2021-12-31 11:00:35 +08:00
if (![_textInput isKindOfClass:[NSTextView class]]) {
2019-11-23 20:27:39 +08:00
[_textInput performSelector:@selector(setBordered:) withObject:nil];
}
2021-12-31 11:00:35 +08:00
_textInput.hidden = NO;
2019-11-23 20:27:39 +08:00
_textInput.wantsLayer = YES;
2021-12-31 11:00:35 +08:00
2019-11-23 20:27:39 +08:00
[_textInput ccui_setDelegate:self];
2021-12-31 11:00:35 +08:00
2019-11-23 20:27:39 +08:00
[self setInputFlag:self.dataInputMode];
[self setReturnType:self.keyboardReturnType];
}
- (void)updateFrame:(CGRect)rect
{
2021-12-31 11:00:35 +08:00
NSRect frame = self.textInput.frame;
frame.origin.x = rect.origin.x;
frame.origin.y = rect.origin.y;
2019-11-23 20:27:39 +08:00
frame.size.height = rect.size.height;
2021-12-31 11:00:35 +08:00
frame.size.width = rect.size.width;
2019-11-23 20:27:39 +08:00
self.textInput.frame = frame;
}
- (void)dealloc
{
self.textInput = nil;
2021-12-31 11:00:35 +08:00
2019-11-23 20:27:39 +08:00
[super dealloc];
}
2021-12-31 11:00:35 +08:00
- (NSWindow *)window
2019-11-23 20:27:39 +08:00
{
auto glview = cocos2d::Director::getInstance()->getOpenGLView();
return glview->getCocoaWindow();
}
- (void)openKeyboard
{
[self.window.contentView addSubview:self.textInput];
2021-12-31 11:00:35 +08:00
if (![self.textInput isKindOfClass:[NSTextView class]]) {
2019-11-23 20:27:39 +08:00
[self.textInput becomeFirstResponder];
2021-12-31 11:00:35 +08:00
}else {
2019-11-23 20:27:39 +08:00
[self.window makeFirstResponder:self.textInput];
}
2021-12-31 11:00:35 +08:00
2019-11-23 20:27:39 +08:00
auto editbox = getEditBoxImplMac()->getEditBox();
2021-12-31 11:00:35 +08:00
auto oldPos = editbox->getPosition();
editbox->setPosition(oldPos + cocos2d::Vec2(10,20));
2019-11-23 20:27:39 +08:00
editbox->setPosition(oldPos);
}
- (void)closeKeyboard
{
2021-12-31 11:00:35 +08:00
if (![self.textInput isKindOfClass:[NSTextView class]]) {
2019-11-23 20:27:39 +08:00
[self.textInput resignFirstResponder];
}
[self.textInput removeFromSuperview];
}
2021-12-31 11:00:35 +08:00
- (const char*) getText
2019-11-23 20:27:39 +08:00
{
return [self.textInput.ccui_text UTF8String];
}
2021-12-31 11:00:35 +08:00
- (void)controlTextDidBeginEditing:(NSNotification *)notification
2019-11-23 20:27:39 +08:00
{
_editState = YES;
2021-12-31 11:00:35 +08:00
2019-11-23 20:27:39 +08:00
getEditBoxImplMac()->editBoxEditingDidBegin();
}
2021-12-31 11:00:35 +08:00
- (void)controlTextDidEndEditing:(NSNotification *)notification
2019-11-23 20:27:39 +08:00
{
_editState = NO;
getEditBoxImplMac()->editBoxEditingDidEnd([self getText], [self getEndAction:notification]);
}
- (void)setMaxLength:(int)length
{
self.textInput.ccui_maxLength = length;
}
/**
* Called each time when the text field's text has changed.
*/
2021-12-31 11:00:35 +08:00
- (void)controlTextDidChange:(NSNotification *)notification
2019-11-23 20:27:39 +08:00
{
getEditBoxImplMac()->editBoxEditingChanged([self getText]);
}
2021-12-31 11:00:35 +08:00
- (NSString *)getDefaultFontName
2019-11-23 20:27:39 +08:00
{
return self.textInput.ccui_font.fontName ?: @"";
}
- (void)setInputMode:(cocos2d::ui::EditBox::InputMode)inputMode
{
2021-12-31 11:00:35 +08:00
//multiline input
if (inputMode == cocos2d::ui::EditBox::InputMode::ANY) {
if (![self.textInput isKindOfClass:[NSTextView class]]) {
2019-11-23 20:27:39 +08:00
[self createMultiLineTextField];
}
}
2021-12-31 11:00:35 +08:00
else {
if (self.dataInputMode != cocos2d::ui::EditBox::InputFlag::PASSWORD) {
if (![self.textInput isKindOfClass:[NSTextField class]]) {
2019-11-23 20:27:39 +08:00
[self createSingleLineTextField];
}
}
}
}
- (void)setInputFlag:(cocos2d::ui::EditBox::InputFlag)inputFlag
{
2021-12-31 11:00:35 +08:00
if (self.dataInputMode == inputFlag) {
2019-11-23 20:27:39 +08:00
return;
}
2021-12-31 11:00:35 +08:00
if (self.dataInputMode == cocos2d::ui::EditBox::InputFlag::PASSWORD
&& inputFlag != cocos2d::ui::EditBox::InputFlag::PASSWORD) {
2019-11-23 20:27:39 +08:00
[self createSingleLineTextField];
}
2021-12-31 11:00:35 +08:00
if (self.dataInputMode != cocos2d::ui::EditBox::InputFlag::PASSWORD
&& inputFlag == cocos2d::ui::EditBox::InputFlag::PASSWORD) {
2019-11-23 20:27:39 +08:00
[self createPasswordTextField];
}
switch (inputFlag)
{
2021-12-31 11:00:35 +08:00
case cocos2d::ui::EditBox::InputFlag::PASSWORD:
self.dataInputMode = inputFlag;
break;
case cocos2d::ui::EditBox::InputFlag::INITIAL_CAPS_WORD:
CCLOG("INITIAL_CAPS_WORD not implemented");
break;
case cocos2d::ui::EditBox::InputFlag::INITIAL_CAPS_SENTENCE:
CCLOG("INITIAL_CAPS_SENTENCE not implemented");
break;
case cocos2d::ui::EditBox::InputFlag::INITIAL_CAPS_ALL_CHARACTERS:
CCLOG("INITIAL_CAPS_ALL_CHARACTERS not implemented");
break;
case cocos2d::ui::EditBox::InputFlag::SENSITIVE:
CCLOG("SENSITIVE not implemented");
break;
case cocos2d::ui::EditBox::InputFlag::LOWERCASE_ALL_CHARACTERS:
CCLOG("LOWERCASE_ALL_CHARACTERS not implemented");
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
}
- (void)setReturnType:(cocos2d::ui::EditBox::KeyboardReturnType)returnType
{
CCLOG("setReturnType not implemented");
}
- (void)setTextHorizontalAlignment:(cocos2d::TextHAlignment)alignment
{
// swizzle center & right, for some reason they're backwards on !TARGET_OS_IPHONE
2021-12-31 11:00:35 +08:00
if (alignment == cocos2d::TextHAlignment::CENTER) alignment = cocos2d::TextHAlignment::RIGHT;
else if (alignment == cocos2d::TextHAlignment::RIGHT) alignment = cocos2d::TextHAlignment::CENTER;
2019-11-23 20:27:39 +08:00
self.textInput.ccui_alignment = static_cast<NSTextAlignment>(alignment);
}
2021-12-31 11:00:35 +08:00
- (void)setPlaceHolder:(const char *)text
2019-11-23 20:27:39 +08:00
{
self.textInput.ccui_placeholder = [NSString stringWithUTF8String:text];
}
- (void)setVisible:(BOOL)visible
{
self.textInput.hidden = !visible;
}
- (void)setTextColor:(NSColor*)color
{
self.textInput.ccui_textColor = color;
}
2021-12-31 11:00:35 +08:00
- (void)setFont:(NSFont *)font
2019-11-23 20:27:39 +08:00
{
2021-12-31 11:00:35 +08:00
if (font != nil) {
2019-11-23 20:27:39 +08:00
self.textInput.ccui_font = font;
}
}
2021-12-31 11:00:35 +08:00
- (void)setPlaceholderFontColor:(NSColor *)color
2019-11-23 20:27:39 +08:00
{
self.textInput.ccui_placeholderColor = color;
}
- (void)setPlaceholderFont:(NSFont*)font
{
self.textInput.ccui_placeholderFont = font;
}
2021-12-31 11:00:35 +08:00
- (void)setText:(NSString *)text
2019-11-23 20:27:39 +08:00
{
self.textInput.ccui_text = text;
}
2021-12-31 11:00:35 +08:00
- (BOOL)textShouldBeginEditing:(NSText *)textObject // YES means do it
2019-11-23 20:27:39 +08:00
{
_editState = YES;
2021-12-31 11:00:35 +08:00
2019-11-23 20:27:39 +08:00
getEditBoxImplMac()->editBoxEditingDidBegin();
return YES;
}
2021-12-31 11:00:35 +08:00
- (void)textDidEndEditing:(NSNotification *)notification
2019-11-23 20:27:39 +08:00
{
_editState = NO;
getEditBoxImplMac()->editBoxEditingDidEnd([self getText], [self getEndAction:notification]);
}
2021-12-31 11:00:35 +08:00
- (cocos2d::ui::EditBoxDelegate::EditBoxEndAction)getEndAction:(NSNotification *)notification
2019-11-23 20:27:39 +08:00
{
2021-12-31 11:00:35 +08:00
auto type = cocos2d::ui::EditBoxDelegate::EditBoxEndAction::UNKNOWN;
2019-11-23 20:27:39 +08:00
NSUInteger reasonForEnding = [[[notification userInfo] objectForKey:@"NSTextMovement"] unsignedIntValue];
2021-12-31 11:00:35 +08:00
if (reasonForEnding == NSTabTextMovement) {
2019-11-23 20:27:39 +08:00
type = cocos2d::ui::EditBoxDelegate::EditBoxEndAction::TAB_TO_NEXT;
2021-12-31 11:00:35 +08:00
} else if (reasonForEnding == NSBacktabTextMovement) {
2019-11-23 20:27:39 +08:00
type = cocos2d::ui::EditBoxDelegate::EditBoxEndAction::TAB_TO_PREVIOUS;
2021-12-31 11:00:35 +08:00
} else if (reasonForEnding == NSReturnTextMovement) {
2019-11-23 20:27:39 +08:00
type = cocos2d::ui::EditBoxDelegate::EditBoxEndAction::RETURN;
}
return type;
}
2021-12-31 11:00:35 +08:00
- (void)textDidChange:(NSNotification *)notification
2019-11-23 20:27:39 +08:00
{
NSTextView* textView = notification.object;
2021-12-31 11:00:35 +08:00
2019-11-23 20:27:39 +08:00
const char* inputText = [textView.string UTF8String];
2021-12-31 11:00:35 +08:00
2019-11-23 20:27:39 +08:00
getEditBoxImplMac()->editBoxEditingChanged(inputText);
}
2021-12-31 11:00:35 +08:00
- (BOOL)textView:(NSTextView *)textView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString
2019-11-23 20:27:39 +08:00
{
int maxLength = getEditBoxImplMac()->getMaxLength();
if (maxLength < 0)
{
return YES;
}
2021-12-31 11:00:35 +08:00
if (affectedCharRange.length + affectedCharRange.location > textView.string.length) {
2019-11-23 20:27:39 +08:00
return NO;
}
2021-12-31 11:00:35 +08:00
NSUInteger oldLength = textView.string.length;
2019-11-23 20:27:39 +08:00
NSUInteger replacementLength = replacementString.length;
2021-12-31 11:00:35 +08:00
NSUInteger rangeLength = affectedCharRange.length;
2019-11-23 20:27:39 +08:00
NSUInteger newLength = oldLength - rangeLength + replacementLength;
2021-12-31 11:00:35 +08:00
2019-11-23 20:27:39 +08:00
return newLength <= maxLength;
}
2021-12-31 11:00:35 +08:00
2019-11-23 20:27:39 +08:00
@end