Dashboard/Dashboard.OpenTK/OpenTKPlatform.cs

104 lines
3.4 KiB
C#
Raw Normal View History

2023-07-28 21:37:49 +02:00
using System;
using System.Collections.Generic;
using OpenTK.Windowing.Desktop;
using OpenTK.Windowing.GraphicsLibraryFramework;
2024-07-17 22:18:20 +02:00
using Dashboard.CommandMachine;
using Dashboard.Media;
using Dashboard.OpenGL;
using Dashboard.PAL;
2023-07-28 21:37:49 +02:00
2024-07-17 22:18:20 +02:00
namespace Dashboard.OpenTK
2023-07-28 21:37:49 +02:00
{
2024-07-18 19:34:39 +02:00
public class OpenTKPlatform : IDashboardPlatform
2023-07-28 21:37:49 +02:00
{
private readonly List<OpenTKPort> _ports = new List<OpenTKPort>();
// These shall remain a sad nop for now.
2024-06-09 21:54:33 +02:00
public string? Title { get; set; }
public QImage? Icon { get; set; } = null;
2023-07-28 21:37:49 +02:00
2024-06-09 21:54:33 +02:00
public event EventHandler? EventRaised;
2023-07-28 21:37:49 +02:00
public NativeWindowSettings DefaultSettings { get; set; } = NativeWindowSettings.Default;
public IReadOnlyList<OpenTKPort> Ports => _ports;
private bool IsGLInitialized = false;
2024-07-18 19:34:39 +02:00
public IDashHandle CreatePort()
2023-07-28 21:37:49 +02:00
{
NativeWindow window = new NativeWindow(DefaultSettings);
OpenTKPort port = new OpenTKPort(window);
_ports.Add(port);
if (!IsGLInitialized)
{
window.Context.MakeCurrent();
2024-04-11 18:09:00 +02:00
GL.LoadBindings(GLFW.GetProcAddress);
2023-07-28 21:37:49 +02:00
IsGLInitialized = true;
}
2024-03-04 19:41:16 +01:00
window.Closing += (ea) =>
{
Environment.Exit(0);
};
2023-07-28 21:37:49 +02:00
return port;
}
public void Dispose()
{
// FIXME: dispose pattern here!
// Copy the array to prevent collection modification exceptions.
foreach (OpenTKPort port in _ports.ToArray())
{
port.Dispose();
}
}
public void ProcessEvents(bool block)
{
NativeWindow.ProcessWindowEvents(block);
}
2024-04-11 18:09:00 +02:00
2024-07-18 19:34:39 +02:00
public void DestroyPort(IDashHandle port) => ((OpenTKPort)port).Dispose();
2024-04-11 18:09:00 +02:00
2024-07-18 19:34:39 +02:00
public string PortGetTitle(IDashHandle port) => ((OpenTKPort)port).Title;
2024-04-11 18:09:00 +02:00
2024-07-18 19:34:39 +02:00
public void PortSetTitle(IDashHandle port, string title) => ((OpenTKPort)port).Title = title;
2024-04-11 18:09:00 +02:00
2024-07-18 19:34:39 +02:00
public QVec2 PortGetSize(IDashHandle port) => ((OpenTKPort)port).Size;
2024-04-11 18:09:00 +02:00
2024-07-18 19:34:39 +02:00
public void PortSetSize(IDashHandle port, QVec2 size) => ((OpenTKPort)port).Size = size;
2024-04-11 18:09:00 +02:00
2024-07-18 19:34:39 +02:00
public QVec2 PortGetPosition(IDashHandle port) => ((OpenTKPort)port).Position;
2024-04-11 18:09:00 +02:00
2024-07-18 19:34:39 +02:00
public void PortSetPosition(IDashHandle port, QVec2 position) => ((OpenTKPort)port).Position = position;
2024-04-11 18:09:00 +02:00
2024-07-18 19:34:39 +02:00
public bool PortIsValid(IDashHandle port) => ((OpenTKPort)port).IsValid;
2024-04-11 18:09:00 +02:00
2024-07-18 19:34:39 +02:00
public void PortSubscribeEvent(IDashHandle port, EventHandler handler) => ((OpenTKPort)port).EventRaised += handler;
2024-04-11 18:09:00 +02:00
2024-07-18 19:34:39 +02:00
public void PortUnsubscribeEvent(IDashHandle port, EventHandler handler) => ((OpenTKPort)port).EventRaised -= handler;
2024-04-11 18:09:00 +02:00
2024-07-18 19:34:39 +02:00
public void PortFocus(IDashHandle port) => ((OpenTKPort)port).Focus();
2024-04-11 18:09:00 +02:00
2024-07-18 19:34:39 +02:00
public void PortShow(IDashHandle port, bool shown = true) => ((OpenTKPort)port).Show(shown);
2024-04-11 18:09:00 +02:00
2024-07-18 19:34:39 +02:00
public void PortPaint(IDashHandle port, CommandList commands) => ((OpenTKPort)port).Paint(commands);
2024-05-15 22:17:01 +02:00
public void GetMaximumImage(out int width, out int height)
{
GL.Get(GLEnum.GL_MAX_TEXTURE_SIZE, out int value);
width = height = value;
}
public void GetMaximumImage(out int width, out int height, out int depth)
{
GetMaximumImage(out width, out height);
GL.Get(GLEnum.GL_MAX_ARRAY_TEXTURE_LAYERS, out int value);
depth = value;
}
2023-07-28 21:37:49 +02:00
}
}