From d73699aabb849cb44b1d34e69a5e0ad3879b4805 Mon Sep 17 00:00:00 2001 From: yangws Date: Thu, 28 Apr 2011 14:19:12 +0800 Subject: [PATCH] [ios] tests add RootViewController for rotate with ui object. --- cocos2dx/platform/ios/EAGLView.h | 1 + cocos2dx/platform/ios/EAGLView.mm | 16 ++++- tests/test.ios/Classes/RootViewController.h | 16 +++++ tests/test.ios/Classes/RootViewController.mm | 61 +++++++++++++++++++ tests/test.ios/Classes/testsAppDelegate.h | 3 + tests/test.ios/Classes/testsAppDelegate.mm | 13 +++- .../project.pbxproj.REMOVED.git-id | 2 +- tests/tests/TextInputTest/TextInputTest.cpp | 52 ++++++++++------ tests/tests/TextInputTest/TextInputTest.h | 35 ++++++----- 9 files changed, 161 insertions(+), 38 deletions(-) create mode 100644 tests/test.ios/Classes/RootViewController.h create mode 100644 tests/test.ios/Classes/RootViewController.mm diff --git a/cocos2dx/platform/ios/EAGLView.h b/cocos2dx/platform/ios/EAGLView.h index fe36d9eaf5..a8be2da169 100755 --- a/cocos2dx/platform/ios/EAGLView.h +++ b/cocos2dx/platform/ios/EAGLView.h @@ -96,6 +96,7 @@ Copyright (C) 2008 Apple Inc. All Rights Reserved. CFMutableDictionaryRef touchesIntergerDict; unsigned int indexBitsUsed; NSString * markedText_; + CGRect caretRect_; } @property(nonatomic, readonly) UITextPosition *beginningOfDocument; diff --git a/cocos2dx/platform/ios/EAGLView.mm b/cocos2dx/platform/ios/EAGLView.mm index 4945aeccde..0fdf0c760d 100755 --- a/cocos2dx/platform/ios/EAGLView.mm +++ b/cocos2dx/platform/ios/EAGLView.mm @@ -696,7 +696,7 @@ static cocos2d::CCTouch *s_pTouches[MAX_TOUCHES]; - (CGRect)caretRectForPosition:(UITextPosition *)position; { CCLOG("caretRectForPosition"); - return CGRectMake(0, 0, 0, 0); + return caretRect_; } #pragma mark Hit testing @@ -756,13 +756,16 @@ static cocos2d::CCTouch *s_pTouches[MAX_TOUCHES]; tmp = end.size.width; end.size.width = end.size.height; end.size.height = tmp; + tmp = viewSize.width; + viewSize.width = viewSize.height; + viewSize.height = tmp; tmp = begin.origin.x; begin.origin.x = begin.origin.y; - begin.origin.y = viewSize.width - tmp - begin.size.height; + begin.origin.y = viewSize.height - tmp - begin.size.height; tmp = end.origin.x; end.origin.x = end.origin.y; - end.origin.y = viewSize.width - tmp - end.size.height; + end.origin.y = viewSize.height - tmp - end.size.height; break; case UIInterfaceOrientationLandscapeRight: @@ -772,6 +775,9 @@ static cocos2d::CCTouch *s_pTouches[MAX_TOUCHES]; tmp = end.size.width; end.size.width = end.size.height; end.size.height = tmp; + tmp = viewSize.width; + viewSize.width = viewSize.height; + viewSize.height = tmp; tmp = begin.origin.x; begin.origin.x = begin.origin.y; @@ -802,6 +808,9 @@ static cocos2d::CCTouch *s_pTouches[MAX_TOUCHES]; else if (UIKeyboardDidShowNotification == type) { dispatcher->dispatchKeyboardDidShow(notiInfo); + caretRect_ = end; + caretRect_.origin.y = caretRect_.origin.y + caretRect_.size.height; + caretRect_.size.height = 0; } else if (UIKeyboardWillHideNotification == type) { @@ -809,6 +818,7 @@ static cocos2d::CCTouch *s_pTouches[MAX_TOUCHES]; } else if (UIKeyboardDidHideNotification == type) { + caretRect_ = CGRectZero; dispatcher->dispatchKeyboardDidHide(notiInfo); } } diff --git a/tests/test.ios/Classes/RootViewController.h b/tests/test.ios/Classes/RootViewController.h new file mode 100644 index 0000000000..ab4f9cf49f --- /dev/null +++ b/tests/test.ios/Classes/RootViewController.h @@ -0,0 +1,16 @@ +// +// RootViewController.h +// test +// +// Created by Walzer on 11-4-28. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import + + +@interface RootViewController : UIViewController { + +} + +@end diff --git a/tests/test.ios/Classes/RootViewController.mm b/tests/test.ios/Classes/RootViewController.mm new file mode 100644 index 0000000000..7904cd4670 --- /dev/null +++ b/tests/test.ios/Classes/RootViewController.mm @@ -0,0 +1,61 @@ + // +// RootViewController.mm +// test +// +// Created by Walzer on 11-4-28. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import "RootViewController.h" + + +@implementation RootViewController + +/* + // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. +- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { + if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { + // Custom initialization + } + return self; +} +*/ + +/* +// Implement loadView to create a view hierarchy programmatically, without using a nib. +- (void)loadView { +} +*/ + +/* +// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. +- (void)viewDidLoad { + [super viewDidLoad]; +} + +*/ +// Override to allow orientations other than the default portrait orientation. +- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { + return UIInterfaceOrientationIsLandscape( interfaceOrientation ); +} + +- (void)didReceiveMemoryWarning { + // Releases the view if it doesn't have a superview. + [super didReceiveMemoryWarning]; + + // Release any cached data, images, etc that aren't in use. +} + +- (void)viewDidUnload { + [super viewDidUnload]; + // Release any retained subviews of the main view. + // e.g. self.myOutlet = nil; +} + + +- (void)dealloc { + [super dealloc]; +} + + +@end diff --git a/tests/test.ios/Classes/testsAppDelegate.h b/tests/test.ios/Classes/testsAppDelegate.h index fee899155f..709b147b68 100644 --- a/tests/test.ios/Classes/testsAppDelegate.h +++ b/tests/test.ios/Classes/testsAppDelegate.h @@ -8,8 +8,11 @@ #import +@class RootViewController; + @interface AppController : NSObject { UIWindow *window; + RootViewController *viewController; } @end diff --git a/tests/test.ios/Classes/testsAppDelegate.mm b/tests/test.ios/Classes/testsAppDelegate.mm index e6213d1fd1..ff632c1c90 100644 --- a/tests/test.ios/Classes/testsAppDelegate.mm +++ b/tests/test.ios/Classes/testsAppDelegate.mm @@ -11,6 +11,7 @@ #import "EAGLView.h" #import "cocos2d.h" #import "AppDelegate.h" +#import "RootViewController.h" @implementation AppController @@ -26,6 +27,11 @@ static AppDelegate s_sharedApplication; // Add the view controller's view to the window and display. window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]]; + + // Init the View Controller + viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil]; + viewController.wantsFullScreenLayout = YES; + EAGLView *__glView = [EAGLView viewWithFrame: [window bounds] pixelFormat: kEAGLColorFormatRGBA8 depthFormat: GL_DEPTH_COMPONENT16_OES @@ -34,7 +40,12 @@ static AppDelegate s_sharedApplication; multiSampling:NO numberOfSamples:0]; [__glView setMultipleTouchEnabled:YES]; - [window addSubview: __glView]; + + // make the OpenGLView a child of the view controller + [viewController setView:__glView]; + + // make the View Controller a child of the main window + [window addSubview: viewController.view]; [window makeKeyAndVisible]; [[UIApplication sharedApplication] setStatusBarHidden:true]; diff --git a/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id b/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id index 8e02741885..a7d84c529e 100644 --- a/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -85a9422443707c5ba69d66e2047740c1a865b91c \ No newline at end of file +a20fbecde64a220d0dbf2af6fc8bdff6f6c775d1 \ No newline at end of file diff --git a/tests/tests/TextInputTest/TextInputTest.cpp b/tests/tests/TextInputTest/TextInputTest.cpp index 66e5dce35c..96f9bca17e 100644 --- a/tests/tests/TextInputTest/TextInputTest.cpp +++ b/tests/tests/TextInputTest/TextInputTest.cpp @@ -14,7 +14,7 @@ enum static int testIdx = -1; -CCLayer* createTextInputTest(int nIndex) +KeyboardNotificationLayer* createTextInputTest(int nIndex) { switch(nIndex) { @@ -26,13 +26,12 @@ CCLayer* createTextInputTest(int nIndex) CCLayer* restartTextInputTest() { - CCLayer* pContainerLayer = new TextInputTest; + TextInputTest* pContainerLayer = new TextInputTest; pContainerLayer->autorelease(); - CCLayer* pTestLayer = createTextInputTest(testIdx); + KeyboardNotificationLayer* pTestLayer = createTextInputTest(testIdx); pTestLayer->autorelease(); - - pContainerLayer->addChild(pTestLayer); + pContainerLayer->addKeyboardNotificationLayer(pTestLayer); return pContainerLayer; } @@ -69,6 +68,12 @@ CCRect getRect(CCNode * pNode) // implement TextInputTest ////////////////////////////////////////////////////////////////////////// +TextInputTest::TextInputTest() +: m_pNotificationLayer(0) +{ + +} + void TextInputTest::restartCallback(CCObject* pSender) { CCScene* s = new TextInputTestScene(); @@ -94,6 +99,18 @@ void TextInputTest::backCallback(CCObject* pSender) s->release(); } +void TextInputTest::addKeyboardNotificationLayer(KeyboardNotificationLayer * pLayer) +{ + m_pNotificationLayer = pLayer; + m_pNotificationLayer->retain(); + addChild(pLayer); +} + +std::string TextInputTest::title() +{ + return "text input test"; +} + void TextInputTest::onEnter() { CCLayer::onEnter(); @@ -104,7 +121,7 @@ void TextInputTest::onEnter() addChild(label); label->setPosition(ccp(s.width/2, s.height-50)); - std::string subTitle = subtitle(); + std::string subTitle = m_pNotificationLayer->subtitle(); if(! subTitle.empty()) { CCLabelTTF* l = CCLabelTTF::labelWithString(subTitle.c_str(), "Thonburi", 16); @@ -125,14 +142,12 @@ void TextInputTest::onEnter() addChild(menu, 1); } -std::string TextInputTest::title() +void TextInputTest::onExit() { - return "text input test"; -} - -std::string TextInputTest::subtitle() -{ - return ""; + if (m_pNotificationLayer) + { + m_pNotificationLayer->release(); + } } ////////////////////////////////////////////////////////////////////////// @@ -145,6 +160,11 @@ KeyboardNotificationLayer::KeyboardNotificationLayer() setIsTouchEnabled(true); } +std::string KeyboardNotificationLayer::subtitle() +{ + return ""; +} + void KeyboardNotificationLayer::registerWithTouchDispatcher() { CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, false); @@ -256,12 +276,6 @@ void TextFieldTTFTest::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) } } - if (index == m_nSelected) - { - CCLOG("TextFieldTTFTest:noCCTextFieldTTFClicked."); - return; - } - if (m_nSelected >= 0) { // hide the keyboard diff --git a/tests/tests/TextInputTest/TextInputTest.h b/tests/tests/TextInputTest/TextInputTest.h index 8e48aad1e7..e1a3653ed3 100644 --- a/tests/tests/TextInputTest/TextInputTest.h +++ b/tests/tests/TextInputTest/TextInputTest.h @@ -3,17 +3,23 @@ #include "../testBasic.h" +class KeyboardNotificationLayer; + class TextInputTest : public CCLayer, public CCIMEDelegate { + KeyboardNotificationLayer * m_pNotificationLayer; public: - void restartCallback(CCObject* pSender); - void nextCallback(CCObject* pSender); + TextInputTest(); + + void restartCallback(CCObject* pSender); + void nextCallback(CCObject* pSender); void backCallback(CCObject* pSender); - virtual std::string title(); - virtual std::string subtitle(); - + std::string title(); + void addKeyboardNotificationLayer(KeyboardNotificationLayer * pLayer); + virtual void onEnter(); + virtual void onExit(); }; class KeyboardNotificationLayer : public CCLayer, public CCIMEDelegate @@ -21,11 +27,12 @@ class KeyboardNotificationLayer : public CCLayer, public CCIMEDelegate public: KeyboardNotificationLayer(); + virtual std::string subtitle(); virtual void registerWithTouchDispatcher(); virtual void keyboardWillShow(CCIMEKeyboardNotificationInfo& info); - -protected: - CCNode * m_pTrackNode; + +protected: + CCNode * m_pTrackNode; }; class TextFieldTTFTest : public KeyboardNotificationLayer @@ -36,16 +43,16 @@ class TextFieldTTFTest : public KeyboardNotificationLayer public: TextFieldTTFTest(); - virtual std::string subtitle(); + virtual std::string subtitle(); virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); }; -class TextInputTestScene : public TestScene -{ -public: - virtual void runThisTest(); -}; +class TextInputTestScene : public TestScene +{ +public: + virtual void runThisTest(); +}; #endif // __TEXT_INPUT_TEST_H__ \ No newline at end of file