Cherry-picked [ab78991]: Rendering tiled sprites with scaling works correctly now (I think?), added option to edit the offset of structure textures
This commit is contained in:
@@ -16,7 +16,28 @@ namespace Barotrauma
|
||||
partial class Structure : MapEntity, IDamageable, IServerSerializable
|
||||
{
|
||||
private List<ConvexHull> convexHulls;
|
||||
|
||||
protected Vector2 textureScale = Vector2.One;
|
||||
[Editable, Serialize("1.0, 1.0", true)]
|
||||
public Vector2 TextureScale
|
||||
{
|
||||
get { return textureScale; }
|
||||
set
|
||||
{
|
||||
textureScale = new Vector2(
|
||||
MathHelper.Clamp(value.X, 0.01f, 10),
|
||||
MathHelper.Clamp(value.Y, 0.01f, 10));
|
||||
}
|
||||
}
|
||||
|
||||
protected Vector2 textureOffset = Vector2.Zero;
|
||||
[Editable, Serialize("0.0, 0.0", true)]
|
||||
public Vector2 TextureOffset
|
||||
{
|
||||
get { return textureOffset; }
|
||||
set { textureOffset = value; }
|
||||
}
|
||||
|
||||
private void GenerateConvexHull()
|
||||
{
|
||||
// If not null and not empty , remove the hulls from the system
|
||||
@@ -125,24 +146,38 @@ namespace Barotrauma
|
||||
float depth = prefab.sprite.Depth;
|
||||
depth -= (ID % 255) * 0.000001f;
|
||||
|
||||
Vector2 textureOffset = this.textureOffset;
|
||||
if (flippedX) textureOffset.X = -textureOffset.X;
|
||||
if (flippedY) textureOffset.Y = -textureOffset.Y;
|
||||
|
||||
if (back && damageEffect == null)
|
||||
{
|
||||
if (prefab.BackgroundSprite != null)
|
||||
{
|
||||
SpriteEffects oldEffects = prefab.BackgroundSprite.effects;
|
||||
prefab.BackgroundSprite.effects ^= SpriteEffects;
|
||||
|
||||
Point backGroundOffset = new Point(
|
||||
MathUtils.PositiveModulo((int)-textureOffset.X, prefab.BackgroundSprite.SourceRect.Width),
|
||||
MathUtils.PositiveModulo((int)-textureOffset.Y, prefab.BackgroundSprite.SourceRect.Height));
|
||||
|
||||
prefab.BackgroundSprite.DrawTiled(
|
||||
spriteBatch,
|
||||
new Vector2(rect.X + drawOffset.X, -(rect.Y + drawOffset.Y)),
|
||||
new Vector2(rect.Width, rect.Height),
|
||||
color: color,
|
||||
textureScale: TextureScale);
|
||||
textureScale: TextureScale,
|
||||
startOffset: backGroundOffset);
|
||||
|
||||
prefab.BackgroundSprite.effects = oldEffects;
|
||||
}
|
||||
}
|
||||
|
||||
SpriteEffects oldEffects = prefab.sprite.effects;
|
||||
prefab.sprite.effects ^= SpriteEffects;
|
||||
|
||||
if (back == prefab.sprite.Depth > 0.5f || editing)
|
||||
{
|
||||
SpriteEffects oldEffects = prefab.sprite.effects;
|
||||
prefab.sprite.effects ^= SpriteEffects;
|
||||
|
||||
for (int i = 0; i < sections.Length; i++)
|
||||
{
|
||||
if (damageEffect != null)
|
||||
@@ -163,27 +198,28 @@ namespace Barotrauma
|
||||
}
|
||||
}
|
||||
|
||||
Point textureOffset = new Point(
|
||||
(int)(Math.Abs(rect.Location.X - sections[i].rect.Location.X) / textureScale.X),
|
||||
(int)(Math.Abs(rect.Location.Y - sections[i].rect.Location.Y) / textureScale.Y));
|
||||
Point sectionOffset = new Point(
|
||||
Math.Abs(rect.Location.X - sections[i].rect.Location.X),
|
||||
Math.Abs(rect.Location.Y - sections[i].rect.Location.Y));
|
||||
|
||||
if (flippedX && isHorizontal) sectionOffset.X = sections[i].rect.Right - rect.Right;
|
||||
if (flippedY && !isHorizontal) sectionOffset.Y = (rect.Y - rect.Height) - (sections[i].rect.Y - sections[i].rect.Height);
|
||||
|
||||
if (flippedX && isHorizontal)
|
||||
{
|
||||
textureOffset.X = rect.Width - textureOffset.X - sections[i].rect.Width;
|
||||
}
|
||||
sectionOffset.X += MathUtils.PositiveModulo((int)-textureOffset.X, prefab.sprite.SourceRect.Width);
|
||||
sectionOffset.Y += MathUtils.PositiveModulo((int)-textureOffset.Y, prefab.sprite.SourceRect.Height);
|
||||
|
||||
prefab.sprite.DrawTiled(
|
||||
spriteBatch,
|
||||
new Vector2(sections[i].rect.X + drawOffset.X, -(sections[i].rect.Y + drawOffset.Y)),
|
||||
new Vector2(sections[i].rect.Width, sections[i].rect.Height),
|
||||
color: color,
|
||||
startOffset: textureOffset,
|
||||
color: spriteColor,
|
||||
startOffset: sectionOffset,
|
||||
depth: depth,
|
||||
textureScale: TextureScale);
|
||||
}
|
||||
}
|
||||
|
||||
prefab.sprite.effects = oldEffects;
|
||||
prefab.sprite.effects = oldEffects;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,95 +109,144 @@ namespace Barotrauma
|
||||
}
|
||||
}
|
||||
|
||||
public void DrawTiled(SpriteBatch spriteBatch, Vector2 pos, Vector2 targetSize,
|
||||
public void DrawTiled(SpriteBatch spriteBatch, Vector2 position, Vector2 targetSize,
|
||||
Rectangle? rect = null, Color? color = null, Point? startOffset = null, Vector2? textureScale = null, float? depth = null)
|
||||
{
|
||||
if (texture == null) return;
|
||||
// Init optional values, if not provided
|
||||
if (rect.HasValue)
|
||||
{
|
||||
//TODO: this probably shouldn't be modifying the sourceRect of the sprite?
|
||||
sourceRect = rect.Value;
|
||||
}
|
||||
color = color ?? Color.White;
|
||||
startOffset = startOffset ?? Point.Zero;
|
||||
//Init optional values
|
||||
Vector2 drawOffset = startOffset.HasValue ? new Vector2(startOffset.Value.X, startOffset.Value.Y) : Vector2.Zero;
|
||||
Vector2 scale = textureScale ?? Vector2.One;
|
||||
Color drawColor = color ?? Color.White;
|
||||
|
||||
//which area of the texture to draw
|
||||
Rectangle texPerspective = sourceRect;
|
||||
texPerspective.Location += startOffset.Value;
|
||||
targetSize = targetSize / scale;
|
||||
|
||||
//how many times the texture needs to be drawn on the x-axis
|
||||
int xTiles = (int)Math.Ceiling(targetSize.X / sourceRect.Width);
|
||||
//how many times the texture needs to be drawn on the y-axis
|
||||
int yTiles = (int)Math.Ceiling(targetSize.Y / sourceRect.Height);
|
||||
|
||||
//wrap texPerspective inside the source rectangle
|
||||
while (texPerspective.X >= sourceRect.Right)
|
||||
texPerspective.X = sourceRect.X + (texPerspective.X - sourceRect.Right);
|
||||
while (texPerspective.Y >= sourceRect.Bottom)
|
||||
texPerspective.Y = sourceRect.Y + (texPerspective.Y - sourceRect.Bottom);
|
||||
|
||||
float top = pos.Y;
|
||||
texPerspective.Height = (int)Math.Min(Math.Ceiling(targetSize.Y), sourceRect.Height);
|
||||
|
||||
for (int y = 0; y < yTiles; y++)
|
||||
//wrap the drawOffset inside the sourceRect
|
||||
drawOffset.X = (drawOffset.X / scale.X) % sourceRect.Width;
|
||||
drawOffset.Y = (drawOffset.Y / scale.Y) % sourceRect.Height;
|
||||
if (effects.HasFlag(SpriteEffects.FlipHorizontally))
|
||||
{
|
||||
float movementY = texPerspective.Height * scale.Y;
|
||||
texPerspective.Height = Math.Min((int)Math.Ceiling(targetSize.Y - texPerspective.Height * y), texPerspective.Height);
|
||||
float diff = targetSize.X % (sourceRect.Width * scale.X);
|
||||
drawOffset.X += (sourceRect.Width * scale.X - diff) / scale.X;
|
||||
}
|
||||
if (effects.HasFlag(SpriteEffects.FlipVertically))
|
||||
{
|
||||
float diff = targetSize.Y % (sourceRect.Height * scale.Y);
|
||||
drawOffset.Y += (sourceRect.Height * scale.Y - diff) / scale.Y;
|
||||
}
|
||||
|
||||
//how many times the texture needs to be drawn on the x-axis
|
||||
int xTiles = (int)Math.Ceiling((targetSize.X + drawOffset.X * scale.X) / (sourceRect.Width * scale.X));
|
||||
//how many times the texture needs to be drawn on the y-axis
|
||||
int yTiles = (int)Math.Ceiling((targetSize.Y + drawOffset.Y * scale.Y) / (sourceRect.Height * scale.Y));
|
||||
|
||||
float left = pos.X;
|
||||
texPerspective.Width = Math.Min((int)Math.Ceiling(targetSize.X), sourceRect.Width);
|
||||
//where the current tile is being drawn;
|
||||
Vector2 currDrawPosition = position - drawOffset;
|
||||
//which part of the texture we are currently drawing
|
||||
Rectangle texPerspective = sourceRect;
|
||||
|
||||
for (int x = 0; x < xTiles; x++)
|
||||
{
|
||||
texPerspective.X = sourceRect.X;
|
||||
texPerspective.Width = sourceRect.Width;
|
||||
texPerspective.Height = sourceRect.Height;
|
||||
|
||||
for (int x = 0; x < xTiles; x++)
|
||||
//offset to the left, draw a partial slice
|
||||
if (currDrawPosition.X < position.X)
|
||||
{
|
||||
float movementX = texPerspective.Width * scale.X;
|
||||
texPerspective.Width = Math.Min((int)Math.Ceiling(targetSize.X - texPerspective.Width * x), texPerspective.Width);
|
||||
|
||||
//the edge of this tile would go over the right edge of the source rectangle,
|
||||
//we need to wrap back and draw a slice from the left side
|
||||
if (texPerspective.Right > sourceRect.Right)
|
||||
float diff = (position.X - currDrawPosition.X);
|
||||
currDrawPosition.X += diff;
|
||||
texPerspective.Width -= (int)diff;
|
||||
if (!effects.HasFlag(SpriteEffects.FlipHorizontally))
|
||||
{
|
||||
int diff = texPerspective.Right - sourceRect.Right;
|
||||
if (effects.HasFlag(SpriteEffects.FlipHorizontally))
|
||||
{
|
||||
spriteBatch.Draw(texture,
|
||||
new Vector2(left, top),
|
||||
new Rectangle(sourceRect.X, texPerspective.Y, diff, texPerspective.Height),
|
||||
color.Value, rotation, Vector2.Zero, scale, effects, depth ?? this.depth);
|
||||
|
||||
texPerspective.Width -= diff;
|
||||
left += diff;
|
||||
}
|
||||
else
|
||||
{
|
||||
texPerspective.Width -= diff;
|
||||
spriteBatch.Draw(texture,
|
||||
new Vector2(left + texPerspective.Width * scale.X, top),
|
||||
new Rectangle(sourceRect.X, texPerspective.Y, diff, texPerspective.Height),
|
||||
color.Value, rotation, Vector2.Zero, scale, effects, depth ?? this.depth);
|
||||
}
|
||||
texPerspective.X += (int)diff;
|
||||
}
|
||||
else if (texPerspective.Bottom > sourceRect.Bottom)
|
||||
{
|
||||
//TODO: make this work correctly on vertically flipped sprites
|
||||
int diff = texPerspective.Bottom - sourceRect.Bottom;
|
||||
texPerspective.Height -= diff;
|
||||
spriteBatch.Draw(texture,
|
||||
new Vector2(left, top + texPerspective.Height * scale.Y),
|
||||
new Rectangle(texPerspective.X, sourceRect.Y, texPerspective.Width, diff),
|
||||
color.Value, rotation, Vector2.Zero, scale, effects, depth ?? this.depth);
|
||||
}
|
||||
|
||||
spriteBatch.Draw(texture, new Vector2(left, top), texPerspective,
|
||||
color.Value, rotation, Vector2.Zero, scale, effects, depth ?? this.depth);
|
||||
|
||||
if (texPerspective.X + movementX >= sourceRect.Right && x < xTiles - 1) texPerspective.X = sourceRect.X;
|
||||
left += movementX;
|
||||
}
|
||||
if (texPerspective.Y + movementY >= sourceRect.Bottom && y < yTiles - 1) texPerspective.Y = sourceRect.Y;
|
||||
top += movementY;
|
||||
//drawing an offset flipped sprite, need to draw an extra slice to the left side
|
||||
if (currDrawPosition.X > position.X && x == 0)
|
||||
{
|
||||
if (effects.HasFlag(SpriteEffects.FlipHorizontally))
|
||||
{
|
||||
int sliceWidth = (int)((currDrawPosition.X - position.X) * scale.X);
|
||||
|
||||
Vector2 slicePos = currDrawPosition;
|
||||
slicePos.X = position.X;
|
||||
Rectangle sliceRect = texPerspective;
|
||||
sliceRect.X = SourceRect.X;
|
||||
sliceRect.Width = (int)(sliceWidth / scale.X);
|
||||
|
||||
if (effects.HasFlag(SpriteEffects.FlipVertically))
|
||||
{
|
||||
slicePos.Y += size.Y;
|
||||
}
|
||||
|
||||
spriteBatch.Draw(texture, slicePos, sliceRect, drawColor, rotation, Vector2.Zero, scale, effects, depth ?? this.depth);
|
||||
currDrawPosition.X = slicePos.X + sliceWidth;
|
||||
}
|
||||
}
|
||||
//make sure the rightmost tiles don't go over the right side
|
||||
if (x == xTiles - 1)
|
||||
{
|
||||
int diff = (int)(((currDrawPosition.X + texPerspective.Width * scale.X) - (position.X + targetSize.X)) / scale.X);
|
||||
texPerspective.Width -= diff;
|
||||
if (effects.HasFlag(SpriteEffects.FlipHorizontally))
|
||||
{
|
||||
texPerspective.X += diff;
|
||||
}
|
||||
}
|
||||
|
||||
currDrawPosition.Y = position.Y - drawOffset.Y;
|
||||
|
||||
for (int y = 0; y < yTiles; y++)
|
||||
{
|
||||
texPerspective.Y = sourceRect.Y;
|
||||
texPerspective.Height = sourceRect.Height;
|
||||
|
||||
//offset above the top, draw a partial slice
|
||||
if (currDrawPosition.Y < position.Y)
|
||||
{
|
||||
float diff = (position.Y - currDrawPosition.Y);
|
||||
currDrawPosition.Y += diff;
|
||||
texPerspective.Height -= (int)diff;
|
||||
if (!effects.HasFlag(SpriteEffects.FlipVertically))
|
||||
{
|
||||
texPerspective.Y += (int)diff;
|
||||
}
|
||||
}
|
||||
|
||||
//drawing an offset flipped sprite, need to draw an extra slice to the top
|
||||
if (currDrawPosition.Y > position.Y && y == 0)
|
||||
{
|
||||
if (effects.HasFlag(SpriteEffects.FlipVertically))
|
||||
{
|
||||
int sliceHeight = (int)((currDrawPosition.Y - position.Y) * scale.Y);
|
||||
|
||||
Vector2 slicePos = currDrawPosition;
|
||||
slicePos.Y = position.Y;
|
||||
Rectangle sliceRect = texPerspective;
|
||||
sliceRect.Y = SourceRect.Y;
|
||||
sliceRect.Height = (int)(sliceHeight / scale.Y);
|
||||
|
||||
spriteBatch.Draw(texture, slicePos, sliceRect, drawColor, rotation, Vector2.Zero, scale, effects, depth ?? this.depth);
|
||||
|
||||
currDrawPosition.Y = slicePos.Y + sliceHeight;
|
||||
}
|
||||
}
|
||||
|
||||
//make sure the bottommost tiles don't go over the bottom
|
||||
if (y == yTiles - 1)
|
||||
{
|
||||
int diff = (int)(((currDrawPosition.Y + texPerspective.Height * scale.Y) - (position.Y + targetSize.Y)) / scale.Y);
|
||||
texPerspective.Height -= diff;
|
||||
if (effects.HasFlag(SpriteEffects.FlipVertically))
|
||||
{
|
||||
texPerspective.Y += diff;
|
||||
}
|
||||
}
|
||||
|
||||
spriteBatch.Draw(texture, currDrawPosition,
|
||||
texPerspective, drawColor, rotation, Vector2.Zero, scale, effects, depth ?? this.depth);
|
||||
|
||||
currDrawPosition.Y += texPerspective.Height * scale.Y;
|
||||
}
|
||||
|
||||
currDrawPosition.X += texPerspective.Width * scale.X;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -57,6 +57,7 @@ namespace Barotrauma
|
||||
private SpriteEffects SpriteEffects = SpriteEffects.None;
|
||||
|
||||
private bool flippedX;
|
||||
private bool flippedY;
|
||||
|
||||
//sections of the wall that are supposed to be rendered
|
||||
public WallSection[] sections
|
||||
@@ -154,21 +155,7 @@ namespace Barotrauma
|
||||
get { return spriteColor; }
|
||||
set { spriteColor = value; }
|
||||
}
|
||||
|
||||
protected Vector2 textureScale = Vector2.One;
|
||||
[Editable, Serialize("1.0, 1.0", true)]
|
||||
public Vector2 TextureScale
|
||||
{
|
||||
get { return textureScale; }
|
||||
set
|
||||
{
|
||||
var v = value;
|
||||
v.X = MathHelper.Clamp(v.X, 0.01f, 10);
|
||||
v.Y = MathHelper.Clamp(v.Y, 0.01f, 10);
|
||||
textureScale = v;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public override Rectangle Rect
|
||||
{
|
||||
get
|
||||
@@ -848,9 +835,7 @@ namespace Barotrauma
|
||||
newBody.Friction = 0.5f;
|
||||
|
||||
newBody.OnCollision += OnWallCollision;
|
||||
|
||||
newBody.CollisionCategories = Physics.CollisionWall;
|
||||
|
||||
newBody.CollisionCategories = (prefab.Platform) ? Physics.CollisionPlatform : Physics.CollisionWall;
|
||||
newBody.UserData = this;
|
||||
|
||||
bodies.Add(newBody);
|
||||
@@ -894,8 +879,12 @@ namespace Barotrauma
|
||||
|
||||
CreateStairBodies();
|
||||
}
|
||||
|
||||
CreateSections();
|
||||
|
||||
if (HasBody)
|
||||
{
|
||||
CreateSections();
|
||||
UpdateSections();
|
||||
}
|
||||
}
|
||||
|
||||
public static void Load(XElement element, Submarine submarine)
|
||||
|
||||
@@ -16,6 +16,11 @@ namespace Barotrauma
|
||||
|
||||
static class MathUtils
|
||||
{
|
||||
public static int PositiveModulo(int i, int n)
|
||||
{
|
||||
return (i % n + n) % n;
|
||||
}
|
||||
|
||||
public static Vector2 SmoothStep(Vector2 v1, Vector2 v2, float amount)
|
||||
{
|
||||
return new Vector2(
|
||||
|
||||
Reference in New Issue
Block a user