From 69453a1baa9a4163998f04cd2f4fb4e79dd245a9 Mon Sep 17 00:00:00 2001 From: tanglong <842690096@qq.com> Date: Tue, 14 Oct 2025 20:23:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E4=BA=BA=E8=B7=B3=E7=BB=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dto/GroupJumpRopeContext.cs | 31 +++++-- .../Views/JumpRope/GroupJumpRope.xaml.cs | 87 ++++++++----------- 2 files changed, 60 insertions(+), 58 deletions(-) diff --git a/Wpf_AiSportsMicrospace/Dto/GroupJumpRopeContext.cs b/Wpf_AiSportsMicrospace/Dto/GroupJumpRopeContext.cs index 90eafb1..1f08581 100644 --- a/Wpf_AiSportsMicrospace/Dto/GroupJumpRopeContext.cs +++ b/Wpf_AiSportsMicrospace/Dto/GroupJumpRopeContext.cs @@ -9,13 +9,30 @@ namespace Dto { public class GroupJumpRopeContext { - public int Index { get; set; } - public List<(double XNorm, double YNorm)> CirclePositions { get; set; } - public List Sports{ get; set; } - public List User { get; set; } + public List<(double XNorm, double YNorm)> CirclePositions { get; private set; } + public List UserList { get; private set; } + public List UserNumberList { get; private set; } + public List Sports { get; private set; } + public List UseNameList { get; private set; } = new() + { + "一号位", "四号位", "二号位", "五号位", "三号位", "六号位" + }; + public GroupJumpRopeContext() + { + CirclePositions = new List<(double XNorm, double YNorm)> + { + (0.21, 0.88 ), + (0.36, 0.68 ), + (0.50, 0.88), + (0.64, 0.68 ), + (0.78, 0.88), + (0.92, 0.68 ) + }; - public string LastNumberText { get; set; } = ""; - public DateTime LastJumpTime { get; set; } = DateTime.MinValue; - public DateTime LastUIUpdateTime { get; set; } = DateTime.MinValue; + UserList = new List(); + UserNumberList = new List(); + Sports = new List(); + + } } } diff --git a/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs b/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs index 9bc6a4b..6f4a39f 100644 --- a/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs +++ b/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs @@ -1,4 +1,5 @@ -using Emgu.CV.Flann; +using Dto; +using Emgu.CV.Flann; using HandyControl.Controls; using SharpDX.Direct3D9; using System; @@ -41,24 +42,18 @@ namespace Wpf_AiSportsMicrospace.Views /// public partial class GroupJumpRope : UserControl { - private List sports = new(); - private List circleTexts = new(); - private List userList = new(); - private List userNumberList = ["0", "0", "0", "0", "0", "0"]; private Main _mainWin => Application.Current.MainWindow as Main; - private List<(double XNorm, double YNorm)> circlePositions = new(); - private MediaPlayer _mediaPlayer = new MediaPlayer(); private bool IsGameStarted = false; - List _useName = ["一号位", "四号位", "二号位", "五号位", "三号位", "六号位",]; - private readonly object _updateLock = new object(); private readonly Dictionary _jumpStatus = new Dictionary(); + private GroupJumpRopeContext _groupJumpRopeContext; public GroupJumpRope() { InitializeComponent(); Loaded += UserControl_Loaded; Unloaded += UserControl_Unloaded; + _groupJumpRopeContext = new GroupJumpRopeContext(); } private async void UserControl_Loaded(object sender, RoutedEventArgs e) @@ -263,7 +258,7 @@ namespace Wpf_AiSportsMicrospace.Views countdownGrid.Visibility = Visibility.Hidden; - userList.ForEach(x => + _groupJumpRopeContext.UserList.ForEach(x => { x.ImageState = "1"; }); @@ -348,29 +343,17 @@ namespace Wpf_AiSportsMicrospace.Views private void DrawCirclesWithText() { userBox.Children.Clear(); - sports.Clear(); - circleTexts.Clear(); - userList.Clear(); // 清空用户控件列表 + _groupJumpRopeContext.Sports.Clear(); + _groupJumpRopeContext.UserList.Clear(); + _groupJumpRopeContext.UserNumberList.Clear(); double imgWidth = userBox.ActualWidth; double imgHeight = userBox.ActualHeight; double radius = 100; - // 每个圆的位置:X 和 Y 都归一化 0~1 - circlePositions = new List<(double XNorm, double YNorm)> - { - //(0.07, 0.58), - (0.21, 0.88 ), - (0.36, 0.68 ), - (0.50, 0.88), - (0.64, 0.68 ), - (0.78, 0.88), - (0.92, 0.68 ) - }; - - for (int i = 0; i < circlePositions.Count; i++) + for (int i = 0; i < _groupJumpRopeContext.CirclePositions.Count; i++) { - var pos = circlePositions[i]; + var pos = _groupJumpRopeContext.CirclePositions[i]; double x = pos.XNorm * imgWidth; double y = pos.YNorm * imgHeight; @@ -385,15 +368,17 @@ namespace Wpf_AiSportsMicrospace.Views // 订阅事件 sport.OnTicked += (count, times) => { - currentItem.NumberText = count.ToString(); - - if (currentItem.ImageState != "2") - { - currentItem.ImageState = "2"; - } + // 更新UI + userItem.NumberText = count.ToString(); + // 更新数字源 + _groupJumpRopeContext.UserNumberList[indexCopy] = count.ToString(); + // 改变状态为“跳绳中” + if (userItem.ImageState != "2") + userItem.ImageState = "2"; }; + sport.Start(); - sports.Add(sport); + _groupJumpRopeContext.Sports.Add(sport); } } private void UpdateCircleCounts(List humans) @@ -401,10 +386,10 @@ namespace Wpf_AiSportsMicrospace.Views double radiusNormX = 0.07; double radiusNormY = 0.14; - for (int i = 0; i < circlePositions.Count; i++) + for (int i = 0; i < _groupJumpRopeContext.CirclePositions.Count; i++) { - var circleX = circlePositions[i].XNorm; - var circleY = circlePositions[i].YNorm; + var circleX = _groupJumpRopeContext.CirclePositions[i].XNorm; + var circleY = _groupJumpRopeContext.CirclePositions[i].YNorm; Human humanInCircle = null; @@ -438,12 +423,12 @@ namespace Wpf_AiSportsMicrospace.Views bool hasHuman = humanInCircle != null; lock (_updateLock) { - userList[i].ImageState = GetJumpState(i, hasHuman); + _groupJumpRopeContext.UserList[i].ImageState = GetJumpState(i, hasHuman); } // 推送计数 if (hasHuman) - sports[i].Pushing(humanInCircle); + _groupJumpRopeContext.Sports[i].Pushing(humanInCircle); } } private string GetJumpState(int circleIndex, bool humanInCircle) @@ -454,26 +439,25 @@ namespace Wpf_AiSportsMicrospace.Views return "3"; } - string currentNumber = userNumberList[circleIndex]; + string currentNumber = _groupJumpRopeContext.UserNumberList[circleIndex]; if (!_jumpStatus.ContainsKey(circleIndex)) { _jumpStatus[circleIndex] = (currentNumber, DateTime.Now, "1"); // 初始状态先为停止 return "1"; } - var (lastNumber, lastChangeTime, lastState) = _jumpStatus[circleIndex]; - //if (currentNumber != lastNumber) - //{ - // // 数字变化 → 跳绳中 - // _jumpStatus[circleIndex] = (currentNumber, DateTime.Now, "2"); - // return "2"; - //} + if (currentNumber != lastNumber) + { + // 数字变化 → 跳绳中 + _jumpStatus[circleIndex] = (currentNumber, DateTime.Now, "2"); + return "2"; + } // 数字未变化,判断是否超过 2 秒 double elapsed = (DateTime.Now - lastChangeTime).TotalSeconds; - if (elapsed >= 2) + if (elapsed >= 0.8) { // 超过 2 秒未变化 → 停止 _jumpStatus[circleIndex] = (currentNumber, lastChangeTime, "1"); @@ -491,14 +475,15 @@ namespace Wpf_AiSportsMicrospace.Views var userItem = new SportUserItem(); userItem.Width = 270; userItem.Height = 560; - userItem.DisplayText = _useName[index]; + userItem.DisplayText = _groupJumpRopeContext.UseNameList[index]; userItem.VerticalAlignment = VerticalAlignment.Top; userItem.HorizontalAlignment = HorizontalAlignment.Left; userItem.ImageState = "1"; //userItem.Margin = new Thickness(centerX - (index == 0 ? 80 : index == 6 ? 190 : 135), centerY - 540, 0, 0); - userItem.Margin = new Thickness(centerX - 265, centerY - 500, 0, 0); + userItem.Margin = new Thickness(centerX - 265, centerY - 500, 0, 0); userBox.Children.Add(userItem); - userList.Add(userItem); + _groupJumpRopeContext.UserList.Add(userItem); + _groupJumpRopeContext.UserNumberList.Add("0"); return userItem; // } }