SettingsTableViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. @implementation SettingsTableViewController {
  13. NSArray *AudioSampleRates;
  14. NSArray *AudioBufferSizes;
  15. NSArray *OPLSampleRates;
  16. NSArray *CDFormats;
  17. NSArray *MusicFormats;
  18. NSArray *OPLFormats;
  19. NSArray *LogLevels;
  20. NSArray *allFiles;
  21. NSMutableArray *AvailFiles;
  22. AbstractActionSheetPicker *picker;
  23. IBOutlet UIView *transitionView;
  24. IBOutlet UILabel *lblResourceStatus;
  25. IBOutlet UILabel *lblLanguageFile;
  26. IBOutlet UILabel *lblFontFile;
  27. IBOutlet UISwitch *toggleTouchScreenOverlay;
  28. IBOutlet UISwitch *toggleKeepAspect;
  29. IBOutlet UILabel *lblMusicType;
  30. IBOutlet UILabel *lblOPLType;
  31. IBOutlet UILabel *lblOPLRate;
  32. IBOutlet UILabel *lblCDAudioSource;
  33. IBOutlet UISwitch *toggleStereo;
  34. IBOutlet UISwitch *toggleSurroundOPL;
  35. IBOutlet UILabel *lblResampleRate;
  36. IBOutlet UILabel *lblAudioBufferSize;
  37. IBOutlet UISlider *sliderResampleQuality;
  38. IBOutlet UISlider *sliderMusicVolume;
  39. IBOutlet UISlider *sliderSFXVolume;
  40. IBOutlet UILabel *lblLogLevel;
  41. IBOutlet UITextField *textLogFile;
  42. }
  43. - (BOOL)includedInList:(NSArray*)array name:(NSString *)filename {
  44. for( NSString *item in array ) {
  45. if( [filename caseInsensitiveCompare:item] == NSOrderedSame )
  46. return YES;
  47. }
  48. return NO;
  49. }
  50. - (void)viewDidLoad {
  51. [super viewDidLoad];
  52. UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 40)];
  53. tlabel.text=[NSString stringWithUTF8String:PAL_GIT_REVISION];
  54. tlabel.backgroundColor =[UIColor clearColor];
  55. tlabel.adjustsFontSizeToFitWidth=YES;
  56. self.navigationItem.titleView=tlabel;
  57. AudioSampleRates = @[ @"11025", @"22050", @"44100", @"49716" ];
  58. AudioBufferSizes = @[ @"512", @"1024", @"2048", @"4096", @"8192" ];
  59. OPLSampleRates = @[ @"12429", @"24858", @"44100", @"49716" ];
  60. CDFormats = @[ @"MP3", @"OGG" ];
  61. MusicFormats = @[ @"MIDI", @"RIX", @"MP3", @"OGG" ];
  62. OPLFormats = @[ @"DOSBOX", @"MAME", @"DOSBOXNEW" ];
  63. LogLevels = @[ @"VERBOSE", @"DEBUG", @"INFO", @"WARNING", @"ERROR", @"FATAL" ];
  64. AvailFiles = [NSMutableArray new];
  65. NSArray *builtinList = @[ @"wor16.fon", @"wor16.asc", @"m.msg"];
  66. NSArray *builtinExtensionList = @[@"exe",@"drv",@"dll",@"rpg",@"mkf",@"avi",@"dat",@"cfg",@"ini"];
  67. allFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSString stringWithUTF8String:UTIL_BasePath()] error:nil];
  68. for( NSString *filename in allFiles ) {
  69. if( ![self includedInList:builtinExtensionList name:filename.pathExtension] &&
  70. ![self includedInList:builtinList name:filename] ) {
  71. [AvailFiles addObject:filename];
  72. }
  73. }
  74. [self readConfigs];
  75. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
  76. tap.cancelsTouchesInView = NO;
  77. [self.view addGestureRecognizer:tap];
  78. }
  79. -(void)dismissKeyboard
  80. {
  81. [self.view endEditing:YES];
  82. }
  83. typedef void(^SelectedBlock)(NSString *selected);
  84. - (void)showPickerWithTitle:(NSString *)title toLabel:(UILabel*)label inArray:(NSArray*)array {
  85. [self showPickerWithTitle:title toLabel:label inArray:array origin:self.navigationController.navigationBar allowEmpty:NO];
  86. }
  87. - (void)showPickerWithTitle:(NSString *)title toLabel:(UILabel*)label inArray:(NSArray*)array origin:(UIView*)origin {
  88. [self showPickerWithTitle:title toLabel:label inArray:array origin:origin allowEmpty:NO];
  89. }
  90. - (void)showPickerWithTitle:(NSString *)title toLabel:(UILabel*)label inArray:(NSArray*)array origin:(UIView*)origin allowEmpty:(BOOL)allowEmpty {
  91. [self showPickerWithTitle:title toLabel:label inArray:array origin:origin allowEmpty:allowEmpty doneBlock:nil];
  92. }
  93. - (void)showPickerWithTitle:(NSString *)title toLabel:(UILabel*)label inArray:(NSArray*)array origin:(UIView*)origin allowEmpty:(BOOL)allowEmpty doneBlock:(SelectedBlock)doneBlock {
  94. array = allowEmpty ? [array arrayByAddingObject:@""] : array;
  95. picker = [ActionSheetStringPicker showPickerWithTitle:nil
  96. rows:array
  97. initialSelection:[array containsObject:label.text] ? [array indexOfObject:label.text] : 0
  98. doneBlock:^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
  99. label.text = array[selectedIndex];
  100. if(doneBlock) doneBlock(label.text);
  101. }
  102. cancelBlock:nil
  103. origin:origin];
  104. }
  105. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  106. //need manually sync with storyboard...
  107. int rows = 1;
  108. switch(section) {
  109. case 0:
  110. rows = 1;
  111. break;
  112. case 1:
  113. rows = 2;
  114. break;
  115. case 2:
  116. rows = 2;
  117. break;
  118. case 3:
  119. rows = [lblMusicType.text isEqualToString:@"RIX"] ? 11 : 4;
  120. break;
  121. case 4:
  122. rows = 2;
  123. break;
  124. default:
  125. break;
  126. }
  127. return rows;
  128. }
  129. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  130. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  131. if( indexPath.section == 1 && indexPath.row == 0 ) { //language file
  132. [self showPickerWithTitle:nil toLabel:lblLanguageFile inArray:AvailFiles origin:cell allowEmpty:YES];
  133. }else if( indexPath.section == 1 && indexPath.row == 1 ) { //font file
  134. [self showPickerWithTitle:nil toLabel:lblFontFile inArray:AvailFiles origin:cell allowEmpty:YES];
  135. }else if( indexPath.section == 3 && indexPath.row == 0 ) { //BGM
  136. [self showPickerWithTitle:nil toLabel:lblMusicType inArray:MusicFormats origin:cell allowEmpty:NO doneBlock:^(NSString *selected) {
  137. [self.tableView reloadData];
  138. }];
  139. }else if( indexPath.section == 3 && indexPath.row == 4 ) { //OPL Type
  140. [self showPickerWithTitle:nil toLabel:lblOPLType inArray:OPLFormats origin:cell];
  141. }else if( indexPath.section == 3 && indexPath.row == 5 ) { //OPL Rate
  142. [self showPickerWithTitle:nil toLabel:lblOPLRate inArray:OPLSampleRates origin:cell];
  143. }else if( indexPath.section == 3 && indexPath.row == 1 ) { //CD Source
  144. [self showPickerWithTitle:nil toLabel:lblCDAudioSource inArray:CDFormats origin:cell];
  145. }else if( indexPath.section == 3 && indexPath.row == 8 ) { //Stereo
  146. toggleStereo.on = !toggleStereo.isOn;
  147. }else if( indexPath.section == 3 && indexPath.row == 9 ) { //Surround
  148. toggleSurroundOPL.on = !toggleSurroundOPL.isOn;
  149. }else if( indexPath.section == 3 && indexPath.row == 6 ) { //SampleRate
  150. [self showPickerWithTitle:nil toLabel:lblResampleRate inArray:AudioSampleRates origin:cell];
  151. }else if( indexPath.section == 3 && indexPath.row == 7 ) { //Buffer size
  152. [self showPickerWithTitle:nil toLabel:lblAudioBufferSize inArray:AudioBufferSizes origin:cell];
  153. }else if( indexPath.section == 4 && indexPath.row == 0 ) { //Log Level
  154. [self showPickerWithTitle:nil toLabel:lblLogLevel inArray:LogLevels origin:cell];
  155. }
  156. }
  157. - (IBAction)btnDefaultClicked:(id)sender {
  158. PAL_LoadConfig(NO);
  159. [self readConfigs];
  160. }
  161. - (IBAction)btnConfirmClicked:(id)sender {
  162. [UIView transitionFromView:[self.navigationController view]
  163. toView:transitionView
  164. duration:0.65f
  165. options:UIViewAnimationOptionTransitionCurlDown
  166. completion:^(BOOL finished){
  167. [self saveConfigs];
  168. gConfig.fLaunchSetting = NO;
  169. PAL_SaveConfig();
  170. [[SDLPalAppDelegate sharedAppDelegate] launchGame];
  171. }];
  172. }
  173. - (void)readConfigs {
  174. gConfig.fFullScreen = YES; //iOS specific; need this to make sure statusbar hidden in game completely
  175. lblResourceStatus.text = [NSString stringWithFormat:@"%@%@",lblResourceStatus.text, [self includedInList:allFiles name:@"fbp.mkf"] ? @"✅" : @"❌" ];
  176. lblLanguageFile.text = [NSString stringWithUTF8String:gConfig.pszMsgFile ? gConfig.pszMsgFile : ""];
  177. lblFontFile.text = [NSString stringWithUTF8String:gConfig.pszFontFile ? gConfig.pszFontFile : ""];
  178. textLogFile.text = [NSString stringWithUTF8String:gConfig.pszLogFile ? gConfig.pszLogFile : ""];
  179. toggleStereo.on = gConfig.iAudioChannels == 2;
  180. toggleSurroundOPL.on = gConfig.fUseSurroundOPL;
  181. lblMusicType.text = MusicFormats[gConfig.eMusicType];
  182. lblOPLType.text = OPLFormats[gConfig.eOPLType];
  183. lblOPLRate.text = [NSString stringWithFormat:@"%d",gConfig.iOPLSampleRate];
  184. lblCDAudioSource.text = CDFormats[gConfig.eCDType-MUSIC_OGG];
  185. lblResampleRate.text = [NSString stringWithFormat:@"%d",gConfig.iSampleRate];
  186. lblAudioBufferSize.text = [NSString stringWithFormat:@"%d",gConfig.wAudioBufferSize];
  187. sliderMusicVolume.value = gConfig.iMusicVolume;
  188. sliderSFXVolume.value = gConfig.iSoundVolume;
  189. sliderResampleQuality.value = gConfig.iResampleQuality;
  190. lblLogLevel.text = LogLevels[gConfig.iLogLevel];
  191. [self.tableView reloadData];
  192. }
  193. - (void)saveConfigs {
  194. gConfig.pszMsgFile = [lblLanguageFile.text length] == 0 ? NULL : strdup([[lblLanguageFile text] UTF8String]);
  195. gConfig.pszFontFile = [lblLanguageFile.text length] == 0 ? NULL : strdup([[lblFontFile text] UTF8String]);
  196. gConfig.pszLogFile = [lblLanguageFile.text length] == 0 ? NULL : strdup([[textLogFile text] UTF8String]);
  197. gConfig.iAudioChannels = toggleStereo.isOn ? 2 : 1;
  198. gConfig.fUseSurroundOPL = toggleSurroundOPL.isOn;
  199. gConfig.eMusicType = (MUSICTYPE)[MusicFormats indexOfObject:lblMusicType.text];
  200. gConfig.eOPLType = (OPLTYPE )[OPLFormats indexOfObject:lblOPLType.text];
  201. gConfig.iOPLSampleRate = [lblOPLRate.text intValue];
  202. gConfig.eCDType = (MUSICTYPE)[CDFormats indexOfObject:lblCDAudioSource.text]+MUSIC_OGG;
  203. gConfig.iSampleRate = [lblResampleRate.text intValue];
  204. gConfig.wAudioBufferSize = [lblAudioBufferSize.text intValue];
  205. gConfig.iMusicVolume = sliderMusicVolume.value;
  206. gConfig.iSoundVolume = sliderSFXVolume.value;
  207. gConfig.iResampleQuality = sliderResampleQuality.value;
  208. gConfig.iLogLevel = (LOGLEVEL)[LogLevels indexOfObject:lblLogLevel.text];
  209. }
  210. @end