Add app pausing and focus management to iOS

This commit is contained in:
Wojtek Figat
2023-06-10 23:26:40 +02:00
parent 621d408078
commit 8cf10bf71d
2 changed files with 39 additions and 10 deletions

View File

@@ -72,6 +72,8 @@ FlaxView* MainView = nullptr;
FlaxViewController* MainViewController = nullptr;
iOSWindow* MainWindow = nullptr;
iOSTouchScreen* TouchScreen;
bool HasFocus = true;
bool IsPaused = false;
struct MessagePipeline
{
@@ -243,7 +245,7 @@ MessagePipeline MainThreadPipeline;
UIThreadPipeline.Run();
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
// Create window
CGRect frame = [[UIScreen mainScreen] bounds];
@@ -289,25 +291,40 @@ MessagePipeline MainThreadPipeline;
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
- (void)applicationWillResignActive:(UIApplication*)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
LOG(Info, "[iOS] applicationWillResignActive");
// Defocus app
HasFocus = false;
if (MainWindow)
MainWindow->OnLostFocus();
}
- (void)applicationDidEnterBackground:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication*)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
LOG(Info, "[iOS] applicationDidEnterBackground");
// Pause
IsPaused = true;
}
- (void)applicationWillEnterForeground:(UIApplication *)application
- (void)applicationWillEnterForeground:(UIApplication*)application
{
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
LOG(Info, "[iOS] applicationWillEnterForeground");
// Resume
IsPaused = false;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
- (void)applicationDidBecomeActive:(UIApplication*)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
LOG(Info, "[iOS] applicationDidBecomeActive");
// Focus app
HasFocus = true;
if (MainWindow)
MainWindow->OnGotFocus();
}
@end
@@ -563,6 +580,16 @@ String iOSPlatform::GetComputerName()
return TEXT("iPhone");
}
bool iOSPlatform::GetHasFocus()
{
return HasFocus;
}
bool iOSPlatform::GetIsPaused()
{
return IsPaused;
}
Float2 iOSPlatform::GetDesktopSize()
{
CGRect frame = [[UIScreen mainScreen] bounds];

View File

@@ -29,6 +29,8 @@ public:
static ScreenOrientationType GetScreenOrientationType();
static Guid GetUniqueDeviceId();
static String GetComputerName();
static bool GetHasFocus();
static bool GetIsPaused();
static Float2 GetDesktopSize();
static String GetMainDirectory();
static Window* CreateWindow(const CreateWindowSettings& settings);