MainPage.xaml.cpp 12 KB

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