#region usings
using System;
using System.Collections.Generic;
using System.Dynamic;
using SM.Base.Contexts;
using SM.Base.Drawing;
#endregion
namespace SM.Base.Scene
{
///
/// A generic scene, that imports functions for scene control.
///
public abstract class GenericScene
{
private IBackgroundItem _background;
private Dictionary _extensions = new Dictionary();
///
/// This contains the background.
///
protected IBackgroundItem _Background
{
get => _background;
set
{
value.Parent = this;
_background = value;
}
}
///
/// If true, the scene was already initialized.
///
public bool IsInitialized { get; private set; }
///
/// Updates this scene.
///
///
public virtual void Update(UpdateContext context)
{
}
///
/// Draws this scene.
///
public virtual void Draw(DrawContext context)
{
}
///
/// Adds a extension to the scene.
///
///
public virtual void SetExtension(object extension)
{
_extensions[extension.GetType()] = extension;
}
///
/// Gets a extension with the type.
///
///
///
///
public virtual T GetExtension()
{
object ext = _extensions[typeof(T)];
if (ext == null) throw new Exception("[Error] Tried to get a extension, that doesn't exist.");
return (T)ext;
}
///
/// Called, when the user activates the scene.
///
internal void Activate()
{
if (!IsInitialized)
{
OnInitialization();
IsInitialized = true;
}
OnActivating();
}
///
/// Called, when the user activates the scene for the first time.
///
protected virtual void OnInitialization()
{ }
///
/// Called, when the user activates the scene.
///
protected virtual void OnActivating()
{
}
}
///
/// A generic scene that imports different functions.
///
/// The type of cameras.
/// The type of show items.
/// The type for collections
public abstract class GenericScene : GenericScene
where TCamera : GenericCamera, new()
where TCollection : GenericItemCollection, new()
where TItem : IShowItem
{
private TCollection _hud = new TCollection();
private TCollection _objectCollection = new TCollection();
///
/// If true, shows a axis helper at (0,0,0)
///
public bool ShowAxisHelper { get; set; } = false;
///
/// A collection for cameras to switch easier to different cameras.
///
public Dictionary Cameras = new Dictionary();
///
/// The active camera, that is used if the context doesn't force the viewport camera.
/// If none set, it automaticly uses the viewport camera.
///
public TCamera Camera { get; set; }
///
/// A camera to control the background.
///
public TCamera BackgroundCamera { get; set; } = new TCamera();
///
/// A camera to control the HUD.
///
public TCamera HUDCamera { get; set; } = new TCamera();
///
/// Objects inside the scene.
///
public TCollection Objects
{
get => _objectCollection;
set
{
value.Parent = this;
_objectCollection = value;
}
}
///
/// This defines the HUD objects.
///
public TCollection HUD
{
get => _hud;
set
{
value.Parent = this;
_hud = value;
}
}
///
public override void Update(UpdateContext context)
{
_objectCollection.Update(context);
_hud.Update(context);
}
///
public override void Draw(DrawContext context)
{
DrawBackground(context);
DrawMainObjects(context);
DrawHUD(context);
DrawDebug(context);
}
///
/// Draws only the background.
///
///
public void DrawBackground(DrawContext context)
{
var backgroundDrawContext = context;
backgroundDrawContext.View = BackgroundCamera.CalculateViewMatrix();
_Background?.Draw(backgroundDrawContext);
}
///
/// Draws only the main objects
///
///
public void DrawMainObjects(DrawContext context)
{
if (!context.ForceViewport && Camera != null) context.View = Camera.CalculateViewMatrix();
_objectCollection.Draw(context);
}
///
/// Draws only the HUD
///
///
public void DrawHUD(DrawContext context)
{
context.View = HUDCamera.CalculateViewMatrix();
_hud.Draw(context);
}
///
/// Draw the debug informations.
///
///
public virtual void DrawDebug(DrawContext context)
{
}
}
}