mirror of
https://github.com/FWGS/xash3d-fwgs.git
synced 2026-08-05 03:24:56 +08:00
platform: ios: Remake launch dialog using UIAlertController
This commit is contained in:
@@ -13,41 +13,22 @@
|
||||
GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#include "SDL_syswm.h"
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <UIKit/UIKit.h>
|
||||
#import <AVFoundation/AVFoundation.h>
|
||||
#include <sys/stat.h>
|
||||
#include "dlfcn.h"
|
||||
|
||||
#define XASHLIB "@rpath/libxash.dylib"
|
||||
|
||||
|
||||
@interface XashPromptAlertViewDelegate : NSObject <UIAlertViewDelegate>
|
||||
|
||||
@property (nonatomic, assign) int *button;
|
||||
|
||||
@end
|
||||
|
||||
@implementation XashPromptAlertViewDelegate
|
||||
|
||||
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
|
||||
*_button = buttonIndex;
|
||||
}
|
||||
@end
|
||||
|
||||
|
||||
|
||||
int szArgc;
|
||||
char **szArgv;
|
||||
char *g_szLibrarySuffix;
|
||||
float g_iOSVer;
|
||||
bool isdark;
|
||||
|
||||
const char *IOS_GetDocsDir(void)
|
||||
//UI vars
|
||||
UIAlertController *alert;
|
||||
UIViewController *controller;
|
||||
bool finishedDialog = false;
|
||||
|
||||
const char *IOS_GetDocsDir( void )
|
||||
{
|
||||
if( g_iOSVer >= 8.0 )
|
||||
{
|
||||
static const char *dir = NULL;
|
||||
|
||||
if( dir )
|
||||
@@ -61,23 +42,9 @@ const char *IOS_GetDocsDir(void)
|
||||
NSLog(@"IOS_GetDocsDir: %s", dir);
|
||||
|
||||
return dir;
|
||||
}
|
||||
else
|
||||
{
|
||||
static char dir[1024];
|
||||
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
|
||||
NSString *basePath = paths.firstObject;
|
||||
[[NSFileManager defaultManager] createDirectoryAtPath:basePath withIntermediateDirectories:YES attributes:nil error:nil];
|
||||
strcpy(dir,[basePath UTF8String]);
|
||||
mkdir(dir,777);
|
||||
|
||||
NSLog(@"IOS_GetDocsDir: %s", dir);
|
||||
|
||||
return dir;
|
||||
}
|
||||
}
|
||||
|
||||
const char *IOS_GetExecDir(void)
|
||||
const char *IOS_GetExecDir( void )
|
||||
{
|
||||
static const char *dir = NULL;
|
||||
|
||||
@@ -87,7 +54,7 @@ const char *IOS_GetExecDir(void)
|
||||
NSString *executableDirctory = [[NSBundle mainBundle] bundlePath];
|
||||
|
||||
dir = [executableDirctory fileSystemRepresentation];
|
||||
NSLog(@"IOS_GetDocsDir: %s", dir);
|
||||
NSLog(@"IOS_GetExecDir: %s", dir);
|
||||
|
||||
return dir;
|
||||
}
|
||||
@@ -99,43 +66,44 @@ typedef struct settings_s
|
||||
{
|
||||
unsigned char magic;
|
||||
char args[1024];
|
||||
unsigned int port;
|
||||
char suffix[32];
|
||||
unsigned int ftpserver;
|
||||
} settings_t;
|
||||
@interface ButtonHandler :NSObject
|
||||
@property (nonatomic, assign) int *button1;
|
||||
@end
|
||||
@implementation ButtonHandler
|
||||
|
||||
settings_t settings;
|
||||
FILE *settingsfile;
|
||||
char settingspath[256];
|
||||
|
||||
-(void) buttonClicked:(UIButton*)sender
|
||||
static void SaveSettings( void )
|
||||
{
|
||||
*_button1 = 0;
|
||||
if( (settingsfile = fopen( settingspath, "wb" )) )
|
||||
{
|
||||
strlcpy( settings.args, [alert.textFields.firstObject.text UTF8String], 1024 );
|
||||
settings.magic = 111;
|
||||
|
||||
fwrite( &settings, sizeof(settings), 1, settingsfile );
|
||||
fclose( settingsfile );
|
||||
}
|
||||
}
|
||||
@end
|
||||
void IOS_PrepareView(void)
|
||||
|
||||
static void IOS_PrepareView( void )
|
||||
{
|
||||
ButtonHandler *handler = [[ButtonHandler alloc] init];
|
||||
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
|
||||
UIViewController *controller = [[UIViewController alloc] init];
|
||||
[[controller view] setBackgroundColor:[UIColor grayColor]];
|
||||
controller = [[UIViewController alloc] init];
|
||||
[controller.view setBackgroundColor:[UIColor grayColor]];
|
||||
[window setRootViewController:controller];
|
||||
[window makeKeyAndVisible];
|
||||
if([[controller traitCollection] userInterfaceStyle] == UIUserInterfaceStyleDark && g_iOSVer >= 13.0) isdark = true; else isdark = false;
|
||||
#if 0
|
||||
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
|
||||
int button1 = -1;
|
||||
handler.button1 = &button1;
|
||||
alert = [UIAlertController alertControllerWithTitle:@"Xash3D FWGS"
|
||||
message:nil
|
||||
preferredStyle:UIAlertControllerStyleAlert];
|
||||
|
||||
[button addTarget:handler action:@selector(buttonClicked:) forControlEvents:UIControlEventValueChanged];
|
||||
@autoreleasepool {
|
||||
while( button1 == -1 ) {
|
||||
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
|
||||
}
|
||||
}
|
||||
[[controller view] addSubview:button];
|
||||
#endif
|
||||
UIAlertAction* startAction = [UIAlertAction actionWithTitle:@"Start" style:UIAlertActionStyleDefault
|
||||
handler:^(UIAlertAction *action) { finishedDialog = true; }];
|
||||
UIAlertAction* exitAction = [UIAlertAction actionWithTitle:@"Exit" style:UIAlertActionStyleDefault
|
||||
handler:^(UIAlertAction *action) {
|
||||
SaveSettings();
|
||||
NSLog(@"Exit Selected\n");
|
||||
exit(0); }];
|
||||
[alert addAction:startAction];
|
||||
[alert addAction:exitAction];
|
||||
}
|
||||
|
||||
void IOS_LaunchDialog( void )
|
||||
@@ -148,91 +116,48 @@ void IOS_LaunchDialog( void )
|
||||
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted){}];
|
||||
|
||||
IOS_PrepareView();
|
||||
int button = -1, bExit, bStart;
|
||||
UIAlertView * alert = [[UIAlertView alloc] init];
|
||||
bExit = [alert addButtonWithTitle:@"Exit"];
|
||||
bStart = [alert addButtonWithTitle:@"Start"];
|
||||
XashPromptAlertViewDelegate *delegate = [[XashPromptAlertViewDelegate alloc] init];
|
||||
delegate.button = &button;
|
||||
|
||||
alert.delegate = delegate;
|
||||
|
||||
const char *docsDir = IOS_GetDocsDir();
|
||||
|
||||
//set working directory to documents so logs can be generated there
|
||||
NSString *workingDir = [NSString stringWithUTF8String:IOS_GetDocsDir()];
|
||||
[[NSFileManager defaultManager] changeCurrentDirectoryPath:workingDir];
|
||||
[[NSFileManager defaultManager]
|
||||
changeCurrentDirectoryPath:[NSString stringWithUTF8String:IOS_GetDocsDir()]];
|
||||
|
||||
FILE *settingsfile;
|
||||
char settingspath[256];
|
||||
snprintf(settingspath, sizeof(settingspath), "%s/settings.bin", docsDir );
|
||||
snprintf( settingspath, sizeof( settingspath ), "%s/settings.bin", docsDir );
|
||||
settingspath[255] = 0;
|
||||
settings_t settings;
|
||||
|
||||
[alert setTransform:CGAffineTransformMakeTranslation(0,109)];
|
||||
|
||||
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
|
||||
|
||||
UILabel *argstitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 30)];
|
||||
[argstitle setText:@"Command-line arguments:"];
|
||||
|
||||
UITextField *args = [[UITextField alloc] initWithFrame:CGRectMake(0, 30, 300, 30)];
|
||||
[args setBackgroundColor:[[UIColor alloc] initWithRed:1 green:1 blue:1 alpha:1]];
|
||||
if(isdark) [args setBackgroundColor:[[UIColor alloc] initWithRed:0 green:0 blue:0 alpha:1]];
|
||||
|
||||
|
||||
UITextField *suffix = [[UITextField alloc] initWithFrame:CGRectMake(140, 90, 160, 30 )];
|
||||
[suffix setBackgroundColor:[[UIColor alloc] initWithRed:1 green:1 blue:1 alpha:1]];
|
||||
if(isdark) [suffix setBackgroundColor:[[UIColor alloc] initWithRed:0 green:0 blue:0 alpha:1]];
|
||||
|
||||
UILabel *suffixtitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, 140, 30)];
|
||||
[suffixtitle setText:@"Library suffix"];
|
||||
|
||||
[scroll addSubview:argstitle];
|
||||
[scroll addSubview:args];
|
||||
[scroll addSubview:suffix];
|
||||
[scroll addSubview:suffixtitle];
|
||||
|
||||
|
||||
settingsfile = fopen( settingspath, "rb" );
|
||||
if( settingsfile && ( fread(&settings, sizeof( settings ), 1, settingsfile ) == 1 ) && ( settings.magic == SETTINGS_MAGIC ) )
|
||||
bool ret = false;
|
||||
if ( settingsfile )
|
||||
{
|
||||
settings.args[1023] = 0;
|
||||
settings.suffix[31] = 0;
|
||||
[args setText:@(settings.args)];
|
||||
[suffix setText:@(settings.suffix)];
|
||||
}
|
||||
else
|
||||
{
|
||||
[args setText:@"-dev 2 -log"];
|
||||
ret = fread( &settings, sizeof( settings ), 1, settingsfile ) == 1;
|
||||
}
|
||||
|
||||
scroll.contentSize=CGSizeMake(250, 200);
|
||||
[alert setValue:scroll forKey:@"accessoryView"];
|
||||
|
||||
[alert show];
|
||||
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
|
||||
[textField setPlaceholder:@"Launch Options"];
|
||||
if (ret && ( settings.magic == SETTINGS_MAGIC ))
|
||||
{
|
||||
[textField setText:@(settings.args)];
|
||||
}
|
||||
else
|
||||
{
|
||||
[textField setText:@"-dev 2 -log"];
|
||||
}
|
||||
[textField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
|
||||
}];
|
||||
|
||||
[controller presentViewController:alert animated:YES completion:nil];
|
||||
|
||||
@autoreleasepool {
|
||||
while( button == -1 ) {
|
||||
while( !finishedDialog ) {
|
||||
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
|
||||
}
|
||||
}
|
||||
|
||||
if( (settingsfile = fopen( settingspath, "wb" )) )
|
||||
{
|
||||
strlcpy(settings.args, [args.text UTF8String], 1024);
|
||||
strlcpy(settings.suffix, [suffix.text UTF8String], 32 );
|
||||
settings.magic = 111;
|
||||
|
||||
fwrite(&settings, sizeof(settings), 1, settingsfile);
|
||||
fclose(settingsfile);
|
||||
}
|
||||
if( button == bExit )
|
||||
{
|
||||
printf("Exit selected\n");
|
||||
exit(0);
|
||||
}
|
||||
NSArray *argv = [alert.textFields.firstObject.text componentsSeparatedByString:@" "];
|
||||
|
||||
NSArray *argv = [ args.text componentsSeparatedByString:@" " ];
|
||||
SaveSettings();
|
||||
|
||||
int count = [argv count];
|
||||
szArgv = calloc( count + 2, sizeof( char* ) );
|
||||
@@ -244,16 +169,6 @@ void IOS_LaunchDialog( void )
|
||||
szArgc = count + 1;
|
||||
szArgv[count + 1] = 0;
|
||||
|
||||
if( [suffix.text length] )
|
||||
g_szLibrarySuffix = strdup([suffix.text UTF8String]);
|
||||
|
||||
alert.delegate = nil;
|
||||
|
||||
[args release];
|
||||
[argstitle release];
|
||||
[suffix release];
|
||||
[suffixtitle release];
|
||||
|
||||
[alert release];
|
||||
}
|
||||
|
||||
@@ -266,7 +181,7 @@ char *IOS_GetUDID( void )
|
||||
return udid;
|
||||
}
|
||||
|
||||
void IOS_Log(const char *text)
|
||||
void IOS_Log( const char *text )
|
||||
{
|
||||
NSLog(@"Xash: %@", [NSString stringWithUTF8String:text]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user