2019-11-08 15:35:15 +01:00
using System ;
2020-02-27 18:56:49 +01:00
using System.IO ;
2020-05-15 00:34:04 +02:00
using System.Linq ;
2019-09-19 20:23:18 +02:00
using Microsoft.Xna.Framework ;
using Microsoft.Xna.Framework.Graphics ;
2019-11-08 15:35:15 +01:00
using Microsoft.Xna.Framework.Input ;
2019-11-10 22:36:25 +01:00
using MLEM.Cameras ;
2020-04-22 00:30:55 +02:00
using MLEM.Content ;
2020-02-27 18:56:49 +01:00
using MLEM.Data ;
2019-11-10 22:36:25 +01:00
using MLEM.Extended.Extensions ;
using MLEM.Extended.Tiled ;
2019-11-08 15:35:15 +01:00
using MLEM.Extensions ;
2019-09-20 13:22:05 +02:00
using MLEM.Font ;
2020-05-15 00:34:04 +02:00
using MLEM.Formatting ;
2020-02-27 18:56:49 +01:00
using MLEM.Misc ;
2019-09-19 20:23:18 +02:00
using MLEM.Startup ;
2019-09-20 13:22:05 +02:00
using MLEM.Textures ;
using MLEM.Ui ;
using MLEM.Ui.Elements ;
using MLEM.Ui.Style ;
2020-01-01 12:41:48 +01:00
using MonoGame.Extended ;
2019-11-10 22:36:25 +01:00
using MonoGame.Extended.Tiled ;
2020-02-27 18:56:49 +01:00
using Newtonsoft.Json.Linq ;
using RectangleF = MonoGame . Extended . RectangleF ;
2019-09-19 20:23:18 +02:00
namespace Sandbox {
public class GameImpl : MlemGame {
2019-11-10 22:36:25 +01:00
private Camera camera ;
private TiledMap map ;
private IndividualTiledMapRenderer mapRenderer ;
2020-01-01 12:41:48 +01:00
private TiledMapCollisions collisions ;
2020-04-22 00:30:55 +02:00
private RawContentManager rawContent ;
2020-05-15 00:34:04 +02:00
private TokenizedString tokenized ;
2019-11-10 22:36:25 +01:00
2019-09-19 20:23:18 +02:00
public GameImpl ( ) {
this . IsMouseVisible = true ;
2019-11-08 15:35:15 +01:00
this . Window . ClientSizeChanged + = ( o , args ) = > {
Console . WriteLine ( "Size changed" ) ;
} ;
2019-09-19 20:23:18 +02:00
}
protected override void LoadContent ( ) {
base . LoadContent ( ) ;
2020-04-22 00:30:55 +02:00
this . Components . Add ( this . rawContent = new RawContentManager ( this . Services ) ) ;
2019-11-10 22:36:25 +01:00
this . map = LoadContent < TiledMap > ( "Tiled/Map" ) ;
this . mapRenderer = new IndividualTiledMapRenderer ( this . map ) ;
2020-01-01 12:41:48 +01:00
this . collisions = new TiledMapCollisions ( this . map ) ;
2019-11-10 22:36:25 +01:00
this . camera = new Camera ( this . GraphicsDevice ) {
AutoScaleWithScreen = true ,
Scale = 2 ,
LookingPosition = new Vector2 ( 25 , 25 ) * this . map . GetTileSize ( ) ,
MinScale = 0.25F ,
MaxScale = 4
} ;
2020-04-22 00:30:55 +02:00
var tex = this . rawContent . Load < Texture2D > ( "Textures/Test" ) ;
2020-04-11 15:32:01 +02:00
var font = new GenericSpriteFont ( LoadContent < SpriteFont > ( "Fonts/TestFont" ) ) ;
2019-09-20 13:22:05 +02:00
this . UiSystem . Style = new UntexturedStyle ( this . SpriteBatch ) {
2020-04-11 15:32:01 +02:00
Font = font ,
2019-09-20 13:22:05 +02:00
TextScale = 0.1F ,
PanelTexture = new NinePatch ( new TextureRegion ( tex , 0 , 8 , 24 , 24 ) , 8 ) ,
ButtonTexture = new NinePatch ( new TextureRegion ( tex , 24 , 8 , 16 , 16 ) , 4 )
} ;
this . UiSystem . AutoScaleReferenceSize = new Point ( 1280 , 720 ) ;
this . UiSystem . AutoScaleWithScreen = true ;
this . UiSystem . GlobalScale = 5 ;
2019-11-05 21:44:51 +01:00
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 ) ) ) ;
2020-03-21 00:49:43 +01:00
this . UiSystem . Add ( "Panel" , panel ) ;
2020-02-27 18:56:49 +01:00
2020-03-21 00:49:43 +01:00
panel . SetData ( "TestKey" , new Vector2 ( 10 , 2 ) ) ;
2020-05-15 00:34:04 +02:00
//Console.WriteLine(panel.GetData<Vector2>("TestKey"));
2020-03-21 00:49:43 +01:00
var obj = new Test {
2020-02-27 18:56:49 +01:00
Vec = new Vector2 ( 10 , 20 ) ,
Point = new Point ( 20 , 30 ) ,
Rectangle = new Rectangle ( 1 , 2 , 3 , 4 ) ,
RectangleF = new RectangleF ( 4 , 5 , 6 , 7 ) . ToMlem ( ) ,
Dir = Direction2 . Left
} ;
var writer = new StringWriter ( ) ;
this . Content . GetJsonSerializer ( ) . Serialize ( writer , obj ) ;
2020-05-15 00:34:04 +02:00
//Console.WriteLine(writer.ToString());
2020-02-27 18:56:49 +01:00
// {"Vec":"10 20","Point":"20 30","Rectangle":"1 2 3 4","RectangleF":"4 5 6 7"}
// Also:
//this.Content.AddJsonConverter(new CustomConverter());
var res = this . Content . LoadJson < Test > ( "Test" ) ;
2020-05-15 00:34:04 +02:00
//Console.WriteLine(res);
2020-04-11 15:32:01 +02:00
2020-05-15 00:34:04 +02:00
/ * this . OnDraw + = ( game , time ) = > {
2020-04-11 15:32:01 +02:00
this . SpriteBatch . Begin ( ) ;
font . DrawString ( this . SpriteBatch , "Left Aligned\nover multiple lines" , new Vector2 ( 640 , 0 ) , TextAlign . Left , Color . White ) ;
font . DrawString ( this . SpriteBatch , "Center Aligned\nover multiple lines" , new Vector2 ( 640 , 100 ) , TextAlign . Center , Color . White ) ;
font . DrawString ( this . SpriteBatch , "Right Aligned\nover multiple lines" , new Vector2 ( 640 , 200 ) , TextAlign . Right , Color . White ) ;
font . DrawString ( this . SpriteBatch , "Center Aligned on both axes" , new Vector2 ( 640 , 360 ) , TextAlign . CenterBothAxes , Color . White ) ;
this . SpriteBatch . Draw ( this . SpriteBatch . GetBlankTexture ( ) , new Rectangle ( 640 - 4 , 360 - 4 , 8 , 8 ) , Color . Green ) ;
2020-04-11 15:44:55 +02:00
this . SpriteBatch . Draw ( this . SpriteBatch . GetBlankTexture ( ) , new Rectangle ( 200 , 400 , 200 , 400 ) , Color . Green ) ;
font . DrawString ( this . SpriteBatch , font . TruncateString ( "This is a very long string" , 200 , 1 ) , new Vector2 ( 200 , 400 ) , Color . White ) ;
font . DrawString ( this . SpriteBatch , font . TruncateString ( "This is a very long string" , 200 , 1 , ellipsis : "..." ) , new Vector2 ( 200 , 450 ) , Color . White ) ;
font . DrawString ( this . SpriteBatch , font . TruncateString ( "This is a very long string" , 200 , 1 , true ) , new Vector2 ( 200 , 500 ) , Color . White ) ;
font . DrawString ( this . SpriteBatch , font . TruncateString ( "This is a very long string" , 200 , 1 , true , "..." ) , new Vector2 ( 200 , 550 ) , Color . White ) ;
2020-04-11 15:32:01 +02:00
this . SpriteBatch . End ( ) ;
2020-05-15 00:34:04 +02:00
} ; * /
var formatter = new TextFormatter ( ) ;
2020-05-15 14:22:33 +02:00
var strg = "This <c CornflowerBlue>is a formatted string</c> with <c #ff0000>two bits of formatting</c>! It also includesavery<c Pink>long</c>wordthatis<c Blue>formatted</c>aswell. Additionally, it <a wobbly>wobbles</a> and has a <s>shadow</s> or a <s #ff0000 4>weird shadow</s>" ;
2020-05-15 00:34:04 +02:00
this . tokenized = formatter . Tokenize ( strg ) ;
this . tokenized . Split ( font , 400 , 1 ) ;
this . OnDraw + = ( g , time ) = > {
this . SpriteBatch . Begin ( ) ;
2020-05-15 14:22:33 +02:00
this . tokenized . Draw ( time , this . SpriteBatch , new Vector2 ( 400 , 20 ) , font , Color . White , 1 , 0 ) ;
2020-05-15 00:34:04 +02:00
this . SpriteBatch . End ( ) ;
2020-04-11 15:32:01 +02:00
} ;
2020-05-15 14:22:33 +02:00
this . OnUpdate + = ( g , time ) = > this . tokenized . Update ( time ) ;
2019-09-19 20:23:18 +02:00
}
2019-12-05 17:35:24 +01:00
protected override void DoUpdate ( GameTime gameTime ) {
base . DoUpdate ( gameTime ) ;
2019-11-08 15:35:15 +01:00
if ( this . InputHandler . IsKeyPressed ( Keys . F11 ) )
this . GraphicsDeviceManager . SetFullscreen ( ! this . GraphicsDeviceManager . IsFullScreen ) ;
2019-11-10 22:36:25 +01:00
var delta = this . InputHandler . ScrollWheel - this . InputHandler . LastScrollWheel ;
if ( delta ! = 0 ) {
this . camera . Zoom ( 0.1F * Math . Sign ( delta ) , this . InputHandler . MousePosition . ToVector2 ( ) ) ;
}
2019-11-08 15:35:15 +01:00
}
2019-09-19 20:23:18 +02:00
protected override void DoDraw ( GameTime gameTime ) {
this . GraphicsDevice . Clear ( Color . Black ) ;
2019-11-10 22:36:25 +01:00
this . SpriteBatch . Begin ( SpriteSortMode . Deferred , null , SamplerState . PointClamp , null , null , null , this . camera . ViewMatrix ) ;
2020-05-15 00:34:04 +02:00
/ * this . mapRenderer . Draw ( this . SpriteBatch , this . camera . GetVisibleRectangle ( ) . ToExtended ( ) ) ;
2020-01-01 12:41:48 +01:00
foreach ( var tile in this . collisions . GetCollidingTiles ( new RectangleF ( 0 , 0 , this . map . Width , this . map . Height ) ) ) {
foreach ( var area in tile . Collisions ) {
this . SpriteBatch . DrawRectangle ( area . Position * this . map . GetTileSize ( ) , area . Size * this . map . GetTileSize ( ) , Color . Red ) ;
}
2020-05-15 00:34:04 +02:00
} * /
2020-01-01 12:41:48 +01:00
2019-11-10 22:36:25 +01:00
this . SpriteBatch . End ( ) ;
2019-09-19 20:23:18 +02:00
base . DoDraw ( gameTime ) ;
}
2020-02-27 18:56:49 +01:00
private class Test {
public Vector2 Vec ;
public Point Point ;
public Rectangle Rectangle ;
public MLEM . Misc . RectangleF RectangleF ;
public Direction2 Dir ;
public override string ToString ( ) {
return $"{nameof(this.Vec)}: {this.Vec}, {nameof(this.Point)}: {this.Point}, {nameof(this.Rectangle)}: {this.Rectangle}, {nameof(this.RectangleF)}: {this.RectangleF}, {nameof(this.Dir)}: {this.Dir}" ;
}
}
2019-09-19 20:23:18 +02:00
}
}