将区域编辑集成到步骤页并合并播放暂停按钮

This commit is contained in:
tfarcraw 2026-11-19 17:34:33 +08:00
parent 20c3860158
commit aefd4ebd76
5 changed files with 89 additions and 57 deletions

View File

@ -72,6 +72,7 @@ public sealed partial class CameraViewModel : BindableBase
public DelegateCommand RestartCommand { get; }
public DelegateCommand PlayVideoCommand { get; }
public DelegateCommand PauseVideoCommand { get; }
public DelegateCommand ToggleVideoPlaybackCommand { get; }
public DelegateCommand StopVideoCommand { get; }
public DelegateCommand ImportVideoCommand { get; }
public event Action<string>? RecordingLimitReached;
@ -99,6 +100,11 @@ public sealed partial class CameraViewModel : BindableBase
RestartCommand = Command(RestartRecordingAsync, () => _sopStandard && !IsBusy && !_closing && !IsTestingFlow && HasStandardVideo && IsPreviewing && Preview != null && HasRecentFrame);
PlayVideoCommand = Command(PlayVideoAsync, () => _sopStandard && !IsBusy && !_closing && !IsTestingFlow && !IsRecording && HasStandardVideo && !Playback.IsPlaying);
PauseVideoCommand = Command(() => { Playback.Pause(); Status = "视频播放已暂停"; return Task.CompletedTask; }, () => !IsBusy && !_closing && !IsTestingFlow && Playback.IsPlaying);
ToggleVideoPlaybackCommand = Command(async () =>
{
if (Playback.IsPlaying) { Playback.Pause(); Status = "视频播放已暂停"; }
else await PlayVideoAsync();
}, () => _sopStandard && !IsBusy && !_closing && !IsTestingFlow && !IsRecording && HasStandardVideo);
ImportVideoCommand = Command(ImportVideoAsync, () => _sopStandard && !IsBusy && !_closing && !IsTestingFlow && !IsRecording);
TestFlowCommand = Command(StartFlowTestAsync, () => _sopStandard && !IsBusy && !_closing && !IsTestingFlow && !IsRecording && HasStandardVideo && FlowTestConfigProvider is not null);
StopVideoCommand = Command(async () => { await Playback.StopAsync(); EndFlowTest("测试已停止"); Status = "视频播放已停止"; }, () => !IsBusy && !_closing && Playback.IsLoaded);

View File

@ -301,48 +301,27 @@
</StackPanel>
</ScrollViewer>
</Grid>
<Grid Grid.Column="2" Margin="6,0,0,0" RowDefinitions="Auto,*,Auto,Auto">
<TextBlock Text="标准视频" FontWeight="Bold" Margin="0,0,0,8" />
<Border x:Name="StepVideoViewport" Grid.Row="1" Background="#080E14">
<Image x:Name="StepVideoImage" Source="{Binding SopRecording.Playback.Frame}" Stretch="Uniform" />
<Grid Grid.Column="2" Margin="6,0,0,0" RowDefinitions="Auto,*,Auto,250">
<TextBlock Text="标准视频 / 区域编辑" FontWeight="Bold" Margin="0,0,0,8" />
<Border x:Name="StepVideoViewport" Grid.Row="1" Background="#080E14" ClipToBounds="True" MinHeight="140">
<ed:RegionCanvas x:Name="StepVideoRegions" Image="{Binding StepEditorImage}" Regions="{Binding Recipe.Regions}"
SelectedRegion="{Binding Recipe.SelectedRegion, Mode=TwoWay}" IsHitTestVisible="{Binding !SopRecording.Playback.IsPlaying}" />
</Border>
<StackPanel Grid.Row="2" DataContext="{Binding SopRecording}" Spacing="8" Margin="0,8,0,0">
<StackPanel Orientation="Horizontal" Spacing="8">
<Button Content="播放" Command="{Binding PlayVideoCommand}" />
<Button Content="暂停" Command="{Binding PauseVideoCommand}" />
<Button Content="停止" Command="{Binding StopVideoCommand}" />
</StackPanel>
<Slider Minimum="0" Maximum="100" Value="{Binding Playback.Progress, Mode=TwoWay}" IsEnabled="{Binding CanSeekVideo}" ToolTip.Tip="拖动跳转视频位置" />
<TextBlock Text="{Binding Playback.TimeText}" />
<TextBlock Text="{Binding Status}" TextWrapping="Wrap" />
<StackPanel Grid.Row="2" Spacing="6" Margin="0,8,0,8">
<WrapPanel>
<Button x:Name="StepPlayPauseButton" Content="{Binding SopRecording.PlayVideoButtonText}" Command="{Binding SopRecording.ToggleVideoPlaybackCommand}" Margin="0,0,8,6" />
<Button Content="停止" Command="{Binding SopRecording.StopVideoCommand}" Margin="0,0,8,6" />
<Button Content="获取关键帧" Command="{Binding CaptureStepKeyFrameCommand}" Margin="0,0,0,6" />
</WrapPanel>
<Grid ColumnDefinitions="*,Auto">
<Slider Minimum="0" Maximum="100" Value="{Binding SopRecording.Playback.Progress, Mode=TwoWay}" IsEnabled="{Binding SopRecording.CanSeekVideo}" ToolTip.Tip="拖动跳转视频位置" />
<TextBlock Grid.Column="1" Text="{Binding SopRecording.Playback.TimeText}" Margin="8,0,0,0" VerticalAlignment="Center" />
</Grid>
<TextBlock Text="{Binding Recipe.SelectedStep.KeyFrameLabel}" TextTrimming="CharacterEllipsis" ToolTip.Tip="{Binding StepKeyFrameStatus}" />
<TextBlock Text="{Binding SopRecording.Status}" TextTrimming="CharacterEllipsis" ToolTip.Tip="{Binding SopRecording.Status}" />
</StackPanel>
<StackPanel Grid.Row="3" Spacing="8" Margin="0,8,0,0">
<Button Content="获取关键帧" Command="{Binding CaptureStepKeyFrameCommand}" />
<TextBlock Text="{Binding Recipe.SelectedStep.KeyFrameLabel}" TextWrapping="Wrap" />
<TextBlock Text="{Binding StepKeyFrameStatus}" TextWrapping="Wrap" />
</StackPanel>
</Grid>
</Grid>
</TabItem>
<TabItem Header="区域">
<Grid Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="180" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="230" />
</Grid.ColumnDefinitions>
<DockPanel>
<Button DockPanel.Dock="Bottom" Content="加区域" Margin="0,8,0,0" Padding="0,6" Command="{Binding Recipe.AddRegionCommand}" />
<ListBox ItemTemplate="{StaticResource RegionRow}" ItemsSource="{Binding Recipe.Regions}" SelectedItem="{Binding Recipe.SelectedRegion}" Classes="Pick" />
</DockPanel>
<Grid Grid.Column="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" MinHeight="210" />
</Grid.RowDefinitions>
<WrapPanel Margin="0,0,0,6">
<Grid Grid.Row="3" RowDefinitions="Auto,Auto,Auto,*">
<WrapPanel Margin="0,0,0,6" IsEnabled="{Binding !SopRecording.Playback.IsPlaying}">
<Button Content="从相机取图" Command="{Binding CaptureImageCommand}" Classes="Mini" />
<Button Content="打开图片" Command="{Binding OpenImageCommand}" Margin="8,0,0,0" Classes="Mini" />
<StackPanel Orientation="Horizontal" Margin="8,0,0,0" Spacing="6">
@ -351,13 +330,14 @@
<NumericUpDown x:Name="StandardVideoSecondsBox" Width="100" Minimum="0" Increment="0.1" FormatString="0.###" Value="{Binding StandardVideoSeconds, Mode=TwoWay}" ToolTip.Tip="当前配方标准录制的视频,0 表示第一帧" />
</StackPanel>
</WrapPanel>
<TextBlock Grid.Row="1" Text="选中左侧区域,再在图上拖动画框" TextWrapping="Wrap" Foreground="{StaticResource FgDim}" FontSize="12" Margin="0,0,0,6" />
<TextBlock Grid.Row="2" Text="{Binding PreviewStatus}" TextWrapping="Wrap" Foreground="{StaticResource FgDim}" FontSize="12" Margin="0,0,0,6" />
<Border Grid.Row="3" Padding="0" CornerRadius="4" ClipToBounds="True" Classes="panel">
<ed:RegionCanvas Image="{Binding PreviewImage}" Regions="{Binding Recipe.Regions}" SelectedRegion="{Binding Recipe.SelectedRegion, Mode=TwoWay}" />
</Border>
</Grid>
<ScrollViewer x:Name="RegionParameters" Grid.Column="2" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
<TextBlock Grid.Row="1" Text="区域编辑:暂停播放,选中下方区域后在上方画面拖动画框;Esc 取消" TextWrapping="Wrap" FontSize="12" Margin="0,0,0,6" />
<TextBlock Grid.Row="2" Text="{Binding PreviewStatus}" TextWrapping="Wrap" FontSize="12" Margin="0,0,0,6" />
<Grid Grid.Row="3" ColumnDefinitions="2*,3*">
<DockPanel Margin="0,0,8,0">
<Button DockPanel.Dock="Bottom" Content="加区域" Margin="0,8,0,0" Padding="0,6" Command="{Binding Recipe.AddRegionCommand}" />
<ListBox x:Name="StepRegionNavigation" ItemTemplate="{StaticResource RegionRow}" ItemsSource="{Binding Recipe.Regions}" SelectedItem="{Binding Recipe.SelectedRegion}" Classes="Pick" />
</DockPanel>
<ScrollViewer x:Name="RegionParameters" Grid.Column="1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
<StackPanel DataContext="{Binding Recipe.SelectedRegion}">
<TextBlock Text="区域参数" FontSize="13" Margin="0,0,0,10" Classes="Title" />
<Grid Classes="Row">
@ -423,6 +403,9 @@
<TextBlock Text="改键名会同步更新所有步骤里对它的引用" TextWrapping="Wrap" Foreground="{StaticResource FgDim}" FontSize="12" Margin="0,8,0,0" />
</StackPanel>
</ScrollViewer>
</Grid>
</Grid>
</Grid>
</Grid>
</TabItem>
<TabItem Header="全局设置">
@ -500,8 +483,7 @@
<Button Content="停止录像" Command="{Binding StopRecordingCommand}" />
<Button Content="重新录制" Command="{Binding RestartCommand}" />
<Button Content="导入视频" Command="{Binding ImportVideoCommand}" ToolTip.Tip="选择视频并复制到当前配方的 sop_stand/sop.avi,覆盖已有标准视频" />
<Button Content="播放" Command="{Binding PlayVideoCommand}" />
<Button Content="暂停" Command="{Binding PauseVideoCommand}" />
<Button Content="{Binding PlayVideoButtonText}" Command="{Binding ToggleVideoPlaybackCommand}" />
<Button Content="测试流程" Command="{Binding TestFlowCommand}" ToolTip.Tip="从头播放标准视频,使用当前编辑的步骤和区域进行流程检测" />
<Button Content="停止播放" Command="{Binding StopVideoCommand}" />
<Button Content="打开保存位置" Command="{Binding RevealCommand}" />

View File

@ -42,7 +42,18 @@ public partial class RecipeEditorViewModel : BindableBase, IDialogAware
public SopHelper.Training.CameraViewModel? SopRecording { get => _sopRecording; private set => SetProperty(ref _sopRecording, value); }
public bool HasSopRecording => SopRecording is not null;
public BitmapSource? PreviewImage { get => _previewImage; private set => SetProperty(ref _previewImage, value); }
private bool _showRegionPreview;
public BitmapSource? StepEditorImage => _showRegionPreview ? PreviewImage : SopRecording?.Playback.Frame ?? PreviewImage;
public BitmapSource? PreviewImage
{
get => _previewImage;
private set
{
SetProperty(ref _previewImage, value);
_showRegionPreview = value is not null;
RaisePropertyChanged(nameof(StepEditorImage));
}
}
public string PreviewStatus { get => _previewStatus; private set => SetProperty(ref _previewStatus, value); }
public bool IsCapturing
{
@ -169,6 +180,13 @@ public partial class RecipeEditorViewModel : BindableBase, IDialogAware
var root = Path.GetDirectoryName(Path.GetDirectoryName(recipeDirectory)!)!;
SopRecording = new SopHelper.Training.CameraViewModel(new CameraService(), root, Path.GetFileNameWithoutExtension(_path), sopStandard: true, selectedCameraProvider: _selectedCameraProvider);
SopRecording.FlowTestConfigProvider = () => Recipe.ToConfig();
SopRecording.Playback.PropertyChanged += (_, e) =>
{
if (e.PropertyName != nameof(SopRecording.Playback.Frame)) return;
_showRegionPreview = false;
RaisePropertyChanged(nameof(StepEditorImage));
};
RaisePropertyChanged(nameof(StepEditorImage));
RaisePropertyChanged(nameof(HasSopRecording));
UpdateAgentDefaultVideo();
}

View File

@ -11,15 +11,16 @@ public partial class RecipeEditorViewModel
private void CaptureStepKeyFrame()
{
if (Recipe.SelectedStep is not { } step) { StepKeyFrameStatus = "请先选中一个步骤。"; return; }
if (IsAgentBusy || IsCapturing || SopRecording is not { } recording || recording.IsTestingFlow
|| recording.Playback.IsSeeking || recording.Playback.Frame is not { } frame)
var recording = SopRecording;
if (IsAgentBusy || IsCapturing || recording is { IsTestingFlow: true }
|| recording?.Playback.IsSeeking == true || StepEditorImage is not { } frame)
{ StepKeyFrameStatus = "请先播放标准视频;等待跳转完成后再获取关键帧,流程测试时不可取图。"; return; }
try
{
// UI线程同步取得已显示帧,避免按秒数重新解码造成图像与用户所见不一致。
var bytes = frame.ToPngBytes();
step.KeyFrameBase64 = Convert.ToBase64String(bytes);
step.KeyFrameSeconds = recording.Playback.DisplayedSeconds;
step.KeyFrameSeconds = ReferenceEquals(frame, recording?.Playback.Frame) ? recording!.Playback.DisplayedSeconds : null;
StepKeyFrameStatus = $"已获取步骤 {step.Id}「{step.Name}」关键帧,请保存配方。";
}
catch (Exception) { StepKeyFrameStatus = "关键帧保存失败,请重新获取。"; }

View File

@ -136,10 +136,10 @@ internal static class Program
var selected = vm.Recipe.SelectedStep!;
vm.CaptureStepKeyFrameCommand.Execute();
Check(selected.KeyFrameBase64.Length == 0, "Cannot capture a keyframe without a displayed video frame");
model.PlayVideoCommand.Execute(); Until(() => !model.IsBusy && model.Playback.IsPlaying);
Check(!model.PlayVideoCommand.CanExecute() && model.PauseVideoCommand.CanExecute(), "Separate play and pause commands expose the correct playing state");
model.PauseVideoCommand.Execute(); Until(() => !model.IsBusy && !model.Playback.IsPlaying);
Check(model.PlayVideoCommand.CanExecute() && !model.PauseVideoCommand.CanExecute(), "Pause only pauses; play becomes available to resume");
model.ToggleVideoPlaybackCommand.Execute(); Until(() => !model.IsBusy && model.Playback.IsPlaying);
Check(model.ToggleVideoPlaybackCommand.CanExecute() && model.PlayVideoButtonText == "暂停播放", "Combined playback button switches to pause while playing");
model.ToggleVideoPlaybackCommand.Execute(); Until(() => !model.IsBusy && !model.Playback.IsPlaying);
Check(model.ToggleVideoPlaybackCommand.CanExecute() && model.PlayVideoButtonText == "继续播放", "Combined playback button pauses and remains available to resume");
Pump(model.Playback.SeekAsync(50));
var displayed = Convert.ToBase64String(model.Playback.Frame!.ToPngBytes());
vm.CaptureStepKeyFrameCommand.Execute();
@ -165,6 +165,25 @@ internal static class Program
var window = new Window { Content = view, Width = 1040, Height = 760 }; window.Show(); window.UpdateLayout();
var stepVideo = view.FindControl<Border>("StepVideoViewport")!;
var parameters = view.FindControl<ScrollViewer>("StepParameters")!;
Check(!view.GetVisualDescendants().OfType<TabItem>().Any(t => t.Header?.ToString() == "区域"),
"Region editing is integrated into the step tab");
var regionNavigation = view.FindControl<ListBox>("StepRegionNavigation")!;
var regionParameters = view.FindControl<ScrollViewer>("RegionParameters")!;
Check(regionNavigation.TranslatePoint(default, window)!.Value.Y >= stepVideo.TranslatePoint(default, window)!.Value.Y + stepVideo.Bounds.Height
&& regionParameters.TranslatePoint(default, window)!.Value.Y >= stepVideo.TranslatePoint(default, window)!.Value.Y + stepVideo.Bounds.Height,
"Region navigation and parameters sit below the step video player");
Pump(model.Playback.SeekAsync(50)); window.UpdateLayout(); Dispatcher.UIThread.RunJobs();
var regionCanvas = view.FindControl<RegionCanvas>("StepVideoRegions")!;
Check(ReferenceEquals(regionCanvas.Image, model.Playback.Frame) && ReferenceEquals(regionCanvas.Regions, vm.Recipe.Regions),
"Integrated region canvas receives actual playback pixels and all recipe regions");
vm.Recipe.AddRegionCommand.Execute(); window.UpdateLayout(); Dispatcher.UIThread.RunJobs();
Check(ReferenceEquals(regionCanvas.SelectedRegion, vm.Recipe.SelectedRegion)
&& ReferenceEquals(regionNavigation.SelectedItem, vm.Recipe.SelectedRegion),
"Region navigation, parameters and canvas share the selected editable region");
var playbackButtons = ((Panel)view.FindControl<Button>("StepPlayPauseButton")!.Parent!).Children.OfType<Button>().ToArray();
Check(playbackButtons.Length == 3 && playbackButtons[1].Content?.ToString() == "停止" && playbackButtons[2].Content?.ToString() == "获取关键帧",
"Combined play/pause, stop and capture-keyframe buttons appear in order");
Pump(model.Playback.StopAsync());
vm.Recipe.SelectedStep = selected;
Dispatcher.UIThread.RunJobs(); window.UpdateLayout();
var keyFrame = view.FindControl<Image>("StepKeyFrameImage")!;
@ -188,6 +207,12 @@ internal static class Program
Check(keyFrame.TranslatePoint(default, window) == keyFramePosition,
"Scrolling step parameters does not move the keyframe");
parameters.Offset = default;
if (_overlayPreviewPath is not null)
{
using var rendered = new Avalonia.Media.Imaging.RenderTargetBitmap(new PixelSize((int)window.Bounds.Width, (int)window.Bounds.Height));
rendered.Render(window);
rendered.Save(Path.Combine(Path.GetDirectoryName(_overlayPreviewPath)!, "step-editor.png"));
}
Check(parameters.Bounds.Width > window.Bounds.Width * .30 && parameters.Bounds.Width < window.Bounds.Width * .37
&& parameters.Extent.Width <= parameters.Viewport.Width + 1,
"Parameters occupy 35 percent and fit without horizontal scrolling");