Skip to content
This repository was archived by the owner on Nov 4, 2023. It is now read-only.
81 changes: 80 additions & 1 deletion targets/ios/modules/native/iosgame.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@

//***** monkeygame.h *****

class IosAppDelegate {
public:
virtual void applicationWillResignActive(UIApplication *application) {};
virtual void applicationDidBecomeActive(UIApplication *application) {};
virtual void applicationDidEnterBackground(UIApplication *application) {};
virtual void applicationWillEnterForeground(UIApplication *application) {};
virtual void applicationWillTerminate(UIApplication *application) {};
virtual bool openURL(NSURL *url, NSString *sourceApplication) { return true; };
virtual void didReceiveLocalNotification(UILocalNotification *notification) {};
};

class BBIosGame : public BBGame{
public:
BBIosGame();
Expand All @@ -25,6 +36,10 @@ class BBIosGame : public BBGame{

virtual BBMonkeyAppDelegate *GetUIAppDelegate();

virtual void AddIosAppDelegate(IosAppDelegate *appDelegate);
virtual void RemoveIosAppDelegate(IosAppDelegate *appDelegate);
virtual NSMutableArray *GetIosAppDelegates();

//***** INTERNAL *****

virtual void StartGame();
Expand All @@ -42,6 +57,7 @@ class BBIosGame : public BBGame{

UIApplication *_app;
BBMonkeyAppDelegate *_appDelegate;
NSMutableArray *_iOSappDelegates;

bool _displayLinkAvail;
UIAccelerometer *_accelerometer;
Expand Down Expand Up @@ -79,7 +95,8 @@ _displayLink( 0 ){

_app=[UIApplication sharedApplication];
_appDelegate=(BBMonkeyAppDelegate*)[_app delegate];

_iOSappDelegates = [[NSMutableArray array] retain];

NSString *reqSysVer=@"3.1";
NSString *currSysVer=[[UIDevice currentDevice] systemVersion];
if( [currSysVer compare:reqSysVer options:NSNumericSearch]!=NSOrderedAscending ) _displayLinkAvail=true;
Expand Down Expand Up @@ -362,6 +379,18 @@ BBMonkeyAppDelegate *BBIosGame::GetUIAppDelegate(){
return _appDelegate;
}

void BBIosGame::AddIosAppDelegate(IosAppDelegate *appDelegate) {
[_iOSappDelegates addObject:[NSValue valueWithPointer:appDelegate]];
}

void BBIosGame::RemoveIosAppDelegate(IosAppDelegate *appDelegate) {
[_iOSappDelegates removeObject:[NSValue valueWithPointer:appDelegate]];
}

NSMutableArray *BBIosGame::GetIosAppDelegates() {
return _iOSappDelegates;
}

//***** INTERNAL *****

void BBIosGame::StartGame(){
Expand Down Expand Up @@ -690,6 +719,10 @@ void BBIosGame::ViewDisappeared(){
return mask;
}

-(NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window {
return UIInterfaceOrientationMaskAllButUpsideDown;
}

@end

//***** BBMonkeyAppDelegate implementation *****
Expand All @@ -700,15 +733,61 @@ void BBIosGame::ViewDisappeared(){
@synthesize view;
@synthesize viewController;
@synthesize textField;
@synthesize localNotification;

-(void)applicationWillResignActive:(UIApplication*)application{

game->SuspendGame();
for (NSValue *appDelegateValue in game->GetIosAppDelegates()) {
IosAppDelegate *appDelegate = (IosAppDelegate*)[appDelegateValue pointerValue];
appDelegate->applicationWillResignActive(application);
}
}

-(void)applicationDidBecomeActive:(UIApplication*)application{

game->ResumeGame();
for (NSValue *appDelegateValue in game->GetIosAppDelegates()) {
IosAppDelegate *appDelegate = (IosAppDelegate*)[appDelegateValue pointerValue];
appDelegate->applicationDidBecomeActive(application);
}
}

-(void)applicationDidEnterBackground:(UIApplication*)application{
for (NSValue *appDelegateValue in game->GetIosAppDelegates()) {
IosAppDelegate *appDelegate = (IosAppDelegate*)[appDelegateValue pointerValue];
appDelegate->applicationDidEnterBackground(application);
}
}

-(void)applicationWillEnterForeground:(UIApplication*)application{
for (NSValue *appDelegateValue in game->GetIosAppDelegates()) {
IosAppDelegate *appDelegate = (IosAppDelegate*)[appDelegateValue pointerValue];
appDelegate->applicationWillEnterForeground(application);
}
}

-(void)applicationWillTerminate:(UIApplication*)application{
for (NSValue *appDelegateValue in game->GetIosAppDelegates()) {
IosAppDelegate *appDelegate = (IosAppDelegate*)[appDelegateValue pointerValue];
appDelegate->applicationWillTerminate(application);
}
}

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
bool returnValue = NO;
for (NSValue *appDelegateValue in game->GetIosAppDelegates()) {
IosAppDelegate *appDelegate = (IosAppDelegate*)[appDelegateValue pointerValue];
returnValue |= appDelegate->openURL(url, sourceApplication);
}
return returnValue;
}

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
for (NSValue *appDelegateValue in game->GetIosAppDelegates()) {
IosAppDelegate *appDelegate = (IosAppDelegate*)[appDelegateValue pointerValue];
appDelegate->didReceiveLocalNotification(notification);
}
}

-(BOOL)textFieldShouldEndEditing:(UITextField*)textField{
Expand Down
8 changes: 8 additions & 0 deletions targets/ios/modules/native/monkeytarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ class BBMonkeyGame : public BBIosGame{

[_window makeKeyAndVisible];

Class cls = NSClassFromString(@"UILocalNotification");
if (cls) {
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
self.localNotification = notification;
}
}

return YES;
}

Expand Down
2 changes: 2 additions & 0 deletions targets/ios/template/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class BBIosGame;
MonkeyViewController *viewController;
UITextField *textField;
int textFieldState;
UILocalNotification *localNotification;
}
-(void)applicationWillResignActive:(UIApplication*)application;
-(void)applicationDidBecomeActive:(UIApplication*)application;
Expand All @@ -108,6 +109,7 @@ class BBIosGame;
@property (nonatomic, retain) IBOutlet MonkeyView *view;
@property (nonatomic, retain) IBOutlet MonkeyViewController *viewController;
@property (nonatomic, retain) IBOutlet UITextField *textField;
@property (nonatomic, retain) IBOutlet UILocalNotification *localNotification;

@end

Expand Down