diff --git a/build/cocos2d_libs.xcodeproj/project.pbxproj b/build/cocos2d_libs.xcodeproj/project.pbxproj index c7f37a1699..98954f5229 100644 --- a/build/cocos2d_libs.xcodeproj/project.pbxproj +++ b/build/cocos2d_libs.xcodeproj/project.pbxproj @@ -1219,6 +1219,17 @@ 29394CF519B01DBA00D2DE1A /* UIWebViewImpl-ios.h in Headers */ = {isa = PBXBuildFile; fileRef = 29394CEE19B01DBA00D2DE1A /* UIWebViewImpl-ios.h */; }; 29394CF619B01DBA00D2DE1A /* UIWebViewImpl-ios.mm in Sources */ = {isa = PBXBuildFile; fileRef = 29394CEF19B01DBA00D2DE1A /* UIWebViewImpl-ios.mm */; }; 29394CF719B01DBA00D2DE1A /* UIWebViewImpl-ios.mm in Sources */ = {isa = PBXBuildFile; fileRef = 29394CEF19B01DBA00D2DE1A /* UIWebViewImpl-ios.mm */; }; + 2980F0221BA9A5550059E678 /* CCUIEditBoxIOS.h in Headers */ = {isa = PBXBuildFile; fileRef = 2980F0171BA9A5550059E678 /* CCUIEditBoxIOS.h */; settings = {ASSET_TAGS = (); }; }; + 2980F0231BA9A5550059E678 /* CCUIEditBoxIOS.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2980F0181BA9A5550059E678 /* CCUIEditBoxIOS.mm */; settings = {ASSET_TAGS = (); }; }; + 2980F0241BA9A5550059E678 /* CCUIMultilineTextField.h in Headers */ = {isa = PBXBuildFile; fileRef = 2980F0191BA9A5550059E678 /* CCUIMultilineTextField.h */; settings = {ASSET_TAGS = (); }; }; + 2980F0251BA9A5550059E678 /* CCUIMultilineTextField.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2980F01A1BA9A5550059E678 /* CCUIMultilineTextField.mm */; settings = {ASSET_TAGS = (); }; }; + 2980F0261BA9A5550059E678 /* CCUISingleLineTextField.h in Headers */ = {isa = PBXBuildFile; fileRef = 2980F01B1BA9A5550059E678 /* CCUISingleLineTextField.h */; settings = {ASSET_TAGS = (); }; }; + 2980F0271BA9A5550059E678 /* CCUISingleLineTextField.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2980F01C1BA9A5550059E678 /* CCUISingleLineTextField.mm */; settings = {ASSET_TAGS = (); }; }; + 2980F0281BA9A5550059E678 /* CCUITextInput.h in Headers */ = {isa = PBXBuildFile; fileRef = 2980F01D1BA9A5550059E678 /* CCUITextInput.h */; settings = {ASSET_TAGS = (); }; }; + 2980F0291BA9A5550059E678 /* UITextField+CCUITextInput.h in Headers */ = {isa = PBXBuildFile; fileRef = 2980F01E1BA9A5550059E678 /* UITextField+CCUITextInput.h */; settings = {ASSET_TAGS = (); }; }; + 2980F02A1BA9A5550059E678 /* UITextField+CCUITextInput.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2980F01F1BA9A5550059E678 /* UITextField+CCUITextInput.mm */; settings = {ASSET_TAGS = (); }; }; + 2980F02B1BA9A5550059E678 /* UITextView+CCUITextInput.h in Headers */ = {isa = PBXBuildFile; fileRef = 2980F0201BA9A5550059E678 /* UITextView+CCUITextInput.h */; settings = {ASSET_TAGS = (); }; }; + 2980F02C1BA9A5550059E678 /* UITextView+CCUITextInput.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2980F0211BA9A5550059E678 /* UITextView+CCUITextInput.mm */; settings = {ASSET_TAGS = (); }; }; 2986667F18B1B246000E39CA /* CCTweenFunction.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2986667818B1B079000E39CA /* CCTweenFunction.cpp */; }; 299754F4193EC95400A54AC3 /* ObjectFactory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 299754F2193EC95400A54AC3 /* ObjectFactory.cpp */; }; 299754F5193EC95400A54AC3 /* ObjectFactory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 299754F2193EC95400A54AC3 /* ObjectFactory.cpp */; }; @@ -4118,6 +4129,17 @@ 29394CEF19B01DBA00D2DE1A /* UIWebViewImpl-ios.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = "UIWebViewImpl-ios.mm"; sourceTree = ""; }; 2958244919873D8E00F9746D /* UIScale9Sprite.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UIScale9Sprite.cpp; sourceTree = ""; }; 2958244A19873D8E00F9746D /* UIScale9Sprite.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIScale9Sprite.h; sourceTree = ""; }; + 2980F0171BA9A5550059E678 /* CCUIEditBoxIOS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCUIEditBoxIOS.h; sourceTree = ""; }; + 2980F0181BA9A5550059E678 /* CCUIEditBoxIOS.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CCUIEditBoxIOS.mm; sourceTree = ""; }; + 2980F0191BA9A5550059E678 /* CCUIMultilineTextField.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCUIMultilineTextField.h; sourceTree = ""; }; + 2980F01A1BA9A5550059E678 /* CCUIMultilineTextField.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CCUIMultilineTextField.mm; sourceTree = ""; }; + 2980F01B1BA9A5550059E678 /* CCUISingleLineTextField.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCUISingleLineTextField.h; sourceTree = ""; }; + 2980F01C1BA9A5550059E678 /* CCUISingleLineTextField.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CCUISingleLineTextField.mm; sourceTree = ""; }; + 2980F01D1BA9A5550059E678 /* CCUITextInput.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCUITextInput.h; sourceTree = ""; }; + 2980F01E1BA9A5550059E678 /* UITextField+CCUITextInput.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UITextField+CCUITextInput.h"; sourceTree = ""; }; + 2980F01F1BA9A5550059E678 /* UITextField+CCUITextInput.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = "UITextField+CCUITextInput.mm"; sourceTree = ""; }; + 2980F0201BA9A5550059E678 /* UITextView+CCUITextInput.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UITextView+CCUITextInput.h"; sourceTree = ""; }; + 2980F0211BA9A5550059E678 /* UITextView+CCUITextInput.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = "UITextView+CCUITextInput.mm"; sourceTree = ""; }; 2986667818B1B079000E39CA /* CCTweenFunction.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCTweenFunction.cpp; sourceTree = ""; }; 2986667918B1B079000E39CA /* CCTweenFunction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCTweenFunction.h; sourceTree = ""; }; 299754F2193EC95400A54AC3 /* ObjectFactory.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ObjectFactory.cpp; path = ../base/ObjectFactory.cpp; sourceTree = ""; }; @@ -6838,6 +6860,7 @@ 292DB12E19B4574100A80320 /* UIEditBox */ = { isa = PBXGroup; children = ( + 2980F0161BA9A5550059E678 /* iOS */, A0E749F51BA8FD7F001A8332 /* UIEditBoxImpl-common.cpp */, A0E749F61BA8FD7F001A8332 /* UIEditBoxImpl-common.h */, 50ED2BDC19BEAF7900A0AB90 /* UIEditBoxImpl-win32.h */, @@ -6866,6 +6889,24 @@ name = mac; sourceTree = ""; }; + 2980F0161BA9A5550059E678 /* iOS */ = { + isa = PBXGroup; + children = ( + 2980F0171BA9A5550059E678 /* CCUIEditBoxIOS.h */, + 2980F0181BA9A5550059E678 /* CCUIEditBoxIOS.mm */, + 2980F0191BA9A5550059E678 /* CCUIMultilineTextField.h */, + 2980F01A1BA9A5550059E678 /* CCUIMultilineTextField.mm */, + 2980F01B1BA9A5550059E678 /* CCUISingleLineTextField.h */, + 2980F01C1BA9A5550059E678 /* CCUISingleLineTextField.mm */, + 2980F01D1BA9A5550059E678 /* CCUITextInput.h */, + 2980F01E1BA9A5550059E678 /* UITextField+CCUITextInput.h */, + 2980F01F1BA9A5550059E678 /* UITextField+CCUITextInput.mm */, + 2980F0201BA9A5550059E678 /* UITextView+CCUITextInput.h */, + 2980F0211BA9A5550059E678 /* UITextView+CCUITextInput.mm */, + ); + path = iOS; + sourceTree = ""; + }; 29CB8F501929D63600C841D6 /* layout */ = { isa = PBXGroup; children = ( @@ -10007,8 +10048,10 @@ files = ( 503DD8F01926736A00CD74DD /* CCStdC-ios.h in Headers */, 15AE1A9A19AAD40300C27E9E /* b2Math.h in Headers */, + 2980F0281BA9A5550059E678 /* CCUITextInput.h in Headers */, B6CAB3221AF9AA1A00B9B856 /* btGenericPoolAllocator.h in Headers */, B665E3851AA80A6500DDB1C5 /* CCPUPathFollower.h in Headers */, + 2980F0291BA9A5550059E678 /* UITextField+CCUITextInput.h in Headers */, 46A170FF1807CECB005B8026 /* CCPhysicsContact.h in Headers */, B6CAB37E1AF9AA1A00B9B856 /* btGjkPairDetector.h in Headers */, B6CAB3101AF9AA1A00B9B856 /* btTriangleShape.h in Headers */, @@ -10113,6 +10156,7 @@ 15AE19B919AAD39700C27E9E /* TextFieldReader.h in Headers */, 15AE181319AAD2F700C27E9E /* CCAnimation3D.h in Headers */, 50ABBEC21925AB6F00A911A9 /* CCValue.h in Headers */, + 2980F0241BA9A5550059E678 /* CCUIMultilineTextField.h in Headers */, B6CAB4FE1AF9AA1A00B9B856 /* btConvexHull.h in Headers */, 50ABBECA1925AB6F00A911A9 /* firePngData.h in Headers */, B257B4511989D5E800D9A687 /* CCPrimitive.h in Headers */, @@ -10538,6 +10582,7 @@ 1A570230180BCC1A0088DEC7 /* CCParticleSystemQuad.h in Headers */, 382383F31A258FA7002C4610 /* idl.h in Headers */, 29394CF119B01DBA00D2DE1A /* UIWebView.h in Headers */, + 2980F0261BA9A5550059E678 /* CCUISingleLineTextField.h in Headers */, B6CAB4EC1AF9AA1A00B9B856 /* TrbStateVec.h in Headers */, 15AE18B419AAD33D00C27E9E /* CCBSelectorResolver.h in Headers */, B24AA988195A675C007B4522 /* CCFastTMXLayer.h in Headers */, @@ -10568,6 +10613,7 @@ B6CAB20E1AF9AA1A00B9B856 /* btQuantizedBvh.h in Headers */, B665E2591AA80A6500DDB1C5 /* CCPUDoAffectorEventHandlerTranslator.h in Headers */, B665E1F91AA80A6500DDB1C5 /* CCPUAffectorManager.h in Headers */, + 2980F0221BA9A5550059E678 /* CCUIEditBoxIOS.h in Headers */, B665E2351AA80A6500DDB1C5 /* CCPUBoxEmitter.h in Headers */, B6CAB36C1AF9AA1A00B9B856 /* btConvexPenetrationDepthSolver.h in Headers */, B6CAB4281AF9AA1A00B9B856 /* btSolveProjectedGaussSeidel.h in Headers */, @@ -10614,6 +10660,7 @@ 15AE18D119AAD33D00C27E9E /* CCNodeLoaderLibrary.h in Headers */, 15AE1AC319AAD40300C27E9E /* b2DistanceJoint.h in Headers */, B6CAB5261AF9AA1A00B9B856 /* btQuickprof.h in Headers */, + 2980F02B1BA9A5550059E678 /* UITextView+CCUITextInput.h in Headers */, 50ABBE741925AB6F00A911A9 /* CCEventListenerMouse.h in Headers */, D0FD03581A3B51AA00825BB5 /* CCAllocatorMutex.h in Headers */, B29A7E1219EE1B7700872B35 /* EventData.h in Headers */, @@ -12313,6 +12360,7 @@ B29A7E2E19EE1B7700872B35 /* Slot.c in Sources */, B665E2471AA80A6500DDB1C5 /* CCPUCollisionAvoidanceAffectorTranslator.cpp in Sources */, 50ABC01E1926664800A911A9 /* CCThread.cpp in Sources */, + 2980F0271BA9A5550059E678 /* CCUISingleLineTextField.mm in Sources */, B29A7DF419EE1B7700872B35 /* AttachmentLoader.c in Sources */, 1A5701EB180BCB8C0088DEC7 /* CCTransitionPageTurn.cpp in Sources */, 15AE1A9D19AAD40300C27E9E /* b2StackAllocator.cpp in Sources */, @@ -12413,6 +12461,7 @@ 1A570287180BCC900088DEC7 /* CCSpriteFrame.cpp in Sources */, 507003221B69735300E83DDD /* HttpConnection-winrt.cpp in Sources */, B6CAB3AA1AF9AA1A00B9B856 /* btContactConstraint.cpp in Sources */, + 2980F02A1BA9A5550059E678 /* UITextField+CCUITextInput.mm in Sources */, B665E2431AA80A6500DDB1C5 /* CCPUCollisionAvoidanceAffector.cpp in Sources */, B6CAB23E1AF9AA1A00B9B856 /* btConvex2dConvex2dAlgorithm.cpp in Sources */, B665E3EB1AA80A6600DDB1C5 /* CCPUSlaveBehaviour.cpp in Sources */, @@ -12527,6 +12576,7 @@ 50ABBD881925AB4100A911A9 /* CCCustomCommand.cpp in Sources */, B6CAB4301AF9AA1A00B9B856 /* btWheelInfo.cpp in Sources */, 15AE19B019AAD39700C27E9E /* ScrollViewReader.cpp in Sources */, + 2980F02C1BA9A5550059E678 /* UITextView+CCUITextInput.mm in Sources */, 85505F061B60E3B6003F2CD4 /* CCSkeletonNode.cpp in Sources */, 50ABBE941925AB6F00A911A9 /* CCProfiling.cpp in Sources */, 5012169B1AC473A3009A4BEA /* CCTechnique.cpp in Sources */, @@ -12640,6 +12690,7 @@ B665E3131AA80A6500DDB1C5 /* CCPUObserverManager.cpp in Sources */, 50CB248019D9C5A100687767 /* AudioPlayer.mm in Sources */, 50ABBE9A1925AB6F00A911A9 /* CCRef.cpp in Sources */, + 2980F0251BA9A5550059E678 /* CCUIMultilineTextField.mm in Sources */, 85B3743B1B204B9400C488D6 /* clipper.cpp in Sources */, 15AE18BF19AAD33D00C27E9E /* CCLabelTTFLoader.cpp in Sources */, B6CAB27E1AF9AA1A00B9B856 /* btBox2dShape.cpp in Sources */, @@ -12702,6 +12753,7 @@ 15AE183519AAD2F700C27E9E /* CCObjLoader.cpp in Sources */, D0FD034C1A3B51AA00825BB5 /* CCAllocatorDiagnostics.cpp in Sources */, B665E31F1AA80A6500DDB1C5 /* CCPUOnClearObserverTranslator.cpp in Sources */, + 2980F0231BA9A5550059E678 /* CCUIEditBoxIOS.mm in Sources */, B665E4331AA80A6600DDB1C5 /* CCPUVertexEmitter.cpp in Sources */, B677B0DA1B18492D006762CB /* CCNavMeshUtils.cpp in Sources */, B665E3C71AA80A6600DDB1C5 /* CCPUScaleVelocityAffector.cpp in Sources */, diff --git a/cocos/platform/ios/CCEAGLView-ios.mm b/cocos/platform/ios/CCEAGLView-ios.mm index 0e9438a75c..a8292d4087 100644 --- a/cocos/platform/ios/CCEAGLView-ios.mm +++ b/cocos/platform/ios/CCEAGLView-ios.mm @@ -378,7 +378,8 @@ Copyright (C) 2008 Apple Inc. All Rights Reserved. for(UIView* view in subviews) { if([view isKindOfClass:NSClassFromString(@"CCCustomUITextField")] || - [view isKindOfClass:NSClassFromString(@"UICustomUITextField")]) + [view isKindOfClass:NSClassFromString(@"UISingleLineTextField")] || + [view isKindOfClass:NSClassFromString(@"UIMultilineTextField")]) { if ([view isFirstResponder]) { diff --git a/cocos/ui/UIEditBox/UIEditBoxImpl-android.cpp b/cocos/ui/UIEditBox/UIEditBoxImpl-android.cpp index 3ef3f6ce09..471750190c 100644 --- a/cocos/ui/UIEditBox/UIEditBoxImpl-android.cpp +++ b/cocos/ui/UIEditBox/UIEditBoxImpl-android.cpp @@ -1,7 +1,8 @@ /**************************************************************************** Copyright (c) 2010-2012 cocos2d-x.org Copyright (c) 2012 James Chen - + Copyright (c) 2013-2015 zilongshanren + http://www.cocos2d-x.org Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/cocos/ui/UIEditBox/UIEditBoxImpl-common.cpp b/cocos/ui/UIEditBox/UIEditBoxImpl-common.cpp index 427b3122e0..69afca8d48 100644 --- a/cocos/ui/UIEditBox/UIEditBoxImpl-common.cpp +++ b/cocos/ui/UIEditBox/UIEditBoxImpl-common.cpp @@ -1,7 +1,8 @@ /**************************************************************************** Copyright (c) 2010-2012 cocos2d-x.org Copyright (c) 2012 James Chen - + Copyright (c) 2013-2015 zilongshanren + http://www.cocos2d-x.org Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/cocos/ui/UIEditBox/UIEditBoxImpl-common.h b/cocos/ui/UIEditBox/UIEditBoxImpl-common.h index ae32efa090..9fea60d3dc 100644 --- a/cocos/ui/UIEditBox/UIEditBoxImpl-common.h +++ b/cocos/ui/UIEditBox/UIEditBoxImpl-common.h @@ -1,7 +1,8 @@ /**************************************************************************** Copyright (c) 2010-2012 cocos2d-x.org Copyright (c) 2012 James Chen - + Copyright (c) 2013-2015 zilongshanren + http://www.cocos2d-x.org Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm index e042462b2e..8579fe8b76 100644 --- a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm +++ b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm @@ -1,6 +1,7 @@ /**************************************************************************** Copyright (c) 2010-2012 cocos2d-x.org Copyright (c) 2012 James Chen + Copyright (c) 2013-2015 zilongshanren http://www.cocos2d-x.org @@ -36,233 +37,10 @@ #import #import +#import "iOS/CCUIEditBoxIOS.h" + #define getEditBoxImplIOS() ((cocos2d::ui::EditBoxImplIOS *)_editBox) -static const int CC_EDIT_BOX_PADDING = 5; - -#pragma mark - Internal Classes - -/** TODO: Missing doc - Why is this subclass necessary? - */ -@interface UICustomUITextField : UITextField -@end - -@implementation UICustomUITextField - -- (CGRect)textRectForBounds:(CGRect)bounds -{ - auto glview = cocos2d::Director::getInstance()->getOpenGLView(); - - float padding = CC_EDIT_BOX_PADDING * glview->getScaleX() / glview->getContentScaleFactor(); - return CGRectInset(bounds, padding, padding); -} - -- (CGRect)editingRectForBounds:(CGRect)bounds -{ - return [self textRectForBounds:bounds]; -} - -@end - -#pragma mark - UIEditBox ios implementation - - -@interface UIEditBoxImplIOS_objc : NSObject - -@property (nonatomic, retain) UITextField *textField; -@property (nonatomic, assign) void *editBox; -@property (nonatomic, readonly, getter = isEditState) BOOL editState; - -- (instancetype)initWithFrame:(CGRect)frameRect editBox:(void *)editBox; -- (void)doAnimationWhenKeyboardMoveWithDuration:(float)duration distance:(float)distance; - -- (void)setPosition:(CGPoint)pos; -- (void)setContentSize:(CGSize)size; - -- (void)openKeyboard; -- (void)closeKeyboard; - -@end - -#pragma mark - UIEditBox iOS implementation - -@implementation UIEditBoxImplIOS_objc - -#pragma mark - Init & Dealloc - -- (instancetype)initWithFrame:(CGRect)frameRect editBox:(void *)editBox -{ - self = [super init]; - - if (self) - { - _editState = NO; - UITextField *textField = [[[UICustomUITextField alloc] initWithFrame: frameRect] autorelease]; - - textField.textColor = [UIColor whiteColor]; - // TODO: need to delete hard code here. - textField.font = [UIFont systemFontOfSize:frameRect.size.height*2/3]; - textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; - textField.backgroundColor = [UIColor clearColor]; - textField.borderStyle = UITextBorderStyleNone; - textField.delegate = self; - textField.hidden = true; - textField.returnKeyType = UIReturnKeyDefault; - - [textField addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged]; - - - self.textField = textField; - self.editBox = editBox; - } - - return self; -} - -- (void)dealloc -{ - [_textField resignFirstResponder]; - [_textField removeFromSuperview]; - - self.textField = nil; - - [super dealloc]; -} - -#pragma mark - Public methods - -- (void)doAnimationWhenKeyboardMoveWithDuration:(float)duration distance:(float)distance -{ - auto view = cocos2d::Director::getInstance()->getOpenGLView(); - CCEAGLView *eaglview = (CCEAGLView *)view->getEAGLView(); - - [eaglview doAnimationWhenKeyboardMoveWithDuration:duration distance:distance]; -} - -- (void)setPosition:(CGPoint)pos -{ - // TODO: Handle anchor point? - CGRect frame = _textField.frame; - frame.origin = pos; - - _textField.frame = frame; -} - -- (void)setContentSize:(CGSize)size -{ - CGRect frame = _textField.frame; - frame.size = size; - - _textField.frame = frame; -} - -- (void)openKeyboard -{ - auto view = cocos2d::Director::getInstance()->getOpenGLView(); - CCEAGLView *eaglview = (CCEAGLView *)view->getEAGLView(); - - [eaglview addSubview:_textField]; - [_textField becomeFirstResponder]; -} - -- (void)closeKeyboard -{ - [_textField resignFirstResponder]; - [_textField removeFromSuperview]; -} - -- (BOOL)textFieldShouldReturn:(UITextField *)sender -{ - if (sender == _textField) { - [sender resignFirstResponder]; - } - return NO; -} - -- (void)animationSelector -{ - auto view = cocos2d::Director::getInstance()->getOpenGLView(); - CCEAGLView *eaglview = (CCEAGLView *)view->getEAGLView(); - - [eaglview doAnimationWhenAnotherEditBeClicked]; -} - -/** - * Called each time when the text field's text has changed. - */ -- (void)textChanged:(UITextField*)textField -{ - int maxLength = getEditBoxImplIOS()->getMaxLength(); - if (textField.text.length > maxLength) { - textField.text = [textField.text substringToIndex:maxLength]; - } - - const char* inputText = [textField.text UTF8String]; - getEditBoxImplIOS()->editBoxEditingChanged(inputText); -} - -#pragma mark - UITextField delegate methods - -- (BOOL)textFieldShouldBeginEditing:(UITextField *)sender // return NO to disallow editing. -{ - CCLOG("textFieldShouldBeginEditing..."); - _editState = YES; - - auto view = cocos2d::Director::getInstance()->getOpenGLView(); - CCEAGLView *eaglview = (CCEAGLView *) view->getEAGLView(); - - if ([eaglview isKeyboardShown]) - { - [self performSelector:@selector(animationSelector) withObject:nil afterDelay:0.0f]; - } - - getEditBoxImplIOS()->editBoxEditingDidBegin(); - return YES; -} - -- (BOOL)textFieldShouldEndEditing:(UITextField *)sender -{ - CCLOG("textFieldShouldEndEditing..."); - _editState = NO; - const char* inputText = [sender.text UTF8String]; - - getEditBoxImplIOS()->editBoxEditingDidEnd(inputText); - - return YES; -} - -/** - * Delegate method called before the text has been changed. - * @param textField The text field containing the text. - * @param range The range of characters to be replaced. - * @param string The replacement string. - * @return YES if the specified text range should be replaced; otherwise, NO to keep the old text. - */ -- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string -{ - int maxLength = getEditBoxImplIOS()->getMaxLength(); - if (maxLength < 0) - { - return YES; - } - - // Prevent crashing undo bug http://stackoverflow.com/questions/433337/set-the-maximum-character-length-of-a-uitextfield - if(range.length + range.location > textField.text.length) - { - return NO; - } - - NSUInteger oldLength = textField.text.length; - NSUInteger replacementLength = string.length; - NSUInteger rangeLength = range.length; - - NSUInteger newLength = oldLength - rangeLength + replacementLength; - - return newLength <= maxLength; -} - -@end - NS_CC_BEGIN @@ -275,7 +53,7 @@ EditBoxImpl* __createSystemEditBox(EditBox* pEditBox) EditBoxImplIOS::EditBoxImplIOS(EditBox* pEditText) : EditBoxImplCommon(pEditText) -,_systemControl(nullptr) +, _systemControl(nullptr) , _anchorPoint(Vec2(0.5f, 0.5f)) { @@ -301,7 +79,7 @@ void EditBoxImplIOS::createNativeControl(const Rect& frame) _systemControl = [[UIEditBoxImplIOS_objc alloc] initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, - rect.size.height) + rect.size.height) editBox:this]; } @@ -313,8 +91,7 @@ bool EditBoxImplIOS::isEditing() void EditBoxImplIOS::doAnimationWhenKeyboardMove(float duration, float distance) { - if ([_systemControl isEditState] || distance < 0.0f) - { + if ([_systemControl isEditState] || distance < 0.0f) { [_systemControl doAnimationWhenKeyboardMoveWithDuration:duration distance:distance]; } } @@ -322,14 +99,14 @@ void EditBoxImplIOS::doAnimationWhenKeyboardMove(float duration, float distance) void EditBoxImplIOS::setNativeFont(const char* pFontName, int fontSize) { UIFont* textFont = constructFont(pFontName, fontSize); - if(textFont != nil) { - [_systemControl.textField setFont:textFont]; + if (textFont != nil) { + [_systemControl setFont:textFont]; } } void EditBoxImplIOS::setNativeFontColor(const Color4B& color) { - _systemControl.textField.textColor = [UIColor colorWithRed:color.r / 255.0f + _systemControl.textColor = [UIColor colorWithRed:color.r / 255.0f green:color.g / 255.0f blue:color.b / 255.0f alpha:color.a / 255.f]; @@ -348,54 +125,12 @@ void EditBoxImplIOS::setNativePlaceholderFontColor(const Color4B& color) void EditBoxImplIOS::setNativeInputMode(EditBox::InputMode inputMode) { - switch (inputMode) - { - case EditBox::InputMode::EMAIL_ADDRESS: - _systemControl.textField.keyboardType = UIKeyboardTypeEmailAddress; - break; - case EditBox::InputMode::NUMERIC: - _systemControl.textField.keyboardType = UIKeyboardTypeDecimalPad; - break; - case EditBox::InputMode::PHONE_NUMBER: - _systemControl.textField.keyboardType = UIKeyboardTypePhonePad; - break; - case EditBox::InputMode::URL: - _systemControl.textField.keyboardType = UIKeyboardTypeURL; - break; - case EditBox::InputMode::DECIMAL: - _systemControl.textField.keyboardType = UIKeyboardTypeDecimalPad; - break; - case EditBox::InputMode::SINGLE_LINE: - _systemControl.textField.keyboardType = UIKeyboardTypeDefault; - break; - default: - _systemControl.textField.keyboardType = UIKeyboardTypeDefault; - break; - } + [_systemControl setInputMode:inputMode]; } void EditBoxImplIOS::setNativeInputFlag(EditBox::InputFlag inputFlag) { - switch (inputFlag) - { - case EditBox::InputFlag::PASSWORD: - _systemControl.textField.secureTextEntry = YES; - break; - case EditBox::InputFlag::INITIAL_CAPS_WORD: - _systemControl.textField.autocapitalizationType = UITextAutocapitalizationTypeWords; - break; - case EditBox::InputFlag::INITIAL_CAPS_SENTENCE: - _systemControl.textField.autocapitalizationType = UITextAutocapitalizationTypeSentences; - break; - case EditBox::InputFlag::INTIAL_CAPS_ALL_CHARACTERS: - _systemControl.textField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters; - break; - case EditBox::InputFlag::SENSITIVE: - _systemControl.textField.autocorrectionType = UITextAutocorrectionTypeNo; - break; - default: - break; - } + [_systemControl setInputFlag:inputFlag]; } NSString* removeSiriString(NSString* str) @@ -404,53 +139,33 @@ NSString* removeSiriString(NSString* str) return [str stringByReplacingOccurrencesOfString:siriString withString:@""]; } -const char* EditBoxImplIOS::getText(void) +const char* EditBoxImplIOS::getText(void) { - return [removeSiriString(_systemControl.textField.text) UTF8String]; + return [removeSiriString(_systemControl.text) UTF8String]; } void EditBoxImplIOS::setNativeReturnType(EditBox::KeyboardReturnType returnType) { - switch (returnType) { - case EditBox::KeyboardReturnType::DEFAULT: - _systemControl.textField.returnKeyType = UIReturnKeyDefault; - break; - case EditBox::KeyboardReturnType::DONE: - _systemControl.textField.returnKeyType = UIReturnKeyDone; - break; - case EditBox::KeyboardReturnType::SEND: - _systemControl.textField.returnKeyType = UIReturnKeySend; - break; - case EditBox::KeyboardReturnType::SEARCH: - _systemControl.textField.returnKeyType = UIReturnKeySearch; - break; - case EditBox::KeyboardReturnType::GO: - _systemControl.textField.returnKeyType = UIReturnKeyGo; - break; - default: - _systemControl.textField.returnKeyType = UIReturnKeyDefault; - break; - } + [_systemControl setReturnType:returnType]; } void EditBoxImplIOS::setNativeText(const char* pText) { NSString* nsText =[NSString stringWithUTF8String:pText]; - if ([nsText compare:_systemControl.textField.text] != NSOrderedSame) - { - _systemControl.textField.text = nsText; + if ([nsText compare:_systemControl.text] != NSOrderedSame) { + _systemControl.text = nsText; } } void EditBoxImplIOS::setNativePlaceHolder(const char* pText) { - _systemControl.textField.placeholder = [NSString stringWithUTF8String:pText]; + [_systemControl setPlaceHolder:[NSString stringWithUTF8String:pText]]; } void EditBoxImplIOS::setNativeVisible(bool visible) { - _systemControl.textField.hidden = !visible; + [_systemControl setVisible:visible]; } void EditBoxImplIOS::updateNativeFrame(const Rect& rect) @@ -470,13 +185,13 @@ void EditBoxImplIOS::setNativeContentSize(const Size& size) const char* EditBoxImplIOS::getNativeDefaultFontName() { - const char* pDefaultFontName = [[_systemControl.textField.font fontName] UTF8String]; + const char* pDefaultFontName = [[_systemControl getDefaultFontName] UTF8String]; return pDefaultFontName; } void EditBoxImplIOS::nativeOpenKeyboard() { - _systemControl.textField.hidden = NO; + [_systemControl setVisible:YES]; [_systemControl openKeyboard]; } @@ -497,7 +212,7 @@ UIFont* EditBoxImplIOS::constructFont(const char *fontName, int fontSize) if (fontSize == -1) { - fontSize = [_systemControl.textField frame].size.height*2/3; + fontSize = _systemControl.frameRect.size.height*2/3; } else { diff --git a/cocos/ui/UIEditBox/iOS/CCUIEditBoxIOS.h b/cocos/ui/UIEditBox/iOS/CCUIEditBoxIOS.h new file mode 100644 index 0000000000..8aa9ff7b78 --- /dev/null +++ b/cocos/ui/UIEditBox/iOS/CCUIEditBoxIOS.h @@ -0,0 +1,62 @@ +/**************************************************************************** + Copyright (c) 2010-2012 cocos2d-x.org + Copyright (c) 2012 James Chen + Copyright (c) 2013-2015 zilongshanren + Copyright (c) 2015 Mazyad Alabduljaleel + + http://www.cocos2d-x.org + + 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: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + 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 +#import "CCUITextInput.h" +#include "UIEditBoxImpl-ios.h" + + +@interface UIEditBoxImplIOS_objc : NSObject + +@property (nonatomic, retain) UIView *textInput; +@property (nonatomic, assign) void *editBox; +@property (nonatomic, assign) NSString *text; +@property (nonatomic, assign) CGRect frameRect; +@property (nonatomic, assign) cocos2d::ui::EditBox::InputFlag dataInputMode; +@property (nonatomic, assign) cocos2d::ui::EditBox::KeyboardReturnType keyboardReturnType; +@property (nonatomic, readonly, getter = isEditState) BOOL editState; + +- (instancetype)initWithFrame:(CGRect)frameRect editBox:(void *)editBox; +- (void)doAnimationWhenKeyboardMoveWithDuration:(float)duration distance:(float)distance; + +- (NSString *)getDefaultFontName; + +- (void)setInputMode:(cocos2d::ui::EditBox::InputMode)inputMode; +- (void)setInputFlag:(cocos2d::ui::EditBox::InputFlag)flag; +- (void)setReturnType:(cocos2d::ui::EditBox::KeyboardReturnType)returnType; + +- (void)setPlaceHolder:(NSString *)text; +- (void)setVisible:(BOOL)visible; +- (void)setTextColor:(UIColor*)color; +- (void)setFont:(UIFont *)font; +- (void)setPosition:(CGPoint)pos; +- (void)setContentSize:(CGSize)size; + +- (void)openKeyboard; +- (void)closeKeyboard; + +@end diff --git a/cocos/ui/UIEditBox/iOS/CCUIEditBoxIOS.mm b/cocos/ui/UIEditBox/iOS/CCUIEditBoxIOS.mm new file mode 100644 index 0000000000..de35ea08d1 --- /dev/null +++ b/cocos/ui/UIEditBox/iOS/CCUIEditBoxIOS.mm @@ -0,0 +1,457 @@ +/**************************************************************************** + Copyright (c) 2010-2012 cocos2d-x.org + Copyright (c) 2012 James Chen + Copyright (c) 2013-2015 zilongshanren + Copyright (c) 2015 Mazyad Alabduljaleel + + http://www.cocos2d-x.org + + 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: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + 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 "CCUIEditBoxIOS.h" +#import "CCUISingleLineTextField.h" +#import "CCUIMultilineTextField.h" + +#import "platform/ios/CCEAGLView-ios.h" +#include "base/CCDirector.h" + +#define getEditBoxImplIOS() ((cocos2d::ui::EditBoxImplIOS *)_editBox) + + +@implementation UIEditBoxImplIOS_objc + +#pragma mark - Static methods + ++ (void)initialize +{ + [super initialize]; + + LoadUITextViewCCUITextInputCategory(); + LoadUITextFieldCCUITextInputCategory(); +} + +#pragma mark - Init & Dealloc + +- (instancetype)initWithFrame:(CGRect)frameRect editBox:(void *)editBox +{ + self = [super init]; + if (self) { + + _editState = NO; + self.frameRect = frameRect; + self.editBox = editBox; + self.dataInputMode = cocos2d::ui::EditBox::InputFlag::INTIAL_CAPS_ALL_CHARACTERS; + self.keyboardReturnType = cocos2d::ui::EditBox::KeyboardReturnType::DEFAULT; + + [self createMultiLineTextField]; + } + + return self; +} + +- (void)dealloc +{ + // custom setter cleanup + self.textInput = nil; + + [super dealloc]; +} + +#pragma mark - Properties + +- (void)setTextInput:(UIView *)textInput +{ + if (_textInput == textInput) { + return; + } + + // common init + textInput.backgroundColor = [UIColor clearColor]; + textInput.hidden = true; + textInput.returnKeyType = UIReturnKeyDefault; + [textInput ccui_setDelegate:self]; + + // Migrate properties + textInput.ccui_textColor = _textInput.ccui_textColor ?: [UIColor whiteColor]; + textInput.ccui_text = _textInput.ccui_text ?: @""; + textInput.ccui_placeholder = _textInput.ccui_placeholder ?: @""; + textInput.ccui_font = _textInput.ccui_font ?: [UIFont systemFontOfSize:self.frameRect.size.height*2/3]; + + [_textInput resignFirstResponder]; + [_textInput removeFromSuperview]; + [_textInput release]; + + _textInput = [textInput retain]; + + [self setInputFlag:self.dataInputMode]; + [self setReturnType:self.keyboardReturnType]; +} + +#pragma mark - Public methods + +- (void)createSingleLineTextField +{ + CCUISingleLineTextField *textField = [[[CCUISingleLineTextField alloc] initWithFrame:self.frameRect] autorelease]; + textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; + textField.borderStyle = UITextBorderStyleNone; + + [textField addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged]; + + self.textInput = textField; +} + +- (void)createMultiLineTextField +{ + CCUIMultilineTextField *textView = [[[CCUIMultilineTextField alloc] initWithFrame:self.frameRect] autorelease]; + self.textInput = textView; +} + +#pragma mark - Public methods + +- (void)setFont:(UIFont *)font +{ + self.textInput.ccui_font = font; +} + +- (void)setTextColor:(UIColor*)color +{ + self.textInput.ccui_textColor = color; +} + +- (void)setInputMode:(cocos2d::ui::EditBox::InputMode)inputMode +{ + //multiline input + if (inputMode == cocos2d::ui::EditBox::InputMode::ANY) { + if (![self.textInput isKindOfClass:[UITextView class]]) { + [self createMultiLineTextField]; + } + } + else { + if (![self.textInput isKindOfClass:[UITextField class]]) { + [self createSingleLineTextField]; + } + } + + switch (inputMode) + { + case cocos2d::ui::EditBox::InputMode::EMAIL_ADDRESS: + self.keyboardType = UIKeyboardTypeEmailAddress; + break; + case cocos2d::ui::EditBox::InputMode::NUMERIC: + self.keyboardType = UIKeyboardTypeDecimalPad; + break; + case cocos2d::ui::EditBox::InputMode::PHONE_NUMBER: + self.keyboardType = UIKeyboardTypePhonePad; + break; + case cocos2d::ui::EditBox::InputMode::URL: + self.keyboardType = UIKeyboardTypeURL; + break; + case cocos2d::ui::EditBox::InputMode::DECIMAL: + self.keyboardType = UIKeyboardTypeDecimalPad; + break; + case cocos2d::ui::EditBox::InputMode::SINGLE_LINE: + self.keyboardType = UIKeyboardTypeDefault; + break; + default: + self.keyboardType = UIKeyboardTypeDefault; + break; + } +} + +- (void)setKeyboardType:(UIKeyboardType)type +{ + self.textInput.keyboardType = type; +} + +- (void)setInputFlag:(cocos2d::ui::EditBox::InputFlag)flag +{ + self.dataInputMode = flag; + switch (flag) + { + case cocos2d::ui::EditBox::InputFlag::PASSWORD: + //textView can't be used for input password + self.textInput.ccui_secureTextEntry = YES; + break; + + case cocos2d::ui::EditBox::InputFlag::INITIAL_CAPS_WORD: + self.textInput.autocapitalizationType = UITextAutocapitalizationTypeWords; + break; + + case cocos2d::ui::EditBox::InputFlag::INITIAL_CAPS_SENTENCE: + self.textInput.autocapitalizationType = UITextAutocapitalizationTypeSentences; + break; + + case cocos2d::ui::EditBox::InputFlag::INTIAL_CAPS_ALL_CHARACTERS: + self.textInput.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters; + break; + + case cocos2d::ui::EditBox::InputFlag::SENSITIVE: + self.textInput.autocorrectionType = UITextAutocorrectionTypeNo; + break; + + default: + break; + } +} + +- (void)setReturnType:(cocos2d::ui::EditBox::KeyboardReturnType)returnType +{ + self.keyboardReturnType = returnType; + switch (returnType) { + case cocos2d::ui::EditBox::KeyboardReturnType::DEFAULT: + self.textInput.returnKeyType = UIReturnKeyDefault; + break; + + case cocos2d::ui::EditBox::KeyboardReturnType::DONE: + self.textInput.returnKeyType = UIReturnKeyDone; + break; + + case cocos2d::ui::EditBox::KeyboardReturnType::SEND: + self.textInput.returnKeyType = UIReturnKeySend; + break; + + case cocos2d::ui::EditBox::KeyboardReturnType::SEARCH: + self.textInput.returnKeyType = UIReturnKeySearch; + break; + + case cocos2d::ui::EditBox::KeyboardReturnType::GO: + self.textInput.returnKeyType = UIReturnKeyGo; + break; + + default: + self.textInput.returnKeyType = UIReturnKeyDefault; + break; + } +} + +- (void)setText:(NSString *)text +{ + self.textInput.ccui_text = text; +} + +- (NSString *)text +{ + return self.textInput.ccui_text ?: @""; +} + +- (void)setVisible:(BOOL)visible +{ + self.textInput.hidden = !visible; +} + +- (NSString *)getDefaultFontName +{ + return self.textInput.ccui_font.fontName ?: @""; +} + +- (void)setPlaceHolder:(NSString *)text +{ + self.textInput.ccui_placeholder = text; +} + +- (void)doAnimationWhenKeyboardMoveWithDuration:(float)duration distance:(float)distance +{ + auto view = cocos2d::Director::getInstance()->getOpenGLView(); + CCEAGLView *eaglview = (CCEAGLView *)view->getEAGLView(); + + [eaglview doAnimationWhenKeyboardMoveWithDuration:duration distance:distance]; +} + +- (void)setPosition:(CGPoint)pos +{ + // TODO: Handle anchor point? + CGRect frame = self.textInput.frame; + frame.origin = pos; + self.textInput.frame = frame; +} + +- (void)setContentSize:(CGSize)size +{ + CGRect frame = self.textInput.frame; + frame.size = size; + self.textInput.frame = frame; +} + +- (void)openKeyboard +{ + auto view = cocos2d::Director::getInstance()->getOpenGLView(); + CCEAGLView *eaglview = (CCEAGLView *)view->getEAGLView(); + + [eaglview addSubview:self.textInput]; + [self.textInput becomeFirstResponder]; +} + +- (void)closeKeyboard +{ + [self.textInput resignFirstResponder]; + [self.textInput removeFromSuperview]; +} + +- (BOOL)textFieldShouldReturn:(UITextField *)sender +{ + if (sender == self.textInput) { + [sender resignFirstResponder]; + } + return NO; +} + +- (void)animationSelector +{ + auto view = cocos2d::Director::getInstance()->getOpenGLView(); + CCEAGLView *eaglview = (CCEAGLView *)view->getEAGLView(); + + [eaglview doAnimationWhenAnotherEditBeClicked]; +} + +#pragma mark - UITextView delegate methods + +- (BOOL)textViewShouldBeginEditing:(UITextView *)textView +{ + CCLOG("textFieldShouldBeginEditing..."); + _editState = YES; + + auto view = cocos2d::Director::getInstance()->getOpenGLView(); + CCEAGLView *eaglview = (CCEAGLView *) view->getEAGLView(); + + if ([eaglview isKeyboardShown]) { + [self performSelector:@selector(animationSelector) withObject:nil afterDelay:0.0f]; + } + + getEditBoxImplIOS()->editBoxEditingDidBegin(); + return YES; +} + +- (BOOL)textViewShouldEndEditing:(UITextView *)textView; +{ + CCLOG("textFieldShouldEndEditing..."); + _editState = NO; + getEditBoxImplIOS()->refreshInactiveText(); + + const char* inputText = [textView.text UTF8String]; + getEditBoxImplIOS()->editBoxEditingDidEnd(inputText); + + return YES; +} + +- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text +{ + int maxLength = getEditBoxImplIOS()->getMaxLength(); + if (maxLength < 0) + { + return YES; + } + + // Prevent crashing undo bug http://stackoverflow.com/questions/433337/set-the-maximum-character-length-of-a-uitextfield + if (range.length + range.location > textView.text.length) { + return NO; + } + + NSUInteger oldLength = textView.text.length; + NSUInteger replacementLength = text.length; + NSUInteger rangeLength = range.length; + + NSUInteger newLength = oldLength - rangeLength + replacementLength; + + return newLength <= maxLength; +} + +- (void)textViewDidChange:(UITextView *)textView +{ + int maxLength = getEditBoxImplIOS()->getMaxLength(); + if (textView.text.length > maxLength) { + textView.text = [textView.text substringToIndex:maxLength]; + } + + const char* inputText = [textView.text UTF8String]; + getEditBoxImplIOS()->editBoxEditingChanged(inputText); +} + + +#pragma mark - UITextField delegate methods +/** + * Called each time when the text field's text has changed. + */ +- (void)textChanged:(UITextField *)textField +{ + int maxLength = getEditBoxImplIOS()->getMaxLength(); + if (textField.text.length > maxLength) { + textField.text = [textField.text substringToIndex:maxLength]; + } + + const char* inputText = [textField.text UTF8String]; + getEditBoxImplIOS()->editBoxEditingChanged(inputText); +} + +- (BOOL)textFieldShouldBeginEditing:(UITextField *)sender // return NO to disallow editing. +{ + CCLOG("textFieldShouldBeginEditing..."); + _editState = YES; + + auto view = cocos2d::Director::getInstance()->getOpenGLView(); + CCEAGLView *eaglview = (CCEAGLView *)view->getEAGLView(); + + if ([eaglview isKeyboardShown]) { + [self performSelector:@selector(animationSelector) withObject:nil afterDelay:0.0f]; + } + + getEditBoxImplIOS()->editBoxEditingDidBegin(); + return YES; +} + +- (BOOL)textFieldShouldEndEditing:(UITextField *)sender +{ + CCLOG("textFieldShouldEndEditing..."); + _editState = NO; + const char* inputText = [sender.text UTF8String]; + + getEditBoxImplIOS()->editBoxEditingDidEnd(inputText); + + return YES; +} + +/** + * Delegate method called before the text has been changed. + * @param textField The text field containing the text. + * @param range The range of characters to be replaced. + * @param string The replacement string. + * @return YES if the specified text range should be replaced; otherwise, NO to keep the old text. + */ +- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string +{ + int maxLength = getEditBoxImplIOS()->getMaxLength(); + if (maxLength < 0) { + return YES; + } + + // Prevent crashing undo bug http://stackoverflow.com/questions/433337/set-the-maximum-character-length-of-a-uitextfield + if (range.length + range.location > textField.text.length) { + return NO; + } + + NSUInteger oldLength = textField.text.length; + NSUInteger replacementLength = string.length; + NSUInteger rangeLength = range.length; + + NSUInteger newLength = oldLength - rangeLength + replacementLength; + + return newLength <= maxLength; +} + +@end diff --git a/cocos/ui/UIEditBox/iOS/CCUIMultilineTextField.h b/cocos/ui/UIEditBox/iOS/CCUIMultilineTextField.h new file mode 100644 index 0000000000..0a57d7d1e1 --- /dev/null +++ b/cocos/ui/UIEditBox/iOS/CCUIMultilineTextField.h @@ -0,0 +1,37 @@ +/**************************************************************************** + Copyright (c) 2010-2012 cocos2d-x.org + Copyright (c) 2012 James Chen + Copyright (c) 2015 Mazyad Alabduljaleel + + http://www.cocos2d-x.org + + 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: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + 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 +#import "UITextView+CCUITextInput.h" + +#pragma mark - UIMultilineTextField implementation + +@interface CCUIMultilineTextField : UITextView + +@property (nonatomic, assign) NSString *placeholder; +@property (nonatomic, retain) UILabel *placeHolderLabel; + +@end diff --git a/cocos/ui/UIEditBox/iOS/CCUIMultilineTextField.mm b/cocos/ui/UIEditBox/iOS/CCUIMultilineTextField.mm new file mode 100644 index 0000000000..d83b4ef78c --- /dev/null +++ b/cocos/ui/UIEditBox/iOS/CCUIMultilineTextField.mm @@ -0,0 +1,148 @@ +/**************************************************************************** + Copyright (c) 2010-2012 cocos2d-x.org + Copyright (c) 2012 James Chen + Copyright (c) 2015 Mazyad Alabduljaleel + + http://www.cocos2d-x.org + + 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: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + 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 "CCUIMultilineTextField.h" + +#include "base/CCDirector.h" + +/** + * http://stackoverflow.com/questions/1328638/placeholder-in-uitextview + */ +CGFloat const UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION = 0.25; + + +@implementation CCUIMultilineTextField + +#pragma mark - Init & Dealloc + +- (instancetype)initWithFrame:(CGRect)frame +{ + self = [super initWithFrame:frame]; + if (self) { + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(textChanged:) + name:UITextViewTextDidChangeNotification + object:nil]; + } + return self; +} + +- (void)dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver:self]; + + [_placeHolderLabel release]; + + [super dealloc]; +} + +#pragma mark - Properties + +- (NSString *)placeholder +{ + return self.placeHolderLabel.text; +} + +- (void)setPlaceholder:(NSString *)placeholder +{ + self.placeHolderLabel.text = placeholder; + [self.placeHolderLabel sizeToFit]; +} + +- (void)setText:(NSString *)text +{ + [super setText:text]; + [self textChanged:nil]; +} + +- (UILabel *)placeHolderLabel +{ + if (_placeHolderLabel == nil) { + + _placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8,8,self.bounds.size.width - 16,0)]; + _placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping; + _placeHolderLabel.numberOfLines = 0; + _placeHolderLabel.font = self.font; + _placeHolderLabel.backgroundColor = [UIColor clearColor]; + _placeHolderLabel.textColor = [UIColor lightGrayColor]; + _placeHolderLabel.alpha = 0; + + [self addSubview:_placeHolderLabel]; + } + + return _placeHolderLabel; +} + +#pragma mark - Public methods + +- (CGRect)textRectForBounds:(CGRect)bounds +{ + auto glview = cocos2d::Director::getInstance()->getOpenGLView(); + + float padding = CC_EDIT_BOX_PADDING * glview->getScaleX() / glview->getContentScaleFactor(); + return CGRectInset(bounds, padding, padding); +} + +- (CGRect)editingRectForBounds:(CGRect)bounds +{ + return [self textRectForBounds:bounds]; +} + +- (void)layoutSubviews +{ + [super layoutSubviews]; + + if (self.placeholder.length > 0) { + [self sendSubviewToBack:self.placeHolderLabel]; + } +} + +- (void)drawRect:(CGRect)rect +{ + if (self.text.length == 0 && self.placeholder.length > 0) { + self.placeHolderLabel.alpha = 1; + } + + [super drawRect:rect]; +} + +#pragma mark - NSNotification Observers + +- (void)textChanged:(NSNotification *)notification +{ + if (self.placeholder.length == 0) { + return; + } + + [UIView animateWithDuration:UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION animations:^{ + + CGFloat alpha = (self.text.length == 0 ? 1 : 0); + self.placeHolderLabel.alpha = alpha; + }]; +} + +@end diff --git a/cocos/ui/UIEditBox/iOS/CCUISingleLineTextField.h b/cocos/ui/UIEditBox/iOS/CCUISingleLineTextField.h new file mode 100644 index 0000000000..8b67df9018 --- /dev/null +++ b/cocos/ui/UIEditBox/iOS/CCUISingleLineTextField.h @@ -0,0 +1,33 @@ +/**************************************************************************** + Copyright (c) 2010-2012 cocos2d-x.org + Copyright (c) 2012 James Chen + Copyright (c) 2015 Mazyad Alabduljaleel + + http://www.cocos2d-x.org + + 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: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + 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 +#import "UITextField+CCUITextInput.h" + +/** TODO: Missing doc - Why is this subclass necessary? + */ +@interface CCUISingleLineTextField : UITextField +@end diff --git a/cocos/ui/UIEditBox/iOS/CCUISingleLineTextField.mm b/cocos/ui/UIEditBox/iOS/CCUISingleLineTextField.mm new file mode 100644 index 0000000000..06096093a0 --- /dev/null +++ b/cocos/ui/UIEditBox/iOS/CCUISingleLineTextField.mm @@ -0,0 +1,48 @@ +/**************************************************************************** + Copyright (c) 2010-2012 cocos2d-x.org + Copyright (c) 2012 James Chen + Copyright (c) 2015 Mazyad Alabduljaleel + + http://www.cocos2d-x.org + + 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: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + 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 "CCUISingleLineTextField.h" +#import "CCUITextInput.h" + +#include "base/CCDirector.h" + + +@implementation CCUISingleLineTextField + +- (CGRect)textRectForBounds:(CGRect)bounds +{ + auto glview = cocos2d::Director::getInstance()->getOpenGLView(); + + float padding = CC_EDIT_BOX_PADDING * glview->getScaleX() / glview->getContentScaleFactor(); + return CGRectInset(bounds, padding, padding); +} + +- (CGRect)editingRectForBounds:(CGRect)bounds +{ + return [self textRectForBounds:bounds]; +} + +@end diff --git a/cocos/ui/UIEditBox/iOS/CCUITextInput.h b/cocos/ui/UIEditBox/iOS/CCUITextInput.h new file mode 100644 index 0000000000..2aef29cdb5 --- /dev/null +++ b/cocos/ui/UIEditBox/iOS/CCUITextInput.h @@ -0,0 +1,46 @@ +/**************************************************************************** + Copyright (c) 2015 Mazyad Alabduljaleel + + http://www.cocos2d-x.org + + 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: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + 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. + ****************************************************************************/ + +#ifndef cocos2d_libs_CCUITextInput_h +#define cocos2d_libs_CCUITextInput_h + +static const int CC_EDIT_BOX_PADDING = 5; + +/** + This protocol provides a common interface for consolidating text input method calls + */ +@protocol CCUITextInput + +@property (nonatomic, retain, setter=ccui_setText:) NSString *ccui_text; +@property (nonatomic, retain, setter=ccui_setPlaceholder:) NSString *ccui_placeholder; +@property (nonatomic, retain, setter=ccui_setTextColor:) UIColor *ccui_textColor; +@property (nonatomic, retain, setter=ccui_setFont:) UIFont *ccui_font; +@property (nonatomic, assign, setter=ccui_setSecureTextEntry:) BOOL ccui_secureTextEntry; + +/* can only set. and not get, since the getter will only return one delegate implementation */ +- (void)ccui_setDelegate:(id)delegate; + +@end + +#endif diff --git a/cocos/ui/UIEditBox/iOS/UITextField+CCUITextInput.h b/cocos/ui/UIEditBox/iOS/UITextField+CCUITextInput.h new file mode 100644 index 0000000000..6a99a8bee8 --- /dev/null +++ b/cocos/ui/UIEditBox/iOS/UITextField+CCUITextInput.h @@ -0,0 +1,36 @@ +/**************************************************************************** + Copyright (c) 2015 Mazyad Alabduljaleel + + http://www.cocos2d-x.org + + 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: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + 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 +#import "CCUITextInput.h" + + +@interface UITextField (CCUITextInput) +@end + + +/** Trick to load category objects without using -ObjC flag + * http://stackoverflow.com/questions/2567498/objective-c-categories-in-static-library + */ +extern void LoadUITextFieldCCUITextInputCategory(); diff --git a/cocos/ui/UIEditBox/iOS/UITextField+CCUITextInput.mm b/cocos/ui/UIEditBox/iOS/UITextField+CCUITextInput.mm new file mode 100644 index 0000000000..289263b9f0 --- /dev/null +++ b/cocos/ui/UIEditBox/iOS/UITextField+CCUITextInput.mm @@ -0,0 +1,89 @@ +/**************************************************************************** + Copyright (c) 2015 Mazyad Alabduljaleel + + http://www.cocos2d-x.org + + 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: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + 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 "UITextField+CCUITextInput.h" + +@implementation UITextField (CCUITextInput) + +- (NSString *)ccui_text +{ + return self.text; +} + +- (void)ccui_setText:(NSString *)ccui_text +{ + self.text = ccui_text; +} + +- (NSString *)ccui_placeholder +{ + return self.placeholder; +} + +- (void)ccui_setPlaceholder:(NSString *)ccui_placeholder +{ + self.placeholder = ccui_placeholder; +} + +- (UIColor *)ccui_textColor +{ + return self.textColor; +} + +- (void)ccui_setTextColor:(UIColor *)ccui_textColor +{ + self.textColor = ccui_textColor; +} + +- (UIFont *)ccui_font +{ + return self.font; +} + +- (void)ccui_setFont:(UIFont *)ccui_font +{ + self.font = ccui_font; +} + +- (BOOL)ccui_secureTextEntry +{ + return self.secureTextEntry; +} + +- (void)ccui_setSecureTextEntry:(BOOL)ccui_secureTextEntry +{ + self.secureTextEntry = ccui_secureTextEntry; +} + +- (void)ccui_setDelegate:(id)delegate +{ + self.delegate = delegate; +} + +@end + + +void LoadUITextFieldCCUITextInputCategory() { + // noop +} diff --git a/cocos/ui/UIEditBox/iOS/UITextView+CCUITextInput.h b/cocos/ui/UIEditBox/iOS/UITextView+CCUITextInput.h new file mode 100644 index 0000000000..62b35df68a --- /dev/null +++ b/cocos/ui/UIEditBox/iOS/UITextView+CCUITextInput.h @@ -0,0 +1,36 @@ +/**************************************************************************** + Copyright (c) 2015 Mazyad Alabduljaleel + + http://www.cocos2d-x.org + + 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: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + 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 +#import "CCUITextInput.h" + + +@interface UITextView (CCUITextInput) +@end + + +/** Trick to load category objects without using -ObjC flag + * http://stackoverflow.com/questions/2567498/objective-c-categories-in-static-library + */ +extern void LoadUITextViewCCUITextInputCategory(); diff --git a/cocos/ui/UIEditBox/iOS/UITextView+CCUITextInput.mm b/cocos/ui/UIEditBox/iOS/UITextView+CCUITextInput.mm new file mode 100644 index 0000000000..bfe5b8730e --- /dev/null +++ b/cocos/ui/UIEditBox/iOS/UITextView+CCUITextInput.mm @@ -0,0 +1,97 @@ +/**************************************************************************** + Copyright (c) 2015 Mazyad Alabduljaleel + + http://www.cocos2d-x.org + + 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: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + 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 "UITextView+CCUITextInput.h" + + +@implementation UITextView (CCUITextInput) + +- (NSString *)ccui_text +{ + return self.text; +} + +- (void)ccui_setText:(NSString *)ccui_text +{ + self.text = ccui_text; +} + +- (NSString *)ccui_placeholder +{ + SEL selector = @selector(placeholder); + if ([self respondsToSelector:selector]) { + return [self performSelector:selector]; + } + return nil; +} + +- (void)ccui_setPlaceholder:(NSString *)ccui_placeholder +{ + SEL selector = @selector(setPlaceholder:); + if ([self respondsToSelector:selector]) { + [self performSelector:selector withObject:ccui_placeholder]; + } +} + +- (UIColor *)ccui_textColor +{ + return self.textColor; +} + +- (void)ccui_setTextColor:(UIColor *)ccui_textColor +{ + self.textColor = ccui_textColor; +} + +- (UIFont *)ccui_font +{ + return self.font; +} + +- (void)ccui_setFont:(UIFont *)ccui_font +{ + self.font = ccui_font; +} + +- (BOOL)ccui_secureTextEntry +{ + return self.secureTextEntry; +} + +- (void)ccui_setSecureTextEntry:(BOOL)ccui_secureTextEntry +{ + self.secureTextEntry = ccui_secureTextEntry; +} + +- (void)ccui_setDelegate:(id)delegate +{ + self.delegate = delegate; +} + +@end + + +void LoadUITextViewCCUITextInputCategory() { + +} diff --git a/tests/cpp-tests/Classes/SpritePolygonTest/SpritePolygonTest.h b/tests/cpp-tests/Classes/SpritePolygonTest/SpritePolygonTest.h index cb043365d8..01fc2df088 100644 --- a/tests/cpp-tests/Classes/SpritePolygonTest/SpritePolygonTest.h +++ b/tests/cpp-tests/Classes/SpritePolygonTest/SpritePolygonTest.h @@ -16,8 +16,8 @@ protected: virtual std::string title() const override {return _title;}; virtual std::string subtitle() const override {return _subtitle;}; virtual bool init() override; - virtual void onEnter(); - virtual void onExit(); + virtual void onEnter()override; + virtual void onExit()override; SpritePolygonTestCase(); ~SpritePolygonTestCase(); void updateDrawNode(); @@ -89,7 +89,7 @@ protected: void initTouch(); void loadDefaultSprites(); void addSpritePolygon(const cocos2d::Vec2& pos); - void update(float dt); + void update(float dt)override; private: cocos2d::PolygonInfo _polygonInfo; int _tagIndex; @@ -125,7 +125,7 @@ protected: unsigned int _incPix; void updateLabel(); - virtual void update(float dt); + virtual void update(float dt)override; virtual bool init() override; virtual Node* makeSprite(); void incrementStats(); diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIEditBoxTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIEditBoxTest.cpp index 8e2d44897e..c6dd7d9a31 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIEditBoxTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIEditBoxTest.cpp @@ -57,8 +57,8 @@ bool UIEditBoxTest::init() // top std::string pNormalSprite = "extensions/green_edit.png"; - _editName = ui::EditBox::create(editBoxSize, ui::Scale9Sprite::create(pNormalSprite)); - _editName->setPosition(Vec2(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y+visibleSize.height*3/4)); + _editName = ui::EditBox::create(editBoxSize + Size(0,40), ui::Scale9Sprite::create(pNormalSprite)); + _editName->setPosition(Vec2(visibleOrigin.x+visibleSize.width/2-50, visibleOrigin.y+visibleSize.height*3/4)); _editName->setFontColor(Color3B::RED); _editName->setPlaceHolder("Name:"); _editName->setPlaceholderFontColor(Color3B::WHITE); @@ -68,10 +68,20 @@ bool UIEditBoxTest::init() _editName->setReturnType(ui::EditBox::KeyboardReturnType::DONE); _editName->setDelegate(this); addChild(_editName); - + + Button* button = Button::create("cocosui/animationbuttonnormal.png", + "cocosui/animationbuttonpressed.png"); + auto buttonSize = button->getContentSize(); + button->setTitleText("Single Line"); + button->setPosition(_editName->getPosition() + Vec2(editBoxSize.width/2 + buttonSize.width/2, 0 )); + button->addClickEventListener([=](Ref* ref){ + _editName->setInputMode(ui::EditBox::InputMode::SINGLE_LINE); + }); + addChild(button); + // middle _editPassword = ui::EditBox::create(editBoxSize, "extensions/orange_edit.png"); - _editPassword->setPosition(Vec2(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y+visibleSize.height/2)); + _editPassword->setPosition(Vec2(visibleOrigin.x+visibleSize.width/2-50, visibleOrigin.y+visibleSize.height/2)); _editPassword->setFontColor(Color3B::GREEN); _editPassword->setPlaceHolder("Password:"); _editPassword->setMaxLength(6); @@ -81,15 +91,31 @@ bool UIEditBoxTest::init() _editPassword->setDelegate(this); addChild(_editPassword); + auto buttonPassword = (ui::Button*)button->clone(); + buttonPassword->setTitleText("Multiline"); + buttonPassword->setPosition(_editPassword->getPosition() + Vec2(editBoxSize.width/2 + buttonSize.width/2, 0 )); + buttonPassword->addClickEventListener([=](Ref* ref){ + _editPassword->setInputMode(ui::EditBox::InputMode::ANY); + }); + addChild(buttonPassword); + // bottom auto bottomButtonSize = Size(editBoxSize.width, editBoxSize.height + 10); _editEmail = ui::EditBox::create(bottomButtonSize, "extensions/yellow_edit.png"); - _editEmail->setPosition(Vec2(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y+visibleSize.height/4)); + _editEmail->setPosition(Vec2(visibleOrigin.x+visibleSize.width/2-50, visibleOrigin.y+visibleSize.height/4)); _editEmail->setPlaceHolder("Email:"); _editEmail->setInputMode(ui::EditBox::InputMode::EMAIL_ADDRESS); _editEmail->setDelegate(this); addChild(_editEmail); + auto buttonEmail = (ui::Button*)button->clone(); + buttonEmail->setTitleText("Multiline"); + buttonEmail->setPosition(_editEmail->getPosition() + Vec2(editBoxSize.width/2 + buttonSize.width/2, 0 )); + buttonEmail->addClickEventListener([=](Ref* ref){ + _editEmail->setInputMode(ui::EditBox::InputMode::ANY); + }); + addChild(buttonEmail); + return true; } return false;