Added Event types

This commit is contained in:
Simon Lübeß
2020-11-08 18:20:50 +01:00
parent 6de44d40b3
commit 30c898185d
4 changed files with 331 additions and 0 deletions
@@ -0,0 +1,92 @@
using System;
namespace GlitchyEngine.Events
{
public class WindowCloseEvent : Event, IEvent
{
public override EventType EventType => .WindowClose;
public override System.StringView Name => "WindowClose";
public override EventCategory Category => .Application;
public static EventType StaticType => .WindowClose;
public this() { }
public override void ToString(String strBuffer)
{
strBuffer.Append("WindowCloseEvent");
}
}
public class WindowResizeEvent : Event, IEvent
{
public int32 _width, _height;
public bool _isResizing;
/// The new width of the window.
public int32 Width = _width;
/// The new height of the window.
public int32 Height = _height;
/*
* If true, indicates that the resizing is not finished.
*/
public bool IsResizing = _isResizing;
public override EventType EventType => .WindowResize;
public override StringView Name => "WindowResize";
public override EventCategory Category => .Application;
public static EventType StaticType => .WindowResize;
public this(int32 width, int32 height, bool isResizing)
{
_width = width;
_height = height;
_isResizing = isResizing;
}
public override void ToString(String strBuffer)
{
strBuffer.AppendF("WindowResizeEvent: {}, {} (is resizing: {})", _width, _height, _isResizing);
}
}
public class WindowMoveEvent : Event, IEvent
{
public int32 _x, _y;
public bool _isMoving;
/// The new x-coordinate of the window.
public int32 X = _x;
/// The new y-coordinate of the window.
public int32 Y = _y;
/*
* If true, indicates that the moving is not finished.
*/
public bool IsMoving = _isMoving;
public override EventType EventType => .WindowMoved;
public override StringView Name => "WindowMoved";
public override EventCategory Category => .Application;
public static EventType StaticType => .WindowMoved;
public this(int32 x, int32 y, bool isMoving)
{
_x = x;
_y = y;
_isMoving = isMoving;
}
public override void ToString(String strBuffer)
{
strBuffer.AppendF("WindowMovedEvent: {}, {} (is moving: {})", _x, _y, _isMoving);
}
}
}
+68
View File
@@ -0,0 +1,68 @@
using System;
namespace GlitchyEngine.Events
{
public enum EventType
{
None = 0,
WindowClose, WindowResize, WindowFocus, WindowLostFocus, WindowMoved,
AppTick, AppUpdate, AppRender,
KeyPressed, KeyReleased,
MouseButtonPressed, MouseButtonReleased, MouseMoved, MouseScrolled
}
public enum EventCategory
{
None = 0,
Application = 1 << 0,
Input = 1 << 1,
Keyboard = 1 << 2,
Mouse = 1 << 3,
MouseButton = 1 << 4
}
public interface IEvent
{
static EventType StaticType {get;}
}
public abstract class Event
{
protected bool _handled;
public abstract EventType EventType {get;}
public abstract StringView Name {get;}
public abstract EventCategory Category {get;}
[Inline]
public bool IsInCategory(EventCategory category) => (Category & category) > 0;
public override void ToString(String strBuffer)
{
strBuffer.Append(Name);
}
}
public class EventDispatcher
{
private Event _event;
typealias EventFn<T> = function bool(T);
private delegate bool EventFunction<T>(T event) where T : Event;
public this(Event event)
{
_event = event;
}
public bool Dispatch<T>(EventFunction<T> eventFunction) where T : Event, IEvent
{
if(_event.EventType == T.StaticType)
{
_event.[Friend]_handled = eventFunction((T)_event);
return true;
}
return false;
}
}
}
+56
View File
@@ -0,0 +1,56 @@
using System;
namespace GlitchyEngine.Events
{
public abstract class KeyEvent : Event
{
protected int32 _keyCode;
[Inline]
public int32 KeyCode => _keyCode;
public override EventCategory Category => .Input | .Keyboard
protected this(int32 keyCode)
{
_keyCode = keyCode;
}
}
public class KeyPressedEvent : KeyEvent, IEvent
{
private int32 _repeatCount;
public int32 RepeatCount => _repeatCount;
public override EventType EventType => .KeyPressed;
public static EventType StaticType => .KeyPressed;
public override StringView Name => "KeyPressed";
public this(int32 keyCode, int32 repeatCount) : base(keyCode)
{
_repeatCount = repeatCount;
}
public override void ToString(String strBuffer)
{
strBuffer.AppendF("KeyPressedEvent: {} ({} repeats)", _keyCode, _repeatCount);
}
}
public class KeyReleasedEvent : KeyEvent, IEvent
{
public override EventType EventType => .KeyReleased;
public static EventType StaticType => .KeyReleased;
public override StringView Name => "KeyReleased";
public this(int32 keyCode) : base(keyCode) { }
public override void ToString(String strBuffer)
{
strBuffer.AppendF("KeyReleasedEvent: {}", _keyCode);
}
}
}
+115
View File
@@ -0,0 +1,115 @@
using System;
namespace GlitchyEngine.Events
{
public class MouseMovedEvent : Event, IEvent
{
private int32 _mouseX, _mouseY;
public override EventType EventType => .MouseMoved;
public override StringView Name => "MouseMoved";
public override EventCategory Category => .Input | .Mouse;
public static EventType StaticType => .MouseMoved;
public int32 PositionX => _mouseX;
public int32 PositionY => _mouseY;
public this(int32 x, int32 y)
{
_mouseX = x;
_mouseY = y;
}
public override void ToString(String strBuffer)
{
strBuffer.AppendF("MouseMovedEvent: Position: ({}, {})", _mouseX, _mouseY);
}
}
public class MouseScrolledEvent : Event, IEvent
{
private int32 _xOffset, _yOffset;
public override EventType EventType => .MouseScrolled;
public override StringView Name => "MouseScrolled";
public override EventCategory Category => .Input | .Mouse;
public static EventType StaticType => .MouseScrolled;
public int32 XOffset => _xOffset;
public int32 YOffset => _yOffset;
public this(int32 xOffset, int32 yOffset)
{
_xOffset = xOffset;
_yOffset = yOffset;
}
public override void ToString(String strBuffer)
{
strBuffer.AppendF("MouseScrolledEvent: {}, {}", _xOffset, _yOffset);
}
}
public enum MouseButton
{
None = 0,
LeftButton,
RightButton,
MiddleButton,
XButton1,
XButton2,
}
public abstract class MouseButtonEvent : Event
{
protected MouseButton _mouseButton;
public override EventCategory Category => .Input | .Mouse;
public MouseButton MouseButton => _mouseButton;
protected this(MouseButton mouseButton)
{
_mouseButton = mouseButton;
}
}
public class MouseButtonPressedEvent : MouseButtonEvent, IEvent
{
public override EventType EventType => .MouseButtonPressed;
public override StringView Name => "MouseButtonPressed";
public static EventType StaticType => .MouseButtonPressed;
public this(MouseButton mouseButton) : base(mouseButton) { }
public override void ToString(String strBuffer)
{
strBuffer.AppendF("MouseButtonPressedEvent: {}", _mouseButton);
}
}
public class MouseButtonReleasedEvent : MouseButtonEvent, IEvent
{
public override EventType EventType => .MouseButtonReleased;
public override StringView Name => "MouseButtonReleased";
public static EventType StaticType => .MouseButtonReleased;
public this(MouseButton mouseButton) : base(mouseButton) { }
public override void ToString(String strBuffer)
{
strBuffer.AppendF("MouseButtonReleasedEvent: {}", _mouseButton);
}
}
}