Merge branch 'master' of https://github.com/Regalis11/Barotrauma into develop

This commit is contained in:
EvilFactory
2024-01-11 11:42:33 -03:00
20 changed files with 263 additions and 137 deletions
@@ -1421,10 +1421,27 @@ namespace Barotrauma.Items.Components
{
Sprite sprite = structure.Sprite;
if (sprite is null) { return; }
Vector2 textureOffset = structure.TextureOffset;
textureOffset = new Vector2(
MathUtils.PositiveModulo(-textureOffset.X, sprite.SourceRect.Width * structure.TextureScale.X * structure.Scale),
MathUtils.PositiveModulo(-textureOffset.Y, sprite.SourceRect.Height * structure.TextureScale.Y * structure.Scale));
RectangleF entityRect = ScaleRectToUI(structure, parent, border);
Vector2 spriteScale = new Vector2(entityRect.Size.X / sprite.size.X, entityRect.Size.Y / sprite.size.Y);
sprite.Draw(spriteBatch, new Vector2(entityRect.Location.X + inflate, entityRect.Location.Y + inflate), structure.SpriteColor, Vector2.Zero, 0f, spriteScale, sprite.effects ^ structure.SpriteEffects);
Vector2 spriteScale = new Vector2(entityRect.Size.X / structure.Rect.Width, entityRect.Size.Y / structure.Rect.Height);
float rotation = MathHelper.ToRadians(structure.Rotation);
sprite.DrawTiled(
spriteBatch: spriteBatch,
position: entityRect.Location + entityRect.Size * 0.5f + (inflate, inflate),
targetSize: entityRect.Size,
rotation: rotation,
origin: entityRect.Size * 0.5f,
color: structure.SpriteColor,
startOffset: textureOffset * spriteScale,
textureScale: structure.TextureScale * structure.Scale * spriteScale,
depth: structure.SpriteDepth,
spriteEffects: sprite.effects ^ structure.SpriteEffects);
}
private static RectangleF ScaleRectToUI(MapEntity entity, RectangleF parentRect, RectangleF worldBorders)
@@ -86,7 +86,7 @@ namespace Barotrauma
MathUtils.RoundTowardsClosest(center.Y, Submarine.GridSize.Y) - center.Y - Submarine.GridSize.Y / 2);
MapEntity.SelectedList.Clear();
entities.ForEach(e => MapEntity.AddSelection(e));
assemblyEntities.ForEach(e => MapEntity.AddSelection(e));
foreach (MapEntity mapEntity in assemblyEntities)
{
@@ -99,6 +99,10 @@ namespace Barotrauma
}
}
//restore the previous selection
MapEntity.SelectedList.Clear();
entities.ForEach(e => MapEntity.AddSelection(e));
return element;
}
}
@@ -51,6 +51,72 @@ namespace Barotrauma
UpdateSpriteStates(0.0f);
}
public static Vector2 UpgradeTextureOffset(
Vector2 targetSize,
Vector2 originalTextureOffset,
SubmarineInfo submarineInfo,
Rectangle sourceRect,
Vector2 scale,
bool flippedX,
bool flippedY)
{
if (submarineInfo.GameVersion <= Sprite.LastBrokenTiledSpriteGameVersion)
{
// Tiled sprite rendering was significantly changed after v1.2.3.0:
// Rendering flipped, scaled and offset textures was completely broken,
// but some existing community submarines depend on that old behavior,
// so let's redo some of the broken logic here if the sub is old enough
Vector2 flipper = (flippedX ? -1f : 1f, flippedY ? -1f : 1f);
var textureOffset = originalTextureOffset * flipper;
textureOffset = new Vector2(
MathUtils.PositiveModulo((int)-textureOffset.X, sourceRect.Width),
MathUtils.PositiveModulo((int)-textureOffset.Y, sourceRect.Height));
textureOffset.X = (textureOffset.X / scale.X) % sourceRect.Width;
textureOffset.Y = (textureOffset.Y / scale.Y) % sourceRect.Height;
Vector2 flippedDrawOffset = Vector2.Zero;
if (flippedX)
{
float diff = targetSize.X % (sourceRect.Width * scale.X);
flippedDrawOffset.X = (sourceRect.Width * scale.X - diff) / scale.X;
flippedDrawOffset.X =
MathUtils.NearlyEqual(flippedDrawOffset.X, MathF.Round(flippedDrawOffset.X)) ?
MathF.Round(flippedDrawOffset.X) : flippedDrawOffset.X;
}
if (flippedY)
{
float diff = targetSize.Y % (sourceRect.Height * scale.Y);
flippedDrawOffset.Y = (sourceRect.Height * scale.Y - diff) / scale.Y;
flippedDrawOffset.Y =
MathUtils.NearlyEqual(flippedDrawOffset.Y, MathF.Round(flippedDrawOffset.Y)) ?
MathF.Round(flippedDrawOffset.Y) : flippedDrawOffset.Y;
}
var textureOffsetPlusFlipBs = textureOffset + flippedDrawOffset;
if (textureOffsetPlusFlipBs.X > sourceRect.Width)
{
var diff = textureOffsetPlusFlipBs.X - sourceRect.Width;
textureOffset.X = (textureOffset.X + diff * (scale.X - 1f)) % sourceRect.Width;
}
if (textureOffsetPlusFlipBs.Y > sourceRect.Height)
{
var diff = textureOffsetPlusFlipBs.Y - sourceRect.Height;
textureOffset.Y = (textureOffset.Y + diff * (scale.Y - 1f)) % sourceRect.Height;
}
textureOffset *= scale * flipper;
return -textureOffset;
}
return originalTextureOffset;
}
partial void CreateConvexHull(Vector2 position, Vector2 size, float rotation)
{
if (!CastShadow) { return; }
@@ -112,8 +178,8 @@ namespace Barotrauma
foreach (LightSource light in Lights)
{
Vector2 bgOffset = new Vector2(
MathUtils.PositiveModulo((int)-textOffset.X, light.texture.Width),
MathUtils.PositiveModulo((int)-textOffset.Y, light.texture.Height));
MathUtils.PositiveModulo(-textOffset.X, light.texture.Width),
MathUtils.PositiveModulo(-textOffset.Y, light.texture.Height));
light.LightTextureOffset = bgOffset;
}
@@ -128,6 +194,16 @@ namespace Barotrauma
CanTakeKeyBoardFocus = false
};
var editor = new SerializableEntityEditor(listBox.Content.RectTransform, this, inGame, showName: true, titleFont: GUIStyle.LargeFont) { UserData = this };
if (editor.Fields.TryGetValue(nameof(Scale).ToIdentifier(), out GUIComponent[] scaleFields) &&
scaleFields.FirstOrDefault() is GUINumberInput scaleInput)
{
//texture offset needs to be adjusted when scaling the entity to keep the look of the entity unchanged
scaleInput.OnValueChanged += (GUINumberInput numberInput) =>
{
TextureOffset *= (Scale / ScaleWhenTextureOffsetSet);
};
}
if (Submarine.MainSub?.Info?.Type == SubmarineType.OutpostModule)
{
@@ -334,8 +410,6 @@ namespace Barotrauma
float depth = GetDrawDepth();
Vector2 textureOffset = this.textureOffset;
if (FlippedX) { textureOffset.X = -textureOffset.X; }
if (FlippedY) { textureOffset.Y = -textureOffset.Y; }
if (back && damageEffect == null && !isWiringMode)
{
@@ -365,8 +439,8 @@ namespace Barotrauma
Prefab.BackgroundSprite.effects ^= SpriteEffects;
Vector2 backGroundOffset = new Vector2(
MathUtils.PositiveModulo((int)-textureOffset.X, Prefab.BackgroundSprite.SourceRect.Width),
MathUtils.PositiveModulo((int)-textureOffset.Y, Prefab.BackgroundSprite.SourceRect.Height));
MathUtils.PositiveModulo(-textureOffset.X, Prefab.BackgroundSprite.SourceRect.Width * TextureScale.X * Scale),
MathUtils.PositiveModulo(-textureOffset.Y, Prefab.BackgroundSprite.SourceRect.Height * TextureScale.Y * Scale));
Prefab.BackgroundSprite.DrawTiled(
spriteBatch,
@@ -442,11 +516,11 @@ namespace Barotrauma
Math.Abs(rect.Location.X - drawSection.Location.X),
Math.Abs(rect.Location.Y - drawSection.Location.Y));
if (FlippedX && IsHorizontal) { sectionOffset.X = drawSection.Right - rect.Right; }
if (FlippedY && !IsHorizontal) { sectionOffset.Y = (rect.Y - rect.Height) - (drawSection.Y - drawSection.Height); }
if (FlippedX && IsHorizontal) { sectionOffset.X = rect.Right - drawSection.Right; }
if (FlippedY && !IsHorizontal) { sectionOffset.Y = (drawSection.Y - drawSection.Height) - (rect.Y - rect.Height); }
sectionOffset.X += MathUtils.PositiveModulo((int)-textureOffset.X, Prefab.Sprite.SourceRect.Width);
sectionOffset.Y += MathUtils.PositiveModulo((int)-textureOffset.Y, Prefab.Sprite.SourceRect.Height);
sectionOffset.X += MathUtils.PositiveModulo(-textureOffset.X, Prefab.Sprite.SourceRect.Width * TextureScale.X * Scale);
sectionOffset.Y += MathUtils.PositiveModulo(-textureOffset.Y, Prefab.Sprite.SourceRect.Height * TextureScale.Y * Scale);
Vector2 pos = new Vector2(drawSection.X, drawSection.Y);
pos -= rect.Location.ToVector2();
@@ -477,12 +477,19 @@ namespace Barotrauma
Vector2 backGroundOffset = Vector2.Zero;
Vector2 textureOffset = element.GetAttributeVector2("textureoffset", Vector2.Zero);
if (flippedX) { textureOffset.X = -textureOffset.X; }
if (flippedY) { textureOffset.Y = -textureOffset.Y; }
textureOffset = Structure.UpgradeTextureOffset(
targetSize: rect.Size.ToVector2(),
originalTextureOffset: textureOffset,
submarineInfo: submarineInfo,
sourceRect: prefab.Sprite.SourceRect,
scale: textureScale * scale,
flippedX: flippedX,
flippedY: flippedY);
backGroundOffset = new Vector2(
MathUtils.PositiveModulo((int)-textureOffset.X, prefab.Sprite.SourceRect.Width),
MathUtils.PositiveModulo((int)-textureOffset.Y, prefab.Sprite.SourceRect.Height));
MathUtils.PositiveModulo(-textureOffset.X, prefab.Sprite.SourceRect.Width * textureScale.X * scale),
MathUtils.PositiveModulo(-textureOffset.Y, prefab.Sprite.SourceRect.Height * textureScale.Y * scale));
prefab.Sprite.DrawTiled(
spriteBatch: spriteRecorder,
@@ -583,12 +583,23 @@ namespace Barotrauma
}
};
HandleSetterValueTampering(numberInput, () => property.GetFloatValue(entity));
refresh += () =>
{
if (!numberInput.TextBox.Selected) { numberInput.FloatValue = (float)property.GetValue(entity); }
};
if (!Fields.ContainsKey(property.Name)) { Fields.Add(property.Name.ToIdentifier(), new GUIComponent[] { numberInput }); }
return frame;
}
private static void HandleSetterValueTampering(GUINumberInput numberInput, Func<float> getter)
{
// Lots of UI boilerplate to handle all(?) cases where the property's setter may be called
// and modify the input value (e.g. rotation value wrapping)
void HandleSetterModifyingInput(GUINumberInput numInput)
{
var inputFloatValue = numInput.FloatValue;
var resultingFloatValue = property.GetFloatValue(entity);
var resultingFloatValue = getter();
if (!MathUtils.NearlyEqual(resultingFloatValue, inputFloatValue))
{
numInput.FloatValue = resultingFloatValue;
@@ -602,12 +613,6 @@ namespace Barotrauma
numberInput.PlusButton.OnClicked += HandleSetterModifyingInputOnButtonClicked;
numberInput.MinusButton.OnPressed += HandleSetterModifyingInputOnButtonPressed;
numberInput.MinusButton.OnClicked += HandleSetterModifyingInputOnButtonClicked;
refresh += () =>
{
if (!numberInput.TextBox.Selected) { numberInput.FloatValue = (float)property.GetValue(entity); }
};
if (!Fields.ContainsKey(property.Name)) { Fields.Add(property.Name.ToIdentifier(), new GUIComponent[] { numberInput }); }
return frame;
}
public GUIComponent CreateEnumField(ISerializableEntity entity, SerializableProperty property, object value, LocalizedString displayName, LocalizedString toolTip)
@@ -881,26 +886,33 @@ namespace Barotrauma
numberInput.MaxValueFloat = editableAttribute.MaxValueFloat;
numberInput.DecimalsToDisplay = editableAttribute.DecimalCount;
numberInput.ValueStep = editableAttribute.ValueStep;
numberInput.ForceShowPlusMinusButtons = editableAttribute.ForceShowPlusMinusButtons;
if (i == 0)
numberInput.FloatValue = value.X;
else
numberInput.FloatValue = value.Y;
numberInput.FloatValue = i == 0 ? value.X : value.Y;
int comp = i;
numberInput.OnValueChanged += (numInput) =>
{
Vector2 newVal = (Vector2)property.GetValue(entity);
if (comp == 0)
{
newVal.X = numInput.FloatValue;
}
else
{
newVal.Y = numInput.FloatValue;
}
if (SetPropertyValue(property, entity, newVal))
{
TrySendNetworkUpdate(entity, property);
}
};
HandleSetterValueTampering(numberInput, () =>
{
Vector2 currVal = (Vector2)property.GetValue(entity);
return comp == 0 ? currVal.X : currVal.Y;
});
fields[i] = numberInput;
}
refresh += () =>
@@ -284,6 +284,11 @@ namespace Barotrauma
}
}
/// <summary>
/// Last version of the game that had broken handling of sprites that were scaled, flipped and offset
/// </summary>
public static readonly Version LastBrokenTiledSpriteGameVersion = new Version(major: 1, minor: 2, build: 7, revision: 0);
public void DrawTiled(ISpriteBatch spriteBatch,
Vector2 position,
Vector2 targetSize,
@@ -298,8 +303,9 @@ namespace Barotrauma
if (Texture == null) { return; }
spriteEffects ??= effects;
bool flipHorizontal = (spriteEffects.Value & SpriteEffects.FlipHorizontally) != 0;
bool flipVertical = (spriteEffects.Value & SpriteEffects.FlipVertically) != 0;
bool flipHorizontal = spriteEffects.Value.HasFlag(SpriteEffects.FlipHorizontally);
bool flipVertical = spriteEffects.Value.HasFlag(SpriteEffects.FlipVertically);
float addedRotation = rotation + this.rotation;
if (flipHorizontal != flipVertical) { addedRotation = -addedRotation; }
@@ -317,42 +323,42 @@ namespace Barotrauma
void drawSection(Vector2 slicePos, Rectangle sliceRect)
{
Vector2 transformedPos = slicePos - position;
Vector2 transformedPos = slicePos;
if (flipHorizontal)
{
transformedPos.X = targetSize.X - transformedPos.X - sliceRect.Width * scale.X;
}
if (flipVertical)
{
transformedPos.Y = targetSize.Y - transformedPos.Y - sliceRect.Height * scale.Y;
}
transformedPos = advanceX * transformedPos.X + advanceY * transformedPos.Y;
transformedPos += position - transformedOrigin;
spriteBatch.Draw(texture, transformedPos, sliceRect, drawColor, addedRotation, Vector2.Zero, scale, spriteEffects.Value, depth ?? this.depth);
spriteBatch.Draw(
texture: texture,
position: transformedPos,
sourceRectangle: sliceRect,
color: drawColor,
rotation: addedRotation,
origin: Vector2.Zero,
scale: scale,
effects: spriteEffects.Value,
layerDepth: depth ?? this.depth);
}
//wrap the drawOffset inside the sourceRect
drawOffset.X = (drawOffset.X / scale.X) % sourceRect.Width;
drawOffset.Y = (drawOffset.Y / scale.Y) % sourceRect.Height;
Vector2 flippedDrawOffset = Vector2.Zero;
if (flipHorizontal)
{
float diff = targetSize.X % (sourceRect.Width * scale.X);
flippedDrawOffset.X = (sourceRect.Width * scale.X - diff) / scale.X;
flippedDrawOffset.X =
MathUtils.NearlyEqual(flippedDrawOffset.X, MathF.Round(flippedDrawOffset.X)) ?
MathF.Round(flippedDrawOffset.X) : flippedDrawOffset.X;
}
if (flipVertical)
{
float diff = targetSize.Y % (sourceRect.Height * scale.Y);
flippedDrawOffset.Y = (sourceRect.Height * scale.Y - diff) / scale.Y;
flippedDrawOffset.Y =
MathUtils.NearlyEqual(flippedDrawOffset.Y, MathF.Round(flippedDrawOffset.Y)) ?
MathF.Round(flippedDrawOffset.Y) : flippedDrawOffset.Y;
}
drawOffset += flippedDrawOffset;
//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));
//where the current tile is being drawn;
Vector2 currDrawPosition = position - drawOffset;
Vector2 currDrawPosition = -drawOffset;
//which part of the texture we are currently drawing
Rectangle texPerspective = sourceRect;
@@ -364,54 +370,22 @@ namespace Barotrauma
texPerspective.Height = sourceRect.Height;
//offset to the left, draw a partial slice
if (currDrawPosition.X < position.X)
if (currDrawPosition.X < 0)
{
float diff = (position.X - currDrawPosition.X);
float diff = -currDrawPosition.X;
currDrawPosition.X += diff;
texPerspective.Width -= (int)diff;
if (!flipHorizontal)
{
texPerspective.X += (int)diff;
}
if (!flipVertical)
{
texPerspective.Y += (int)diff;
}
}
//drawing an offset flipped sprite, need to draw an extra slice to the left side
if (currDrawPosition.X > position.X && x == 0)
{
if (flipHorizontal)
{
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 (flipVertical)
{
slicePos.Y += flippedDrawOffset.Y;
}
drawSection(slicePos, sliceRect);
currDrawPosition.X = slicePos.X + sliceWidth;
}
texPerspective.X += (int)diff;
}
//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);
int diff = (int)(((currDrawPosition.X + texPerspective.Width * scale.X) - targetSize.X) / scale.X);
texPerspective.Width -= diff;
if (flipHorizontal)
{
texPerspective.X += diff;
}
}
currDrawPosition.Y = position.Y - drawOffset.Y;
currDrawPosition.Y = -drawOffset.Y;
for (int y = 0; y < yTiles; y++)
{
@@ -419,45 +393,19 @@ namespace Barotrauma
texPerspective.Height = sourceRect.Height;
//offset above the top, draw a partial slice
if (currDrawPosition.Y < position.Y)
if (currDrawPosition.Y < 0f)
{
float diff = (position.Y - currDrawPosition.Y);
float diff = -currDrawPosition.Y;
currDrawPosition.Y += diff;
texPerspective.Height -= (int)diff;
if (!flipVertical)
{
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 (flipVertical)
{
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);
drawSection(slicePos, sliceRect);
currDrawPosition.Y = slicePos.Y + sliceHeight;
}
texPerspective.Y += (int)diff;
}
//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);
int diff = (int)(((currDrawPosition.Y + texPerspective.Height * scale.Y) - targetSize.Y) / scale.Y);
texPerspective.Height -= diff;
if (flipVertical)
{
texPerspective.Y += diff;
}
}
drawSection(currDrawPosition, texPerspective);