Files
MRCC/launcher/PCL-CE/Plain Craft Launcher 2/Controls/MyButton.xaml.cs
T
xyou f70b061d1a
CI / Go Backend (push) Canceled after 0s
feat: 项目初始化 + 3D方块世界原型 + AI助搭系统
初始化 monorepo: Go后端(7微服务) + Unity客户端(9模块) + 启动器

HTML5原型: Three.js 3D体素世界, Perlin噪声地形, 原版材质, 22种方块

Minecraft创造模式背包: 双栏布局, 拖拽移动物品, 方向性元件引脚

AI助搭策划文档 + 客户端/服务端骨架 + Docker Compose + CI
2026-08-08 14:07:56 +08:00

210 lines
6.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
namespace PCL;
[ContentProperty("Inlines")]
public partial class MyButton
{
public delegate void ClickEventHandler(object sender, MouseButtonEventArgs e); // 自定义事件
public enum ColorState
{
Normal = 0,
Highlight = 1,
Red = 2
}
// 自定义事件
private const int animationColorIn = 100;
private const int animationColorOut = 200;
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string),
typeof(MyButton), new PropertyMetadata((sender, e) =>
{
if (sender is not null) ((MyButton)sender).LabText.Text = (string)e.NewValue;
}));
// 属性穿透
public new static readonly DependencyProperty PaddingProperty = DependencyProperty.Register("Padding",
typeof(Thickness), typeof(MyButton), new PropertyMetadata((sender, e) =>
{
if (sender is not null) ((MyButton)sender).PanFore.Padding = (Thickness)e.NewValue;
}));
private ColorState _ColorType = ColorState.Normal; // 配色方案
// 鼠标点击判定(务必放在点击事件之后,以使得 Button_MouseUp 先于 Button_MouseLeave 执行)
// 自定义属性
public int Uuid = ModBase.GetUuid();
public MyButton()
{
InitializeComponent();
MouseEnter += RefreshColor;
MouseLeave += RefreshColor;
Loaded += RefreshColor;
IsEnabledChanged += (_, _) => RefreshColor();
MouseLeftButtonUp += Button_MouseUp;
MouseLeftButtonDown += Button_MouseDown;
MouseEnter += (_, _) => Button_MouseEnter();
MouseLeftButtonUp += (_, _) => Button_MouseUp();
MouseLeave += (_, _) => Button_MouseLeave();
}
public InlineCollection Inlines => LabText.Inlines;
public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
} // 显示文本
public Thickness TextPadding
{
get => LabText.Padding;
set => LabText.Padding = value;
}
public ColorState ColorType
{
get => _ColorType;
set
{
_ColorType = value;
RefreshColor();
}
}
public new Thickness Padding
{
get => PanFore.Padding;
set => PanFore.Padding = value;
}
public Transform RealRenderTransform
{
get => PanFore.RenderTransform;
set => PanFore.RenderTransform = value;
}
// 声明
public event ClickEventHandler? Click;
private string GetBorderBrushResourceKey()
{
return ColorType switch
{
ColorState.Normal => IsMouseOver ? "ColorBrush3" : "ColorBrush1",
ColorState.Highlight => IsMouseOver ? "ColorBrush3" : "ColorBrush2",
ColorState.Red => IsMouseOver ? "ColorBrushRedLight" : "ColorBrushRedDark",
_ => "ColorBrush1"
};
}
private void StartBorderBrushAnimation(string resourceKey, int duration)
{
ModAnimation.AniStart(
new[]
{
ModAnimation.AaColor(PanFore, BorderBrushProperty, resourceKey, duration)
}, "MyButton Color " + Uuid);
}
private void RefreshColor(object obj = null, object e = null)
{
try
{
if (ControlVisualHelpers.ShouldAnimate(this)) // 防止默认属性变更触发动画
{
if (IsEnabled)
StartBorderBrushAnimation(GetBorderBrushResourceKey(), IsMouseOver ? animationColorIn : animationColorOut);
else
// 不可用(Gray 4
ModAnimation.AniStart(
new[]
{
ModAnimation.AaColor(PanFore, BorderBrushProperty,
ThemeManager.colorGray4 - PanFore.BorderBrush, animationColorOut)
}, "MyButton Color " + Uuid);
}
else
{
ModAnimation.AniStop("MyButton Color " + Uuid);
if (IsEnabled)
PanFore.SetResourceReference(BorderBrushProperty, GetBorderBrushResourceKey());
else
PanFore.BorderBrush = ThemeManager.colorGray4;
}
}
catch (Exception ex)
{
ModBase.Log(ex, "刷新按钮颜色出错");
}
}
// 实现自定义事件
private bool isMouseDown = false;
private void Button_MouseUp(object sender, MouseButtonEventArgs e)
{
if (!isMouseDown)
return;
ModBase.Log("[Control] 按下按钮:" + Text);
Click?.Invoke(sender, e);
ModMain.RaiseCustomEvent(this);
}
private void Button_MouseDown(object sender, MouseButtonEventArgs e)
{
isMouseDown = true;
Focus();
ModAnimation.AniStart(
new[]
{
ModAnimation.AaScaleTransform(PanFore, 0.955d - ((ScaleTransform)PanFore.RenderTransform).ScaleX, 80,
ease: new ModAnimation.AniEaseOutFluent(ModAnimation.AniEasePower.ExtraStrong)),
ModAnimation.AaScaleTransform(PanFore, -0.01d, 700, ease: new ModAnimation.AniEaseOutFluent())
}, "MyButton Scale " + Uuid);
}
private void Button_MouseEnter()
{
ModAnimation.AniStart(
ModAnimation.AaColor(PanFore, BackgroundProperty,
_ColorType == ColorState.Red ? "ColorBrushRedBack" : "ColorBrush7", animationColorIn),
"MyButton Background " + Uuid);
}
private void Button_MouseUp()
{
if (!isMouseDown)
return;
isMouseDown = false;
ModAnimation.AniStart(
new[]
{
ModAnimation.AaScaleTransform(PanFore, 1d - ((ScaleTransform)PanFore.RenderTransform).ScaleX, 300, 10,
new ModAnimation.AniEaseOutFluent())
}, "MyButton Scale " + Uuid);
}
private void Button_MouseLeave()
{
ModAnimation.AniStart(
ModAnimation.AaColor(PanFore, BackgroundProperty, "ColorBrushHalfWhite", animationColorOut),
"MyButton Background " + Uuid);
if (!isMouseDown)
return;
isMouseDown = false;
ModAnimation.AniStart(
ModAnimation.AaScaleTransform(PanFore, 1d - ((ScaleTransform)PanFore.RenderTransform).ScaleX, 800,
ease: new ModAnimation.AniEaseOutFluent(ModAnimation.AniEasePower.Strong)), "MyButton Scale " + Uuid);
}
}