mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-25 22:18:34 +01:00
added camera scaling
This commit is contained in:
parent
47af6f992a
commit
ee051f2ab0
2 changed files with 43 additions and 3 deletions
|
@ -8,7 +8,13 @@ namespace MLEM.Cameras {
|
|||
public class Camera {
|
||||
|
||||
public Vector2 Position;
|
||||
public float Scale = 1;
|
||||
public float Scale {
|
||||
get => this.scale;
|
||||
set => this.scale = MathHelper.Clamp(value, this.MinScale, this.MaxScale);
|
||||
}
|
||||
private float scale = 1;
|
||||
public float MinScale = 0;
|
||||
public float MaxScale = float.MaxValue;
|
||||
public bool AutoScaleWithScreen;
|
||||
public Point AutoScaleReferenceSize;
|
||||
public float ActualScale {
|
||||
|
@ -76,5 +82,12 @@ namespace MLEM.Cameras {
|
|||
this.Max = new Vector2(this.Max.X, max.Y);
|
||||
}
|
||||
|
||||
public void Zoom(float delta, Vector2? zoomCenter = null) {
|
||||
var center = (zoomCenter ?? this.Viewport.Size.ToVector2() / 2) / this.ActualScale;
|
||||
var lastScale = this.Scale;
|
||||
this.Scale += delta;
|
||||
this.Position += center * ((this.Scale - lastScale) / this.Scale);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -2,6 +2,9 @@ using System;
|
|||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using MLEM.Cameras;
|
||||
using MLEM.Extended.Extensions;
|
||||
using MLEM.Extended.Tiled;
|
||||
using MLEM.Extensions;
|
||||
using MLEM.Font;
|
||||
using MLEM.Startup;
|
||||
|
@ -9,10 +12,15 @@ using MLEM.Textures;
|
|||
using MLEM.Ui;
|
||||
using MLEM.Ui.Elements;
|
||||
using MLEM.Ui.Style;
|
||||
using MonoGame.Extended.Tiled;
|
||||
|
||||
namespace Sandbox {
|
||||
public class GameImpl : MlemGame {
|
||||
|
||||
private Camera camera;
|
||||
private TiledMap map;
|
||||
private IndividualTiledMapRenderer mapRenderer;
|
||||
|
||||
public GameImpl() {
|
||||
this.IsMouseVisible = true;
|
||||
this.Window.ClientSizeChanged += (o, args) => {
|
||||
|
@ -23,7 +31,18 @@ namespace Sandbox {
|
|||
protected override void LoadContent() {
|
||||
base.LoadContent();
|
||||
|
||||
var tex = LoadContent<Texture2D>("Textures/Test");
|
||||
this.map = LoadContent<TiledMap>("Tiled/Map");
|
||||
this.mapRenderer = new IndividualTiledMapRenderer(this.map);
|
||||
|
||||
this.camera = new Camera(this.GraphicsDevice) {
|
||||
AutoScaleWithScreen = true,
|
||||
Scale = 2,
|
||||
LookingPosition = new Vector2(25, 25) * this.map.GetTileSize(),
|
||||
MinScale = 0.25F,
|
||||
MaxScale = 4
|
||||
};
|
||||
|
||||
/*var tex = LoadContent<Texture2D>("Textures/Test");
|
||||
this.UiSystem.Style = new UntexturedStyle(this.SpriteBatch) {
|
||||
Font = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont")),
|
||||
TextScale = 0.1F,
|
||||
|
@ -37,17 +56,25 @@ namespace Sandbox {
|
|||
var panel = new Panel(Anchor.Center, new Vector2(0, 100), Vector2.Zero) {SetWidthBasedOnChildren = true};
|
||||
panel.AddChild(new Button(Anchor.AutoLeft, new Vector2(100, 10)));
|
||||
panel.AddChild(new Button(Anchor.AutoCenter, new Vector2(80, 10)));
|
||||
this.UiSystem.Add("Panel", panel);
|
||||
this.UiSystem.Add("Panel", panel);*/
|
||||
}
|
||||
|
||||
protected override void Update(GameTime gameTime) {
|
||||
base.Update(gameTime);
|
||||
if (this.InputHandler.IsKeyPressed(Keys.F11))
|
||||
this.GraphicsDeviceManager.SetFullscreen(!this.GraphicsDeviceManager.IsFullScreen);
|
||||
|
||||
var delta = this.InputHandler.ScrollWheel - this.InputHandler.LastScrollWheel;
|
||||
if (delta != 0) {
|
||||
this.camera.Zoom(0.1F * Math.Sign(delta), this.InputHandler.MousePosition.ToVector2());
|
||||
}
|
||||
}
|
||||
|
||||
protected override void DoDraw(GameTime gameTime) {
|
||||
this.GraphicsDevice.Clear(Color.Black);
|
||||
this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, this.camera.ViewMatrix);
|
||||
this.mapRenderer.Draw(this.SpriteBatch, this.camera.GetVisibleRectangle().ToExtended());
|
||||
this.SpriteBatch.End();
|
||||
base.DoDraw(gameTime);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue