MainPage.xaml.cpp 7.4 KB

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