Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up window events. #967

Merged
merged 4 commits into from Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -12,7 +12,7 @@ namespace OpenToolkit.Windowing.Common
/// <summary>
/// Raised when a single unicode code point is input.
/// </summary>
public struct TextInputEventArgs
public readonly struct TextInputEventArgs
{
/// <summary>
/// Initializes a new instance of the <see cref="TextInputEventArgs"/> struct.
Expand Down

This file was deleted.

This file was deleted.

Expand Up @@ -43,26 +43,26 @@ public interface IGameWindow : INativeWindow, IGameWindowProperties
/// occurs when the update thread has started. This would be a good place to initialize thread specific stuff (like
/// setting a synchronization context).
/// </summary>
event EventHandler<EventArgs> RenderThreadStarted;
event Action RenderThreadStarted;

/// <summary>
/// Occurs before the window is displayed for the first time.
/// </summary>
event EventHandler<EventArgs> Load;
event Action Load;

/// <summary>
/// Occurs before the window is destroyed.
/// </summary>
event EventHandler<EventArgs> Unload;
event Action Unload;

/// <summary>
/// Occurs when it is time to update a frame.
/// </summary>
event EventHandler<FrameEventArgs> UpdateFrame;
event Action<FrameEventArgs> UpdateFrame;

/// <summary>
/// Occurs when it is time to render a frame.
/// </summary>
event EventHandler<FrameEventArgs> RenderFrame;
event Action<FrameEventArgs> RenderFrame;
}
}
Expand Up @@ -21,129 +21,96 @@ public interface IWindowEvents
/// <summary>
/// Occurs whenever the window is moved.
/// </summary>
event EventHandler<WindowPositionEventArgs> Move;
event Action<WindowPositionEventArgs> Move;

/// <summary>
/// Occurs whenever the window is resized.
/// </summary>
event EventHandler<ResizeEventArgs> Resize;
event Action<ResizeEventArgs> Resize;

/// <summary>
/// Occurs whenever the window is refreshed.
/// </summary>
event EventHandler<EventArgs> Refresh;
event Action Refresh;

/// <summary>
/// Occurs when the window is about to close.
/// </summary>
event EventHandler<CancelEventArgs> Closing;
event Action<CancelEventArgs> Closing;

/// <summary>
/// Occurs after the window has closed.
/// </summary>
event EventHandler<EventArgs> Closed;

/// <summary>
/// Occurs when the window is disposed.
/// </summary>
event EventHandler<EventArgs> Disposed;
event Action Closed;

/// <summary>
/// Occurs when the window is minimized.
/// </summary>
event EventHandler<MinimizedEventArgs> Minimized;

/// <summary>
/// Occurs when the icon of the window changes.
/// </summary>
event EventHandler<EventArgs> IconChanged;
event Action<MinimizedEventArgs> Minimized;

/// <summary>
/// Occurs when a joystick is connected or disconnected.
/// </summary>
event EventHandler<JoystickEventArgs> JoystickConnected;

/// <summary>
/// Occurs when the <see cref="INativeWindowProperties.Title" /> property of the window changes.
/// </summary>
event EventHandler<TitleChangedEventArgs> TitleChanged;

/// <summary>
/// Occurs when the <see cref="INativeWindowProperties.IsVisible" /> property of the window changes.
/// </summary>
event EventHandler<VisibilityChangedEventArgs> VisibleChanged;
event Action<JoystickEventArgs> JoystickConnected;

/// <summary>
/// Occurs when the <see cref="INativeWindowProperties.IsFocused" /> property of the window changes.
/// </summary>
event EventHandler<FocusedChangedEventArgs> FocusedChanged;

/// <summary>
/// Occurs when the <see cref="WindowBorder" /> property of the window changes.
/// </summary>
event EventHandler<EventArgs> WindowBorderChanged;

/// <summary>
/// Occurs when the <see cref="WindowState" /> property of the window changes.
/// </summary>
event EventHandler<EventArgs> WindowStateChanged;
event Action<FocusedChangedEventArgs> FocusedChanged;

/// <summary>
/// Occurs whenever a keyboard key is pressed.
/// </summary>
event EventHandler<KeyboardKeyEventArgs> KeyDown;
event Action<KeyboardKeyEventArgs> KeyDown;

/// <summary>
/// Occurs whenever a Unicode code point is typed.
/// </summary>
event EventHandler<TextInputEventArgs> TextInput;
event Action<TextInputEventArgs> TextInput;

/// <summary>
/// Occurs whenever a keyboard key is released.
/// </summary>
event EventHandler<KeyboardKeyEventArgs> KeyUp;
event Action<KeyboardKeyEventArgs> KeyUp;

/// <summary>
/// Occurs when a <see cref="Monitor"/> is connected or disconnected.
/// </summary>
event EventHandler<MonitorEventArgs> MonitorConnected;
event Action<MonitorEventArgs> MonitorConnected;

/// <summary>
/// Occurs whenever the mouse cursor leaves the window <see cref="INativeWindowProperties.Bounds" />.
/// </summary>
event EventHandler<EventArgs> MouseLeave;
event Action MouseLeave;

/// <summary>
/// Occurs whenever the mouse cursor enters the window <see cref="INativeWindowProperties.Bounds" />.
/// </summary>
event EventHandler<EventArgs> MouseEnter;
event Action MouseEnter;

/// <summary>
/// Occurs whenever a <see cref="Input.MouseButton" /> is clicked.
/// </summary>
event EventHandler<MouseButtonEventArgs> MouseDown;
event Action<MouseButtonEventArgs> MouseDown;

/// <summary>
/// Occurs whenever a <see cref="Input.MouseButton" /> is released.
/// </summary>
event EventHandler<MouseButtonEventArgs> MouseUp;
event Action<MouseButtonEventArgs> MouseUp;

/// <summary>
/// Occurs whenever the mouse cursor is moved;
/// </summary>
event EventHandler<MouseMoveEventArgs> MouseMove;
event Action<MouseMoveEventArgs> MouseMove;

/// <summary>
/// Occurs whenever a mouse wheel is moved;
/// </summary>
event EventHandler<MouseWheelEventArgs> MouseWheel;

// event EventHandler<MouseEventArgs> MouseClick;
// event EventHandler<MouseEventArgs> MouseDoubleClick;
event Action<MouseWheelEventArgs> MouseWheel;

/// <summary>
/// Occurs whenever file dropped on window.
/// </summary>
event EventHandler<FileDropEventArgs> FileDrop;
event Action<FileDropEventArgs> FileDrop;
}
}
50 changes: 21 additions & 29 deletions src/Windowing/OpenToolkit.Windowing.Desktop/GameWindow.cs
Expand Up @@ -46,19 +46,19 @@ namespace OpenToolkit.Windowing.Desktop
public class GameWindow : NativeWindow, IGameWindow
{
/// <inheritdoc/>
public event EventHandler<EventArgs> Load;
public event Action Load;

/// <inheritdoc/>
public event EventHandler<EventArgs> Unload;
public event Action Unload;

/// <inheritdoc/>
public event EventHandler<FrameEventArgs> UpdateFrame;
public event Action<FrameEventArgs> UpdateFrame;

/// <inheritdoc/>
public event EventHandler<EventArgs> RenderThreadStarted;
public event Action RenderThreadStarted;

/// <inheritdoc/>
public event EventHandler<FrameEventArgs> RenderFrame;
public event Action<FrameEventArgs> RenderFrame;

/// <summary>
/// Frequency cap for Update/RenderFrame events.
Expand Down Expand Up @@ -189,10 +189,10 @@ public virtual void Run()
IsVisible = true;

// Send the OnLoad event, to load all user code.
OnLoad(this, EventArgs.Empty);
OnLoad();

// Send a redundant OnResize event, to make sure all user code has the correct values.
OnResize(this, new ResizeEventArgs(Width, Height));
OnResize(new ResizeEventArgs(Width, Height));

Debug.Print("Entering main loop.");
if (IsMultiThreaded)
Expand Down Expand Up @@ -223,7 +223,7 @@ public virtual void Run()

private void StartRenderThread()
{
OnRenderThreadStarted(this, EventArgs.Empty);
OnRenderThreadStarted();
_watchRender.Start();
while (Exists && !IsExiting)
{
Expand All @@ -240,7 +240,7 @@ private void DispatchUpdateFrame()

while (elapsed > 0 && elapsed + _updateEpsilon >= updatePeriod)
{
OnUpdateFrame(this, new FrameEventArgs(elapsed));
OnUpdateFrame(new FrameEventArgs(elapsed));

// Calculate difference (positive or negative) between
// actual elapsed time and target elapsed time. We must
Expand Down Expand Up @@ -276,7 +276,7 @@ private void DispatchUpdateFrame()
private void DispatchRenderFrame()
{
var timestamp = _watchRender.Elapsed.TotalMilliseconds;
OnRenderFrame(this, new FrameEventArgs(timestamp));
OnRenderFrame(new FrameEventArgs(timestamp));
_watchRender.Restart();
}

Expand All @@ -292,58 +292,50 @@ public virtual void SwapBuffers()
/// <inheritdoc />
public override void Close()
{
OnUnload(this, EventArgs.Empty);
OnUnload();
base.Close();
}

/// <summary>
/// Run when the update thread is started. This will never run if you set IsSingleThreaded to true.
/// </summary>
/// <param name="sender">A reference to the window that ran the function.</param>
/// <param name="args">The event arguments. Always empty for this function.</param>
protected virtual void OnRenderThreadStarted(object sender, EventArgs args)
protected virtual void OnRenderThreadStarted()
{
RenderThreadStarted?.Invoke(sender, args);
RenderThreadStarted?.Invoke();
}

/// <summary>
/// Run immediately after Run() is called.
/// </summary>
/// <param name="sender">A reference to the window that ran the function.</param>
/// <param name="args">The event arguments. Always empty for this function.</param>
protected virtual void OnLoad(object sender, EventArgs args)
protected virtual void OnLoad()
{
Load?.Invoke(sender, args);
Load?.Invoke();
}

/// <summary>
/// Run when the window is about to close.
/// </summary>
/// <param name="sender">A reference to the window that ran the function.</param>
/// <param name="args">The event arguments. Always empty for this function.</param>
protected virtual void OnUnload(object sender, EventArgs args)
protected virtual void OnUnload()
{
Unload?.Invoke(sender, args);
Unload?.Invoke();
}

/// <summary>
/// Run when the window is ready to update.
/// </summary>
/// <param name="sender">A reference to the window that ran the function.</param>
/// <param name="args">The event arguments for this frame.</param>
protected virtual void OnUpdateFrame(object sender, FrameEventArgs args)
protected virtual void OnUpdateFrame(FrameEventArgs args)
{
UpdateFrame?.Invoke(sender, args);
UpdateFrame?.Invoke(args);
}

/// <summary>
/// Run when the window is ready to update.
/// </summary>
/// <param name="sender">A reference to the window that ran the function.</param>
/// <param name="args">The event arguments for this frame.</param>
protected virtual void OnRenderFrame(object sender, FrameEventArgs args)
protected virtual void OnRenderFrame(FrameEventArgs args)
{
RenderFrame?.Invoke(sender, args);
RenderFrame?.Invoke(args);
}
}
}