SettingsTableViewController.m 9.9 KB

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