mirror of https://github.com/axmolengine/axmol.git
Merge pull request #12901 from Mazyod/mac-editbox-cleanup
Mac EditBox cleanup:
This commit is contained in:
commit
c4fa3ac6c6
|
@ -30,38 +30,9 @@
|
||||||
|
|
||||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
||||||
|
|
||||||
#import <Foundation/Foundation.h>
|
|
||||||
#import <AppKit/AppKit.h>
|
|
||||||
|
|
||||||
#include "UIEditBoxImpl.h"
|
#include "UIEditBoxImpl.h"
|
||||||
|
|
||||||
|
@class UIEditBoxImplMac;
|
||||||
@interface UIEditBoxImplMac : NSObject <NSTextFieldDelegate>
|
|
||||||
{
|
|
||||||
NSTextField* textField_;
|
|
||||||
NSSecureTextField* secureTextField_;
|
|
||||||
void* editBox_;
|
|
||||||
BOOL editState_;
|
|
||||||
NSMutableDictionary* placeholderAttributes_;
|
|
||||||
}
|
|
||||||
|
|
||||||
@property(nonatomic, retain) NSTextField* textField;
|
|
||||||
@property(nonatomic, retain) NSSecureTextField* secureTextField;
|
|
||||||
@property(nonatomic, retain) NSMutableDictionary* placeholderAttributes;
|
|
||||||
@property(nonatomic, readonly, getter = isEditState) BOOL editState;
|
|
||||||
@property(nonatomic, assign) void* editBox;
|
|
||||||
@property(nonatomic, assign, getter = isSecure) BOOL secure;
|
|
||||||
|
|
||||||
-(id) initWithFrame: (NSRect) frameRect editBox: (void*) editBox;
|
|
||||||
-(void) doAnimationWhenKeyboardMoveWithDuration:(float)duration distance:(float)distance;
|
|
||||||
-(void) setPosition:(NSPoint) pos;
|
|
||||||
-(void) setContentSize:(NSSize) size;
|
|
||||||
-(void) visit;
|
|
||||||
-(void) openKeyboard;
|
|
||||||
-(void) closeKeyboard;
|
|
||||||
|
|
||||||
@end
|
|
||||||
|
|
||||||
|
|
||||||
NS_CC_BEGIN
|
NS_CC_BEGIN
|
||||||
|
|
||||||
|
|
|
@ -31,42 +31,40 @@
|
||||||
#include "UIEditBox.h"
|
#include "UIEditBox.h"
|
||||||
#include "deprecated/CCString.h"
|
#include "deprecated/CCString.h"
|
||||||
|
|
||||||
|
#import <Foundation/Foundation.h>
|
||||||
|
#import <AppKit/AppKit.h>
|
||||||
|
|
||||||
#define getEditBoxImplMac() ((cocos2d::ui::EditBoxImplMac*)editBox_)
|
#define getEditBoxImplMac() ((cocos2d::ui::EditBoxImplMac *)_editBox)
|
||||||
|
|
||||||
@interface CustomTextFieldFormatter : NSFormatter {
|
#pragma mark - internal classes
|
||||||
int maxLength;
|
|
||||||
}
|
/** TODO: Missing doc - What does "CustomTextFieldFormatter" do?
|
||||||
- (void)setMaximumLength:(int)len;
|
*/
|
||||||
- (int)maximumLength;
|
@interface CustomTextFieldFormatter : NSFormatter
|
||||||
|
|
||||||
|
@property (nonatomic, assign) int maximumLength;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation CustomTextFieldFormatter
|
@implementation CustomTextFieldFormatter
|
||||||
|
|
||||||
- (id)init {
|
- (instancetype)init
|
||||||
|
{
|
||||||
if(self = [super init]){
|
self = [super init];
|
||||||
|
if (self) {
|
||||||
maxLength = INT_MAX;
|
_maximumLength = INT_MAX;
|
||||||
}
|
}
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setMaximumLength:(int)len {
|
- (NSString *)stringForObjectValue:(id)object
|
||||||
maxLength = len;
|
{
|
||||||
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (int)maximumLength {
|
- (BOOL)getObjectValue:(id *)object forString:(NSString *)string errorDescription:(NSString **)error
|
||||||
return maxLength;
|
{
|
||||||
}
|
|
||||||
|
|
||||||
- (NSString *)stringForObjectValue:(id)object {
|
|
||||||
return (NSString *)object;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (BOOL)getObjectValue:(id *)object forString:(NSString *)string errorDescription:(NSString **)error {
|
|
||||||
*object = string;
|
*object = string;
|
||||||
return YES;
|
return YES;
|
||||||
}
|
}
|
||||||
|
@ -75,68 +73,63 @@
|
||||||
proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
|
proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
|
||||||
originalString:(NSString *)origString
|
originalString:(NSString *)origString
|
||||||
originalSelectedRange:(NSRange)origSelRange
|
originalSelectedRange:(NSRange)origSelRange
|
||||||
errorDescription:(NSString **)error {
|
errorDescription:(NSString **)error
|
||||||
if ([*partialStringPtr length] > maxLength) {
|
{
|
||||||
return NO;
|
return (*partialStringPtr).length <= self.maximumLength;
|
||||||
}
|
|
||||||
|
|
||||||
return YES;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSAttributedString *)attributedStringForObjectValue:(id)anObject withDefaultAttributes:(NSDictionary *)attributes {
|
- (NSAttributedString *)attributedStringForObjectValue:(id)anObject withDefaultAttributes:(NSDictionary *)attributes
|
||||||
|
{
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
#pragma mark - UIEditBox mac implementation
|
||||||
|
|
||||||
|
@interface UIEditBoxImplMac : NSObject <NSTextFieldDelegate>
|
||||||
|
|
||||||
|
@property (nonatomic, retain) NSTextField* textField;
|
||||||
|
@property (nonatomic, retain) NSSecureTextField *secureTextField;
|
||||||
|
@property (nonatomic, retain) NSMutableDictionary *placeholderAttributes;
|
||||||
|
@property (nonatomic, readonly) NSWindow *window;
|
||||||
|
|
||||||
|
@property (nonatomic, readonly, getter = isEditState) BOOL editState;
|
||||||
|
@property (nonatomic, assign, getter = isSecure) BOOL secure;
|
||||||
|
@property (nonatomic, assign) void *editBox;
|
||||||
|
|
||||||
|
- (instancetype)initWithFrame:(NSRect)frameRect editBox:(void *)editBox;
|
||||||
|
|
||||||
|
- (void)doAnimationWhenKeyboardMoveWithDuration:(float)duration distance:(float)distance;
|
||||||
|
- (void)setPosition:(NSPoint)pos;
|
||||||
|
|
||||||
|
- (void)openKeyboard;
|
||||||
|
- (void)closeKeyboard;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
|
||||||
@implementation UIEditBoxImplMac
|
@implementation UIEditBoxImplMac
|
||||||
|
|
||||||
@synthesize textField = textField_;
|
- (instancetype)initWithFrame:(NSRect)frameRect editBox:(void *)editBox
|
||||||
@synthesize secureTextField = secureTextField_;
|
|
||||||
@synthesize placeholderAttributes = placeholderAttributes_;
|
|
||||||
@synthesize editState = editState_;
|
|
||||||
@synthesize editBox = editBox_;
|
|
||||||
@synthesize secure = secure_;
|
|
||||||
|
|
||||||
- (id) getNSWindow
|
|
||||||
{
|
|
||||||
auto glview = cocos2d::Director::getInstance()->getOpenGLView();
|
|
||||||
return glview->getCocoaWindow();
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)dealloc
|
|
||||||
{
|
|
||||||
[textField_ resignFirstResponder];
|
|
||||||
[textField_ removeFromSuperview];
|
|
||||||
[textField_ release];
|
|
||||||
|
|
||||||
[secureTextField_ resignFirstResponder];
|
|
||||||
[secureTextField_ removeFromSuperview];
|
|
||||||
[secureTextField_ release];
|
|
||||||
|
|
||||||
[placeholderAttributes_ release];
|
|
||||||
[super dealloc];
|
|
||||||
}
|
|
||||||
|
|
||||||
-(id) initWithFrame: (NSRect) frameRect editBox: (void*) editBox
|
|
||||||
{
|
{
|
||||||
self = [super init];
|
self = [super init];
|
||||||
|
|
||||||
if (self)
|
if (self) {
|
||||||
{
|
|
||||||
editState_ = NO;
|
_editState = NO;
|
||||||
secure_ = NO;
|
_secure = NO;
|
||||||
|
|
||||||
self.textField = [[[NSTextField alloc] initWithFrame:frameRect] autorelease];
|
self.textField = [[[NSTextField alloc] initWithFrame:frameRect] autorelease];
|
||||||
self.secureTextField = [[[NSSecureTextField alloc] initWithFrame:frameRect] autorelease];
|
self.secureTextField = [[[NSSecureTextField alloc] initWithFrame:frameRect] autorelease];
|
||||||
|
|
||||||
//TODO: need to delete hard code here.
|
//TODO: need to delete hard code here.
|
||||||
NSFont *font = [NSFont systemFontOfSize:frameRect.size.height*2/3];
|
NSFont *font = [NSFont systemFontOfSize:frameRect.size.height*2/3];
|
||||||
textField_.font = font;
|
_textField.font = font;
|
||||||
secureTextField_.font = font;
|
_secureTextField.font = font;
|
||||||
|
|
||||||
[self setupTextField:textField_];
|
[self setupTextField:_textField];
|
||||||
[self setupTextField:secureTextField_];
|
[self setupTextField:_secureTextField];
|
||||||
|
|
||||||
self.editBox = editBox;
|
self.editBox = editBox;
|
||||||
self.placeholderAttributes = [NSMutableDictionary dictionaryWithObjectsAndKeys:
|
self.placeholderAttributes = [NSMutableDictionary dictionaryWithObjectsAndKeys:
|
||||||
|
@ -144,96 +137,104 @@
|
||||||
[NSColor grayColor], NSForegroundColorAttributeName,
|
[NSColor grayColor], NSForegroundColorAttributeName,
|
||||||
nil];
|
nil];
|
||||||
|
|
||||||
[[[self getNSWindow] contentView] addSubview:textField_];
|
[self.window.contentView addSubview:_textField];
|
||||||
}
|
}
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)dealloc
|
||||||
|
{
|
||||||
|
[_textField resignFirstResponder];
|
||||||
|
[_textField removeFromSuperview];
|
||||||
|
[_textField release];
|
||||||
|
|
||||||
|
[_secureTextField resignFirstResponder];
|
||||||
|
[_secureTextField removeFromSuperview];
|
||||||
|
[_secureTextField release];
|
||||||
|
|
||||||
|
[_placeholderAttributes release];
|
||||||
|
|
||||||
|
[super dealloc];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSWindow *)window
|
||||||
|
{
|
||||||
|
auto glview = cocos2d::Director::getInstance()->getOpenGLView();
|
||||||
|
return glview->getCocoaWindow();
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)setPosition:(NSPoint)pos
|
||||||
|
{
|
||||||
|
NSRect frame = _textField.frame;
|
||||||
|
frame.origin = pos;
|
||||||
|
|
||||||
|
_textField.frame = frame;
|
||||||
|
_secureTextField.frame = frame;
|
||||||
|
}
|
||||||
|
|
||||||
- (void)setupTextField:(NSTextField *)textField
|
- (void)setupTextField:(NSTextField *)textField
|
||||||
{
|
{
|
||||||
[textField setTextColor:[NSColor whiteColor]];
|
textField.textColor = [NSColor whiteColor];
|
||||||
[textField setBackgroundColor:[NSColor clearColor]];
|
textField.backgroundColor = [NSColor clearColor];
|
||||||
[textField setBordered:NO];
|
textField.bordered = NO;
|
||||||
[textField setHidden:NO];
|
textField.hidden = NO;
|
||||||
[textField setWantsLayer:YES];
|
textField.wantsLayer = YES;
|
||||||
[textField setDelegate:self];
|
textField.delegate = self;
|
||||||
}
|
}
|
||||||
|
|
||||||
-(void) doAnimationWhenKeyboardMoveWithDuration:(float)duration distance:(float)distance
|
- (void)doAnimationWhenKeyboardMoveWithDuration:(float)duration distance:(float)distance
|
||||||
{
|
{
|
||||||
[[[self getNSWindow] contentView] doAnimationWhenKeyboardMoveWithDuration:duration distance:distance];
|
[self.window.contentView doAnimationWhenKeyboardMoveWithDuration:duration distance:distance];
|
||||||
}
|
}
|
||||||
|
|
||||||
-(void) setSecure:(BOOL)secure
|
- (void)setSecure:(BOOL)secure
|
||||||
{
|
{
|
||||||
NSAssert(secure, @"Can only set this flag to true");
|
NSAssert(secure, @"Can only set this flag to true");
|
||||||
|
|
||||||
secure_ = secure;
|
_secure = secure;
|
||||||
|
|
||||||
[textField_.superview addSubview:secureTextField_];
|
[_textField.superview addSubview:_secureTextField];
|
||||||
[textField_ removeFromSuperview];
|
[_textField removeFromSuperview];
|
||||||
}
|
}
|
||||||
|
|
||||||
-(void) setPosition:(NSPoint) pos
|
- (void)openKeyboard
|
||||||
{
|
|
||||||
NSRect frame = [textField_ frame];
|
|
||||||
frame.origin = pos;
|
|
||||||
[textField_ setFrame:frame];
|
|
||||||
[secureTextField_ setFrame:frame];
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) setContentSize:(NSSize) size
|
|
||||||
{
|
{
|
||||||
|
NSView *contentView = self.window.contentView;
|
||||||
|
|
||||||
}
|
if (!_secure) {
|
||||||
|
[contentView addSubview:_textField];
|
||||||
-(void) visit
|
[_textField becomeFirstResponder];
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) openKeyboard
|
|
||||||
{
|
|
||||||
NSView *contentView = [[self getNSWindow] contentView];
|
|
||||||
|
|
||||||
if (!secure_) {
|
|
||||||
[contentView addSubview:textField_];
|
|
||||||
[textField_ becomeFirstResponder];
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
[contentView addSubview:secureTextField_];
|
[contentView addSubview:_secureTextField];
|
||||||
[secureTextField_ becomeFirstResponder];
|
[_secureTextField becomeFirstResponder];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
-(void) closeKeyboard
|
- (void)closeKeyboard
|
||||||
{
|
{
|
||||||
if ([textField_ superview]) {
|
if ([_textField superview]) {
|
||||||
[textField_ resignFirstResponder];
|
[_textField resignFirstResponder];
|
||||||
[textField_ removeFromSuperview];
|
[_textField removeFromSuperview];
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
[secureTextField_ resignFirstResponder];
|
[_secureTextField resignFirstResponder];
|
||||||
[secureTextField_ removeFromSuperview];
|
[_secureTextField removeFromSuperview];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (BOOL)textFieldShouldReturn:(NSTextField *)sender
|
- (BOOL)textFieldShouldReturn:(NSTextField *)sender
|
||||||
{
|
{
|
||||||
if (sender == textField_ || sender == secureTextField_) {
|
if (sender == _textField || sender == _secureTextField) {
|
||||||
[sender resignFirstResponder];
|
[sender resignFirstResponder];
|
||||||
}
|
}
|
||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
|
|
||||||
-(void)animationSelector
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)controlTextDidBeginEditing:(NSNotification *)notification
|
- (void)controlTextDidBeginEditing:(NSNotification *)notification
|
||||||
{
|
{
|
||||||
editState_ = YES;
|
_editState = YES;
|
||||||
cocos2d::ui::EditBoxDelegate* pDelegate = getEditBoxImplMac()->getDelegate();
|
cocos2d::ui::EditBoxDelegate* pDelegate = getEditBoxImplMac()->getDelegate();
|
||||||
if (pDelegate != NULL)
|
if (pDelegate != NULL)
|
||||||
{
|
{
|
||||||
|
@ -253,7 +254,7 @@
|
||||||
|
|
||||||
- (void)controlTextDidEndEditing:(NSNotification *)notification
|
- (void)controlTextDidEndEditing:(NSNotification *)notification
|
||||||
{
|
{
|
||||||
editState_ = NO;
|
_editState = NO;
|
||||||
cocos2d::ui::EditBoxDelegate* pDelegate = getEditBoxImplMac()->getDelegate();
|
cocos2d::ui::EditBoxDelegate* pDelegate = getEditBoxImplMac()->getDelegate();
|
||||||
if (pDelegate != NULL)
|
if (pDelegate != NULL)
|
||||||
{
|
{
|
||||||
|
@ -301,6 +302,7 @@
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|
||||||
NS_CC_BEGIN
|
NS_CC_BEGIN
|
||||||
|
|
||||||
namespace ui {
|
namespace ui {
|
||||||
|
@ -353,13 +355,13 @@ bool EditBoxImplMac::initWithSize(const Size& size)
|
||||||
|
|
||||||
void EditBoxImplMac::setFont(const char* pFontName, int fontSize)
|
void EditBoxImplMac::setFont(const char* pFontName, int fontSize)
|
||||||
{
|
{
|
||||||
NSString * fntName = [NSString stringWithUTF8String:pFontName];
|
NSString * fntName = [NSString stringWithUTF8String:pFontName];
|
||||||
float retinaFactor = _inRetinaMode ? 2.0f : 1.0f;
|
float retinaFactor = _inRetinaMode ? 2.0f : 1.0f;
|
||||||
auto glview = cocos2d::Director::getInstance()->getOpenGLView();
|
auto glview = cocos2d::Director::getInstance()->getOpenGLView();
|
||||||
float scaleFactor = glview->getScaleX();
|
float scaleFactor = glview->getScaleX();
|
||||||
NSFont *textFont = [NSFont fontWithName:fntName size:fontSize * scaleFactor / retinaFactor];
|
NSFont *textFont = [NSFont fontWithName:fntName size:fontSize * scaleFactor / retinaFactor];
|
||||||
if (textFont != nil) {
|
if (textFont != nil) {
|
||||||
[_sysEdit.textField setFont:textFont];
|
[_sysEdit.textField setFont:textFont];
|
||||||
[_sysEdit.secureTextField setFont:textFont];
|
[_sysEdit.secureTextField setFont:textFont];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -514,12 +516,12 @@ void EditBoxImplMac::updatePosition(float dt)
|
||||||
|
|
||||||
void EditBoxImplMac::adjustTextFieldPosition()
|
void EditBoxImplMac::adjustTextFieldPosition()
|
||||||
{
|
{
|
||||||
Size contentSize = _editBox->getContentSize();
|
Size contentSize = _editBox->getContentSize();
|
||||||
Rect rect = Rect(0, 0, contentSize.width, contentSize.height);
|
Rect rect = Rect(0, 0, contentSize.width, contentSize.height);
|
||||||
|
|
||||||
rect = RectApplyAffineTransform(rect, _editBox->nodeToWorldTransform());
|
rect = RectApplyAffineTransform(rect, _editBox->nodeToWorldTransform());
|
||||||
|
|
||||||
Vec2 designCoord = Vec2(rect.origin.x, rect.origin.y + rect.size.height);
|
Vec2 designCoord = Vec2(rect.origin.x, rect.origin.y + rect.size.height);
|
||||||
[_sysEdit setPosition:convertDesignCoordToScreenCoord(designCoord, _inRetinaMode)];
|
[_sysEdit setPosition:convertDesignCoordToScreenCoord(designCoord, _inRetinaMode)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -544,8 +546,8 @@ void EditBoxImplMac::setContentSize(const Size& size)
|
||||||
void EditBoxImplMac::setAnchorPoint(const Vec2& anchorPoint)
|
void EditBoxImplMac::setAnchorPoint(const Vec2& anchorPoint)
|
||||||
{
|
{
|
||||||
CCLOG("[Edit text] anchor point = (%f, %f)", anchorPoint.x, anchorPoint.y);
|
CCLOG("[Edit text] anchor point = (%f, %f)", anchorPoint.x, anchorPoint.y);
|
||||||
_anchorPoint = anchorPoint;
|
_anchorPoint = anchorPoint;
|
||||||
setPosition(_position);
|
setPosition(_position);
|
||||||
}
|
}
|
||||||
|
|
||||||
void EditBoxImplMac::visit(void)
|
void EditBoxImplMac::visit(void)
|
||||||
|
|
Loading…
Reference in New Issue