Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using MonoGame.Extended.BitmapFonts;
- namespace TheMarket.Share
- {
- class InDrawFunctions
- {
- private static Vector2 defaultvec2 = default(Vector2);
- private static SpriteFont defaultsf = default(SpriteFont);
- private static BitmapFont defaultbmf = default(BitmapFont);
- private static Color defaultcolor = default(Color);
- private static BasicEffect be;
- private static VertexBuffer solidTriangles;
- private static VertexBuffer hollowTriangles;
- private static VertexBuffer lines;
- private static List<Poly> solidTrianglesList = new List<Poly>();
- private static List<Poly> linesList = new List<Poly>();
- public static void Initialize(GraphicsDevice gd)
- {
- be = new BasicEffect(gd);
- //right bottom
- //be.Projection=Matrix.CreateOrthographic(TheMarket.PreferredCurrentScreenSize.X, TheMarket.PreferredCurrentScreenSize.Y, 0,1000)+Matrix.CreateTranslation(350,350) * Matrix.CreateScale(Manager.TranslationVectorToVirtual.X, Manager.TranslationVectorToVirtual.Y, 1f);
- //be.Projection = Matrix.CreateOrthographicOffCenter(0, TheMarket.PreferredCurrentScreenSize.X, TheMarket.PreferredCurrentScreenSize.Y, 0, 0, 1000); //Correct One!
- be.Projection = Matrix.CreateOrthographicOffCenter(0, Manager.Game.GraphicsDevice.Viewport.Width,
- Manager.Game.GraphicsDevice.Viewport.Height, 0, 0, 1);// * Matrix.CreateTranslation(-0.5f, -0.5f, 0);
- //be.Projection = Matrix.CreateOrthographicOffCenter(0, gd.Adapter.CurrentDisplayMode.Width, gd.Adapter.CurrentDisplayMode.Height, 0, 0, 1000);
- be.View = Matrix.Identity;//* Matrix.CreateScale(Manager.TranslationVectorToReal.X, Manager.TranslationVectorToReal.Y, 1f);
- be.World = Matrix.Identity ;
- be.VertexColorEnabled = true;
- }
- public static void Begin()
- {
- solidTrianglesList=new List<Poly>();
- linesList = new List<Poly>();
- }
- public static void End()
- {
- if (solidTrianglesList.Count > 0)
- {
- VertexPositionColor[] vpcs = new VertexPositionColor[solidTrianglesList.Sum(m => m.Vertices.Length)];
- for (int i = 0; i < solidTrianglesList.Count; i++)
- {
- for (int j = 0; j < solidTrianglesList[i].Vertices.Length; j++)
- {
- vpcs[i* solidTrianglesList[i].Vertices.Length + j] = solidTrianglesList[i].Vertices[j];
- }
- }
- solidTriangles = new VertexBuffer(Manager.Game.GraphicsDevice, typeof(VertexPositionColor), vpcs.Length,
- BufferUsage.WriteOnly);
- solidTriangles.SetData(vpcs);
- var rasterizerState = new RasterizerState();
- rasterizerState.CullMode = CullMode.None;
- Manager.Game.GraphicsDevice.RasterizerState = rasterizerState;
- Manager.Game.GraphicsDevice.SetVertexBuffer(solidTriangles);
- foreach (var pass in be.CurrentTechnique.Passes)
- {
- pass.Apply();
- Manager.Game.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, vpcs.Length/3);
- }
- }
- if (linesList.Count > 0)
- {
- VertexPositionColor[] vpcs3 = new VertexPositionColor[linesList.Sum(m => m.Vertices.Length)];
- int offset = 0;
- for (int i = 0; i < linesList.Count; i++)
- {
- for (int j = 0; j < linesList[i].Vertices.Length; j++)
- {
- var index = offset + j;
- vpcs3[index] = linesList[i].Vertices[j];
- }
- offset += linesList[i].Vertices.Length;
- }
- lines = new VertexBuffer(Manager.Game.GraphicsDevice, typeof(VertexPositionColor), vpcs3.Length,
- BufferUsage.WriteOnly);
- lines.SetData(vpcs3);
- var rasterizerState = new RasterizerState();
- rasterizerState.CullMode = CullMode.None;
- Manager.Game.GraphicsDevice.RasterizerState = rasterizerState;
- Manager.Game.GraphicsDevice.SetVertexBuffer(lines);
- foreach (var pass in be.CurrentTechnique.Passes)
- {
- pass.Apply();
- Manager.Game.GraphicsDevice.DrawPrimitives(PrimitiveType.LineList, 0, vpcs3.Length / 2);
- }
- }
- }
- public static void Set<T>(SpriteBatch spb, int x, int y, T obj, int minLength = 0, Color col = default(Color), SpriteFont sprfnt = default(SpriteFont), Vector2 origin = default(Vector2), Vector2 scale = default(Vector2), float rotation = 0f)
- {
- if (col == defaultcolor)
- col = Color.Black;
- if (sprfnt == defaultsf)
- sprfnt = Manager.Fonts[0];
- if (origin == defaultvec2)
- origin = Vector2.Zero;
- if (scale == defaultvec2)
- scale = Vector2.One;
- #if BOXES
- DrawBox(spb, new Rectangle(new Point(x, y), sprfnt.MeasureString(obj.ToString()).ToPoint()), Color.FromNonPremultiplied(0,0,0,127)); //Debug the Boundings
- #endif
- spb.DrawString(sprfnt, obj.ToString(), new Vector2(x, y), col, 0f, origin, scale, SpriteEffects.None, rotation);
- }
- public static void SetBF<T>(SpriteBatch spb, int x, int y, T obj, int minLength = 0, Color col = default(Color), BitmapFont sprfnt = default(BitmapFont), Vector2 origin = default(Vector2), Vector2 scale = default(Vector2), float rotation = 0f)
- {
- if (col == defaultcolor)
- col = Color.Black;
- if (sprfnt == defaultbmf)
- sprfnt = Manager.BitmapFonts[0];
- if (origin == defaultvec2)
- origin = Vector2.Zero;
- if (scale == defaultvec2)
- scale = Vector2.One;
- if (sprfnt != null)
- {
- #if BOXES
- DrawBox(spb, new Rectangle(new Point(x, y), new Point(sprfnt.MeasureString(obj.ToString()).Width, sprfnt.MeasureString(obj.ToString()).Height)), Color.FromNonPremultiplied(0, 0, 0, 127),true);
- DrawBox(spb, new Rectangle(new Point(x, y), new Point(sprfnt.MeasureString(obj.ToString()).Width, sprfnt.MeasureString(obj.ToString()).Height)), Color.FromNonPremultiplied(255, 0, 0, 255),false); //Debug the Boundings
- #endif
- spb.DrawString(sprfnt, obj.ToString(), new Vector2(x, y), col,rotation,origin,scale,SpriteEffects.None,0f);
- }
- }
- public static void Set<T>(SpriteBatch spb, Vector2 pos, T obj, int minLength = 0, Color col = default(Color), SpriteFont sprfnt = default(SpriteFont), Vector2 origin = default(Vector2), Vector2 scale = default(Vector2), float rotation = 0f)
- {
- Set<T>(spb, (int)pos.X, (int)pos.Y, obj, minLength, col, sprfnt, origin, scale, rotation);
- }
- public static void SetBF<T>(SpriteBatch spb, Vector2 pos, T obj, int minLength = 0, Color col = default(Color), BitmapFont bmfnt = default(BitmapFont), Vector2 origin = default(Vector2), Vector2 scale = default(Vector2), float rotation = 0f)
- {
- SetBF<T>(spb, (int)pos.X, (int)pos.Y, obj, minLength, col, bmfnt, origin, scale, rotation);
- }
- public static void DrawBox(SpriteBatch spb, Rectangle rect, Color col, bool filled = false)
- {
- if (filled)
- {
- solidTrianglesList.Add(new Poly
- {
- Vertices = new VertexPositionColor[]
- {
- new VertexPositionColor(new Vector3(rect.X, rect.Y, 0), col),
- new VertexPositionColor(new Vector3(rect.X + rect.Width, rect.Y, 0), col),
- new VertexPositionColor(new Vector3(rect.X + rect.Width, rect.Y + rect.Height, 0), col),
- new VertexPositionColor(new Vector3(rect.X, rect.Y, 0), col),
- new VertexPositionColor(new Vector3(rect.X, rect.Y + rect.Height, 0), col),
- new VertexPositionColor(new Vector3(rect.X + rect.Width, rect.Y + rect.Height, 0), col),
- }
- });
- }
- else
- {
- linesList.Add(new Poly
- {
- Vertices = new VertexPositionColor[]
- {
- new VertexPositionColor(new Vector3(rect.X, rect.Y, 0), col),
- new VertexPositionColor(new Vector3(rect.X+rect.Width, rect.Y, 0), col),
- new VertexPositionColor(new Vector3(rect.X, rect.Y, 0), col),
- new VertexPositionColor(new Vector3(rect.X, rect.Y+rect.Height, 0), col),
- new VertexPositionColor(new Vector3(rect.X, rect.Y+rect.Height, 0), col),
- new VertexPositionColor(new Vector3(rect.X+rect.Width, rect.Y+rect.Height, 0), col),
- new VertexPositionColor(new Vector3(rect.X+rect.Width, rect.Y, 0), col),
- new VertexPositionColor(new Vector3(rect.X+rect.Width, rect.Y+rect.Height, 0), col)
- }
- });
- }
- }
- public static void DrawLine(SpriteBatch spb, Vector2 a,Vector2 b,Color col)
- {
- linesList.Add(new Poly
- {
- Vertices = new VertexPositionColor[]
- {
- new VertexPositionColor(new Vector3(a,0), col),
- new VertexPositionColor(new Vector3(b,0), col),
- }
- });
- }
- private class Poly
- {
- public VertexPositionColor[] Vertices;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement