MainPage.xaml.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. m_resLdr = Windows::ApplicationModel::Resources::ResourceLoader::GetForCurrentView();
  27. auto app = static_cast<App^>(Application::Current);
  28. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  29. app->Page = this;
  30. #endif
  31. if (app->LastCrashed)
  32. (ref new Windows::UI::Popups::MessageDialog(m_resLdr->GetString("MBCrashContent")))->ShowAsync();
  33. }
  34. void SDLPal::MainPage::LoadControlContents()
  35. {
  36. if (gConfig.pszGamePath) tbGamePath->Text = ConvertString(gConfig.pszGamePath);
  37. if (gConfig.pszMsgFile) tbMsgFile->Text = ConvertString(gConfig.pszMsgFile);
  38. tsLanguage->IsOn = (gConfig.uCodePage == CP_GBK);
  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. slMusicVolume->Value = gConfig.iMusicVolume;
  45. slSoundVolume->Value = gConfig.iSoundVolume;
  46. slQuality->Value = gConfig.iResampleQuality;
  47. cbCD->SelectedIndex = (gConfig.eCDType == MUSIC_MP3) ? 0 : 1;
  48. cbBGM->SelectedIndex = (gConfig.eMusicType <= MUSIC_OGG) ? gConfig.eMusicType : MUSIC_RIX;
  49. cbOPL->SelectedIndex = (int)gConfig.eOPLType;
  50. if (gConfig.iSampleRate <= 11025)
  51. cbSampleRate->SelectedIndex = 0;
  52. else if (gConfig.iSampleRate <= 22050)
  53. cbSampleRate->SelectedIndex = 1;
  54. else
  55. cbSampleRate->SelectedIndex = 2;
  56. auto wValue = gConfig.wAudioBufferSize >> 10;
  57. unsigned int index = 0;
  58. while (wValue) { index++; wValue >>= 1; }
  59. if (index >= cbAudioBuffer->Items->Size)
  60. cbAudioBuffer->SelectedIndex = cbAudioBuffer->Items->Size - 1;
  61. else
  62. cbAudioBuffer->SelectedIndex = index;
  63. if (gConfig.iOPLSampleRate <= 12429)
  64. cbOPLSR->SelectedIndex = 0;
  65. else if (gConfig.iSampleRate <= 24858)
  66. cbOPLSR->SelectedIndex = 1;
  67. else
  68. cbOPLSR->SelectedIndex = 2;
  69. }
  70. void SDLPal::MainPage::SaveControlContents()
  71. {
  72. std::wstring path;
  73. if (gConfig.pszGamePath) free(gConfig.pszGamePath);
  74. path.assign(tbGamePath->Text->Data());
  75. if (path.back() != '\\') path.append(L"\\");
  76. gConfig.pszGamePath = strdup(ConvertString(path).c_str());
  77. if (gConfig.pszMsgFile) { free(gConfig.pszMsgFile); gConfig.pszMsgFile = NULL; }
  78. gConfig.pszMsgFile = (tbMsgFile->Text->Length() > 0) ? strdup(ConvertString(tbMsgFile->Text).c_str()) : nullptr;
  79. gConfig.fUseEmbeddedFonts = tsUseEmbedFont->IsOn ? TRUE : FALSE;
  80. gConfig.fKeepAspectRatio = tsKeepAspect->IsOn ? TRUE : FALSE;
  81. gConfig.iAudioChannels = tsStereo->IsOn ? 2 : 1;
  82. gConfig.fUseSurroundOPL = tsSurroundOPL->IsOn ? TRUE : FALSE;
  83. gConfig.fUseTouchOverlay = tsTouchOverlay->IsOn ? TRUE : FALSE;
  84. gConfig.iMusicVolume = (int)slMusicVolume->Value;
  85. gConfig.iSoundVolume = (int)slSoundVolume->Value;
  86. gConfig.iResampleQuality = (int)slQuality->Value;
  87. gConfig.uCodePage = tsLanguage->IsOn ? CP_GBK : CP_BIG5;
  88. gConfig.eCDType = (MUSICTYPE)(MUSIC_MP3 + cbCD->SelectedIndex);
  89. gConfig.eMusicType = (MUSICTYPE)cbBGM->SelectedIndex;
  90. gConfig.eOPLType = (OPLTYPE)cbOPL->SelectedIndex;
  91. gConfig.iSampleRate = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbSampleRate->SelectedItem)->Content)->Data(), nullptr, 10);
  92. gConfig.iOPLSampleRate = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbOPLSR->SelectedItem)->Content)->Data(), nullptr, 10);
  93. gConfig.wAudioBufferSize = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbAudioBuffer->SelectedItem)->Content)->Data(), nullptr, 10);
  94. }
  95. void SDLPal::MainPage::cbBGM_SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
  96. {
  97. auto visibility = (cbBGM->SelectedIndex == MUSIC_RIX) ? Windows::UI::Xaml::Visibility::Visible : Windows::UI::Xaml::Visibility::Collapsed;
  98. cbOPL->Visibility = visibility;
  99. cbOPLSR->Visibility = visibility;
  100. tsSurroundOPL->Visibility = visibility;
  101. }
  102. void SDLPal::MainPage::btnReset_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  103. {
  104. PAL_LoadConfig(FALSE);
  105. LoadControlContents();
  106. }
  107. void SDLPal::MainPage::btnFinish_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  108. {
  109. if (tbGamePath->Text->Length() > 0)
  110. {
  111. auto mru_list = Windows::Storage::AccessCache::StorageApplicationPermissions::MostRecentlyUsedList;
  112. if (tbGamePath->Tag) mru_list->Add(safe_cast<Windows::Storage::StorageFolder^>(tbGamePath->Tag), tbGamePath->Text);
  113. if (tbMsgFile->Tag) mru_list->Add(safe_cast<Windows::Storage::StorageFile^>(tbMsgFile->Tag), tbMsgFile->Text);
  114. SaveControlContents();
  115. gConfig.fLaunchSetting = FALSE;
  116. PAL_SaveConfig();
  117. auto dlg = ref new Windows::UI::Popups::MessageDialog(m_resLdr->GetString("MBExitContent"));
  118. dlg->Title = m_resLdr->GetString("MBExitTitle");
  119. concurrency::create_task(dlg->ShowAsync()).then([] (Windows::UI::Popups::IUICommand^ command) {
  120. Application::Current->Exit();
  121. });
  122. }
  123. else
  124. {
  125. (ref new Windows::UI::Popups::MessageDialog(m_resLdr->GetString("MBEmptyContent")))->ShowAsync();
  126. }
  127. }
  128. void SDLPal::MainPage::btnClearFile_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  129. {
  130. tbMsgFile->Text = "";
  131. tbMsgFile->Tag = nullptr;
  132. }
  133. void SDLPal::MainPage::SetPath(Windows::Storage::StorageFolder^ folder)
  134. {
  135. if (folder)
  136. {
  137. tbGamePath->Text = folder->Path;
  138. tbGamePath->Tag = folder;
  139. }
  140. }
  141. void SDLPal::MainPage::SetFile(Windows::Storage::StorageFile^ file)
  142. {
  143. if (file)
  144. {
  145. tbMsgFile->Text = file->Path;
  146. tbMsgFile->Tag = file;
  147. }
  148. }
  149. void SDLPal::MainPage::btnBrowse_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  150. {
  151. auto picker = ref new Windows::Storage::Pickers::FolderPicker();
  152. picker->FileTypeFilter->Append("*");
  153. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  154. picker->PickFolderAndContinue();
  155. #else
  156. concurrency::create_task(picker->PickSingleFolderAsync()).then([this](Windows::Storage::StorageFolder^ folder) { SetPath(folder); });
  157. #endif
  158. }
  159. void SDLPal::MainPage::btnBrowseFile_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  160. {
  161. auto picker = ref new Windows::Storage::Pickers::FileOpenPicker();
  162. picker->FileTypeFilter->Append("*");
  163. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  164. picker->PickSingleFileAndContinue();
  165. #else
  166. concurrency::create_task(picker->PickSingleFileAsync()).then([this](Windows::Storage::StorageFile^ file) { SetFile(file); });
  167. #endif
  168. }
  169. void SDLPal::MainPage::Page_Loaded(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  170. {
  171. #if NTDDI_VERSION >= NTDDI_WIN10
  172. if (!Windows::Foundation::Metadata::ApiInformation::IsTypePresent("Windows.UI.ViewManagement.StatusBar")) return;
  173. #endif
  174. #if NTDDI_VERSION >= NTDDI_WIN10 || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  175. auto statusBar = Windows::UI::ViewManagement::StatusBar::GetForCurrentView();
  176. concurrency::create_task(statusBar->ShowAsync()).then([statusBar]() { statusBar->BackgroundOpacity = 1.0; });
  177. #endif
  178. }