MainPage.xaml.cpp 13 KB

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