kobold2d disable full screen on orientation change - cocos2d-iphone

I want to include cocos2d scene besides UIKit GUI elements in my app using kobold2d library, so it should take just part of the screen.
I alter cocos2d glview size like this when initializing the first layer:
UIView * glView = [[CCDirector sharedDirector]view];
CGRect rct = CGRectMake(100, 100, 300, 400);
[glView setFrame:rct];
The view is displayed properly until I change orientation, then glview again becomes fullscreen. Is there a way to disable this behaviour?
XCode 4.5.2, iOS 6, Kobold2D 2.04

Calling reshape after setting the frame might help:
UIView * glView = [[CCDirector sharedDirector]view];
CGRect rct = CGRectMake(100, 100, 300, 400);
[glView setFrame:rct];
[glView reshape];

Now after some experiments I kind of found solution, please correct me if I am wrong. At least this worked for me, hope someone else makes use of it either.
CCDirectorIOS is a UIViewController, and it is usually being set as a rootViewController of app window.
Looks like [window rootViewController] is responsible for making it's view fullscreen every time orientation changes, thus forcing all children to layout (I knew that!!! %).
Therefore, to avoid glView shrinking to the whole window now and then, give window another UIViewController as a rootController, not the CCDirector. That another UIViewController should have it's own brand new UIView. This new rootView is what this rootViewController can resize however he wants, leave alone our glView. Now, assign director as a child of rootController and assign glView as a child of a rootView.
Set glView to size You want - and off we go! Now when rootView resizes, glView doesn't. In my case it still had to resize while maintaining free space proportions so i used setAutoresizingMask on it. please gurus tell what You think.
known caveats:
rootController and navController of KKAppDelegate not used except for proper initial orientation
this raises bar of least ios version to 5 because of addChildViewController: method
the code inside app's AppDelegate (suppose ARC is ON):
-(void) initializationComplete
{
#ifdef KK_PLATFORM_IOS
RootViewController * rootvc = [[RootViewController alloc]init];
UIView * rootView = [[UIView alloc]init];
[rootvc setView:rootView];
[rootvc.view setBackgroundColor:[UIColor blueColor]];//to see views in colors
[window setRootViewController:rootvc];
[rootvc addChildViewController:[CCDirector sharedDirector]];
UIView * glview = [[CCDirector sharedDirector]view];
[rootvc.view addSubview:glview];//[[CCDirector sharedDirector]openGLView]];
CGRect glRect = CGRectInset(rootvc.view.bounds, 50, 50);//our glview frame is smaller than rootView frame
[glview setFrame:glRect];
[glview setAutoresizingMask: UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];//so the view size follows window's size even though it's not fullscreen
#endif
}

Related

Video not fitting in properly in QGraphicsView

I am trying a play a video(640 * 360) through rtsp in QGraphicsView. But the issue is that it is not fitting completely within the view and scroll bar is appearing, which should not happen. And moreover, I am able to get the same peace of code working properly in Linux environment but I am getting the issue in windows.
Please find the code snippet below, If anyone can point out the error I am making will be helpful.
scene = new QGraphicsScene(this);
view= new graphicsView();
view->setScene(scene);
videoItem = new QGraphicsVideoItem;
player= new QMediaPlayer;
player->setVideoOutput(videoItem);
view->scene()->addItem(videoItem);
controlLayout = new QHBoxLayout;
controlLayout->setMargin(0);
controlLayout->addWidget(view);
view->setSceneRect(scene->sceneRect());
view->scale(1.97,1.97);
ui.m_pframePlay->setLayout(controlLayout);
ui.m_pframePlay->show();
player->setMedia(QUrl("rtsp:..."));
player->play();
The documentation for QGraphicsView sais about setSceneRect
The scene rectangle defines the extent of the scene, and in the view's case, this means the area of the scene that you can navigate using the scroll bars.
This means, setSceneRect does not resize the visible area of the view but only which area of the scene is visible in the view. So I guess you simply have to resize your view, e.g.
view->resize(scene->width()*1.97, scene->height()*1.97)
(I scaled width/height with 1.97 because you scale your view using factor 1.97 for some reason).

Is it possible to attach a UISwitch to a certain CCLayer in cocos2d?

I am using a UISwitch in my cocos2d project like so:
//header file
UISwitch *musicCtrl;
//implementation file
musicCtrl = [[UISwitch alloc] initWithFrame:CGRectMake(100, 50, 0, 0)];
musicCtrl.on = YES;
[musicCtrl addTarget:self action:#selector(musicOnOff) forControlEvents:UIControlEventValueChanged];
[[[CCDirector sharedDirector]openGLView] addSubview:musicCtrl];
musicCtrl.transform = CGAffineTransformMakeRotation(M_PI/2);
[musicCtrl release];
I have attached the UISwitch to the openGLView but I need to be able to attach it to a certain CCLayer on the GLView. Is that possible? I can't seem to find a UISwitch alternative for cocos2d.
It's not possible.
You can only add UIKit views to the cocos2d view, or the main window. Either way, the UIKit view will be drawn above all of the cocos2d nodes. You can not add a UIKit view to a specific layer.
Specifically you can't add UIKit views to a cocos2d app in a way where some cocos2d nodes are drawn in front of and other nodes drawn behind the UIKit view. That is impossible.

Cocos2D UITextView not rotating with GLView

So I added a UITextView to my OpenGL view in Cocos2D.
Did some rotation to fit my landscape view and all. But somehow when the whole scene rotates, the textview doesn't. I've tried
[[[CCDirector sharedDirector] openGLView] window].autoresizesSubviews = YES;
and including these functions but it still stays put..
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
any ideas?
i've added it to my openGLView using:
[[[[CCDirector sharedDirector] openGLView] window] addSubview:self];
my class inherits from UITextView.
Open file GameConfig.h, define Autorotation type. Hope it will help.

UIButton of previous scene overlaps CCSprite

I have a UIButton that moves around randomly on the screen. On clicking on the button, a new scene is loaded that, for now, contains a CCSprite. Here is the code:
//in init
CCSprite *a = [CCSprite spriteWithFile:#"a.png"];
[a setPosition:ccp(0,0)];
[self addChild:a];
Pretty straightforward, and it stumps me why after 'replaceScene' the UIButton of theHelloWorldScene.m is still visible, right on top of the Sprite. Where am I going wrong?
I'm assuming since you're adding a UIButton into a cocos2d Scene you're using the openGLView
something like:
[[[CCDirector sharedDirector] openGLView] addSubView:button];
If this is the case, then before you replace your HelloWorldScene you'll need call something similar to
[button removeFromSuperview]
Where button is the name of your UIButton (in both instances).
A suggestion though would be to use a CCMenu with a CCMenuItem on your HelloWorldScene as UIKit objects don't really mesh very well with Cocos2d.
You really aren't giving enough information. The simplest answer however is that if you do not want the button to be visible anymore then remove the child (the button). You can set a tag on the button and then use [layer getChildByTag:(NSInteger)].

UIScrollView in cocos2d

I have added the UIScrollView into the COCOS2D game engine using
[[[CCDirector sharedDirector] openGLView] addSubview: tempScrollView];.
On that scroll view I have added a image view and a buttons. My problem is that the scroll view is scrolling vertically only. I want the UIScrollView to scroll horizontally only. Also I want the images to be rotate as well. I have tried the view transform property but its not working in my case.
Does any body know how to do this in cocos2d?
Horizontaly only ? try to change contentsize. e.g.:
ccp(5000,winsize.height)