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

391 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.
2022-08-08 18:02:17 +08:00
https://axys1.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.
****************************************************************************/
#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"
2022-08-08 18:02:17 +08:00
#define getEditBoxImplMac() ((ax::ui::EditBoxImplMac*)_editBox)
2019-11-23 20:27:39 +08:00
@implementation UIEditBoxImplMac
- (instancetype)initWithFrame:(NSRect)frameRect editBox:(void*)editBox
2019-11-23 20:27:39 +08:00
{
self = [super init];
if (self)
{
_editState = NO;
2019-11-23 20:27:39 +08:00
self.frameRect = frameRect;
self.editBox = editBox;
2022-08-08 18:02:17 +08:00
self.dataInputMode = ax::ui::EditBox::InputFlag::LOWERCASE_ALL_CHARACTERS;
self.keyboardReturnType = ax::ui::EditBox::KeyboardReturnType::DEFAULT;
2019-11-23 20:27:39 +08:00
[self createMultiLineTextField];
}
2019-11-23 20:27:39 +08:00
return self;
}
- (void)createSingleLineTextField
{
CCUISingleLineTextField* textField = [[[CCUISingleLineTextField alloc] initWithFrame:self.frameRect] autorelease];
2019-11-23 20:27:39 +08:00
self.textInput = textField;
}
- (void)createMultiLineTextField
{
CCUIMultilineTextField* textView = [[[CCUIMultilineTextField alloc] initWithFrame:self.frameRect] autorelease];
2019-11-23 20:27:39 +08:00
[textView setVerticallyResizable:NO];
self.textInput = textView;
}
- (void)createPasswordTextField
{
CCUIPasswordTextField* textField = [[[CCUIPasswordTextField alloc] initWithFrame:self.frameRect] autorelease];
2019-11-23 20:27:39 +08:00
self.textInput = textField;
}
- (void)setTextInput:(NSView<CCUITextInput>*)textInput
2019-11-23 20:27:39 +08:00
{
if (_textInput == textInput)
{
2019-11-23 20:27:39 +08:00
return;
}
// Migrate properties
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 ?: @"";
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];
2019-11-23 20:27:39 +08:00
_textInput = [textInput retain];
2019-11-23 20:27:39 +08:00
[_textInput performSelector:@selector(setTextColor:) withObject:_textInput.ccui_textColor];
[_textInput performSelector:@selector(setBackgroundColor:) withObject:[NSColor clearColor]];
if (![_textInput isKindOfClass:[NSTextView class]])
{
2019-11-23 20:27:39 +08:00
[_textInput performSelector:@selector(setBordered:) withObject:nil];
}
_textInput.hidden = NO;
2019-11-23 20:27:39 +08:00
_textInput.wantsLayer = YES;
2019-11-23 20:27:39 +08:00
[_textInput ccui_setDelegate:self];
2019-11-23 20:27:39 +08:00
[self setInputFlag:self.dataInputMode];
[self setReturnType:self.keyboardReturnType];
}
- (void)updateFrame:(CGRect)rect
{
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;
frame.size.width = rect.size.width;
2019-11-23 20:27:39 +08:00
self.textInput.frame = frame;
}
- (void)dealloc
{
self.textInput = nil;
2019-11-23 20:27:39 +08:00
[super dealloc];
}
- (NSWindow*)window
2019-11-23 20:27:39 +08:00
{
auto glView = ax::Director::getInstance()->getOpenGLView();
return glView->getCocoaWindow();
2019-11-23 20:27:39 +08:00
}
- (void)openKeyboard
{
[self.window.contentView addSubview:self.textInput];
if (![self.textInput isKindOfClass:[NSTextView class]])
{
2019-11-23 20:27:39 +08:00
[self.textInput becomeFirstResponder];
}
else
{
2019-11-23 20:27:39 +08:00
[self.window makeFirstResponder:self.textInput];
}
2019-11-23 20:27:39 +08:00
auto editbox = getEditBoxImplMac()->getEditBox();
auto oldPos = editbox->getPosition();
2022-08-08 18:02:17 +08:00
editbox->setPosition(oldPos + ax::Vec2(10, 20));
2019-11-23 20:27:39 +08:00
editbox->setPosition(oldPos);
}
- (void)closeKeyboard
{
if (![self.textInput isKindOfClass:[NSTextView class]])
{
2019-11-23 20:27:39 +08:00
[self.textInput resignFirstResponder];
}
[self.textInput removeFromSuperview];
}
- (const char*)getText
2019-11-23 20:27:39 +08:00
{
return [self.textInput.ccui_text UTF8String];
}
- (void)controlTextDidBeginEditing:(NSNotification*)notification
2019-11-23 20:27:39 +08:00
{
_editState = YES;
2019-11-23 20:27:39 +08:00
getEditBoxImplMac()->editBoxEditingDidBegin();
}
- (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.
*/
- (void)controlTextDidChange:(NSNotification*)notification
2019-11-23 20:27:39 +08:00
{
getEditBoxImplMac()->editBoxEditingChanged([self getText]);
}
- (NSString*)getDefaultFontName
2019-11-23 20:27:39 +08:00
{
return self.textInput.ccui_font.fontName ?: @"";
}
2022-08-08 18:02:17 +08:00
- (void)setInputMode:(ax::ui::EditBox::InputMode)inputMode
2019-11-23 20:27:39 +08:00
{
// multiline input
2022-08-08 18:02:17 +08:00
if (inputMode == ax::ui::EditBox::InputMode::ANY)
{
if (![self.textInput isKindOfClass:[NSTextView class]])
{
2019-11-23 20:27:39 +08:00
[self createMultiLineTextField];
}
}
else
{
2022-08-08 18:02:17 +08:00
if (self.dataInputMode != ax::ui::EditBox::InputFlag::PASSWORD)
{
if (![self.textInput isKindOfClass:[NSTextField class]])
{
2019-11-23 20:27:39 +08:00
[self createSingleLineTextField];
}
}
}
}
2022-08-08 18:02:17 +08:00
- (void)setInputFlag:(ax::ui::EditBox::InputFlag)inputFlag
2019-11-23 20:27:39 +08:00
{
if (self.dataInputMode == inputFlag)
{
2019-11-23 20:27:39 +08:00
return;
}
2022-08-08 18:02:17 +08:00
if (self.dataInputMode == ax::ui::EditBox::InputFlag::PASSWORD &&
inputFlag != ax::ui::EditBox::InputFlag::PASSWORD)
{
2019-11-23 20:27:39 +08:00
[self createSingleLineTextField];
}
2022-08-08 18:02:17 +08:00
if (self.dataInputMode != ax::ui::EditBox::InputFlag::PASSWORD &&
inputFlag == ax::ui::EditBox::InputFlag::PASSWORD)
{
2019-11-23 20:27:39 +08:00
[self createPasswordTextField];
}
switch (inputFlag)
{
2022-08-08 18:02:17 +08:00
case ax::ui::EditBox::InputFlag::PASSWORD:
self.dataInputMode = inputFlag;
break;
2022-08-08 18:02:17 +08:00
case ax::ui::EditBox::InputFlag::INITIAL_CAPS_WORD:
2022-07-16 10:43:05 +08:00
AXLOG("INITIAL_CAPS_WORD not implemented");
break;
2022-08-08 18:02:17 +08:00
case ax::ui::EditBox::InputFlag::INITIAL_CAPS_SENTENCE:
2022-07-16 10:43:05 +08:00
AXLOG("INITIAL_CAPS_SENTENCE not implemented");
break;
2022-08-08 18:02:17 +08:00
case ax::ui::EditBox::InputFlag::INITIAL_CAPS_ALL_CHARACTERS:
2022-07-16 10:43:05 +08:00
AXLOG("INITIAL_CAPS_ALL_CHARACTERS not implemented");
break;
2022-08-08 18:02:17 +08:00
case ax::ui::EditBox::InputFlag::SENSITIVE:
2022-07-16 10:43:05 +08:00
AXLOG("SENSITIVE not implemented");
break;
2022-08-08 18:02:17 +08:00
case ax::ui::EditBox::InputFlag::LOWERCASE_ALL_CHARACTERS:
2022-07-16 10:43:05 +08:00
AXLOG("LOWERCASE_ALL_CHARACTERS not implemented");
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
}
2022-08-08 18:02:17 +08:00
- (void)setReturnType:(ax::ui::EditBox::KeyboardReturnType)returnType
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXLOG("setReturnType not implemented");
2019-11-23 20:27:39 +08:00
}
2022-08-08 18:02:17 +08:00
- (void)setTextHorizontalAlignment:(ax::TextHAlignment)alignment
2019-11-23 20:27:39 +08:00
{
// swizzle center & right, for some reason they're backwards on !TARGET_OS_IPHONE
2022-08-08 18:02:17 +08:00
if (alignment == ax::TextHAlignment::CENTER)
alignment = ax::TextHAlignment::RIGHT;
else if (alignment == ax::TextHAlignment::RIGHT)
alignment = ax::TextHAlignment::CENTER;
2019-11-23 20:27:39 +08:00
self.textInput.ccui_alignment = static_cast<NSTextAlignment>(alignment);
}
- (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;
}
- (void)setFont:(NSFont*)font
2019-11-23 20:27:39 +08:00
{
if (font != nil)
{
2019-11-23 20:27:39 +08:00
self.textInput.ccui_font = font;
}
}
- (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;
}
- (void)setText:(NSString*)text
2019-11-23 20:27:39 +08:00
{
self.textInput.ccui_text = text;
}
- (BOOL)textShouldBeginEditing:(NSText*)textObject // YES means do it
2019-11-23 20:27:39 +08:00
{
_editState = YES;
2019-11-23 20:27:39 +08:00
getEditBoxImplMac()->editBoxEditingDidBegin();
return YES;
}
- (void)textDidEndEditing:(NSNotification*)notification
2019-11-23 20:27:39 +08:00
{
_editState = NO;
getEditBoxImplMac()->editBoxEditingDidEnd([self getText], [self getEndAction:notification]);
}
2022-08-08 18:02:17 +08:00
- (ax::ui::EditBoxDelegate::EditBoxEndAction)getEndAction:(NSNotification*)notification
2019-11-23 20:27:39 +08:00
{
2022-08-08 18:02:17 +08:00
auto type = ax::ui::EditBoxDelegate::EditBoxEndAction::UNKNOWN;
2019-11-23 20:27:39 +08:00
NSUInteger reasonForEnding = [[[notification userInfo] objectForKey:@"NSTextMovement"] unsignedIntValue];
if (reasonForEnding == NSTabTextMovement)
{
2022-08-08 18:02:17 +08:00
type = ax::ui::EditBoxDelegate::EditBoxEndAction::TAB_TO_NEXT;
}
else if (reasonForEnding == NSBacktabTextMovement)
{
2022-08-08 18:02:17 +08:00
type = ax::ui::EditBoxDelegate::EditBoxEndAction::TAB_TO_PREVIOUS;
}
else if (reasonForEnding == NSReturnTextMovement)
{
2022-08-08 18:02:17 +08:00
type = ax::ui::EditBoxDelegate::EditBoxEndAction::RETURN;
2019-11-23 20:27:39 +08:00
}
return type;
}
- (void)textDidChange:(NSNotification*)notification
2019-11-23 20:27:39 +08:00
{
NSTextView* textView = notification.object;
2019-11-23 20:27:39 +08:00
const char* inputText = [textView.string UTF8String];
2019-11-23 20:27:39 +08:00
getEditBoxImplMac()->editBoxEditingChanged(inputText);
}
- (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;
}
if (affectedCharRange.length + affectedCharRange.location > textView.string.length)
{
2019-11-23 20:27:39 +08:00
return NO;
}
NSUInteger oldLength = textView.string.length;
2019-11-23 20:27:39 +08:00
NSUInteger replacementLength = replacementString.length;
NSUInteger rangeLength = affectedCharRange.length;
2019-11-23 20:27:39 +08:00
NSUInteger newLength = oldLength - rangeLength + replacementLength;
2019-11-23 20:27:39 +08:00
return newLength <= maxLength;
}
@end