MainPage.xaml.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // MainPage.xaml.cpp
  3. // MainPage 类的实现。
  4. //
  5. #include "pch.h"
  6. #include "MainPage.xaml.h"
  7. #include "../SDLPal.Common/StringHelper.h"
  8. #include "../SDLPal.Common/AsyncHelper.h"
  9. #include "../../global.h"
  10. using namespace SDLPal;
  11. using namespace Platform;
  12. using namespace Windows::Foundation;
  13. using namespace Windows::Foundation::Collections;
  14. using namespace Windows::UI::Xaml;
  15. using namespace Windows::UI::Xaml::Controls;
  16. using namespace Windows::UI::Xaml::Controls::Primitives;
  17. using namespace Windows::UI::Xaml::Data;
  18. using namespace Windows::UI::Xaml::Input;
  19. using namespace Windows::UI::Xaml::Media;
  20. using namespace Windows::UI::Xaml::Navigation;
  21. MainPage::MainPage()
  22. {
  23. InitializeComponent();
  24. static_cast<App^>(Application::Current)->SetMainPage(this);
  25. LoadControlContents();
  26. Windows::UI::Xaml::Controls::Button^ buttons[] = { btnExport1, btnExport2, btnExport3, btnExport4, btnExport5 };
  27. for (int i = 0; i < 5; i++) m_buttons[i] = buttons[i];
  28. CheckSaveSlots();
  29. }
  30. void SDLPal::MainPage::SetPath(Windows::Storage::StorageFolder^ folder)
  31. {
  32. tbGamePath->Text = folder->Path;
  33. tbGamePath->Tag = folder;
  34. }
  35. void SDLPal::MainPage::Export(Windows::Storage::StorageFile^ file, Platform::String^ slot)
  36. {
  37. auto src = AWait(Windows::Storage::ApplicationData::Current->LocalFolder->GetFileAsync(slot), g_eventHandle);
  38. try
  39. {
  40. AWait(src->CopyAndReplaceAsync(file), g_eventHandle);
  41. }
  42. catch (Platform::Exception^ e)
  43. {
  44. (ref new Windows::UI::Popups::MessageDialog(e->Message))->ShowAsync();
  45. }
  46. }
  47. void SDLPal::MainPage::Import(Windows::Storage::StorageFile^ file, Platform::String^ slot)
  48. {
  49. try
  50. {
  51. AWait(file->CopyAsync(Windows::Storage::ApplicationData::Current->LocalFolder, slot, Windows::Storage::NameCollisionOption::ReplaceExisting), g_eventHandle);
  52. m_buttons[_wtoi(slot->Data()) - 1]->IsEnabled = true;
  53. }
  54. catch (Platform::Exception^ e)
  55. {
  56. (ref new Windows::UI::Popups::MessageDialog(e->Message))->ShowAsync();
  57. }
  58. }
  59. void SDLPal::MainPage::LoadControlContents()
  60. {
  61. if (gConfig.pszGamePath)
  62. {
  63. tbGamePath->Text = ConvertString(gConfig.pszGamePath);
  64. try { tbGamePath->Tag = AWait(Windows::Storage::StorageFolder::GetFolderFromPathAsync(tbGamePath->Text), g_eventHandle); }
  65. catch (Platform::Exception^) {}
  66. }
  67. else
  68. {
  69. tbGamePath->Tag = Windows::Storage::ApplicationData::Current->LocalFolder;
  70. tbGamePath->Text = Windows::Storage::ApplicationData::Current->LocalFolder->Path;
  71. }
  72. if (gConfig.pszMsgFile) tbMsgFile->Text = ConvertString(gConfig.pszMsgFile);
  73. tsLanguage->IsOn = (gConfig.uCodePage == CP_GBK);
  74. tsIsDOS->IsOn = (gConfig.fIsWIN95 == FALSE);
  75. tsUseEmbedFont->IsOn = (gConfig.fUseEmbeddedFonts == TRUE);
  76. tsKeepAspect->IsOn = (gConfig.fKeepAspectRatio == TRUE);
  77. tsStereo->IsOn = (gConfig.iAudioChannels == 2);
  78. tsSurroundOPL->IsOn = (gConfig.fUseSurroundOPL == TRUE);
  79. slVolume->Value = gConfig.iVolume;
  80. slQuality->Value = gConfig.iResampleQuality;
  81. cbCD->SelectedIndex = (gConfig.eCDType == MUSIC_MP3) ? 0 : 1;
  82. if (gConfig.eMusicType == MUSIC_OGG)
  83. cbBGM->SelectedIndex = 2;
  84. else if (gConfig.eMusicType == MUSIC_MP3)
  85. cbBGM->SelectedIndex = 1;
  86. else
  87. cbBGM->SelectedIndex = 0;
  88. cbOPL->SelectedIndex = (int)gConfig.eOPLType;
  89. if (gConfig.iSampleRate <= 11025)
  90. cbSampleRate->SelectedIndex = 0;
  91. else if (gConfig.iSampleRate <= 22050)
  92. cbSampleRate->SelectedIndex = 1;
  93. else
  94. cbSampleRate->SelectedIndex = 2;
  95. if (gConfig.wAudioBufferSize <= 512)
  96. cbAudioBuffer->SelectedIndex = 0;
  97. else if (gConfig.wAudioBufferSize == 1024)
  98. cbAudioBuffer->SelectedIndex = 1;
  99. else if (gConfig.wAudioBufferSize == 2048)
  100. cbAudioBuffer->SelectedIndex = 2;
  101. else
  102. cbAudioBuffer->SelectedIndex = 3;
  103. if (gConfig.iOPLSampleRate <= 12429)
  104. cbOPLSR->SelectedIndex = 0;
  105. else if (gConfig.iSampleRate <= 24858)
  106. cbOPLSR->SelectedIndex = 1;
  107. else
  108. cbOPLSR->SelectedIndex = 2;
  109. tsUseEmbedFont->Visibility = tsIsDOS->IsOn ? Windows::UI::Xaml::Visibility::Visible : Windows::UI::Xaml::Visibility::Collapsed;
  110. }
  111. void SDLPal::MainPage::SaveControlContents()
  112. {
  113. std::wstring path;
  114. if (gConfig.pszGamePath) free(gConfig.pszGamePath);
  115. path.assign(tbGamePath->Text->Data());
  116. if (path.back() != '\\') path.append(L"\\");
  117. gConfig.pszGamePath = strdup(ConvertString(path).c_str());
  118. if (gConfig.pszMsgFile) { free(gConfig.pszMsgFile); gConfig.pszMsgFile = NULL; }
  119. gConfig.pszMsgFile = (tbMsgFile->Text->Length() > 0) ? strdup(ConvertString(tbMsgFile->Text).c_str()) : nullptr;
  120. gConfig.fIsWIN95 = tsIsDOS->IsOn ? FALSE : TRUE;
  121. gConfig.fUseEmbeddedFonts = tsUseEmbedFont->IsOn ? TRUE : FALSE;
  122. gConfig.fKeepAspectRatio = tsKeepAspect->IsOn ? TRUE : FALSE;
  123. gConfig.iAudioChannels = tsStereo->IsOn ? 2 : 1;
  124. gConfig.fUseSurroundOPL = tsSurroundOPL->IsOn ? TRUE : FALSE;
  125. gConfig.iVolume = (int)slVolume->Value;
  126. gConfig.iResampleQuality = (int)slQuality->Value;
  127. gConfig.uCodePage = tsLanguage->IsOn ? CP_GBK : CP_BIG5;
  128. gConfig.eCDType = (MUSICTYPE)(MUSIC_MP3 + cbCD->SelectedIndex);
  129. gConfig.eMusicType = (cbBGM->SelectedIndex >= 1) ? (MUSICTYPE)(MUSIC_MP3 + cbBGM->SelectedIndex) : MUSIC_RIX;
  130. gConfig.eOPLType = (OPLTYPE)cbOPL->SelectedIndex;
  131. gConfig.iSampleRate = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbSampleRate->SelectedItem)->Content)->Data(), nullptr, 10);
  132. gConfig.iOPLSampleRate = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbOPLSR->SelectedItem)->Content)->Data(), nullptr, 10);
  133. gConfig.wAudioBufferSize = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbAudioBuffer->SelectedItem)->Content)->Data(), nullptr, 10);
  134. }
  135. void SDLPal::MainPage::CheckSaveSlots()
  136. {
  137. static Platform::String^ slots[] = { "1.rpg", "2.rpg", "3.rpg", "4.rpg", "5.rpg" };
  138. auto folder = Windows::Storage::ApplicationData::Current->LocalFolder;
  139. for (int i = 0; i < 5; i++)
  140. {
  141. try
  142. {
  143. AWait(folder->GetFileAsync(slots[i]), g_eventHandle);
  144. m_buttons[i]->IsEnabled = true;
  145. }
  146. catch (Platform::Exception^)
  147. {
  148. m_buttons[i]->IsEnabled = false;
  149. }
  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->PickFolderAndContinue();
  156. }
  157. void SDLPal::MainPage::btnDefault_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  158. {
  159. tbGamePath->Tag = Windows::Storage::ApplicationData::Current->LocalFolder;
  160. tbGamePath->Text = Windows::Storage::ApplicationData::Current->LocalFolder->Path;
  161. }
  162. void SDLPal::MainPage::tsIsDOS_Toggled(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  163. {
  164. if (tsIsDOS->IsOn)
  165. {
  166. tsLanguage->IsOn = false;
  167. tsLanguage->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
  168. tsUseEmbedFont->Visibility = Windows::UI::Xaml::Visibility::Visible;
  169. }
  170. else
  171. {
  172. tsLanguage->IsOn = (gConfig.uCodePage == CP_GBK);
  173. tsLanguage->Visibility = Windows::UI::Xaml::Visibility::Visible;
  174. tsUseEmbedFont->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
  175. }
  176. }
  177. void SDLPal::MainPage::cbBGM_SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
  178. {
  179. auto visibility = (cbBGM->SelectedIndex == 0) ? Windows::UI::Xaml::Visibility::Visible : Windows::UI::Xaml::Visibility::Collapsed;
  180. cbOPL->Visibility = visibility;
  181. cbOPLSR->Visibility = visibility;
  182. tsSurroundOPL->Visibility = visibility;
  183. }
  184. void SDLPal::MainPage::btnReset_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  185. {
  186. PAL_LoadConfig(FALSE);
  187. LoadControlContents();
  188. }
  189. void SDLPal::MainPage::btnFinish_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  190. {
  191. SaveControlContents();
  192. auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
  193. auto handler = ref new Windows::UI::Popups::UICommandInvokedHandler(this, &MainPage::CloseUICommandHandler);
  194. auto dlg = ref new Windows::UI::Popups::MessageDialog(loader->GetString("MBContent"));
  195. dlg->Commands->Append(ref new Windows::UI::Popups::UICommand(loader->GetString("MBYes"), handler, safe_cast<Platform::Object^>(0)));
  196. dlg->Commands->Append(ref new Windows::UI::Popups::UICommand(loader->GetString("MBNo"), handler, safe_cast<Platform::Object^>(1)));
  197. dlg->ShowAsync();
  198. }
  199. void SDLPal::MainPage::btnImport_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  200. {
  201. auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
  202. auto picker = ref new Windows::Storage::Pickers::FileOpenPicker();
  203. picker->SettingsIdentifier = safe_cast<Platform::String^>(static_cast<Windows::UI::Xaml::Controls::Button^>(sender)->Tag);
  204. picker->ContinuationData->Insert("Slot", picker->SettingsIdentifier);
  205. picker->FileTypeFilter->Append(".rpg");
  206. picker->PickSingleFileAndContinue();
  207. }
  208. void SDLPal::MainPage::btnExport_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  209. {
  210. auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
  211. auto picker = ref new Windows::Storage::Pickers::FileSavePicker();
  212. picker->FileTypeChoices->Insert(loader->GetString("SaveSlotType"), ref new Platform::Collections::Vector<Platform::String^>(1, { ".rpg" }));
  213. picker->SettingsIdentifier = safe_cast<Platform::String^>(static_cast<Windows::UI::Xaml::Controls::Button^>(sender)->Tag);
  214. picker->SuggestedFileName = picker->SettingsIdentifier;
  215. picker->DefaultFileExtension = ".rpg";
  216. picker->ContinuationData->Insert("Slot", picker->SettingsIdentifier);
  217. picker->PickSaveFileAndContinue();
  218. }
  219. void SDLPal::MainPage::CloseUICommandHandler(Windows::UI::Popups::IUICommand^ command)
  220. {
  221. gConfig.fLaunchSetting = ((int)command->Id == 0);
  222. PAL_SaveConfig();
  223. for (int i = 0; i < 5; i++) m_buttons[i] = nullptr;
  224. Application::Current->Exit();
  225. }