Skip to content

Commit

Permalink
a new game icon of title bar
Browse files Browse the repository at this point in the history
  • Loading branch information
Scighost committed Dec 31, 2023
1 parent ae7e025 commit 8ea84ee
Show file tree
Hide file tree
Showing 8 changed files with 622 additions and 309 deletions.
55 changes: 55 additions & 0 deletions src/Starward/Controls/TitleBarGameIcon/TitleBarGameIconBH3.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<UserControl x:Class="Starward.Controls.TitleBarGameIcon.TitleBarGameIconBH3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:Starward.Controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sc="using:Starward.Core"
x:DefaultBindMode="OneWay"
mc:Ignorable="d">

<Button Padding="0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
BorderThickness="0"
Command="{x:Bind ClickCommand}"
CommandParameter="{x:Bind GameBiz}"
CornerRadius="8"
DoubleTapped="Button_DoubleTapped"
PointerEntered="Button_PointerEntered"
PointerExited="Button_PointerExited"
RightTapped="Button_RightTapped"
Style="{ThemeResource DateTimePickerFlyoutButtonStyle}">
<Grid>
<Image Source="ms-appx:///Assets/Image/icon_bh3.jpg" />
<Border Name="Border_Mask" Background="#60000000">
<Border.OpacityTransition>
<ScalarTransition />
</Border.OpacityTransition>
</Border>
</Grid>
<Button.ContextFlyout>
<MenuFlyout Closed="MenuFlyout_Closed">
<MenuFlyoutItem Command="{x:Bind ClickCommand}"
CommandParameter="{x:Bind sc:GameBiz.bh3_cn}"
Text="China" />
<MenuFlyoutItem Command="{x:Bind ClickCommand}"
CommandParameter="{x:Bind sc:GameBiz.bh3_global}"
Text="Europe &amp; Americas" />
<MenuFlyoutItem Command="{x:Bind ClickCommand}"
CommandParameter="{x:Bind sc:GameBiz.bh3_jp}"
Text="Japan" />
<MenuFlyoutItem Command="{x:Bind ClickCommand}"
CommandParameter="{x:Bind sc:GameBiz.bh3_kr}"
Text="Korea" />
<MenuFlyoutItem Command="{x:Bind ClickCommand}"
CommandParameter="{x:Bind sc:GameBiz.bh3_overseas}"
Text="Southeast Asia" />
<MenuFlyoutItem Command="{x:Bind ClickCommand}"
CommandParameter="{x:Bind sc:GameBiz.bh3_tw}"
Text="Traditional Chinese" />
</MenuFlyout>
</Button.ContextFlyout>
</Button>

</UserControl>
140 changes: 140 additions & 0 deletions src/Starward/Controls/TitleBarGameIcon/TitleBarGameIconBH3.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using CommunityToolkit.Mvvm.Input;
using Microsoft.UI.Composition;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Hosting;
using Microsoft.UI.Xaml.Input;
using Starward.Core;
using System;
using System.Numerics;
using System.Windows.Input;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace Starward.Controls.TitleBarGameIcon;

public sealed partial class TitleBarGameIconBH3 : UserControl
{


private readonly Compositor compositor;

public GameBiz GameBiz { get; set; } = GameBiz.Honkai3rd;


public TitleBarGameIconBH3()
{
this.InitializeComponent();
compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;
this.Loaded += (_, _) => UpdateCornerRadius(false);
}



public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}

public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(TitleBarGameIconBH3), new PropertyMetadata(default));


public void Select(GameBiz biz)
{
IsSelected = biz.ToGame() == GameBiz;
}


private bool isSelected;
public bool IsSelected
{
get => isSelected;
set
{
isSelected = value;
UpdateCornerRadius(value);
Border_Mask.Opacity = value ? 0 : 1;
}
}



private bool isTapped;




[RelayCommand]
private void Click(GameBiz biz)
{
IsSelected = true;
Command?.Execute(biz);
}


private void Button_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
Click(GameBiz);
}

private void Button_PointerEntered(object sender, PointerRoutedEventArgs e)
{
UpdateCornerRadius(true);
}

private void Button_PointerExited(object sender, PointerRoutedEventArgs e)
{
if (!isTapped)
{
UpdateCornerRadius(isSelected);
}
}

private void Button_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
isTapped = true;
}

private void MenuFlyout_Closed(object sender, object e)
{
isTapped = false;
UpdateCornerRadius(isSelected);
}



private void UpdateCornerRadius(bool isSelect)
{
var visual = ElementCompositionPreview.GetElementVisual(this);
CompositionRoundedRectangleGeometry geometry;
if (visual.Clip is CompositionGeometricClip clip && clip.Geometry is CompositionRoundedRectangleGeometry geo)
{
geometry = geo;
geometry.Size = new Vector2((float)ActualWidth, (float)ActualHeight);
}
else
{
geometry = compositor.CreateRoundedRectangleGeometry();
geometry.Size = new Vector2((float)ActualWidth, (float)ActualHeight);
geometry.CornerRadius = Vector2.Zero;
clip = compositor.CreateGeometricClip(geometry);
visual.Clip = clip;
}
var animation = compositor.CreateVector2KeyFrameAnimation();
animation.Duration = TimeSpan.FromSeconds(0.3);
if (isSelect)
{
animation.InsertKeyFrame(1, new Vector2(8, 8));
}
else
{
animation.InsertKeyFrame(1, new Vector2((float)ActualWidth / 2, (float)ActualHeight / 2));
}
geometry.StartAnimation(nameof(CompositionRoundedRectangleGeometry.CornerRadius), animation);
}


}
43 changes: 43 additions & 0 deletions src/Starward/Controls/TitleBarGameIcon/TitleBarGameIconSR.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<UserControl x:Class="Starward.Controls.TitleBarGameIcon.TitleBarGameIconSR"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:Starward.Controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sc="using:Starward.Core"
x:DefaultBindMode="OneWay"
mc:Ignorable="d">

<Button Padding="0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
BorderThickness="0"
Command="{x:Bind ClickCommand}"
CommandParameter="{x:Bind GameBiz}"
CornerRadius="8"
DoubleTapped="Button_DoubleTapped"
PointerEntered="Button_PointerEntered"
PointerExited="Button_PointerExited"
RightTapped="Button_RightTapped"
Style="{ThemeResource DateTimePickerFlyoutButtonStyle}">
<Grid>
<Image Source="ms-appx:///Assets/Image/icon_sr.jpg" />
<Border Name="Border_Mask" Background="#60000000">
<Border.OpacityTransition>
<ScalarTransition />
</Border.OpacityTransition>
</Border>
</Grid>
<Button.ContextFlyout>
<MenuFlyout Closed="MenuFlyout_Closed">
<MenuFlyoutItem Command="{x:Bind ClickCommand}"
CommandParameter="{x:Bind sc:GameBiz.hkrpg_cn}"
Text="China" />
<MenuFlyoutItem Command="{x:Bind ClickCommand}"
CommandParameter="{x:Bind sc:GameBiz.hkrpg_global}"
Text="Global" />
</MenuFlyout>
</Button.ContextFlyout>
</Button>

</UserControl>
140 changes: 140 additions & 0 deletions src/Starward/Controls/TitleBarGameIcon/TitleBarGameIconSR.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using CommunityToolkit.Mvvm.Input;
using Microsoft.UI.Composition;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Hosting;
using Microsoft.UI.Xaml.Input;
using Starward.Core;
using System;
using System.Numerics;
using System.Windows.Input;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace Starward.Controls.TitleBarGameIcon;

public sealed partial class TitleBarGameIconSR : UserControl
{


private readonly Compositor compositor;

public GameBiz GameBiz { get; set; } = GameBiz.StarRail;


public TitleBarGameIconSR()
{
this.InitializeComponent();
compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;
this.Loaded += (_, _) => UpdateCornerRadius(false);
}



public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}

public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(TitleBarGameIconBH3), new PropertyMetadata(default));


public void Select(GameBiz biz)
{
IsSelected = biz.ToGame() == GameBiz;
}


private bool isSelected;
public bool IsSelected
{
get => isSelected;
set
{
isSelected = value;
UpdateCornerRadius(value);
Border_Mask.Opacity = value ? 0 : 1;
}
}



private bool isTapped;




[RelayCommand]
private void Click(GameBiz biz)
{
IsSelected = true;
Command?.Execute(biz);
}


private void Button_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
Click(GameBiz);
}

private void Button_PointerEntered(object sender, PointerRoutedEventArgs e)
{
UpdateCornerRadius(true);
}

private void Button_PointerExited(object sender, PointerRoutedEventArgs e)
{
if (!isTapped)
{
UpdateCornerRadius(isSelected);
}
}

private void Button_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
isTapped = true;
}

private void MenuFlyout_Closed(object sender, object e)
{
isTapped = false;
UpdateCornerRadius(isSelected);
}



private void UpdateCornerRadius(bool isSelect)
{
var visual = ElementCompositionPreview.GetElementVisual(this);
CompositionRoundedRectangleGeometry geometry;
if (visual.Clip is CompositionGeometricClip clip && clip.Geometry is CompositionRoundedRectangleGeometry geo)
{
geometry = geo;
geometry.Size = new Vector2((float)ActualWidth, (float)ActualHeight);
}
else
{
geometry = compositor.CreateRoundedRectangleGeometry();
geometry.Size = new Vector2((float)ActualWidth, (float)ActualHeight);
geometry.CornerRadius = Vector2.Zero;
clip = compositor.CreateGeometricClip(geometry);
visual.Clip = clip;
}
var animation = compositor.CreateVector2KeyFrameAnimation();
animation.Duration = TimeSpan.FromSeconds(0.3);
if (isSelect)
{
animation.InsertKeyFrame(1, new Vector2(8, 8));
}
else
{
animation.InsertKeyFrame(1, new Vector2((float)ActualWidth / 2, (float)ActualHeight / 2));
}
geometry.StartAnimation(nameof(CompositionRoundedRectangleGeometry.CornerRadius), animation);
}


}
Loading

0 comments on commit 8ea84ee

Please sign in to comment.