SettingsTableViewController.m 12 KB

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