2024-01-22 23:47:47 +03:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2024-01-31 18:46:54 +03:00
|
|
|
|
2024-01-22 23:47:47 +03:00
|
|
|
using Syntriax.Engine.Core;
|
2024-01-31 18:46:54 +03:00
|
|
|
using Syntriax.Engine.Core.Abstract;
|
2024-01-22 23:47:47 +03:00
|
|
|
|
|
|
|
namespace Pong.Behaviours;
|
2024-01-31 12:51:23 +03:00
|
|
|
|
2024-01-31 18:46:54 +03:00
|
|
|
public class MonoGameCamera2DBehaviour(GraphicsDeviceManager Graphics) : BehaviourOverride, ICamera2D
|
2024-01-22 23:47:47 +03:00
|
|
|
{
|
2024-07-15 01:17:05 +03:00
|
|
|
public event OnMatrixTransformChangedDelegate? OnMatrixTransformChanged = null;
|
|
|
|
public event OnViewportChangedDelegate? OnViewportChanged = null;
|
|
|
|
public event OnZoomChangedDelegate? OnZoomChanged = null;
|
2024-01-22 23:47:47 +03:00
|
|
|
|
|
|
|
private Matrix _matrixTransform = Matrix.Identity;
|
|
|
|
|
|
|
|
private Viewport _viewport = default;
|
|
|
|
private float _zoom = 1f;
|
|
|
|
|
2024-01-31 12:51:23 +03:00
|
|
|
public GraphicsDeviceManager Graphics { get; } = Graphics;
|
|
|
|
|
2024-01-22 23:47:47 +03:00
|
|
|
public Matrix MatrixTransform
|
|
|
|
{
|
|
|
|
get => _matrixTransform;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (_matrixTransform == value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_matrixTransform = value;
|
|
|
|
OnMatrixTransformChanged?.Invoke(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2D Position
|
|
|
|
{
|
|
|
|
get => Transform.Position;
|
|
|
|
set => Transform.Position = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Viewport Viewport
|
|
|
|
{
|
|
|
|
get => _viewport;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (_viewport.Equals(value))
|
|
|
|
return;
|
|
|
|
|
|
|
|
_viewport = value;
|
|
|
|
OnViewportChanged?.Invoke(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public float Zoom
|
|
|
|
{
|
|
|
|
get => _zoom;
|
|
|
|
set
|
|
|
|
{
|
2024-01-30 12:15:26 +03:00
|
|
|
float newValue = Math.Max(0.1f, value);
|
2024-01-22 23:47:47 +03:00
|
|
|
|
|
|
|
if (_zoom == newValue)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_zoom = newValue;
|
|
|
|
OnZoomChanged?.Invoke(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public float Rotation
|
|
|
|
{
|
|
|
|
get => Transform.Rotation;
|
|
|
|
set => Transform.Rotation = value;
|
|
|
|
}
|
|
|
|
|
2024-07-15 01:17:05 +03:00
|
|
|
public event IAssignableTransform.OnTransformAssignedDelegate? OnTransformAssigned { add => GameObject.OnTransformAssigned += value; remove => GameObject.OnTransformAssigned -= value; }
|
2024-01-31 18:46:54 +03:00
|
|
|
ITransform IAssignableTransform.Transform => GameObject.Transform;
|
|
|
|
bool IAssignableTransform.Assign(ITransform transform) => GameObject.Assign(transform);
|
|
|
|
|
|
|
|
// TODO This causes delay since OnPreDraw calls assuming this is called in in Update
|
|
|
|
public Vector2D ScreenToWorldPosition(Vector2D screenPosition)
|
|
|
|
{
|
|
|
|
Vector2D worldPosition = Vector2.Transform(screenPosition.ToVector2(), Matrix.Invert(MatrixTransform)).ToVector2D();
|
|
|
|
return worldPosition.Scale(EngineConverter.screenScale);
|
|
|
|
}
|
|
|
|
public Vector2D WorldToScreenPosition(Vector2D worldPosition)
|
|
|
|
{
|
|
|
|
Vector2D screenPosition = Vector2.Transform(worldPosition.ToVector2(), MatrixTransform).ToVector2D();
|
|
|
|
return screenPosition.Scale(EngineConverter.screenScale);
|
|
|
|
}
|
|
|
|
|
2024-01-30 12:59:41 +03:00
|
|
|
protected override void OnFirstActiveFrame()
|
2024-01-30 20:13:56 +03:00
|
|
|
=> Viewport = Graphics.GraphicsDevice.Viewport;
|
2024-01-30 12:59:41 +03:00
|
|
|
|
2024-01-30 12:14:49 +03:00
|
|
|
protected override void OnPreDraw()
|
2024-01-22 23:47:47 +03:00
|
|
|
{
|
|
|
|
MatrixTransform =
|
|
|
|
Matrix.CreateTranslation(new Vector3(-Position.X, Position.Y, 0f)) *
|
2024-01-31 18:46:54 +03:00
|
|
|
Matrix.CreateRotationZ(Rotation * Math.DegreeToRadian) *
|
2024-01-22 23:47:47 +03:00
|
|
|
Matrix.CreateScale(Zoom) *
|
|
|
|
Matrix.CreateTranslation(new Vector3(_viewport.Width * .5f, _viewport.Height * .5f, 0f));
|
|
|
|
}
|
2024-07-15 01:17:05 +03:00
|
|
|
|
|
|
|
public delegate void OnMatrixTransformChangedDelegate(MonoGameCamera2DBehaviour sender);
|
|
|
|
public delegate void OnViewportChangedDelegate(MonoGameCamera2DBehaviour sender);
|
|
|
|
public delegate void OnZoomChangedDelegate(MonoGameCamera2DBehaviour sender);
|
2024-01-22 23:47:47 +03:00
|
|
|
}
|