MainPage.xaml.cpp 12 KB

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