Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions OpenTKAvalonia/BaseTkOpenGlControl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Avalonia;
using System.Runtime.InteropServices;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.OpenGL;
using Avalonia.OpenGL.Controls;
Expand All @@ -24,7 +26,7 @@ public abstract class BaseTkOpenGlControl : OpenGlControlBase, ICustomHitTest
/// </summary>
protected virtual void OpenTkRender()
{

}

/// <summary>
Expand All @@ -34,7 +36,7 @@ protected virtual void OpenTkRender()
/// </summary>
protected virtual void OpenTkInit()
{

}

/// <summary>
Expand All @@ -45,16 +47,17 @@ protected virtual void OpenTkInit()
/// </summary>
protected virtual void OpenTkTeardown()
{

}

protected sealed override void OnOpenGlRender(GlInterface gl, int fb)
{
//Update last key states
KeyboardState.OnFrame();

//Set up the aspect ratio so shapes aren't stretched.
GL.Viewport(0, 0, (int) Bounds.Width, (int) Bounds.Height);
var (w, h) = GetPlatformSpecificBounds();
GL.Viewport(0, 0, w, h);

//Tell our subclass to render
if (Bounds.Width != 0 && Bounds.Height != 0)
Expand All @@ -66,6 +69,17 @@ protected sealed override void OnOpenGlRender(GlInterface gl, int fb)
Dispatcher.UIThread.Post(RequestNextFrameRendering, DispatcherPriority.Background);
}

private static readonly bool OnLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);

private double? _renderScaling = null;
private double RenderScaling => (_renderScaling ??= TopLevel.GetTopLevel(this)?.RenderScaling)
?? throw new PlatformNotSupportedException("Could not obtain TopLevel");
private (int width, int height) GetPlatformSpecificBounds()
=> OnLinux
? ((int)Bounds.Width, (int)Bounds.Height)
: (Math.Max(1, (int)(Bounds.Width * RenderScaling)),
Math.Max(1, (int)(Bounds.Height * RenderScaling)));


protected sealed override void OnOpenGlInit(GlInterface gl)
{
Expand All @@ -87,15 +101,15 @@ protected override void OnKeyDown(KeyEventArgs e)
{
if (!IsEffectivelyVisible)
return;

KeyboardState.SetKey(e.Key, true);
}

protected override void OnKeyUp(KeyEventArgs e)
{
if (!IsEffectivelyVisible)
return;

KeyboardState.SetKey(e.Key, false);
}

Expand Down