SettingsTableViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //
  2. // Settings.m
  3. // SDLPal
  4. //
  5. // Created by palxex on 2017/5/18.
  6. // Copyright © 2017年 SDLPAL team. All rights reserved.
  7. //
  8. #import "SettingsTableViewController.h"
  9. #import "SDLPal_AppDelegate.h"
  10. #import <ActionSheetPicker-3.0/ActionSheetStringPicker.h>
  11. #include "palcfg.h"
  12. #define UIKitLocalizedString(key) [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] localizedStringForKey:key value:@"" table:nil]
  13. @implementation SettingsTableViewController {
  14. NSArray *AudioSampleRates;
  15. NSArray *AudioBufferSizes;
  16. NSArray *OPLSampleRates;
  17. NSArray *CDFormats;
  18. NSArray *MusicFormats;
  19. NSArray *OPLFormats;
  20. NSArray *LogLevels;
  21. NSArray *allFiles;
  22. NSMutableArray *AvailFiles;
  23. BOOL checkAllFilesIncluded;
  24. NSString *resourceStatus;
  25. AbstractActionSheetPicker *picker;
  26. IBOutlet UIView *transitionView;
  27. IBOutlet UILabel *lblResourceStatus;
  28. IBOutlet UILabel *lblLanguageFile;
  29. IBOutlet UILabel *lblFontFile;
  30. IBOutlet UISwitch *toggleTouchScreenOverlay;
  31. IBOutlet UISwitch *toggleKeepAspect;
  32. IBOutlet UISwitch *toggleSmoothScaling;
  33. IBOutlet UILabel *lblMusicType;
  34. IBOutlet UILabel *lblOPLType;
  35. IBOutlet UILabel *lblOPLRate;
  36. IBOutlet UILabel *lblCDAudioSource;
  37. IBOutlet UISwitch *toggleStereo;
  38. IBOutlet UISwitch *toggleSurroundOPL;
  39. IBOutlet UILabel *lblResampleRate;
  40. IBOutlet UILabel *lblAudioBufferSize;
  41. IBOutlet UISlider *sliderResampleQuality;
  42. IBOutlet UISlider *sliderMusicVolume;
  43. IBOutlet UISlider *sliderSFXVolume;
  44. IBOutlet UILabel *lblLogLevel;
  45. IBOutlet UITextField *textLogFile;
  46. }
  47. - (BOOL)includedInList:(NSArray*)array name:(NSString *)filename {
  48. for( NSString *item in array ) {
  49. if( [filename caseInsensitiveCompare:item] == NSOrderedSame )
  50. return YES;
  51. }
  52. return NO;
  53. }
  54. - (void)viewDidLoad {
  55. [super viewDidLoad];
  56. [transitionView setFrame:self.view.frame];
  57. UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 40)];
  58. tlabel.text=[NSString stringWithUTF8String:PAL_GIT_REVISION];
  59. tlabel.backgroundColor =[UIColor clearColor];
  60. tlabel.adjustsFontSizeToFitWidth=YES;
  61. self.navigationItem.titleView=tlabel;
  62. AudioSampleRates = @[ @"11025", @"22050", @"44100", @"49716" ];
  63. AudioBufferSizes = @[ @"512", @"1024", @"2048", @"4096", @"8192" ];
  64. OPLSampleRates = @[ @"12429", @"24858", @"49716", @"11025", @"22050", @"44100" ];
  65. CDFormats = @[ @"MP3", @"OGG" ];
  66. MusicFormats = @[ @"MIDI", @"RIX", @"MP3", @"OGG" ];
  67. OPLFormats = @[ @"DOSBOX", @"MAME", @"DOSBOXNEW" ];
  68. LogLevels = @[ @"VERBOSE", @"DEBUG", @"INFO", @"WARNING", @"ERROR", @"FATAL" ];
  69. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
  70. tap.cancelsTouchesInView = NO;
  71. [self.view addGestureRecognizer:tap];
  72. UIRefreshControl *refreshController = [[UIRefreshControl alloc] init];
  73. [refreshController addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
  74. [self.tableView addSubview:refreshController];
  75. [self recheckSharingFolder];
  76. [self readConfigs];
  77. }
  78. -(void)dismissKeyboard
  79. {
  80. [self.view endEditing:YES];
  81. }
  82. -(void)handleRefresh : (id)sender
  83. {
  84. UIRefreshControl *refreshController = sender;
  85. [self recheckSharingFolder];
  86. [refreshController endRefreshing];
  87. }
  88. - (void)recheckSharingFolder {
  89. AvailFiles = [NSMutableArray new];
  90. NSArray *builtinList = @[ @"wor16.fon", @"wor16.asc", @"m.msg"];
  91. NSArray *builtinExtensionList = @[@"exe",@"drv",@"dll",@"rpg",@"mkf",@"avi",@"dat",@"cfg",@"ini"];
  92. allFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSString stringWithUTF8String:UTIL_BasePath()] error:nil];
  93. for( NSString *filename in allFiles ) {
  94. if( ![self includedInList:builtinExtensionList name:filename.pathExtension] &&
  95. ![self includedInList:builtinList name:filename] ) {
  96. [AvailFiles addObject:filename];
  97. }
  98. }
  99. checkAllFilesIncluded = YES;
  100. for( NSString *checkFile in @[@"abc.mkf", @"ball.mkf", @"data.mkf", @"f.mkf", @"fbp.mkf", @"fire.mkf", @"gop.mkf", @"m.msg", @"map.mkf", @"mgo.mkf", @"rgm.mkf", @"rng.mkf", @"sss.mkf", @"word.dat"] ) {
  101. if( ![self includedInList:allFiles name:checkFile] ) {
  102. checkAllFilesIncluded = NO;
  103. break;
  104. }
  105. }
  106. if(!resourceStatus) resourceStatus = lblResourceStatus.text;
  107. lblResourceStatus.text = [NSString stringWithFormat:@"%@%@", resourceStatus, checkAllFilesIncluded ? @"✅" : @"❌" ];
  108. }
  109. typedef void(^SelectedBlock)(NSString *selected);
  110. - (void)showPickerWithTitle:(NSString *)title toLabel:(UILabel*)label inArray:(NSArray*)array {
  111. [self showPickerWithTitle:title toLabel:label inArray:array origin:self.navigationController.navigationBar allowEmpty:NO];
  112. }
  113. - (void)showPickerWithTitle:(NSString *)title toLabel:(UILabel*)label inArray:(NSArray*)array origin:(UIView*)origin {
  114. [self showPickerWithTitle:title toLabel:label inArray:array origin:origin allowEmpty:NO];
  115. }
  116. - (void)showPickerWithTitle:(NSString *)title toLabel:(UILabel*)label inArray:(NSArray*)array origin:(UIView*)origin allowEmpty:(BOOL)allowEmpty {
  117. [self showPickerWithTitle:title toLabel:label inArray:array origin:origin allowEmpty:allowEmpty doneBlock:nil];
  118. }
  119. - (void)showPickerWithTitle:(NSString *)title toLabel:(UILabel*)label inArray:(NSArray*)array origin:(UIView*)origin allowEmpty:(BOOL)allowEmpty doneBlock:(SelectedBlock)doneBlock {
  120. array = allowEmpty ? [array arrayByAddingObject:@""] : array;
  121. picker = [ActionSheetStringPicker showPickerWithTitle:nil
  122. rows:array
  123. initialSelection:[array containsObject:label.text] ? [array indexOfObject:label.text] : 0
  124. doneBlock:^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
  125. label.text = array[selectedIndex];
  126. if(doneBlock) doneBlock(label.text);
  127. }
  128. cancelBlock:nil
  129. origin:origin];
  130. }
  131. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  132. //need manually sync with storyboard...
  133. int rows = 1;
  134. switch(section) {
  135. case 0:
  136. rows = 1;
  137. break;
  138. case 1:
  139. rows = 2;
  140. break;
  141. case 2:
  142. rows = 3;
  143. break;
  144. case 3:
  145. rows = [lblMusicType.text isEqualToString:@"RIX"] ? 11 : 4;
  146. break;
  147. case 4:
  148. rows = 2;
  149. break;
  150. default:
  151. break;
  152. }
  153. return rows;
  154. }
  155. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  156. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  157. if( indexPath.section == 1 && indexPath.row == 0 ) { //language file
  158. [self showPickerWithTitle:nil toLabel:lblLanguageFile inArray:AvailFiles origin:cell allowEmpty:YES];
  159. }else if( indexPath.section == 1 && indexPath.row == 1 ) { //font file
  160. [self showPickerWithTitle:nil toLabel:lblFontFile inArray:AvailFiles origin:cell allowEmpty:YES];
  161. }else if( indexPath.section == 2 && indexPath.row == 0 ) { //touch overlay
  162. toggleTouchScreenOverlay.on = !toggleTouchScreenOverlay.isOn;
  163. }else if( indexPath.section == 2 && indexPath.row == 1 ) { //keep aspect
  164. toggleKeepAspect.on = !toggleKeepAspect.isOn;
  165. }else if( indexPath.section == 2 && indexPath.row == 2 ) { //smooth scaling
  166. toggleSmoothScaling.on = !toggleSmoothScaling.isOn;
  167. }else if( indexPath.section == 3 && indexPath.row == 0 ) { //BGM
  168. [self showPickerWithTitle:nil toLabel:lblMusicType inArray:MusicFormats origin:cell allowEmpty:NO doneBlock:^(NSString *selected) {
  169. [self.tableView reloadData];
  170. }];
  171. }else if( indexPath.section == 3 && indexPath.row == 4 ) { //OPL Type
  172. [self showPickerWithTitle:nil toLabel:lblOPLType inArray:OPLFormats origin:cell];
  173. }else if( indexPath.section == 3 && indexPath.row == 5 ) { //OPL Rate
  174. [self showPickerWithTitle:nil toLabel:lblOPLRate inArray:OPLSampleRates origin:cell];
  175. }else if( indexPath.section == 3 && indexPath.row == 1 ) { //CD Source
  176. [self showPickerWithTitle:nil toLabel:lblCDAudioSource inArray:CDFormats origin:cell];
  177. }else if( indexPath.section == 3 && indexPath.row == 8 ) { //Stereo
  178. toggleStereo.on = !toggleStereo.isOn;
  179. }else if( indexPath.section == 3 && indexPath.row == 9 ) { //Surround
  180. toggleSurroundOPL.on = !toggleSurroundOPL.isOn;
  181. }else if( indexPath.section == 3 && indexPath.row == 6 ) { //SampleRate
  182. [self showPickerWithTitle:nil toLabel:lblResampleRate inArray:AudioSampleRates origin:cell];
  183. }else if( indexPath.section == 3 && indexPath.row == 7 ) { //Buffer size
  184. [self showPickerWithTitle:nil toLabel:lblAudioBufferSize inArray:AudioBufferSizes origin:cell];
  185. }else if( indexPath.section == 4 && indexPath.row == 0 ) { //Log Level
  186. [self showPickerWithTitle:nil toLabel:lblLogLevel inArray:LogLevels origin:cell];
  187. }
  188. }
  189. - (IBAction)btnDefaultClicked:(id)sender {
  190. PAL_LoadConfig(NO);
  191. [self readConfigs];
  192. }
  193. - (IBAction)btnConfirmClicked:(id)sender {
  194. if(!checkAllFilesIncluded){
  195. UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Cannot find data file in the iTunes File Sharing directory",nil)
  196. message:NSLocalizedString(@"NOTE: For copyright reasons data files required to run the game are NOT included.",nil)
  197. preferredStyle:UIAlertControllerStyleAlert];
  198. UIAlertAction *cacelAction = [UIAlertAction actionWithTitle:UIKitLocalizedString(@"OK") style:UIAlertActionStyleDefault handler:nil];
  199. [alert addAction:cacelAction];
  200. [self presentViewController:alert animated:YES completion:nil];
  201. return;
  202. }
  203. [UIView animateWithDuration:0.65
  204. delay:0.0
  205. usingSpringWithDamping:1.0
  206. initialSpringVelocity:0.0
  207. options:UIViewAnimationOptionCurveEaseInOut
  208. animations:^{
  209. CGPoint point = self.navigationController.view.center;
  210. point.y += self.navigationController.view.frame.size.height;
  211. [self.navigationController.view setCenter:point];
  212. } completion:^(BOOL finished) {
  213. [self saveConfigs];
  214. gConfig.fLaunchSetting = NO;
  215. PAL_SaveConfig();
  216. [[SDLPalAppDelegate sharedAppDelegate] launchGame];
  217. }];
  218. }
  219. - (void)readConfigs {
  220. gConfig.fFullScreen = YES; //iOS specific; need this to make sure statusbar hidden in game completely
  221. lblLanguageFile.text = [NSString stringWithUTF8String:gConfig.pszMsgFile ? gConfig.pszMsgFile : ""];
  222. lblFontFile.text = [NSString stringWithUTF8String:gConfig.pszFontFile ? gConfig.pszFontFile : ""];
  223. textLogFile.text = [NSString stringWithUTF8String:gConfig.pszLogFile ? gConfig.pszLogFile : ""];
  224. toggleStereo.on = gConfig.iAudioChannels == 2;
  225. toggleSurroundOPL.on = gConfig.fUseSurroundOPL;
  226. toggleTouchScreenOverlay.on = gConfig.fUseTouchOverlay;
  227. toggleKeepAspect.on = gConfig.fKeepAspectRatio;
  228. toggleSmoothScaling.on = gConfig.pszScaleQuality ? strncmp(gConfig.pszScaleQuality, "0", sizeof(char)) != 0 : NO;
  229. lblMusicType.text = MusicFormats[gConfig.eMusicType];
  230. lblOPLType.text = OPLFormats[gConfig.eOPLType];
  231. lblOPLRate.text = [NSString stringWithFormat:@"%d",gConfig.iOPLSampleRate];
  232. lblCDAudioSource.text = CDFormats[gConfig.eCDType-MUSIC_OGG];
  233. lblResampleRate.text = [NSString stringWithFormat:@"%d",gConfig.iSampleRate];
  234. lblAudioBufferSize.text = [NSString stringWithFormat:@"%d",gConfig.wAudioBufferSize];
  235. sliderMusicVolume.value = gConfig.iMusicVolume;
  236. sliderSFXVolume.value = gConfig.iSoundVolume;
  237. sliderResampleQuality.value = gConfig.iResampleQuality;
  238. lblLogLevel.text = LogLevels[gConfig.iLogLevel];
  239. [self.tableView reloadData];
  240. }
  241. - (void)saveConfigs {
  242. gConfig.pszMsgFile = [lblLanguageFile.text length] == 0 ? NULL : strdup([[lblLanguageFile text] UTF8String]);
  243. gConfig.pszFontFile = [lblLanguageFile.text length] == 0 ? NULL : strdup([[lblFontFile text] UTF8String]);
  244. gConfig.pszLogFile = [lblLanguageFile.text length] == 0 ? NULL : strdup([[textLogFile text] UTF8String]);
  245. gConfig.iAudioChannels = toggleStereo.isOn ? 2 : 1;
  246. gConfig.fUseSurroundOPL = toggleSurroundOPL.isOn;
  247. gConfig.fKeepAspectRatio = toggleKeepAspect.isOn;
  248. gConfig.fUseTouchOverlay = toggleTouchScreenOverlay.isOn;
  249. gConfig.pszScaleQuality = strdup(toggleSmoothScaling.on ? "1" : "0");
  250. gConfig.eMusicType = (MUSICTYPE)[MusicFormats indexOfObject:lblMusicType.text];
  251. gConfig.eOPLType = (OPLTYPE )[OPLFormats indexOfObject:lblOPLType.text];
  252. gConfig.iOPLSampleRate = [lblOPLRate.text intValue];
  253. gConfig.eCDType = (MUSICTYPE)[CDFormats indexOfObject:lblCDAudioSource.text]+MUSIC_OGG;
  254. gConfig.iSampleRate = [lblResampleRate.text intValue];
  255. gConfig.wAudioBufferSize = [lblAudioBufferSize.text intValue];
  256. gConfig.iMusicVolume = sliderMusicVolume.value;
  257. gConfig.iSoundVolume = sliderSFXVolume.value;
  258. gConfig.iResampleQuality = sliderResampleQuality.value;
  259. gConfig.iLogLevel = (LOGLEVEL)[LogLevels indexOfObject:lblLogLevel.text];
  260. }
  261. @end