Skip to content

Commit

Permalink
Merge pull request #40 from exomia/development
Browse files Browse the repository at this point in the history
ref v1.5.2
  • Loading branch information
baetz-daniel authored Feb 24, 2021
2 parents 220f2bf + 2b9245e commit 77b3cee
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/Exomia.Framework/Scene/SceneBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System;
using System.Collections.Generic;
using Exomia.Framework.Game;
using Exomia.Framework.Input;
using SharpDX;

namespace Exomia.Framework.Scene
Expand All @@ -26,7 +27,7 @@ public abstract class SceneBase : ISceneInternal
/// Occurs when Scene State Changed.
/// </summary>
public event EventHandler<SceneBase, SceneState>? SceneStateChanged;

private readonly List<IContentable> _contentableComponent;
private readonly List<IContentable> _currentlyContentableComponent;
private readonly List<IDrawable> _currentlyDrawableComponent;
Expand Down Expand Up @@ -101,8 +102,8 @@ ISceneManager ISceneInternal.SceneManager
/// <param name="key"> The key. </param>
protected SceneBase(string key)
{
_key = key;

_key = key ?? throw new ArgumentNullException(nameof(key));
_sceneComponents = new Dictionary<string, IComponent>(INITIAL_QUEUE_SIZE);
_pendingInitializables = new List<IInitializable>(INITIAL_QUEUE_SIZE);
_updateableComponent = new List<IUpdateable>(INITIAL_QUEUE_SIZE);
Expand All @@ -111,8 +112,7 @@ protected SceneBase(string key)
_currentlyUpdateableComponent = new List<IUpdateable>(INITIAL_QUEUE_SIZE);
_currentlyDrawableComponent = new List<IDrawable>(INITIAL_QUEUE_SIZE);
_currentlyContentableComponent = new List<IContentable>(INITIAL_QUEUE_SIZE);

_collector = new DisposeCollector();
_collector = new DisposeCollector();
}

/// <inheritdoc />
Expand Down
144 changes: 135 additions & 9 deletions src/Exomia.Framework/UI/UiManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,42 @@
#endregion

using System;
using System.Runtime.CompilerServices;
using System.Threading;
using Exomia.Framework.Game;
using Exomia.Framework.Graphics;
using Exomia.Framework.Input;
using Exomia.Framework.UI.Controls;
using SharpDX;

namespace Exomia.Framework.UI
{
/// <summary>
/// A ui manager. This class cannot be inherited.
/// </summary>
public sealed class UiManager : Renderer, IInputHandler
public sealed class UiManager : IComponent, IInitializable, IDrawable, IDisposable, IInputHandler
{
internal const int INITIAL_LIST_SIZE = 8;

/// <summary>
/// Occurs when the <see cref="DrawOrder" /> property changes.
/// </summary>
public event EventHandler? DrawOrderChanged;

/// <summary>
/// Occurs when the <see cref="Visible" /> property changes.
/// </summary>
public event EventHandler? VisibleChanged;

/// <summary>
/// Flag to identify, if the component is already initialized.
/// </summary>
private bool _isInitialized;

private readonly DisposeCollector _collector;
private int _drawOrder;
private bool _visible;

private Control[] _controls;
private Control[] _currentlyControls;
private int _controlCount;
Expand All @@ -37,13 +58,57 @@ public sealed class UiManager : Renderer, IInputHandler
private Control? _focusedControl;
private Control? _enteredControl;

/// <inheritdoc />
public int DrawOrder
{
get { return _drawOrder; }
set
{
if (_drawOrder != value)
{
_drawOrder = value;
DrawOrderChanged?.Invoke();
}
}
}

/// <inheritdoc />
public string Name { get; }

/// <inheritdoc />
public bool Visible
{
get { return _visible; }
set
{
if (_visible != value)
{
_visible = value;
VisibleChanged?.Invoke();
}
}
}

/// <summary>
/// Gets the input handler.
/// </summary>
/// <value>
/// The input handler.
/// </value>
public IInputHandler InputHandler
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return this; }
}

/// <summary>
/// Initializes a new instance of the <see cref="UiManager" /> class.
/// </summary>
/// <param name="name"> The name. </param>
public UiManager(string name)
: base(name)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
_collector = new DisposeCollector();
_controls = new Control[INITIAL_LIST_SIZE];
_currentlyControls = new Control[INITIAL_LIST_SIZE];
}
Expand All @@ -69,9 +134,13 @@ void IInputHandler.UnregisterInput(IInputDevice device)
device.UnregisterKeyUp(KeyUp);
device.UnregisterKeyPress(KeyPress);
}

/// <inheritdoc />
public override void Draw(GameTime gameTime)

bool IDrawable.BeginDraw()
{
return _visible;
}

void IDrawable.Draw(GameTime gameTime)
{
if (_isDirty)
{
Expand Down Expand Up @@ -104,12 +173,15 @@ public override void Draw(GameTime gameTime)

_canvas.End();
}

void IDrawable.EndDraw() { }

/// <summary>
/// Adds the <paramref name="control" /> to this ui manger.
/// </summary>
/// <param name="control"> The control to add. </param>
public void Add(Control control)
/// <returns>The <paramref name="control"/></returns>
public Control Add(Control control)
{
if (control.GetUiManager() != null || control._parent != null)
{
Expand All @@ -128,14 +200,18 @@ public void Add(Control control)
}

_isDirty = true;

return _collector.Collect(control);
}

/// <summary>
/// Removes the given <paramref name="control" /> from this ui manager.
/// </summary>
/// <param name="control"> The control to remove. </param>
/// <param name="dispose"> True to dispose the control after removing. </param>
/// <returns>The <paramref name="control"/></returns>
/// <exception cref="InvalidOperationException"> Thrown when the requested operation is invalid. </exception>
public void Remove(Control control)
public Control Remove(Control control, bool dispose = false)
{
if (control._parent != null)
{
Expand All @@ -144,6 +220,15 @@ public void Remove(Control control)
}

RemoveAt(control._uiListIndex);

_collector.Remove(control);

if (dispose)
{
control.Dispose();
}

return control;
}

/// <summary>
Expand Down Expand Up @@ -181,9 +266,13 @@ public void Clear()
}

/// <inheritdoc />
protected override void OnInitialize(IServiceRegistry registry)
void IInitializable.Initialize(IServiceRegistry registry)
{
_canvas = new Canvas(registry.GetService<IGraphicsDevice>());
if (!_isInitialized)
{
_canvas = new Canvas(registry.GetService<IGraphicsDevice>());
_isInitialized = true;
}
}

internal void SetFocusedControl(Control control, bool focus)
Expand Down Expand Up @@ -303,5 +392,42 @@ private EventAction MouseUp(in MouseEventArgs e)

return EventAction.Continue;
}

#region IDisposable Support

private bool _disposed;

/// <inheritdoc />
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting
/// unmanaged/managed resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting
/// unmanaged/managed resources.
/// </summary>
/// <param name="disposing"> true if user code; false called by finalizer. </param>
private void Dispose(bool disposing)
{
if (!_disposed)
{
_collector.DisposeAndClear(disposing);
_disposed = true;
}
}

/// <inheritdoc />
~UiManager()
{
Dispose(false);
}

#endregion
}
}

0 comments on commit 77b3cee

Please sign in to comment.