|
1 | | -usingBetterGenshinImpact.Core.Config; |
| 1 | +usingBetterGenshinImpact.Core.Config; |
2 | 2 | usingBetterGenshinImpact.Core.Monitor; |
3 | 3 | usingBetterGenshinImpact.Core.Recognition.ONNX; |
4 | 4 | usingBetterGenshinImpact.Core.Script; |
|
20 | 20 | usingCommunityToolkit.Mvvm.Messaging.Messages; |
21 | 21 | usingFischless.GameCapture; |
22 | 22 | usingMicrosoft.Extensions.Logging; |
| 23 | +usingMicrosoft.Win32; |
23 | 24 | usingSystem; |
24 | 25 | usingSystem.Collections.Frozen; |
25 | 26 | usingSystem.Collections.Generic; |
|
35 | 36 | usingSystem.Windows.Controls; |
36 | 37 | usingSystem.Windows.Interop; |
37 | 38 | usingSystem.Windows.Media; |
| 39 | +usingSystem.Windows.Media.Imaging; |
38 | 40 | usingWindows.System; |
39 | 41 | usingWpf.Ui.Controls; |
| 42 | +usingWpf.Ui.Violeta.Controls; |
40 | 43 |
|
41 | 44 | namespaceBetterGenshinImpact.ViewModel.Pages; |
42 | 45 |
|
@@ -73,11 +76,18 @@ public partial class HomePageViewModel : ViewModel |
73 | 76 | [ObservableProperty] |
74 | 77 | privateInferenceDeviceType[]_inferenceDeviceTypes=Enum.GetValues<InferenceDeviceType>(); |
75 | 78 |
|
| 79 | +[ObservableProperty] |
| 80 | +privateImageSource_bannerImageSource; |
| 81 | + |
| 82 | +privateconststringDefaultBannerImagePath="pack://application:,,,/Resources/Images/banner.jpg"; |
| 83 | +privateconststringCustomBannerImagePath="UserData/Images/custom_banner.jpg"; |
| 84 | + |
76 | 85 | publicHomePageViewModel(IConfigServiceconfigService,TaskTriggerDispatchertaskTriggerDispatcher) |
77 | 86 | { |
78 | 87 | _taskDispatcher=taskTriggerDispatcher; |
79 | 88 | Config=configService.Get(); |
80 | 89 | ReadGameInstallPath(); |
| 90 | +InitializeBannerImage(); |
81 | 91 |
|
82 | 92 |
|
83 | 93 | // WindowsGraphicsCapture 只支持 Win10 18362 及以上的版本 (Windows 10 version 1903 or later) |
@@ -509,4 +519,123 @@ public void OnOpenHardwareAccelerationSettings() |
509 | 519 | }; |
510 | 520 | varresult=dialogWindow.ShowDialog(); |
511 | 521 | } |
| 522 | + |
| 523 | + #region 背景图片管理 |
| 524 | + |
| 525 | +privatevoidInitializeBannerImage() |
| 526 | +{ |
| 527 | +try |
| 528 | +{ |
| 529 | +// 检查是否存在自定义图片 |
| 530 | +if(File.Exists(CustomBannerImagePath)) |
| 531 | +{ |
| 532 | +BannerImageSource=newBitmapImage(newUri(Path.GetFullPath(CustomBannerImagePath))); |
| 533 | +_logger.LogInformation("已加载自定义背景图片"); |
| 534 | +} |
| 535 | +else |
| 536 | +{ |
| 537 | +// 使用默认图片 |
| 538 | +BannerImageSource=newBitmapImage(newUri(DefaultBannerImagePath,UriKind.Absolute)); |
| 539 | +_logger.LogInformation("已加载默认背景图片"); |
| 540 | +} |
| 541 | +} |
| 542 | +catch(Exceptionex) |
| 543 | +{ |
| 544 | +_logger.LogError(ex,"初始化背景图片失败,使用默认图片"); |
| 545 | +BannerImageSource=newBitmapImage(newUri(DefaultBannerImagePath,UriKind.Absolute)); |
| 546 | +} |
| 547 | +} |
| 548 | + |
| 549 | +[RelayCommand] |
| 550 | +privatevoidChangeBannerImage() |
| 551 | +{ |
| 552 | +try |
| 553 | +{ |
| 554 | +varopenFileDialog=newOpenFileDialog |
| 555 | +{ |
| 556 | +Title="选择背景图片", |
| 557 | +Filter="图片文件|*.jpg;*.jpeg;*.png;*.bmp;*.gif|所有文件|*.*", |
| 558 | +Multiselect=false |
| 559 | +}; |
| 560 | + |
| 561 | +if(openFileDialog.ShowDialog()==true) |
| 562 | +{ |
| 563 | +varselectedFile=openFileDialog.FileName; |
| 564 | +_logger.LogInformation("用户选择了图片: {ImagePath}",selectedFile); |
| 565 | + |
| 566 | +// 确保目标目录存在 |
| 567 | +vardirectory=Path.GetDirectoryName(CustomBannerImagePath); |
| 568 | +if(!string.IsNullOrEmpty(directory)&&!Directory.Exists(directory)) |
| 569 | +{ |
| 570 | +Directory.CreateDirectory(directory); |
| 571 | +_logger.LogInformation("创建了自定义图片目录: {Directory}",directory); |
| 572 | +} |
| 573 | + |
| 574 | +// 复制图片到自定义路径 |
| 575 | +File.Copy(selectedFile,CustomBannerImagePath,true); |
| 576 | +_logger.LogInformation("图片已复制到: {CustomPath}",CustomBannerImagePath); |
| 577 | + |
| 578 | +// 更新UI |
| 579 | +BannerImageSource=newBitmapImage(newUri(Path.GetFullPath(CustomBannerImagePath))); |
| 580 | +Toast.Success("背景图片更换成功!"); |
| 581 | +_logger.LogInformation("背景图片更换成功"); |
| 582 | +} |
| 583 | +} |
| 584 | +catch(Exceptionex) |
| 585 | +{ |
| 586 | +_logger.LogError(ex,"更换背景图片失败"); |
| 587 | +Toast.Error($"更换背景图片失败:{ex.Message}"); |
| 588 | +} |
| 589 | +} |
| 590 | + |
| 591 | +[RelayCommand] |
| 592 | +privatevoidResetBannerImage() |
| 593 | +{ |
| 594 | +try |
| 595 | +{ |
| 596 | +// 获取自定义图片的完整路径 |
| 597 | +varcustomImageFullPath=Path.GetFullPath(CustomBannerImagePath); |
| 598 | +_logger.LogInformation("尝试恢复默认背景图片,自定义图片路径: {CustomPath}",customImageFullPath); |
| 599 | + |
| 600 | +if(File.Exists(customImageFullPath)) |
| 601 | +{ |
| 602 | +File.Delete(customImageFullPath); |
| 603 | +_logger.LogInformation("已删除自定义背景图片: {CustomPath}",customImageFullPath); |
| 604 | +} |
| 605 | +else |
| 606 | +{ |
| 607 | +_logger.LogInformation("自定义背景图片不存在: {CustomPath}",customImageFullPath); |
| 608 | +} |
| 609 | + |
| 610 | +// 恢复为默认图片 |
| 611 | +_logger.LogInformation("正在恢复为默认背景图片,默认路径: {DefaultPath}",DefaultBannerImagePath); |
| 612 | +BannerImageSource=newBitmapImage(newUri(DefaultBannerImagePath,UriKind.Absolute)); |
| 613 | +Toast.Success("已恢复为默认背景图片!"); |
| 614 | +_logger.LogInformation("背景图片已恢复为默认"); |
| 615 | +} |
| 616 | +catch(Exceptionex) |
| 617 | +{ |
| 618 | +_logger.LogError(ex,"恢复默认背景图片失败,尝试使用备用方案"); |
| 619 | + |
| 620 | +// 备用方案:尝试重新创建默认图片的BitmapImage |
| 621 | +try |
| 622 | +{ |
| 623 | +varbitmap=newBitmapImage(); |
| 624 | +bitmap.BeginInit(); |
| 625 | +bitmap.UriSource=newUri(DefaultBannerImagePath,UriKind.Absolute); |
| 626 | +bitmap.CacheOption=BitmapCacheOption.OnLoad; |
| 627 | +bitmap.EndInit(); |
| 628 | +BannerImageSource=bitmap; |
| 629 | +Toast.Success("已恢复为默认背景图片!"); |
| 630 | +_logger.LogInformation("使用备用方案恢复默认背景图片成功"); |
| 631 | +} |
| 632 | +catch(ExceptionfallbackEx) |
| 633 | +{ |
| 634 | +_logger.LogError(fallbackEx,"备用方案也失败"); |
| 635 | +Toast.Error($"恢复默认背景图片失败:{ex.Message}"); |
| 636 | +} |
| 637 | +} |
| 638 | +} |
| 639 | + |
| 640 | + #endregion |
512 | 641 | } |