MainPage.xaml.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. m_controls = ref new Platform::Collections::Map<Platform::String^, ButtonAttribute^>();
  26. m_controls->Insert(btnBrowseMsgFile->Name, ref new ButtonAttribute(tbMsgFile, ref new Platform::Array<Platform::String^>{ ".bdf" }));
  27. m_controls->Insert(btnBrowseFontFile->Name, ref new ButtonAttribute(tbFontFile, ref new Platform::Array<Platform::String^>{ ".msg" }));
  28. m_controls->Insert(btnBrowseLogFile->Name, ref new ButtonAttribute(tbLogFile, ref new Platform::Array<Platform::String^>{ ".log" }));
  29. m_controls->Insert(cbUseMsgFile->Name, ref new ButtonAttribute(gridMsgFile, nullptr));
  30. m_controls->Insert(cbUseFontFile->Name, ref new ButtonAttribute(gridFontFile, nullptr));
  31. m_controls->Insert(cbUseLogFile->Name, ref new ButtonAttribute(gridLogFile, nullptr));
  32. m_acl[PALCFG_GAMEPATH] = ref new AccessListEntry(tbGamePath, nullptr, ConvertString(PAL_ConfigName(PALCFG_GAMEPATH)));
  33. m_acl[PALCFG_SAVEPATH] = ref new AccessListEntry(tbGamePath, nullptr, ConvertString(PAL_ConfigName(PALCFG_SAVEPATH)));
  34. m_acl[PALCFG_MESSAGEFILE] = ref new AccessListEntry(tbMsgFile, cbUseMsgFile, ConvertString(PAL_ConfigName(PALCFG_MESSAGEFILE)));
  35. m_acl[PALCFG_FONTFILE] = ref new AccessListEntry(tbFontFile, cbUseFontFile, ConvertString(PAL_ConfigName(PALCFG_FONTFILE)));
  36. m_acl[PALCFG_LOGFILE] = ref new AccessListEntry(tbLogFile, cbUseLogFile, ConvertString(PAL_ConfigName(PALCFG_LOGFILE)));
  37. LoadControlContents();
  38. m_resLdr = Windows::ApplicationModel::Resources::ResourceLoader::GetForCurrentView();
  39. if (static_cast<App^>(Application::Current)->LastCrashed)
  40. {
  41. (ref new Windows::UI::Popups::MessageDialog(m_resLdr->GetString("MBCrashContent")))->ShowAsync();
  42. }
  43. }
  44. void SDLPal::MainPage::LoadControlContents(bool loadDefault)
  45. {
  46. for (auto i = m_acl.begin(); i != m_acl.end(); i++)
  47. {
  48. auto item = i->second;
  49. item->text->Text = "";
  50. item->text->Tag = nullptr;
  51. if (item->check)
  52. {
  53. item->check->IsChecked = false;
  54. m_controls->Lookup(item->check->Name)->Object->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
  55. }
  56. }
  57. if (!loadDefault)
  58. {
  59. // Always load folder/files from FutureAccessList
  60. std::list<Platform::String^> invalid_tokens;
  61. auto fal = Windows::Storage::AccessCache::StorageApplicationPermissions::FutureAccessList;
  62. for each (auto entry in fal->Entries)
  63. {
  64. auto& ace = m_acl[PAL_ConfigIndex(ConvertString(entry.Token).c_str())];
  65. ace->text->Tag = AWait(fal->GetItemAsync(entry.Token), g_eventHandle);
  66. if (ace->text->Tag)
  67. ace->text->Text = entry.Metadata;
  68. else
  69. invalid_tokens.push_back(entry.Token);
  70. if (ace->check)
  71. {
  72. auto grid = m_controls->Lookup(ace->check->Name)->Object;
  73. ace->check->IsChecked = (ace->text->Tag != nullptr);
  74. grid->Visibility = ace->check->IsChecked->Value ? Windows::UI::Xaml::Visibility::Visible : Windows::UI::Xaml::Visibility::Collapsed;
  75. }
  76. }
  77. for (auto i = invalid_tokens.begin(); i != invalid_tokens.end(); fal->Remove(*i++));
  78. }
  79. tsKeepAspect->IsOn = (gConfig.fKeepAspectRatio == TRUE);
  80. tsStereo->IsOn = (gConfig.iAudioChannels == 2);
  81. tsSurroundOPL->IsOn = (gConfig.fUseSurroundOPL == TRUE);
  82. tsTouchOverlay->IsOn = (gConfig.fUseTouchOverlay == TRUE);
  83. slMusicVolume->Value = gConfig.iMusicVolume;
  84. slSoundVolume->Value = gConfig.iSoundVolume;
  85. slQuality->Value = gConfig.iResampleQuality;
  86. cbLogLevel->SelectedIndex = (int)gConfig.iLogLevel;
  87. cbCD->SelectedIndex = (gConfig.eCDType == MUSIC_MP3) ? 0 : 1;
  88. cbBGM->SelectedIndex = (gConfig.eMusicType <= MUSIC_OGG) ? gConfig.eMusicType : MUSIC_RIX;
  89. cbOPL->SelectedIndex = (int)gConfig.eOPLType;
  90. if (gConfig.iSampleRate <= 11025)
  91. cbSampleRate->SelectedIndex = 0;
  92. else if (gConfig.iSampleRate <= 22050)
  93. cbSampleRate->SelectedIndex = 1;
  94. else
  95. cbSampleRate->SelectedIndex = 2;
  96. auto wValue = gConfig.wAudioBufferSize >> 10;
  97. unsigned int index = 0;
  98. while (wValue) { index++; wValue >>= 1; }
  99. if (index >= cbAudioBuffer->Items->Size)
  100. cbAudioBuffer->SelectedIndex = cbAudioBuffer->Items->Size - 1;
  101. else
  102. cbAudioBuffer->SelectedIndex = index;
  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. }
  110. void SDLPal::MainPage::SaveControlContents()
  111. {
  112. // All folders/files are not stored in config file, as they are store in FutureAcessList
  113. if (gConfig.pszGamePath) { free(gConfig.pszGamePath); gConfig.pszGamePath = nullptr; }
  114. if (gConfig.pszMsgFile) { free(gConfig.pszMsgFile); gConfig.pszMsgFile = nullptr; }
  115. if (gConfig.pszFontFile) { free(gConfig.pszFontFile); gConfig.pszFontFile = nullptr; }
  116. if (gConfig.pszLogFile) { free(gConfig.pszLogFile); gConfig.pszLogFile = nullptr; }
  117. gConfig.fKeepAspectRatio = tsKeepAspect->IsOn ? TRUE : FALSE;
  118. gConfig.iAudioChannels = tsStereo->IsOn ? 2 : 1;
  119. gConfig.fUseSurroundOPL = tsSurroundOPL->IsOn ? TRUE : FALSE;
  120. gConfig.fUseTouchOverlay = tsTouchOverlay->IsOn ? TRUE : FALSE;
  121. gConfig.iMusicVolume = (int)slMusicVolume->Value;
  122. gConfig.iSoundVolume = (int)slSoundVolume->Value;
  123. gConfig.iResampleQuality = (int)slQuality->Value;
  124. gConfig.iLogLevel = (LOGLEVEL)cbLogLevel->SelectedIndex;
  125. gConfig.eCDType = (MUSICTYPE)(MUSIC_MP3 + cbCD->SelectedIndex);
  126. gConfig.eMusicType = (MUSICTYPE)cbBGM->SelectedIndex;
  127. gConfig.eOPLType = (OPLTYPE)cbOPL->SelectedIndex;
  128. gConfig.iSampleRate = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbSampleRate->SelectedItem)->Content)->Data(), nullptr, 10);
  129. gConfig.iOPLSampleRate = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbOPLSR->SelectedItem)->Content)->Data(), nullptr, 10);
  130. gConfig.wAudioBufferSize = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbAudioBuffer->SelectedItem)->Content)->Data(), nullptr, 10);
  131. }
  132. void SDLPal::MainPage::cbBGM_SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
  133. {
  134. auto visibility = (cbBGM->SelectedIndex == MUSIC_RIX) ? Windows::UI::Xaml::Visibility::Visible : Windows::UI::Xaml::Visibility::Collapsed;
  135. cbOPL->Visibility = visibility;
  136. cbOPLSR->Visibility = visibility;
  137. tsSurroundOPL->Visibility = visibility;
  138. }
  139. void SDLPal::MainPage::btnDefault_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  140. {
  141. PAL_LoadConfig(FALSE);
  142. LoadControlContents(true);
  143. }
  144. void SDLPal::MainPage::btnReset_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  145. {
  146. PAL_LoadConfig(TRUE);
  147. LoadControlContents();
  148. }
  149. void SDLPal::MainPage::btnFinish_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  150. {
  151. if (tbGamePath->Text->Length() > 0)
  152. {
  153. auto fal = Windows::Storage::AccessCache::StorageApplicationPermissions::FutureAccessList;
  154. for (auto i = m_acl.begin(); i != m_acl.end(); i++)
  155. {
  156. auto item = i->second;
  157. auto check = item->check ? item->check->IsChecked->Value : true;
  158. if (check && item->text->Tag)
  159. fal->AddOrReplace(item->token, safe_cast<Windows::Storage::IStorageItem^>(item->text->Tag), item->text->Text);
  160. else if (fal->ContainsItem(item->token))
  161. fal->Remove(item->token);
  162. }
  163. SaveControlContents();
  164. gConfig.fLaunchSetting = FALSE;
  165. PAL_SaveConfig();
  166. auto dlg = ref new Windows::UI::Popups::MessageDialog(m_resLdr->GetString("MBExitContent"));
  167. dlg->Title = m_resLdr->GetString("MBExitTitle");
  168. concurrency::create_task(dlg->ShowAsync()).then([] (Windows::UI::Popups::IUICommand^ command) {
  169. Application::Current->Exit();
  170. });
  171. }
  172. else
  173. {
  174. (ref new Windows::UI::Popups::MessageDialog(m_resLdr->GetString("MBEmptyContent")))->ShowAsync();
  175. }
  176. }
  177. void SDLPal::MainPage::btnClearFile_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  178. {
  179. tbMsgFile->Text = "";
  180. tbMsgFile->Tag = nullptr;
  181. }
  182. void SDLPal::MainPage::SetPath(Windows::Storage::StorageFolder^ folder)
  183. {
  184. if (folder)
  185. {
  186. tbGamePath->Text = folder->Path;
  187. tbGamePath->Tag = folder;
  188. }
  189. }
  190. void SDLPal::MainPage::SetFile(Windows::UI::Xaml::Controls::TextBox^ target, Windows::Storage::StorageFile^ file)
  191. {
  192. if (target && file)
  193. {
  194. target->Text = file->Path;
  195. target->Tag = file;
  196. }
  197. }
  198. void SDLPal::MainPage::btnBrowseFolder_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  199. {
  200. auto picker = ref new Windows::Storage::Pickers::FolderPicker();
  201. picker->FileTypeFilter->Append("*");
  202. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  203. picker->ContinuationData->Insert("Page", this);
  204. picker->PickFolderAndContinue();
  205. #else
  206. concurrency::create_task(picker->PickSingleFolderAsync()).then([this](Windows::Storage::StorageFolder^ folder) { SetPath(folder); });
  207. #endif
  208. }
  209. void SDLPal::MainPage::btnBrowseFileOpen_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  210. {
  211. auto button = static_cast<Windows::UI::Xaml::Controls::Button^>(sender);
  212. auto target = m_controls->Lookup(button->Name);
  213. auto picker = ref new Windows::Storage::Pickers::FileOpenPicker();
  214. picker->FileTypeFilter->ReplaceAll(target->Filter);
  215. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  216. picker->ContinuationData->Insert("Page", this);
  217. picker->ContinuationData->Insert("Target", target->Object);
  218. picker->PickSingleFileAndContinue();
  219. #else
  220. concurrency::create_task(picker->PickSingleFileAsync()).then(
  221. [this, target](Windows::Storage::StorageFile^ file) {
  222. SetFile(static_cast<Windows::UI::Xaml::Controls::TextBox^>(target->Object), file);
  223. }
  224. );
  225. #endif
  226. }
  227. void SDLPal::MainPage::btnBrowseFileSave_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  228. {
  229. auto button = static_cast<Windows::UI::Xaml::Controls::Button^>(sender);
  230. auto target = m_controls->Lookup(button->Name);
  231. auto picker = ref new Windows::Storage::Pickers::FileSavePicker();
  232. picker->FileTypeChoices->Insert(m_resLdr->GetString("LogFileType"), ref new Platform::Collections::Vector<Platform::String^>(target->Filter));
  233. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  234. picker->ContinuationData->Insert("Page", this);
  235. picker->ContinuationData->Insert("Target", target->Object);
  236. picker->PickSaveFileAndContinue();
  237. #else
  238. concurrency::create_task(picker->PickSaveFileAsync()).then(
  239. [this, target](Windows::Storage::StorageFile^ file) {
  240. SetFile(static_cast<Windows::UI::Xaml::Controls::TextBox^>(target->Object), file);
  241. }
  242. );
  243. #endif
  244. }
  245. void SDLPal::MainPage::cbUseFile_CheckChanged(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  246. {
  247. auto checker = static_cast<Windows::UI::Xaml::Controls::CheckBox^>(sender);
  248. auto attr = m_controls->Lookup(checker->Name);
  249. attr->Object->Visibility = checker->IsChecked->Value ? Windows::UI::Xaml::Visibility::Visible : Windows::UI::Xaml::Visibility::Collapsed;
  250. }
  251. void SDLPal::MainPage::Page_Loaded(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  252. {
  253. #if NTDDI_VERSION >= NTDDI_WIN10
  254. if (!Windows::Foundation::Metadata::ApiInformation::IsTypePresent("Windows.UI.ViewManagement.StatusBar")) return;
  255. #endif
  256. #if NTDDI_VERSION >= NTDDI_WIN10 || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  257. auto statusBar = Windows::UI::ViewManagement::StatusBar::GetForCurrentView();
  258. concurrency::create_task(statusBar->ShowAsync()).then([statusBar]() { statusBar->BackgroundOpacity = 1.0; });
  259. #endif
  260. }