Client LuaCsForBarotrauma
BarotraumaClient/ClientSource/Map/StructurePrefab.cs
1 using Microsoft.Xna.Framework;
2 using Microsoft.Xna.Framework.Graphics;
3 using System;
4 using System.Collections.Generic;
5 using System.Collections.Immutable;
6 
7 namespace Barotrauma
8 {
9  partial class StructurePrefab : MapEntityPrefab
10  {
11  public readonly Color BackgroundSpriteColor;
12 
13  public readonly ImmutableArray<DecorativeSprite> DecorativeSprites;
14  public readonly ImmutableDictionary<int, ImmutableArray<DecorativeSprite>> DecorativeSpriteGroups;
15 
16  public override void UpdatePlacing(Camera cam)
17  {
19  {
20  Selected = null;
21  return;
22  }
23 
24  Vector2 position = Submarine.MouseToWorldGrid(cam, Submarine.MainSub);
25  Vector2 size = ScaledSize;
26  Rectangle newRect = new Rectangle((int)position.X, (int)position.Y, (int)size.X, (int)size.Y);
27 
28  if (placePosition == Vector2.Zero)
29  {
30  if (PlayerInput.PrimaryMouseButtonHeld() && GUI.MouseOn == null)
31  {
33  }
34  newRect.X = (int)position.X;
35  newRect.Y = (int)position.Y;
36  }
37  else
38  {
39  Vector2 placeSize = size;
40  if (ResizeHorizontal) placeSize.X = position.X - placePosition.X;
41  if (ResizeVertical) placeSize.Y = placePosition.Y - position.Y;
42 
43  //don't allow resizing width/height to less than the grid size
44  if (ResizeHorizontal && Math.Abs(placeSize.X) < Submarine.GridSize.X)
45  {
46  placeSize.X = Submarine.GridSize.X;
47  }
48  if (ResizeVertical && Math.Abs(placeSize.Y) < Submarine.GridSize.Y)
49  {
50  placeSize.Y = Submarine.GridSize.Y;
51  }
52 
53  newRect = Submarine.AbsRect(placePosition, placeSize);
55  {
56  newRect.Location -= MathUtils.ToPoint(Submarine.MainSub.Position);
57  var structure = new Structure(newRect, this, Submarine.MainSub)
58  {
60  };
61 
62  SubEditorScreen.StoreCommand(new AddOrDeleteCommand(new List<MapEntity> { structure }, false));
63  placePosition = Vector2.Zero;
64  if (!PlayerInput.IsShiftDown())
65  {
66  Selected = null;
67  }
68  return;
69  }
70  }
71  }
72 
73  public override void DrawPlacing(SpriteBatch spriteBatch, Camera cam)
74  {
75  Vector2 position = Submarine.MouseToWorldGrid(cam, Submarine.MainSub);
76  Rectangle newRect = new Rectangle((int)position.X, (int)position.Y, (int)ScaledSize.X, (int)ScaledSize.Y);
77 
78  if (placePosition == Vector2.Zero)
79  {
80  newRect.X = (int)position.X;
81  newRect.Y = (int)position.Y;
82  }
83  else
84  {
85  Vector2 placeSize = ScaledSize;
86  if (ResizeHorizontal) placeSize.X = position.X - placePosition.X;
87  if (ResizeVertical) placeSize.Y = placePosition.Y - position.Y;
88 
89  newRect = Submarine.AbsRect(placePosition, placeSize);
90  }
91 
92  Sprite.DrawTiled(spriteBatch, new Vector2(newRect.X, -newRect.Y), new Vector2(newRect.Width, newRect.Height), textureScale: TextureScale * Scale);
93  GUI.DrawRectangle(spriteBatch, new Rectangle(newRect.X - GameMain.GraphicsWidth, -newRect.Y, newRect.Width + GameMain.GraphicsWidth * 2, newRect.Height), Color.White);
94  GUI.DrawRectangle(spriteBatch, new Rectangle(newRect.X, -newRect.Y - GameMain.GraphicsHeight, newRect.Width, newRect.Height + GameMain.GraphicsHeight * 2), Color.White);
95  }
96 
97  public override void DrawPlacing(SpriteBatch spriteBatch, Rectangle placeRect, float scale = 1.0f, float rotation = 0.0f, SpriteEffects spriteEffects = SpriteEffects.None)
98  {
99  var position = placeRect.Location.ToVector2().FlipY();
100  position += placeRect.Size.ToVector2() * 0.5f;
101 
103  spriteBatch,
104  position,
105  placeRect.Size.ToVector2(),
106  color: Color.White * 0.8f,
107  origin: placeRect.Size.ToVector2() * 0.5f,
108  rotation: rotation,
109  textureScale: TextureScale * scale,
110  spriteEffects: spriteEffects ^ Sprite.effects);
111  }
112  }
113 }
static int GraphicsWidth
Definition: GameMain.cs:162
static int GraphicsHeight
Definition: GameMain.cs:168
void DrawTiled(ISpriteBatch spriteBatch, Vector2 position, Vector2 targetSize, float rotation=0f, Vector2? origin=null, Color? color=null, Vector2? startOffset=null, Vector2? textureScale=null, float? depth=null, SpriteEffects? spriteEffects=null)
readonly ImmutableDictionary< int, ImmutableArray< DecorativeSprite > > DecorativeSpriteGroups
readonly ImmutableArray< DecorativeSprite > DecorativeSprites
override void DrawPlacing(SpriteBatch spriteBatch, Rectangle placeRect, float scale=1.0f, float rotation=0.0f, SpriteEffects spriteEffects=SpriteEffects.None)
override void DrawPlacing(SpriteBatch spriteBatch, Camera cam)
static void StoreCommand(Command command)
static Vector2 MouseToWorldGrid(Camera cam, Submarine sub, Vector2? mousePos=null, bool round=false)
static Rectangle AbsRect(Vector2 pos, Vector2 size)