[SOLVED] [iPhone] New UIView / UIViewController for input

Problems building or running the engine, queries about how to use features etc.
Post Reply
User avatar
sphere
Gnoblar
Posts: 10
Joined: Thu Mar 20, 2008 9:22 am
Location: Melbourne, Australia

[SOLVED] [iPhone] New UIView / UIViewController for input

Post by sphere »

Hi all,

What I'm attempting to do is create a custom UIViewController that I can use to capture and handle touch/movement events instead of relying on OIS (as has been advised in the forums), and I've looked at the following tutorials to learn about touch events:
http://www.iphonesdkarticles.com/2008/0 ... orial.html (1)
http://chris-software.com/index.php/200 ... matically/ (2)

What I've managed to do so far (in a NON-Ogre project) is create a new UIViewController programatically (link 2) and detect the events and output particulars (for debug) to the console (link 1). I've implemented the very basic code into my Ogre app (based on the iPhone templates), and the new view is created (it's set to be yellow for now so that I can see it; it'll be set to transparent later), but no matter where I create it in the AppDelegate's appDidFinishLaunching method, Ogre always renders over the top and my touch events aren't detected.

What I'm needing to know is where/how should I be creating the new subView so that it'll detect touch events on top of Ogre's rendering view, and/or is there a way I can pump through the touch events to all the views?

InputViewController.h

Code: Select all

#import <UIKit/UIKit.h>
@interface InputViewController : UIViewController
  {}
@end

InputViewController.m

Code: Select all

#import "InputViewController.h"

@implementation InputViewController

-(void)loadView
{
	NSLog(@"Test Load View");
	self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
	self.view.backgroundColor = [UIColor yellowColor];
	[self.view release];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
	NSLog(@"Test touch-began");
}


main.cpp, in AppDelegate declaration and implementation

Code: Select all


@interface AkuEngineAppDelegate : NSObject <UIApplicationDelegate>
{
	UIWindow *window;
	InputViewController *inputController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

- (void)go;

@end

~~~~~~~~~~~~~~~~~~~~


- (void)applicationDidFinishLaunching:(UIApplication *)application
{
	[[UIApplication sharedApplication] setStatusBarHidden:YES];  // Hide the status bar
	window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  // Create a window

	inputController = [InputViewController alloc];  // My code
	[window addSubview:inputController.view];  // My code
	
	[window makeKeyAndVisible];  // Display our window
    
	[NSThread detachNewThreadSelector:@selector(go) toTarget:self withObject:nil];	
}
Last edited by sphere on Wed Mar 10, 2010 12:34 pm, edited 1 time in total.

User avatar
masterfalcon
OGRE Team Member
OGRE Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126
Contact:

Re: [iPhone] New UIView / UIViewController for input

Post by masterfalcon »

Try using UIView instead of UIViewController. While UIViewController also inherits from UIResponder(where the touch handling methods are declared) its purpose is to control multiple views. Transitioning from one to another for example. While the view itself should be the one to handle the touch events. If you run into problems here are some other methods that may help but might not be necessary.

Code: Select all

    [myView setMultipleTouchEnabled:YES];
    [myView setExclusiveTouch:YES];
    [myView becomeFirstResponder];

User avatar
sphere
Gnoblar
Posts: 10
Joined: Thu Mar 20, 2008 9:22 am
Location: Melbourne, Australia

Re: [iPhone] New UIView / UIViewController for input

Post by sphere »

I changed the custom class to inherit from UIView and changed the appDidLoad method to the following, but still no luck (I've left class/variable names the same as before for consistency):

Code: Select all

...
	inputController = [[inputViewController alloc] initWithFrame:CGRectMake(0, 0, 320, 480 )];
	[inputController becomeFirstResponder];
	[inputController setMultipleTouchEnabled:YES];
	[inputController setExclusiveTouch:YES];
	
	[window addSubview:inputController];
	[window bringSubviewToFront:inputController];

	[window makeKeyAndVisible];
	
	[NSThread detachNewThreadSelector:@selector(go) toTarget:self withObject:nil];
...

Again, I still see it load up, but once my game loop starts it's covered over. (I also commented out the last line - [NSThread ...] - and confirmed that the touches *are* being detected.) I've also tried using 'bringSubviewToFront:' after I initialise Ogre to make sure it's put on top of any other views it might create, but again, it had no affect.

Any other ideas?

User avatar
masterfalcon
OGRE Team Member
OGRE Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126
Contact:

Re: [iPhone] New UIView / UIViewController for input

Post by masterfalcon »

Instead of creating a new window, try attaching your UIView to the window created by Ogre. You can get a pointer to it with this code:

Code: Select all

UIWindow *mUIWindow = nil;
RenderWindow *mWindow = Ogre::Root::getSingleton().getAutoCreatedWindow();
mWindow->getCustomAttribute("WINDOW", &mUIWindow);
Then add your view using the mUIWindow pointer.

User avatar
sphere
Gnoblar
Posts: 10
Joined: Thu Mar 20, 2008 9:22 am
Location: Melbourne, Australia

Re: [iPhone] New UIView / UIViewController for input

Post by sphere »

Oh, of course. I was getting confused by that UIWindow in the appDelegate and thought that it was *the* window.

Anyway, that code did the trick, so many thanks!

(To anyone else that's reading this with the same problems, you should be able to patch together my code from above, and keep in mind that this view will need to be added AFTER you initialise Ogre - It took me a few minutes to realise that ;) ).

Thanks again, Master F.

User avatar
DanielSefton
Ogre Magi
Posts: 1235
Joined: Fri Oct 26, 2007 12:36 am
Location: Mountain View, CA
x 10
Contact:

Re: [SOLVED] [iPhone] New UIView / UIViewController for input

Post by DanielSefton »

Question: The XCode templates allocs a window at the very beginning to display the loading view. If I want to do the same as suggested above, would I need to release the loading window, set it to nil and then pass it through getCustomAttribute? (Because Ogre is initialised much later.) Well, I've tried it and it causes a period of blackness. Just can't think of a better solution.

Edit: Nevermind, all is good now, having a window exclusively for loading is fine. :P

Post Reply