MainPage.xaml.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // MainPage.xaml.cpp
  3. // MainPage 类的实现。
  4. //
  5. #include "pch.h"
  6. #include "MainPage.xaml.h"
  7. #include "StringHelper.h"
  8. #include "AsyncHelper.h"
  9. #include "../../global.h"
  10. #include "../../palcfg.h"
  11. using namespace SDLPal;
  12. using namespace Platform;
  13. using namespace Windows::Foundation;
  14. using namespace Windows::Foundation::Collections;
  15. using namespace Windows::UI::Xaml;
  16. using namespace Windows::UI::Xaml::Controls;
  17. using namespace Windows::UI::Xaml::Controls::Primitives;
  18. using namespace Windows::UI::Xaml::Data;
  19. using namespace Windows::UI::Xaml::Input;
  20. using namespace Windows::UI::Xaml::Media;
  21. using namespace Windows::UI::Xaml::Navigation;
  22. MainPage::MainPage()
  23. {
  24. InitializeComponent();
  25. LoadControlContents();
  26. auto app = static_cast<App^>(Application::Current);
  27. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  28. app->Page = this;
  29. #endif
  30. if (app->LastCrashed)
  31. (ref new Windows::UI::Popups::MessageDialog((ref new Windows::ApplicationModel::Resources::ResourceLoader())->GetString("MBCrashContent")))->ShowAsync();
  32. }
  33. void SDLPal::MainPage::LoadControlContents()
  34. {
  35. if (gConfig.pszGamePath) tbGamePath->Text = ConvertString(gConfig.pszGamePath);
  36. if (gConfig.pszMsgFile) tbMsgFile->Text = ConvertString(gConfig.pszMsgFile);
  37. tsLanguage->IsOn = (gConfig.uCodePage == CP_GBK);
  38. tsIsDOS->IsOn = (gConfig.fIsWIN95 == FALSE);
  39. tsUseEmbedFont->IsOn = (gConfig.fUseEmbeddedFonts == TRUE);
  40. tsKeepAspect->IsOn = (gConfig.fKeepAspectRatio == TRUE);
  41. tsStereo->IsOn = (gConfig.iAudioChannels == 2);
  42. tsSurroundOPL->IsOn = (gConfig.fUseSurroundOPL == TRUE);
  43. tsTouchOverlay->IsOn = (gConfig.fUseTouchOverlay == TRUE);
  44. slVolume->Value = gConfig.iVolume;
  45. slQuality->Value = gConfig.iResampleQuality;
  46. cbCD->SelectedIndex = (gConfig.eCDType == MUSIC_MP3) ? 0 : 1;
  47. if (gConfig.eMusicType == MUSIC_OGG)
  48. cbBGM->SelectedIndex = 2;
  49. else if (gConfig.eMusicType == MUSIC_MP3)
  50. cbBGM->SelectedIndex = 1;
  51. else
  52. cbBGM->SelectedIndex = 0;
  53. cbOPL->SelectedIndex = (int)gConfig.eOPLType;
  54. if (gConfig.iSampleRate <= 11025)
  55. cbSampleRate->SelectedIndex = 0;
  56. else if (gConfig.iSampleRate <= 22050)
  57. cbSampleRate->SelectedIndex = 1;
  58. else
  59. cbSampleRate->SelectedIndex = 2;
  60. auto wValue = gConfig.wAudioBufferSize >> 10;
  61. unsigned int index = 0;
  62. while (wValue) { index++; wValue >>= 1; }
  63. if (index >= cbAudioBuffer->Items->Size)
  64. cbAudioBuffer->SelectedIndex = cbAudioBuffer->Items->Size - 1;
  65. else
  66. cbAudioBuffer->SelectedIndex = index;
  67. if (gConfig.iOPLSampleRate <= 12429)
  68. cbOPLSR->SelectedIndex = 0;
  69. else if (gConfig.iSampleRate <= 24858)
  70. cbOPLSR->SelectedIndex = 1;
  71. else
  72. cbOPLSR->SelectedIndex = 2;
  73. tsUseEmbedFont->Visibility = tsIsDOS->IsOn ? Windows::UI::Xaml::Visibility::Visible : Windows::UI::Xaml::Visibility::Collapsed;
  74. }
  75. void SDLPal::MainPage::SaveControlContents()
  76. {
  77. std::wstring path;
  78. if (gConfig.pszGamePath) free(gConfig.pszGamePath);
  79. path.assign(tbGamePath->Text->Data());
  80. if (path.back() != '\\') path.append(L"\\");
  81. gConfig.pszGamePath = strdup(ConvertString(path).c_str());
  82. if (gConfig.pszMsgFile) { free(gConfig.pszMsgFile); gConfig.pszMsgFile = NULL; }
  83. gConfig.pszMsgFile = (tbMsgFile->Text->Length() > 0) ? strdup(ConvertString(tbMsgFile->Text).c_str()) : nullptr;
  84. gConfig.fIsWIN95 = tsIsDOS->IsOn ? FALSE : TRUE;
  85. gConfig.fUseEmbeddedFonts = tsUseEmbedFont->IsOn ? TRUE : FALSE;
  86. gConfig.fKeepAspectRatio = tsKeepAspect->IsOn ? TRUE : FALSE;
  87. gConfig.iAudioChannels = tsStereo->IsOn ? 2 : 1;
  88. gConfig.fUseSurroundOPL = tsSurroundOPL->IsOn ? TRUE : FALSE;
  89. gConfig.fUseTouchOverlay = tsTouchOverlay->IsOn ? TRUE : FALSE;
  90. gConfig.iVolume = (int)slVolume->Value;
  91. gConfig.iResampleQuality = (int)slQuality->Value;
  92. gConfig.uCodePage = tsLanguage->IsOn ? CP_GBK : CP_BIG5;
  93. gConfig.eCDType = (MUSICTYPE)(MUSIC_MP3 + cbCD->SelectedIndex);
  94. gConfig.eMusicType = (cbBGM->SelectedIndex >= 1) ? (MUSICTYPE)(MUSIC_MIDI + cbBGM->SelectedIndex) : MUSIC_RIX;
  95. gConfig.eOPLType = (OPLTYPE)cbOPL->SelectedIndex;
  96. gConfig.iSampleRate = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbSampleRate->SelectedItem)->Content)->Data(), nullptr, 10);
  97. gConfig.iOPLSampleRate = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbOPLSR->SelectedItem)->Content)->Data(), nullptr, 10);
  98. gConfig.wAudioBufferSize = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbAudioBuffer->SelectedItem)->Content)->Data(), nullptr, 10);
  99. }
  100. void SDLPal::MainPage::tsIsDOS_Toggled(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  101. {
  102. if (tsIsDOS->IsOn)
  103. {
  104. tsLanguage->IsOn = false;
  105. tsLanguage->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
  106. tsUseEmbedFont->Visibility = Windows::UI::Xaml::Visibility::Visible;
  107. }
  108. else
  109. {
  110. tsLanguage->IsOn = (gConfig.uCodePage == CP_GBK);
  111. tsLanguage->Visibility = Windows::UI::Xaml::Visibility::Visible;
  112. tsUseEmbedFont->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
  113. }
  114. }
  115. void SDLPal::MainPage::cbBGM_SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
  116. {
  117. auto visibility = (cbBGM->SelectedIndex == 0) ? Windows::UI::Xaml::Visibility::Visible : Windows::UI::Xaml::Visibility::Collapsed;
  118. cbOPL->Visibility = visibility;
  119. cbOPLSR->Visibility = visibility;
  120. tsSurroundOPL->Visibility = visibility;
  121. }
  122. void SDLPal::MainPage::btnReset_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  123. {
  124. PAL_LoadConfig(FALSE);
  125. LoadControlContents();
  126. }
  127. void SDLPal::MainPage::btnFinish_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  128. {
  129. auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
  130. if (tbGamePath->Text->Length() > 0)
  131. {
  132. auto mru_list = Windows::Storage::AccessCache::StorageApplicationPermissions::MostRecentlyUsedList;
  133. if (tbGamePath->Tag) mru_list->Add(safe_cast<Windows::Storage::StorageFolder^>(tbGamePath->Tag), tbGamePath->Text);
  134. if (tbMsgFile->Tag) mru_list->Add(safe_cast<Windows::Storage::StorageFile^>(tbMsgFile->Tag), tbMsgFile->Text);
  135. SaveControlContents();
  136. gConfig.fLaunchSetting = FALSE;
  137. PAL_SaveConfig();
  138. auto dlg = ref new Windows::UI::Popups::MessageDialog(loader->GetString("MBExitContent"));
  139. dlg->Title = loader->GetString("MBExitTitle");
  140. concurrency::create_task(dlg->ShowAsync()).then([] (Windows::UI::Popups::IUICommand^ command) {
  141. Application::Current->Exit();
  142. });
  143. }
  144. else
  145. {
  146. (ref new Windows::UI::Popups::MessageDialog(loader->GetString("MBEmptyContent")))->ShowAsync();
  147. }
  148. }
  149. void SDLPal::MainPage::btnClearFile_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  150. {
  151. tbMsgFile->Text = "";
  152. tbMsgFile->Tag = nullptr;
  153. }
  154. void SDLPal::MainPage::SetPath(Windows::Storage::StorageFolder^ folder)
  155. {
  156. if (folder)
  157. {
  158. tbGamePath->Text = folder->Path;
  159. tbGamePath->Tag = folder;
  160. }
  161. }
  162. void SDLPal::MainPage::SetFile(Windows::Storage::StorageFile^ file)
  163. {
  164. if (file)
  165. {
  166. tbMsgFile->Text = file->Path;
  167. tbMsgFile->Tag = file;
  168. }
  169. }
  170. void SDLPal::MainPage::btnBrowse_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  171. {
  172. auto picker = ref new Windows::Storage::Pickers::FolderPicker();
  173. picker->FileTypeFilter->Append("*");
  174. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  175. picker->PickFolderAndContinue();
  176. #else
  177. concurrency::create_task(picker->PickSingleFolderAsync()).then([this](Windows::Storage::StorageFolder^ folder) { SetPath(folder); });
  178. #endif
  179. }
  180. void SDLPal::MainPage::btnBrowseFile_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  181. {
  182. auto picker = ref new Windows::Storage::Pickers::FileOpenPicker();
  183. picker->FileTypeFilter->Append("*");
  184. picker->ViewMode = Windows::Storage::Pickers::PickerViewMode::List;
  185. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  186. picker->PickSingleFileAndContinue();
  187. #else
  188. concurrency::create_task(picker->PickSingleFileAsync()).then([this](Windows::Storage::StorageFile^ file) { SetFile(file); });
  189. #endif
  190. }