#region usings 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; /// /// 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) { } /// /// 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(); 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); } public void DrawBackground(DrawContext context) { var backgroundDrawContext = context; backgroundDrawContext.View = BackgroundCamera.CalculateViewMatrix(); _Background?.Draw(backgroundDrawContext); } public void DrawMainObjects(DrawContext context) { if (!context.ForceViewport && Camera != null) context.View = Camera.CalculateViewMatrix(); _objectCollection.Draw(context); } public void DrawHUD(DrawContext context) { context.View = HUDCamera.CalculateViewMatrix(); _hud.Draw(context); } public virtual void DrawDebug(DrawContext context) { } } }