Loads and loads of small improvements I added while developing on my game

This commit is contained in:
Michel Fedde 2021-03-02 19:54:19 +01:00
parent 41421b1df9
commit a7c71e7ea1
107 changed files with 2278 additions and 1023 deletions

View file

@ -1,6 +1,7 @@
#region usings
using OpenTK;
using SM.Base.Windows;
#endregion
@ -11,48 +12,44 @@ namespace SM.Base.Scene
/// </summary>
public abstract class GenericCamera
{
/// <summary>
/// The matrix for the orthographic world.
/// </summary>
public static Matrix4 OrthographicWorld { get; protected set; }
/// <summary>
/// The matrix for the perspective world.
/// </summary>
public static Matrix4 PerspectiveWorld { get; protected set; }
/// <summary>
/// This defines what is up. (Normalized)
/// <para>Default: <see cref="Vector3.UnitY" /></para>
/// </summary>
public static Vector3 UpVector { get; set; } = Vector3.UnitY;
public Vector3 UpVector { get; set; } = Vector3.UnitY;
/// <summary>
/// Returns the world matrix that is connected to this camera.
/// </summary>
public Matrix4 World { get; protected set; }
/// <summary>
/// Contains the view matrix of this camera.
/// <para>Default: <see cref="Matrix4.Identity" /></para>
/// </summary>
public Matrix4 ViewMatrix { get; protected set; } = Matrix4.Identity;
/// <summary>
/// Returns the world matrix that is connected to this camera.
/// </summary>
public Matrix4 World => Orthographic ? OrthographicWorld : PerspectiveWorld;
public Matrix4 View { get; protected set; } = Matrix4.Identity;
/// <summary>
/// Represents if the camera is orthographic.
/// </summary>
public abstract bool Orthographic { get; }
/// <summary>
/// Exposure defines the exposure to the Scene.
/// </summary>
public float Exposure = 1;
/// <summary>
/// Calculates the view matrix.
/// </summary>
/// <returns>The calculated view matrix. Same as <see cref="ViewMatrix" /></returns>
internal Matrix4 CalculateViewMatrix()
/// <returns>The calculated view matrix. Same as <see cref="View" /></returns>
internal void CalculateViewMatrix(IGenericWindow window)
{
ViewMatrix = ViewCalculation();
return ViewMatrix;
View = ViewCalculation(window);
if (WorldCalculation(window, out Matrix4 world))
{
World = world;
}
}
/// <summary>
@ -60,16 +57,10 @@ namespace SM.Base.Scene
/// </summary>
/// <returns>
/// The new view matrix. This is the returns for <see cref="CalculateViewMatrix" /> and the next value for
/// <see cref="ViewMatrix" />.
/// <see cref="View" />.
/// </returns>
protected abstract Matrix4 ViewCalculation();
protected abstract Matrix4 ViewCalculation(IGenericWindow window);
/// <summary>
/// This will calculate the world.
/// <para>This is called on <see cref="GenericWindow{TScene,TCamera}.ViewportCamera" /> to calculate the world.</para>
/// </summary>
/// <param name="world">The world scale</param>
/// <param name="aspect">The aspect ratio from the window.</param>
public abstract void RecalculateWorld(Vector2 world, float aspect);
protected abstract bool WorldCalculation(IGenericWindow window, out Matrix4 world);
}
}

View file

@ -2,8 +2,10 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using SM.Base.Contexts;
using OpenTK;
using SM.Base;
using SM.Base.Drawing;
using SM.Base.Windows;
#endregion
@ -12,9 +14,7 @@ namespace SM.Base.Scene
/// <summary>
/// Contains a list of show items.
/// </summary>
/// <typeparam name="TItem">The type of show items.</typeparam>
public abstract class GenericItemCollection<TItem> : List<TItem>, IShowItem, IShowCollection<TItem>, IScriptable
where TItem : IShowItem
public abstract class GenericItemCollection : List<IShowItem>, IShowItem, IShowCollection, IScriptable
{
private List<IScriptable> _scriptableObjects = new List<IScriptable>();
@ -23,7 +23,7 @@ namespace SM.Base.Scene
/// </summary>
public ReadOnlyCollection<IScriptable> ScriptableObjects => new ReadOnlyCollection<IScriptable>(_scriptableObjects);
/// <inheritdoc />
public List<TItem> Objects => this;
public List<IShowItem> Objects => this;
/// <inheritdoc />
public object Parent { get; set; }
@ -34,20 +34,32 @@ namespace SM.Base.Scene
/// <inheritdoc />
public ICollection<string> Flags { get; set; } = new List<string>() {"collection"};
public bool Active { get; set; } = true;
public bool UpdateActive { get; set; } = true;
public bool RenderActive { get; set; } = true;
/// <inheritdoc />
public virtual void Update(UpdateContext context)
{
if (!Active || !UpdateActive) return;
for (var i = 0; i < _scriptableObjects.Count; i++)
{
if (!_scriptableObjects[i].Active) continue;
_scriptableObjects[i].Update(context);
}
}
/// <inheritdoc cref="IShowCollection{TItem}.Draw" />
/// <inheritdoc cref="IShowCollection.Draw" />
public virtual void Draw(DrawContext context)
{
context.LastPassthough = this;
if (!Active || !RenderActive) return;
for (var i = 0; i < Objects.Count; i++)
{
if (!this[i].Active) continue;
this[i].Draw(context);
}
}
/// <inheritdoc />
@ -63,20 +75,22 @@ namespace SM.Base.Scene
/// <summary>
/// Adds a item to the draw and the script collection, when applicable.
/// </summary>
public new void Add(TItem item)
public new void Add(params IShowItem[] items)
{
AddObject(item);
if (item is IScriptable scriptable)
AddScript(scriptable);
foreach (var item in items)
{
AddObject(item);
if (item is IScriptable scriptable)
AddScript(scriptable);
}
}
/// <summary>
/// Adds the object to the collection.
/// </summary>
/// <param name="item"></param>
public void AddObject(TItem item)
public void AddObject(IShowItem item)
{
base.Add(item);
item.Parent = this;
@ -91,23 +105,22 @@ namespace SM.Base.Scene
_scriptableObjects.Add(item);
}
/// <summary>
/// Removes a item from the draw and script collection, when applicable.
/// </summary>
/// <param name="item"></param>
public new void Remove(TItem item)
public new void Remove(params IShowItem[] items)
{
RemoveObject(item);
foreach (var item in items)
{
RemoveObject(item);
if (item.GetType().IsAssignableFrom(typeof(IScriptable)))
RemoveScript((IScriptable)item);
if (item is IScriptable scriptable)
RemoveScript(scriptable);
}
}
/// <summary>
/// Remove the object from the draw collection.
/// </summary>
/// <param name="item"></param>
public void RemoveObject(TItem item)
public void RemoveObject(IShowItem item)
{
base.Remove(item);
item.Parent = null;
@ -123,15 +136,27 @@ namespace SM.Base.Scene
_scriptableObjects.Remove(item);
}
public ICollection<IShowItem> GetAllItems(bool includeCollections = false)
{
List<IShowItem> items = new List<IShowItem>();
for (var i = 0; i < this.Count; i++)
{
if (!includeCollections && this[i] is IShowCollection) continue;
items.Add(this[i]);
}
return items;
}
/// <summary>
/// Returns a object with this name or the default, if not available.
/// <para>Not reclusive.</para>
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public TItem GetItemByName(string name)
public IShowItem GetItemByName(string name)
{
TItem obj = default;
IShowItem obj = default;
for (var i = 0; i < Count; i++)
if (this[i].Name == name)
{
@ -148,7 +173,7 @@ namespace SM.Base.Scene
/// </summary>
/// <typeparam name="TGetItem">Type of return</typeparam>
public TGetItem GetItemByName<TGetItem>(string name)
where TGetItem : TItem
where TGetItem : IShowItem
{
return (TGetItem) GetItemByName(name);
}
@ -157,9 +182,9 @@ namespace SM.Base.Scene
/// Returns all object that have this flag.
/// <para>Only in this list.</para>
/// </summary>
public ICollection<TItem> GetItemsWithFlag(string flag)
public ICollection<IShowItem> GetItemsWithFlag(string flag)
{
var list = new List<TItem>();
var list = new List<IShowItem>();
for (var i = 0; i < Count; i++)
{
var obj = this[i];
@ -176,19 +201,19 @@ namespace SM.Base.Scene
/// </summary>
/// <typeparam name="TItem">The type of show items.</typeparam>
/// <typeparam name="TTransformation">The type of transformation.</typeparam>
public abstract class GenericItemCollection<TItem, TTransformation> : GenericItemCollection<TItem>
where TItem : IShowItem
public abstract class GenericItemCollection<TTransformation> : GenericItemCollection, IShowTransformItem<TTransformation>
where TTransformation : GenericTransformation, new()
{
/// <summary>
/// Transformation of the collection
/// </summary>
public TTransformation Transform = new TTransformation();
public TTransformation Transform { get; set; } = new TTransformation();
/// <inheritdoc />
public override void Draw(DrawContext context)
{
context.ModelMaster = Transform.GetMatrix() * context.ModelMaster;
Transform.LastMaster = context.ModelMatrix;
context.ModelMatrix = Transform.MergeMatrix(context.ModelMatrix);
base.Draw(context);
}

View file

@ -3,8 +3,11 @@
using System;
using System.Collections.Generic;
using System.Dynamic;
using SM.Base.Contexts;
using System.Windows.Controls;
using SM.Base;
using SM.Base.Drawing;
using SM.Base.Windows;
using SM.Utility;
#endregion
@ -13,8 +16,11 @@ namespace SM.Base.Scene
/// <summary>
/// A generic scene, that imports functions for scene control.
/// </summary>
public abstract class GenericScene
public abstract class GenericScene : IInitializable
{
private GenericItemCollection _hud;
private GenericItemCollection _objectCollection;
private IBackgroundItem _background;
private Dictionary<Type, object> _extensions = new Dictionary<Type, object>();
@ -32,11 +38,31 @@ namespace SM.Base.Scene
}
/// <summary>
/// The active camera, that is used if the context doesn't force the viewport camera.
/// <para>If none set, it automaticly uses the viewport camera.</para>
/// Objects inside the scene.
/// </summary>
internal GenericCamera _camera { get; set; }
public GenericItemCollection Objects
{
get => _objectCollection;
set
{
value.Parent = this;
_objectCollection = value;
}
}
/// <summary>
/// This defines the HUD objects.
/// </summary>
public GenericItemCollection HUD
{
get => _hud;
set
{
value.Parent = this;
_hud = value;
}
}
/// <summary>
/// A collection for cameras to switch easier to different cameras.
/// </summary>
@ -45,15 +71,38 @@ namespace SM.Base.Scene
/// <summary>
/// If true, the scene was already initialized.
/// </summary>
public bool IsInitialized { get; private set; }
public bool IsInitialized { get; set; }
/// <summary>
/// If true, shows a axis helper at (0,0,0)
/// </summary>
public bool ShowAxisHelper { get; set; } = false;
/// <summary>
/// The active camera, that is used if the context doesn't force the viewport camera.
/// <para>If none set, it automaticly uses the viewport camera.</para>
/// </summary>
public GenericCamera Camera { get; set; }
/// <summary>
/// A camera to control the background.
/// </summary>
public GenericCamera BackgroundCamera { get; set; }
/// <summary>
/// A camera to control the HUD.
/// </summary>
public GenericCamera HUDCamera { get; set; }
/// <summary>
/// Updates this scene.
/// </summary>
/// <param name="context"></param>
public virtual void Update(UpdateContext context)
{
_objectCollection?.Update(context);
_hud?.Update(context);
}
/// <summary>
@ -61,15 +110,61 @@ namespace SM.Base.Scene
/// </summary>
public virtual void Draw(DrawContext context)
{
DrawBackground(context);
DrawMainObjects(context);
DrawHUD(context);
DrawDebug(context);
}
/// <summary>
/// Draws only the background.
/// </summary>
/// <param name="context"></param>
public void DrawBackground(DrawContext context)
{
var backgroundDrawContext = context;
backgroundDrawContext.SetCamera(BackgroundCamera);
_Background?.Draw(backgroundDrawContext);
}
/// <summary>
/// Draws only the main objects
/// </summary>
/// <param name="context"></param>
public void DrawMainObjects(DrawContext context)
{
if (!context.Window.ForceViewportCamera && Camera != null) context.SetCamera(Camera);
_objectCollection.Draw(context);
}
/// <summary>
/// Draws only the HUD
/// </summary>
/// <param name="context"></param>
public void DrawHUD(DrawContext context)
{
context.SetCamera(HUDCamera);
_hud?.Draw(context);
}
/// <summary>
/// Draw the debug informations.
/// </summary>
/// <param name="context"></param>
public virtual void DrawDebug(DrawContext context)
{
}
/// <summary>
/// Adds a extension to the scene.
/// </summary>
/// <param name="extension"></param>
public virtual void SetExtension(object extension)
{
_extensions[extension.GetType()] = extension;
_extensions[extension.GetType()] = extension;
}
/// <summary>
@ -89,32 +184,19 @@ namespace SM.Base.Scene
return (T)ext;
}
/// <summary>
/// Called, when the user activates the scene.
/// </summary>
internal void Activate()
{
if (!IsInitialized)
{
OnInitialization();
IsInitialized = true;
}
OnActivating();
public virtual void Activate()
{
}
/// <summary>
/// Called, when the user activates the scene for the first time.
/// </summary>
protected virtual void OnInitialization()
{ }
public virtual void Initialization()
{
/// <summary>
/// Called, when the user activates the scene.
/// </summary>
protected virtual void OnActivating()
{ }
}
public virtual void Deactivate() {}
}
/// <summary>
@ -123,117 +205,43 @@ namespace SM.Base.Scene
/// <typeparam name="TCamera">The type of cameras.</typeparam>
/// <typeparam name="TItem">The type of show items.</typeparam>
/// <typeparam name="TCollection">The type for collections</typeparam>
public abstract class GenericScene<TCamera, TCollection, TItem> : GenericScene
public abstract class GenericScene<TCamera, TCollection> : GenericScene
where TCamera : GenericCamera, new()
where TCollection : GenericItemCollection<TItem>, new()
where TItem : IShowItem
where TCollection : GenericItemCollection, new()
{
private TCollection _hud = new TCollection();
private TCollection _objectCollection = new TCollection();
/// <summary>
/// If true, shows a axis helper at (0,0,0)
/// </summary>
public bool ShowAxisHelper { get; set; } = false;
/// <summary>
/// The active camera, that is used if the context doesn't force the viewport camera.
/// <para>If none set, it automaticly uses the viewport camera.</para>
/// </summary>
public TCamera Camera => (TCamera) _camera;
/// <summary>
/// A camera to control the background.
/// </summary>
public TCamera BackgroundCamera { get; set; } = new TCamera();
/// <summary>
/// A camera to control the HUD.
/// </summary>
public TCamera HUDCamera { get; set; } = new TCamera();
/// <summary>
/// Objects inside the scene.
/// </summary>
public TCollection Objects
public new TCollection Objects
{
get => _objectCollection;
set
get => (TCollection) base.Objects;
set => base.Objects = value;
}
public new TCollection HUD
{
get
{
value.Parent = this;
_objectCollection = value;
base.HUD ??= new TCollection();
return (TCollection) base.HUD;
}
set => base.HUD = value;
}
/// <summary>
/// This defines the HUD objects.
/// </summary>
public TCollection HUD
public new TCamera Camera
{
get => _hud;
set
{
value.Parent = this;
_hud = value;
}
get => (TCamera) base.Camera;
set => base.Camera = value;
}
/// <inheritdoc />
public override void Update(UpdateContext context)
public new TCamera HUDCamera
{
_objectCollection.Update(context);
_hud.Update(context);
get => (TCamera) base.HUDCamera;
set => base.HUDCamera = value;
}
/// <inheritdoc />
public override void Draw(DrawContext context)
public new TCamera BackgroundCamera
{
DrawBackground(context);
DrawMainObjects(context);
DrawHUD(context);
DrawDebug(context);
get => (TCamera) base.BackgroundCamera;
set => base.BackgroundCamera = value;
}
/// <summary>
/// Draws only the background.
/// </summary>
/// <param name="context"></param>
public void DrawBackground(DrawContext context)
{
var backgroundDrawContext = context;
backgroundDrawContext.View = BackgroundCamera.CalculateViewMatrix();
_Background?.Draw(backgroundDrawContext);
}
/// <summary>
/// Draws only the main objects
/// </summary>
/// <param name="context"></param>
public void DrawMainObjects(DrawContext context)
{
if (!context.ForceViewport && Camera != null) context.View = Camera.CalculateViewMatrix();
_objectCollection.Draw(context);
}
/// <summary>
/// Draws only the HUD
/// </summary>
/// <param name="context"></param>
public void DrawHUD(DrawContext context)
{
context.View = HUDCamera.CalculateViewMatrix();
_hud.Draw(context);
}
/// <summary>
/// Draw the debug informations.
/// </summary>
/// <param name="context"></param>
public virtual void DrawDebug(DrawContext context)
{
}
}
}

View file

@ -1,4 +1,5 @@
using SM.Base.Contexts;
using SM.Base;
using SM.Base.Windows;
namespace SM.Base.Scene
{
@ -7,6 +8,8 @@ namespace SM.Base.Scene
/// </summary>
public interface IScriptable
{
bool Active { get; set; }
/// <summary>
/// Updates the object.
/// </summary>

View file

@ -1,7 +1,8 @@
#region usings
using System.Collections.Generic;
using SM.Base.Contexts;
using SM.Base;
using SM.Base.Windows;
#endregion
@ -11,12 +12,12 @@ namespace SM.Base.Scene
/// Adds functions, that is required for a collection.
/// </summary>
/// <typeparam name="TItem">The type of show item.</typeparam>
public interface IShowCollection<TItem> where TItem : IShowItem
public interface IShowCollection
{
/// <summary>
/// The object collection.
/// </summary>
List<TItem> Objects { get; }
List<IShowItem> Objects { get; }
/// <summary>
/// This draws the objects in the <see cref="Objects" /> list.

View file

@ -1,7 +1,10 @@
#region usings
using System.Collections.Generic;
using SM.Base.Contexts;
using SM.Base;
using SM.Base.Drawing;
using SM.Base.Windows;
using SM.OGL.Mesh;
#endregion
@ -26,6 +29,8 @@ namespace SM.Base.Scene
/// Contains specific flags for the object.
/// </summary>
ICollection<string> Flags { get; set; }
bool Active { get; set; }
/// <summary>
/// Tells the object to draw its object.
@ -43,4 +48,19 @@ namespace SM.Base.Scene
/// </summary>
void OnRemoved(object sender);
}
public interface ITransformItem<TTransform>
where TTransform : GenericTransformation
{
TTransform Transform { get; set; }
}
public interface IShowTransformItem<TTransform> : IShowItem, ITransformItem<TTransform>
where TTransform : GenericTransformation
{}
public interface IModelItem
{
GenericMesh Mesh { get; set; }
}
}