mirror of https://github.com/axmolengine/axmol.git
Mac EditBox cleanup:
+ Reduce header clutter + consistent property access and overall code style + cleanup extra code
This commit is contained in:
parent
22ef262295
commit
d8b7b1d565
|
@ -35,33 +35,7 @@
|
|||
|
||||
#include "UIEditBoxImpl.h"
|
||||
|
||||
|
||||
@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
|
||||
|
||||
@class UIEditBoxImplMac;
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
|
|
|
@ -32,41 +32,37 @@
|
|||
#include "deprecated/CCString.h"
|
||||
|
||||
|
||||
#define getEditBoxImplMac() ((cocos2d::ui::EditBoxImplMac*)editBox_)
|
||||
#define getEditBoxImplMac() ((cocos2d::ui::EditBoxImplMac *)_editBox)
|
||||
|
||||
@interface CustomTextFieldFormatter : NSFormatter {
|
||||
int maxLength;
|
||||
}
|
||||
- (void)setMaximumLength:(int)len;
|
||||
- (int)maximumLength;
|
||||
#pragma mark - internal classes
|
||||
|
||||
/** TODO: Missing doc - What does "CustomTextFieldFormatter" do?
|
||||
*/
|
||||
@interface CustomTextFieldFormatter : NSFormatter
|
||||
|
||||
@property (nonatomic, assign) int maximumLength;
|
||||
|
||||
@end
|
||||
|
||||
@implementation CustomTextFieldFormatter
|
||||
|
||||
- (id)init {
|
||||
|
||||
if(self = [super init]){
|
||||
|
||||
maxLength = INT_MAX;
|
||||
- (instancetype)init
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_maximumLength = INT_MAX;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)setMaximumLength:(int)len {
|
||||
maxLength = len;
|
||||
- (NSString *)stringForObjectValue:(id)object
|
||||
{
|
||||
return object;
|
||||
}
|
||||
|
||||
- (int)maximumLength {
|
||||
return maxLength;
|
||||
}
|
||||
|
||||
- (NSString *)stringForObjectValue:(id)object {
|
||||
return (NSString *)object;
|
||||
}
|
||||
|
||||
- (BOOL)getObjectValue:(id *)object forString:(NSString *)string errorDescription:(NSString **)error {
|
||||
- (BOOL)getObjectValue:(id *)object forString:(NSString *)string errorDescription:(NSString **)error
|
||||
{
|
||||
*object = string;
|
||||
return YES;
|
||||
}
|
||||
|
@ -75,68 +71,63 @@
|
|||
proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
|
||||
originalString:(NSString *)origString
|
||||
originalSelectedRange:(NSRange)origSelRange
|
||||
errorDescription:(NSString **)error {
|
||||
if ([*partialStringPtr length] > maxLength) {
|
||||
return NO;
|
||||
}
|
||||
|
||||
return YES;
|
||||
errorDescription:(NSString **)error
|
||||
{
|
||||
return (*partialStringPtr).length <= self.maximumLength;
|
||||
}
|
||||
|
||||
- (NSAttributedString *)attributedStringForObjectValue:(id)anObject withDefaultAttributes:(NSDictionary *)attributes {
|
||||
- (NSAttributedString *)attributedStringForObjectValue:(id)anObject withDefaultAttributes:(NSDictionary *)attributes
|
||||
{
|
||||
return nil;
|
||||
}
|
||||
|
||||
@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
|
||||
|
||||
@synthesize textField = textField_;
|
||||
@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
|
||||
- (instancetype)initWithFrame:(NSRect)frameRect editBox:(void *)editBox
|
||||
{
|
||||
self = [super init];
|
||||
|
||||
if (self)
|
||||
{
|
||||
editState_ = NO;
|
||||
secure_ = NO;
|
||||
if (self) {
|
||||
|
||||
_editState = NO;
|
||||
_secure = NO;
|
||||
|
||||
self.textField = [[[NSTextField alloc] initWithFrame:frameRect] autorelease];
|
||||
self.secureTextField = [[[NSSecureTextField alloc] initWithFrame:frameRect] autorelease];
|
||||
|
||||
//TODO: need to delete hard code here.
|
||||
NSFont *font = [NSFont systemFontOfSize:frameRect.size.height*2/3];
|
||||
textField_.font = font;
|
||||
secureTextField_.font = font;
|
||||
_textField.font = font;
|
||||
_secureTextField.font = font;
|
||||
|
||||
[self setupTextField:textField_];
|
||||
[self setupTextField:secureTextField_];
|
||||
[self setupTextField:_textField];
|
||||
[self setupTextField:_secureTextField];
|
||||
|
||||
self.editBox = editBox;
|
||||
self.placeholderAttributes = [NSMutableDictionary dictionaryWithObjectsAndKeys:
|
||||
|
@ -144,96 +135,104 @@
|
|||
[NSColor grayColor], NSForegroundColorAttributeName,
|
||||
nil];
|
||||
|
||||
[[[self getNSWindow] contentView] addSubview:textField_];
|
||||
[self.window.contentView addSubview:_textField];
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
[textField setTextColor:[NSColor whiteColor]];
|
||||
[textField setBackgroundColor:[NSColor clearColor]];
|
||||
[textField setBordered:NO];
|
||||
[textField setHidden:NO];
|
||||
[textField setWantsLayer:YES];
|
||||
[textField setDelegate:self];
|
||||
textField.textColor = [NSColor whiteColor];
|
||||
textField.backgroundColor = [NSColor clearColor];
|
||||
textField.bordered = NO;
|
||||
textField.hidden = NO;
|
||||
textField.wantsLayer = YES;
|
||||
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");
|
||||
|
||||
secure_ = secure;
|
||||
_secure = secure;
|
||||
|
||||
[textField_.superview addSubview:secureTextField_];
|
||||
[textField_ removeFromSuperview];
|
||||
[_textField.superview addSubview:_secureTextField];
|
||||
[_textField removeFromSuperview];
|
||||
}
|
||||
|
||||
-(void) setPosition:(NSPoint) pos
|
||||
{
|
||||
NSRect frame = [textField_ frame];
|
||||
frame.origin = pos;
|
||||
[textField_ setFrame:frame];
|
||||
[secureTextField_ setFrame:frame];
|
||||
}
|
||||
|
||||
-(void) setContentSize:(NSSize) size
|
||||
- (void)openKeyboard
|
||||
{
|
||||
NSView *contentView = [self getNSWindow].contentView;
|
||||
|
||||
}
|
||||
|
||||
-(void) visit
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
-(void) openKeyboard
|
||||
{
|
||||
NSView *contentView = [[self getNSWindow] contentView];
|
||||
|
||||
if (!secure_) {
|
||||
[contentView addSubview:textField_];
|
||||
[textField_ becomeFirstResponder];
|
||||
if (!_secure) {
|
||||
[contentView addSubview:_textField];
|
||||
[_textField becomeFirstResponder];
|
||||
}
|
||||
else {
|
||||
[contentView addSubview:secureTextField_];
|
||||
[secureTextField_ becomeFirstResponder];
|
||||
[contentView addSubview:_secureTextField];
|
||||
[_secureTextField becomeFirstResponder];
|
||||
}
|
||||
}
|
||||
|
||||
-(void) closeKeyboard
|
||||
- (void)closeKeyboard
|
||||
{
|
||||
if ([textField_ superview]) {
|
||||
[textField_ resignFirstResponder];
|
||||
[textField_ removeFromSuperview];
|
||||
if ([_textField superview]) {
|
||||
[_textField resignFirstResponder];
|
||||
[_textField removeFromSuperview];
|
||||
}
|
||||
else {
|
||||
[secureTextField_ resignFirstResponder];
|
||||
[secureTextField_ removeFromSuperview];
|
||||
[_secureTextField resignFirstResponder];
|
||||
[_secureTextField removeFromSuperview];
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)textFieldShouldReturn:(NSTextField *)sender
|
||||
{
|
||||
if (sender == textField_ || sender == secureTextField_) {
|
||||
if (sender == _textField || sender == _secureTextField) {
|
||||
[sender resignFirstResponder];
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
-(void)animationSelector
|
||||
{
|
||||
}
|
||||
|
||||
- (void)controlTextDidBeginEditing:(NSNotification *)notification
|
||||
{
|
||||
editState_ = YES;
|
||||
_editState = YES;
|
||||
cocos2d::ui::EditBoxDelegate* pDelegate = getEditBoxImplMac()->getDelegate();
|
||||
if (pDelegate != NULL)
|
||||
{
|
||||
|
@ -253,7 +252,7 @@
|
|||
|
||||
- (void)controlTextDidEndEditing:(NSNotification *)notification
|
||||
{
|
||||
editState_ = NO;
|
||||
_editState = NO;
|
||||
cocos2d::ui::EditBoxDelegate* pDelegate = getEditBoxImplMac()->getDelegate();
|
||||
if (pDelegate != NULL)
|
||||
{
|
||||
|
@ -301,6 +300,7 @@
|
|||
|
||||
@end
|
||||
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
namespace ui {
|
||||
|
|
Loading…
Reference in New Issue