MainPage.xaml.cpp 12 KB

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