Changes to platform abstraction layer.

This commit is contained in:
H. Utku Maden 2024-04-11 19:09:00 +03:00
parent 2eb5663ee9
commit 7ce474d92a
7 changed files with 225 additions and 100 deletions

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using OpenTK.Windowing.Desktop; using OpenTK.Windowing.Desktop;
using OpenTK.Windowing.GraphicsLibraryFramework; using OpenTK.Windowing.GraphicsLibraryFramework;
using Quik.CommandMachine;
using Quik.Media; using Quik.Media;
using Quik.OpenGL; using Quik.OpenGL;
using Quik.PAL; using Quik.PAL;
@ -24,7 +25,7 @@ namespace Quik.OpenTK
private bool IsGLInitialized = false; private bool IsGLInitialized = false;
public IQuikPort CreatePort() public IQuikPortHandle CreatePort()
{ {
NativeWindow window = new NativeWindow(DefaultSettings); NativeWindow window = new NativeWindow(DefaultSettings);
OpenTKPort port = new OpenTKPort(window); OpenTKPort port = new OpenTKPort(window);
@ -33,7 +34,7 @@ namespace Quik.OpenTK
if (!IsGLInitialized) if (!IsGLInitialized)
{ {
window.Context.MakeCurrent(); window.Context.MakeCurrent();
GL.LoadBindings((string proc) => GLFW.GetProcAddress(proc)); GL.LoadBindings(GLFW.GetProcAddress);
IsGLInitialized = true; IsGLInitialized = true;
} }
@ -60,5 +61,31 @@ namespace Quik.OpenTK
{ {
NativeWindow.ProcessWindowEvents(block); NativeWindow.ProcessWindowEvents(block);
} }
public void DestroyPort(IQuikPortHandle port) => ((OpenTKPort)port).Dispose();
public string PortGetTitle(IQuikPortHandle port) => ((OpenTKPort)port).Title;
public void PortSetTitle(IQuikPortHandle port, string title) => ((OpenTKPort)port).Title = title;
public QVec2 PortGetSize(IQuikPortHandle port) => ((OpenTKPort)port).Size;
public void PortSetSize(IQuikPortHandle port, QVec2 size) => ((OpenTKPort)port).Size = size;
public QVec2 PortGetPosition(IQuikPortHandle port) => ((OpenTKPort)port).Position;
public void PortSetPosition(IQuikPortHandle port, QVec2 position) => ((OpenTKPort)port).Position = position;
public bool PortIsValid(IQuikPortHandle port) => ((OpenTKPort)port).IsValid;
public void PortSubscribeEvent(IQuikPortHandle port, EventHandler handler) => ((OpenTKPort)port).EventRaised += handler;
public void PortUnsubscribeEvent(IQuikPortHandle port, EventHandler handler) => ((OpenTKPort)port).EventRaised -= handler;
public void PortFocus(IQuikPortHandle port) => ((OpenTKPort)port).Focus();
public void PortShow(IQuikPortHandle port, bool shown = true) => ((OpenTKPort)port).Show(shown);
public void PortPaint(IQuikPortHandle port, CommandList commands) => ((OpenTKPort)port).Paint(commands);
} }
} }

@ -8,7 +8,7 @@ using Quik.VertexGenerator;
namespace Quik.OpenTK namespace Quik.OpenTK
{ {
public class OpenTKPort : IQuikPort public class OpenTKPort : IQuikPortHandle
{ {
private readonly NativeWindow _window; private readonly NativeWindow _window;
private readonly GL21Driver _glDriver; private readonly GL21Driver _glDriver;
@ -70,7 +70,7 @@ namespace Quik.OpenTK
QRectangle view = new QRectangle(Size, new QVec2(0, 0)); QRectangle view = new QRectangle(Size, new QVec2(0, 0));
_vertexEngine.Reset(); _vertexEngine.Reset();
_vertexEngine.ProcessCommands(new QRectangle(), queue); _vertexEngine.ProcessCommands(view, queue);
if (!_window.Context.IsCurrent) if (!_window.Context.IsCurrent)
_window.Context.MakeCurrent(); _window.Context.MakeCurrent();

@ -1,38 +0,0 @@
using System;
using Quik.Media;
namespace Quik.PAL
{
/// <summary>
/// The primary primary platform abstraction interface for Quik hosts.
/// </summary>
public interface IQuikPlatform : IDisposable
{
/// <summary>
/// The title of the application.
/// </summary>
string Title { get; set; }
/// <summary>
/// The default icon for the application.
/// </summary>
QImage Icon { get; set; }
/// <summary>
/// The event raised when an event is received.
/// </summary>
event EventHandler EventRaised;
/// <summary>
/// Create a window.
/// </summary>
/// <returns>The window instance.</returns>
IQuikPort CreatePort();
/// <summary>
/// Raise the events that have been enqueued.
/// </summary>
/// <param name="block">True to block until a new event arrives.</param>
void ProcessEvents(bool block);
}
}

59
Quik/PAL/IQuikPlatform.cs Normal file

@ -0,0 +1,59 @@
using System;
using Quik.CommandMachine;
using Quik.Media;
namespace Quik.PAL
{
/// <summary>
/// An empty interface to statically type Quik port handles.
/// </summary>
public interface IQuikPortHandle
{
}
/// <summary>
/// The primary primary platform abstraction interface for Quik hosts.
/// </summary>
public interface IQuikPlatform : IDisposable
{
/// <summary>
/// The title of the application.
/// </summary>
string Title { get; set; }
/// <summary>
/// The default icon for the application.
/// </summary>
QImage Icon { get; set; }
/// <summary>
/// The event raised when an event is received.
/// </summary>
event EventHandler EventRaised;
/// <summary>
/// Raise the events that have been enqueued.
/// </summary>
/// <param name="block">True to block until a new event arrives.</param>
void ProcessEvents(bool block);
/// <summary>
/// Create a window.
/// </summary>
/// <returns>The window instance.</returns>
IQuikPortHandle CreatePort();
void DestroyPort(IQuikPortHandle port);
string PortGetTitle(IQuikPortHandle port);
void PortSetTitle(IQuikPortHandle port, string title);
QVec2 PortGetSize(IQuikPortHandle port);
void PortSetSize(IQuikPortHandle port, QVec2 size);
QVec2 PortGetPosition(IQuikPortHandle port);
void PortSetPosition(IQuikPortHandle port, QVec2 position);
bool PortIsValid(IQuikPortHandle port);
void PortSubscribeEvent(IQuikPortHandle port, EventHandler handler);
void PortUnsubscribeEvent(IQuikPortHandle port, EventHandler handler);
void PortFocus(IQuikPortHandle port);
void PortShow(IQuikPortHandle port, bool shown = true);
void PortPaint(IQuikPortHandle port, CommandList commands);
}
}

@ -1,49 +0,0 @@
using System;
using Quik.CommandMachine;
namespace Quik.PAL
{
/// <summary>
/// An abstraction over the a window or a rendering context.
/// </summary>
public interface IQuikPort : IDisposable
{
/// <summary>
/// Title of the window, if applicable.
/// </summary>
string Title { get; set; }
/// <summary>
/// Size of the window.
/// </summary>
QVec2 Size { get; set; }
/// <summary>
/// Position of the window in the coordinate system.
/// </summary>
QVec2 Position { get; set; }
bool IsValid { get; }
/// <summary>
/// Called when an event for this port is raised.
/// </summary>
event EventHandler EventRaised;
/// <summary>
/// Focus the window, bringing it to the front.
/// </summary>
void Focus();
/// <summary>
/// Show or hide the window.
/// </summary>
/// <param name="shown">True to show the window, false to hide.</param>
void Show(bool shown = true);
/// <summary>
/// Paint the given command queue onto the port.
/// </summary>
/// <param name="queue">The command queue to paint.</param>
void Paint(CommandList queue);
}
}

92
Quik/PAL/QuikPort.cs Normal file

@ -0,0 +1,92 @@
using Quik.CommandMachine;
using Quik.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quik.PAL
{
/// <summary>
/// An abstraction layer over the UI input and output.
/// </summary>
public class QuikPort
{
private readonly IQuikPortHandle handle;
private readonly IQuikPlatform platform;
public string Title
{
get => platform.PortGetTitle(handle);
set => platform.PortSetTitle(handle, value);
}
public QVec2 Size
{
get => platform.PortGetSize(handle);
set => platform.PortSetSize(handle, value);
}
public QVec2 Position
{
get => platform.PortGetPosition(handle);
set => platform.PortSetPosition(handle, value);
}
public UIBase UIElement { get; set; }
public bool IsValid => platform.PortIsValid(handle);
public event EventHandler EventRaised
{
add
{
platform.PortSubscribeEvent(handle, value);
}
remove
{
platform.PortUnsubscribeEvent(handle, value);
}
}
public QuikPort(IQuikPlatform platform)
{
this.platform = platform;
handle = platform.CreatePort();
}
bool isDisposed = false;
public void Dispose()
{
if (isDisposed) return;
platform.DestroyPort(handle);
isDisposed = true;
}
public void Focus()
{
platform.PortFocus(handle);
}
public void Paint(CommandList list = null)
{
if (UIElement == null)
return;
if(list == null)
list = new CommandList();
list.Clear();
UIElement.Bounds = new QRectangle(Size, new QVec2(0,0));
UIElement.Paint(list);
platform.PortPaint(handle, list);
}
public void Show(bool shown = true)
{
platform.PortShow(handle, shown);
}
}
}

@ -4,6 +4,7 @@ using Quik.CommandMachine;
using Quik.Controls; using Quik.Controls;
using Quik.Media; using Quik.Media;
using Quik.PAL; using Quik.PAL;
using Quik.Typography;
namespace Quik namespace Quik
{ {
@ -35,7 +36,9 @@ namespace Quik
set => Platform.Icon = value; set => Platform.Icon = value;
} }
public View MainView { get; private set; } = null; public QuikPort MainPort { get; private set; } = null;
public FontProvider FontProvider { get; }
/// <summary> /// <summary>
/// List of media loaders, drivers that load media such as images and fonts. /// List of media loaders, drivers that load media such as images and fonts.
@ -45,25 +48,56 @@ namespace Quik
public QuikApplication(IQuikPlatform platform) public QuikApplication(IQuikPlatform platform)
{ {
Platform = platform; Platform = platform;
FontProvider = new FontProvider(this);
}
public IDisposable GetMedia(object key, MediaHint hint)
{
IDisposable disposable = null;
foreach (MediaLoader loader in MediaLoaders)
{
disposable = loader.GetMedia(key, hint);
if (disposable != null)
break;
}
return disposable;
}
public IDisposable GetMedia<T>(T key, MediaHint hint)
{
IDisposable disposable = null;
foreach (MediaLoader loader in MediaLoaders)
{
if (loader is MediaLoader<T> typedLoader)
{
disposable = typedLoader.GetMedia(key, hint);
if (disposable != null)
break;
}
}
return disposable;
} }
public void Run(View mainView) public void Run(View mainView)
{ {
IQuikPort port = Platform.CreatePort(); MainPort = new QuikPort(Platform) { UIElement = mainView };
CommandList cmd = new CommandList(); CommandList cmd = new CommandList();
MainView = mainView; MainPort.EventRaised += (sender, ea) => mainView.NotifyEvent(sender, ea);
port.EventRaised += (sender, ea) => mainView.NotifyEvent(sender, ea); while (MainPort.IsValid)
while (port.IsValid)
{ {
Platform.ProcessEvents(false); Platform.ProcessEvents(false);
if (port.IsValid) if (MainPort.IsValid)
{ {
cmd.Clear(); cmd.Clear();
MainView.Paint(cmd); MainPort.Paint(cmd);
port.Paint(cmd);
} }
} }
} }