SettingsTableViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 origin:self.navigationController.navigationBar allowEmpty:NO];
  84. }
  85. - (void)showPickerWithTitle:(NSString *)title toLabel:(UILabel*)label inArray:(NSArray*)array origin:(UIView*)origin {
  86. [self showPickerWithTitle:title toLabel:label inArray:array origin:origin allowEmpty:NO];
  87. }
  88. - (void)showPickerWithTitle:(NSString *)title toLabel:(UILabel*)label inArray:(NSArray*)array origin:(UIView*)origin allowEmpty:(BOOL)allowEmpty {
  89. [self showPickerWithTitle:title toLabel:label inArray:array origin:origin allowEmpty:allowEmpty doneBlock:nil];
  90. }
  91. - (void)showPickerWithTitle:(NSString *)title toLabel:(UILabel*)label inArray:(NSArray*)array origin:(UIView*)origin allowEmpty:(BOOL)allowEmpty doneBlock:(SelectedBlock)doneBlock {
  92. array = allowEmpty ? [array arrayByAddingObject:@""] : array;
  93. picker = [ActionSheetStringPicker showPickerWithTitle:nil
  94. rows:array
  95. initialSelection:[array containsObject:label.text] ? [array indexOfObject:label.text] : 0
  96. doneBlock:^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
  97. label.text = array[selectedIndex];
  98. if(doneBlock) doneBlock(label.text);
  99. }
  100. cancelBlock:nil
  101. origin:origin];
  102. }
  103. //- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  104. //{
  105. // float heightForRow = 40;
  106. //
  107. // if([lblMusicType.text isEqualToString:@"MIDI"] &&
  108. // ((indexPath.row == 1 && indexPath.section == 3) ||
  109. // (indexPath.row == 1 && indexPath.section == 3)))
  110. // return 0;
  111. // else
  112. // return heightForRow;
  113. //}
  114. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  115. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  116. if( indexPath.section == 1 && indexPath.row == 0 ) { //language file
  117. [self showPickerWithTitle:nil toLabel:lblLanguageFile inArray:AvailFiles origin:cell allowEmpty:YES];
  118. }else if( indexPath.section == 1 && indexPath.row == 1 ) { //font file
  119. [self showPickerWithTitle:nil toLabel:lblFontFile inArray:AvailFiles origin:cell allowEmpty:YES];
  120. }else if( indexPath.section == 3 && indexPath.row == 0 ) { //BGM
  121. [self showPickerWithTitle:nil toLabel:lblMusicType inArray:MusicFormats origin:cell];
  122. }else if( indexPath.section == 3 && indexPath.row == 1 ) { //OPL Type
  123. [self showPickerWithTitle:nil toLabel:lblOPLType inArray:OPLFormats origin:cell];
  124. }else if( indexPath.section == 3 && indexPath.row == 2 ) { //OPL Rate
  125. [self showPickerWithTitle:nil toLabel:lblOPLRate inArray:OPLSampleRates origin:cell];
  126. }else if( indexPath.section == 3 && indexPath.row == 3 ) { //CD Source
  127. [self showPickerWithTitle:nil toLabel:lblCDAudioSource inArray:CDFormats origin:cell];
  128. }else if( indexPath.section == 3 && indexPath.row == 4 ) { //Stereo
  129. toggleStereo.enabled = !toggleStereo.isEnabled;
  130. }else if( indexPath.section == 3 && indexPath.row == 5 ) { //Surround
  131. toggleSurroundOPL.enabled = !toggleSurroundOPL.isEnabled;
  132. }else if( indexPath.section == 3 && indexPath.row == 6 ) { //SampleRate
  133. [self showPickerWithTitle:nil toLabel:lblResampleRate inArray:AudioSampleRates origin:cell];
  134. }else if( indexPath.section == 3 && indexPath.row == 7 ) { //Buffer size
  135. [self showPickerWithTitle:nil toLabel:lblAudioBufferSize inArray:AudioBufferSizes origin:cell];
  136. }else if( indexPath.section == 4 && indexPath.row == 0 ) { //Log Level
  137. [self showPickerWithTitle:nil toLabel:lblLogLevel inArray:LogLevels origin:cell];
  138. }
  139. }
  140. - (IBAction)btnDefaultClicked:(id)sender {
  141. PAL_LoadConfig(NO);
  142. [self readConfigs];
  143. }
  144. - (IBAction)btnConfirmClicked:(id)sender {
  145. [UIView transitionFromView:[self.navigationController view]
  146. toView:transitionView
  147. duration:0.65f
  148. options:UIViewAnimationOptionTransitionCurlDown
  149. completion:^(BOOL finished){
  150. [self saveConfigs];
  151. gConfig.fLaunchSetting = NO;
  152. PAL_SaveConfig();
  153. [[SDLPalAppDelegate sharedAppDelegate] launchGame];
  154. }];
  155. }
  156. - (void)readConfigs {
  157. gConfig.fFullScreen = YES; //iOS specific; need this to make sure statusbar hidden in game completely
  158. lblLanguageFile.text = [NSString stringWithUTF8String:gConfig.pszMsgFile ? gConfig.pszMsgFile : ""];
  159. lblFontFile.text = [NSString stringWithUTF8String:gConfig.pszFontFile ? gConfig.pszFontFile : ""];
  160. textLogFile.text = [NSString stringWithUTF8String:gConfig.pszLogFile ? gConfig.pszLogFile : ""];
  161. toggleStereo.enabled = gConfig.iAudioChannels == 2;
  162. toggleSurroundOPL.enabled = gConfig.fUseSurroundOPL;
  163. lblMusicType.text = MusicFormats[gConfig.eMusicType];
  164. lblOPLType.text = OPLFormats[gConfig.eOPLType];
  165. lblOPLRate.text = [NSString stringWithFormat:@"%d",gConfig.iOPLSampleRate];
  166. lblCDAudioSource.text = CDFormats[gConfig.eCDType-MUSIC_OGG];
  167. lblResampleRate.text = [NSString stringWithFormat:@"%d",gConfig.iSampleRate];
  168. lblAudioBufferSize.text = [NSString stringWithFormat:@"%d",gConfig.wAudioBufferSize];
  169. sliderMusicVolume.value = gConfig.iMusicVolume;
  170. sliderSFXVolume.value = gConfig.iSoundVolume;
  171. sliderResampleQuality.value = gConfig.iResampleQuality;
  172. lblLogLevel.text = LogLevels[gConfig.iLogLevel];
  173. }
  174. - (void)saveConfigs {
  175. gConfig.pszMsgFile = [lblLanguageFile.text length] == 0 ? NULL : strdup([[lblLanguageFile text] UTF8String]);
  176. gConfig.pszFontFile = [lblLanguageFile.text length] == 0 ? NULL : strdup([[lblFontFile text] UTF8String]);
  177. gConfig.pszLogFile = [lblLanguageFile.text length] == 0 ? NULL : strdup([[textLogFile text] UTF8String]);
  178. gConfig.iAudioChannels = toggleStereo.isEnabled ? 2 : 1;
  179. gConfig.fUseSurroundOPL = toggleSurroundOPL.isEnabled;
  180. gConfig.eMusicType = (MUSICTYPE)[MusicFormats indexOfObject:lblMusicType.text];
  181. gConfig.eOPLType = (OPLTYPE )[OPLFormats indexOfObject:lblOPLType.text];
  182. gConfig.iOPLSampleRate = [lblOPLRate.text intValue];
  183. gConfig.eCDType = (MUSICTYPE)[CDFormats indexOfObject:lblCDAudioSource.text]+MUSIC_OGG;
  184. gConfig.iSampleRate = [lblResampleRate.text intValue];
  185. gConfig.wAudioBufferSize = [lblAudioBufferSize.text intValue];
  186. gConfig.iMusicVolume = sliderMusicVolume.value;
  187. gConfig.iSoundVolume = sliderSFXVolume.value;
  188. gConfig.iResampleQuality = sliderResampleQuality.value;
  189. gConfig.iLogLevel = (LOGLEVEL)[LogLevels indexOfObject:lblLogLevel.text];
  190. }
  191. @end