mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
improve editbox on mac (#1324)
* support password * support length limitation * prevent enter key in single line mode
This commit is contained in:
@@ -72,7 +72,7 @@ namespace
|
||||
{
|
||||
bool g_isMultiline = false;
|
||||
bool g_confirmHold = false;
|
||||
int g_maxLength = 0;
|
||||
int g_maxLength = INT_MAX;
|
||||
KeyboardEventHandler* g_keyboardHandler = nil;
|
||||
|
||||
UITextField* g_textField = nil;
|
||||
|
||||
@@ -27,18 +27,131 @@
|
||||
#include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
|
||||
#include "cocos/scripting/js-bindings/manual/jsb_global.h"
|
||||
|
||||
@interface TextViewDelegate : NSObject<NSTextViewDelegate>
|
||||
-(void) textDidChange:(NSNotification *)notification;
|
||||
@end
|
||||
/*************************************************************************
|
||||
Forward declaration of global functions.
|
||||
************************************************************************/
|
||||
namespace
|
||||
{
|
||||
void callJSFunc(const std::string& type, const std::string& text);
|
||||
}
|
||||
|
||||
/*************************************************************************
|
||||
Global variables.
|
||||
************************************************************************/
|
||||
namespace
|
||||
{
|
||||
NSTextView* g_textView = nil;
|
||||
NSScrollView* g_scrollView = nil;
|
||||
NSTextField* g_textField = nil;
|
||||
NSSecureTextField* g_secureTextField = nil;
|
||||
bool g_isMultiline = false;
|
||||
int g_maxLength = 0;
|
||||
bool g_isPassword = false;
|
||||
int g_maxLength = INT_MAX;
|
||||
se::Value g_textInputCallback;
|
||||
}
|
||||
|
||||
/*************************************************************************
|
||||
TextViewDelegate
|
||||
************************************************************************/
|
||||
@interface TextViewDelegate : NSObject<NSTextViewDelegate>
|
||||
@end
|
||||
|
||||
@implementation TextViewDelegate
|
||||
// Get notification when something is input.
|
||||
- (void) textDidChange:(NSNotification *)notification
|
||||
{
|
||||
callJSFunc("input", [[g_textView.textStorage string] UTF8String]);
|
||||
}
|
||||
|
||||
// Max length limitaion
|
||||
- (BOOL) textView:(NSTextView *)textView
|
||||
shouldChangeTextInRange:(NSRange)affectedCharRange
|
||||
replacementString:(NSString *)replacementString
|
||||
{
|
||||
NSUInteger newLength = [textView.string length] + [replacementString length] - affectedCharRange.length;
|
||||
if (newLength > g_maxLength)
|
||||
return FALSE;
|
||||
|
||||
if (!g_isMultiline && [replacementString containsString:@"\n"])
|
||||
return FALSE;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
@end
|
||||
|
||||
/*************************************************************************
|
||||
TextFieldDelegate
|
||||
************************************************************************/
|
||||
@interface TextFieldDelegate: NSObject<NSTextFieldDelegate>
|
||||
@end
|
||||
|
||||
@implementation TextFieldDelegate
|
||||
- (void)controlTextDidChange:(NSNotification *)notification
|
||||
{
|
||||
NSTextField *textField = [notification object];
|
||||
callJSFunc("input", [textField.stringValue UTF8String]);
|
||||
}
|
||||
@end
|
||||
|
||||
/*************************************************************************
|
||||
TextFieldFormatter: used for textfield length limitation.
|
||||
************************************************************************/
|
||||
@interface TextFieldFormatter : NSFormatter
|
||||
{
|
||||
int maxLength;
|
||||
}
|
||||
- (void)setMaximumLength:(int)len;
|
||||
@end
|
||||
|
||||
@implementation TextFieldFormatter
|
||||
|
||||
- (id)init
|
||||
{
|
||||
if(self = [super init])
|
||||
maxLength = INT_MAX;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)setMaximumLength:(int)len
|
||||
{
|
||||
maxLength = len;
|
||||
}
|
||||
|
||||
- (NSString *)stringForObjectValue:(id)object
|
||||
{
|
||||
return (NSString *)object;
|
||||
}
|
||||
|
||||
- (BOOL)getObjectValue:(id *)object forString:(NSString *)string errorDescription:(NSString **)error
|
||||
{
|
||||
*object = string;
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (BOOL)isPartialStringValid:(NSString **)partialStringPtr
|
||||
proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
|
||||
originalString:(NSString *)origString
|
||||
originalSelectedRange:(NSRange)origSelRange
|
||||
errorDescription:(NSString **)error
|
||||
{
|
||||
if ([*partialStringPtr length] > maxLength)
|
||||
return NO;
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (NSAttributedString *)attributedStringForObjectValue:(id)anObject withDefaultAttributes:(NSDictionary *)attributes
|
||||
{
|
||||
return nil;
|
||||
}
|
||||
@end
|
||||
|
||||
/*************************************************************************
|
||||
Implementation of global helper functions.
|
||||
************************************************************************/
|
||||
namespace
|
||||
{
|
||||
void getTextInputCallback()
|
||||
{
|
||||
if (! g_textInputCallback.isUndefined())
|
||||
@@ -82,22 +195,66 @@ namespace
|
||||
g_textView.string = [NSString stringWithUTF8String:showInfo.defaultValue.c_str()];
|
||||
g_textView.frame = rect;
|
||||
g_scrollView.frame = rect;
|
||||
|
||||
|
||||
auto glfwWindow = ((cocos2d::GLView*)cocos2d::Application::getInstance()->getView())->getGLFWWindow();
|
||||
NSWindow* nsWindow = glfwGetCocoaWindow(glfwWindow);
|
||||
[nsWindow.contentView addSubview:g_scrollView];
|
||||
[nsWindow makeFirstResponder:g_scrollView];
|
||||
}
|
||||
|
||||
void doInitTextField(NSTextField* textField, const CGRect& rect, const cocos2d::EditBox::ShowInfo& showInfo)
|
||||
{
|
||||
textField.editable = TRUE;
|
||||
textField.wantsLayer = TRUE;
|
||||
textField.frame = rect;
|
||||
textField.stringValue = [NSString stringWithUTF8String:showInfo.defaultValue.c_str()];
|
||||
[(TextFieldFormatter*)textField.formatter setMaximumLength: showInfo.maxLength];
|
||||
|
||||
auto glfwWindow = ((cocos2d::GLView*)cocos2d::Application::getInstance()->getView())->getGLFWWindow();
|
||||
NSWindow* nsWindow = glfwGetCocoaWindow(glfwWindow);
|
||||
[nsWindow.contentView addSubview:textField];
|
||||
[textField becomeFirstResponder];
|
||||
}
|
||||
|
||||
void initTextField(const cocos2d::EditBox::ShowInfo& showInfo)
|
||||
{
|
||||
CGRect rect = CGRectMake(showInfo.x, showInfo.y, showInfo.width, showInfo.height);
|
||||
|
||||
// Use NSSecureTextField for password, use NSTextField for others.
|
||||
if (g_isPassword)
|
||||
{
|
||||
if (! g_secureTextField)
|
||||
{
|
||||
g_secureTextField = [[NSSecureTextField alloc] init];
|
||||
g_secureTextField.delegate = [[TextFieldDelegate alloc] init];
|
||||
g_secureTextField.formatter = [[TextFieldFormatter alloc] init];
|
||||
}
|
||||
doInitTextField(g_secureTextField, rect, showInfo);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (! g_textField)
|
||||
{
|
||||
g_textField = [[NSTextField alloc] init];
|
||||
g_textField.delegate = [[TextFieldDelegate alloc] init];
|
||||
g_textField.formatter = [[TextFieldFormatter alloc] init];
|
||||
}
|
||||
doInitTextField(g_textField, rect, showInfo);
|
||||
}
|
||||
}
|
||||
|
||||
void init(const cocos2d::EditBox::ShowInfo& showInfo)
|
||||
{
|
||||
if (showInfo.isMultiline)
|
||||
initTextView(showInfo);
|
||||
else
|
||||
initTextField(showInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@implementation TextViewDelegate
|
||||
|
||||
-(void) textDidChange:(NSNotification *)notification
|
||||
{
|
||||
callJSFunc("input", [[g_textView.textStorage string] UTF8String]);
|
||||
}
|
||||
|
||||
@end
|
||||
/*************************************************************************
|
||||
Implementation of EditBox.
|
||||
************************************************************************/
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
@@ -105,8 +262,9 @@ void EditBox::show(const ShowInfo& showInfo)
|
||||
{
|
||||
g_isMultiline = showInfo.isMultiline;
|
||||
g_maxLength = showInfo.maxLength;
|
||||
g_isPassword = showInfo.inputType == "password";
|
||||
|
||||
initTextView(showInfo);
|
||||
init(showInfo);
|
||||
((GLView*)Application::getInstance()->getView())->setIsEditboxEditing(true);
|
||||
}
|
||||
|
||||
@@ -116,12 +274,33 @@ void EditBox::hide()
|
||||
if (g_scrollView)
|
||||
[g_scrollView removeFromSuperview];
|
||||
|
||||
if (g_textField)
|
||||
{
|
||||
[g_textField resignFirstResponder];
|
||||
[g_textField removeFromSuperview];
|
||||
}
|
||||
|
||||
if (g_secureTextField)
|
||||
{
|
||||
[g_secureTextField resignFirstResponder];
|
||||
[g_secureTextField removeFromSuperview];
|
||||
}
|
||||
|
||||
((GLView*)Application::getInstance()->getView())->setIsEditboxEditing(false);
|
||||
}
|
||||
|
||||
void EditBox::complete()
|
||||
{
|
||||
callJSFunc("complete", [[g_textView.textStorage string] UTF8String]);
|
||||
if (g_isMultiline)
|
||||
callJSFunc("complete", [[g_textView.textStorage string] UTF8String]);
|
||||
else
|
||||
{
|
||||
if (g_isPassword)
|
||||
callJSFunc("complete", [g_secureTextField.stringValue UTF8String]);
|
||||
else
|
||||
callJSFunc("complete", [g_textField.stringValue UTF8String]);
|
||||
}
|
||||
|
||||
EditBox::hide();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user