Some additional work on dashboard architecture.
This commit is contained in:
parent
2c957a0c1a
commit
1c3c730e82
20
Dashboard.Common/Events/AnimationTickEventArgs.cs
Normal file
20
Dashboard.Common/Events/AnimationTickEventArgs.cs
Normal file
@ -0,0 +1,20 @@
|
||||
namespace Dashboard.Events
|
||||
{
|
||||
public class AnimationTickEventArgs : UiEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Animation delta time in seconds.
|
||||
/// </summary>
|
||||
public float Delta { get; }
|
||||
|
||||
public AnimationTickEventArgs(float delta) : base(UiEventType.AnimationTick)
|
||||
{
|
||||
Delta = delta;
|
||||
}
|
||||
}
|
||||
|
||||
public interface IAnimationTickEvent
|
||||
{
|
||||
event EventHandler<AnimationTickEventArgs> AnimationTimerEvent;
|
||||
}
|
||||
}
|
69
Dashboard.Common/Events/MouseEvents.cs
Normal file
69
Dashboard.Common/Events/MouseEvents.cs
Normal file
@ -0,0 +1,69 @@
|
||||
using System.Drawing;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Dashboard.Events
|
||||
{
|
||||
[Flags]
|
||||
public enum MouseButtons
|
||||
{
|
||||
M1 = 1 << 0,
|
||||
M2 = 1 << 1,
|
||||
M3 = 1 << 2,
|
||||
M4 = 1 << 3,
|
||||
M5 = 1 << 4,
|
||||
M6 = 1 << 5,
|
||||
M7 = 1 << 6,
|
||||
M8 = 1 << 7,
|
||||
|
||||
Left = M1,
|
||||
Right = M2,
|
||||
Middle = M3,
|
||||
}
|
||||
|
||||
public sealed class MouseMoveEventArgs : UiEventArgs
|
||||
{
|
||||
public Vector2 ClientPosition { get; }
|
||||
public Vector2 Delta { get; }
|
||||
|
||||
public MouseMoveEventArgs(Vector2 clientPosition, Vector2 delta)
|
||||
: base(UiEventType.MouseMove)
|
||||
{
|
||||
ClientPosition = clientPosition;
|
||||
Delta = delta;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class MouseButtonEventArgs : UiEventArgs
|
||||
{
|
||||
public Vector2 ClientPosition { get; }
|
||||
public MouseButtons Buttons { get; }
|
||||
|
||||
public MouseButtonEventArgs(Vector2 clientPosition, MouseButtons buttons, bool up)
|
||||
: base(up ? UiEventType.MouseButtonUp : UiEventType.MouseButtonDown)
|
||||
{
|
||||
ClientPosition = clientPosition;
|
||||
Buttons = buttons;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class MouseScrollEventArgs : UiEventArgs
|
||||
{
|
||||
public Vector2 ClientPosition { get; }
|
||||
public Vector2 ScrollDelta { get; }
|
||||
|
||||
public MouseScrollEventArgs(Vector2 clientPosition, Vector2 scrollDelta)
|
||||
: base(UiEventType.MouseScroll)
|
||||
{
|
||||
ClientPosition = clientPosition;
|
||||
ScrollDelta = scrollDelta;
|
||||
}
|
||||
}
|
||||
|
||||
public interface IMouseEvents
|
||||
{
|
||||
event EventHandler<MouseMoveEventArgs> MouseMoved;
|
||||
event EventHandler<MouseButtonEventArgs> MouseButtonDown;
|
||||
event EventHandler<MouseButtonEventArgs> MouseButtonUp;
|
||||
event EventHandler<MouseScrollEventArgs> MouseScroll;
|
||||
}
|
||||
}
|
76
Dashboard.Common/Events/UiEventArgs.cs
Normal file
76
Dashboard.Common/Events/UiEventArgs.cs
Normal file
@ -0,0 +1,76 @@
|
||||
using System.Numerics;
|
||||
|
||||
namespace Dashboard.Events
|
||||
{
|
||||
public enum UiEventType
|
||||
{
|
||||
None,
|
||||
AnimationTick, // Generic timer event.
|
||||
|
||||
// Text input related events.
|
||||
KeyDown, // Keyboard key down.
|
||||
KeyUp, // Keyboard key up.
|
||||
TextInput, // Non-IME text event.
|
||||
TextEdit, // IME text event.
|
||||
TextCandidates, // IME text candidate list.
|
||||
TextLanguage, // Keyboard language changed event.
|
||||
|
||||
// Mouse & touch related events
|
||||
MouseButtonDown, // Mouse button down.
|
||||
MouseButtonUp, // Mouse button up.
|
||||
MouseMove, // Mouse moved.
|
||||
MouseScroll, // Mouse scrolled.
|
||||
|
||||
// Reserved event names
|
||||
StylusEnter, // The stylus has entered the hover region.
|
||||
StylusLeave, // The stylus has left the hover region.
|
||||
StylusMove, // The stylus has moved.
|
||||
StylusDown, // The stylus is touching.
|
||||
StylusUp, // The stylus is no longer touching.
|
||||
StylusButtonUp, // Stylus button up.
|
||||
StylusButtonDown, // Stylus button down.
|
||||
StylusAxes, // Extra stylus axes data.
|
||||
|
||||
// Window & Control Events
|
||||
ControlInvalidateVisual, // Force rendering the control again.
|
||||
ControlStateChanged, // Control state changed.
|
||||
ControlMoved, // Control moved.
|
||||
ControlResized, // Control resized.
|
||||
ControlEnter, // The pointing device entered the control.
|
||||
ControlLeave, // The pointing device left the control.
|
||||
ControlFocusGet, // The control acquired focus.
|
||||
ControlFocusLost, // The control lost focus.
|
||||
WindowClose, // The window closed.
|
||||
|
||||
UserRangeStart = 1 << 12,
|
||||
}
|
||||
|
||||
public class UiEventArgs : EventArgs
|
||||
{
|
||||
public UiEventType Type { get; }
|
||||
|
||||
public UiEventArgs(UiEventType type)
|
||||
{
|
||||
Type = type;
|
||||
}
|
||||
|
||||
public static readonly UiEventArgs None = new UiEventArgs(UiEventType.None);
|
||||
}
|
||||
|
||||
public class ControlMovedEventArgs : UiEventArgs
|
||||
{
|
||||
public Vector2 OldPosition { get; }
|
||||
public Vector2 NewPosition { get; }
|
||||
|
||||
public ControlMovedEventArgs(Vector2 oldPosition, Vector2 newPosition) : base(UiEventType.ControlMoved)
|
||||
{
|
||||
OldPosition = oldPosition;
|
||||
NewPosition = newPosition;
|
||||
}
|
||||
}
|
||||
|
||||
public class ControlResizedEventArgs
|
||||
{
|
||||
|
||||
}
|
||||
}
|
43
Dashboard.Common/Windowing/ICompositor.cs
Normal file
43
Dashboard.Common/Windowing/ICompositor.cs
Normal file
@ -0,0 +1,43 @@
|
||||
namespace Dashboard.Windowing
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface for a class that composites multiple windows together.
|
||||
/// </summary>
|
||||
public interface ICompositor : IDisposable
|
||||
{
|
||||
void Composite(IPhysicalWindow window, IEnumerable<IVirtualWindow> windows);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Interface for classes that implement a window manager.
|
||||
/// </summary>
|
||||
public interface IWindowManager : IEnumerable<IVirtualWindow>, IEventListener, IPaintable, IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// The physical window that this window manager is associated with.
|
||||
/// </summary>
|
||||
IPhysicalWindow PhysicalWindow { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The compositor that will composite all virtual windows.
|
||||
/// </summary>
|
||||
ICompositor Compositor { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The window that is currently focused.
|
||||
/// </summary>
|
||||
IVirtualWindow? FocusedWindow { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Create a virtual window.
|
||||
/// </summary>
|
||||
/// <returns>Virtual window handle.</returns>
|
||||
IVirtualWindow CreateWindow();
|
||||
|
||||
/// <summary>
|
||||
/// Focus a virtual window, if it is owned by this window manager.
|
||||
/// </summary>
|
||||
/// <param name="window">The window to focus.</param>
|
||||
void Focus(IVirtualWindow window);
|
||||
}
|
||||
}
|
20
Dashboard.Common/Windowing/IDeviceContext.cs
Normal file
20
Dashboard.Common/Windowing/IDeviceContext.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System.Drawing;
|
||||
|
||||
namespace Dashboard.Windowing
|
||||
{
|
||||
/// <summary>
|
||||
/// Generic interface for the rendering system present in a <see cref="IPhysicalWindow"/>
|
||||
/// </summary>
|
||||
public interface IDeviceContext
|
||||
{
|
||||
/// <summary>
|
||||
/// The swap group for this device context.
|
||||
/// </summary>
|
||||
ISwapGroup SwapGroup { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The size of the window framebuffer in pixels.
|
||||
/// </summary>
|
||||
Size FramebufferSize { get; }
|
||||
}
|
||||
}
|
11
Dashboard.Common/Windowing/IEventListener.cs
Normal file
11
Dashboard.Common/Windowing/IEventListener.cs
Normal file
@ -0,0 +1,11 @@
|
||||
namespace Dashboard.Windowing
|
||||
{
|
||||
public interface IEventListener
|
||||
{
|
||||
/// <summary>
|
||||
/// Send an event to this windowing object.
|
||||
/// </summary>
|
||||
/// <param name="args">The event arguments sent.</param>
|
||||
void SendEvent(EventArgs args);
|
||||
}
|
||||
}
|
12
Dashboard.Common/Windowing/IPaintable.cs
Normal file
12
Dashboard.Common/Windowing/IPaintable.cs
Normal file
@ -0,0 +1,12 @@
|
||||
namespace Dashboard.Windowing
|
||||
{
|
||||
public interface IPaintable
|
||||
{
|
||||
event EventHandler Painting;
|
||||
|
||||
/// <summary>
|
||||
/// Paint this paintable object.
|
||||
/// </summary>
|
||||
void Paint();
|
||||
}
|
||||
}
|
18
Dashboard.Common/Windowing/ISwapGroup.cs
Normal file
18
Dashboard.Common/Windowing/ISwapGroup.cs
Normal file
@ -0,0 +1,18 @@
|
||||
namespace Dashboard.Windowing
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface that is used to swap the buffers of windows
|
||||
/// </summary>
|
||||
public interface ISwapGroup
|
||||
{
|
||||
/// <summary>
|
||||
/// The swap interval for this swap group.
|
||||
/// </summary>
|
||||
public int SwapInterval { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Swap buffers.
|
||||
/// </summary>
|
||||
void Swap();
|
||||
}
|
||||
}
|
@ -1,11 +1,16 @@
|
||||
using System.Drawing;
|
||||
using Dashboard.Events;
|
||||
|
||||
namespace Dashboard
|
||||
namespace Dashboard.Windowing
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class of all Dashboard windows.
|
||||
/// </summary>
|
||||
public interface IWindow : IDisposable
|
||||
public interface IWindow :
|
||||
IPaintable,
|
||||
IDisposable,
|
||||
IAnimationTickEvent,
|
||||
IMouseEvents
|
||||
{
|
||||
/// <summary>
|
||||
/// Name of the window.
|
||||
@ -42,46 +47,8 @@ namespace Dashboard
|
||||
/// <summary>
|
||||
/// An object that represents a window in a virtual space, usually another window or a rendering system.
|
||||
/// </summary>
|
||||
public interface IVirtualWindow : IWindow
|
||||
public interface IVirtualWindow : IWindow, IEventListener
|
||||
{
|
||||
/// <summary>
|
||||
/// Send an event to this window.
|
||||
/// </summary>
|
||||
/// <param name="args">The event arguments</param>
|
||||
/// TODO:
|
||||
void SendEvent(EventArgs args);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generic interface for the rendering system present in a <see cref="IPhysicalWindow"/>
|
||||
/// </summary>
|
||||
public interface IDeviceContext
|
||||
{
|
||||
/// <summary>
|
||||
/// The swap group for this device context.
|
||||
/// </summary>
|
||||
ISwapGroup SwapGroup { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The size of the window framebuffer in pixels.
|
||||
/// </summary>
|
||||
Size FramebufferSize { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Interface that is used to swap the buffers of
|
||||
/// </summary>
|
||||
public interface ISwapGroup
|
||||
{
|
||||
/// <summary>
|
||||
/// The swap interval for this swap group.
|
||||
/// </summary>
|
||||
public int SwapInterval { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Swap buffers.
|
||||
/// </summary>
|
||||
void Swap();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -98,5 +65,10 @@ namespace Dashboard
|
||||
/// True if the window is double buffered.
|
||||
/// </summary>
|
||||
public bool DoubleBuffered { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The window manager for this physical window.
|
||||
/// </summary>
|
||||
public IWindowManager? WindowManager { get; set; }
|
||||
}
|
||||
}
|
9
Dashboard.Drawing/IDrawQueuePaintable.cs
Normal file
9
Dashboard.Drawing/IDrawQueuePaintable.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using Dashboard.Windowing;
|
||||
|
||||
namespace Dashboard.Drawing
|
||||
{
|
||||
public interface IDrawQueuePaintable : IPaintable
|
||||
{
|
||||
DrawQueue DrawQueue { get; }
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using System.Drawing;
|
||||
using Dashboard.Windowing;
|
||||
|
||||
namespace Dashboard.OpenGL
|
||||
{
|
||||
|
18
Dashboard.OpenTK/PAL2/EventConverter.cs
Normal file
18
Dashboard.OpenTK/PAL2/EventConverter.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using Dashboard.Events;
|
||||
using OpenTK.Platform;
|
||||
|
||||
namespace Dashboard.OpenTK.PAL2
|
||||
{
|
||||
public static class EventConverter
|
||||
{
|
||||
public static UiEventArgs? Convert(PlatformEventType type, EventArgs ea)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case PlatformEventType.KeyDown:
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Drawing;
|
||||
using Dashboard.OpenGL;
|
||||
using Dashboard.Windowing;
|
||||
using OpenTK.Mathematics;
|
||||
using OpenTK.Platform;
|
||||
using TK = OpenTK.Platform.Toolkit;
|
||||
|
7
Dashboard.OpenTK/PAL2/OpenTKEventExtensions.cs
Normal file
7
Dashboard.OpenTK/PAL2/OpenTKEventExtensions.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace Dashboard.OpenTK.PAL2
|
||||
{
|
||||
public static class OpenTKEventExtensions
|
||||
{
|
||||
// public static EventArgs ToDashboardEvent(this EventArgs)
|
||||
}
|
||||
}
|
50
Dashboard.OpenTK/PAL2/Pal2DashboardBackend.cs
Normal file
50
Dashboard.OpenTK/PAL2/Pal2DashboardBackend.cs
Normal file
@ -0,0 +1,50 @@
|
||||
using Dashboard.Windowing;
|
||||
using OpenTK.Graphics;
|
||||
using OpenTK.Platform;
|
||||
using TK = OpenTK.Platform.Toolkit;
|
||||
|
||||
namespace Dashboard.OpenTK.PAL2
|
||||
{
|
||||
public class Pal2DashboardBackend : IDashboardBackend
|
||||
{
|
||||
public GraphicsApiHints GraphicsApiHints { get; set; } = new OpenGLGraphicsApiHints();
|
||||
public bool OpenGLBindingsInitialized { get; set; } = false;
|
||||
|
||||
public IPhysicalWindow CreatePhysicalWindow()
|
||||
{
|
||||
PhysicalWindow window = new PhysicalWindow(GraphicsApiHints);
|
||||
|
||||
if (!OpenGLBindingsInitialized)
|
||||
{
|
||||
OpenGLBindingsInitialized = true;
|
||||
GLLoader.LoadBindings(
|
||||
new Pal2BindingsContext(TK.OpenGL,
|
||||
((OpenGLDeviceContext)window.DeviceContext).ContextHandle));
|
||||
}
|
||||
|
||||
return window;
|
||||
}
|
||||
|
||||
public virtual IWindow CreateWindow()
|
||||
{
|
||||
return CreatePhysicalWindow();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
}
|
||||
|
||||
public void Leave()
|
||||
{
|
||||
}
|
||||
|
||||
public void RunEvents(bool wait)
|
||||
{
|
||||
TK.Window.ProcessEvents(wait);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,16 +1,24 @@
|
||||
using System.Drawing;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Drawing;
|
||||
using Dashboard.Drawing;
|
||||
using Dashboard.Events;
|
||||
using Dashboard.Windowing;
|
||||
using OpenTK.Mathematics;
|
||||
using OpenTK.Platform;
|
||||
using MouseMoveEventArgs = Dashboard.Events.MouseMoveEventArgs;
|
||||
using TK = OpenTK.Platform.Toolkit;
|
||||
|
||||
namespace Dashboard.OpenTK.PAL2
|
||||
{
|
||||
public class PhysicalWindow : IPhysicalWindow
|
||||
public class PhysicalWindow : IPhysicalWindow, IDrawQueuePaintable, IEventListener
|
||||
{
|
||||
public DrawQueue DrawQueue { get; } = new DrawQueue();
|
||||
public WindowHandle WindowHandle { get; }
|
||||
public IDeviceContext DeviceContext { get; }
|
||||
public bool DoubleBuffered => true; // Always true for OpenTK windows.
|
||||
|
||||
public IWindowManager? WindowManager { get; set; }
|
||||
|
||||
public string Title
|
||||
{
|
||||
get => TK.Window.GetTitle(WindowHandle);
|
||||
@ -37,22 +45,32 @@ namespace Dashboard.OpenTK.PAL2
|
||||
set => TK.Window.SetClientSize(WindowHandle, new Vector2i((int)value.Width, (int)value.Height));
|
||||
}
|
||||
|
||||
public event EventHandler? Painting;
|
||||
public event EventHandler<AnimationTickEventArgs>? AnimationTimerEvent;
|
||||
public event EventHandler<MouseMoveEventArgs>? MouseMoved;
|
||||
public event EventHandler<MouseButtonEventArgs>? MouseButtonDown;
|
||||
public event EventHandler<MouseButtonEventArgs>? MouseButtonUp;
|
||||
public event EventHandler<MouseScrollEventArgs>? MouseScroll;
|
||||
|
||||
public PhysicalWindow(WindowHandle window, IDeviceContext dc)
|
||||
{
|
||||
WindowHandle = window;
|
||||
DeviceContext = dc;
|
||||
AddWindow(this);
|
||||
}
|
||||
|
||||
public PhysicalWindow(WindowHandle window, OpenGLContextHandle context, ISwapGroup? swapGroup = null)
|
||||
{
|
||||
WindowHandle = window;
|
||||
DeviceContext = new OpenGLDeviceContext(window, context, swapGroup);
|
||||
AddWindow(this);
|
||||
}
|
||||
|
||||
public PhysicalWindow(GraphicsApiHints hints)
|
||||
{
|
||||
WindowHandle = TK.Window.Create(hints);
|
||||
DeviceContext = CreateDeviceContext(WindowHandle, hints);
|
||||
AddWindow(this);
|
||||
}
|
||||
|
||||
private static IDeviceContext CreateDeviceContext(WindowHandle window, GraphicsApiHints hints)
|
||||
@ -74,8 +92,73 @@ namespace Dashboard.OpenTK.PAL2
|
||||
if (_isDisposed) return;
|
||||
_isDisposed = true;
|
||||
|
||||
RemoveWindow(this);
|
||||
|
||||
(DeviceContext as IDisposable)?.Dispose();
|
||||
TK.Window.Destroy(WindowHandle);
|
||||
}
|
||||
|
||||
protected virtual void OnPaint()
|
||||
{
|
||||
WindowManager?.Paint();
|
||||
Painting?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
public void Paint()
|
||||
{
|
||||
DrawQueue.Clear();
|
||||
OnPaint();
|
||||
}
|
||||
|
||||
protected virtual void OnAnimationTimerEvent(AnimationTickEventArgs ea) =>
|
||||
AnimationTimerEvent?.Invoke(this, ea);
|
||||
|
||||
protected virtual void OnMouseMoved(MouseMoveEventArgs ea) => MouseMoved?.Invoke(this, ea);
|
||||
|
||||
protected virtual void OnMouseButtonDown(MouseButtonEventArgs ea) => MouseButtonDown?.Invoke(this, ea);
|
||||
|
||||
protected virtual void OnMouseButtonUp(MouseButtonEventArgs ea) => MouseButtonUp?.Invoke(this, ea);
|
||||
|
||||
protected virtual void OnMouseScroll(MouseScrollEventArgs ea) => MouseScroll?.Invoke(this, ea);
|
||||
|
||||
public void SendEvent(EventArgs args)
|
||||
{
|
||||
}
|
||||
|
||||
private static readonly ConcurrentDictionary<WindowHandle, PhysicalWindow> _windows =
|
||||
new ConcurrentDictionary<WindowHandle, PhysicalWindow>();
|
||||
|
||||
static PhysicalWindow()
|
||||
{
|
||||
EventQueue.EventRaised += EventQueueOnEventRaised;
|
||||
}
|
||||
|
||||
private static void AddWindow(PhysicalWindow window)
|
||||
{
|
||||
_windows.TryAdd(window.WindowHandle, window);
|
||||
}
|
||||
|
||||
private static void RemoveWindow(PhysicalWindow window)
|
||||
{
|
||||
_windows.TryRemove(window.WindowHandle, out _);
|
||||
}
|
||||
|
||||
private static void EventQueueOnEventRaised(PalHandle? handle, PlatformEventType type, EventArgs args)
|
||||
{
|
||||
if (handle is not WindowHandle windowHandle)
|
||||
return;
|
||||
|
||||
if (!_windows.TryGetValue(windowHandle, out PhysicalWindow? window))
|
||||
return;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case PlatformEventType.MouseMove:
|
||||
case PlatformEventType.Scroll:
|
||||
case PlatformEventType.MouseUp:
|
||||
case PlatformEventType.MouseDown:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
130
Dashboard/Application.cs
Normal file
130
Dashboard/Application.cs
Normal file
@ -0,0 +1,130 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Dashboard.Windowing;
|
||||
|
||||
namespace Dashboard
|
||||
{
|
||||
public class Application : IDisposable
|
||||
{
|
||||
public IDashboardBackend Backend { get; }
|
||||
public IWindowFactory WindowFactory { get; set; }
|
||||
|
||||
public string Name { get; } = "Dashboard Application";
|
||||
|
||||
protected bool IsInitialized { get; private set; } = false;
|
||||
protected bool IsDisposed { get; private set; } = false;
|
||||
|
||||
private readonly List<IPhysicalWindow> _physicalWindows = new List<IPhysicalWindow>();
|
||||
|
||||
public event EventHandler? PreInitializing;
|
||||
public event EventHandler? Initializing;
|
||||
public event EventHandler? PostInitializing;
|
||||
public event EventHandler? Leaving;
|
||||
|
||||
public Application(IDashboardBackend backend)
|
||||
{
|
||||
Backend = backend;
|
||||
WindowFactory = backend;
|
||||
}
|
||||
|
||||
protected virtual void PreInitialize()
|
||||
{
|
||||
PreInitializing?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
public virtual void Initialize()
|
||||
{
|
||||
Backend.Initialize();
|
||||
Initializing?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
protected virtual void PostInitialize()
|
||||
{
|
||||
PostInitializing?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
private void InitializeInternal()
|
||||
{
|
||||
if (IsInitialized)
|
||||
return;
|
||||
|
||||
IsInitialized = true;
|
||||
|
||||
PreInitialize();
|
||||
Initialize();
|
||||
PostInitialize();
|
||||
}
|
||||
|
||||
public virtual void RunEvents(bool wait)
|
||||
{
|
||||
if (!IsInitialized)
|
||||
throw new InvalidOperationException("The application is not initialized. Cannot run events at this time.");
|
||||
|
||||
Backend.RunEvents(wait);
|
||||
}
|
||||
|
||||
public virtual void Leave()
|
||||
{
|
||||
Backend.Leave();
|
||||
}
|
||||
|
||||
public void Run() => Run(true, CancellationToken.None);
|
||||
public void Run(bool wait) => Run(wait, CancellationToken.None);
|
||||
|
||||
public void Run(bool waitForEvents, CancellationToken token)
|
||||
{
|
||||
InitializeInternal();
|
||||
|
||||
while (!token.IsCancellationRequested)
|
||||
{
|
||||
RunEvents(waitForEvents);
|
||||
|
||||
foreach (IPhysicalWindow window in _physicalWindows)
|
||||
{
|
||||
window.Paint();
|
||||
}
|
||||
}
|
||||
|
||||
Leave();
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWindowFactory.CreateWindow"/>
|
||||
public IWindow CreateWindow()
|
||||
{
|
||||
IWindow window = WindowFactory.CreateWindow();
|
||||
|
||||
if (window is IPhysicalWindow physical)
|
||||
_physicalWindows.Add(physical);
|
||||
|
||||
return window;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWindowFactory.CreatePhysicalWindow"/>
|
||||
public IPhysicalWindow CreatePhysicalWindow()
|
||||
{
|
||||
IPhysicalWindow window = WindowFactory.CreatePhysicalWindow();
|
||||
_physicalWindows.Add(window);
|
||||
return window;
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
}
|
||||
|
||||
protected void InvokeDispose(bool disposing)
|
||||
{
|
||||
if (IsDisposed)
|
||||
return;
|
||||
|
||||
IsDisposed = true;
|
||||
|
||||
Dispose(disposing);
|
||||
|
||||
if (disposing)
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public void Dispose() => InvokeDispose(true);
|
||||
}
|
||||
}
|
77
Dashboard/Controls/ClassSet.cs
Normal file
77
Dashboard/Controls/ClassSet.cs
Normal file
@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Dashboard.Controls
|
||||
{
|
||||
public enum ClassChangeType
|
||||
{
|
||||
Added,
|
||||
Removed,
|
||||
}
|
||||
|
||||
public record struct ClassChanged(object? Owner, string ClassName, ClassChangeType Type);
|
||||
|
||||
public class ClassSet : ICollection<string>
|
||||
{
|
||||
public int Count => _classes.Count;
|
||||
public bool IsReadOnly => false;
|
||||
public object? Owner { get; }
|
||||
public event EventHandler<ClassChanged>? ClassChanged;
|
||||
|
||||
private readonly HashSet<string> _classes = new HashSet<string>();
|
||||
|
||||
public ClassSet(object? owner)
|
||||
{
|
||||
Owner = owner;
|
||||
}
|
||||
|
||||
public IEnumerator<string> GetEnumerator() => _classes.GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
public void Add(string item)
|
||||
{
|
||||
if (_classes.Add(item))
|
||||
{
|
||||
OnClassAdded(item);
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
foreach (string @class in _classes)
|
||||
OnClassRemoved(@class);
|
||||
|
||||
_classes.Clear();
|
||||
}
|
||||
|
||||
public bool Contains(string item) => _classes.Contains(item);
|
||||
|
||||
public void CopyTo(string[] array, int arrayIndex) => _classes.CopyTo(array, arrayIndex);
|
||||
|
||||
public bool Remove(string item)
|
||||
{
|
||||
if (_classes.Remove(item))
|
||||
{
|
||||
OnClassRemoved(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void OnClassAdded(string @class)
|
||||
{
|
||||
ClassChanged?.Invoke(this, new ClassChanged(Owner, @class, ClassChangeType.Added));
|
||||
}
|
||||
|
||||
private void OnClassRemoved(string @class)
|
||||
{
|
||||
ClassChanged?.Invoke(this, new ClassChanged(Owner, @class, ClassChangeType.Removed));
|
||||
}
|
||||
}
|
||||
}
|
68
Dashboard/Controls/Control.cs
Normal file
68
Dashboard/Controls/Control.cs
Normal file
@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using Dashboard.Drawing;
|
||||
using Dashboard.Windowing;
|
||||
|
||||
namespace Dashboard.Controls
|
||||
{
|
||||
public class Control : IEventListener, IDrawQueuePaintable, IDisposable
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public ClassSet Classes { get; }
|
||||
public Form? Owner { get; protected set; } = null;
|
||||
public Control? Parent { get; private set; } = null;
|
||||
public bool Disposed { get; private set; }
|
||||
public virtual DrawQueue DrawQueue => Owner?.DrawQueue ?? throw NoOwnerException;
|
||||
public virtual Box2d ClientArea { get; set; }
|
||||
|
||||
public event EventHandler? Painting;
|
||||
public event EventHandler? OwnerChanged;
|
||||
public event EventHandler? ParentChanged;
|
||||
public event EventHandler? Disposing;
|
||||
public event EventHandler? Resized;
|
||||
|
||||
public Control()
|
||||
{
|
||||
Classes = new ClassSet(this);
|
||||
}
|
||||
|
||||
public virtual void OnPaint()
|
||||
{
|
||||
Painting?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
public void Paint()
|
||||
{
|
||||
OnPaint();
|
||||
}
|
||||
|
||||
protected void InvokeDispose(bool disposing)
|
||||
{
|
||||
if (Disposed)
|
||||
return;
|
||||
Disposed = true;
|
||||
|
||||
Dispose(disposing);
|
||||
|
||||
if (disposing)
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
}
|
||||
|
||||
public void Dispose() => InvokeDispose(true);
|
||||
|
||||
public void SendEvent(EventArgs args)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
internal static void SetParent(Control parent, Control child)
|
||||
{
|
||||
child.Parent = parent;
|
||||
}
|
||||
|
||||
protected static Exception NoOwnerException => new Exception("No form owns this control");
|
||||
}
|
||||
}
|
23
Dashboard/Controls/Form.cs
Normal file
23
Dashboard/Controls/Form.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using Dashboard.Drawing;
|
||||
using Dashboard.Windowing;
|
||||
|
||||
namespace Dashboard.Controls
|
||||
{
|
||||
public class Form : Control
|
||||
{
|
||||
public IWindow Window { get; }
|
||||
public override DrawQueue DrawQueue { get; }
|
||||
|
||||
public override Box2d ClientArea
|
||||
{
|
||||
get => new Box2d(0, 0, Window.ClientSize.Width, Window.ClientSize.Height);
|
||||
set { }
|
||||
}
|
||||
|
||||
public Form(IWindow window)
|
||||
{
|
||||
Window = window;
|
||||
DrawQueue = (window as IDrawQueuePaintable)?.DrawQueue ?? new DrawQueue();
|
||||
}
|
||||
}
|
||||
}
|
37
Dashboard/Controls/Label.cs
Normal file
37
Dashboard/Controls/Label.cs
Normal file
@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Numerics;
|
||||
using Dashboard.Drawing;
|
||||
|
||||
namespace Dashboard.Controls
|
||||
{
|
||||
public class Label : Control
|
||||
{
|
||||
public bool AutoSize { get; set; } = true;
|
||||
public string Text { get; set; } = "";
|
||||
|
||||
public event EventHandler? TextChanged;
|
||||
|
||||
protected IBrush TextBrush => throw new NotImplementedException();
|
||||
protected IFont Font => throw new NotImplementedException();
|
||||
|
||||
protected virtual void OnTextChanged(string oldValue, string newValue)
|
||||
{
|
||||
if (AutoSize)
|
||||
CalculateSize();
|
||||
}
|
||||
|
||||
protected void CalculateSize()
|
||||
{
|
||||
SizeF sz = Typesetter.MeasureString(Font, Text);
|
||||
ClientArea = new Box2d(ClientArea.Min, ClientArea.Min + (Vector2)sz);
|
||||
}
|
||||
|
||||
|
||||
public override void OnPaint()
|
||||
{
|
||||
base.OnPaint();
|
||||
DrawQueue.Text(new Vector3(ClientArea.Min, 0), TextBrush, Text, Font);
|
||||
}
|
||||
}
|
||||
}
|
29
Dashboard/IDashboardBackend.cs
Normal file
29
Dashboard/IDashboardBackend.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using Dashboard.Windowing;
|
||||
|
||||
namespace Dashboard
|
||||
{
|
||||
public interface IWindowFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a window. It could be a virtual window, or a physical window.
|
||||
/// </summary>
|
||||
/// <returns>A window.</returns>
|
||||
IWindow CreateWindow();
|
||||
|
||||
/// <summary>
|
||||
/// Always creates a physical window.
|
||||
/// </summary>
|
||||
/// <returns>A physical window.</returns>
|
||||
IPhysicalWindow CreatePhysicalWindow();
|
||||
}
|
||||
|
||||
public interface IDashboardBackend : IDisposable, IWindowFactory
|
||||
{
|
||||
void Initialize();
|
||||
|
||||
void RunEvents(bool wait);
|
||||
|
||||
void Leave();
|
||||
}
|
||||
}
|
@ -10,6 +10,7 @@ using OpenTK.Graphics.OpenGL;
|
||||
using OpenTK.Mathematics;
|
||||
using Box2d = Dashboard.Box2d;
|
||||
using TK = OpenTK.Platform.Toolkit;
|
||||
using Dashboard;
|
||||
|
||||
TK.Init(new ToolkitOptions()
|
||||
{
|
||||
@ -21,8 +22,10 @@ TK.Init(new ToolkitOptions()
|
||||
}
|
||||
});
|
||||
|
||||
PhysicalWindow window = new PhysicalWindow(new OpenGLGraphicsApiHints()
|
||||
Application app = new Application(new Pal2DashboardBackend()
|
||||
{
|
||||
GraphicsApiHints = new OpenGLGraphicsApiHints()
|
||||
{
|
||||
Version = new Version(3, 2),
|
||||
ForwardCompatibleFlag = true,
|
||||
DebugFlag = true,
|
||||
@ -38,67 +41,70 @@ PhysicalWindow window = new PhysicalWindow(new OpenGLGraphicsApiHints()
|
||||
Multisamples = 0,
|
||||
|
||||
SupportTransparentFramebufferX11 = true,
|
||||
}
|
||||
});
|
||||
|
||||
window.Title = "DashTerm";
|
||||
TK.Window.SetMinClientSize(window.WindowHandle, 300, 200);
|
||||
TK.Window.SetClientSize(window.WindowHandle, new Vector2i(320, 240));
|
||||
TK.Window.SetBorderStyle(window.WindowHandle, WindowBorderStyle.ResizableBorder);
|
||||
// TK.Window.SetTransparencyMode(wnd, WindowTransparencyMode.TransparentFramebuffer, 0.1f);
|
||||
|
||||
OpenGLDeviceContext context = (OpenGLDeviceContext)window.DeviceContext;
|
||||
|
||||
context.MakeCurrent();
|
||||
context.SwapGroup.SwapInterval = 1;
|
||||
|
||||
GLLoader.LoadBindings(new Pal2BindingsContext(TK.OpenGL, context.ContextHandle));
|
||||
|
||||
DrawQueue queue = new DrawQueue();
|
||||
PhysicalWindow window;
|
||||
SolidBrush fg = new SolidBrush(Color.FromArgb(0, 0, 0, 0));
|
||||
SolidBrush bg = new SolidBrush(Color.Black);
|
||||
bool shouldExit = false;
|
||||
|
||||
GLEngine engine = new GLEngine();
|
||||
engine.Initialize();
|
||||
|
||||
ContextExecutor executor = engine.GetExecutor(context);
|
||||
DimUI dimUI = new DimUI(new DimUIConfig()
|
||||
{
|
||||
Font = new NamedFont("Noto Sans", 9f),
|
||||
});
|
||||
|
||||
CancellationTokenSource source = new CancellationTokenSource();
|
||||
GLEngine engine;
|
||||
ContextExecutor executor;
|
||||
DimUI dimUI;
|
||||
Vector2 mousePos = Vector2.Zero;
|
||||
Random r = new Random();
|
||||
EventQueue.EventRaised += (handle, type, eventArgs) =>
|
||||
{
|
||||
List<Vector3> points = new List<Vector3>();
|
||||
IFont font;
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
app.PostInitializing += (sender, ea) => {
|
||||
window = (PhysicalWindow)app.CreatePhysicalWindow();
|
||||
|
||||
window.Title = "DashTerm";
|
||||
TK.Window.SetMinClientSize(window.WindowHandle, 300, 200);
|
||||
TK.Window.SetClientSize(window.WindowHandle, new Vector2i(320, 240));
|
||||
TK.Window.SetBorderStyle(window.WindowHandle, WindowBorderStyle.ResizableBorder);
|
||||
// TK.Window.SetTransparencyMode(wnd, WindowTransparencyMode.TransparentFramebuffer, 0.1f);
|
||||
|
||||
OpenGLDeviceContext context = (OpenGLDeviceContext)window.DeviceContext;
|
||||
|
||||
context.MakeCurrent();
|
||||
context.SwapGroup.SwapInterval = 1;
|
||||
|
||||
engine = new GLEngine();
|
||||
engine.Initialize();
|
||||
|
||||
executor = engine.GetExecutor(context);
|
||||
|
||||
dimUI = new DimUI(new DimUIConfig()
|
||||
{
|
||||
Font = new NamedFont("Noto Sans", 9f),
|
||||
});
|
||||
|
||||
EventQueue.EventRaised += (handle, type, eventArgs) =>
|
||||
{
|
||||
if (handle != window.WindowHandle)
|
||||
return;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case PlatformEventType.Close:
|
||||
shouldExit = true;
|
||||
source.Cancel();
|
||||
break;
|
||||
case PlatformEventType.MouseMove:
|
||||
mousePos = ((MouseMoveEventArgs)eventArgs).ClientPosition;
|
||||
break;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
TK.Window.SetMode(window.WindowHandle, WindowMode.Normal);
|
||||
TK.Window.SetMode(window.WindowHandle, WindowMode.Normal);
|
||||
font = Typesetter.LoadFont("Nimbus Mono", 12f);
|
||||
|
||||
List<Vector3> points = new List<Vector3>();
|
||||
|
||||
IFont font = Typesetter.LoadFont("Nimbus Mono", 12f);
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
while (!shouldExit)
|
||||
{
|
||||
TK.Window.ProcessEvents(true);
|
||||
window.Painting += (sender, ea) => {
|
||||
TK.Window.GetFramebufferSize(context.WindowHandle, out Vector2i framebufferSize);
|
||||
executor.BeginFrame();
|
||||
|
||||
dimUI.Begin(new Box2d(0, 0, framebufferSize.X, framebufferSize.Y), queue);
|
||||
dimUI.Begin(new Box2d(0, 0, framebufferSize.X, framebufferSize.Y), window.DrawQueue);
|
||||
dimUI.Text("Hello World!");
|
||||
dimUI.Button("Cancel"); dimUI.SameLine();
|
||||
dimUI.Button("OK");
|
||||
@ -161,9 +167,11 @@ while (!shouldExit)
|
||||
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
|
||||
GL.ColorMask(true, true, true, true);
|
||||
|
||||
executor.Draw(queue);
|
||||
executor.Draw(window.DrawQueue);
|
||||
executor.EndFrame();
|
||||
queue.Clear();
|
||||
|
||||
context.SwapGroup.Swap();
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
app.Run(true, source.Token);
|
||||
|
Loading…
Reference in New Issue
Block a user