Browse Source

Began implementing event handling. Events are polled, and a method of converting event structs has been added.

improved_timing
Ian Burgmyer 7 years ago
parent
commit
2caf1521ae
  1. 46
      DotSDL/Events/EventHandler.cs
  2. 6
      DotSDL/Events/IEvent.cs
  3. 22
      DotSDL/Events/WindowEvent.cs
  4. 57
      DotSDL/Events/WindowEventType.cs
  5. 2
      DotSDL/IResourceObject.cs
  6. 5
      DotSDL/Sdl/Events.cs
  7. 56
      DotSDL/Sdl/Video.cs

46
DotSDL/Events/EventHandler.cs

@ -1,4 +1,7 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using SdlEvents = DotSDL.Sdl.Events;
namespace DotSDL.Events {
/// <summary>
@ -7,11 +10,52 @@ namespace DotSDL.Events {
internal static class EventHandler {
private static readonly ResourceManager Resources = ResourceManager.Instance;
/// <summary>
/// An incredibly unsafe function that forcibly casts one type to
/// another. This is used to convert between SDL2 events, since C# has
/// no concept of union types.
/// </summary>
/// <typeparam name="T">The type to cast to.</typeparam>
/// <param name="sdlEvent">The event that should be converted.</param>
/// <returns>A structure of type T, containing the data in <paramref name="sdlEvent"/>.</returns>
private static unsafe T CastEvent<T>(SdlEvents.SdlEvent sdlEvent) {
var sdlEventIntPtr = new IntPtr(&sdlEvent);
return Marshal.PtrToStructure<T>(sdlEventIntPtr);
}
/// <summary>
/// Converts an <see cref="SdlEvents.SdlEvent"/> into an <see cref="IEvent"/>
/// for use with applications.
/// </summary>
/// <param name="sdlEvent">The <see cref="SdlEvents.SdlEvent"/> to process.</param>
/// <returns>An <see cref="IEvent"/> that can be passed to an application.</returns>
private static IEvent ConvertEvent(SdlEvents.SdlEvent sdlEvent) {
if(sdlEvent.Type == SdlEvents.EventType.WindowEvent) {
var ohgod = CastEvent<SdlEvents.SdlWindowEvent>(sdlEvent);
Debug.WriteLine(ohgod.EventId);
}
return null;
}
/// <summary>
/// Dispatches an <see cref="IEvent"/> to the appropriate <see cref="IResourceObject"/>.
/// </summary>
/// <param name="newEvent">The <see cref="IEvent"/> to dispatch.</param>
private static void DispatchEvent(IEvent newEvent) {
}
/// <summary>
/// Processes and dispatches all outstanding events.
/// </summary>
internal static void ProcessEvents() {
throw new NotImplementedException();
var inEvent = new SdlEvents.SdlEvent();
while(SdlEvents.PollEvent(ref inEvent) != 0) {
var newEvent = ConvertEvent(inEvent);
if(newEvent is null) continue;
if(newEvent.Resource != null)
DispatchEvent(newEvent);
}
}
}
}

6
DotSDL/Events/IEvent.cs

@ -8,5 +8,11 @@
/// starting from when the application is first executed.
/// </summary>
uint Timestamp { get; set; }
/// <summary>
/// The resources that this event should be dispatched to. If this
/// is null, the event will not be dispatched.
/// </summary>
IResourceObject Resource { get; set; }
}
}

22
DotSDL/Events/WindowEvent.cs

@ -1,11 +1,31 @@
namespace DotSDL.Events {
/// <summary>
/// Represents a window event. Window events are thrown when an event occurs
/// that affects an <see cref="DotSDL.Graphics.SdlWindow"/> instance and/or
/// that affects an <see cref="Graphics.SdlWindow"/> instance and/or
/// the applicaiton as a whole. This includes the UI toolkit minimizing,
/// maximizing, and closing a window, as well as application quit messages.
/// </summary>
public class WindowEvent : IEvent {
public uint Timestamp { get; set; }
public IResourceObject Resource { get; set; }
/// <summary>
/// The event that was triggered.
/// </summary>
public WindowEvent Event { get; set; }
/// <summary>
/// When Event is WindowEventType.Moved, this contains the new X coordinate
/// of the window. When Event is WindowEventType.Resized, this contains the
/// new width of the window. For all other events, this value is unused.
/// </summary>
public int X { get; set; }
/// <summary>
/// When Event is WindowEventType.Moved, this contains the new Y coordinate
/// of the window. When Event is WindowEventType.Resized, this contains the
/// new height of the window. For all other events, this value is unused.
/// </summary>
public int Y { get; set; }
}
}

57
DotSDL/Events/WindowEventType.cs

@ -0,0 +1,57 @@
namespace DotSDL.Events {
/// <summary>
/// Events that can occur on a window.
/// </summary>
public enum WindowEventType : byte {
/// <summary>Unused.</summary>
None,
/// <summary>Window has been shown.</summary>
Shown,
/// <summary>Window has been hidden.</summary>
Hidden,
/// <summary>Window has been exposed and should be redrawn.</summary>
Exposed,
/// <summary>Window has been moved.</summary>
Moved,
/// <summary>Window has been resized.</summary>
Resized,
/// <summary>The window size has changed, either as a result of an API call or through the system or user changing the window size.</summary>
SizeChanged,
/// <summary>Window has been minimized.</summary>
Minimized,
/// <summary>Window has been maximized.</summary>
Maximized,
/// <summary>Window has been restored to normal size and position.</summary>
Restored,
/// <summary>Window has gained mouse focus.</summary>
Enter,
/// <summary>Window has lost mouse focus.</summary>
Leave,
/// <summary>Window has gained keyboard focus.</summary>
FocusGained,
/// <summary>Window has lost keyboard focus.</summary>
FocusLost,
/// <summary>The window manager requests that the window be closed.</summary>
Close,
/// <summary>Window is being offered a focus (should SetWindowInputFocus() on itself or a subwindow, or ignore).</summary>
TakeFocus,
/// <summary>Window had a hit test that wasn't SDL_HITTEST_NORMAL.</summary>
HitTest
}
}

2
DotSDL/IResourceObject.cs

@ -2,7 +2,7 @@
/// <summary>
/// Specifies an object that contains a resource.
/// </summary>
internal interface IResourceObject {
public interface IResourceObject {
/// <summary>
/// <c>true</c> if the underlying <see cref="IResourceObject"/> has been destroyed, otherwise <c>false</c>.
/// </summary>

5
DotSDL/Sdl/Events.cs

@ -5,6 +5,7 @@ using DotSDL.Input.Mouse;
using MouseButton = DotSDL.Input.Mouse.Button;
using System;
using System.Runtime.InteropServices;
using DotSDL.Events;
namespace DotSDL.Sdl {
/// <summary>
@ -478,7 +479,7 @@ namespace DotSDL.Sdl {
internal EventType Type;
internal uint Timestamp;
internal uint WindowId;
internal byte EventId;
internal WindowEventType EventId;
internal byte Padding1;
internal byte Padding2;
internal byte Padding3;
@ -492,6 +493,6 @@ namespace DotSDL.Sdl {
/// <param name="sdlEvent">An object to store event data into. If this is not NULL, the event is removed from the queue and stored into the object.</param>
/// <returns>1 if there are any pending events, or 0 if there are none available.</returns>
[DllImport(Meta.DllName, EntryPoint = "SDL_PollEvent", CallingConvention = CallingConvention.Cdecl)]
internal static extern int PollEvent(object sdlEvent);
internal static extern int PollEvent(ref SdlEvent sdlEvent);
}
}

56
DotSDL/Sdl/Video.cs

@ -75,62 +75,6 @@ namespace DotSDL.Sdl {
PopupMenu = 0x00080000
}
/// <summary>
/// Events that can occur on a window.
/// </summary>
internal enum WindowEvent : byte {
/// <summary>Unused.</summary>
None,
/// <summary>Window has been shown.</summary>
Shown,
/// <summary>Window has been hidden.</summary>
Hidden,
/// <summary>Window has been exposed and should be redrawn.</summary>
Exposed,
/// <summary>Window has been moved.</summary>
Moved,
/// <summary>Window has been resized.</summary>
Resized,
/// <summary>The window size has changed, either as a result of an API call or through the system or user changing the window size.</summary>
SizeChanged,
/// <summary>Window has been minimized.</summary>
Minimized,
/// <summary>Window has been maximized.</summary>
Maximized,
/// <summary>Window has been restored to normal size and position.</summary>
Restored,
/// <summary>Window has gained mouse focus.</summary>
Enter,
/// <summary>Window has lost mouse focus.</summary>
Leave,
/// <summary>Window has gained keyboard focus.</summary>
FocusGained,
/// <summary>Window has lost keyboard focus.</summary>
FocusLost,
/// <summary>The window manager requests that the window be closed.</summary>
Close,
/// <summary>Window is being offered a focus (should SetWindowInputFocus() on itself or a subwindow, or ignore).</summary>
TakeFocus,
/// <summary>Window had a hit test that wasn't SDL_HITTEST_NORMAL.</summary>
HitTest
}
/// <summary>
/// Creates a window with the specified position, dimensions, and flags.
/// </summary>

Loading…
Cancel
Save