SettingsTableViewController.m 11 KB

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