Implemented Layers and LayerStack

This commit is contained in:
Simon Lübeß
2020-11-09 00:10:02 +01:00
parent aa2165ef77
commit efb46ff25c
6 changed files with 148 additions and 15 deletions
+25 -11
View File
@@ -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;
+8
View File
@@ -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;}
+25
View File
@@ -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) { }
}
}
+61
View File
@@ -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: