MainPage.xaml.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. tsEnableAVI->IsOn = (gConfig.fEnableAviPlay == TRUE);
  89. slMusicVolume->Value = gConfig.iMusicVolume;
  90. slSoundVolume->Value = gConfig.iSoundVolume;
  91. slQuality->Value = gConfig.iResampleQuality;
  92. cbLogLevel->SelectedIndex = (int)gConfig.iLogLevel;
  93. cbCD->SelectedIndex = (gConfig.eCDType == MUSIC_MP3) ? 0 : 1;
  94. cbBGM->SelectedIndex = (gConfig.eMusicType <= MUSIC_OGG) ? gConfig.eMusicType : MUSIC_RIX;
  95. cbOPL->SelectedIndex = (int)gConfig.eOPLType;
  96. if (gConfig.iSampleRate <= 11025)
  97. cbSampleRate->SelectedIndex = 0;
  98. else if (gConfig.iSampleRate <= 22050)
  99. cbSampleRate->SelectedIndex = 1;
  100. else
  101. cbSampleRate->SelectedIndex = 2;
  102. auto wValue = gConfig.wAudioBufferSize >> 10;
  103. unsigned int index = 0;
  104. while (wValue) { index++; wValue >>= 1; }
  105. if (index >= cbAudioBuffer->Items->Size)
  106. cbAudioBuffer->SelectedIndex = cbAudioBuffer->Items->Size - 1;
  107. else
  108. cbAudioBuffer->SelectedIndex = index;
  109. if (gConfig.iOPLSampleRate <= 12429)
  110. cbOPLSR->SelectedIndex = 0;
  111. else if (gConfig.iSampleRate <= 24858)
  112. cbOPLSR->SelectedIndex = 1;
  113. else
  114. cbOPLSR->SelectedIndex = 2;
  115. }
  116. void SDLPal::MainPage::SaveControlContents()
  117. {
  118. // All folders/files are not stored in config file, as they are store in FutureAcessList
  119. if (gConfig.pszGamePath) { free(gConfig.pszGamePath); gConfig.pszGamePath = nullptr; }
  120. if (gConfig.pszMsgFile) { free(gConfig.pszMsgFile); gConfig.pszMsgFile = nullptr; }
  121. if (gConfig.pszFontFile) { free(gConfig.pszFontFile); gConfig.pszFontFile = nullptr; }
  122. if (gConfig.pszLogFile) { free(gConfig.pszLogFile); gConfig.pszLogFile = nullptr; }
  123. gConfig.fKeepAspectRatio = tsKeepAspect->IsOn ? TRUE : FALSE;
  124. gConfig.iAudioChannels = tsStereo->IsOn ? 2 : 1;
  125. gConfig.fUseSurroundOPL = tsSurroundOPL->IsOn ? TRUE : FALSE;
  126. gConfig.fUseTouchOverlay = tsTouchOverlay->IsOn ? TRUE : FALSE;
  127. gConfig.fEnableAviPlay = tsEnableAVI->IsOn ? TRUE : FALSE;
  128. gConfig.iMusicVolume = (int)slMusicVolume->Value;
  129. gConfig.iSoundVolume = (int)slSoundVolume->Value;
  130. gConfig.iResampleQuality = (int)slQuality->Value;
  131. gConfig.iLogLevel = (LOGLEVEL)cbLogLevel->SelectedIndex;
  132. gConfig.eCDType = (MUSICTYPE)(MUSIC_MP3 + cbCD->SelectedIndex);
  133. gConfig.eMusicType = (MUSICTYPE)cbBGM->SelectedIndex;
  134. gConfig.eOPLType = (OPLTYPE)cbOPL->SelectedIndex;
  135. gConfig.iSampleRate = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbSampleRate->SelectedItem)->Content)->Data(), nullptr, 10);
  136. gConfig.iOPLSampleRate = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbOPLSR->SelectedItem)->Content)->Data(), nullptr, 10);
  137. gConfig.wAudioBufferSize = wcstoul(static_cast<Platform::String^>(static_cast<ComboBoxItem^>(cbAudioBuffer->SelectedItem)->Content)->Data(), nullptr, 10);
  138. }
  139. void SDLPal::MainPage::cbBGM_SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
  140. {
  141. auto visibility = (cbBGM->SelectedIndex == MUSIC_RIX) ? Windows::UI::Xaml::Visibility::Visible : Windows::UI::Xaml::Visibility::Collapsed;
  142. cbOPL->Visibility = visibility;
  143. cbOPLSR->Visibility = visibility;
  144. tsSurroundOPL->Visibility = visibility;
  145. }
  146. void SDLPal::MainPage::btnDefault_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  147. {
  148. PAL_LoadConfig(FALSE);
  149. LoadControlContents(true);
  150. }
  151. void SDLPal::MainPage::btnReset_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  152. {
  153. PAL_LoadConfig(TRUE);
  154. LoadControlContents();
  155. }
  156. void SDLPal::MainPage::btnFinish_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  157. {
  158. if (tbGamePath->Text->Length() > 0)
  159. {
  160. auto fal = Windows::Storage::AccessCache::StorageApplicationPermissions::FutureAccessList;
  161. for (auto i = m_acl.begin(); i != m_acl.end(); i++)
  162. {
  163. auto item = i->second;
  164. auto check = item->check ? item->check->IsChecked->Value : true;
  165. if (check && item->text->Tag)
  166. fal->AddOrReplace(item->token, safe_cast<Windows::Storage::IStorageItem^>(item->text->Tag), item->text->Text);
  167. else if (fal->ContainsItem(item->token))
  168. fal->Remove(item->token);
  169. }
  170. SaveControlContents();
  171. gConfig.fLaunchSetting = FALSE;
  172. PAL_SaveConfig();
  173. auto dlg = ref new Windows::UI::Popups::MessageDialog(m_resLdr->GetString("MBExitContent"));
  174. dlg->Title = m_resLdr->GetString("MBExitTitle");
  175. concurrency::create_task(dlg->ShowAsync()).then([] (Windows::UI::Popups::IUICommand^ command) {
  176. Application::Current->Exit();
  177. });
  178. }
  179. else
  180. {
  181. (ref new Windows::UI::Popups::MessageDialog(m_resLdr->GetString("MBEmptyContent")))->ShowAsync();
  182. }
  183. }
  184. void SDLPal::MainPage::btnClearFile_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  185. {
  186. tbMsgFile->Text = "";
  187. tbMsgFile->Tag = nullptr;
  188. }
  189. void SDLPal::MainPage::SetPath(Windows::Storage::StorageFolder^ folder)
  190. {
  191. if (folder)
  192. {
  193. tbGamePath->Text = folder->Path;
  194. tbGamePath->Tag = folder;
  195. }
  196. }
  197. void SDLPal::MainPage::SetFile(Windows::UI::Xaml::Controls::TextBox^ target, Windows::Storage::StorageFile^ file)
  198. {
  199. if (target && file)
  200. {
  201. target->Text = file->Path;
  202. target->Tag = file;
  203. }
  204. }
  205. void SDLPal::MainPage::btnBrowseFolder_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  206. {
  207. auto picker = ref new Windows::Storage::Pickers::FolderPicker();
  208. picker->FileTypeFilter->Append("*");
  209. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  210. picker->ContinuationData->Insert("Page", this);
  211. picker->PickFolderAndContinue();
  212. #else
  213. concurrency::create_task(picker->PickSingleFolderAsync()).then([this](Windows::Storage::StorageFolder^ folder) { SetPath(folder); });
  214. #endif
  215. }
  216. void SDLPal::MainPage::btnBrowseFileOpen_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  217. {
  218. auto button = static_cast<Windows::UI::Xaml::Controls::Button^>(sender);
  219. auto target = m_controls->Lookup(button->Name);
  220. auto picker = ref new Windows::Storage::Pickers::FileOpenPicker();
  221. picker->FileTypeFilter->ReplaceAll(target->Filter);
  222. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  223. picker->ContinuationData->Insert("Page", this);
  224. picker->ContinuationData->Insert("Target", target->Object);
  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("Page", this);
  242. picker->ContinuationData->Insert("Target", target->Object);
  243. picker->PickSaveFileAndContinue();
  244. #else
  245. concurrency::create_task(picker->PickSaveFileAsync()).then(
  246. [this, target](Windows::Storage::StorageFile^ file) {
  247. SetFile(static_cast<Windows::UI::Xaml::Controls::TextBox^>(target->Object), file);
  248. }
  249. );
  250. #endif
  251. }
  252. void SDLPal::MainPage::cbUseFile_CheckChanged(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  253. {
  254. auto checker = static_cast<Windows::UI::Xaml::Controls::CheckBox^>(sender);
  255. auto attr = m_controls->Lookup(checker->Name);
  256. attr->Object->Visibility = checker->IsChecked->Value ? Windows::UI::Xaml::Visibility::Visible : Windows::UI::Xaml::Visibility::Collapsed;
  257. }
  258. void SDLPal::MainPage::Page_Loaded(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  259. {
  260. #if NTDDI_VERSION >= NTDDI_WIN10
  261. if (!Windows::Foundation::Metadata::ApiInformation::IsTypePresent("Windows.UI.ViewManagement.StatusBar")) return;
  262. #endif
  263. #if NTDDI_VERSION >= NTDDI_WIN10 || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  264. auto statusBar = Windows::UI::ViewManagement::StatusBar::GetForCurrentView();
  265. concurrency::create_task(statusBar->ShowAsync()).then([statusBar]() { statusBar->BackgroundOpacity = 1.0; });
  266. #endif
  267. }