A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

56 lines
1.8 KiB

using System;
using System.Diagnostics;
using Avalonia;
using Avalonia.Controls;
using Avalonia.LogicalTree;
using Avalonia.Media;
using Avalonia.Media.Immutable;
using Avalonia.VisualTree;
namespace ControlCatalog.Pages
{
public class TimeCriticalRenderPage : Control, IRenderTimeCriticalVisual
{
private int _frame;
private TimeSpan _lastFps;
private int _lastFpsFrame;
private double _fps;
public bool HasRenderTimeCriticalContent => true;
public bool ThreadSafeHasNewFrame => true;
Stopwatch _st = Stopwatch.StartNew();
private Typeface _typeface = Typeface.Default;
public override void Render(DrawingContext context)
{
var nowTs = _st.Elapsed;
var now = DateTime.Now;
var fpsTimeDiff = (nowTs - _lastFps).TotalSeconds;
if (fpsTimeDiff > 1)
{
_fps = (_frame - _lastFpsFrame) / fpsTimeDiff;
_lastFpsFrame = _frame;
_lastFps = nowTs;
}
var text = $"Frame: {_frame}\nFPS: {_fps}\nNow: {now}";
text += $"\nTransform{context.CurrentTransform}";
text += $"\nContainer Transform{context.CurrentContainerTransform}";
var fmt = new FormattedText()
{
Text = text,
Typeface = _typeface
};
var back = new ImmutableSolidColorBrush(Colors.LightGray);
var textBrush = new ImmutableSolidColorBrush(Colors.Black);
context.FillRectangle(back, new Rect(0,0,12,12));
context.DrawText(textBrush, new Point(5, 5), fmt);
_frame++;
}
public void ThreadSafeRender(DrawingContext context, Size logicalSize, double scaling)
{
}
}
}