Client LuaCsForBarotrauma
WikiImage.cs
1 using Barotrauma.IO;
2 using FarseerPhysics;
3 using Microsoft.Xna.Framework;
4 using Microsoft.Xna.Framework.Graphics;
5 using System;
6 using System.Collections.Generic;
7 using System.Text;
8 
9 namespace Barotrauma
10 {
11  static class WikiImage
12  {
13  public static Rectangle CalculateBoundingBox(Character character)
14  {
15  Rectangle boundingBox = new Rectangle(character.WorldPosition.ToPoint(), Point.Zero);
16 
17  void addPointsToBBox(float extentX, float extentY, Vector2 worldPos, Vector2 origin, float rotation)
18  {
19  float sinRotation = (float)Math.Sin((double)rotation);
20  float cosRotation = (float)Math.Cos((double)rotation);
21 
22  origin = new Vector2(
23  origin.X * cosRotation + origin.Y * sinRotation,
24  origin.X * sinRotation - origin.Y * cosRotation);
25  var limbPos = worldPos.ToPoint();
26  boundingBox.AddPoint(limbPos);
27  Vector2 xExtend = new Vector2((extentX * cosRotation), (extentX * sinRotation));
28  Vector2 yExtend = new Vector2((extentY * sinRotation), (-extentY * cosRotation));
29  boundingBox.AddPoint(limbPos + (xExtend + yExtend - origin).ToPoint());
30  boundingBox.AddPoint(limbPos + (xExtend - yExtend - origin).ToPoint());
31  boundingBox.AddPoint(limbPos + (-xExtend - yExtend - origin).ToPoint());
32  boundingBox.AddPoint(limbPos + (-xExtend + yExtend - origin).ToPoint());
33  }
34 
35  foreach (Limb limb in character.AnimController.Limbs)
36  {
37  if (limb.ActiveSprite == null) { continue; }
38  float extentX = (float)limb.ActiveSprite.size.X * limb.Scale * limb.TextureScale * 0.5f;
39  //extentX = ConvertUnits.ToDisplayUnits(extentX);
40  float extentY = (float)limb.ActiveSprite.size.Y * limb.Scale * limb.TextureScale * 0.5f;
41  //extentY = ConvertUnits.ToDisplayUnits(extentY);
42 
43  Vector2 origin = (limb.ActiveSprite.Origin - (limb.ActiveSprite.SourceRect.Size.ToVector2() * 0.5f)) * limb.Scale * limb.TextureScale;
44  addPointsToBBox(extentX, extentY, limb.WorldPosition, origin, limb.body.Rotation);
45  }
46 
47 
48  if (character.Inventory != null)
49  {
50  foreach (var item in character.Inventory.AllItems)
51  {
52  if (item?.Sprite != null && item?.body != null)
53  {
54  float extentX = (float)item.Sprite.size.X * item.Scale * 0.5f;
55  //extentX = ConvertUnits.ToDisplayUnits(extentX);
56  float extentY = (float)item.Sprite.size.Y * item.Scale * 0.5f;
57  //extentY = ConvertUnits.ToDisplayUnits(extentY);
58 
59  Vector2 origin = (item.Sprite.Origin - (item.Sprite.SourceRect.Size.ToVector2() * 0.5f)) * item.Scale;
60  addPointsToBBox(extentX, extentY, item.WorldPosition, origin, item.body.Rotation);
61  }
62  }
63  }
64 
65  boundingBox.X -= 25; boundingBox.Y -= 25;
66  boundingBox.Width += 50; boundingBox.Height += 50;
67 
68  return boundingBox;
69  }
70 
71  public static void Create(Character character)
72  {
73  Rectangle boundingBox = CalculateBoundingBox(character);
74 
75  int texWidth = Math.Clamp((int)(boundingBox.Width * 2.5f), 512, 4096);
76  float zoom = (float)texWidth / (float)boundingBox.Width;
77  int texHeight = (int)(zoom * boundingBox.Height);
78 
79  Camera cam = new Camera();
80  cam.SetResolution(new Point(texWidth, texHeight));
81  cam.MaxZoom = zoom;
82  cam.MinZoom = zoom * 0.5f;
83  cam.Zoom = zoom;
84  cam.Position = boundingBox.Center.ToVector2();
85  cam.UpdateTransform(false);
86 
87  using (RenderTarget2D rt = new RenderTarget2D(
88  GameMain.Instance.GraphicsDevice,
89  texWidth, texHeight, false, SurfaceFormat.Color, DepthFormat.None))
90  {
91  using (SpriteBatch spriteBatch = new SpriteBatch(GameMain.Instance.GraphicsDevice))
92  {
93  Viewport prevViewport = GameMain.Instance.GraphicsDevice.Viewport;
94  GameMain.Instance.GraphicsDevice.Viewport = new Viewport(0, 0, texWidth, texHeight);
95  GameMain.Instance.GraphicsDevice.SetRenderTarget(rt);
96  GameMain.Instance.GraphicsDevice.Clear(Color.Transparent);
97  spriteBatch.Begin(SpriteSortMode.BackToFront, transformMatrix: cam.Transform);
98  character.Draw(spriteBatch, cam);
99  if (character.Inventory != null)
100  {
101  foreach (var item in character.Inventory.AllItems)
102  {
103  if (item != null)
104  {
105  item.Draw(spriteBatch, false, false);
106  item.Draw(spriteBatch, false, true);
107  }
108  }
109  }
110  spriteBatch.End();
111  GameMain.Instance.GraphicsDevice.SetRenderTarget(null);
112  GameMain.Instance.GraphicsDevice.Viewport = prevViewport;
113  using (FileStream fs = File.Open("wikiimage.png", System.IO.FileMode.Create))
114  {
115  rt.SaveAsPng(fs, texWidth, texHeight);
116  }
117  }
118  }
119  }
120 
121  public static void Create(Submarine sub)
122  {
123  int width = 4096; int height = 4096;
124 
125  Rectangle subDimensions = sub.CalculateDimensions(false);
126  Vector2 viewPos = subDimensions.Center.ToVector2();
127  float scale = Math.Min(width / (float)subDimensions.Width, height / (float)subDimensions.Height);
128 
129  var viewMatrix = Matrix.CreateTranslation(new Vector3(width / 2.0f, height / 2.0f, 0));
130  var transform = Matrix.CreateTranslation(
131  new Vector3(-viewPos.X, viewPos.Y, 0)) *
132  Matrix.CreateScale(new Vector3(scale, scale, 1)) *
133  viewMatrix;
134 
135  using (RenderTarget2D rt = new RenderTarget2D(
136  GameMain.Instance.GraphicsDevice,
137  width, height, false, SurfaceFormat.Color, DepthFormat.None))
138  using (SpriteBatch spriteBatch = new SpriteBatch(GameMain.Instance.GraphicsDevice))
139  {
140  Viewport prevViewport = GameMain.Instance.GraphicsDevice.Viewport;
141  GameMain.Instance.GraphicsDevice.Viewport = new Viewport(0, 0, width, height);
142  GameMain.Instance.GraphicsDevice.SetRenderTarget(rt);
143  GameMain.Instance.GraphicsDevice.Clear(Color.Transparent);
144 
145  DrawBatch(() => Submarine.DrawBack(spriteBatch, editing: false, e => e is Structure s && (e.SpriteDepth >= 0.9f || s.Prefab.BackgroundSprite != null)));
146  DrawBatch(() => Submarine.DrawBack(spriteBatch, editing: false, e => (e is not Structure || e.SpriteDepth < 0.9f)));
147  DrawBatch(() => Submarine.DrawDamageable(spriteBatch, null, editing: false));
148  DrawBatch(() => Submarine.DrawFront(spriteBatch, editing: false));
149 
150  void DrawBatch(Action drawAction)
151  {
152  spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.NonPremultiplied, null, null, null, null, transform);
153  drawAction.Invoke();
154  spriteBatch.End();
155  }
156 
157  GameMain.Instance.GraphicsDevice.SetRenderTarget(null);
158  GameMain.Instance.GraphicsDevice.Viewport = prevViewport;
159  using (FileStream fs = File.Open("wikiimage.png", System.IO.FileMode.Create))
160  {
161  rt.SaveAsPng(fs, width, height);
162  }
163  }
164  }
165  }
166 }