mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Implemented Layers and LayerStack
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
using GlitchyEngine.Platform.Windows;
|
||||
using GlitchyEngine.Events;
|
||||
using GlitchyEngine.Platform.Windows;
|
||||
|
||||
namespace GlitchyEngine
|
||||
{
|
||||
@@ -8,6 +8,8 @@ namespace GlitchyEngine
|
||||
private Window _window ~ delete _;
|
||||
private bool _running = true;
|
||||
|
||||
private LayerStack _layerStack = new LayerStack() ~ delete _;
|
||||
|
||||
public bool IsRunning => _running;
|
||||
|
||||
public this()
|
||||
@@ -17,22 +19,34 @@ namespace GlitchyEngine
|
||||
_window.EventCallback = new => OnEvent;
|
||||
}
|
||||
|
||||
public void Run()
|
||||
{
|
||||
while(_running)
|
||||
{
|
||||
_window.Update();
|
||||
}
|
||||
}
|
||||
|
||||
public void OnEvent(Event e)
|
||||
{
|
||||
EventDispatcher dispatcher = scope .(e);
|
||||
dispatcher.Dispatch<WindowCloseEvent>(scope => OnWindowClose);
|
||||
|
||||
Log.EngineLogger.Trace("{}", e);
|
||||
for(Layer layer in _layerStack)
|
||||
{
|
||||
layer.OnEvent(e);
|
||||
if(e.Handled)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void Run()
|
||||
{
|
||||
while(_running)
|
||||
{
|
||||
for(Layer layer in _layerStack)
|
||||
layer.Update();
|
||||
|
||||
_window.Update();
|
||||
}
|
||||
}
|
||||
|
||||
public void PushLayer(Layer ownLayer) => _layerStack.PushLayer(ownLayer);
|
||||
|
||||
public void PushOverlay(Layer ownOverlay) => _layerStack.PushOverlay(ownOverlay);
|
||||
|
||||
public bool OnWindowClose(WindowCloseEvent e)
|
||||
{
|
||||
_running = false;
|
||||
|
||||
@@ -29,6 +29,14 @@ namespace GlitchyEngine.Events
|
||||
{
|
||||
protected bool _handled;
|
||||
|
||||
public bool Handled
|
||||
{
|
||||
[Inline]
|
||||
get => _handled;
|
||||
[Inline]
|
||||
set => _handled = value;
|
||||
}
|
||||
|
||||
public abstract EventType EventType {get;}
|
||||
public abstract StringView Name {get;}
|
||||
public abstract EventCategory Category {get;}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using GlitchyEngine.Events;
|
||||
|
||||
namespace GlitchyEngine
|
||||
{
|
||||
public abstract class Layer
|
||||
{
|
||||
protected String _debugName;
|
||||
|
||||
[Inline]
|
||||
public StringView Name => _debugName;
|
||||
|
||||
[AllowAppend]
|
||||
public this(StringView name = "Layer")
|
||||
{
|
||||
String debugName = append String(name);
|
||||
_debugName = debugName;
|
||||
}
|
||||
|
||||
public virtual void OnAttach() { }
|
||||
public virtual void OnDetach() { }
|
||||
public virtual void Update() { }
|
||||
public virtual void OnEvent(Event event) { }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using System.Collections;
|
||||
|
||||
namespace GlitchyEngine
|
||||
{
|
||||
public class LayerStack
|
||||
{
|
||||
private List<Layer> _layers = new List<Layer>() ~ DeleteContainerAndItems!(_);
|
||||
|
||||
/*
|
||||
* Marks the index of the last layer.
|
||||
* Used to insert new layers before overlays.
|
||||
*/
|
||||
private int _insertIndex = 0;
|
||||
|
||||
/**
|
||||
* Pushes the provided layer onto the layer stack.
|
||||
* @param ownLayer The layer that will be pushed onto the layerstack.
|
||||
* @remarks The layerstack takes ownership of ownLayer.
|
||||
*/
|
||||
public void PushLayer(Layer ownLayer)
|
||||
{
|
||||
_layers.Insert(_insertIndex++, ownLayer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pushes the provided overlay onto the layer stack.
|
||||
* @param ownLayer The overlay that will be pushed onto the layerstack.
|
||||
* @remarks The layerstack takes ownership of ownOverlay.
|
||||
*/
|
||||
public void PushOverlay(Layer ownOverlay)
|
||||
{
|
||||
_layers.Add(ownOverlay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the given overlay from the layer stack.
|
||||
* @param layer The layer to pop from the LayerStack.
|
||||
*/
|
||||
public void PopLayer(Layer layer)
|
||||
{
|
||||
if(_layers.Remove(layer))
|
||||
{
|
||||
_insertIndex--;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the given overlay from the layer stack.
|
||||
* @param overlay The overlay to pop from the LayerStack.
|
||||
*/
|
||||
public void PopOverlay(Layer overlay)
|
||||
{
|
||||
_layers.Remove(overlay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an enumerator for enumerating the layers.
|
||||
*/
|
||||
public List<Layer>.Enumerator GetEnumerator() => _layers.GetEnumerator();
|
||||
}
|
||||
}
|
||||
@@ -348,9 +348,7 @@ namespace GlitchyEngine.Platform.Windows
|
||||
{
|
||||
/* Remove beeping sound when ALT + some key is pressed. */
|
||||
if ( wParam == SC_KEYMENU )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
// Window closing
|
||||
case WM_CLOSE:
|
||||
|
||||
@@ -1,17 +1,44 @@
|
||||
using System;
|
||||
using GlitchyEngine;
|
||||
using GlitchyEngine.Events;
|
||||
using System.Diagnostics;
|
||||
using GlitchLog;
|
||||
|
||||
namespace Sandbox
|
||||
{
|
||||
class ExampleLayer : Layer
|
||||
{
|
||||
[AllowAppend]
|
||||
public this() : base("Example") { }
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
Log.ClientLogger.Info("ExampleLayer.Update");
|
||||
|
||||
// Just for temporary vsyncing
|
||||
// Todo: remove
|
||||
DwmFlush();
|
||||
}
|
||||
|
||||
[CLink, Import("Dwmapi.lib")]
|
||||
static extern void DwmFlush();
|
||||
|
||||
public override void OnEvent(Event event)
|
||||
{
|
||||
Log.ClientLogger.Trace("{}", event);
|
||||
}
|
||||
}
|
||||
|
||||
class SandboxApp : Application
|
||||
{
|
||||
public this()
|
||||
{
|
||||
PushLayer(new ExampleLayer());
|
||||
}
|
||||
|
||||
[Export, LinkName("CreateApplication")]
|
||||
public static Application CreateApplication()
|
||||
{
|
||||
|
||||
|
||||
return new SandboxApp();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user