feat: capture fresh image (#10460)

* feat: 使用 AsstAsyncScreencap 的实时截图

---------

Co-authored-by: uye <99072975+ABA2396@users.noreply.github.com>

* feat: 增加空阻塞任务以便调试

* chore: 增加帧率显示

---------

Co-authored-by: zzyyyl <aysyxx53@hotmail.com>
This commit is contained in:
uye
2024-09-18 00:41:30 +08:00
committed by GitHub
parent 228e4f03b8
commit 2829455fa2
6 changed files with 67 additions and 18 deletions

View File

@@ -1,4 +1,9 @@
{
"Block": {
"algorithm": "JustReturn",
"postDelay": 1000,
"next": ["#self"]
},
"SlowlySwipeToTheLeft": {
"algorithm": "JustReturn",
"action": "Swipe",

View File

@@ -99,7 +99,7 @@ namespace asst
public:
std::shared_ptr<Controller> ctrler() const { return m_ctrler; }
std::shared_ptr<Status> status() const { return m_status; }
bool need_exit() const { return m_thread_idle; }
bool need_exit() const { return m_thread_idle && m_running; }
private:
void append_callback(AsstMsg msg, const json::value& detail);

View File

@@ -194,6 +194,12 @@ namespace MaaWpfGui.Main
return AsstGetImage(_handle);
}
public BitmapImage? AsstGetFreshImage()
{
MaaService.AsstAsyncScreencap(_handle, true);
return AsstGetImage(_handle);
}
private readonly MaaService.CallbackDelegate _callback;
/// <summary>
@@ -2599,6 +2605,9 @@ namespace MaaWpfGui.Main
["task_names"] = new JArray
{
once ? "GachaOnce" : "GachaTenTimes",
// TEST
// "Block",
},
};
AsstTaskId id = AsstAppendTaskWithEncoding("Custom", taskParams);

View File

@@ -60,6 +60,9 @@ namespace MaaWpfGui.Services
[DllImport("MaaCore.dll")]
public static extern bool AsstStop(AsstHandle handle);
[DllImport("MaaCore.dll")]
public static extern unsafe Int32 AsstAsyncScreencap(AsstHandle handle, bool block);
[DllImport("MaaCore.dll")]
public static extern unsafe ulong AsstGetImage(AsstHandle handle, byte* buff, ulong buffSize);

View File

@@ -747,6 +747,14 @@ namespace MaaWpfGui.ViewModels.UI
set => SetAndNotify(ref _gachaInfo, value);
}
private double _gachaScreenFpf;
public double GachaScreenFpf
{
get => _gachaScreenFpf;
set => SetAndNotify(ref _gachaScreenFpf, value);
}
// xaml 中用到了
// ReSharper disable once UnusedMember.Global
public void GachaOnce()
@@ -782,15 +790,13 @@ namespace MaaWpfGui.ViewModels.UI
return;
}
_gachaImageTimer.Interval = TimeSpan.FromMilliseconds(500);
_gachaImageTimer.Interval = TimeSpan.FromMilliseconds(10);
_gachaImageTimer.Tick += RefreshGachaImage;
_gachaImageTimer.Start();
}
private readonly DispatcherTimer _gachaImageTimer = new();
private static readonly object _lock = new();
private BitmapImage? _gachaImage;
public BitmapImage? GachaImage
@@ -799,21 +805,31 @@ namespace MaaWpfGui.ViewModels.UI
set => SetAndNotify(ref _gachaImage, value);
}
private DateTime _lastTipUpdateTime = DateTime.MinValue;
private DateTime _lastFpsUpdateTime = DateTime.MinValue;
private int _frameCount;
private void RefreshGachaImage(object? sender, EventArgs? e)
{
lock (_lock)
GachaImage = Instances.AsstProxy.AsstGetFreshImage();
var now = DateTime.Now;
_frameCount++;
if ((now - _lastFpsUpdateTime).TotalSeconds >= 1)
{
var image = Instances.AsstProxy.AsstGetImage();
if (GachaImage.IsEqual(image))
{
return;
}
GachaImage = image;
var rd = new Random();
GachaInfo = LocalizationHelper.GetString("GachaTip" + rd.Next(1, 18).ToString());
GachaScreenFpf = _frameCount / (now - _lastFpsUpdateTime).TotalSeconds;
_frameCount = 0;
_lastFpsUpdateTime = now;
}
if ((now - _lastTipUpdateTime).TotalSeconds < 5)
{
return;
}
var rd = new Random();
GachaInfo = LocalizationHelper.GetString("GachaTip" + rd.Next(1, 18));
_lastTipUpdateTime = now;
}
// DO NOT CHANGE

View File

@@ -358,12 +358,28 @@
Text="{Binding GachaInfo}"
TextWrapping="Wrap"
Visibility="{c:Binding !GachaShowDisclaimer}" />
<Image
<Grid
Grid.Row="1"
Margin="10"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Source="{Binding GachaImage}" />
Visibility="{c:Binding !GachaShowDisclaimer}">
<Image
Margin="10"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Source="{Binding GachaImage}" />
<Canvas>
<controls:TextBlock
Canvas.Left="10"
Canvas.Top="10"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FontWeight="Bold"
Foreground="Aquamarine"
Text="{Binding GachaScreenFpf, StringFormat={}{0:F2}}"
Visibility="{c:Binding 'GachaImage != null'}" />
</Canvas>
</Grid>
<StackPanel
Grid.Row="1"
HorizontalAlignment="Center"