diff --git a/GlitchyEngine/src/Events/ApplicationEvent.bf b/GlitchyEngine/src/Events/ApplicationEvent.bf new file mode 100644 index 0000000..00007ad --- /dev/null +++ b/GlitchyEngine/src/Events/ApplicationEvent.bf @@ -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); + } + } +} diff --git a/GlitchyEngine/src/Events/Event.bf b/GlitchyEngine/src/Events/Event.bf new file mode 100644 index 0000000..490960d --- /dev/null +++ b/GlitchyEngine/src/Events/Event.bf @@ -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 = function bool(T); + + private delegate bool EventFunction(T event) where T : Event; + + public this(Event event) + { + _event = event; + } + + public bool Dispatch(EventFunction eventFunction) where T : Event, IEvent + { + if(_event.EventType == T.StaticType) + { + _event.[Friend]_handled = eventFunction((T)_event); + return true; + } + return false; + } + } +} diff --git a/GlitchyEngine/src/Events/KeyEvent.bf b/GlitchyEngine/src/Events/KeyEvent.bf new file mode 100644 index 0000000..8cf4706 --- /dev/null +++ b/GlitchyEngine/src/Events/KeyEvent.bf @@ -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); + } + } +} diff --git a/GlitchyEngine/src/Events/MouseEvent.bf b/GlitchyEngine/src/Events/MouseEvent.bf new file mode 100644 index 0000000..493eaa1 --- /dev/null +++ b/GlitchyEngine/src/Events/MouseEvent.bf @@ -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); + } + } +}