MainPage.xaml.cpp 7.4 KB

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