Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Regalis11/Barotrauma into…
Browse files Browse the repository at this point in the history
… develop
  • Loading branch information
evilfactory committed Jan 11, 2024
2 parents 8b3d8ba + 27bceae commit 40beade
Show file tree
Hide file tree
Showing 20 changed files with 263 additions and 137 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ body:
label: Version
description: Which version of the game did the bug happen in? You can see the current version number in the bottom left corner of your screen in the main menu.
options:
- v1.2.7.0 (Winter Update hotfix)
- v1.2.8.0 (Winter Update hotfix 2)
- Other
validations:
required: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1421,10 +1421,27 @@ private static void DrawStructure(ISpriteBatch spriteBatch, Structure structure,
{
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static XElement Save(List<MapEntity> entities, string name, string descri
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)
{
Expand All @@ -99,6 +99,10 @@ public static XElement Save(List<MapEntity> entities, string name, string descri
}
}

//restore the previous selection
MapEntity.SelectedList.Clear();
entities.ForEach(e => MapEntity.AddSelection(e));

return element;
}
}
Expand Down
94 changes: 84 additions & 10 deletions Barotrauma/BarotraumaClient/ClientSource/Map/Structure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,72 @@ partial void InitProjSpecific()
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; }
Expand Down Expand Up @@ -112,8 +178,8 @@ private void SetLightTextureOffset()
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;
}
Expand All @@ -128,6 +194,16 @@ public GUIComponent CreateEditingHUD(bool inGame = false)
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)
{
Expand Down Expand Up @@ -334,8 +410,6 @@ private void Draw(SpriteBatch spriteBatch, bool editing, bool back = true, Effec
float depth = GetDrawDepth();

Vector2 textureOffset = this.textureOffset;
if (FlippedX) { textureOffset.X = -textureOffset.X; }
if (FlippedY) { textureOffset.Y = -textureOffset.Y; }

if (back && damageEffect == null && !isWiringMode)
{
Expand Down Expand Up @@ -365,8 +439,8 @@ private void Draw(SpriteBatch spriteBatch, bool editing, bool back = true, Effec
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,
Expand Down Expand Up @@ -442,11 +516,11 @@ private void Draw(SpriteBatch spriteBatch, bool editing, bool back = true, Effec
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();
Expand Down
15 changes: 11 additions & 4 deletions Barotrauma/BarotraumaClient/ClientSource/Map/SubmarinePreview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,12 +477,19 @@ private void BakeMapEntity(XElement element)
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,12 +583,23 @@ public GUIComponent CreateFloatField(ISerializableEntity entity, SerializablePro
}
};

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;
Expand All @@ -602,12 +613,6 @@ void HandleSetterModifyingInput(GUINumberInput numInput)
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)
Expand Down Expand Up @@ -881,26 +886,33 @@ public GUIComponent CreateVector2Field(ISerializableEntity entity, SerializableP
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 += () =>
Expand Down
Loading

0 comments on commit 40beade

Please sign in to comment.