mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 21:01:52 +00:00
Added window (de-)activate events
This commit is contained in:
@@ -53,6 +53,9 @@ namespace GlitchyEngine
|
|||||||
|
|
||||||
public void OnEvent(Event e)
|
public void OnEvent(Event e)
|
||||||
{
|
{
|
||||||
|
if(!_running)
|
||||||
|
return;
|
||||||
|
|
||||||
EventDispatcher dispatcher = scope .(e);
|
EventDispatcher dispatcher = scope .(e);
|
||||||
dispatcher.Dispatch<WindowCloseEvent>(scope => OnWindowClose);
|
dispatcher.Dispatch<WindowCloseEvent>(scope => OnWindowClose);
|
||||||
|
|
||||||
|
|||||||
@@ -89,4 +89,44 @@ namespace GlitchyEngine.Events
|
|||||||
strBuffer.AppendF("WindowMovedEvent: {}, {} (is moving: {})", _x, _y, _isMoving);
|
strBuffer.AppendF("WindowMovedEvent: {}, {} (is moving: {})", _x, _y, _isMoving);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class WindowActivateEvent : Event, IEvent
|
||||||
|
{
|
||||||
|
public override EventType EventType => .WindowMoved;
|
||||||
|
|
||||||
|
public override StringView Name => "WindowActivated";
|
||||||
|
|
||||||
|
public override EventCategory Category => .Application;
|
||||||
|
|
||||||
|
public static EventType StaticType => .WindowActivated;
|
||||||
|
|
||||||
|
public this()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void ToString(String strBuffer)
|
||||||
|
{
|
||||||
|
strBuffer.AppendF("WindowActivatedEvent");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class WindowDeactivateEvent : Event, IEvent
|
||||||
|
{
|
||||||
|
public override EventType EventType => .WindowMoved;
|
||||||
|
|
||||||
|
public override StringView Name => "WindowDeactivated";
|
||||||
|
|
||||||
|
public override EventCategory Category => .Application;
|
||||||
|
|
||||||
|
public static EventType StaticType => .WindowDeactivated;
|
||||||
|
|
||||||
|
public this()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void ToString(String strBuffer)
|
||||||
|
{
|
||||||
|
strBuffer.AppendF("WindowDeactivatedEvent");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ namespace GlitchyEngine.Events
|
|||||||
public enum EventType
|
public enum EventType
|
||||||
{
|
{
|
||||||
None = 0,
|
None = 0,
|
||||||
WindowClose, WindowResize, WindowFocus, WindowLostFocus, WindowMoved,
|
WindowClose, WindowResize, WindowFocus, WindowLostFocus, WindowMoved, WindowActivated, WindowDeactivated,
|
||||||
AppTick, AppUpdate, AppRender,
|
AppTick, AppUpdate, AppRender,
|
||||||
KeyPressed, KeyReleased, KeyTyped,
|
KeyPressed, KeyReleased, KeyTyped,
|
||||||
MouseButtonPressed, MouseButtonReleased, MouseMoved, MouseScrolled
|
MouseButtonPressed, MouseButtonReleased, MouseMoved, MouseScrolled
|
||||||
|
|||||||
@@ -20,8 +20,10 @@ namespace GlitchyEngine
|
|||||||
public Point CursorPositionDifference;
|
public Point CursorPositionDifference;
|
||||||
}
|
}
|
||||||
|
|
||||||
static WindowsInputState* CurrentState = new WindowsInputState() ~ delete _;
|
static WindowsInputState[2] IputStates;
|
||||||
static WindowsInputState* LastState = new WindowsInputState() ~ delete _;
|
|
||||||
|
static WindowsInputState* CurrentState = &IputStates[0];
|
||||||
|
static WindowsInputState* LastState = &IputStates[1];
|
||||||
|
|
||||||
//
|
//
|
||||||
// Current Keyboard state
|
// Current Keyboard state
|
||||||
@@ -162,10 +164,7 @@ namespace GlitchyEngine
|
|||||||
|
|
||||||
public override static void NewFrame()
|
public override static void NewFrame()
|
||||||
{
|
{
|
||||||
// Switch last and current states
|
Swap!(CurrentState, LastState);
|
||||||
var buffer = LastState;
|
|
||||||
LastState = CurrentState;
|
|
||||||
CurrentState = buffer;
|
|
||||||
|
|
||||||
// Get current keyboard state
|
// Get current keyboard state
|
||||||
if(DirectX.Windows.Winuser.GetKeyboardState((uint8*)&CurrentState.KeyStates) == 0)
|
if(DirectX.Windows.Winuser.GetKeyboardState((uint8*)&CurrentState.KeyStates) == 0)
|
||||||
|
|||||||
@@ -31,6 +31,8 @@ namespace GlitchyEngine
|
|||||||
|
|
||||||
private bool _isVSync = true;
|
private bool _isVSync = true;
|
||||||
|
|
||||||
|
private bool _isActive;
|
||||||
|
|
||||||
private GraphicsContext _graphicsContext ~ _?.ReleaseRef();
|
private GraphicsContext _graphicsContext ~ _?.ReleaseRef();
|
||||||
|
|
||||||
public override GraphicsContext Context => _graphicsContext;
|
public override GraphicsContext Context => _graphicsContext;
|
||||||
@@ -149,6 +151,8 @@ namespace GlitchyEngine
|
|||||||
set => _isVSync = value;
|
set => _isVSync = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override bool IsActive => _isActive;
|
||||||
|
|
||||||
public override void* NativeWindow => (void*)(int)_windowHandle;
|
public override void* NativeWindow => (void*)(int)_windowHandle;
|
||||||
|
|
||||||
public override this(WindowDescription desc)
|
public override this(WindowDescription desc)
|
||||||
@@ -176,6 +180,9 @@ namespace GlitchyEngine
|
|||||||
UnregisterClass(WindowClassName.ToScopedNativeWChar!(), _instanceHandle);
|
UnregisterClass(WindowClassName.ToScopedNativeWChar!(), _instanceHandle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[LinkName(.C)]
|
||||||
|
static extern HWnd GetActiveWindow();
|
||||||
|
|
||||||
private void Init(WindowDescription desc)
|
private void Init(WindowDescription desc)
|
||||||
{
|
{
|
||||||
Log.EngineLogger.Trace($"Creating window \"{desc.Title}\" ({desc.Width}, {desc.Height})...");
|
Log.EngineLogger.Trace($"Creating window \"{desc.Title}\" ({desc.Width}, {desc.Height})...");
|
||||||
@@ -215,6 +222,9 @@ namespace GlitchyEngine
|
|||||||
LoadWindowRectangle();
|
LoadWindowRectangle();
|
||||||
|
|
||||||
Log.EngineLogger.Trace($"Created window \"{Title}\" ({Width}, {Height})");
|
Log.EngineLogger.Trace($"Created window \"{Title}\" ({Width}, {Height})");
|
||||||
|
|
||||||
|
// Determine whether or not the window is currently active
|
||||||
|
_isActive = GetActiveWindow() == _windowHandle;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool _isResizingOrMoving;
|
private bool _isResizingOrMoving;
|
||||||
@@ -305,6 +315,25 @@ namespace GlitchyEngine
|
|||||||
window._eventCallback(moveEvent);
|
window._eventCallback(moveEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case 0x0006: //WM_ACTIVATE
|
||||||
|
{
|
||||||
|
SplitHighAndLowOrder!((int64)wParam, let lowWParam, let highWParam);
|
||||||
|
|
||||||
|
bool isActivate = lowWParam > 0;
|
||||||
|
|
||||||
|
//HWnd windowHandle = (.)lParam;
|
||||||
|
|
||||||
|
if(window._isActive != isActivate)
|
||||||
|
{
|
||||||
|
window._isActive = isActivate;
|
||||||
|
|
||||||
|
if(window._isActive)
|
||||||
|
window._eventCallback(scope WindowActivateEvent());
|
||||||
|
else
|
||||||
|
window._eventCallback(scope WindowDeactivateEvent());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
////
|
////
|
||||||
//// Keyboard input
|
//// Keyboard input
|
||||||
////
|
////
|
||||||
|
|||||||
@@ -98,6 +98,11 @@ namespace GlitchyEngine
|
|||||||
*/
|
*/
|
||||||
public extern void* NativeWindow {get;}
|
public extern void* NativeWindow {get;}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets whether or not the window is active.
|
||||||
|
*/
|
||||||
|
public extern bool IsActive {get;}
|
||||||
|
|
||||||
public EventCallback EventCallback
|
public EventCallback EventCallback
|
||||||
{
|
{
|
||||||
get => _eventCallback;
|
get => _eventCallback;
|
||||||
|
|||||||
+26
-23
@@ -193,32 +193,35 @@ namespace Sandbox
|
|||||||
|
|
||||||
public override void Update(GameTime gameTime)
|
public override void Update(GameTime gameTime)
|
||||||
{
|
{
|
||||||
Vector2 movement = .();
|
if(Application.Get().Window.IsActive)
|
||||||
|
|
||||||
if(Input.IsKeyPressed(Key.W))
|
|
||||||
{
|
{
|
||||||
movement.Y += 1;
|
Vector2 movement = .();
|
||||||
}
|
|
||||||
if(Input.IsKeyPressed(Key.S))
|
|
||||||
{
|
|
||||||
movement.Y -= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(Input.IsKeyPressed(Key.A))
|
if(Input.IsKeyPressed(Key.W))
|
||||||
{
|
{
|
||||||
movement.X -= 1;
|
movement.Y += 1;
|
||||||
|
}
|
||||||
|
if(Input.IsKeyPressed(Key.S))
|
||||||
|
{
|
||||||
|
movement.Y -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Input.IsKeyPressed(Key.A))
|
||||||
|
{
|
||||||
|
movement.X -= 1;
|
||||||
|
}
|
||||||
|
if(Input.IsKeyPressed(Key.D))
|
||||||
|
{
|
||||||
|
movement.X += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(movement != .Zero)
|
||||||
|
movement.Normalize();
|
||||||
|
|
||||||
|
movement *= (float)(gameTime.FrameTime.TotalSeconds);
|
||||||
|
|
||||||
|
_camera.Position += .(movement, 0);
|
||||||
}
|
}
|
||||||
if(Input.IsKeyPressed(Key.D))
|
|
||||||
{
|
|
||||||
movement.X += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(movement != .Zero)
|
|
||||||
movement.Normalize();
|
|
||||||
|
|
||||||
movement *= (float)(gameTime.FrameTime.TotalSeconds);
|
|
||||||
|
|
||||||
_camera.Position += .(movement, 0);
|
|
||||||
|
|
||||||
_camera.Width = _context.SwapChain.BackbufferViewport.Width / 256;
|
_camera.Width = _context.SwapChain.BackbufferViewport.Width / 256;
|
||||||
_camera.Height = _context.SwapChain.BackbufferViewport.Height / 256;
|
_camera.Height = _context.SwapChain.BackbufferViewport.Height / 256;
|
||||||
|
|||||||
Reference in New Issue
Block a user