using System; using System.Collections.Generic; using OpenTK.Windowing.Desktop; using OpenTK.Windowing.GraphicsLibraryFramework; using Dashboard.CommandMachine; using Dashboard.Media; using Dashboard.OpenGL; using Dashboard.PAL; namespace Dashboard.OpenTK { public class OpenTKPlatform : IDashboardPlatform { private readonly List _ports = new List(); // These shall remain a sad nop for now. public string? Title { get; set; } public QImage? Icon { get; set; } = null; public event EventHandler? EventRaised; public NativeWindowSettings DefaultSettings { get; set; } = NativeWindowSettings.Default; public IReadOnlyList Ports => _ports; private bool IsGLInitialized = false; public IDashHandle CreatePort() { NativeWindow window = new NativeWindow(DefaultSettings); OpenTKPort port = new OpenTKPort(window); _ports.Add(port); if (!IsGLInitialized) { window.Context.MakeCurrent(); GL.LoadBindings(GLFW.GetProcAddress); IsGLInitialized = true; } window.Closing += (ea) => { Environment.Exit(0); }; 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); } public void DestroyPort(IDashHandle port) => ((OpenTKPort)port).Dispose(); public string PortGetTitle(IDashHandle port) => ((OpenTKPort)port).Title; public void PortSetTitle(IDashHandle port, string title) => ((OpenTKPort)port).Title = title; public QVec2 PortGetSize(IDashHandle port) => ((OpenTKPort)port).Size; public void PortSetSize(IDashHandle port, QVec2 size) => ((OpenTKPort)port).Size = size; public QVec2 PortGetPosition(IDashHandle port) => ((OpenTKPort)port).Position; public void PortSetPosition(IDashHandle port, QVec2 position) => ((OpenTKPort)port).Position = position; public bool PortIsValid(IDashHandle port) => ((OpenTKPort)port).IsValid; public void PortSubscribeEvent(IDashHandle port, EventHandler handler) => ((OpenTKPort)port).EventRaised += handler; public void PortUnsubscribeEvent(IDashHandle port, EventHandler handler) => ((OpenTKPort)port).EventRaised -= handler; public void PortFocus(IDashHandle port) => ((OpenTKPort)port).Focus(); public void PortShow(IDashHandle port, bool shown = true) => ((OpenTKPort)port).Show(shown); public void PortPaint(IDashHandle port, CommandList commands) => ((OpenTKPort)port).Paint(commands); 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; } } }