|
| 1 | +using System.ComponentModel; |
| 2 | +using Avalonia; |
| 3 | +using Avalonia.Media; |
| 4 | +using WheelWizard.MiiImages; |
| 5 | +using WheelWizard.MiiImages.Domain; |
| 6 | +using WheelWizard.WiiManagement.MiiManagement.Domain.Mii; |
| 7 | + |
| 8 | +namespace WheelWizard.Views.BehaviorComponent; |
| 9 | + |
| 10 | +public partial class MiiImageLoaderWithHover : BaseMiiImage |
| 11 | +{ |
| 12 | + public static readonly StyledProperty<bool> IsHoveredProperty = AvaloniaProperty.Register<MiiImageLoaderWithHover, bool>( |
| 13 | + nameof(IsHovered), |
| 14 | + false |
| 15 | + ); |
| 16 | + |
| 17 | + public bool IsHovered |
| 18 | + { |
| 19 | + get => GetValue(IsHoveredProperty); |
| 20 | + set => SetValue(IsHoveredProperty, value); |
| 21 | + } |
| 22 | + |
| 23 | + public static readonly StyledProperty<bool> ShowNormalImageProperty = AvaloniaProperty.Register<MiiImageLoaderWithHover, bool>( |
| 24 | + nameof(ShowNormalImage), |
| 25 | + true |
| 26 | + ); |
| 27 | + |
| 28 | + public bool ShowNormalImage |
| 29 | + { |
| 30 | + get => GetValue(ShowNormalImageProperty); |
| 31 | + private set => SetValue(ShowNormalImageProperty, value); |
| 32 | + } |
| 33 | + |
| 34 | + public static readonly StyledProperty<bool> ShowHoverImageProperty = AvaloniaProperty.Register<MiiImageLoaderWithHover, bool>( |
| 35 | + nameof(ShowHoverImage), |
| 36 | + false |
| 37 | + ); |
| 38 | + |
| 39 | + public bool ShowHoverImage |
| 40 | + { |
| 41 | + get => GetValue(ShowHoverImageProperty); |
| 42 | + private set => SetValue(ShowHoverImageProperty, value); |
| 43 | + } |
| 44 | + |
| 45 | + private void UpdateImageVisibility() |
| 46 | + { |
| 47 | + var hasHoverImage = GeneratedImages.Count > 1 && GeneratedImages[1] != null; |
| 48 | + |
| 49 | + if (IsHovered && hasHoverImage) |
| 50 | + { |
| 51 | + ShowNormalImage = false; |
| 52 | + ShowHoverImage = true; |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + ShowNormalImage = true; |
| 57 | + ShowHoverImage = false; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public static readonly StyledProperty<IBrush> LoadingColorProperty = AvaloniaProperty.Register<MiiImageLoaderWithHover, IBrush>( |
| 62 | + nameof(LoadingColor), |
| 63 | + new SolidColorBrush(ViewUtils.Colors.Neutral900) |
| 64 | + ); |
| 65 | + |
| 66 | + public IBrush LoadingColor |
| 67 | + { |
| 68 | + get => GetValue(LoadingColorProperty); |
| 69 | + set => SetValue(LoadingColorProperty, value); |
| 70 | + } |
| 71 | + |
| 72 | + public static readonly StyledProperty<IBrush> FallBackColorProperty = AvaloniaProperty.Register<MiiImageLoaderWithHover, IBrush>( |
| 73 | + nameof(FallBackColor), |
| 74 | + new SolidColorBrush(ViewUtils.Colors.Neutral700) |
| 75 | + ); |
| 76 | + |
| 77 | + public IBrush FallBackColor |
| 78 | + { |
| 79 | + get => GetValue(FallBackColorProperty); |
| 80 | + set => SetValue(FallBackColorProperty, value); |
| 81 | + } |
| 82 | + |
| 83 | + public static readonly StyledProperty<Thickness> ImageOnlyMarginProperty = AvaloniaProperty.Register< |
| 84 | + MiiImageLoaderWithHover, |
| 85 | + Thickness |
| 86 | + >(nameof(ImageOnlyMargin), enableDataValidation: true); |
| 87 | + |
| 88 | + public Thickness ImageOnlyMargin |
| 89 | + { |
| 90 | + get => GetValue(ImageOnlyMarginProperty); |
| 91 | + set => SetValue(ImageOnlyMarginProperty, value); |
| 92 | + } |
| 93 | + |
| 94 | + public static readonly StyledProperty<MiiImageSpecifications> ImageVariantProperty = AvaloniaProperty.Register< |
| 95 | + MiiImageLoaderWithHover, |
| 96 | + MiiImageSpecifications |
| 97 | + >(nameof(ImageVariant), MiiImageVariants.OnlinePlayerSmall, coerce: CoerceVariant); |
| 98 | + |
| 99 | + public MiiImageSpecifications ImageVariant |
| 100 | + { |
| 101 | + get => GetValue(ImageVariantProperty); |
| 102 | + set => SetValue(ImageVariantProperty, value); |
| 103 | + } |
| 104 | + |
| 105 | + public static readonly StyledProperty<MiiImageSpecifications?> HoverVariantProperty = AvaloniaProperty.Register< |
| 106 | + MiiImageLoaderWithHover, |
| 107 | + MiiImageSpecifications? |
| 108 | + >(nameof(HoverVariant), coerce: CoerceHoverVariant); |
| 109 | + |
| 110 | + public MiiImageSpecifications? HoverVariant |
| 111 | + { |
| 112 | + get => GetValue(HoverVariantProperty); |
| 113 | + set => SetValue(HoverVariantProperty, value); |
| 114 | + } |
| 115 | + |
| 116 | + private static MiiImageSpecifications? CoerceHoverVariant(AvaloniaObject o, MiiImageSpecifications? value) |
| 117 | + { |
| 118 | + var loader = (MiiImageLoaderWithHover)o; |
| 119 | + // Reload both variants when hover variant changes (if Mii is already set) |
| 120 | + // If Mii is not set yet, OnMiiChanged will handle the reload |
| 121 | + if (loader.Mii != null && value != null) |
| 122 | + { |
| 123 | + loader.ReloadBothVariants(); |
| 124 | + } |
| 125 | + return value; |
| 126 | + } |
| 127 | + |
| 128 | + private static MiiImageSpecifications CoerceVariant(AvaloniaObject o, MiiImageSpecifications value) |
| 129 | + { |
| 130 | + ((MiiImageLoaderWithHover)o).OnVariantChanged(value); |
| 131 | + return value; |
| 132 | + } |
| 133 | + |
| 134 | + public MiiImageLoaderWithHover() |
| 135 | + { |
| 136 | + InitializeComponent(); |
| 137 | + PropertyChanged += MiiImageLoaderWithHover_PropertyChanged; |
| 138 | + GeneratedImages.CollectionChanged += (s, e) => UpdateImageVisibility(); |
| 139 | + MiiImageLoaded += (s, e) => UpdateImageVisibility(); |
| 140 | + } |
| 141 | + |
| 142 | + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) |
| 143 | + { |
| 144 | + base.OnPropertyChanged(change); |
| 145 | + |
| 146 | + if (change.Property == IsHoveredProperty) |
| 147 | + { |
| 148 | + UpdateImageVisibility(); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private void MiiImageLoaderWithHover_PropertyChanged(object? sender, PropertyChangedEventArgs e) |
| 153 | + { |
| 154 | + if (e.PropertyName == nameof(GeneratedImages)) |
| 155 | + { |
| 156 | + UpdateImageVisibility(); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private void OnVariantChanged(MiiImageSpecifications newSpecifications) |
| 161 | + { |
| 162 | + ReloadBothVariants(); |
| 163 | + } |
| 164 | + |
| 165 | + protected override void OnMiiChanged(Mii? newMii) |
| 166 | + { |
| 167 | + // Always reload both variants when Mii changes |
| 168 | + // This ensures both images are loaded even if hover variant is set later |
| 169 | + ReloadBothVariants(); |
| 170 | + } |
| 171 | + |
| 172 | + public void ReloadBothVariants() |
| 173 | + { |
| 174 | + if (Mii == null) |
| 175 | + return; |
| 176 | + |
| 177 | + var variants = new List<MiiImageSpecifications>(); |
| 178 | + |
| 179 | + // Always load the normal variant first |
| 180 | + variants.Add(ImageVariant); |
| 181 | + |
| 182 | + // If hover variant is set, load it as the second image |
| 183 | + if (HoverVariant != null) |
| 184 | + { |
| 185 | + variants.Add(HoverVariant); |
| 186 | + } |
| 187 | + |
| 188 | + ReloadImages(Mii, variants); |
| 189 | + } |
| 190 | +} |
0 commit comments