Browse Source

SdlWindow objects can now respond to close actions.

improved_timing
Ian Burgmyer 7 years ago
parent
commit
ebffa2803c
  1. 39
      DotSDL/Events/EventConversion.cs
  2. 21
      DotSDL/Events/EventDispatcher.cs
  3. 15
      DotSDL/Events/EventHandler.cs
  4. 2
      DotSDL/Events/WindowEvent.cs
  5. 23
      DotSDL/Graphics/SdlWindow.cs
  6. 8
      Samples/Sample.BasicPixels/Window.cs

39
DotSDL/Events/EventConversion.cs

@ -0,0 +1,39 @@
using SdlEvents = DotSDL.Sdl.Events;
namespace DotSDL.Events {
/// <summary>
/// Converts from a SDL2-style event to a DotSDL-style <see cref="IEvent"/>.
/// </summary>
internal static class EventConversion {
private static readonly ResourceManager Resources = ResourceManager.Instance;
/// <summary>
/// Converts an SDL2-style event to a DotSDL-style <see cref="IEvent"/>.
/// </summary>
/// <param name="sdlEvent">The event to convert.</param>
/// <returns>A DotSDL <see cref="IEvent"/> that can be passed to applications.</returns>
internal static IEvent Convert(object sdlEvent) {
switch(sdlEvent) {
case SdlEvents.SdlWindowEvent e:
return ConvertWindowEvent(e);
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
private static WindowEvent ConvertWindowEvent(SdlEvents.SdlWindowEvent e) {
return new WindowEvent {
Timestamp = e.Timestamp,
Event = e.EventId,
Resource = Resources.GetResourceById(e.WindowId),
X = e.Data1,
Y = e.Data2
};
}
}
}

21
DotSDL/Events/EventDispatcher.cs

@ -0,0 +1,21 @@
using DotSDL.Graphics;
namespace DotSDL.Events {
/// <summary>
/// Handles dispatching events to an application's windows and other objects.
/// </summary>
internal static class EventDispatcher {
/// <summary>
/// Dispatches an event to its associated object.
/// </summary>
/// <param name="ev">The event to process.</param>
internal static void Dispatch(IEvent ev) {
switch(ev) {
case WindowEvent e:
var window = (SdlWindow)e.Resource;
window.HandleEvent(e);
break;
}
}
}
}

15
DotSDL/Events/EventHandler.cs

@ -7,8 +7,6 @@ namespace DotSDL.Events {
/// Handles dispatching events to objects that can receive them.
/// </summary>
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
@ -31,19 +29,12 @@ namespace DotSDL.Events {
private static IEvent ConvertEvent(SdlEvents.SdlEvent sdlEvent) {
switch(sdlEvent.Type) {
case SdlEvents.EventType.WindowEvent:
var sdlWindowEvent = CastEvent<SdlEvents.SdlWindowEvent>(sdlEvent);
break;
var e = CastEvent<SdlEvents.SdlWindowEvent>(sdlEvent);
return EventConversion.Convert(e);
}
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>
@ -54,7 +45,7 @@ namespace DotSDL.Events {
var newEvent = ConvertEvent(inEvent);
if(newEvent is null) continue;
if(newEvent.Resource != null)
DispatchEvent(newEvent);
EventDispatcher.Dispatch(newEvent);
}
}
}

2
DotSDL/Events/WindowEvent.cs

@ -12,7 +12,7 @@
/// <summary>
/// The event that was triggered.
/// </summary>
public WindowEvent Event { get; set; }
public WindowEventType Event { get; set; }
/// <summary>
/// When Event is WindowEventType.Moved, this contains the new X coordinate

23
DotSDL/Graphics/SdlWindow.cs

@ -1,5 +1,6 @@
using DotSDL.Sdl;
using System;
using DotSDL.Events;
namespace DotSDL.Graphics {
/// <summary>
@ -60,6 +61,8 @@ namespace DotSDL.Graphics {
/// </summary>
public const int WindowPosCentered = 0x2FFF0000;
public event EventHandler<WindowEvent> Close;
/// <summary>
/// Calculates a value that allows the window to be placed in the center of a specified display.
/// </summary>
@ -163,6 +166,18 @@ namespace DotSDL.Graphics {
return Video.GetWindowId(_window);
}
/// <summary>
/// Triggers this window to handle a specified event.
/// </summary>
/// <param name="ev">The event to handle</param>
internal void HandleEvent(WindowEvent ev) {
switch(ev.Event) {
case WindowEventType.Close:
Close?.Invoke(this, ev);
break;
}
}
/// <summary>
/// A game loop that calls the <see cref="SdlWindow"/> update and draw functions.
/// </summary>
@ -234,5 +249,13 @@ namespace DotSDL.Graphics {
Video.ShowWindow(_window);
Loop();
}
/// <summary>
/// Stops executing the game loop and destroys the window.
/// </summary>
public void Stop() {
_running = false;
DestroyObject();
}
}
}

8
Samples/Sample.BasicPixels/Window.cs

@ -3,7 +3,9 @@ using System;
namespace DotSDL.Sample.BasicPixels {
internal class Window : SdlWindow {
public Window(int width, int height) : base("Basic Pixels", new Point { X = WindowPosUndefined, Y = WindowPosUndefined }, width, height) {}
public Window(int width, int height) : base("Basic Pixels", new Point { X = WindowPosUndefined, Y = WindowPosUndefined }, width, height) {
Close += Window_Close;
}
private void DrawBackground(ref Color[] pixels) {
byte d = 0;
@ -111,5 +113,9 @@ namespace DotSDL.Sample.BasicPixels {
return color;
}
private void Window_Close(object sender, Events.WindowEvent e) {
Stop();
}
}
}

Loading…
Cancel
Save